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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
|
/* $OpenBSD: getchar.c,v 1.2 1996/09/21 06:22:59 downsj Exp $ */
/* vi:set ts=4 sw=4:
*
* 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.
*/
/*
* getchar.c
*
* functions related with getting a character from the user/mapping/redo/...
*
* manipulations with redo buffer and stuff buffer
* mappings and abbreviations
*/
#include "vim.h"
#include "globals.h"
#include "proto.h"
#include "option.h"
/*
* structure used to store one block of the stuff/redo/macro buffers
*/
struct bufblock
{
struct bufblock *b_next; /* pointer to next bufblock */
char_u b_str[1]; /* contents (actually longer) */
};
#define MINIMAL_SIZE 20 /* minimal size for b_str */
/*
* header used for the stuff buffer and the redo buffer
*/
struct buffheader
{
struct bufblock bh_first; /* first (dummy) block of list */
struct bufblock *bh_curr; /* bufblock for appending */
int bh_index; /* index for reading */
int bh_space; /* space in bh_curr for appending */
};
static struct buffheader stuffbuff = {{NULL, {NUL}}, NULL, 0, 0};
static struct buffheader redobuff = {{NULL, {NUL}}, NULL, 0, 0};
static struct buffheader old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
static struct buffheader recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
/*
* 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;
/*
* structure used for mapping
*/
struct mapblock
{
struct mapblock *m_next; /* next mapblock */
char_u *m_keys; /* mapped from */
int m_keylen; /* strlen(m_keys) */
char_u *m_str; /* mapped to */
int m_mode; /* valid mode */
int m_noremap; /* if non-zero no re-mapping for m_str */
};
static struct mapblock maplist = {NULL, NULL, 0, NULL, 0, 0};
/* first dummy entry in maplist */
/*
* variables used by vgetorpeek() and flush_buffers()
*
* typebuf[] contains all characters that are not consumed yet.
* typebuf[typeoff] is the first valid character in typebuf[].
* typebuf[typeoff + typelen - 1] is the last valid char.
* typebuf[typeoff + typelen] must be NUL.
* The part in front may contain the result of mappings, abbreviations and
* @a commands. The length of this part is typemaplen.
* After it are characters that come from the terminal.
* no_abbr_cnt is the number of characters in typebuf that should not be
* considered for abbreviations.
* Some parts of typebuf may not be mapped. These parts are remembered in
* noremapbuf, which is the same length as typebuf and contains TRUE for the
* characters that are not to be remapped. noremapbuf[typeoff] is the first
* valid flag.
* (typebuf has been put in globals.h, because check_termcode() needs it).
*/
static char_u *noremapbuf = NULL; /* flags for typeahead characters */
#define TYPELEN_INIT (3 * (MAXMAPLEN + 3))
static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf */
static char_u noremapbuf_init[TYPELEN_INIT]; /* initial noremapbuf */
static int typemaplen = 0; /* nr of mapped characters in typebuf */
static int no_abbr_cnt = 0; /* nr of chars without abbrev. in typebuf */
static int last_recorded_len = 0; /* number of last recorded chars */
static void free_buff __ARGS((struct buffheader *));
static char_u *get_bufcont __ARGS((struct buffheader *, int));
static void add_buff __ARGS((struct buffheader *, char_u *));
static void add_num_buff __ARGS((struct buffheader *, long));
static void add_char_buff __ARGS((struct buffheader *, int));
static int read_stuff __ARGS((int));
static void start_stuff __ARGS((void));
static int read_redo __ARGS((int, int));
static void copy_redo __ARGS((int));
static void init_typebuf __ARGS((void));
static void gotchars __ARGS((char_u *, int));
static void may_sync_undo __ARGS((void));
static int vgetorpeek __ARGS((int));
static int inchar __ARGS((char_u *, int, long));
static void map_free __ARGS((struct mapblock *));
static void showmap __ARGS((struct mapblock *));
/*
* free and clear a buffer
*/
static void
free_buff(buf)
struct buffheader *buf;
{
register struct bufblock *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;
}
/*
* return the contents of a buffer as a single string
*/
static char_u *
get_bufcont(buffer, dozero)
struct buffheader *buffer;
int dozero; /* count == zero is not an error */
{
long_u count = 0;
char_u *p = NULL;
char_u *p2;
char_u *str;
struct bufblock *bp;
/* compute the total length of the string */
for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
count += STRLEN(bp->b_str);
if ((count || dozero) && (p = lalloc(count + 1, TRUE)) != 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
*/
char_u *
get_recorded()
{
char_u *p;
size_t len;
p = get_bufcont(&recordbuff, TRUE);
free_buff(&recordbuff);
/*
* Remove the characters that were added the last time, these must be the
* (possibly mapped) characters that stopped recording.
*/
len = STRLEN(p);
if ((int)len >= last_recorded_len)
p[len - last_recorded_len] = NUL;
return (p);
}
/*
* return the contents of the redo buffer as a single string
*/
char_u *
get_inserted()
{
return(get_bufcont(&redobuff, FALSE));
}
/*
* add string "s" after the current block of buffer "buf"
*/
static void
add_buff(buf, s)
register struct buffheader *buf;
char_u *s;
{
struct bufblock *p;
long_u n;
long_u len;
if ((n = STRLEN(s)) == 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 */
{
EMSG("Add to read buffer");
return;
}
else if (buf->bh_index != 0)
STRCPY(buf->bh_first.b_next->b_str,
buf->bh_first.b_next->b_str + buf->bh_index);
buf->bh_index = 0;
if (buf->bh_space >= (int)n)
{
strcat((char *)buf->bh_curr->b_str, (char *)s);
buf->bh_space -= n;
}
else
{
if (n < MINIMAL_SIZE)
len = MINIMAL_SIZE;
else
len = n;
p = (struct bufblock *)lalloc((long_u)(sizeof(struct bufblock) + len), TRUE);
if (p == NULL)
return; /* no space, just forget it */
buf->bh_space = len - n;
STRCPY(p->b_str, s);
p->b_next = buf->bh_curr->b_next;
buf->bh_curr->b_next = p;
buf->bh_curr = p;
}
return;
}
static void
add_num_buff(buf, n)
struct buffheader *buf;
long n;
{
char_u number[32];
sprintf((char *)number, "%ld", n);
add_buff(buf, number);
}
static void
add_char_buff(buf, c)
struct buffheader *buf;
int c;
{
char_u temp[4];
/*
* translate special key code into three byte sequence
*/
if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
{
temp[0] = K_SPECIAL;
temp[1] = K_SECOND(c);
temp[2] = K_THIRD(c);
temp[3] = NUL;
}
else
{
temp[0] = c;
temp[1] = NUL;
}
add_buff(buf, temp);
}
/*
* get one character from the stuff buffer
* If advance == TRUE go to the next char.
*/
static int
read_stuff(advance)
int advance;
{
register char_u c;
register struct bufblock *curr;
if (stuffbuff.bh_first.b_next == NULL) /* buffer is empty */
return NUL;
curr = stuffbuff.bh_first.b_next;
c = curr->b_str[stuffbuff.bh_index];
if (advance)
{
if (curr->b_str[++stuffbuff.bh_index] == NUL)
{
stuffbuff.bh_first.b_next = curr->b_next;
vim_free(curr);
stuffbuff.bh_index = 0;
}
}
return c;
}
/*
* prepare stuff buffer for reading (if it contains something)
*/
static void
start_stuff()
{
if (stuffbuff.bh_first.b_next != NULL)
{
stuffbuff.bh_curr = &(stuffbuff.bh_first);
stuffbuff.bh_space = 0;
}
}
/*
* check if the stuff buffer is empty
*/
int
stuff_empty()
{
return (stuffbuff.bh_first.b_next == NULL);
}
/*
* Remove the contents of the stuff buffer and the mapped characters in the
* typeahead buffer (used in case of an error). If 'typeahead' is true,
* flush all typeahead characters (used when interrupted by a CTRL-C).
*/
void
flush_buffers(typeahead)
int typeahead;
{
init_typebuf();
start_stuff();
while (read_stuff(TRUE) != NUL)
;
if (typeahead) /* remove all typeahead */
{
/*
* 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, MAXMAPLEN, 10L))
;
typeoff = MAXMAPLEN;
typelen = 0;
}
else /* remove mapped characters only */
{
typeoff += typemaplen;
typelen -= typemaplen;
}
typemaplen = 0;
no_abbr_cnt = 0;
}
/*
* 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()
{
if (!block_redo)
{
free_buff(&old_redobuff);
old_redobuff = redobuff;
redobuff.bh_first.b_next = NULL;
}
}
void
AppendToRedobuff(s)
char_u *s;
{
if (!block_redo)
add_buff(&redobuff, s);
}
void
AppendCharToRedobuff(c)
int c;
{
if (!block_redo)
add_char_buff(&redobuff, c);
}
void
AppendNumberToRedobuff(n)
long n;
{
if (!block_redo)
add_num_buff(&redobuff, n);
}
void
stuffReadbuff(s)
char_u *s;
{
add_buff(&stuffbuff, s);
}
void
stuffcharReadbuff(c)
int c;
{
add_char_buff(&stuffbuff, c);
}
void
stuffnumReadbuff(n)
long n;
{
add_num_buff(&stuffbuff, n);
}
/*
* Read a character from the redo buffer.
* 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(init, old_redo)
int init;
int old_redo;
{
static struct bufblock *bp;
static char_u *p;
int c;
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)
{
if (c == K_SPECIAL)
{
c = TO_SPECIAL(p[1], p[2]);
p += 2;
}
if (*++p == NUL && bp->b_next != NULL)
{
bp = bp->b_next;
p = bp->b_str;
}
}
return c;
}
/*
* copy the rest of the redo buffer into the stuff buffer (could be done faster)
* if old_redo is TRUE, use old_redobuff instead of redobuff
*/
static void
copy_redo(old_redo)
int old_redo;
{
register int c;
while ((c = read_redo(FALSE, old_redo)) != NUL)
stuffcharReadbuff(c);
}
/*
* Stuff the redo buffer into the stuffbuff.
* 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(count, old_redo)
long count;
int old_redo;
{
register int c;
if (read_redo(TRUE, old_redo) == FAIL) /* init the pointers; return if nothing to redo */
return FAIL;
c = read_redo(FALSE, old_redo);
/* copy the buffer name, if present */
if (c == '"')
{
add_buff(&stuffbuff, (char_u *)"\"");
c = read_redo(FALSE, old_redo);
/* if a numbered buffer is used, increment the number */
if (c >= '1' && c < '9')
++c;
add_char_buff(&stuffbuff, c);
c = read_redo(FALSE, old_redo);
}
if (c == 'v') /* redo Visual */
{
VIsual = curwin->w_cursor;
VIsual_active = 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 (isdigit(c)) /* skip "old" count */
c = read_redo(FALSE, old_redo);
add_num_buff(&stuffbuff, count);
}
/* copy from the redo buffer into the stuff buffer */
add_char_buff(&stuffbuff, 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 the stuffbuff.
* return FAIL for failure, OK otherwise
*/
int
start_redo_ins()
{
register 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)
{
c = TO_UPPER(c);
if (vim_strchr((char_u *)"AIRO", c) != NULL)
{
if (c == 'O')
stuffReadbuff(NL_STR);
break;
}
}
/* copy the typed text from the redo buffer into the stuff buffer */
copy_redo(FALSE);
block_redo = TRUE;
return OK;
}
void
set_redo_ins()
{
block_redo = TRUE;
}
void
stop_redo_ins()
{
block_redo = FALSE;
}
/*
* Initialize typebuf 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()
{
if (typebuf == NULL)
{
typebuf = typebuf_init;
noremapbuf = noremapbuf_init;
typebuflen = TYPELEN_INIT;
typelen = 0;
typeoff = 0;
}
}
/*
* insert a string in position 'offset' in the typeahead buffer (for "@r"
* and ":normal" command, vgetorpeek() and check_termcode())
*
* If noremap is 0, new string can be mapped again.
* If noremap is -1, new string cannot be mapped again.
* 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!).
*
* return FAIL for failure, OK otherwise
*/
int
ins_typebuf(str, noremap, offset, nottyped)
char_u *str;
int noremap;
int offset;
int nottyped;
{
register char_u *s1, *s2;
register int newlen;
register int addlen;
register int i;
register int newoff;
init_typebuf();
addlen = STRLEN(str);
/*
* Easy case: there is room in front of typebuf[typeoff]
*/
if (offset == 0 && addlen <= typeoff)
{
typeoff -= addlen;
vim_memmove(typebuf + typeoff, str, (size_t)addlen);
}
/*
* Need to allocate new buffer.
* In typebuf there must always be room for MAXMAPLEN + 4 characters.
* We add some extra room to avoid having to allocate too often.
*/
else
{
newoff = MAXMAPLEN + 4;
newlen = typelen + addlen + newoff + 2 * (MAXMAPLEN + 4);
if (newlen < 0) /* string is getting too long */
{
emsg(e_toocompl); /* also calls flush_buffers */
setcursor();
return FAIL;
}
s1 = alloc(newlen);
if (s1 == NULL) /* out of memory */
return FAIL;
s2 = alloc(newlen);
if (s2 == NULL) /* out of memory */
{
vim_free(s1);
return FAIL;
}
typebuflen = newlen;
/* copy the old chars, before the insertion point */
vim_memmove(s1 + newoff, typebuf + typeoff, (size_t)offset);
/* copy the new chars */
vim_memmove(s1 + newoff + offset, str, (size_t)addlen);
/* copy the old chars, after the insertion point, including
* the NUL at the end */
vim_memmove(s1 + newoff + offset + addlen, typebuf + typeoff + offset,
(size_t)(typelen - offset + 1));
if (typebuf != typebuf_init)
vim_free(typebuf);
typebuf = s1;
vim_memmove(s2 + newoff, noremapbuf + typeoff, (size_t)offset);
vim_memmove(s2 + newoff + offset + addlen,
noremapbuf + typeoff + offset, (size_t)(typelen - offset));
if (noremapbuf != noremapbuf_init)
vim_free(noremapbuf);
noremapbuf = s2;
typeoff = newoff;
}
typelen += addlen;
/*
* Adjust noremapbuf[] for the new characters:
* If noremap < 0: all the new characters are flagged not remappable
* If noremap == 0: all the new characters are flagged mappable
* If noremap > 0: 'noremap' characters are flagged not remappable, the
* rest mappable
*/
if (noremap < 0) /* length not specified */
noremap = addlen;
for (i = 0; i < addlen; ++i)
noremapbuf[typeoff + i + offset] = (noremap-- > 0);
/* this is only correct for offset == 0! */
if (nottyped) /* the inserted string is not typed */
typemaplen += addlen;
if (no_abbr_cnt && offset == 0) /* and not used for abbreviations */
no_abbr_cnt += addlen;
return OK;
}
/*
* Return TRUE if there are no characters in the typeahead buffer that have
* not been typed (result from a mapping or come from ":normal").
*/
int
typebuf_typed()
{
return typemaplen == 0;
}
/*
* remove "len" characters from typebuf[typeoff + offset]
*/
void
del_typebuf(len, offset)
int len;
int offset;
{
int i;
typelen -= len;
/*
* Easy case: Just increase typeoff.
*/
if (offset == 0 && typebuflen - (typeoff + len) >= MAXMAPLEN + 3)
typeoff += len;
/*
* Have to move the characters in typebuf[] and noremapbuf[]
*/
else
{
i = typeoff + offset;
/*
* Leave some extra room at the end to avoid reallocation.
*/
if (typeoff > MAXMAPLEN)
{
vim_memmove(typebuf + MAXMAPLEN, typebuf + typeoff, (size_t)offset);
vim_memmove(noremapbuf + MAXMAPLEN, noremapbuf + typeoff,
(size_t)offset);
typeoff = MAXMAPLEN;
}
/* adjust typebuf (include the NUL at the end) */
vim_memmove(typebuf + typeoff + offset, typebuf + i + len,
(size_t)(typelen - offset + 1));
/* adjust noremapbuf[] */
vim_memmove(noremapbuf + typeoff + offset, noremapbuf + i + len,
(size_t)(typelen - offset));
}
if (typemaplen > offset) /* adjust typemaplen */
{
if (typemaplen < offset + len)
typemaplen = offset;
else
typemaplen -= len;
}
if (no_abbr_cnt > offset) /* adjust no_abbr_cnt */
{
if (no_abbr_cnt < offset + len)
no_abbr_cnt = offset;
else
no_abbr_cnt -= len;
}
}
/*
* Write typed characters to script file.
* If recording is on put the character in the recordbuffer.
*/
static void
gotchars(s, len)
char_u *s;
int len;
{
int c;
char_u buf[2];
/* remember how many chars were last recorded */
if (Recording)
last_recorded_len += len;
buf[1] = NUL;
while (len--)
{
c = *s++;
updatescript(c);
if (Recording)
{
buf[0] = c;
add_buff(&recordbuff, buf);
}
}
may_sync_undo();
}
/*
* Sync undo. Called when typed characters are obtained from the typeahead
* buffer, or when a menu is used.
* Do not sync in insert mode, unless cursor key has been used.
* Also don't sync while reading a script file.
*/
static void
may_sync_undo()
{
if ((!(State & (INSERT + CMDLINE)) || arrow_used) &&
scriptin[curscript] == NULL)
u_sync();
}
/*
* open new script file for ":so!" command
* return OK on success, FAIL on error
*/
int
openscript(name)
char_u *name;
{
int oldcurscript;
if (curscript + 1 == NSCRIPT)
{
emsg(e_nesting);
return FAIL;
}
else
{
if (scriptin[curscript] != NULL) /* already reading script */
++curscript;
/* use NameBuff for expanded name */
expand_env(name, NameBuff, MAXPATHL);
if ((scriptin[curscript] = fopen((char *)NameBuff, READBIN)) == NULL)
{
emsg2(e_notopen, name);
if (curscript)
--curscript;
return FAIL;
}
/*
* With command ":g/pat/so! file" we have to execute the
* commands from the file now.
*/
if (global_busy)
{
State = NORMAL;
oldcurscript = curscript;
do
{
normal();
vpeekc(); /* check for end of file */
}
while (scriptin[oldcurscript]);
State = CMDLINE;
}
}
return OK;
}
/*
* updatescipt() is called when a character can be written into the script file
* or when we have waited some time for a character (c == 0)
*
* All the changed memfiles are synced if c == 0 or when the number of typed
* characters reaches 'updatecount' and 'updatecount' is non-zero.
*/
void
updatescript(c)
int c;
{
static int count = 0;
if (c && scriptout)
putc(c, scriptout);
if (c == 0 || (p_uc > 0 && ++count >= p_uc))
{
ml_sync_all(c == 0, TRUE);
count = 0;
}
}
#define K_NEEDMORET -1 /* keylen value for incomplete key-code */
#define M_NEEDMORET -2 /* keylen value for incomplete mapping */
static int old_char = -1; /* ungotten character */
int
vgetc()
{
int c, c2;
mod_mask = 0x0;
last_recorded_len = 0;
for (;;) /* this is done twice if there are modifiers */
{
if (mod_mask) /* no mapping after modifier has been read */
{
++no_mapping;
++allow_keys;
}
c = vgetorpeek(TRUE);
if (mod_mask)
{
--no_mapping;
--allow_keys;
}
/* Get two extra bytes for special keys */
if (c == K_SPECIAL)
{
++no_mapping;
c2 = vgetorpeek(TRUE); /* no mapping for these chars */
c = vgetorpeek(TRUE);
--no_mapping;
if (c2 == KS_MODIFIER)
{
mod_mask = c;
continue;
}
c = TO_SPECIAL(c2, c);
}
#ifdef MSDOS
/*
* If K_NUL was typed, it is replaced by K_NUL, 3 in mch_inchar().
* Delete the 3 here.
*/
else if (c == K_NUL && vpeekc() == 3)
(void)vgetorpeek(TRUE);
#endif
return check_shifted_spec_key(c);
}
}
int
vpeekc()
{
return (vgetorpeek(FALSE));
}
/*
* Call vpeekc() without causing anything to be mapped.
* Return TRUE if a character is available, FALSE otherwise.
*/
int
char_avail()
{
int retval;
++no_mapping;
retval = vgetorpeek(FALSE);
--no_mapping;
return (retval != NUL);
}
void
vungetc(c) /* unget one character (can only be done once!) */
int c;
{
old_char = c;
}
/*
* get a character: 1. from a previously ungotten character
* 2. from the stuffbuffer
* 3. from the typeahead buffer
* 4. from the user
*
* if "advance" is TRUE (vgetc()):
* really get the character.
* KeyTyped is set to TRUE in the case the user typed the key.
* KeyStuffed is TRUE if the character comes from the stuff buffer.
* if "advance" is FALSE (vpeekc()):
* just look whether there is a character available.
*/
static int
vgetorpeek(advance)
int advance;
{
register int c, c1;
int keylen = 0; /* init for gcc */
#ifdef AMIGA
char_u *s;
#endif
register struct mapblock *mp;
int timedout = FALSE; /* waited for more than 1 second
for mapping to complete */
int mapdepth = 0; /* check for recursive mapping */
int mode_deleted = FALSE; /* set when mode has been deleted */
int local_State;
register int mlen;
int max_mlen;
int i;
#ifdef USE_GUI
int idx;
#endif
/*
* VISUAL state is never set, it is used only here, therefore a check is
* made if NORMAL state is actually VISUAL state.
*/
local_State = State;
if ((State & NORMAL) && VIsual_active)
local_State = VISUAL;
/*
* get a character: 1. from a previously ungotten character
*/
if (old_char >= 0)
{
c = old_char;
if (advance)
old_char = -1;
return c;
}
if (advance)
KeyStuffed = FALSE;
init_typebuf();
start_stuff();
if (advance && typemaplen == 0)
Exec_reg = FALSE;
do
{
/*
* get a character: 2. from the stuffbuffer
*/
c = read_stuff(advance);
if (c != NUL && !got_int)
{
if (advance)
{
KeyTyped = FALSE;
KeyStuffed = TRUE;
}
if (no_abbr_cnt == 0)
no_abbr_cnt = 1; /* no abbreviations now */
}
else
{
/*
* Loop until we either find a matching mapped key, or we
* are sure that it is not a mapped key.
* If a mapped key sequence is found we go back to the start to
* try re-mapping.
*/
for (;;)
{
mch_breakcheck(); /* check for CTRL-C */
if (got_int)
{
c = inchar(typebuf, MAXMAPLEN, 0L); /* flush all input */
/*
* If inchar returns TRUE (script file was active) or we are
* inside a mapping, get out of insert mode.
* Otherwise we behave like having gotten a CTRL-C.
* As a result typing CTRL-C in insert mode will
* really insert a CTRL-C.
*/
if ((c || typemaplen) && (State & (INSERT + CMDLINE)))
c = ESC;
else
c = Ctrl('C');
flush_buffers(TRUE); /* flush all typeahead */
break;
}
else if (typelen > 0) /* check for a mappable key sequence */
{
/*
* walk through the maplist until we find an
* entry that matches.
*
* Don't look for mappings if:
* - timed out
* - no_mapping set: mapping disabled (e.g. for CTRL-V)
* - typebuf[typeoff] should not be remapped
* - in insert or cmdline mode and 'paste' option set
* - waiting for "hit return to continue" and CR or SPACE
* typed
* - waiting for a char with --more--
* - in Ctrl-X mode, and we get a valid char for that mode
*/
mp = NULL;
max_mlen = 0;
if (!timedout && no_mapping == 0 && (typemaplen == 0 ||
(p_remap && noremapbuf[typeoff] == FALSE))
&& !(p_paste && (State & (INSERT + CMDLINE)))
&& !(State == HITRETURN && (typebuf[typeoff] == CR
|| typebuf[typeoff] == ' '))
&& State != ASKMORE
#ifdef INSERT_EXPAND
&& !(ctrl_x_mode && is_ctrl_x_key(typebuf[typeoff]))
#endif
)
{
c1 = typebuf[typeoff];
#ifdef HAVE_LANGMAP
LANGMAP_ADJUST(c1, TRUE);
#endif
for (mp = maplist.m_next; mp; mp = mp->m_next)
{
/*
* Only consider an entry if
* - the first character matches and
* - it is not an abbreviation and
* - it is for the current state
*/
if ( mp->m_keys[0] == c1 &&
!(mp->m_mode & ABBREV) &&
(mp->m_mode & local_State))
{
int n;
#ifdef HAVE_LANGMAP
int c2;
#endif
/* find the match length of this mapping */
for (mlen = 1; mlen < typelen; ++mlen)
{
#ifdef HAVE_LANGMAP
c2 = typebuf[typeoff + mlen];
LANGMAP_ADJUST(c2, TRUE);
if (mp->m_keys[mlen] != c2)
#else
if (mp->m_keys[mlen] !=
typebuf[typeoff + mlen])
#endif
break;
}
/* if one of the typed keys cannot be
* remapped, skip the entry */
for (n = 0; n < mlen; ++n)
if (noremapbuf[typeoff + n] == TRUE)
break;
if (n != mlen)
continue;
/* (partly) match found */
keylen = mp->m_keylen;
if (mlen == (keylen > typelen ?
typelen : keylen))
{
/* partial match, need more chars */
if (keylen > typelen)
keylen = M_NEEDMORET;
break;
}
/* no match, may have to check for
* termcode at next character */
if (max_mlen < mlen)
max_mlen = mlen;
}
}
}
if (mp == NULL) /* no match found */
{
/*
* check if we have a terminal code, when
* mapping is allowed,
* keys have not been mapped,
* and not an ESC sequence, not in insert mode or
* p_ek is on,
* and when not timed out,
*/
if ((no_mapping == 0 || allow_keys != 0) &&
(typemaplen == 0 ||
(p_remap && noremapbuf[typeoff] == FALSE)) &&
!timedout)
keylen = check_termcode(max_mlen + 1);
else
keylen = 0;
if (keylen == 0) /* no matching terminal code */
{
#ifdef AMIGA /* check for window bounds report */
if (typemaplen == 0 &&
(typebuf[typeoff] & 0xff) == CSI)
{
for (s = typebuf + typeoff + 1;
s < typebuf + typeoff + typelen &&
(isdigit(*s) || *s == ';' || *s == ' ');
++s)
;
if (*s == 'r' || *s == '|') /* found one */
{
del_typebuf((int)(s + 1 -
(typebuf + typeoff)), 0);
/* get size and redraw screen */
set_winsize(0, 0, FALSE);
continue;
}
if (*s == NUL) /* need more characters */
keylen = K_NEEDMORET;
}
if (keylen >= 0)
#endif
{
/*
* get a character: 3. from the typeahead buffer
*/
c = typebuf[typeoff] & 255;
if (advance) /* remove chars from typebuf */
{
if (typemaplen)
KeyTyped = FALSE;
else
{
KeyTyped = TRUE;
/* write char to script file(s) */
gotchars(typebuf + typeoff, 1);
}
del_typebuf(1, 0);
}
break; /* got character, break for loop */
}
}
if (keylen > 0) /* full matching terminal code */
{
#ifdef USE_GUI
if (typebuf[typeoff] == K_SPECIAL &&
typebuf[typeoff + 1] == KS_MENU)
{
/*
* Using a menu may cause a break in undo!
* It's like using gotchars(), but without
* recording or writing to a script file.
*/
may_sync_undo();
del_typebuf(3, 0);
idx = gui_get_menu_index(current_menu,
local_State);
if (idx != MENU_INDEX_INVALID)
{
ins_typebuf(current_menu->strings[idx],
current_menu->noremap[idx] ? -1 : 0,
0, TRUE);
}
}
#endif /* USE_GUI */
continue; /* try mapping again */
}
/* partial match: get some more characters */
keylen = K_NEEDMORET;
}
/* complete match */
if (keylen >= 0 && keylen <= typelen)
{
/* write chars to script file(s) */
if (keylen > typemaplen)
gotchars(typebuf + typeoff + typemaplen,
keylen - typemaplen);
del_typebuf(keylen, 0); /* remove the mapped keys */
/*
* Put the replacement string in front of mapstr.
* The depth check catches ":map x y" and ":map y x".
*/
if (++mapdepth >= p_mmd)
{
EMSG("recursive mapping");
if (State == CMDLINE)
redrawcmdline();
else
setcursor();
flush_buffers(FALSE);
mapdepth = 0; /* for next one */
c = -1;
break;
}
/*
* Insert the 'to' part in the typebuf.
* If 'from' field is the same as the start of the
* 'to' field, don't remap the first character.
* If m_noremap is set, don't remap the whole 'to'
* part.
*/
if (ins_typebuf(mp->m_str, mp->m_noremap ? -1 :
STRNCMP(mp->m_str, mp->m_keys,
(size_t)keylen) ? 0 : 1,
0, TRUE) == FAIL)
{
c = -1;
break;
}
continue;
}
}
/*
* special case: if we get an <ESC> in insert mode and there
* are no more characters at once, we pretend to go out of
* insert mode. This prevents the one second delay after
* typing an <ESC>. If we get something after all, we may
* have to redisplay the mode. That the cursor is in the wrong
* place does not matter.
*/
c = 0;
if (advance && typelen == 1 && typebuf[typeoff] == ESC &&
!no_mapping && typemaplen == 0 && (State & INSERT) &&
(p_timeout || (keylen == K_NEEDMORET && p_ttimeout)) &&
(c = inchar(typebuf + typeoff + typelen, 3, 0L)) == 0)
{
if (p_smd)
{
delmode();
mode_deleted = TRUE;
}
if (curwin->w_cursor.col != 0) /* move cursor one left if
possible */
{
if (curwin->w_col)
{
if (did_ai)
{
/*
* We are expecting to truncate the trailing
* white-space, so find the last non-white
* character -- webb
*/
colnr_t col, vcol;
char_u *ptr;
col = vcol = curwin->w_col = 0;
ptr = ml_get_curline();
while (col < curwin->w_cursor.col)
{
if (!vim_iswhite(ptr[col]))
curwin->w_col = vcol;
vcol += lbr_chartabsize(ptr + col,
(colnr_t)vcol);
++col;
}
if (curwin->w_p_nu)
curwin->w_col += 8;
}
else
--curwin->w_col;
}
else if (curwin->w_p_wrap && curwin->w_row)
{
--curwin->w_row;
curwin->w_col = Columns - 1;
}
}
setcursor();
flushbuf();
}
typelen += c;
/* buffer full, don't map */
if (typelen >= typemaplen + MAXMAPLEN)
{
timedout = TRUE;
continue;
}
/*
* get a character: 4. from the user
*/
/*
* If we have a partial match (and are going to wait for more
* input from the user), show the partially matched characters
* to the user with showcmd -- webb.
*/
i = 0;
if (typelen > 0 && (State & (NORMAL | INSERT)) && advance)
{
push_showcmd();
while (i < typelen)
(void)add_to_showcmd(typebuf[typeoff + i++], TRUE);
}
c = inchar(typebuf + typeoff + typelen,
typemaplen + MAXMAPLEN - typelen,
!advance
? 0
: ((typelen == 0 || !(p_timeout || (p_ttimeout &&
keylen == K_NEEDMORET)))
? -1L
: ((keylen == K_NEEDMORET && p_ttm >= 0)
? p_ttm
: p_tm)));
if (i)
pop_showcmd();
if (c <= NUL) /* no character available */
{
if (!advance)
break;
if (typelen) /* timed out */
{
timedout = TRUE;
continue;
}
}
else
{ /* allow mapping for just typed characters */
while (typebuf[typeoff + typelen] != NUL)
noremapbuf[typeoff + typelen++] = FALSE;
}
} /* for (;;) */
} /* if (!character from stuffbuf) */
/* if advance is FALSE don't loop on NULs */
} while (c < 0 || (advance && c == NUL));
/*
* The "INSERT" message is taken care of here:
* if we return an ESC to exit insert mode, the message is deleted
* if we don't return an ESC but deleted the message before, redisplay it
*/
if (p_smd && (State & INSERT))
{
if (c == ESC && !mode_deleted && !no_mapping)
delmode();
else if (c != ESC && mode_deleted)
showmode();
}
return c;
}
/*
* inchar() - get one character from
* 1. a scriptfile
* 2. the keyboard
*
* As much characters as we can get (upto 'maxlen') are put in buf and
* NUL terminated (buffer length must be 'maxlen' + 1).
* Minimum for 'maxlen' is 3!!!!
*
* If we got an interrupt all input is read until none is available.
*
* If wait_time == 0 there is no waiting for the char.
* If wait_time == n we wait for n msec for a character to arrive.
* If wait_time == -1 we wait forever for a character to arrive.
*
* Return the number of obtained characters.
*/
static int
inchar(buf, maxlen, wait_time)
char_u *buf;
int maxlen;
long wait_time; /* milli seconds */
{
int len = 0; /* init for GCC */
int retesc = FALSE; /* return ESC with gotint */
register int c;
register int i;
if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */
{
cursor_on();
flushbuf();
}
/*
* Don't reset these when at the hit-return prompt, otherwise a endless
* recursive loop may result (write error in swapfile, hit-return, timeout
* on char wait, flush swapfile, write error....).
*/
if (State != HITRETURN)
{
did_outofmem_msg = FALSE; /* display out of memory message (again) */
did_swapwrite_msg = FALSE; /* display swap file write error again */
}
undo_off = FALSE; /* restart undo now */
/*
* first try script file
* If interrupted: Stop reading script files.
*/
c = -1;
while (scriptin[curscript] != NULL && c < 0)
{
if (got_int || (c = getc(scriptin[curscript])) < 0) /* reached EOF */
{
/* when reading script file is interrupted, return an ESC to
get back to normal mode */
if (got_int)
retesc = TRUE;
fclose(scriptin[curscript]);
scriptin[curscript] = NULL;
if (curscript > 0)
--curscript;
}
else
{
buf[0] = c;
len = 1;
}
}
if (c < 0) /* did not get a character from script */
{
/*
* If we got an interrupt, skip all previously typed characters and
* return TRUE if quit reading script file.
*/
if (got_int) /* skip typed characters */
{
while (mch_inchar(buf, maxlen, 0L))
;
return retesc;
}
/* fill up to a third of the buffer, because each character may be
* tripled below */
len = mch_inchar(buf, maxlen / 3, wait_time);
}
/*
* Two characters are special: NUL and K_SPECIAL.
* Replace NUL by K_SPECIAL KS_ZERO K_FILLER
* Replace K_SPECIAL by K_SPECIAL KS_SPECIAL K_FILLER
* Don't replace K_SPECIAL when reading a script file.
*/
for (i = len; --i >= 0; ++buf)
{
if (buf[0] == NUL || (buf[0] == K_SPECIAL && c < 0))
{
vim_memmove(buf + 3, buf + 1, (size_t)i);
buf[2] = K_THIRD(buf[0]);
buf[1] = K_SECOND(buf[0]);
buf[0] = K_SPECIAL;
buf += 2;
len += 2;
}
}
*buf = NUL; /* add trailing NUL */
return len;
}
/*
* map[!] : show all key mappings
* map[!] {lhs} : show key mapping for {lhs}
* map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs}
* noremap[!] {lhs} {rhs} : same, but no remapping for {rhs}
* unmap[!] {lhs} : remove key mapping for {lhs}
* abbr : show all abbreviations
* abbr {lhs} : show abbreviations for {lhs}
* abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs}
* noreabbr {lhs} {rhs} : same, but no remapping for {rhs}
* unabbr {lhs} : remove abbreviation for {lhs}
*
* maptype == 1 for unmap command, 2 for noremap command.
*
* keys is pointer to any arguments. Note: keys cannot be a read-only string,
* it will be modified.
*
* for :map mode is NORMAL + VISUAL
* for :map! mode is INSERT + CMDLINE
* for :cmap mode is CMDLINE
* for :imap mode is INSERT
* for :nmap mode is NORMAL
* for :vmap mode is VISUAL
* for :abbr mode is INSERT + CMDLINE + ABBREV
* for :iabbr mode is INSERT + ABBREV
* for :cabbr mode is CMDLINE + ABBREV
*
* Return 0 for success
* 1 for invalid arguments
* 2 for no match
* 3 for ambiguety
* 4 for out of mem
*/
int
do_map(maptype, keys, mode)
int maptype;
char_u *keys;
int mode;
{
struct mapblock *mp, *mprev;
char_u *arg;
char_u *p;
int n;
int len = 0; /* init for GCC */
char_u *newstr;
int hasarg;
int haskey;
int did_it = FALSE;
int abbrev = 0;
int round;
char_u *keys_buf = NULL;
char_u *arg_buf = NULL;
int retval = 0;
int do_backslash;
if (mode & ABBREV) /* not a mapping but an abbreviation */
{
abbrev = ABBREV;
mode &= ~ABBREV;
}
/*
* find end of keys and skip CTRL-Vs (and backslashes) in it
* Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'.
* with :unmap white space is included in the keys, no argument possible
*/
p = keys;
do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
while (*p && (maptype == 1 || !vim_iswhite(*p)))
{
if ((p[0] == Ctrl('V') || (do_backslash && p[0] == '\\')) &&
p[1] != NUL)
++p; /* skip CTRL-V or backslash */
++p;
}
if (*p != NUL)
*p++ = NUL;
p = skipwhite(p);
arg = p;
hasarg = (*arg != NUL);
haskey = (*keys != NUL);
/* check for :unmap without argument */
if (maptype == 1 && !haskey)
{
retval = 1;
goto theend;
}
/*
* If mapping has been given as ^V<C_UP> say, then replace the term codes
* with the appropriate two bytes. If it is a shifted special key, unshift
* it too, giving another two bytes.
* replace_termcodes() may move the result to allocated memory, which
* needs to be freed later (*keys_buf and *arg_buf).
* replace_termcodes() also removes CTRL-Vs and sometimes backslashes.
*/
if (haskey)
keys = replace_termcodes(keys, &keys_buf, TRUE);
if (hasarg)
arg = replace_termcodes(arg, &arg_buf, FALSE);
/*
* check arguments and translate function keys
*/
if (haskey)
{
len = STRLEN(keys);
if (len > MAXMAPLEN) /* maximum length of MAXMAPLEN chars */
{
retval = 1;
goto theend;
}
if (abbrev)
{
/*
* If an abbreviation ends in a keyword character, the
* rest must be all keyword-char or all non-keyword-char.
* Otherwise we won't be able to find the start of it in a
* vi-compatible way.
* An abbrevation cannot contain white space.
*/
if (iswordchar(keys[len - 1])) /* ends in keyword char */
for (n = 0; n < len - 2; ++n)
if (iswordchar(keys[n]) != iswordchar(keys[len - 2]))
{
retval = 1;
goto theend;
}
for (n = 0; n < len; ++n)
if (vim_iswhite(keys[n]))
{
retval = 1;
goto theend;
}
}
}
if (haskey && hasarg && abbrev) /* if we will add an abbreviation */
no_abbr = FALSE; /* reset flag that indicates there are
no abbreviations */
if (!haskey || (maptype != 1 && !hasarg))
msg_start();
/*
* Find an entry in the maplist that matches.
* For :unmap we may loop two times: once to try to unmap an entry with a
* matching 'from' part, a second time, if the first fails, to unmap an
* entry with a matching 'to' part. This was done to allow ":ab foo bar" to be
* unmapped by typing ":unab foo", where "foo" will be replaced by "bar" because
* of the abbreviation.
*/
for (round = 0; (round == 0 || maptype == 1) && round <= 1 &&
!did_it && !got_int; ++round)
{
for (mp = maplist.m_next, mprev = &maplist; mp && !got_int;
mprev = mp, mp = mp->m_next)
{
/* skip entries with wrong mode */
if (!(mp->m_mode & mode) || (mp->m_mode & ABBREV) != abbrev)
continue;
if (!haskey) /* show all entries */
{
showmap(mp);
did_it = TRUE;
}
else /* do we have a match? */
{
if (round) /* second round: try 'to' string for unmap */
{
n = STRLEN(mp->m_str);
p = mp->m_str;
}
else
{
n = mp->m_keylen;
p = mp->m_keys;
}
if (!STRNCMP(p, keys, (size_t)(n < len ? n : len)))
{
if (maptype == 1) /* delete entry */
{
if (n != len) /* not a full match */
continue;
/*
* We reset the indicated mode bits. If nothing is
* left the entry is deleted below.
*/
mp->m_mode &= (~mode | ABBREV);
did_it = TRUE; /* remember that we did something */
}
else if (!hasarg) /* show matching entry */
{
showmap(mp);
did_it = TRUE;
}
else if (n != len) /* new entry is ambigious */
{
if (abbrev) /* for abbreviations that's ok */
continue;
retval = 3;
goto theend;
}
else
{
mp->m_mode &= (~mode | ABBREV); /* remove mode bits */
if (!(mp->m_mode & ~ABBREV) && !did_it) /* reuse existing entry */
{
newstr = strsave(arg);
if (newstr == NULL)
{
retval = 4; /* no mem */
goto theend;
}
vim_free(mp->m_str);
mp->m_str = newstr;
mp->m_noremap = maptype;
mp->m_mode = mode + abbrev;
did_it = TRUE;
}
}
if (!(mp->m_mode & ~ABBREV)) /* entry can be deleted */
{
map_free(mprev);
mp = mprev; /* continue with next entry */
}
}
}
}
}
if (maptype == 1) /* delete entry */
{
if (!did_it)
retval = 2; /* no match */
goto theend;
}
if (!haskey || !hasarg) /* print entries */
{
if (!did_it)
{
if (abbrev)
MSG("No abbreviation found");
else
MSG("No mapping found");
}
goto theend; /* listing finished */
}
if (did_it) /* have added the new entry already */
goto theend;
/*
* get here when we have to add a new entry
*/
/* allocate a new entry for the maplist */
mp = (struct mapblock *)alloc((unsigned)sizeof(struct mapblock));
if (mp == NULL)
{
retval = 4; /* no mem */
goto theend;
}
mp->m_keys = strsave(keys);
mp->m_str = strsave(arg);
if (mp->m_keys == NULL || mp->m_str == NULL)
{
vim_free(mp->m_keys);
vim_free(mp->m_str);
vim_free(mp);
retval = 4; /* no mem */
goto theend;
}
mp->m_keylen = STRLEN(mp->m_keys);
mp->m_noremap = maptype;
mp->m_mode = mode + abbrev;
/* add the new entry in front of the maplist */
mp->m_next = maplist.m_next;
maplist.m_next = mp;
theend:
vim_free(keys_buf);
vim_free(arg_buf);
return retval;
}
/*
* Delete one entry from the maplist.
* The argument is a pointer to the PREVIOUS entry!
*/
static void
map_free(mprev)
struct mapblock *mprev;
{
struct mapblock *mp;
mp = mprev->m_next;
vim_free(mp->m_keys);
vim_free(mp->m_str);
mprev->m_next = mp->m_next;
vim_free(mp);
}
/*
* Clear all mappings or abbreviations.
* 'abbr' should be FALSE for mappings, TRUE for abbreviations.
*/
void
map_clear(modec, forceit, abbr)
int modec;
int forceit;
int abbr;
{
struct mapblock *mp;
int mode;
if (forceit) /* :mapclear! */
mode = INSERT + CMDLINE;
else if (modec == 'i')
mode = INSERT;
else if (modec == 'n')
mode = NORMAL;
else if (modec == 'c')
mode = CMDLINE;
else if (modec == 'v')
mode = VISUAL;
else
mode = VISUAL + NORMAL;
for (mp = &maplist; mp->m_next != NULL; )
{
if (abbr != !(mp->m_next->m_mode & ABBREV) && mp->m_next->m_mode & mode)
{
mp->m_next->m_mode &= ~mode;
if ((mp->m_next->m_mode & ~ABBREV) == 0) /* entry can be deleted */
{
map_free(mp);
continue;
}
}
mp = mp->m_next;
}
}
static void
showmap(mp)
struct mapblock *mp;
{
int len;
if (msg_didout)
msg_outchar('\n');
if ((mp->m_mode & (INSERT + CMDLINE)) == INSERT + CMDLINE)
MSG_OUTSTR("! ");
else if (mp->m_mode & INSERT)
MSG_OUTSTR("i ");
else if (mp->m_mode & CMDLINE)
MSG_OUTSTR("c ");
else if (!(mp->m_mode & VISUAL))
MSG_OUTSTR("n ");
else if (!(mp->m_mode & NORMAL))
MSG_OUTSTR("v ");
else
MSG_OUTSTR(" ");
/* Get length of what we write */
len = msg_outtrans_special(mp->m_keys, TRUE);
do
{
msg_outchar(' '); /* padd with blanks */
++len;
} while (len < 12);
if (mp->m_noremap)
msg_outchar('*');
else
msg_outchar(' ');
/* Use FALSE below if we only want things like <Up> to show up as such on
* the rhs, and not M-x etc, TRUE gets both -- webb
*/
msg_outtrans_special(mp->m_str, TRUE);
flushbuf(); /* show one line at a time */
}
/*
* Check for an abbreviation.
* Cursor is at ptr[col]. When inserting, mincol is where insert started.
* "c" is the character typed before check_abbr was called.
*
* Historic vi practice: The last character of an abbreviation must be an id
* character ([a-zA-Z0-9_]). The characters in front of it must be all id
* characters or all non-id characters. This allows for abbr. "#i" to
* "#include".
*
* Vim addition: Allow for abbreviations that end in a non-keyword character.
* Then there must be white space before the abbr.
*
* return TRUE if there is an abbreviation, FALSE if not
*/
int
check_abbr(c, ptr, col, mincol)
int c;
char_u *ptr;
int col;
int mincol;
{
int len;
int j;
char_u tb[4];
struct mapblock *mp;
int is_id = TRUE;
int vim_abbr;
if (no_abbr_cnt) /* abbrev. are not recursive */
return FALSE;
/*
* Check for word before the cursor: If it ends in a keyword char all
* chars before it must be al keyword chars or non-keyword chars, but not
* white space. If it ends in a non-keyword char we accept any characters
* before it except white space.
*/
if (col == 0) /* cannot be an abbr. */
return FALSE;
if (!iswordchar(ptr[col - 1]))
vim_abbr = TRUE; /* Vim added abbr. */
else
{
vim_abbr = FALSE; /* vi compatible abbr. */
if (col > 1)
is_id = iswordchar(ptr[col - 2]);
}
for (len = col - 1; len > 0 && !vim_isspace(ptr[len - 1]) &&
(vim_abbr || is_id == iswordchar(ptr[len - 1])); --len)
;
if (len < mincol)
len = mincol;
if (len < col) /* there is a word in front of the cursor */
{
ptr += len;
len = col - len;
for (mp = maplist.m_next; mp; mp = mp->m_next)
{
/* find entries with right mode and keys */
if ((mp->m_mode & ABBREV) == ABBREV &&
(mp->m_mode & State) &&
mp->m_keylen == len &&
!STRNCMP(mp->m_keys, ptr, (size_t)len))
break;
}
if (mp)
{
/*
* Found a match:
* Insert the rest of the abbreviation in typebuf[].
* This goes from end to start.
*
* Characters 0x000 - 0x100: normal chars, may need CTRL-V,
* except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL K_FILLER
* Characters where IS_SPECIAL() == TRUE: key codes, need
* K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V.
*/
j = 0;
/* special key code, split up */
if (IS_SPECIAL(c) || c == K_SPECIAL)
{
tb[j++] = K_SPECIAL;
tb[j++] = K_SECOND(c);
c = K_THIRD(c);
}
else if (c < 0x100 && (c < ' ' || c > '~'))
tb[j++] = Ctrl('V'); /* special char needs CTRL-V */
tb[j++] = c;
tb[j] = NUL;
/* insert the last typed char */
(void)ins_typebuf(tb, TRUE, 0, TRUE);
/* insert the to string */
(void)ins_typebuf(mp->m_str, mp->m_noremap ? -1 : 0, 0, TRUE);
/* no abbrev. for these chars */
no_abbr_cnt += STRLEN(mp->m_str) + j + 1;
tb[0] = Ctrl('H');
tb[1] = NUL;
while (len--) /* delete the from string */
(void)ins_typebuf(tb, TRUE, 0, TRUE);
return TRUE;
}
}
return FALSE;
}
/*
* Write map commands for the current mappings to an .exrc file.
* Return FAIL on error, OK otherwise.
*/
int
makemap(fd)
FILE *fd;
{
struct mapblock *mp;
char_u c1;
char_u *p;
for (mp = maplist.m_next; mp; mp = mp->m_next)
{
c1 = NUL;
p = (char_u *)"map";
switch (mp->m_mode)
{
case NORMAL + VISUAL:
break;
case NORMAL:
c1 = 'n';
break;
case VISUAL:
c1 = 'v';
break;
case CMDLINE + INSERT:
p = (char_u *)"map!";
break;
case CMDLINE:
c1 = 'c';
break;
case INSERT:
c1 = 'i';
break;
case INSERT + CMDLINE + ABBREV:
p = (char_u *)"abbr";
break;
case CMDLINE + ABBREV:
c1 = 'c';
p = (char_u *)"abbr";
break;
case INSERT + ABBREV:
c1 = 'i';
p = (char_u *)"abbr";
break;
default:
EMSG("makemap: Illegal mode");
return FAIL;
}
if (c1 && putc(c1, fd) < 0)
return FAIL;
if (mp->m_noremap && fprintf(fd, "nore") < 0)
return FAIL;
if (fprintf(fd, (char *)p) < 0)
return FAIL;
if ( putc(' ', fd) < 0 || putescstr(fd, mp->m_keys, FALSE) == FAIL ||
putc(' ', fd) < 0 || putescstr(fd, mp->m_str, FALSE) == FAIL ||
#ifdef USE_CRNL
putc('\r', fd) < 0 ||
#endif
putc('\n', fd) < 0)
return FAIL;
}
return OK;
}
/*
* write escape string to file
*
* return FAIL for failure, OK otherwise
*/
int
putescstr(fd, str, set)
FILE *fd;
char_u *str;
int set; /* TRUE for makeset, FALSE for makemap */
{
int c;
int modifiers;
for ( ; *str; ++str)
{
c = *str;
/*
* Special key codes have to be translated to be able to make sense
* when they are read back.
*/
if (c == K_SPECIAL && !set)
{
modifiers = 0x0;
if (str[1] == KS_MODIFIER)
{
modifiers = str[2];
str += 3;
c = *str;
}
if (c == K_SPECIAL)
{
c = TO_SPECIAL(str[1], str[2]);
str += 2;
}
if (IS_SPECIAL(c) || modifiers) /* special key */
{
fprintf(fd, (char *)get_special_key_name(c, modifiers));
continue;
}
}
/*
* A '\n' in a map command should be written as <NL>.
* A '\n' in a set command should be written as \^V^J.
*/
if (c == NL)
{
if (set)
fprintf(fd, "\\\026\n");
else
fprintf(fd, "<NL>");
continue;
}
/*
* some characters have to be escaped with CTRL-V to
* prevent them from misinterpreted in DoOneCmd().
* A space, Tab and '"' has to be escaped with a backslash to
* prevent it to be misinterpreted in do_set().
*/
if (set && (vim_iswhite(c) || c == '"' || c == '\\'))
{
if (putc('\\', fd) < 0)
return FAIL;
}
else if (c < ' ' || c > '~' || c == '|')
{
if (putc(Ctrl('V'), fd) < 0)
return FAIL;
}
if (putc(c, fd) < 0)
return FAIL;
}
return OK;
}
/*
* Check all mappings for the presence of special key codes.
* Used after ":set term=xxx".
*/
void
check_map_keycodes()
{
struct mapblock *mp;
char_u *p;
int i;
char_u buf[3];
char_u *save_name;
save_name = sourcing_name;
sourcing_name = (char_u *)"mappings";/* don't give error messages */
for (mp = maplist.m_next; mp != NULL; mp = mp->m_next)
{
for (i = 0; i <= 1; ++i) /* do this twice */
{
if (i == 0)
p = mp->m_keys; /* once for the "from" part */
else
p = mp->m_str; /* and once for the "to" part */
while (*p)
{
if (*p == K_SPECIAL)
{
++p;
if (*p < 128) /* only for "normal" termcap entries */
{
buf[0] = p[0];
buf[1] = p[1];
buf[2] = NUL;
(void)add_termcap_entry(buf, FALSE);
}
++p;
}
++p;
}
}
}
sourcing_name = save_name;
}
|