comparison src/getchar.c @ 5649:beb037a6c270 v7.4.171

updated for version 7.4.171 Problem: Redo does not set v:count and v:count1. Solution: Use a separate buffer for redo, so that we can set the counts when performing redo.
author Bram Moolenaar <bram@vim.org>
date Tue, 11 Feb 2014 15:10:43 +0100
parents 1cf89d38aa76
children cb5683bcde03
comparison
equal deleted inserted replaced
5648:e2140f8c3b20 5649:beb037a6c270
38 * Un-escaping is done by vgetc(). 38 * Un-escaping is done by vgetc().
39 */ 39 */
40 40
41 #define MINIMAL_SIZE 20 /* minimal size for b_str */ 41 #define MINIMAL_SIZE 20 /* minimal size for b_str */
42 42
43 static struct buffheader redobuff = {{NULL, {NUL}}, NULL, 0, 0}; 43 static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0};
44 static struct buffheader old_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; 44 static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
45 #if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO) 45 #if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
46 static struct buffheader save_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; 46 static buffheader_T save_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
47 static struct buffheader save_old_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; 47 static buffheader_T save_old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
48 #endif 48 #endif
49 static struct buffheader recordbuff = {{NULL, {NUL}}, NULL, 0, 0}; 49 static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
50 50
51 static int typeahead_char = 0; /* typeahead char that's not flushed */ 51 static int typeahead_char = 0; /* typeahead char that's not flushed */
52 52
53 /* 53 /*
54 * when block_redo is TRUE redo buffer will not be changed 54 * when block_redo is TRUE redo buffer will not be changed
110 static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf.tb_buf */ 110 static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf.tb_buf */
111 static char_u noremapbuf_init[TYPELEN_INIT]; /* initial typebuf.tb_noremap */ 111 static char_u noremapbuf_init[TYPELEN_INIT]; /* initial typebuf.tb_noremap */
112 112
113 static int last_recorded_len = 0; /* number of last recorded chars */ 113 static int last_recorded_len = 0; /* number of last recorded chars */
114 114
115 static char_u *get_buffcont __ARGS((struct buffheader *, int)); 115 static char_u *get_buffcont __ARGS((buffheader_T *, int));
116 static void add_buff __ARGS((struct buffheader *, char_u *, long n)); 116 static void add_buff __ARGS((buffheader_T *, char_u *, long n));
117 static void add_num_buff __ARGS((struct buffheader *, long)); 117 static void add_num_buff __ARGS((buffheader_T *, long));
118 static void add_char_buff __ARGS((struct buffheader *, int)); 118 static void add_char_buff __ARGS((buffheader_T *, int));
119 static int read_stuff __ARGS((int advance)); 119 static int read_readbuffers __ARGS((int advance));
120 static int read_readbuf __ARGS((buffheader_T *buf, int advance));
120 static void start_stuff __ARGS((void)); 121 static void start_stuff __ARGS((void));
121 static int read_redo __ARGS((int, int)); 122 static int read_redo __ARGS((int, int));
122 static void copy_redo __ARGS((int)); 123 static void copy_redo __ARGS((int));
123 static void init_typebuf __ARGS((void)); 124 static void init_typebuf __ARGS((void));
124 static void gotchars __ARGS((char_u *, int)); 125 static void gotchars __ARGS((char_u *, int));
135 /* 136 /*
136 * Free and clear a buffer. 137 * Free and clear a buffer.
137 */ 138 */
138 void 139 void
139 free_buff(buf) 140 free_buff(buf)
140 struct buffheader *buf; 141 buffheader_T *buf;
141 { 142 {
142 struct buffblock *p, *np; 143 buffblock_T *p, *np;
143 144
144 for (p = buf->bh_first.b_next; p != NULL; p = np) 145 for (p = buf->bh_first.b_next; p != NULL; p = np)
145 { 146 {
146 np = p->b_next; 147 np = p->b_next;
147 vim_free(p); 148 vim_free(p);
153 * Return the contents of a buffer as a single string. 154 * Return the contents of a buffer as a single string.
154 * K_SPECIAL and CSI in the returned string are escaped. 155 * K_SPECIAL and CSI in the returned string are escaped.
155 */ 156 */
156 static char_u * 157 static char_u *
157 get_buffcont(buffer, dozero) 158 get_buffcont(buffer, dozero)
158 struct buffheader *buffer; 159 buffheader_T *buffer;
159 int dozero; /* count == zero is not an error */ 160 int dozero; /* count == zero is not an error */
160 { 161 {
161 long_u count = 0; 162 long_u count = 0;
162 char_u *p = NULL; 163 char_u *p = NULL;
163 char_u *p2; 164 char_u *p2;
164 char_u *str; 165 char_u *str;
165 struct buffblock *bp; 166 buffblock_T *bp;
166 167
167 /* compute the total length of the string */ 168 /* compute the total length of the string */
168 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next) 169 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
169 count += (long_u)STRLEN(bp->b_str); 170 count += (long_u)STRLEN(bp->b_str);
170 171
228 * Add string "s" after the current block of buffer "buf". 229 * Add string "s" after the current block of buffer "buf".
229 * K_SPECIAL and CSI should have been escaped already. 230 * K_SPECIAL and CSI should have been escaped already.
230 */ 231 */
231 static void 232 static void
232 add_buff(buf, s, slen) 233 add_buff(buf, s, slen)
233 struct buffheader *buf; 234 buffheader_T *buf;
234 char_u *s; 235 char_u *s;
235 long slen; /* length of "s" or -1 */ 236 long slen; /* length of "s" or -1 */
236 { 237 {
237 struct buffblock *p; 238 buffblock_T *p;
238 long_u len; 239 long_u len;
239 240
240 if (slen < 0) 241 if (slen < 0)
241 slen = (long)STRLEN(s); 242 slen = (long)STRLEN(s);
242 if (slen == 0) /* don't add empty strings */ 243 if (slen == 0) /* don't add empty strings */
268 { 269 {
269 if (slen < MINIMAL_SIZE) 270 if (slen < MINIMAL_SIZE)
270 len = MINIMAL_SIZE; 271 len = MINIMAL_SIZE;
271 else 272 else
272 len = slen; 273 len = slen;
273 p = (struct buffblock *)lalloc((long_u)(sizeof(struct buffblock) + len), 274 p = (buffblock_T *)lalloc((long_u)(sizeof(buffblock_T) + len),
274 TRUE); 275 TRUE);
275 if (p == NULL) 276 if (p == NULL)
276 return; /* no space, just forget it */ 277 return; /* no space, just forget it */
277 buf->bh_space = (int)(len - slen); 278 buf->bh_space = (int)(len - slen);
278 vim_strncpy(p->b_str, s, (size_t)slen); 279 vim_strncpy(p->b_str, s, (size_t)slen);
287 /* 288 /*
288 * Add number "n" to buffer "buf". 289 * Add number "n" to buffer "buf".
289 */ 290 */
290 static void 291 static void
291 add_num_buff(buf, n) 292 add_num_buff(buf, n)
292 struct buffheader *buf; 293 buffheader_T *buf;
293 long n; 294 long n;
294 { 295 {
295 char_u number[32]; 296 char_u number[32];
296 297
297 sprintf((char *)number, "%ld", n); 298 sprintf((char *)number, "%ld", n);
302 * Add character 'c' to buffer "buf". 303 * Add character 'c' to buffer "buf".
303 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. 304 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
304 */ 305 */
305 static void 306 static void
306 add_char_buff(buf, c) 307 add_char_buff(buf, c)
307 struct buffheader *buf; 308 buffheader_T *buf;
308 int c; 309 int c;
309 { 310 {
310 #ifdef FEAT_MBYTE 311 #ifdef FEAT_MBYTE
311 char_u bytes[MB_MAXBYTES + 1]; 312 char_u bytes[MB_MAXBYTES + 1];
312 int len; 313 int len;
352 #ifdef FEAT_MBYTE 353 #ifdef FEAT_MBYTE
353 } 354 }
354 #endif 355 #endif
355 } 356 }
356 357
357 /* 358 /* First read ahead buffer. Used for translated commands. */
358 * Get one byte from the stuff buffer. 359 static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
360
361 /* Second read ahead buffer. Used for redo. */
362 static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
363
364 /*
365 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
366 * if that one is empty.
359 * If advance == TRUE go to the next char. 367 * If advance == TRUE go to the next char.
360 * No translation is done K_SPECIAL and CSI are escaped. 368 * No translation is done K_SPECIAL and CSI are escaped.
361 */ 369 */
362 static int 370 static int
363 read_stuff(advance) 371 read_readbuffers(advance)
364 int advance; 372 int advance;
365 { 373 {
366 char_u c; 374 int c;
367 struct buffblock *curr; 375
368 376 c = read_readbuf(&readbuf1, advance);
369 if (stuffbuff.bh_first.b_next == NULL) /* buffer is empty */ 377 if (c == NUL)
378 c = read_readbuf(&readbuf2, advance);
379 return c;
380 }
381
382 static int
383 read_readbuf(buf, advance)
384 buffheader_T *buf;
385 int advance;
386 {
387 char_u c;
388 buffblock_T *curr;
389
390 if (buf->bh_first.b_next == NULL) /* buffer is empty */
370 return NUL; 391 return NUL;
371 392
372 curr = stuffbuff.bh_first.b_next; 393 curr = buf->bh_first.b_next;
373 c = curr->b_str[stuffbuff.bh_index]; 394 c = curr->b_str[buf->bh_index];
374 395
375 if (advance) 396 if (advance)
376 { 397 {
377 if (curr->b_str[++stuffbuff.bh_index] == NUL) 398 if (curr->b_str[++buf->bh_index] == NUL)
378 { 399 {
379 stuffbuff.bh_first.b_next = curr->b_next; 400 buf->bh_first.b_next = curr->b_next;
380 vim_free(curr); 401 vim_free(curr);
381 stuffbuff.bh_index = 0; 402 buf->bh_index = 0;
382 } 403 }
383 } 404 }
384 return c; 405 return c;
385 } 406 }
386 407
387 /* 408 /*
388 * Prepare the stuff buffer for reading (if it contains something). 409 * Prepare the read buffers for reading (if they contains something).
389 */ 410 */
390 static void 411 static void
391 start_stuff() 412 start_stuff()
392 { 413 {
393 if (stuffbuff.bh_first.b_next != NULL) 414 if (readbuf1.bh_first.b_next != NULL)
394 { 415 {
395 stuffbuff.bh_curr = &(stuffbuff.bh_first); 416 readbuf1.bh_curr = &(readbuf1.bh_first);
396 stuffbuff.bh_space = 0; 417 readbuf1.bh_space = 0;
418 }
419 if (readbuf2.bh_first.b_next != NULL)
420 {
421 readbuf2.bh_curr = &(readbuf2.bh_first);
422 readbuf2.bh_space = 0;
397 } 423 }
398 } 424 }
399 425
400 /* 426 /*
401 * Return TRUE if the stuff buffer is empty. 427 * Return TRUE if the stuff buffer is empty.
402 */ 428 */
403 int 429 int
404 stuff_empty() 430 stuff_empty()
405 { 431 {
406 return (stuffbuff.bh_first.b_next == NULL); 432 return (readbuf1.bh_first.b_next == NULL
433 && readbuf2.bh_first.b_next == NULL);
434 }
435
436 /*
437 * Return TRUE if readbuf1 is empty. There may still be redo characters in
438 * redbuf2.
439 */
440 int
441 readbuf1_empty()
442 {
443 return (readbuf1.bh_first.b_next == NULL);
407 } 444 }
408 445
409 /* 446 /*
410 * Set a typeahead character that won't be flushed. 447 * Set a typeahead character that won't be flushed.
411 */ 448 */
426 int flush_typeahead; 463 int flush_typeahead;
427 { 464 {
428 init_typebuf(); 465 init_typebuf();
429 466
430 start_stuff(); 467 start_stuff();
431 while (read_stuff(TRUE) != NUL) 468 while (read_readbuffers(TRUE) != NUL)
432 ; 469 ;
433 470
434 if (flush_typeahead) /* remove all typeahead */ 471 if (flush_typeahead) /* remove all typeahead */
435 { 472 {
436 /* 473 /*
481 { 518 {
482 free_buff(&redobuff); 519 free_buff(&redobuff);
483 redobuff = old_redobuff; 520 redobuff = old_redobuff;
484 old_redobuff.bh_first.b_next = NULL; 521 old_redobuff.bh_first.b_next = NULL;
485 start_stuff(); 522 start_stuff();
486 while (read_stuff(TRUE) != NUL) 523 while (read_readbuffers(TRUE) != NUL)
487 ; 524 ;
488 } 525 }
489 } 526 }
490 527
491 #if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO) 528 #if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
636 */ 673 */
637 void 674 void
638 stuffReadbuff(s) 675 stuffReadbuff(s)
639 char_u *s; 676 char_u *s;
640 { 677 {
641 add_buff(&stuffbuff, s, -1L); 678 add_buff(&readbuf1, s, -1L);
642 } 679 }
643 680
644 void 681 void
645 stuffReadbuffLen(s, len) 682 stuffReadbuffLen(s, len)
646 char_u *s; 683 char_u *s;
647 long len; 684 long len;
648 { 685 {
649 add_buff(&stuffbuff, s, len); 686 add_buff(&readbuf1, s, len);
650 } 687 }
651 688
652 #if defined(FEAT_EVAL) || defined(PROTO) 689 #if defined(FEAT_EVAL) || defined(PROTO)
653 /* 690 /*
654 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and 691 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
690 */ 727 */
691 void 728 void
692 stuffcharReadbuff(c) 729 stuffcharReadbuff(c)
693 int c; 730 int c;
694 { 731 {
695 add_char_buff(&stuffbuff, c); 732 add_char_buff(&readbuf1, c);
696 } 733 }
697 734
698 /* 735 /*
699 * Append a number to the stuff buffer. 736 * Append a number to the stuff buffer.
700 */ 737 */
701 void 738 void
702 stuffnumReadbuff(n) 739 stuffnumReadbuff(n)
703 long n; 740 long n;
704 { 741 {
705 add_num_buff(&stuffbuff, n); 742 add_num_buff(&readbuf1, n);
706 } 743 }
707 744
708 /* 745 /*
709 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and 746 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
710 * multibyte characters. 747 * multibyte characters.
716 static int 753 static int
717 read_redo(init, old_redo) 754 read_redo(init, old_redo)
718 int init; 755 int init;
719 int old_redo; 756 int old_redo;
720 { 757 {
721 static struct buffblock *bp; 758 static buffblock_T *bp;
722 static char_u *p; 759 static char_u *p;
723 int c; 760 int c;
724 #ifdef FEAT_MBYTE 761 #ifdef FEAT_MBYTE
725 int n; 762 int n;
726 char_u buf[MB_MAXBYTES + 1]; 763 char_u buf[MB_MAXBYTES + 1];
727 int i; 764 int i;
728 #endif 765 #endif
729 766
730 if (init) 767 if (init)
731 { 768 {
732 if (old_redo) 769 if (old_redo)
793 int old_redo; 830 int old_redo;
794 { 831 {
795 int c; 832 int c;
796 833
797 while ((c = read_redo(FALSE, old_redo)) != NUL) 834 while ((c = read_redo(FALSE, old_redo)) != NUL)
798 stuffcharReadbuff(c); 835 add_char_buff(&readbuf2, c);
799 } 836 }
800 837
801 /* 838 /*
802 * Stuff the redo buffer into the stuffbuff. 839 * Stuff the redo buffer into readbuf2.
803 * Insert the redo count into the command. 840 * Insert the redo count into the command.
804 * If "old_redo" is TRUE, the last but one command is repeated 841 * If "old_redo" is TRUE, the last but one command is repeated
805 * instead of the last command (inserting text). This is used for 842 * instead of the last command (inserting text). This is used for
806 * CTRL-O <.> in insert mode 843 * CTRL-O <.> in insert mode
807 * 844 *
821 c = read_redo(FALSE, old_redo); 858 c = read_redo(FALSE, old_redo);
822 859
823 /* copy the buffer name, if present */ 860 /* copy the buffer name, if present */
824 if (c == '"') 861 if (c == '"')
825 { 862 {
826 add_buff(&stuffbuff, (char_u *)"\"", 1L); 863 add_buff(&readbuf2, (char_u *)"\"", 1L);
827 c = read_redo(FALSE, old_redo); 864 c = read_redo(FALSE, old_redo);
828 865
829 /* if a numbered buffer is used, increment the number */ 866 /* if a numbered buffer is used, increment the number */
830 if (c >= '1' && c < '9') 867 if (c >= '1' && c < '9')
831 ++c; 868 ++c;
832 add_char_buff(&stuffbuff, c); 869 add_char_buff(&readbuf2, c);
833 c = read_redo(FALSE, old_redo); 870 c = read_redo(FALSE, old_redo);
834 } 871 }
835 872
836 #ifdef FEAT_VISUAL 873 #ifdef FEAT_VISUAL
837 if (c == 'v') /* redo Visual */ 874 if (c == 'v') /* redo Visual */
848 /* try to enter the count (in place of a previous count) */ 885 /* try to enter the count (in place of a previous count) */
849 if (count) 886 if (count)
850 { 887 {
851 while (VIM_ISDIGIT(c)) /* skip "old" count */ 888 while (VIM_ISDIGIT(c)) /* skip "old" count */
852 c = read_redo(FALSE, old_redo); 889 c = read_redo(FALSE, old_redo);
853 add_num_buff(&stuffbuff, count); 890 add_num_buff(&readbuf2, count);
854 } 891 }
855 892
856 /* copy from the redo buffer into the stuff buffer */ 893 /* copy from the redo buffer into the stuff buffer */
857 add_char_buff(&stuffbuff, c); 894 add_char_buff(&readbuf2, c);
858 copy_redo(old_redo); 895 copy_redo(old_redo);
859 return OK; 896 return OK;
860 } 897 }
861 898
862 /* 899 /*
863 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing 900 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
864 * the redo buffer into the stuffbuff. 901 * the redo buffer into readbuf2.
865 * return FAIL for failure, OK otherwise 902 * return FAIL for failure, OK otherwise
866 */ 903 */
867 int 904 int
868 start_redo_ins() 905 start_redo_ins()
869 { 906 {
877 while ((c = read_redo(FALSE, FALSE)) != NUL) 914 while ((c = read_redo(FALSE, FALSE)) != NUL)
878 { 915 {
879 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL) 916 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
880 { 917 {
881 if (c == 'O' || c == 'o') 918 if (c == 'O' || c == 'o')
882 stuffReadbuff(NL_STR); 919 add_buff(&readbuf2, NL_STR, -1L);
883 break; 920 break;
884 } 921 }
885 } 922 }
886 923
887 /* copy the typed text from the redo buffer into the stuff buffer */ 924 /* copy the typed text from the redo buffer into the stuff buffer */
1358 1395
1359 tp->old_char = old_char; 1396 tp->old_char = old_char;
1360 tp->old_mod_mask = old_mod_mask; 1397 tp->old_mod_mask = old_mod_mask;
1361 old_char = -1; 1398 old_char = -1;
1362 1399
1363 tp->save_stuffbuff = stuffbuff; 1400 tp->save_readbuf1 = readbuf1;
1364 stuffbuff.bh_first.b_next = NULL; 1401 readbuf1.bh_first.b_next = NULL;
1402 tp->save_readbuf2 = readbuf2;
1403 readbuf2.bh_first.b_next = NULL;
1365 # ifdef USE_INPUT_BUF 1404 # ifdef USE_INPUT_BUF
1366 tp->save_inputbuf = get_input_buf(); 1405 tp->save_inputbuf = get_input_buf();
1367 # endif 1406 # endif
1368 } 1407 }
1369 1408
1382 } 1421 }
1383 1422
1384 old_char = tp->old_char; 1423 old_char = tp->old_char;
1385 old_mod_mask = tp->old_mod_mask; 1424 old_mod_mask = tp->old_mod_mask;
1386 1425
1387 free_buff(&stuffbuff); 1426 free_buff(&readbuf1);
1388 stuffbuff = tp->save_stuffbuff; 1427 readbuf1 = tp->save_readbuf1;
1428 free_buff(&readbuf2);
1429 readbuf2 = tp->save_readbuf2;
1389 # ifdef USE_INPUT_BUF 1430 # ifdef USE_INPUT_BUF
1390 set_input_buf(tp->save_inputbuf); 1431 set_input_buf(tp->save_inputbuf);
1391 # endif 1432 # endif
1392 } 1433 }
1393 #endif 1434 #endif
1990 c = typeahead_char; 2031 c = typeahead_char;
1991 if (advance) 2032 if (advance)
1992 typeahead_char = 0; 2033 typeahead_char = 0;
1993 } 2034 }
1994 else 2035 else
1995 c = read_stuff(advance); 2036 c = read_readbuffers(advance);
1996 if (c != NUL && !got_int) 2037 if (c != NUL && !got_int)
1997 { 2038 {
1998 if (advance) 2039 if (advance)
1999 { 2040 {
2000 /* KeyTyped = FALSE; When the command that stuffed something 2041 /* KeyTyped = FALSE; When the command that stuffed something