-
Notifications
You must be signed in to change notification settings - Fork 0
/
tio.c
1296 lines (1171 loc) · 26.7 KB
/
tio.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
/* tio.c */
/* Author:
* Steve Kirkendall
* 1500 SW Park #326
* Portland OR, 97201
*/
/* This file contains terminal I/O functions */
#include <string.h>
#include "config.h"
#include "vi.h"
#include "ctype.h"
extern char *printable();
static int showmsg P_((void));
static int _F_F = 1;
/* [cdpark] prevents recursively expand an abbreviation */
/* This function reads in a line from the terminal. It simulates the normal
* line editing for cooked input, with support for backspace, ^U, and ^V.
*
* Support for ^W and ^P added by sdw, Nov. 1993.
*
* It tries to hide the extra ^O that a "visual" map inserts before each
* character. When it reads a ^O, it discards it and reads the next character.
* Then only exception is after a ^V which itself was not preceded by a ^O, the
* character immediately following the ^V is accepted even if it is a ^O.
*
* Eventually I hope to make it use ^O to access a history of previously
* entered commands. ^Ok to move back, ^Ol to move forward, etc. This way,
* the standard arrow key mappings can be used to access history easily, and
* users who don't have the benefit of arrow keys will still be able to use
* history. But that hasn't happened yet.
*/
int vgets(prompt, buf, bsize)
int prompt; /* the prompt character, or '\0' for none */
char *buf; /* buffer into which the string is read */
int bsize; /* size of the buffer */
{
int len; /* how much we've read so far */
int ch; /* a character from the user */
int quoted; /* is the next char quoted? */
int tab; /* column position of cursor */
char widths[132]; /* widths of characters */
int word; /* index of first letter of word */
#ifndef NO_DIGRAPH
int erased; /* 0, or first char of a digraph */
#endif
#ifndef NO_EXTENSIONS
int ctrlO; /* boolean: was last character ^O ? */
#endif
#if 1 /* [sdw] */
int cbsize; /* size of cut buffer to be pasted */
#endif
/* show the prompt */
move(LINES - 1, 0);
tab = 0;
if (prompt)
{
addch(prompt);
tab = 1;
}
clrtoeol();
refresh();
/* read in the line */
#ifndef NO_DIGRAPH
erased =
#endif
#ifndef NO_EXTENSIONS
ctrlO =
#endif
quoted = len = 0;
for (;;)
{
#ifndef NO_ABBR
if (quoted || mode == MODE_EX)
{
ch = getkey(0);
}
else
{
/* maybe expand an abbreviation while getting key */
ch = getabkey(WHEN_EX, buf, len);
}
#else
ch = getkey(0);
#endif
#ifndef NO_EXTENSIONS
if (ctrlO || !quoted && ch == ctrl('O'))
{
ch = getkey(quoted ? 0 : WHEN_EX);
if (ch == ctrl('V'))
{
ctrlO = TRUE;
}
}
#endif
/* some special conversions */
#if 0
if (ch == ctrl('D') && len == 0)
ch = ctrl('[');
#endif
#ifndef NO_DIGRAPH
if (*o_digraph && erased != 0 && ch != '\b')
{
ch = digraph(erased, ch);
erased = 0;
}
#endif
/* inhibit detection of special chars (except ^J) after a ^V */
if (quoted && ch != '\n')
{
ch |= 256;
}
/* process the character */
switch(ch)
{
case ctrl('V'):
qaddch('^');
qaddch('\b');
quoted = TRUE;
break;
case ctrl('D'):
return -1;
case ctrl('['):
case '\n':
#if OSK
case '\l':
#else
case '\r':
#endif
clrtoeol();
goto BreakBreak;
#ifndef CRUNCH
case ctrl('U'):
while (len > 0)
{
len--;
while (widths[len]-- > 0)
{
qaddch('\b');
}
}
clrtoeol();
break;
/* [sdw] -- verbose but functional... */
/* erase over previous Word */
case ctrl('W'):
if (len == 0)
{
return -1;
}
while (len > 0
&& (buf[len-1] == ' ' || buf[len-1] == '\t'))
{
len--;
# ifndef NO_DIGRAPH
erased = buf[len];
# endif
for (ch = widths[len]; ch > 0; ch--)
addch('\b');
tab -= widths[len];
}
while (len > 0
&& buf[len-1] != ' ' && buf[len-1] != '\t')
{
len--;
# ifndef NO_DIGRAPH
erased = buf[len];
# endif
for (ch = widths[len]; ch > 0; ch--)
addch('\b');
tab -= widths[len];
}
clrtoeol();
break;
#endif
case '\b':
if (len > 0)
{
len--;
#ifndef NO_DIGRAPH
erased = buf[len];
#endif
if (IsHIdx(buf, len) == HAN_SECOND)
{
len--;
addch('\b');
tab--;
}
for (ch = widths[len]; ch > 0; ch--)
addch('\b');
clrtoeol();
tab -= widths[len];
}
else
{
return -1;
}
break;
#if 1 /* [sdw] */
/* paste in contents of anonymous buffer */
case ctrl('P'):
cbsize = cb2str(0, tmpblk.c, BLKSIZE);
if (cbsize > 0 && cbsize != BLKSIZE)
{
execmap(0, tmpblk.c, FALSE);
}
break;
#endif
default:
/* strip off quotation bit */
if (ch & 256)
{
ch &= ~256;
qaddch(' ');
qaddch('\b');
}
/* add & echo the char */
if (len < bsize - 1)
{
if (ch == '\t' && !quoted)
{
widths[len] = *o_tabstop - (tab % *o_tabstop);
addstr(" " + 8 - widths[len]);
tab += widths[len];
}
else if (ch > 0 && ch < ' ') /* > 0 by GB */
{
addch('^');
addch(ch + '@');
widths[len] = 2;
tab += 2;
}
else if (ch == '\177')
{
addch('^');
addch('?');
widths[len] = 2;
tab += 2;
}
else if (IsHiBitOn(ch))
{
if (len == bsize - 2)
beep();
addch(ch);
widths[len] = 1;
buf[len++] = ch;
ch = getkey(quoted ? 0 : WHEN_EX);
addch(ch);
widths[len] = 1;
tab += 2;
}
else
{
addch(ch);
widths[len] = 1;
tab++;
}
buf[len++] = ch;
}
else
{
beep();
}
#ifndef NO_EXTENSIONS
ctrlO =
#endif
quoted = FALSE;
}
}
BreakBreak:
refresh();
buf[len] = '\0';
return len;
}
static int manymsgs; /* This variable keeps msgs from overwriting each other */
static char pmsg[80]; /* previous message (waiting to be displayed) */
static int showmsg()
{
/* if there is no message to show, then don't */
if (!manymsgs)
return FALSE;
/* display the message */
move(LINES - 1, 0);
if (*pmsg)
{
standout();
qaddch(' ');
qaddstr(pmsg);
qaddch(' ');
standend();
}
clrtoeol();
manymsgs = FALSE;
return TRUE;
}
void endmsgs()
{
if (manymsgs)
{
showmsg();
addch('\n');
}
}
/* Write a message in an appropriate way. This should really be a varargs
* function, but there is no such thing as vwprintw. Hack!!!
*
* In MODE_EX or MODE_COLON, the message is written immediately, with a
* newline at the end.
*
* In MODE_VI, the message is stored in a character buffer. It is not
* displayed until getkey() is called. msg() will call getkey() itself,
* if necessary, to prevent messages from being lost.
*
* msg("") - clears the message line
* msg("%s %d", ...) - does a printf onto the message line
*/
#if NEWSTYLE
void msg (char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
#else
void msg(fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
char *fmt;
long arg1, arg2, arg3, arg4, arg5, arg6, arg7;
{
#endif
if (mode != MODE_VI)
{
#if NEWSTYLE
vsprintf (pmsg, fmt, ap);
#else
sprintf(pmsg, fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
#endif
qaddstr(pmsg);
addch('\n');
exrefresh();
}
else
{
/* wait for keypress between consecutive msgs */
if (manymsgs)
{
getkey(WHEN_MSG);
}
/* real message */
#if NEWSTYLE
vsprintf (pmsg, fmt, ap);
#else
sprintf(pmsg, fmt, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
#endif
if (*fmt)
{
manymsgs = TRUE;
}
}
#ifdef __STDC__
va_end (ap);
#endif
}
/* This function calls refresh() if the option exrefresh is set */
void exrefresh()
{
char *scan;
/* If this ex command wrote ANYTHING set exwrote so vi's : command
* can tell that it must wait for a user keystroke before redrawing.
*/
for (scan=kbuf; scan<stdscr; scan++)
if (*scan == '\n')
exwrote = TRUE;
/* now we do the refresh thing */
if (*o_exrefresh)
{
refresh();
}
else
{
wqrefresh();
}
if (mode != MODE_VI && *o_more)
{
manymsgs = FALSE;
}
}
/* This structure is used to store maps and abbreviations. The distinction
* between them is that maps are stored in the list referenced by the "maps"
* pointer, while abbreviations are referenced by the "abbrs" pointer.
*/
typedef struct _map
{
struct _map *next; /* another abbreviation */
short len; /* length of the "rawin" characters */
short flags; /* various flags */
char *label; /* label of the map/abbr, or NULL */
char *rawin; /* the "rawin" characters */
char *cooked;/* the "cooked" characters */
} MAP;
static char keybuf[KEYBUFSIZE];
static int cend; /* end of input characters */
static int user; /* from user through cend are chars typed by user */
static int next; /* index of the next character to be returned */
static MAP *match; /* the matching map, found by countmatch() */
static MAP *maps; /* the map table */
#ifndef NO_ABBR
static MAP *abbrs; /* the abbreviation table */
#endif
/* ring the terminal's bell */
void beep()
{
/* do a visible/audible bell */
if (*o_flash)
{
do_VB();
refresh();
}
else if (*o_errorbells)
{
do_beep();
}
/* discard any buffered input, and abort macros */
next = user = cend;
}
/* This function replaces a "rawin" character sequence with the "cooked" version,
* by modifying the internal type-ahead buffer.
*/
void execmap(rawlen, cookedstr, visual)
int rawlen; /* length of rawin text -- string to delete */
char *cookedstr; /* the cooked text -- string to insert */
int visual; /* boolean -- chars to be executed in visual mode? */
{
int cookedlen;
char *src, *dst;
int i;
/* find the length of the cooked string */
cookedlen = strlen(cookedstr);
#ifndef NO_EXTENSIONS
if (visual)
{
cookedlen *= 2;
}
#endif
/* if too big to fit in type-ahead buffer, then don't do it */
if (cookedlen + (cend - next) - rawlen > KEYBUFSIZE)
{
return;
}
/* shift to make room for cookedstr at the front of keybuf */
src = &keybuf[next + rawlen];
dst = &keybuf[cookedlen];
i = cend - (next + rawlen);
if (src >= dst)
{
while (i-- > 0)
{
*dst++ = *src++;
}
}
else
{
src += i;
dst += i;
while (i-- > 0)
{
*--dst = *--src;
}
}
/* insert cookedstr, and adjust offsets */
cend += cookedlen - rawlen - next;
user += cookedlen - rawlen - next;
next = 0;
for (dst = keybuf, src = cookedstr; *src; )
{
#ifndef NO_EXTENSIONS
if (visual)
{
*dst++ = ctrl('O');
cookedlen--;
}
#endif
*dst++ = *src++;
}
#ifdef DEBUG2
{
#include <stdio.h>
FILE *debout;
int i;
debout = fopen("debug.out", "a");
fprintf(debout, "After execmap(%d, \"%s\", %d)...\n", rawlen, cookedstr, visual);
for (i = 0; i < cend; i++)
{
if (i == next) fprintf(debout, "(next)");
if (i == user) fprintf(debout, "(user)");
if (UCHAR(keybuf[i]) < ' ')
fprintf(debout, "^%c", keybuf[i] ^ '@');
else
fprintf(debout, "%c", keybuf[i]);
}
fprintf(debout, "(end)\n");
fclose(debout);
}
#endif
}
#ifndef NO_CURSORSHAPE
/* made global so that suspend_curses() can reset it. -nox */
int oldcurs;
#endif
/* This function calls ttyread(). If necessary, it will also redraw the screen,
* change the cursor shape, display the mode, and update the ruler. If the
* number of characters read is 0, and we didn't time-out, then it exits because
* we've apparently reached the end of an EX script.
*/
static int fillkeybuf(when, timeout)
int when; /* mixture of WHEN_XXX flags */
int timeout;/* timeout in 1/10 second increments, or 0 */
{
int nkeys;
#ifndef NO_SHOWMODE
static int oldwhen; /* "when" from last time */
static int oldleft;
static long oldtop;
static long oldnlines;
char *str;
#endif
#ifdef DEBUG
watch();
#endif
#ifndef NO_CURSORSHAPE
/* make sure the cursor is the right shape */
if (has_CQ)
{
if (when != oldcurs)
{
switch (when)
{
case WHEN_EX: do_CX(); break;
case WHEN_VICMD: do_CV(); break;
case WHEN_VIINP: do_CI(); break;
case WHEN_VIREP: do_CR(); break;
}
oldcurs = when;
}
}
#endif
#ifndef NO_SHOWMODE
/* if "showmode" then say which mode we're in */
if (*o_smd && (when & WHENMASK))
{
/* redraw the screen before we check to see whether the
* "showmode" message needs to be redrawn.
*/
redraw(cursor, !(when & WHEN_VICMD));
/* now the "topline" test should be valid */
if (when != oldwhen
|| topline != oldtop
|| leftcol != oldleft
|| nlines != oldnlines)
{
oldwhen = when;
oldtop = topline;
oldleft = leftcol;
oldnlines = nlines;
if (when & WHEN_VICMD) str = "Command";
else if (when & WHEN_VIINP) str = " Input ";
else if (when & WHEN_VIREP) str = "Replace";
else if (when & WHEN_REP1) str = "Replc 1";
else if (when & WHEN_CUT) str = "Buffer ";
else if (when & WHEN_MARK) str = " Mark ";
else if (when & WHEN_CHAR) str = "Dest Ch";
else str = (char *)0;
if (str)
{
move(LINES - 1, COLS - 10);
standout();
qaddstr(str);
standend();
}
}
}
#endif
#ifndef NO_EXTENSIONS
/* maybe display the ruler */
if (*o_ruler && (when & (WHEN_VICMD|WHEN_VIINP|WHEN_VIREP)))
{
char buf[20];
redraw(cursor, !(when & WHEN_VICMD));
pfetch(markline(cursor));
# ifndef NO_LEARN
if (learn)
sprintf(buf, "%7ld%c%-4d", markline(cursor), learn, 1 + idx2col(cursor, ptext, when & (WHEN_VIINP|WHEN_VIREP)));
else
# endif
sprintf(buf, "%7ld,%-4d", markline(cursor), 1 + idx2col(cursor, ptext, when & (WHEN_VIINP|WHEN_VIREP)));
move(LINES - 1, COLS - 22);
addstr(buf);
}
#ifndef NO_LEARN
else if (when & (WHEN_VICMD|WHEN_VIINP|WHEN_VIREP))
{
move(LINES - 1, COLS - 15);
if (learn)
addch(learn);
else
addch(' ');
}
#endif
#endif
/* redraw, so the cursor is in the right place */
if (when & WHENMASK)
{
redraw(cursor, !(when & (WHENMASK & ~(WHEN_VIREP|WHEN_VIINP))));
}
/* Okay, now we can finally read the rawin keystrokes */
refresh();
nkeys = ttyread(keybuf + cend, sizeof keybuf - cend, timeout);
/* if nkeys == 0 then we've reached EOF of an ex script. */
if (nkeys == 0 && timeout == 0)
{
tmpabort(TRUE);
move(LINES - 1, 0);
clrtoeol();
refresh();
endwin();
exit(exitcode);
}
cend += nkeys;
#if 0 /* [sdw] this looks like a bug... */
user += nkeys;
#endif
return nkeys;
}
/* This function counts the number of maps that could match the characters
* between &keybuf[next] and &keybuf[cend], including incomplete matches.
* The longest comlete match is remembered via the "match" variable.
*/
static int countmatch(when)
int when; /* mixture of WHEN_XXX flags */
{
MAP *map;
int count;
/* clear the "match" variable */
match = (MAP *)0;
/* check every map */
for (count = 0, map = maps; map; map = map->next)
{
/* can't match if wrong mode */
if ((map->flags & when) == 0)
{
continue;
}
/* would this be a complete match? */
if (map->len <= cend - next)
{
/* Yes, it would be. Now does it really match? */
if (!strncmp(map->rawin, &keybuf[next], map->len))
{
count++;
/* if this is the longest complete match,
* then remember it.
*/
if (!match || match->len < map->len)
{
match = map;
}
}
}
else
{
/* No, it wouldn't. But check for partial match */
if (!strncmp(map->rawin, &keybuf[next], cend - next))
{
/* increment by 2 instead of 1 so that, in the
* event that we have a partial match with a
* single map, we don't mistakenly assume we
* have resolved the map yet.
*/
count += 2;
}
}
}
return count;
}
#ifndef NO_ABBR
/* This function checks to see whether a word is an abbreviation. If it is,
* then an appropriate number of backspoace characters is inserted into the
* type-ahead buffer, followed by the expanded form of the abbreviation.
*/
static void expandabbr(line, llen)
char *line;
int llen;
{
MAP *abbr;
/* if the next character wouldn't end the word, then don't expand */
if (isalnum(keybuf[next]) || keybuf[next] == ctrl('V') || keybuf[next] == '\b')
{
return;
}
/* find the abbreviation, if any */
for (abbr = abbrs;
abbr && (abbr->len > llen /* abbreviation longer than line */
|| (abbr->len < llen && isalnum(line[llen - abbr->len - 1]))
/* text would be preceded by alnum */
|| strncmp(abbr->rawin, line + llen - abbr->len, abbr->len));
/* text doesn't match abbr */
abbr = abbr->next)
{
}
/* If an abbreviation was found, then expand it by inserting the long
* version into the type-ahead buffer, and then inserting (in front of
* the long version) enough backspaces to erase to the short version.
*/
if (abbr)
{
llen = AdjunstHLen(abbr->rawin, abbr->len);
execmap(0, abbr->cooked, FALSE);
while (llen > 15)
{
execmap(0, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", FALSE);
llen -= 15;
}
if (llen > 0)
{
execmap(0, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" + 15 - llen, FALSE);
}
_F_F = 0;
}
}
#endif
/* This function calls getabkey() without attempting to expand abbreviations */
int getkey(when)
int when; /* mixture of WHEN_XXX flags */
{
return getabkey(when, "", 0);
}
/* This is it. This function returns keystrokes one-at-a-time, after mapping
* and abbreviations have been taken into account.
*/
int getabkey(when, line, llen)
int when; /* mixture of WHEN_XXX flags */
char *line; /* a line that may need to be expanded as an abbr */
int llen; /* length of "line" -- since "line" might not have \0 */
{
int matches;
#ifdef DEBUG
static long prevchg;
static int nslow;
static char slow[80];
#endif
/* if not reading an EX command, and we're not optimizing, then redraw
* the display.
*/
#ifndef CRUNCH
if (!*o_optimize && (when & WHENMASK))
{
redraw(cursor, !(when & (WHENMASK & ~(WHEN_VIREP|WHEN_VIINP))));
}
#endif
/* if this key is needed for delay between multiple error messages,
* then reset the manymsgs flag and abort any mapped key sequence.
*/
if (showmsg())
{
if (when == WHEN_MSG)
{
#ifndef CRUNCH
if (!*o_more)
{
refresh();
return ' ';
}
#endif
qaddstr("[More...]");
refresh();
execmap(user, "", FALSE);
}
}
#ifdef DEBUG
/* periodically check for screwed up internal tables */
watch();
#endif
/* if buffer empty, read some characters without timeout */
if (next >= cend)
{
next = user = cend = 0;
fillkeybuf(when, 0);
_F_F = 1;
}
/* cdpark. correct user valus s.t. user >= next */
if ( next > user)
user = next;
/* try to map the key, unless already mapped and not ":set noremap" */
if (next <= user || *o_remap)
{
do
{
/* read keystrokes until we have either eliminated
* all possible matching maps, or have found exactly
* one complete match and have eliminated all partial
* maps.
*/
do
{
matches = countmatch(when);
} while (matches > 1 && fillkeybuf(when, *o_keytime) > 0);
/* if we have 1 complete match, then map it */
if (matches == 1)
{
execmap(match->len, match->cooked,
(match->flags & WHEN_INMV) != 0
&& (when & (WHEN_VIINP|WHEN_VIREP)) != 0);
}
} while (*o_remap && matches == 1);
}
/* ERASEKEY should always be mapped to '\b'. */
if (keybuf[next] == ERASEKEY)
{
keybuf[next] = '\b';
}
#ifndef NO_LEARN
learnkey(keybuf[next]);
#endif
#ifndef NO_ABBR
/* try to expand an abbreviation, except in visual command mode */
if (llen > 0 && _F_F && (mode & (WHEN_EX|WHEN_VIINP|WHEN_VIREP)) != 0)
{
expandabbr(line, llen);
}
#endif
#ifdef DEBUG
/* if slowmacro is set, then show keystroke before executing anything */
if (*o_slowmacro && next < user)
{
/* if previous command changed something, then pause */
if (changes != prevchg)
{
prevchg = changes;
redraw(cursor, !(when & (WHENMASK & ~(WHEN_VIREP|WHEN_VIINP))));
slow[nslow] = 0;
move(LINES - 1, 0);
qaddstr(printable(slow));
clrtoeol();
refresh();
redraw(cursor, !(when & (WHENMASK & ~(WHEN_VIREP|WHEN_VIINP))));
sleep(1);
nslow = 0;
}
/* display the next key to be processed */
slow[nslow++] = keybuf[next];
slow[nslow] = 0;
move(LINES - 1, 0);
qaddstr(printable(slow));
clrtoeol();
refresh();
if (nslow > 50)
{
nslow = 0;
}
}
else
{
nslow = 0;
prevchg = changes;
}
#endif /* DEBUG */
/* return the next key */
#ifndef NO_HANEXTENSIONS
if ((*o_hangulinsert) && !(*o_readonly) && (when&WHEN_VICMD) && IsHiBitOn(keybuf[next]))
{
return 'i';
}
else
#endif
{
return keybuf[next++];
}
}
/* This function maps or unmaps a key */
void mapkey(rawin, cooked, when, name)
char *rawin; /* the input key sequence, before mapping */
char *cooked;/* after mapping -- or NULL to remove map */
int when; /* bitmap of when mapping should happen */
char *name; /* name of the key, NULL for no name, "abbr" for abbr */
{
MAP **head; /* head of list of maps or abbreviations */
MAP *scan; /* used for scanning through the list */
MAP *prev; /* used during deletions */
/* Is this a map or an abbreviation? Choose the right list. */
#ifndef NO_ABBR
head = ((!name || strcmp(name, "abbr")) ? &maps : &abbrs);
#else
head = &maps;
#endif
/* try to find the map in the list. For maps, rawin must match the
* map's rawin; for abbreviations, the rawin may match either the
* abbreviation's rawin or its cooked string.
*/
for (scan = *head, prev = (MAP *)0;
#ifndef NO_ABBR
scan && (strcmp(rawin, scan->rawin) &&
(head != &abbrs || strcmp(rawin, scan->cooked)) ||
!(scan->flags & when & (WHEN_EX|WHEN_VICMD|WHEN_VIINP|WHEN_VIREP)));