forked from vim/vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getchar.c
4050 lines (3685 loc) · 101 KB
/
getchar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* getchar.c: Code related to getting a character from the user or a script
* file, manipulations with redo buffer and stuff buffer.
*/
#include "vim.h"
/*
* These buffers are used for storing:
* - stuffed characters: A command that is translated into another command.
* - redo characters: will redo the last change.
* - recorded characters: for the "q" command.
*
* The bytes are stored like in the typeahead buffer:
* - K_SPECIAL introduces a special key (two more bytes follow). A literal
* K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
* - CSI introduces a GUI termcap code (also when gui.in_use is FALSE,
* otherwise switching the GUI on would make mappings invalid).
* A literal CSI is stored as CSI KS_EXTRA KE_CSI.
* These translations are also done on multi-byte characters!
*
* Escaping CSI bytes is done by the system-specific input functions, called
* by ui_inchar().
* Escaping K_SPECIAL is done by inchar().
* Un-escaping is done by vgetc().
*/
#define MINIMAL_SIZE 20 // minimal size for b_str
static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0};
static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
static int typeahead_char = 0; // typeahead char that's not flushed
/*
* when block_redo is TRUE redo buffer will not be changed
* used by edit() to repeat insertions and 'V' command for redoing
*/
static int block_redo = FALSE;
static int KeyNoremap = 0; // remapping flags
/*
* Variables used by vgetorpeek() and flush_buffers().
*
* typebuf.tb_buf[] contains all characters that are not consumed yet.
* typebuf.tb_buf[typebuf.tb_off] is the first valid character.
* typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
* typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
* The head of the buffer may contain the result of mappings, abbreviations
* and @a commands. The length of this part is typebuf.tb_maplen.
* typebuf.tb_silent is the part where <silent> applies.
* After the head are characters that come from the terminal.
* typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
* should not be considered for abbreviations.
* Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
* in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
* contains RM_NONE for the characters that are not to be remapped.
* typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
* (typebuf has been put in globals.h, because check_termcode() needs it).
*/
#define RM_YES 0 // tb_noremap: remap
#define RM_NONE 1 // tb_noremap: don't remap
#define RM_SCRIPT 2 // tb_noremap: remap local script mappings
#define RM_ABBR 4 // tb_noremap: don't remap, do abbrev.
// typebuf.tb_buf has three parts: room in front (for result of mappings), the
// middle for typeahead and room for new characters (which needs to be 3 *
// MAXMAPLEN for the Amiga).
#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
static char_u typebuf_init[TYPELEN_INIT]; // initial typebuf.tb_buf
static char_u noremapbuf_init[TYPELEN_INIT]; // initial typebuf.tb_noremap
static int last_recorded_len = 0; // number of last recorded chars
#ifdef FEAT_EVAL
mapblock_T *last_used_map = NULL;
int last_used_sid = -1;
#endif
static int read_readbuf(buffheader_T *buf, int advance);
static void init_typebuf(void);
static void may_sync_undo(void);
static void free_typebuf(void);
static void closescript(void);
static void updatescript(int c);
static int vgetorpeek(int);
static int inchar(char_u *buf, int maxlen, long wait_time);
/*
* Free and clear a buffer.
*/
static void
free_buff(buffheader_T *buf)
{
buffblock_T *p, *np;
for (p = buf->bh_first.b_next; p != NULL; p = np)
{
np = p->b_next;
vim_free(p);
}
buf->bh_first.b_next = NULL;
buf->bh_curr = NULL;
}
/*
* Return the contents of a buffer as a single string.
* K_SPECIAL and CSI in the returned string are escaped.
*/
static char_u *
get_buffcont(
buffheader_T *buffer,
int dozero) // count == zero is not an error
{
long_u count = 0;
char_u *p = NULL;
char_u *p2;
char_u *str;
buffblock_T *bp;
// compute the total length of the string
for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
count += (long_u)STRLEN(bp->b_str);
if ((count || dozero) && (p = alloc(count + 1)) != NULL)
{
p2 = p;
for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
for (str = bp->b_str; *str; )
*p2++ = *str++;
*p2 = NUL;
}
return (p);
}
/*
* Return the contents of the record buffer as a single string
* and clear the record buffer.
* K_SPECIAL and CSI in the returned string are escaped.
*/
char_u *
get_recorded(void)
{
char_u *p;
size_t len;
p = get_buffcont(&recordbuff, TRUE);
free_buff(&recordbuff);
/*
* Remove the characters that were added the last time, these must be the
* (possibly mapped) characters that stopped the recording.
*/
len = STRLEN(p);
if ((int)len >= last_recorded_len)
{
len -= last_recorded_len;
p[len] = NUL;
}
/*
* When stopping recording from Insert mode with CTRL-O q, also remove the
* CTRL-O.
*/
if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
p[len - 1] = NUL;
return (p);
}
/*
* Return the contents of the redo buffer as a single string.
* K_SPECIAL and CSI in the returned string are escaped.
*/
char_u *
get_inserted(void)
{
return get_buffcont(&redobuff, FALSE);
}
/*
* Add string "s" after the current block of buffer "buf".
* K_SPECIAL and CSI should have been escaped already.
*/
static void
add_buff(
buffheader_T *buf,
char_u *s,
long slen) // length of "s" or -1
{
buffblock_T *p;
long_u len;
if (slen < 0)
slen = (long)STRLEN(s);
if (slen == 0) // don't add empty strings
return;
if (buf->bh_first.b_next == NULL) // first add to list
{
buf->bh_space = 0;
buf->bh_curr = &(buf->bh_first);
}
else if (buf->bh_curr == NULL) // buffer has already been read
{
iemsg(_(e_add_to_internal_buffer_that_was_already_read_from));
return;
}
else if (buf->bh_index != 0)
mch_memmove(buf->bh_first.b_next->b_str,
buf->bh_first.b_next->b_str + buf->bh_index,
STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
buf->bh_index = 0;
if (buf->bh_space >= (int)slen)
{
len = (long_u)STRLEN(buf->bh_curr->b_str);
vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
buf->bh_space -= slen;
}
else
{
if (slen < MINIMAL_SIZE)
len = MINIMAL_SIZE;
else
len = slen;
p = alloc(offsetof(buffblock_T, b_str) + len + 1);
if (p == NULL)
return; // no space, just forget it
buf->bh_space = (int)(len - slen);
vim_strncpy(p->b_str, s, (size_t)slen);
p->b_next = buf->bh_curr->b_next;
buf->bh_curr->b_next = p;
buf->bh_curr = p;
}
}
/*
* Delete "slen" bytes from the end of "buf".
* Only works when it was just added.
*/
static void
delete_buff_tail(buffheader_T *buf, int slen)
{
int len;
if (buf->bh_curr == NULL)
return; // nothing to delete
len = (int)STRLEN(buf->bh_curr->b_str);
if (len < slen)
return;
buf->bh_curr->b_str[len - slen] = NUL;
buf->bh_space += slen;
}
/*
* Add number "n" to buffer "buf".
*/
static void
add_num_buff(buffheader_T *buf, long n)
{
char_u number[32];
sprintf((char *)number, "%ld", n);
add_buff(buf, number, -1L);
}
/*
* Add character 'c' to buffer "buf".
* Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
*/
static void
add_char_buff(buffheader_T *buf, int c)
{
char_u bytes[MB_MAXBYTES + 1];
int len;
int i;
char_u temp[4];
if (IS_SPECIAL(c))
len = 1;
else
len = (*mb_char2bytes)(c, bytes);
for (i = 0; i < len; ++i)
{
if (!IS_SPECIAL(c))
c = bytes[i];
if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
{
// translate special key code into three byte sequence
temp[0] = K_SPECIAL;
temp[1] = K_SECOND(c);
temp[2] = K_THIRD(c);
temp[3] = NUL;
}
#ifdef FEAT_GUI
else if (c == CSI)
{
// Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
temp[0] = CSI;
temp[1] = KS_EXTRA;
temp[2] = (int)KE_CSI;
temp[3] = NUL;
}
#endif
else
{
temp[0] = c;
temp[1] = NUL;
}
add_buff(buf, temp, -1L);
}
}
// First read ahead buffer. Used for translated commands.
static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
// Second read ahead buffer. Used for redo.
static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
/*
* Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
* if that one is empty.
* If advance == TRUE go to the next char.
* No translation is done K_SPECIAL and CSI are escaped.
*/
static int
read_readbuffers(int advance)
{
int c;
c = read_readbuf(&readbuf1, advance);
if (c == NUL)
c = read_readbuf(&readbuf2, advance);
return c;
}
static int
read_readbuf(buffheader_T *buf, int advance)
{
char_u c;
buffblock_T *curr;
if (buf->bh_first.b_next == NULL) // buffer is empty
return NUL;
curr = buf->bh_first.b_next;
c = curr->b_str[buf->bh_index];
if (advance)
{
if (curr->b_str[++buf->bh_index] == NUL)
{
buf->bh_first.b_next = curr->b_next;
vim_free(curr);
buf->bh_index = 0;
}
}
return c;
}
/*
* Prepare the read buffers for reading (if they contain something).
*/
static void
start_stuff(void)
{
if (readbuf1.bh_first.b_next != NULL)
{
readbuf1.bh_curr = &(readbuf1.bh_first);
readbuf1.bh_space = 0;
}
if (readbuf2.bh_first.b_next != NULL)
{
readbuf2.bh_curr = &(readbuf2.bh_first);
readbuf2.bh_space = 0;
}
}
/*
* Return TRUE if the stuff buffer is empty.
*/
int
stuff_empty(void)
{
return (readbuf1.bh_first.b_next == NULL
&& readbuf2.bh_first.b_next == NULL);
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Return TRUE if readbuf1 is empty. There may still be redo characters in
* redbuf2.
*/
int
readbuf1_empty(void)
{
return (readbuf1.bh_first.b_next == NULL);
}
#endif
/*
* Set a typeahead character that won't be flushed.
*/
void
typeahead_noflush(int c)
{
typeahead_char = c;
}
/*
* Remove the contents of the stuff buffer and the mapped characters in the
* typeahead buffer (used in case of an error). If "flush_typeahead" is true,
* flush all typeahead characters (used when interrupted by a CTRL-C).
*/
void
flush_buffers(flush_buffers_T flush_typeahead)
{
init_typebuf();
start_stuff();
while (read_readbuffers(TRUE) != NUL)
;
if (flush_typeahead == FLUSH_MINIMAL)
{
// remove mapped characters at the start only
typebuf.tb_off += typebuf.tb_maplen;
typebuf.tb_len -= typebuf.tb_maplen;
#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
if (typebuf.tb_len == 0)
typebuf_was_filled = FALSE;
#endif
}
else
{
// remove typeahead
if (flush_typeahead == FLUSH_INPUT)
// We have to get all characters, because we may delete the first
// part of an escape sequence. In an xterm we get one char at a
// time and we have to get them all.
while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
;
typebuf.tb_off = MAXMAPLEN;
typebuf.tb_len = 0;
#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
// Reset the flag that text received from a client or from feedkeys()
// was inserted in the typeahead buffer.
typebuf_was_filled = FALSE;
#endif
}
typebuf.tb_maplen = 0;
typebuf.tb_silent = 0;
cmd_silent = FALSE;
typebuf.tb_no_abbr_cnt = 0;
if (++typebuf.tb_change_cnt == 0)
typebuf.tb_change_cnt = 1;
}
/*
* The previous contents of the redo buffer is kept in old_redobuffer.
* This is used for the CTRL-O <.> command in insert mode.
*/
void
ResetRedobuff(void)
{
if (block_redo)
return;
free_buff(&old_redobuff);
old_redobuff = redobuff;
redobuff.bh_first.b_next = NULL;
}
/*
* Discard the contents of the redo buffer and restore the previous redo
* buffer.
*/
void
CancelRedo(void)
{
if (block_redo)
return;
free_buff(&redobuff);
redobuff = old_redobuff;
old_redobuff.bh_first.b_next = NULL;
start_stuff();
while (read_readbuffers(TRUE) != NUL)
;
}
/*
* Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
* Used before executing autocommands and user functions.
*/
void
saveRedobuff(save_redo_T *save_redo)
{
char_u *s;
save_redo->sr_redobuff = redobuff;
redobuff.bh_first.b_next = NULL;
save_redo->sr_old_redobuff = old_redobuff;
old_redobuff.bh_first.b_next = NULL;
// Make a copy, so that ":normal ." in a function works.
s = get_buffcont(&save_redo->sr_redobuff, FALSE);
if (s == NULL)
return;
add_buff(&redobuff, s, -1L);
vim_free(s);
}
/*
* Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
* Used after executing autocommands and user functions.
*/
void
restoreRedobuff(save_redo_T *save_redo)
{
free_buff(&redobuff);
redobuff = save_redo->sr_redobuff;
free_buff(&old_redobuff);
old_redobuff = save_redo->sr_old_redobuff;
}
/*
* Append "s" to the redo buffer.
* K_SPECIAL and CSI should already have been escaped.
*/
void
AppendToRedobuff(char_u *s)
{
if (!block_redo)
add_buff(&redobuff, s, -1L);
}
/*
* Append to Redo buffer literally, escaping special characters with CTRL-V.
* K_SPECIAL and CSI are escaped as well.
*/
void
AppendToRedobuffLit(
char_u *str,
int len) // length of "str" or -1 for up to the NUL
{
char_u *s = str;
int c;
char_u *start;
if (block_redo)
return;
while (len < 0 ? *s != NUL : s - str < len)
{
// Put a string of normal characters in the redo buffer (that's
// faster).
start = s;
while (*s >= ' ' && *s < DEL && (len < 0 || s - str < len))
++s;
// Don't put '0' or '^' as last character, just in case a CTRL-D is
// typed next.
if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
--s;
if (s > start)
add_buff(&redobuff, start, (long)(s - start));
if (*s == NUL || (len >= 0 && s - str >= len))
break;
// Handle a special or multibyte character.
if (has_mbyte)
// Handle composing chars separately.
c = mb_cptr2char_adv(&s);
else
c = *s++;
if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
add_char_buff(&redobuff, Ctrl_V);
// CTRL-V '0' must be inserted as CTRL-V 048
if (*s == NUL && c == '0')
add_buff(&redobuff, (char_u *)"048", 3L);
else
add_char_buff(&redobuff, c);
}
}
/*
* Append a character to the redo buffer.
* Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
*/
void
AppendCharToRedobuff(int c)
{
if (!block_redo)
add_char_buff(&redobuff, c);
}
/*
* Append a number to the redo buffer.
*/
void
AppendNumberToRedobuff(long n)
{
if (!block_redo)
add_num_buff(&redobuff, n);
}
/*
* Append string "s" to the stuff buffer.
* CSI and K_SPECIAL must already have been escaped.
*/
void
stuffReadbuff(char_u *s)
{
add_buff(&readbuf1, s, -1L);
}
/*
* Append string "s" to the redo stuff buffer.
* CSI and K_SPECIAL must already have been escaped.
*/
void
stuffRedoReadbuff(char_u *s)
{
add_buff(&readbuf2, s, -1L);
}
static void
stuffReadbuffLen(char_u *s, long len)
{
add_buff(&readbuf1, s, len);
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Stuff "s" into the stuff buffer, leaving special key codes unmodified and
* escaping other K_SPECIAL and CSI bytes.
* Change CR, LF and ESC into a space.
*/
void
stuffReadbuffSpec(char_u *s)
{
int c;
while (*s != NUL)
{
if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
{
// Insert special key literally.
stuffReadbuffLen(s, 3L);
s += 3;
}
else
{
c = mb_cptr2char_adv(&s);
if (c == CAR || c == NL || c == ESC)
c = ' ';
stuffcharReadbuff(c);
}
}
}
#endif
/*
* Append a character to the stuff buffer.
* Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
*/
void
stuffcharReadbuff(int c)
{
add_char_buff(&readbuf1, c);
}
/*
* Append a number to the stuff buffer.
*/
void
stuffnumReadbuff(long n)
{
add_num_buff(&readbuf1, n);
}
/*
* Stuff a string into the typeahead buffer, such that edit() will insert it
* literally ("literally" TRUE) or interpret is as typed characters.
*/
void
stuffescaped(char_u *arg, int literally)
{
int c;
char_u *start;
while (*arg != NUL)
{
// Stuff a sequence of normal ASCII characters, that's fast. Also
// stuff K_SPECIAL to get the effect of a special key when "literally"
// is TRUE.
start = arg;
while ((*arg >= ' ' && *arg < DEL)
|| (*arg == K_SPECIAL && !literally))
++arg;
if (arg > start)
stuffReadbuffLen(start, (long)(arg - start));
// stuff a single special character
if (*arg != NUL)
{
if (has_mbyte)
c = mb_cptr2char_adv(&arg);
else
c = *arg++;
if (literally && ((c < ' ' && c != TAB) || c == DEL))
stuffcharReadbuff(Ctrl_V);
stuffcharReadbuff(c);
}
}
}
/*
* Read a character from the redo buffer. Translates K_SPECIAL, CSI and
* multibyte characters.
* The redo buffer is left as it is.
* If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
* otherwise.
* If old is TRUE, use old_redobuff instead of redobuff.
*/
static int
read_redo(int init, int old_redo)
{
static buffblock_T *bp;
static char_u *p;
int c;
int n;
char_u buf[MB_MAXBYTES + 1];
int i;
if (init)
{
if (old_redo)
bp = old_redobuff.bh_first.b_next;
else
bp = redobuff.bh_first.b_next;
if (bp == NULL)
return FAIL;
p = bp->b_str;
return OK;
}
if ((c = *p) != NUL)
{
// Reverse the conversion done by add_char_buff()
// For a multi-byte character get all the bytes and return the
// converted character.
if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
n = MB_BYTE2LEN_CHECK(c);
else
n = 1;
for (i = 0; ; ++i)
{
if (c == K_SPECIAL) // special key or escaped K_SPECIAL
{
c = TO_SPECIAL(p[1], p[2]);
p += 2;
}
#ifdef FEAT_GUI
if (c == CSI) // escaped CSI
p += 2;
#endif
if (*++p == NUL && bp->b_next != NULL)
{
bp = bp->b_next;
p = bp->b_str;
}
buf[i] = c;
if (i == n - 1) // last byte of a character
{
if (n != 1)
c = (*mb_ptr2char)(buf);
break;
}
c = *p;
if (c == NUL) // cannot happen?
break;
}
}
return c;
}
/*
* Copy the rest of the redo buffer into the stuff buffer (in a slow way).
* If old_redo is TRUE, use old_redobuff instead of redobuff.
* The escaped K_SPECIAL and CSI are copied without translation.
*/
static void
copy_redo(int old_redo)
{
int c;
while ((c = read_redo(FALSE, old_redo)) != NUL)
add_char_buff(&readbuf2, c);
}
/*
* Stuff the redo buffer into readbuf2.
* Insert the redo count into the command.
* If "old_redo" is TRUE, the last but one command is repeated
* instead of the last command (inserting text). This is used for
* CTRL-O <.> in insert mode
*
* return FAIL for failure, OK otherwise
*/
int
start_redo(long count, int old_redo)
{
int c;
// init the pointers; return if nothing to redo
if (read_redo(TRUE, old_redo) == FAIL)
return FAIL;
c = read_redo(FALSE, old_redo);
#ifdef FEAT_EVAL
if (c == K_SID)
{
// Copy the <SID>{sid}; sequence
add_char_buff(&readbuf2, c);
for (;;)
{
c = read_redo(FALSE, old_redo);
add_char_buff(&readbuf2, c);
if (!isdigit(c))
break;
}
c = read_redo(FALSE, old_redo);
}
#endif
// copy the buffer name, if present
if (c == '"')
{
add_buff(&readbuf2, (char_u *)"\"", 1L);
c = read_redo(FALSE, old_redo);
// if a numbered buffer is used, increment the number
if (c >= '1' && c < '9')
++c;
add_char_buff(&readbuf2, c);
// the expression register should be re-evaluated
if (c == '=')
{
add_char_buff(&readbuf2, CAR);
cmd_silent = TRUE;
}
c = read_redo(FALSE, old_redo);
}
if (c == 'v') // redo Visual
{
VIsual = curwin->w_cursor;
VIsual_active = TRUE;
VIsual_select = FALSE;
VIsual_reselect = TRUE;
redo_VIsual_busy = TRUE;
c = read_redo(FALSE, old_redo);
}
// try to enter the count (in place of a previous count)
if (count)
{
while (VIM_ISDIGIT(c)) // skip "old" count
c = read_redo(FALSE, old_redo);
add_num_buff(&readbuf2, count);
}
// copy the rest from the redo buffer into the stuff buffer
add_char_buff(&readbuf2, c);
copy_redo(old_redo);
return OK;
}
/*
* Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
* the redo buffer into readbuf2.
* return FAIL for failure, OK otherwise
*/
int
start_redo_ins(void)
{
int c;
if (read_redo(TRUE, FALSE) == FAIL)
return FAIL;
start_stuff();
// skip the count and the command character
while ((c = read_redo(FALSE, FALSE)) != NUL)
{
if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
{
if (c == 'O' || c == 'o')
add_buff(&readbuf2, NL_STR, -1L);
break;
}
}
// copy the typed text from the redo buffer into the stuff buffer
copy_redo(FALSE);
block_redo = TRUE;
return OK;
}
void
stop_redo_ins(void)
{
block_redo = FALSE;
}
/*
* Initialize typebuf.tb_buf to point to typebuf_init.
* alloc() cannot be used here: In out-of-memory situations it would
* be impossible to type anything.
*/
static void
init_typebuf(void)
{
if (typebuf.tb_buf != NULL)
return;
typebuf.tb_buf = typebuf_init;
typebuf.tb_noremap = noremapbuf_init;
typebuf.tb_buflen = TYPELEN_INIT;
typebuf.tb_len = 0;
typebuf.tb_off = MAXMAPLEN + 4;
typebuf.tb_change_cnt = 1;
}
/*
* Returns TRUE when keys cannot be remapped.
*/
int
noremap_keys(void)
{
return KeyNoremap & (RM_NONE|RM_SCRIPT);
}
/*
* Insert a string in position 'offset' in the typeahead buffer (for "@r"
* and ":normal" command, vgetorpeek() and check_termcode()).
*
* If "noremap" is REMAP_YES, new string can be mapped again.
* If "noremap" is REMAP_NONE, new string cannot be mapped again.
* If "noremap" is REMAP_SKIP, first char of new string cannot be mapped again,
* but abbreviations are allowed.
* If "noremap" is REMAP_SCRIPT, new string cannot be mapped again, except for
* script-local mappings.
* If "noremap" is > 0, that many characters of the new string cannot be mapped.
*
* If "nottyped" is TRUE, the string does not return KeyTyped (don't use when
* "offset" is non-zero!).
*
* If "silent" is TRUE, cmd_silent is set when the characters are obtained.
*
* return FAIL for failure, OK otherwise
*/
int
ins_typebuf(
char_u *str,
int noremap,
int offset,
int nottyped,
int silent)
{
char_u *s1, *s2;
int newlen;
int addlen;
int i;
int newoff;
int val;
int nrm;