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
|
/* $NetBSD: emacs.c,v 1.38 2018/05/08 16:37:59 kamil Exp $ */
/*
* Emacs-like command line editing and history
*
* created by Ron Natalie at BRL
* modified by Doug Kingston, Doug Gwyn, and Lou Salkind
* adapted to PD ksh by Eric Gisin
*/
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: emacs.c,v 1.38 2018/05/08 16:37:59 kamil Exp $");
#endif
#include "config.h"
#ifdef EMACS
#include <sys/stat.h>
#include <ctype.h>
#include <locale.h>
#include <stdbool.h>
#include "sh.h"
#include "ksh_dir.h"
#include "edit.h"
static Area aedit;
#define AEDIT &aedit /* area for kill ring and macro defns */
#undef CTRL /* _BSD brain damage */
#define CTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F) /* ASCII */
#define UNCTRL(x) ((x) == 0x7F ? '?' : (x) | 0x40) /* ASCII */
#define META(x) ((x) & 0x7f)
#define ISMETA(x) (Flag(FEMACSUSEMETA) && ((x) & 0x80))
/* values returned by keyboard functions */
#define KSTD 0
#define KEOL 1 /* ^M, ^J */
#define KINTR 2 /* ^G, ^C */
struct x_ftab {
int (*xf_func) ARGS((int c));
const char *xf_name;
short xf_flags;
};
/* index into struct x_ftab x_ftab[] - small is good */
typedef unsigned char Findex;
struct x_defbindings {
Findex xdb_func; /* XFUNC_* */
unsigned char xdb_tab;
unsigned char xdb_char;
};
#define XF_ARG 1 /* command takes number prefix */
#define XF_NOBIND 2 /* not allowed to bind to function */
#define XF_PREFIX 4 /* function sets prefix */
/* Separator for completion */
#define is_cfs(c) (c == ' ' || c == '\t' || c == '"' || c == '\'')
#define is_mfs(c) (!(isalnum((unsigned char)c) || c == '_' || c == '$')) /* Separator for motion */
# define CHARMASK 0xFF /* 8-bit character mask */
# define X_NTABS 3 /* normal, meta1, meta2 */
#define X_TABSZ (CHARMASK+1) /* size of keydef tables etc */
/* Arguments for do_complete()
* 0 = enumerate M-= complete as much as possible and then list
* 1 = complete M-Esc
* 2 = list M-?
*/
typedef enum { CT_LIST, /* list the possible completions */
CT_COMPLETE, /* complete to longest prefix */
CT_COMPLIST /* complete and then list (if non-exact) */
} Comp_type;
/* { from 4.9 edit.h */
/*
* The following are used for my horizontal scrolling stuff
*/
static char *xbuf; /* beg input buffer */
static char *xend; /* end input buffer */
static char *xcp; /* current position */
static char *xep; /* current end */
static char *xbp; /* start of visible portion of input buffer */
static char *xlp; /* last char visible on screen */
static int x_adj_ok;
/*
* we use x_adj_done so that functions can tell
* whether x_adjust() has been called while they are active.
*/
static int x_adj_done;
static int xx_cols;
static int x_col;
static int x_displen;
static int x_arg; /* general purpose arg */
static int x_arg_defaulted;/* x_arg not explicitly set; defaulted to 1 */
static int xlp_valid;
/* end from 4.9 edit.h } */
static int x_prefix1 = CTRL('['), x_prefix2 = CTRL('X');
static char **x_histp; /* history position */
static int x_nextcmd; /* for newline-and-next */
static char *xmp; /* mark pointer */
static Findex x_last_command;
static Findex (*x_tab)[X_TABSZ]; /* key definition */
static char *(*x_atab)[X_TABSZ]; /* macro definitions */
static unsigned char x_bound[(X_TABSZ * X_NTABS + 7) / 8];
#define KILLSIZE 20
static char *killstack[KILLSIZE];
static int killsp, killtp;
static int x_curprefix;
static char *macroptr;
static int prompt_trunc;
static int prompt_skip;
static int x_ins ARGS((char *cp));
static void x_delete ARGS((int nc, int push));
static int x_bword ARGS((void));
static int x_fword ARGS((void));
static void x_goto ARGS((char *cp));
static void x_bs ARGS((int c));
static int x_size_str ARGS((char *cp));
static int x_size ARGS((int c));
static void x_zots ARGS((char *str));
static void x_zotc ARGS((int c));
static void x_load_hist ARGS((char **hp));
static int x_search ARGS((char *pat, int sameline, int offset));
static int x_match ARGS((char *str, char *pat));
static void x_redraw ARGS((int limit));
static void x_push ARGS((int nchars));
static char * x_mapin ARGS((const char *cp, Area *area));
static char * x_mapout ARGS((int c));
static void x_print ARGS((int prefix, int key));
static void x_adjust ARGS((void));
static void x_e_ungetc ARGS((int c));
static int x_e_getc ARGS((void));
static void x_e_putc ARGS((int c));
static void x_e_puts ARGS((const char *s));
static int x_comment ARGS((int c));
static int x_fold_case ARGS((int c));
static char *x_lastcp ARGS((void));
static void do_complete ARGS((int flags, Comp_type type));
static int x_emacs_putbuf ARGS((const char *s, size_t len));
/* The lines between START-FUNC-TAB .. END-FUNC-TAB are run through a
* script (emacs-gen.sh) that generates emacs.out which contains:
* - function declarations for x_* functions
* - defines of the form XFUNC_<name> where <name> is function
* name, sans leading x_.
* Note that the script treats #ifdef and { 0, 0, 0} specially - use with
* caution.
*/
#include "emacs.out"
static const struct x_ftab x_ftab[] = {
/* @START-FUNC-TAB@ */
{ x_abort, "abort", 0 },
{ x_beg_hist, "beginning-of-history", 0 },
{ x_comp_comm, "complete-command", 0 },
{ x_comp_file, "complete-file", 0 },
{ x_complete, "complete", 0 },
{ x_del_back, "delete-char-backward", XF_ARG },
{ x_del_bword, "delete-word-backward", XF_ARG },
{ x_del_char, "delete-char-forward", XF_ARG },
{ x_del_fword, "delete-word-forward", XF_ARG },
{ x_del_line, "kill-line", 0 },
{ x_draw_line, "redraw", 0 },
{ x_end_hist, "end-of-history", 0 },
{ x_end_of_text, "eot", 0 },
{ x_enumerate, "list", 0 },
{ x_eot_del, "eot-or-delete", XF_ARG },
{ x_error, "error", 0 },
{ x_goto_hist, "goto-history", XF_ARG },
{ x_ins_string, "macro-string", XF_NOBIND },
{ x_insert, "auto-insert", XF_ARG },
{ x_kill, "kill-to-eol", XF_ARG },
{ x_kill_region, "kill-region", 0 },
{ x_list_comm, "list-command", 0 },
{ x_list_file, "list-file", 0 },
{ x_literal, "quote", 0 },
{ x_meta1, "prefix-1", XF_PREFIX },
{ x_meta2, "prefix-2", XF_PREFIX },
{ x_meta_yank, "yank-pop", 0 },
{ x_mv_back, "backward-char", XF_ARG },
{ x_mv_begin, "beginning-of-line", 0 },
{ x_mv_bword, "backward-word", XF_ARG },
{ x_mv_end, "end-of-line", 0 },
{ x_mv_forw, "forward-char", XF_ARG },
{ x_mv_fword, "forward-word", XF_ARG },
{ x_newline, "newline", 0 },
{ x_next_com, "down-history", XF_ARG },
{ x_nl_next_com, "newline-and-next", 0 },
{ x_noop, "no-op", 0 },
{ x_prev_com, "up-history", XF_ARG },
{ x_prev_histword, "prev-hist-word", XF_ARG },
{ x_search_char_forw, "search-character-forward", XF_ARG },
{ x_search_char_back, "search-character-backward", XF_ARG },
{ x_search_hist, "search-history", 0 },
{ x_set_mark, "set-mark-command", 0 },
{ x_stuff, "stuff", 0 },
{ x_stuffreset, "stuff-reset", 0 },
{ x_transpose, "transpose-chars", 0 },
{ x_version, "version", 0 },
{ x_xchg_point_mark, "exchange-point-and-mark", 0 },
{ x_yank, "yank", 0 },
{ x_comp_list, "complete-list", 0 },
{ x_expand, "expand-file", 0 },
{ x_fold_capitalize, "capitalize-word", XF_ARG },
{ x_fold_lower, "downcase-word", XF_ARG },
{ x_fold_upper, "upcase-word", XF_ARG },
{ x_set_arg, "set-arg", XF_NOBIND },
{ x_comment, "comment", 0 },
#ifdef SILLY
{ x_game_of_life, "play-game-of-life", 0 },
#else
{ 0, 0, 0 },
#endif
#ifdef DEBUG
{ x_debug_info, "debug-info", 0 },
#else
{ 0, 0, 0 },
{ 0, 0, 0 },
#endif
/* @END-FUNC-TAB@ */
};
static struct x_defbindings const x_defbindings[] = {
{ XFUNC_del_back, 0, CTRL('?') },
{ XFUNC_del_bword, 1, CTRL('?') },
{ XFUNC_eot_del, 0, CTRL('D') },
{ XFUNC_del_back, 0, CTRL('H') },
{ XFUNC_del_bword, 1, CTRL('H') },
{ XFUNC_del_bword, 1, 'h' },
{ XFUNC_mv_bword, 1, 'b' },
{ XFUNC_mv_fword, 1, 'f' },
{ XFUNC_del_fword, 1, 'd' },
{ XFUNC_mv_back, 0, CTRL('B') },
{ XFUNC_mv_forw, 0, CTRL('F') },
{ XFUNC_search_char_forw, 0, CTRL(']') },
{ XFUNC_search_char_back, 1, CTRL(']') },
{ XFUNC_newline, 0, CTRL('M') },
{ XFUNC_newline, 0, CTRL('J') },
{ XFUNC_end_of_text, 0, CTRL('_') },
{ XFUNC_abort, 0, CTRL('G') },
{ XFUNC_prev_com, 0, CTRL('P') },
{ XFUNC_next_com, 0, CTRL('N') },
{ XFUNC_nl_next_com, 0, CTRL('O') },
{ XFUNC_search_hist, 0, CTRL('R') },
{ XFUNC_beg_hist, 1, '<' },
{ XFUNC_end_hist, 1, '>' },
{ XFUNC_goto_hist, 1, 'g' },
{ XFUNC_mv_end, 0, CTRL('E') },
{ XFUNC_mv_begin, 0, CTRL('A') },
{ XFUNC_draw_line, 0, CTRL('L') },
{ XFUNC_meta1, 0, CTRL('[') },
{ XFUNC_meta2, 0, CTRL('X') },
{ XFUNC_kill, 0, CTRL('K') },
{ XFUNC_yank, 0, CTRL('Y') },
{ XFUNC_meta_yank, 1, 'y' },
{ XFUNC_literal, 0, CTRL('^') },
{ XFUNC_comment, 1, '#' },
#if defined(BRL) && defined(TIOCSTI)
{ XFUNC_stuff, 0, CTRL('T') },
#else
{ XFUNC_transpose, 0, CTRL('T') },
#endif
{ XFUNC_complete, 1, CTRL('[') },
{ XFUNC_comp_list, 0, CTRL('I') },
{ XFUNC_comp_list, 1, '=' },
{ XFUNC_enumerate, 1, '?' },
{ XFUNC_expand, 1, '*' },
{ XFUNC_comp_file, 1, CTRL('X') },
{ XFUNC_comp_comm, 2, CTRL('[') },
{ XFUNC_list_comm, 2, '?' },
{ XFUNC_list_file, 2, CTRL('Y') },
{ XFUNC_set_mark, 1, ' ' },
{ XFUNC_kill_region, 0, CTRL('W') },
{ XFUNC_xchg_point_mark, 2, CTRL('X') },
{ XFUNC_version, 0, CTRL('V') },
#ifdef DEBUG
{ XFUNC_debug_info, 1, CTRL('H') },
#endif
{ XFUNC_prev_histword, 1, '.' },
{ XFUNC_prev_histword, 1, '_' },
{ XFUNC_set_arg, 1, '0' },
{ XFUNC_set_arg, 1, '1' },
{ XFUNC_set_arg, 1, '2' },
{ XFUNC_set_arg, 1, '3' },
{ XFUNC_set_arg, 1, '4' },
{ XFUNC_set_arg, 1, '5' },
{ XFUNC_set_arg, 1, '6' },
{ XFUNC_set_arg, 1, '7' },
{ XFUNC_set_arg, 1, '8' },
{ XFUNC_set_arg, 1, '9' },
{ XFUNC_fold_upper, 1, 'U' },
{ XFUNC_fold_upper, 1, 'u' },
{ XFUNC_fold_lower, 1, 'L' },
{ XFUNC_fold_lower, 1, 'l' },
{ XFUNC_fold_capitalize, 1, 'C' },
{ XFUNC_fold_capitalize, 1, 'c' },
/* These for ansi arrow keys: arguablely shouldn't be here by
* default, but its simpler/faster/smaller than using termcap
* entries.
*/
{ XFUNC_meta2, 1, '[' },
{ XFUNC_meta2, 1, 'O' },
{ XFUNC_prev_com, 2, 'A' },
{ XFUNC_next_com, 2, 'B' },
{ XFUNC_mv_forw, 2, 'C' },
{ XFUNC_mv_back, 2, 'D' },
};
int
x_emacs(buf, len)
char *buf;
size_t len;
{
int c;
const char *p;
int i;
Findex f;
xbp = xbuf = buf; xend = buf + len;
xlp = xcp = xep = buf;
*xcp = 0;
xlp_valid = true;
xmp = NULL;
x_curprefix = 0;
macroptr = (char *) 0;
x_histp = histptr + 1;
x_last_command = XFUNC_error;
xx_cols = x_cols;
x_col = promptlen(prompt, &p);
prompt_skip = p - prompt;
prompt_trunc = x_col - (x_cols - 3 - MIN_EDIT_SPACE);
if (prompt_trunc > 0)
x_col -= prompt_trunc;
else
prompt_trunc = 0;
x_adj_ok = 1;
x_displen = xx_cols - 2 - x_col;
x_adj_done = 0;
pprompt(prompt, prompt_trunc);
if (x_nextcmd >= 0) {
int off = source->line - x_nextcmd;
if (histptr - histlist >= off)
x_load_hist(histptr - off);
x_nextcmd = -1;
}
while (1) {
x_flush();
if ((c = x_e_getc()) < 0)
return 0;
if (ISMETA(c)) {
c = META(c);
x_curprefix = 1;
}
f = x_curprefix == -1 ? XFUNC_insert
: x_tab[x_curprefix][c&CHARMASK];
if (!(x_ftab[f].xf_flags & XF_PREFIX)
&& x_last_command != XFUNC_set_arg)
{
x_arg = 1;
x_arg_defaulted = 1;
}
i = c | (x_curprefix << 8);
x_curprefix = 0;
switch (i = (*x_ftab[f].xf_func)(i)) {
case KSTD:
if (!(x_ftab[f].xf_flags & XF_PREFIX))
x_last_command = f;
break;
case KEOL:
i = xep - xbuf;
return i;
case KINTR: /* special case for interrupt */
trapsig(SIGINT);
x_mode(false);
unwind(LSHELL);
}
}
}
static int
x_insert(c)
int c;
{
char str[2];
/*
* Should allow tab and control chars.
*/
if (c == 0) {
x_e_putc(BEL);
return KSTD;
}
str[0] = c;
str[1] = '\0';
while (x_arg--)
x_ins(str);
return KSTD;
}
static int
x_ins_string(c)
int c;
{
if (macroptr) {
x_e_putc(BEL);
return KSTD;
}
macroptr = x_atab[c>>8][c & CHARMASK];
if (macroptr && !*macroptr) {
/* XXX bell? */
macroptr = (char *) 0;
}
return KSTD;
}
static int x_do_ins(const char *cp, int len);
static int
x_do_ins(cp, len)
const char *cp;
int len;
{
if (xep+len >= xend) {
x_e_putc(BEL);
return -1;
}
memmove(xcp+len, xcp, xep - xcp + 1);
memmove(xcp, cp, len);
xcp += len;
xep += len;
return 0;
}
static int
x_ins(s)
char *s;
{
char *cp = xcp;
int adj = x_adj_done;
if (x_do_ins(s, strlen(s)) < 0)
return -1;
/*
* x_zots() may result in a call to x_adjust()
* we want xcp to reflect the new position.
*/
xlp_valid = false;
x_lastcp();
x_adj_ok = (xcp >= xlp);
x_zots(cp);
if (adj == x_adj_done) /* has x_adjust() been called? */
{
/* no */
for (cp = xlp; cp > xcp; )
x_bs(*--cp);
}
x_adj_ok = 1;
return 0;
}
/*
* this is used for x_escape() in do_complete()
*/
static int
x_emacs_putbuf(s, len)
const char *s;
size_t len;
{
int rval;
if ((rval = x_do_ins(s, len)) != 0)
return (rval);
return (rval);
}
static int
x_del_back(c)
int c;
{
int col = xcp - xbuf;
if (col == 0) {
x_e_putc(BEL);
return KSTD;
}
if (x_arg > col)
x_arg = col;
x_goto(xcp - x_arg);
x_delete(x_arg, false);
return KSTD;
}
static int
x_del_char(c)
int c;
{
int nleft = xep - xcp;
if (!nleft) {
x_e_putc(BEL);
return KSTD;
}
if (x_arg > nleft)
x_arg = nleft;
x_delete(x_arg, false);
return KSTD;
}
/* Delete nc chars to the right of the cursor (including cursor position) */
static void
x_delete(nc, push)
int nc;
int push;
{
int i,j;
char *cp;
if (nc == 0)
return;
if (xmp != NULL && xmp > xcp) {
if (xcp + nc > xmp)
xmp = xcp;
else
xmp -= nc;
}
/*
* This lets us yank a word we have deleted.
*/
if (push)
x_push(nc);
xep -= nc;
cp = xcp;
j = 0;
i = nc;
while (i--) {
j += x_size(*cp++);
}
memmove(xcp, xcp+nc, xep - xcp + 1); /* Copies the null */
x_adj_ok = 0; /* don't redraw */
x_zots(xcp);
/*
* if we are already filling the line,
* there is no need to ' ','\b'.
* But if we must, make sure we do the minimum.
*/
if ((i = x_displen) > 0)
{
j = (j < i) ? j : i;
i = j;
while (i--)
x_e_putc(' ');
i = j;
while (i--)
x_e_putc('\b');
}
/*x_goto(xcp);*/
x_adj_ok = 1;
xlp_valid = false;
for (cp = x_lastcp(); cp > xcp; )
x_bs(*--cp);
return;
}
static int
x_del_bword(c)
int c;
{
x_delete(x_bword(), true);
return KSTD;
}
static int
x_mv_bword(c)
int c;
{
(void)x_bword();
return KSTD;
}
static int
x_mv_fword(c)
int c;
{
x_goto(xcp + x_fword());
return KSTD;
}
static int
x_del_fword(c)
int c;
{
x_delete(x_fword(), true);
return KSTD;
}
static int
x_bword()
{
int nc = 0;
char *cp = xcp;
if (cp == xbuf) {
x_e_putc(BEL);
return 0;
}
while (x_arg--)
{
while (cp != xbuf && is_mfs(cp[-1]))
{
cp--;
nc++;
}
while (cp != xbuf && !is_mfs(cp[-1]))
{
cp--;
nc++;
}
}
x_goto(cp);
return nc;
}
static int
x_fword()
{
int nc = 0;
char *cp = xcp;
if (cp == xep) {
x_e_putc(BEL);
return 0;
}
while (x_arg--)
{
while (cp != xep && is_mfs(*cp))
{
cp++;
nc++;
}
while (cp != xep && !is_mfs(*cp))
{
cp++;
nc++;
}
}
return nc;
}
static void
x_goto(cp)
char *cp;
{
if (cp < xbp || cp >= (xbp + x_displen))
{
/* we are heading off screen */
xcp = cp;
x_adjust();
}
else
{
if (cp < xcp) /* move back */
{
while (cp < xcp)
x_bs(*--xcp);
}
else
{
if (cp > xcp) /* move forward */
{
while (cp > xcp)
x_zotc(*xcp++);
}
}
}
}
static void
x_bs(c)
int c;
{
int i;
i = x_size(c);
while (i--)
x_e_putc('\b');
}
static int
x_size_str(cp)
char *cp;
{
int size = 0;
while (*cp)
size += x_size(*cp++);
return size;
}
static int
x_size(c)
int c;
{
if (c=='\t')
return 4; /* Kludge, tabs are always four spaces. */
if (iscntrl((unsigned char)c)) /* control char */
return 2;
return 1;
}
static void
x_zots(str)
char *str;
{
int adj = x_adj_done;
x_lastcp();
while (*str && str < xlp && adj == x_adj_done)
x_zotc(*str++);
}
static void
x_zotc(c)
int c;
{
if (c == '\t') {
/* Kludge, tabs are always four spaces. */
x_e_puts(" ");
} else if (iscntrl((unsigned char)c)) {
x_e_putc('^');
x_e_putc(UNCTRL(c));
} else
x_e_putc(c);
}
static int
x_mv_back(c)
int c;
{
int col = xcp - xbuf;
if (col == 0) {
x_e_putc(BEL);
return KSTD;
}
if (x_arg > col)
x_arg = col;
x_goto(xcp - x_arg);
return KSTD;
}
static int
x_mv_forw(c)
int c;
{
int nleft = xep - xcp;
if (!nleft) {
x_e_putc(BEL);
return KSTD;
}
if (x_arg > nleft)
x_arg = nleft;
x_goto(xcp + x_arg);
return KSTD;
}
static int
x_search_char_forw(c)
int c;
{
char *cp = xcp;
*xep = '\0';
c = x_e_getc();
while (x_arg--) {
if (c < 0
|| ((cp = (cp == xep) ? NULL : strchr(cp + 1, c)) == NULL
&& (cp = strchr(xbuf, c)) == NULL))
{
x_e_putc(BEL);
return KSTD;
}
}
x_goto(cp);
return KSTD;
}
static int
x_search_char_back(c)
int c;
{
char *cp = xcp, *p;
c = x_e_getc();
for (; x_arg--; cp = p)
for (p = cp; ; ) {
if (p-- == xbuf)
p = xep;
if (c < 0 || p == cp) {
x_e_putc(BEL);
return KSTD;
}
if (*p == c)
break;
}
x_goto(cp);
return KSTD;
}
static int
x_newline(c)
int c;
{
x_e_putc('\r');
x_e_putc('\n');
x_flush();
*xep++ = '\n';
return KEOL;
}
static int
x_end_of_text(c)
int c;
{
return KEOL;
}
static int x_beg_hist(c) int c; { x_load_hist(histlist); return KSTD;}
static int x_end_hist(c) int c; { x_load_hist(histptr); return KSTD;}
static int x_prev_com(c) int c; { x_load_hist(x_histp - x_arg); return KSTD;}
static int x_next_com(c) int c; { x_load_hist(x_histp + x_arg); return KSTD;}
/* Goto a particular history number obtained from argument.
* If no argument is given history 1 is probably not what you
* want so we'll simply go to the oldest one.
*/
static int
x_goto_hist(c)
int c;
{
if (x_arg_defaulted)
x_load_hist(histlist);
else
x_load_hist(histptr + x_arg - source->line);
return KSTD;
}
static void
x_load_hist(hp)
char **hp;
{
int oldsize;
if (hp < histlist || hp > histptr) {
x_e_putc(BEL);
return;
}
x_histp = hp;
oldsize = x_size_str(xbuf);
strlcpy(xbuf, *hp, xend - xbuf);
xbp = xbuf;
xep = xcp = xbuf + strlen(xbuf);
xlp_valid = false;
if (xep > x_lastcp())
x_goto(xep);
else
x_redraw(oldsize);
}
static int
x_nl_next_com(c)
int c;
{
x_nextcmd = source->line - (histptr - x_histp) + 1;
return (x_newline(c));
}
static int
x_eot_del(c)
int c;
{
if (xep == xbuf && x_arg_defaulted)
return (x_end_of_text(c));
else
return (x_del_char(c));
}
/* reverse incremental history search */
static int
x_search_hist(c)
int c;
{
int offset = -1; /* offset of match in xbuf, else -1 */
char pat [256+1]; /* pattern buffer */
char *p = pat;
Findex f;
*p = '\0';
while (1) {
if (offset < 0) {
x_e_puts("\nI-search: ");
x_e_puts(pat);
}
x_flush();
if ((c = x_e_getc()) < 0)
return KSTD;
f = x_tab[0][c&CHARMASK];
if (c == CTRL('['))
break;
else if (f == XFUNC_search_hist)
offset = x_search(pat, 0, offset);
else if (f == XFUNC_del_back) {
if (p == pat) {
offset = -1;
break;
}
if (p > pat)
*--p = '\0';
if (p == pat)
offset = -1;
else
offset = x_search(pat, 1, offset);
continue;
} else if (f == XFUNC_insert) {
/* add char to pattern */
/* overflow check... */
if (p >= &pat[sizeof(pat) - 1]) {
x_e_putc(BEL);
continue;
}
*p++ = c, *p = '\0';
if (offset >= 0) {
/* already have partial match */
offset = x_match(xbuf, pat);
if (offset >= 0) {
x_goto(xbuf + offset + (p - pat) - (*pat == '^'));
continue;
}
}
offset = x_search(pat, 0, offset);
} else { /* other command */
x_e_ungetc(c);
break;
}
}
if (offset < 0)
x_redraw(-1);
return KSTD;
}
/* search backward from current line */
static int
x_search(pat, sameline, offset)
char *pat;
int sameline;
int offset;
{
char **hp;
int i;
for (hp = x_histp - (sameline ? 0 : 1) ; hp >= histlist; --hp) {
i = x_match(*hp, pat);
if (i >= 0) {
if (offset < 0)
x_e_putc('\n');
x_load_hist(hp);
x_goto(xbuf + i + strlen(pat) - (*pat == '^'));
return i;
}
}
x_e_putc(BEL);
x_histp = histptr;
return -1;
}
/* return position of first match of pattern in string, else -1 */
static int
x_match(str, pat)
char *str, *pat;
{
if (*pat == '^') {
return (strncmp(str, pat+1, strlen(pat+1)) == 0) ? 0 : -1;
} else {
char *q = strstr(str, pat);
return (q == NULL) ? -1 : q - str;
}
}
static int
x_del_line(c)
int c;
{
int i, j;
*xep = 0;
i = xep - xbuf;
j = x_size_str(xbuf);
xcp = xbuf;
x_push(i);
xlp = xbp = xep = xbuf;
xlp_valid = true;
*xcp = 0;
xmp = NULL;
x_redraw(j);
return KSTD;
}
static int
x_mv_end(c)
int c;
{
x_goto(xep);
return KSTD;
}
static int
x_mv_begin(c)
int c;
{
x_goto(xbuf);
return KSTD;
}
static int
x_draw_line(c)
int c;
{
x_redraw(-1);
return KSTD;
}
/* Redraw (part of) the line. If limit is < 0, the everything is redrawn
* on a NEW line, otherwise limit is the screen column up to which needs
* redrawing.
*/
static void
x_redraw(limit)
int limit;
{
int i, j;
char *cp;
x_adj_ok = 0;
if (limit == -1)
x_e_putc('\n');
else
x_e_putc('\r');
x_flush();
if (xbp == xbuf)
{
pprompt(prompt + prompt_skip, 0);
x_col = promptlen(prompt, (const char **) 0);
}
x_displen = xx_cols - 2 - x_col;
xlp_valid = false;
cp = x_lastcp();
x_zots(xbp);
if (xbp != xbuf || xep > xlp)
limit = xx_cols;
if (limit >= 0)
{
if (xep > xlp)
i = 0; /* we fill the line */
else
i = limit - (xlp - xbp);
for (j = 0; j < i && x_col < (xx_cols - 2); j++)
x_e_putc(' ');
i = ' ';
if (xep > xlp) /* more off screen */
{
if (xbp > xbuf)
i = '*';
else
i = '>';
}
else
if (xbp > xbuf)
i = '<';
x_e_putc(i);
j++;
while (j--)
x_e_putc('\b');
}
for (cp = xlp; cp > xcp; )
x_bs(*--cp);
x_adj_ok = 1;
D__(x_flush();)
return;
}
static int
x_transpose(c)
int c;
{
char tmp;
/* What transpose is meant to do seems to be up for debate. This
* is a general summary of the options; the text is abcd with the
* upper case character or underscore indicating the cursor position:
* Who Before After Before After
* at&t ksh in emacs mode: abCd abdC abcd_ (bell)
* at&t ksh in gmacs mode: abCd baCd abcd_ abdc_
* gnu emacs: abCd acbD abcd_ abdc_
* Pdksh currently goes with GNU behavior since I believe this is the
* most common version of emacs, unless in gmacs mode, in which case
* it does the at&t ksh gmacs mode.
* This should really be broken up into 3 functions so users can bind
* to the one they want.
*/
if (xcp == xbuf) {
x_e_putc(BEL);
return KSTD;
} else if (xcp == xep || Flag(FGMACS)) {
if (xcp - xbuf == 1) {
x_e_putc(BEL);
return KSTD;
}
/* Gosling/Unipress emacs style: Swap two characters before the
* cursor, do not change cursor position
*/
x_bs(xcp[-1]);
x_bs(xcp[-2]);
x_zotc(xcp[-1]);
x_zotc(xcp[-2]);
tmp = xcp[-1];
xcp[-1] = xcp[-2];
xcp[-2] = tmp;
} else {
/* GNU emacs style: Swap the characters before and under the
* cursor, move cursor position along one.
*/
x_bs(xcp[-1]);
x_zotc(xcp[0]);
x_zotc(xcp[-1]);
tmp = xcp[-1];
xcp[-1] = xcp[0];
xcp[0] = tmp;
x_bs(xcp[0]);
x_goto(xcp + 1);
}
return KSTD;
}
static int
x_literal(c)
int c;
{
x_curprefix = -1;
return KSTD;
}
static int
x_meta1(c)
int c;
{
x_curprefix = 1;
return KSTD;
}
static int
x_meta2(c)
int c;
{
x_curprefix = 2;
return KSTD;
}
static int
x_kill(c)
int c;
{
int col = xcp - xbuf;
int lastcol = xep - xbuf;
int ndel;
if (x_arg_defaulted)
x_arg = lastcol;
else if (x_arg > lastcol)
x_arg = lastcol;
ndel = x_arg - col;
if (ndel < 0) {
x_goto(xbuf + x_arg);
ndel = -ndel;
}
x_delete(ndel, true);
return KSTD;
}
static void
x_push(nchars)
int nchars;
{
char *cp = str_nsave(xcp, nchars, AEDIT);
if (killstack[killsp])
afree((void *)killstack[killsp], AEDIT);
killstack[killsp] = cp;
killsp = (killsp + 1) % KILLSIZE;
}
static int
x_yank(c)
int c;
{
if (killsp == 0)
killtp = KILLSIZE;
else
killtp = killsp;
killtp--;
if (killstack[killtp] == 0) {
x_e_puts("\nnothing to yank");
x_redraw(-1);
return KSTD;
}
xmp = xcp;
x_ins(killstack[killtp]);
return KSTD;
}
static int
x_meta_yank(c)
int c;
{
int len;
if ((x_last_command != XFUNC_yank && x_last_command != XFUNC_meta_yank)
|| killstack[killtp] == 0) {
killtp = killsp;
x_e_puts("\nyank something first");
x_redraw(-1);
return KSTD;
}
len = strlen(killstack[killtp]);
x_goto(xcp - len);
x_delete(len, false);
do {
if (killtp == 0)
killtp = KILLSIZE - 1;
else
killtp--;
} while (killstack[killtp] == 0);
x_ins(killstack[killtp]);
return KSTD;
}
static int
x_abort(c)
int c;
{
/* x_zotc(c); */
xlp = xep = xcp = xbp = xbuf;
xlp_valid = true;
*xcp = 0;
return KINTR;
}
static int
x_error(c)
int c;
{
x_e_putc(BEL);
return KSTD;
}
static int
x_stuffreset(c)
int c;
{
#ifdef TIOCSTI
(void)x_stuff(c);
return KINTR;
#else
x_zotc(c);
xlp = xcp = xep = xbp = xbuf;
xlp_valid = true;
*xcp = 0;
x_redraw(-1);
return KSTD;
#endif
}
static int
x_stuff(c)
int c;
{
#if 0 || defined TIOCSTI
char ch = c;
bool savmode = x_mode(false);
(void)ioctl(TTY, TIOCSTI, &ch);
(void)x_mode(savmode);
x_redraw(-1);
#endif
return KSTD;
}
static char *
x_mapin(cp, area)
const char *cp;
Area *area;
{
char *new, *op;
op = new = str_save(cp, area);
while (*cp) {
/* XXX -- should handle \^ escape? */
if (*cp == '^') {
cp++;
if (*cp >= '?') /* includes '?'; ASCII */
*op++ = CTRL(*cp);
else {
*op++ = '^';
cp--;
}
} else
*op++ = *cp;
cp++;
}
*op = '\0';
return new;
}
static char *
x_mapout(c)
int c;
{
static char buf[8];
char *p = buf;
if (iscntrl((unsigned char)c)) {
*p++ = '^';
*p++ = UNCTRL(c);
} else
*p++ = c;
*p = 0;
return buf;
}
static void
x_print(prefix, key)
int prefix, key;
{
if (prefix == 1)
shprintf("%s", x_mapout(x_prefix1));
if (prefix == 2)
shprintf("%s", x_mapout(x_prefix2));
shprintf("%s = ", x_mapout(key));
if (x_tab[prefix][key] != XFUNC_ins_string)
shprintf("%s\n", x_ftab[x_tab[prefix][key]].xf_name);
else
shprintf("'%s'\n", x_atab[prefix][key]);
}
int
x_bind(a1, a2, macro, list)
const char *a1, *a2;
int macro; /* bind -m */
int list; /* bind -l */
{
Findex f;
int prefix, key;
char *sp = NULL;
char *m1, *m2;
if (x_tab == NULL) {
bi_errorf("cannot bind, not a tty");
return 1;
}
/* List function names */
if (list) {
for (f = 0; f < NELEM(x_ftab); f++)
if (x_ftab[f].xf_name
&& !(x_ftab[f].xf_flags & XF_NOBIND))
shprintf("%s\n", x_ftab[f].xf_name);
return 0;
}
if (a1 == NULL) {
for (prefix = 0; prefix < X_NTABS; prefix++)
for (key = 0; key < X_TABSZ; key++) {
f = x_tab[prefix][key];
if (f == XFUNC_insert || f == XFUNC_error
|| (macro && f != XFUNC_ins_string))
continue;
x_print(prefix, key);
}
return 0;
}
m2 = m1 = x_mapin(a1, ATEMP);
prefix = key = 0;
for (;; m1++) {
key = *m1 & CHARMASK;
if (x_tab[prefix][key] == XFUNC_meta1)
prefix = 1;
else if (x_tab[prefix][key] == XFUNC_meta2)
prefix = 2;
else
break;
}
afree(m2, ATEMP);
if (a2 == NULL) {
x_print(prefix, key);
return 0;
}
if (*a2 == 0)
f = XFUNC_insert;
else if (!macro) {
for (f = 0; f < NELEM(x_ftab); f++)
if (x_ftab[f].xf_name
&& strcmp(x_ftab[f].xf_name, a2) == 0)
break;
if (f == NELEM(x_ftab) || x_ftab[f].xf_flags & XF_NOBIND) {
bi_errorf("%s: no such function", a2);
return 1;
}
#if 0 /* This breaks the bind commands that map arrow keys */
if (f == XFUNC_meta1)
x_prefix1 = key;
if (f == XFUNC_meta2)
x_prefix2 = key;
#endif /* 0 */
} else {
f = XFUNC_ins_string;
sp = x_mapin(a2, AEDIT);
}
if (x_tab[prefix][key] == XFUNC_ins_string && x_atab[prefix][key])
afree((void *)x_atab[prefix][key], AEDIT);
x_tab[prefix][key] = f;
x_atab[prefix][key] = sp;
/* Track what the user has bound so x_emacs_keys() won't toast things */
if (f == XFUNC_insert)
x_bound[(prefix * X_TABSZ + key) / 8] &=
~(1 << ((prefix * X_TABSZ + key) % 8));
else
x_bound[(prefix * X_TABSZ + key) / 8] |=
(1 << ((prefix * X_TABSZ + key) % 8));
return 0;
}
void
x_init_emacs()
{
size_t i;
int j;
char *locale;
ainit(AEDIT);
x_nextcmd = -1;
x_tab = (Findex (*)[X_TABSZ]) alloc(sizeofN(*x_tab, X_NTABS), AEDIT);
for (j = 0; j < X_TABSZ; j++)
x_tab[0][j] = XFUNC_insert;
for (i = 1; i < X_NTABS; i++)
for (j = 0; j < X_TABSZ; j++)
x_tab[i][j] = XFUNC_error;
for (i = 0; i < NELEM(x_defbindings); i++)
x_tab[(unsigned char)x_defbindings[i].xdb_tab][x_defbindings[i].xdb_char]
= x_defbindings[i].xdb_func;
x_atab = (char *(*)[X_TABSZ]) alloc(sizeofN(*x_atab, X_NTABS), AEDIT);
for (i = 1; i < X_NTABS; i++)
for (j = 0; j < X_TABSZ; j++)
x_atab[i][j] = NULL;
/* Determine if we can translate meta key or use 8-bit AscII
* XXX - It would be nice if there was a locale attribute to
* determine if the locale is 7-bit or not.
*/
locale = setlocale(LC_CTYPE, NULL);
if (locale == NULL || !strcmp(locale, "C") || !strcmp(locale, "POSIX"))
Flag(FEMACSUSEMETA) = 0;
}
static void bind_if_not_bound(int p, int k, int func);
static void
bind_if_not_bound(p, k, func)
int p, k;
int func;
{
/* Has user already bound this key? If so, don't override it */
if (x_bound[((p) * X_TABSZ + (k)) / 8]
& (1 << (((p) * X_TABSZ + (k)) % 8)))
return;
x_tab[p][k] = func;
}
void
x_emacs_keys(ec)
X_chars *ec;
{
if (ec->erase >= 0) {
bind_if_not_bound(0, ec->erase, XFUNC_del_back);
bind_if_not_bound(1, ec->erase, XFUNC_del_bword);
}
if (ec->kill >= 0)
bind_if_not_bound(0, ec->kill, XFUNC_del_line);
if (ec->werase >= 0)
bind_if_not_bound(0, ec->werase, XFUNC_del_bword);
if (ec->intr >= 0)
bind_if_not_bound(0, ec->intr, XFUNC_abort);
if (ec->quit >= 0)
bind_if_not_bound(0, ec->quit, XFUNC_noop);
}
static int
x_set_mark(c)
int c;
{
xmp = xcp;
return KSTD;
}
static int
x_kill_region(c)
int c;
{
int rsize;
char *xr;
if (xmp == NULL) {
x_e_putc(BEL);
return KSTD;
}
if (xmp > xcp) {
rsize = xmp - xcp;
xr = xcp;
} else {
rsize = xcp - xmp;
xr = xmp;
}
x_goto(xr);
x_delete(rsize, true);
xmp = xr;
return KSTD;
}
static int
x_xchg_point_mark(c)
int c;
{
char *tmp;
if (xmp == NULL) {
x_e_putc(BEL);
return KSTD;
}
tmp = xmp;
xmp = xcp;
x_goto( tmp );
return KSTD;
}
static int
x_version(c)
int c;
{
char *o_xbuf = xbuf, *o_xend = xend;
char *o_xbp = xbp, *o_xep = xep, *o_xcp = xcp;
int lim = x_lastcp() - xbp;
xbuf = xbp = xcp = ksh_version + 4;
xend = xep = ksh_version + 4 + strlen(ksh_version + 4);
x_redraw(lim);
x_flush();
c = x_e_getc();
xbuf = o_xbuf;
xend = o_xend;
xbp = o_xbp;
xep = o_xep;
xcp = o_xcp;
x_redraw(strlen(ksh_version));
if (c < 0)
return KSTD;
/* This is what at&t ksh seems to do... Very bizarre */
if (c != ' ')
x_e_ungetc(c);
return KSTD;
}
static int
x_noop(c)
int c;
{
return KSTD;
}
#ifdef SILLY
static int
x_game_of_life(c)
int c;
{
char newbuf [256+1];
char *ip, *op;
int i, len;
i = xep - xbuf;
*xep = 0;
len = x_size_str(xbuf);
xcp = xbp = xbuf;
memmove(newbuf+1, xbuf, i);
newbuf[0] = 'A';
newbuf[i] = 'A';
for (ip = newbuf+1, op = xbuf; --i >= 0; ip++, op++) {
/* Empty space */
if (*ip < '@' || *ip == '_' || *ip == 0x7F) {
/* Two adults, make whoopee */
if (ip[-1] < '_' && ip[1] < '_') {
/* Make kid look like parents. */
*op = '`' + ((ip[-1] + ip[1])/2)%32;
if (*op == 0x7F) /* Birth defect */
*op = '`';
}
else
*op = ' '; /* nothing happens */
continue;
}
/* Child */
if (*ip > '`') {
/* All alone, dies */
if (ip[-1] == ' ' && ip[1] == ' ')
*op = ' ';
else /* Gets older */
*op = *ip-'`'+'@';
continue;
}
/* Adult */
/* Overcrowded, dies */
if (ip[-1] >= '@' && ip[1] >= '@') {
*op = ' ';
continue;
}
*op = *ip;
}
*op = 0;
x_redraw(len);
return KSTD;
}
#endif
/*
* File/command name completion routines
*/
static int
x_comp_comm(c)
int c;
{
do_complete(XCF_COMMAND, CT_COMPLETE);
return KSTD;
}
static int
x_list_comm(c)
int c;
{
do_complete(XCF_COMMAND, CT_LIST);
return KSTD;
}
static int
x_complete(c)
int c;
{
do_complete(XCF_COMMAND_FILE, CT_COMPLETE);
return KSTD;
}
static int
x_enumerate(c)
int c;
{
do_complete(XCF_COMMAND_FILE, CT_LIST);
return KSTD;
}
static int
x_comp_file(c)
int c;
{
do_complete(XCF_FILE, CT_COMPLETE);
return KSTD;
}
static int
x_list_file(c)
int c;
{
do_complete(XCF_FILE, CT_LIST);
return KSTD;
}
static int
x_comp_list(c)
int c;
{
do_complete(XCF_COMMAND_FILE, CT_COMPLIST);
return KSTD;
}
static int
x_expand(c)
int c;
{
char **words;
int nwords = 0;
int start, end;
int is_command;
int i;
nwords = x_cf_glob(XCF_FILE,
xbuf, xep - xbuf, xcp - xbuf,
&start, &end, &words, &is_command);
if (nwords == 0) {
x_e_putc(BEL);
return KSTD;
}
x_goto(xbuf + start);
x_delete(end - start, false);
for (i = 0; i < nwords;) {
if (x_escape(words[i], strlen(words[i]), x_emacs_putbuf) < 0 ||
(++i < nwords && x_ins(space) < 0))
{
x_e_putc(BEL);
return KSTD;
}
}
x_adjust();
return KSTD;
}
/* type == 0 for list, 1 for complete and 2 for complete-list */
static void
do_complete(flags, type)
int flags; /* XCF_{COMMAND,FILE,COMMAND_FILE} */
Comp_type type;
{
char **words;
int nwords;
int start, end, nlen, olen;
int is_command;
int completed = 0;
nwords = x_cf_glob(flags, xbuf, xep - xbuf, xcp - xbuf,
&start, &end, &words, &is_command);
/* no match */
if (nwords == 0) {
x_e_putc(BEL);
return;
}
if (type == CT_LIST) {
x_print_expansions(nwords, words, is_command);
x_redraw(0);
x_free_words(nwords, words);
return;
}
olen = end - start;
nlen = x_longest_prefix(nwords, words);
/* complete */
if (nwords == 1 || nlen > olen) {
x_goto(xbuf + start);
x_delete(olen, false);
x_escape(words[0], nlen, x_emacs_putbuf);
x_adjust();
completed = 1;
}
/* add space if single non-dir match */
if ((nwords == 1) && (!ISDIRSEP(words[0][nlen - 1]))) {
x_ins(space);
completed = 1;
}
if (type == CT_COMPLIST && !completed) {
x_print_expansions(nwords, words, is_command);
completed = 1;
}
if (completed)
x_redraw(0);
x_free_words(nwords, words);
}
/* NAME:
* x_adjust - redraw the line adjusting starting point etc.
*
* DESCRIPTION:
* This function is called when we have exceeded the bounds
* of the edit window. It increments x_adj_done so that
* functions like x_ins and x_delete know that we have been
* called and can skip the x_bs() stuff which has already
* been done by x_redraw.
*
* RETURN VALUE:
* None
*/
static void
x_adjust()
{
x_adj_done++; /* flag the fact that we were called. */
/*
* we had a problem if the prompt length > xx_cols / 2
*/
if ((xbp = xcp - (x_displen / 2)) < xbuf)
xbp = xbuf;
xlp_valid = false;
x_redraw(xx_cols);
x_flush();
}
static int unget_char = -1;
static void
x_e_ungetc(c)
int c;
{
unget_char = c;
}
static int
x_e_getc()
{
int c;
if (unget_char >= 0) {
c = unget_char;
unget_char = -1;
} else {
if (macroptr) {
c = (unsigned char) *macroptr++;
if (!*macroptr)
macroptr = (char *) 0;
} else
c = x_getc();
}
return c <= CHARMASK ? c : (c & CHARMASK);
}
static void
x_e_putc(c)
int c;
{
if (c == '\r' || c == '\n')
x_col = 0;
if (x_col < xx_cols)
{
x_putc(c);
switch(c)
{
case BEL:
break;
case '\r':
case '\n':
break;
case '\b':
x_col--;
break;
default:
x_col++;
break;
}
}
if (x_adj_ok && (x_col < 0 || x_col >= (xx_cols - 2)))
{
x_adjust();
}
}
#ifdef DEBUG
static int
x_debug_info(c)
int c;
{
x_flush();
shellf("\nksh debug:\n");
shellf("\tx_col == %d,\t\tx_cols == %d,\tx_displen == %d\n",
x_col, xx_cols, x_displen);
shellf("\txcp == 0x%lx,\txep == 0x%lx\n", (long) xcp, (long) xep);
shellf("\txbp == 0x%lx,\txbuf == 0x%lx\n", (long) xbp, (long) xbuf);
shellf("\txlp == 0x%lx\n", (long) xlp);
shellf("\txlp == 0x%lx\n", (long) x_lastcp());
shellf(newline);
x_redraw(-1);
return 0;
}
#endif
static void
x_e_puts(s)
const char *s;
{
int adj = x_adj_done;
while (*s && adj == x_adj_done)
x_e_putc(*s++);
}
/* NAME:
* x_set_arg - set an arg value for next function
*
* DESCRIPTION:
* This is a simple implementation of M-[0-9].
*
* RETURN VALUE:
* KSTD
*/
static int
x_set_arg(c)
int c;
{
int n = 0;
int first = 1;
c &= CHARMASK; /* strip command prefix */
for (; c >= 0 && isdigit(c); c = x_e_getc(), first = 0)
n = n * 10 + (c - '0');
if (c < 0 || first) {
x_e_putc(BEL);
x_arg = 1;
x_arg_defaulted = 1;
} else {
x_e_ungetc(c);
x_arg = n;
x_arg_defaulted = 0;
}
return KSTD;
}
/* Comment or uncomment the current line. */
static int
x_comment(c)
int c;
{
int oldsize = x_size_str(xbuf);
int len = xep - xbuf;
int ret = x_do_comment(xbuf, xend - xbuf, &len);
if (ret < 0)
x_e_putc(BEL);
else {
xep = xbuf + len;
*xep = '\0';
xcp = xbp = xbuf;
x_redraw(oldsize);
if (ret > 0)
return x_newline('\n');
}
return KSTD;
}
/* NAME:
* x_prev_histword - recover word from prev command
*
* DESCRIPTION:
* This function recovers the last word from the previous
* command and inserts it into the current edit line. If a
* numeric arg is supplied then the n'th word from the
* start of the previous command is used.
*
* Bound to M-.
*
* RETURN VALUE:
* KSTD
*/
static int
x_prev_histword(c)
int c;
{
char *rcp;
char *cp;
cp = *histptr;
if (!cp)
x_e_putc(BEL);
else if (x_arg_defaulted) {
rcp = &cp[strlen(cp) - 1];
/*
* ignore white-space after the last word
*/
while (rcp > cp && is_cfs(*rcp))
rcp--;
while (rcp > cp && !is_cfs(*rcp))
rcp--;
if (is_cfs(*rcp))
rcp++;
x_ins(rcp);
} else {
int i;
rcp = cp;
/*
* ignore white-space at start of line
*/
while (*rcp && is_cfs(*rcp))
rcp++;
while (x_arg-- > 1)
{
while (*rcp && !is_cfs(*rcp))
rcp++;
while (*rcp && is_cfs(*rcp))
rcp++;
}
cp = rcp;
while (*rcp && !is_cfs(*rcp))
rcp++;
i = *rcp;
*rcp = '\0';
x_ins(cp);
*rcp = i;
}
return KSTD;
}
/* Uppercase N(1) words */
static int
x_fold_upper(c)
int c;
{
return x_fold_case('U');
}
/* Lowercase N(1) words */
static int
x_fold_lower(c)
int c;
{
return x_fold_case('L');
}
/* Lowercase N(1) words */
static int
x_fold_capitalize(c)
int c;
{
return x_fold_case('C');
}
/* NAME:
* x_fold_case - convert word to UPPER/lower/Capital case
*
* DESCRIPTION:
* This function is used to implement M-U,M-u,M-L,M-l,M-C and M-c
* to UPPER case, lower case or Capitalize words.
*
* RETURN VALUE:
* None
*/
static int
x_fold_case(c)
int c;
{
char *cp = xcp;
if (cp == xep) {
x_e_putc(BEL);
return KSTD;
}
while (x_arg--) {
/*
* first skip over any white-space
*/
while (cp != xep && is_mfs(*cp))
cp++;
/*
* do the first char on its own since it may be
* a different action than for the rest.
*/
if (cp != xep) {
if (c == 'L') { /* lowercase */
if (isupper((unsigned char)*cp))
*cp = tolower((unsigned char)*cp);
} else { /* uppercase, capitialize */
if (islower((unsigned char)*cp))
*cp = toupper((unsigned char)*cp);
}
cp++;
}
/*
* now for the rest of the word
*/
while (cp != xep && !is_mfs((unsigned char)*cp)) {
if (c == 'U') { /* uppercase */
if (islower((unsigned char)*cp))
*cp = toupper((unsigned char)*cp);
} else { /* lowercase, capitialize */
if (isupper((unsigned char)*cp))
*cp = tolower((unsigned char)*cp);
}
cp++;
}
}
x_goto(cp);
return KSTD;
}
/* NAME:
* x_lastcp - last visible char
*
* SYNOPSIS:
* x_lastcp()
*
* DESCRIPTION:
* This function returns a pointer to that char in the
* edit buffer that will be the last displayed on the
* screen. The sequence:
*
* for (cp = x_lastcp(); cp > xcp; cp)
* x_bs(*--cp);
*
* Will position the cursor correctly on the screen.
*
* RETURN VALUE:
* cp or NULL
*/
static char *
x_lastcp()
{
char *rcp;
int i;
if (!xlp_valid)
{
for (i = 0, rcp = xbp; rcp < xep && i < x_displen; rcp++)
i += x_size(*rcp);
xlp = rcp;
}
xlp_valid = true;
return (xlp);
}
#endif /* EDIT */
|