summaryrefslogtreecommitdiff
path: root/lisp/emulation/edt.el
blob: e54869289548e4970a7578eefed6dfba01a05749 (plain)
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
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
;;; edt.el --- enhanced EDT keypad mode emulation for GNU Emacs 19

;; Copyright (C) 1986, 1992, 1993, 1994, 1995, 2000, 2001, 2002, 2003,
;;   2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.

;; Author: Kevin Gallagher <Kevin.Gallagher@boeing.com>
;; Maintainer: Kevin Gallagher <Kevin.Gallagher@boeing.com>
;; Keywords: emulations

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation; either version 3, or (at your
;; option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.



;;; Commentary:
;;

;; This is Version 4.0 of the EDT Emulation for Emacs 19 and above.
;; It comes with special functions which replicate nearly all of EDT's
;; keypad mode behavior.  It sets up default keypad and function key
;; bindings which closely match those found in EDT.  Support is
;; provided so that users may reconfigure most keypad and function key
;; bindings to their own liking.

;; NOTE: Version 4.0 contains several enhancements.  See the
;; Enhancement section below for the details.

;; Getting Started:

;; To start the EDT Emulation, first start Emacs and then enter
;;
;;    M-x edt-emulation-on
;;
;; to begin the emulation.  After initialization is complete, the
;; following message will appear below the status line informing you
;; that the emulation has been enabled: "Default EDT keymap active".

;; You can have the EDT Emulation start up automatically, each time
;; you initiate a GNU Emacs session, by adding the following line to
;; your .emacs file:
;;
;;    (add-hook term-setup-hook 'edt-emulation-on)

;; IMPORTANT: Be sure to read the file, edt-user.doc, located in the
;; Emacs "etc" directory.  It contains very helpful user information.

;; The EDT emulation consists of the following files:
;;
;; edt-user.doc     - User Instructions and Sample Customization File
;; edt.el           - EDT Emulation Functions and Default Configuration
;; edt-lk201.el     - Built-in support for DEC LK-201 Keyboards
;; edt-vt100.el     - Built-in support for DEC VT-100 (and above) terminals
;; edt-pc.el        - Built-in support for PC 101 Keyboards under MS-DOS
;; edt-mapper.el    - Create an EDT LK-201 Map File for Keyboards Without
;;                      Built-in Support

;; Enhancements:

;; Version 4.0 contains the following enhancements:

;;  1.  Scroll margins at the top and bottom of the window are now
;;      supported.  (The design was copied from tpu-extras.el.)  By
;;      default, this feature is enabled, with the top margin set to
;;      10% of the window and the bottom margin set to 15% of the
;;      window.  To change these settings, you can invoke the function
;;      edt-set-scroll-margins in your .emacs file.  For example, the
;;      following line
;;
;;           (edt-set-scroll-margins "20%" "25%")
;;
;;      sets the top margin to 20% of the window and the bottom margin
;;      to 25% of the window.  To disable this feature, set each
;;      margin to 0%.  You can also invoke edt-set-scroll-margins
;;      interactively while EDT Emulation is active to change the
;;      settings for that session.
;;
;;      NOTE: Another way to set the scroll margins is to use the
;;      Emacs customization feature (not available in Emacs 19) to set
;;      the following two variables directly:
;;
;;           edt-top-scroll-margin and edt-bottom-scroll-margin
;;
;;      Enter the Emacs `customize' command.  First select the Editing
;;      group and then select the Emulations group.  Finally, select
;;      the Edt group and follow the directions.
;;
;;  2.  The SUBS command is now supported and bound to GOLD-Enter by
;;      default.  (This design was copied from tpu-edt.el.)  Note, in
;;      earlier versions of EDT Emulation, GOLD-Enter was assigned to
;;      the Emacs function `query-replace'.  The binding of
;;      `query-replace' has been moved to GOLD-/.  If you prefer to
;;      restore `query-replace' to GOLD-Enter, then use an EDT user
;;      customization file, edt-user.el, to do this.  See edt-user.doc
;;      for details.

;;  3.  EDT Emulation now also works in XEmacs, including the
;;      highlighting of selected text.

;;  4.  If you access a workstation using an X Server, observe that
;;      the initialization file generated by edt-mapper.el will now
;;      contain the name of the X Server vendor.  This is a
;;      convenience for those who have access to their Unix account
;;      from more than one type of X Server.  Since different X
;;      Servers typically require different EDT emulation
;;      initialization files, edt-mapper.el will now generate these
;;      different initialization files and save them with different
;;      names.  Then, the correct initialization file for the
;;      particular X server in use is loaded correctly automatically.

;;  5.  Also, edt-mapper.el is now capable of binding an ASCII key
;;      sequence, providing the ASCII key sequence prefix is already
;;      known by Emacs to be a prefix.  As a result of providing this
;;      support, some terminal/keyboard/window system configurations,
;;      which don't have a complete set of sensible function key
;;      bindings built into Emacs in `function-key-map', can still be
;;      configured for use with EDT Emulation.  (Note: In a few rare
;;      circumstances this does not work properly.  In particular, it
;;      does not work if a subset of the leading ASCII characters in a
;;      key sequence are recognized by Emacs as having an existing
;;      binding.  For example, if the keypad 7 (KP-7) key generates
;;      the sequence \"<ESC>Ow\" and \"<ESC>O\" is already bound to a
;;      function, pressing KP-7 when told to do so by edt-mapper.el
;;      will result in edt-mapper.el incorrectly mapping \"<ESC>O\" to
;;      KP-7 and \"w\" to KP-8.  If something like this happens to
;;      you, it is probably a bug in the support for your keyboard
;;      within Emacs OR a bug in the Unix termcap/terminfo support for
;;      your terminal OR a bug in the terminal emulation software you
;;      are using.)

;;  6.  The edt-quit function (bound to GOLD-q by default) has been
;;      modified to warn the user when file-related buffer
;;      modifications exist.  It now cautions the user that those
;;      modifications will be lost if the user quits without saving
;;      those buffers.


;;; History:
;;
;;  Version 4.0    2000    Added New Features and Fixed a Few Bugs
;;


;;; Code:

;;;  Electric Help functions are used for keypad help displays.  A few
;;;  picture functions are used in rectangular cut and paste commands.

(require 'ehelp)
(require 'picture)

;;;;
;;;; VARIABLES and CONSTANTS
;;;;

(defgroup edt nil
  "Emacs emulating EDT."
  :prefix "edt-"
  :group 'emulations)

;; To silence the byte-compiler
(defvar *EDT-keys*)
(defvar edt-default-global-map)
(defvar edt-last-copied-word)
(defvar edt-learn-macro-count)
(defvar edt-orig-page-delimiter)
(defvar edt-orig-transient-mark-mode)
(defvar edt-rect-start-point)
(defvar edt-user-global-map)
(defvar rect-start-point)
(defvar time-string)
(defvar zmacs-region-stays)

;;;
;;;  Version Information
;;;
(defconst edt-version "4.0" "EDT Emulation version number.")

;;;
;;;  User Configurable Variables
;;;

(defcustom edt-keep-current-page-delimiter nil
  "*Emacs MUST be restarted for a change in value to take effect!
Non-nil leaves Emacs value of `page-delimiter' unchanged within EDT
Emulation.  If set to nil (the default), the `page-delimiter' variable
is set to \"\\f\" when edt-emulation-on is first invoked.  This
setting replicates EDT's page delimiter behavior.  The original value
is restored when edt-emulation-off is called."
  :type 'boolean
  :group 'edt)

(defcustom edt-use-EDT-control-key-bindings nil
  "*Emacs MUST be restarted for a change in value to take effect!
Non-nil causes the control key bindings to be replaced with EDT
bindings.  If set to nil (the default), EDT control key bindings are
not used and the current Emacs control key bindings are retained for
use within the EDT emulation."
  :type 'boolean
  :group 'edt)

(defcustom edt-word-entities '(?\t)
  "*Specifies the list of EDT word entity characters.
The default list, (\?\\t), contains just the TAB character, which
emulates EDT.  Characters are specified in the list using their
decimal ASCII values.  A question mark, followed by the actual
character, can be used to indicate the numerical value of the
character, instead of the actual decimal value.  So, ?A means the
numerical value for the letter A, \?/ means the numerical value for /,
etc.  Several unprintable and special characters have special
representations, which you can also use:

            \?\\b  specifies  BS, C-h
            \?\\t  specifies  TAB, C-i
            \?\\n  specifies  LFD, C-j
            \?\\v  specifies  VTAB, C-k
            \?\\f  specifies  FF, C-l
            \?\\r  specifies  CR, C-m
            \?\\e  specifies  ESC, C-[
            \?\\\\  specifies  \\

In EDT Emulation movement-by-word commands, each character in the list
will be treated as if it were a separate word."
  :type '(repeat integer)
  :group 'edt)

(defcustom edt-top-scroll-margin 10
  "*Scroll margin at the top of the screen.
Interpreted as a percent of the current window size with a default
setting of 10%.  If set to 0, top scroll margin is disabled."
  :type 'integer
  :group 'edt)

(defcustom edt-bottom-scroll-margin 15
  "*Scroll margin at the bottom of the screen.
Interpreted as a percent of the current window size with a default
setting of 15%.  If set to 0, bottom scroll margin is disabled."
  :type 'integer
  :group 'edt)

;;;
;;; Internal Variables
;;;

(defvar edt-last-deleted-lines ""
  "Last text deleted by the EDT emulation DEL L command.")

(defvar edt-last-deleted-words ""
  "Last text deleted by the EDT emulation DEL W command.")

(defvar edt-last-deleted-chars ""
  "Last text deleted by the EDT emulation DEL C command.")

(defvar edt-find-last-text ""
  "Last text found by the EDT emulation FIND command.")

(defvar edt-match-beginning-mark (make-marker)
  "Used internally by the EDT emulation SUBS command.")

(defvar edt-match-end-mark (make-marker)
  "Used internally by the EDT emulation SUBS command.")

(defvar edt-last-replaced-key-definition nil
  "Key definition replaced with `edt-define-key' or `edt-learn' command.")

(defvar edt-direction-string ""
  "String indicating current direction of movement.")

(defvar edt-select-mode nil
  "Non-nil means select mode is active.")

(defvar edt-select-mode-current ""
  "Text displayed in mode line to indicate the state of EDT select mode.
When select mode is inactive, it is set to an empty string.")

(defconst edt-select-mode-string " Select"
  "Used in mode line to indicate select mode is active.")

(defconst edt-forward-string " ADVANCE"
  "Direction string in mode line to indicate forward movement.")

(defconst edt-backward-string "  BACKUP"
  "Direction string in mode line to indicate backward movement.")

(defvar edt-default-map-active nil
  "Non-nil indicates that default EDT emulation key bindings are active.
nil means user-defined custom bindings are active.")

(defvar edt-user-map-configured nil
  "Non-nil indicates that user custom EDT key bindings are configured.
This means that an edt-user.el file was found in the user's `load-path'.")

(defvar edt-term nil
  "Specifies the terminal type, if applicable.")

;;;
;;;  Emacs version identifiers - currently referenced by
;;;
;;;     o edt-emulation-on      o edt-load-keys
;;;
(defconst edt-emacs-variant (if (featurep 'emacs) "gnu" "xemacs")
  "Indicates Emacs variant:  GNU Emacs or XEmacs \(aka Lucid Emacs\).")

(defconst edt-window-system (if (featurep 'emacs) window-system (console-type))
  "Indicates window system \(in GNU Emacs\) or console type \(in XEmacs\).")

(defconst edt-xserver (if (eq edt-window-system 'x)
			  (if (featurep 'xemacs)
			      ;; The Cygwin window manager has a `/' in its
			      ;; name, which breaks the generated file name of
			      ;; the custom key map file.  Replace `/' with a
			      ;; `-' to work around that.
			      (replace-in-string (x-server-vendor) "[ /]" "-")
			    (subst-char-in-string ?/ ?- (subst-char-in-string ?  ?- (x-server-vendor))))
			nil)
  "Indicates X server vendor name, if applicable.")

(defvar edt-keys-file nil
  "User's custom keypad and function keys mappings to emulate LK-201 keyboard.")

(defvar edt-last-copied-word nil
  "Last word that the user copied.")


;;;;
;;;; EDT Emulation Commands
;;;;

;;; Almost all of EDT's keypad mode commands have equivalent Emacs
;;; function counterparts.  But many of these counterparts behave
;;; somewhat differently in Emacs.
;;;
;;; So, the following Emacs functions emulate, where practical, the
;;; exact behavior of the corresponding EDT keypad mode commands.  In
;;; a few cases, the emulation is not exact, but it should be close
;;; enough for most EDT die-hards.
;;;

;;;
;;; PAGE
;;;
;;; Emacs uses the regexp assigned to page-delimiter to determine what
;;; marks a page break.  This is normally "^\f", which causes the
;;; edt-page command to ignore form feeds not located at the beginning
;;; of a line.  To emulate the EDT PAGE command exactly,
;;; page-delimiter is set to "\f" when EDT emulation is turned on, and
;;; restored to its original value when EDT emulation is turned off.
;;; But this can be overridden if the EDT definition is not desired by
;;; placing
;;;
;;;         (setq edt-keep-current-page-delimiter t)
;;;
;;; in your .emacs file.

(defun edt-page-forward (num)
  "Move forward to just after next page delimiter.
Argument NUM is the number of page delimiters to move."
  (interactive "p")
  (edt-check-prefix num)
  (if (eobp)
      (error "End of buffer")
	(progn
	  (forward-page num)
	  (if (eobp)
		  (edt-line-to-bottom-of-window)
		(edt-line-to-top-of-window)))))

(defun edt-page-backward (num)
  "Move backward to just after previous page delimiter.
Argument NUM is the number of page delimiters to move."
  (interactive "p")
  (edt-check-prefix num)
  (if (bobp)
      (error "Beginning of buffer")
	(progn
	  (backward-page num)
      (edt-line-to-top-of-window)
	  (if (featurep 'xemacs) (setq zmacs-region-stays t)))))

(defun edt-page (num)
  "Move in current direction to next page delimiter.
Argument NUM is the number of page delimiters to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-page-forward num)
    (edt-page-backward num)))

;;;
;;; SECT
;;;
;;; EDT defaults a section size to be 16 lines of its one and only
;;; 24-line window.  That's two-thirds of the window at a time.  The
;;; EDT SECT commands moves the cursor, not the window.
;;;
;;; This emulation of EDT's SECT moves the cursor approximately
;;; two-thirds of the current window at a time.

(defun edt-sect-forward (num)
  "Move cursor forward two-thirds of a window's number of lines.
Argument NUM is the number of sections to move."
  (interactive "p")
  (edt-check-prefix num)
  (edt-line-forward (* (* (/ (- (window-height) 1) 3) 2) num)))


(defun edt-sect-backward (num)
  "Move cursor backward two-thirds of a window.
Argument NUM is the number of sections to move."
  (interactive "p")
  (edt-check-prefix num)
  (edt-line-backward (* (* (/ (- (window-height) 1) 3) 2) num)))

(defun edt-sect (num)
  "Move in current direction a full window.
Argument NUM is the number of sections to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-sect-forward num)
    (edt-sect-backward num)))

;;;
;;; BEGINNING OF LINE
;;;
;;; EDT's beginning-of-line command is not affected by current
;;; direction, for some unknown reason.

(defun edt-beginning-of-line (num)
  "Move backward to next beginning of line mark.
Argument NUM is the number of BOL marks to move."
  (interactive "p")
  (edt-check-prefix num)
  (let ((beg (edt-current-line)))
    (if (bolp)
	(forward-line (* -1 num))
      (progn
	(setq num (1- num))
	(forward-line (* -1 num))))
    (edt-top-check beg num))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))


;;;
;;; EOL (End of Line)
;;;

(defun edt-end-of-line-forward (num)
  "Move forward to next end of line mark.
Argument NUM is the number of EOL marks to move."
  (interactive "p")
  (edt-check-prefix num)
  (let ((beg (edt-current-line)))
    (forward-char)
    (end-of-line num)
    (edt-bottom-check beg num))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))


(defun edt-end-of-line-backward (num)
  "Move backward to next end of line mark.
Argument NUM is the number of EOL marks to move."
  (interactive "p")
  (edt-check-prefix num)
  (let ((beg (edt-current-line)))
    (end-of-line (1- num))
    (edt-top-check beg num))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))


(defun edt-end-of-line (num)
  "Move in current direction to next end of line mark.
Argument NUM is the number of EOL marks to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-end-of-line-forward num)
    (edt-end-of-line-backward num)))

;;;
;;; WORD
;;;
;;; This one is a tad messy.  To emulate EDT's behavior everywhere in
;;; the file (beginning of file, end of file, beginning of line, end
;;; of line, etc.) it takes a bit of special handling.
;;;
;;; The variable edt-word-entities contains a list of characters which
;;; are to be viewed as distinct words where ever they appear in the
;;; buffer.  This emulates the EDT line mode command SET ENTITY WORD.


(defun edt-one-word-forward ()
  "Move forward to first character of next word."
  (interactive)
  (if (eobp)
      (error "End of buffer"))
  (if (eolp)
      (forward-char)
    (progn
      (if (memq (following-char) edt-word-entities)
	  (forward-char)
	(while (and
		(not (eolp))
		(not (eobp))
		(not (eq ?\  (char-syntax (following-char))))
		(not (memq (following-char) edt-word-entities)))
	  (forward-char)))
      (while (and
	      (not (eolp))
	      (not (eobp))
	      (eq ?\  (char-syntax (following-char)))
	      (not (memq (following-char) edt-word-entities)))
	(forward-char))))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-one-word-backward ()
  "Move backward to first character of previous word."
  (interactive)
  (if (bobp)
      (error "Beginning of buffer"))
  (if (bolp)
      (backward-char)
    (progn
      (backward-char)
      (while (and
	      (not (bolp))
	      (not (bobp))
	      (eq ?\  (char-syntax (following-char)))
	      (not (memq (following-char) edt-word-entities)))
	(backward-char))
      (if (not (memq (following-char) edt-word-entities))
	  (while (and
		  (not (bolp))
		  (not (bobp))
		  (not (eq ?\  (char-syntax (preceding-char))))
		  (not (memq (preceding-char) edt-word-entities)))
	    (backward-char)))))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-word-forward (num)
  "Move forward to first character of next word.
Argument NUM is the number of words to move."
  (interactive "p")
  (edt-check-prefix num)
  (while (> num 0)
    (edt-one-word-forward)
    (setq num (1- num))))

(defun edt-word-backward (num)
  "Move backward to first character of previous word.
Argument NUM is the number of words to move."
  (interactive "p")
  (edt-check-prefix num)
  (while (> num 0)
    (edt-one-word-backward)
    (setq num (1- num))))

(defun edt-word (num)
  "Move in current direction to first character of next word.
Argument NUM is the number of words to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-word-forward num)
    (edt-word-backward num)))

;;;
;;; CHAR
;;;

(defun edt-character (num)
  "Move in current direction to next character.
Argument NUM is the number of characters to move."
  (interactive "p")
  (edt-check-prefix num)
  (if (equal edt-direction-string edt-forward-string)
      (forward-char num)
    (backward-char num))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

;;;
;;; LINE
;;;
;;; When direction is set to BACKUP, LINE behaves just like BEGINNING
;;; OF LINE in EDT.  So edt-line-backward is not really needed as a
;;; separate function.

(defun edt-line-backward (num)
  "Move backward to next beginning of line mark.
Argument NUM is the number of BOL marks to move."
  (interactive "p")
  (edt-beginning-of-line num))

(defun edt-line-forward (num)
  "Move forward to next beginning of line mark.
Argument NUM is the number of BOL marks to move."
  (interactive "p")
  (edt-check-prefix num)
  (let ((beg (edt-current-line)))
    (forward-line num)
    (edt-bottom-check beg num))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-line (num)
  "Move in current direction to next beginning of line mark.
Argument NUM is the number of BOL marks to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-line-forward num)
    (edt-line-backward num)))

;;;
;;; UP and DOWN Arrows
;;;

(defun edt-next-line (num)
  "Move cursor down one line.
Argument NUM is the number of lines to move."
  (interactive "p")
  (edt-check-prefix num)
  (let ((beg (edt-current-line)))
    (forward-line num)
    (edt-bottom-check beg num))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-previous-line (num)
  "Move cursor up one line.
Argument NUM is the number of lines to move."
  (interactive "p")
  (edt-check-prefix num)
  (let ((beg (edt-current-line)))
    (forward-line (- num))
    (edt-top-check beg num))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))


;;;
;;; TOP
;;;

(defun edt-top ()
  "Move cursor to the beginning of buffer."
  (interactive)
  (goto-char (point-min))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

;;;
;;; BOTTOM
;;;

(defun edt-bottom ()
  "Move cursor to the end of buffer."
  (interactive)
  (goto-char (point-max))
  (edt-line-to-bottom-of-window))

;;;
;;; FIND
;;;

(defun edt-find-forward (&optional find)
  "Find first occurrence of a string in forward direction and save it.
Optional argument FIND is t is this function is called from `edt-find'."
  (interactive)
  (if (not find)
      (set 'edt-find-last-text (read-string "Search forward: ")))
  (let* ((left nil)
	 (beg (edt-current-line))
	 (height (window-height))
	 (top-percent
	  (if (= 0 edt-top-scroll-margin) 10 edt-top-scroll-margin))
	 (bottom-percent
	  (if (= 0 edt-bottom-scroll-margin) 15 edt-bottom-scroll-margin))
	 (top-margin (/ (* height top-percent) 100))
	 (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
	 (bottom-margin (max beg (- height bottom-up-margin 1)))
	 (top (save-excursion (move-to-window-line top-margin) (point)))
	 (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
	 (far (save-excursion
		(goto-char bottom) (forward-line (- height 2)) (point))))
    (if (search-forward edt-find-last-text)
	(progn
	  (search-backward edt-find-last-text)
	  (edt-set-match)
	  (cond((> (point) far)
		(setq left (save-excursion (forward-line height)))
		(if (= 0 left) (recenter top-margin)
		  (recenter (- left bottom-up-margin))))
	       (t
		(and (> (point) bottom) (recenter bottom-margin)))))))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-find-backward (&optional find)
  "Find first occurrence of a string in the backward direction and save it.
Optional argument FIND is t if this function is called from `edt-find'."
  (interactive)
  (if (not find)
      (set 'edt-find-last-text (read-string "Search backward: ")))
  (let* ((left nil)
	 (beg (edt-current-line))
	 (height (window-height))
	 (top-percent
	  (if (= 0 edt-top-scroll-margin) 10 edt-top-scroll-margin))
	 (bottom-percent
	  (if (= 0 edt-bottom-scroll-margin) 15 edt-bottom-scroll-margin))
	 (top-margin (/ (* height top-percent) 100))
	 (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
	 (bottom-margin (max beg (- height bottom-up-margin 1)))
	 (top (save-excursion (move-to-window-line top-margin) (point)))
	 (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
	 (far (save-excursion
		(goto-char bottom) (forward-line (- height 2)) (point))))
    (if (search-backward edt-find-last-text)
	(edt-set-match))
    (and (< (point) top) (recenter (min beg top-margin))))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-find ()
  "Find first occurrence of string in current direction and save it."
  (interactive)
  (set 'edt-find-last-text (read-string "Search: "))
  (if (equal edt-direction-string edt-forward-string)
      (edt-find-forward t)
      (edt-find-backward t)))


;;;
;;; FNDNXT
;;;

(defun edt-find-next-forward ()
  "Find next occurrence of a string in forward direction."
  (interactive)
  (let* ((left nil)
	 (beg (edt-current-line))
	 (height (window-height))
	 (top-percent
	  (if (= 0 edt-top-scroll-margin) 10 edt-top-scroll-margin))
	 (bottom-percent
	  (if (= 0 edt-bottom-scroll-margin) 15 edt-bottom-scroll-margin))
	 (top-margin (/ (* height top-percent) 100))
	 (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
	 (bottom-margin (max beg (- height bottom-up-margin 1)))
	 (top (save-excursion (move-to-window-line top-margin) (point)))
	 (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
	 (far (save-excursion
		(goto-char bottom) (forward-line (- height 2)) (point))))
    (forward-char 1)
    (if (search-forward edt-find-last-text nil t)
	(progn
	  (search-backward edt-find-last-text)
	  (edt-set-match)
	  (cond((> (point) far)
		(setq left (save-excursion (forward-line height)))
		(if (= 0 left) (recenter top-margin)
		  (recenter (- left bottom-up-margin))))
	       (t
		(and (> (point) bottom) (recenter bottom-margin)))))
      (progn
	(backward-char 1)
	(error "Search failed: \"%s\"" edt-find-last-text))))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-find-next-backward ()
  "Find next occurrence of a string in backward direction."
  (interactive)
  (let* ((left nil)
	 (beg (edt-current-line))
	 (height (window-height))
	 (top-percent
	  (if (= 0 edt-top-scroll-margin) 10 edt-top-scroll-margin))
	 (bottom-percent
	  (if (= 0 edt-bottom-scroll-margin) 15 edt-bottom-scroll-margin))
	 (top-margin (/ (* height top-percent) 100))
	 (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
	 (bottom-margin (max beg (- height bottom-up-margin 1)))
	 (top (save-excursion (move-to-window-line top-margin) (point)))
	 (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
	 (far (save-excursion
		(goto-char bottom) (forward-line (- height 2)) (point))))
    (if (not (search-backward edt-find-last-text nil t))
	(error "Search failed: \"%s\"" edt-find-last-text)
      (progn
	(edt-set-match)
	(and (< (point) top) (recenter (min beg top-margin))))))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-find-next ()
  "Find next occurrence of a string in current direction."
  (interactive)
  (if (equal edt-direction-string edt-forward-string)
      (edt-find-next-forward)
    (edt-find-next-backward)))

;;;
;;; APPEND
;;;

(defun edt-append ()
  "Append this kill region to last killed region."
  (interactive "*")
  (edt-check-selection)
  (append-next-kill)
  (kill-region (mark) (point))
  (message "Selected text APPENDED to kill ring"))

;;;
;;; DEL L
;;;

(defun edt-delete-line (num)
  "Delete from cursor up to and including the end of line mark.
Argument NUM is the number of lines to delete."
  (interactive "*p")
  (edt-check-prefix num)
  (let ((beg (point)))
    (forward-line num)
    (if (not (eq (preceding-char) ?\n))
        (insert "\n"))
    (setq edt-last-deleted-lines
          (buffer-substring beg (point)))
    (delete-region beg (point))))

;;;
;;; DEL EOL
;;;

(defun edt-delete-to-end-of-line (num)
  "Delete from cursor up to but excluding the end of line mark.
Argument NUM is the number of lines to delete."
  (interactive "*p")
  (edt-check-prefix num)
  (let ((beg (point)))
    (forward-char 1)
    (end-of-line num)
    (setq edt-last-deleted-lines
          (buffer-substring beg (point)))
    (delete-region beg (point))))

;;;
;;; SELECT
;;;

(defun edt-select-mode (arg)
  "Turn EDT select mode off if ARG is nil; otherwise, turn EDT select mode on.
In select mode, selected text is highlighted."
  (if arg
      (progn
	(make-local-variable 'edt-select-mode)
	(setq edt-select-mode 'edt-select-mode-current)
	(setq rect-start-point (window-point)))
    (progn
      (kill-local-variable 'edt-select-mode)))
  (force-mode-line-update))

(defun edt-select ()
  "Set mark at cursor and start text selection."
  (interactive)
  (set-mark-command nil))

(defun edt-reset ()
  "Cancel text selection."
  (interactive)
  (if (featurep 'emacs)
      (deactivate-mark)
    (zmacs-deactivate-region)))

;;;
;;; CUT
;;;

(defun edt-cut ()
  "Deletes selected text but copies to kill ring."
  (interactive "*")
  (edt-check-selection)
  (kill-region (mark) (point))
  (message "Selected text CUT to kill ring"))

;;;
;;; DELETE TO BEGINNING OF LINE
;;;

(defun edt-delete-to-beginning-of-line (num)
  "Delete from cursor to beginning of line.
Argument NUM is the number of lines to delete."
  (interactive "*p")
  (edt-check-prefix num)
  (let ((beg (point)))
    (edt-beginning-of-line num)
    (setq edt-last-deleted-lines
          (buffer-substring (point) beg))
    (delete-region beg (point))))

;;;
;;; DEL W
;;;

(defun edt-delete-word (num)
  "Delete from cursor up to but excluding first character of next word.
Argument NUM is the number of words to delete."
  (interactive "*p")
  (edt-check-prefix num)
  (let ((beg (point)))
    (edt-word-forward num)
    (setq edt-last-deleted-words (buffer-substring beg (point)))
    (delete-region beg (point))))

;;;
;;; DELETE TO BEGINNING OF WORD
;;;

(defun edt-delete-to-beginning-of-word (num)
  "Delete from cursor to beginning of word.
Argument NUM is the number of words to delete."
  (interactive "*p")
  (edt-check-prefix num)
  (let ((beg (point)))
    (edt-word-backward num)
    (setq edt-last-deleted-words (buffer-substring (point) beg))
    (delete-region beg (point))))

;;;
;;; DEL C
;;;

(defun edt-delete-character (num)
  "Delete character under cursor.
Argument NUM is the number of characters to delete."
  (interactive "*p")
  (edt-check-prefix num)
  (setq edt-last-deleted-chars
        (buffer-substring (point) (min (point-max) (+ (point) num))))
  (delete-region (point) (min (point-max) (+ (point) num))))

;;;
;;; DELETE CHAR
;;;

(defun edt-delete-previous-character (num)
  "Delete character in front of cursor.
Argument NUM is the number of characters to delete."
  (interactive "*p")
  (edt-check-prefix num)
  (setq edt-last-deleted-chars
        (buffer-substring (max (point-min) (- (point) num)) (point)))
  (delete-region (max (point-min) (- (point) num)) (point)))

;;;
;;; UND L
;;;

(defun edt-undelete-line ()
  "Undelete previous deleted line(s)."
  (interactive "*")
  (point-to-register 1)
  (insert edt-last-deleted-lines)
  (register-to-point 1))

;;;
;;; UND W
;;;

(defun edt-undelete-word ()
  "Undelete previous deleted word(s)."
  (interactive "*")
  (point-to-register 1)
  (insert edt-last-deleted-words)
  (register-to-point 1))

;;;
;;; UND C
;;;

(defun edt-undelete-character ()
  "Undelete previous deleted character(s)."
  (interactive "*")
  (point-to-register 1)
  (insert edt-last-deleted-chars)
  (register-to-point 1))

;;;
;;; REPLACE
;;;

(defun edt-replace ()
  "Replace marked section with last CUT (killed) text."
  (interactive "*")
  (if (edt-check-match)
      (replace-match (car kill-ring-yank-pointer))
    (progn
      (exchange-point-and-mark)
      (let ((beg (point)))
	(exchange-point-and-mark)
	(delete-region beg (point)))
      (yank))))

;;;
;;; SUBS
;;;

(defun edt-substitute (num)
  "Replace the selected region with the contents of the CUT buffer and.
Repeat the most recent FIND command.  (The Emacs kill ring is used as
the CUT buffer.)
Argument NUM is the repeat count.  A positive value indicates the of times
to repeat the substitution.  A negative argument means replace all occurrences
of the search text."
  (interactive "p")
  (cond ((or edt-select-mode (edt-check-match))
	 (while (and (not (= num 0)) (or edt-select-mode (edt-check-match)))
	   (edt-replace)
	   (edt-find-next)
	   (setq num (1- num))))
	(t
	 (error "No selection active"))))

(defun edt-set-match nil
  "Set markers at match beginning and end."
  ;; Add one to beginning mark so it stays with the first character of
  ;;   the string even if characters are added just before the string.
  (setq edt-match-beginning-mark (copy-marker (1+ (match-beginning 0))))
  (setq edt-match-end-mark (copy-marker (match-end 0))))

(defun edt-unset-match nil
  "Unset match beginning and end markers."
  (set-marker edt-match-beginning-mark nil)
  (set-marker edt-match-end-mark nil))

(defun edt-match-beginning nil
  "Return the location of the last match beginning."
  (1- (marker-position edt-match-beginning-mark)))

(defun edt-match-end nil
  "Return the location of the last match end."
  (marker-position edt-match-end-mark))

(defun edt-check-match nil
  "Return t if point is between edt-match markers.
Otherwise sets the edt-match markers to nil and returns nil."
  ;; make sure 1- marker is in this buffer
  ;;           2- point is at or after beginning marker
  ;;           3- point is before ending marker, or in the case of
  ;;              zero length regions (like bol, or eol) that the
  ;;              beginning, end, and point are equal.
  (cond ((and
	  (equal (marker-buffer edt-match-beginning-mark) (current-buffer))
	  (>= (point) (1- (marker-position edt-match-beginning-mark)))
	  (or
	   (< (point) (marker-position edt-match-end-mark))
	   (and (= (1- (marker-position edt-match-beginning-mark))
		   (marker-position edt-match-end-mark))
		(= (marker-position edt-match-end-mark) (point))))) t)
	(t
	 (edt-unset-match) nil)))

(defun edt-show-match-markers nil
  "Show the values of the match markers."
  (interactive)
  (if (markerp edt-match-beginning-mark)
      (let ((beg (marker-position edt-match-beginning-mark)))
	(message "(%s, %s) in %s -- current %s in %s"
		 (if beg (1- beg) nil)
		 (marker-position edt-match-end-mark)
		 (marker-buffer edt-match-end-mark)
		 (point) (current-buffer)))))


;;;
;;; ADVANCE
;;;

(defun edt-advance ()
  "Set movement direction forward.
Also, execute command specified if in Minibuffer."
  (interactive)
  (setq edt-direction-string edt-forward-string)
  (force-mode-line-update)
  (if (string-equal " *Minibuf"
                    (substring (buffer-name) 0 (min (length (buffer-name)) 9)))
      (exit-minibuffer))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))


;;;
;;; BACKUP
;;;

(defun edt-backup ()
  "Set movement direction backward.
Also, execute command specified if in Minibuffer."
  (interactive)
  (setq edt-direction-string edt-backward-string)
  (force-mode-line-update)
  (if (string-equal " *Minibuf"
                    (substring (buffer-name) 0 (min (length (buffer-name)) 9)))
      (exit-minibuffer))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))


;;;
;;; CHNGCASE
;;;
;; This function is based upon Jeff Kowalski's case-flip function in his
;; tpu.el.

(defun edt-change-case (num)
  "Change the case of specified characters.
If text selection IS active, then characters between the cursor and mark are
changed.  If text selection is NOT active, there are two cases.  First, if the
current direction is ADVANCE, then the prefix number of character(s) under and
following cursor are changed.  Second, if the current direction is BACKUP, then
the prefix number of character(s) before the cursor are changed.  Accepts a
positive prefix for the number of characters to change, but the prefix is
ignored if text selection is active.
Argument NUM is the numbers of consecutive characters to change."
  (interactive "*p")
  (edt-check-prefix num)
  (if edt-select-mode
      (let ((end (max (mark) (point)))
            (point-save (point)))
        (goto-char (min (point) (mark)))
        (while (not (eq (point) end))
          (funcall (if (<= ?a (following-char))
                       'upcase-region 'downcase-region)
                   (point) (1+ (point)))
          (forward-char 1))
        (goto-char point-save))
    (progn
      (if (string= edt-direction-string edt-backward-string)
	  (backward-char num))
      (while (> num 0)
	(funcall (if (<= ?a (following-char))
		     'upcase-region 'downcase-region)
		 (point) (1+ (point)))
	(forward-char 1)
	(setq num (1- num))))))

;;;
;;; DEFINE KEY
;;;

(defun edt-define-key ()
  "Assign an interactively-callable function to a specified key sequence.
The current key definition is saved in `edt-last-replaced-key-definition'.
Use `edt-restore-key' to restore last replaced key definition."
  (interactive)
  (if (featurep 'xemacs) (setq zmacs-region-stays t))
  (let (edt-function
	edt-key-definition)
    (setq edt-key-definition
	  (read-key-sequence "Press the key to be defined: "))
    (if (if (featurep 'emacs)
	    (string-equal "\C-m" edt-key-definition)
	  (string-equal "\C-m" (events-to-keys edt-key-definition)))
        (message "Key not defined")
      (progn
	(setq edt-function (read-command "Enter command name: "))
	(if (string-equal "" edt-function)
	    (message "Key not defined")
	  (progn
	    (setq edt-last-replaced-key-definition
		  (lookup-key (current-global-map) edt-key-definition))
	    (define-key (current-global-map)
	      edt-key-definition edt-function)))))))

;;;
;;; FORM FEED INSERT
;;;

(defun edt-form-feed-insert (num)
  "Insert form feed character at cursor position.
Argument NUM is the number of form feeds to insert."
  (interactive "*p")
  (edt-check-prefix num)
  (while (> num 0)
    (insert ?\f)
    (setq num (1- num))))

;;;
;;; TAB INSERT
;;;

(defun edt-tab-insert (num)
  "Insert tab character at cursor position.
Argument NUM is the number of tabs to insert."
  (interactive "*p")
  (edt-check-prefix num)
  (while (> num 0)
    (insert ?\t)
    (setq num (1- num))))

;;;
;;; Check Prefix
;;;

(defun edt-check-prefix (num)
  "Indicate error if prefix is not positive.
Argument NUM is the prefix value tested."
  (if (<= num 0)
      (error "Prefix must be positive")))

;;;
;;; Check Selection
;;;

(defun edt-check-selection ()
  "Indicate error if EDT selection is not active."
  (if (not edt-select-mode)
      (error "Selection NOT active")))

;;;
;;; Scroll Margins
;;;

(defun edt-top-check (beg lines)
  "Enforce scroll margin at the top of screen.
Argument BEG is the starting line number before cursor was moved.
Argument LINES is the number of lines the cursor moved toward the top."
  (let ((margin	 (/ (* (window-height) edt-top-scroll-margin) 100)))
    (cond ((< beg margin) (recenter beg))
	  ((< (- beg lines) margin) (recenter margin)))))

(defun edt-bottom-check (beg lines)
  "Enforce scroll margin at the bottom of screen.
Argument BEG is the starting line number before cursor was moved.
Argument LINES is the number of lines the cursor moved toward the bottom."
  (let* ((height (window-height))
		 (margin (+ 1 (/ (* height edt-bottom-scroll-margin) 100)))
		 ;; subtract 1 from height because it includes mode line
		 (difference (- height margin 1)))
    (cond ((> beg difference) (recenter beg))
		  ((and (featurep 'xemacs) (> (+ beg lines 1) difference))
		   (recenter (- margin)))
		  ((> (+ beg lines) difference) (recenter (- margin))))))

(defun edt-current-line nil
  "Return the vertical position of point in the selected window.
Top line is 0.  Counts each text line only once, even if it wraps."
  (+ (count-lines (window-start) (point)) (if (= (current-column) 0) 1 0) -1))

;;;###autoload
(defun edt-set-scroll-margins (top bottom)
  "Set scroll margins.
Argument TOP is the top margin in number of lines or percent of window.
Argument BOTTOM is the bottom margin in number of lines or percent of window."
  (interactive
   "sEnter top scroll margin (N lines or N%% or RETURN for current value): \
\nsEnter bottom scroll margin (N lines or N%% or RETURN for current value): ")
  ;; set top scroll margin
  (or (string= top "")
      (if (string= "%" (substring top -1))
	  (setq edt-top-scroll-margin (string-to-number top))
	(setq edt-top-scroll-margin
	      (/ (1- (+ (* (string-to-number top) 100) (window-height)))
		 (window-height)))))
  ;; set bottom scroll margin
  (or (string= bottom "")
      (if (string= "%" (substring bottom -1))
	  (setq edt-bottom-scroll-margin (string-to-number bottom))
	(setq edt-bottom-scroll-margin
	      (/ (1- (+ (* (string-to-number bottom) 100) (window-height)))
		 (window-height)))))
  ;; report scroll margin settings if running interactively
  (and (interactive-p)
       (message "Scroll margins set.  Top = %s%%, Bottom = %s%%"
		edt-top-scroll-margin edt-bottom-scroll-margin)))


;;;;
;;;; ENHANCEMENTS AND ADDITIONS FOR EDT KEYPAD MODE
;;;;

;;;
;;; Several enhancements and additions to EDT keypad mode commands are
;;; provided here.  Some of these have been motivated by similar
;;; TPU/EVE and EVE-Plus commands.  Others are new.

;;;
;;; CHANGE DIRECTION
;;;

(defun edt-change-direction ()
  "Toggle movement direction."
  (interactive)
  (if (equal edt-direction-string edt-forward-string)
      (edt-backup)
    (edt-advance)))

;;;
;;; TOGGLE SELECT
;;;

(defun edt-toggle-select ()
  "Toggle to start (or cancel) text selection."
  (interactive)
  (if edt-select-mode
      (edt-reset)
    (edt-select)))

;;;
;;; SENTENCE
;;;

(defun edt-sentence-forward (num)
  "Move forward to start of next sentence.
Argument NUM is the positive number of sentences to move."
  (interactive "p")
  (edt-check-prefix num)
  (let* ((left nil)
	 (beg (edt-current-line))
	 (height (window-height))
	 (top-percent
	  (if (= 0 edt-top-scroll-margin) 10 edt-top-scroll-margin))
	 (bottom-percent
	  (if (= 0 edt-bottom-scroll-margin) 15 edt-bottom-scroll-margin))
	 (top-margin (/ (* height top-percent) 100))
	 (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
	 (bottom-margin (max beg (- height bottom-up-margin 1)))
	 (top (save-excursion (move-to-window-line top-margin) (point)))
	 (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
	 (far (save-excursion
		(goto-char bottom) (forward-line (- height 2)) (point))))
    (if (eobp)
	(progn
	  (error "End of buffer"))
      (progn
	(forward-sentence num)
	(forward-word 1)
	(backward-sentence)))
    (cond((> (point) far)
	  (setq left (save-excursion (forward-line height)))
	  (if (= 0 left) (recenter top-margin)
	    (recenter (- left bottom-up-margin))))
	 (t
	  (and (> (point) bottom) (recenter bottom-margin)))))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-sentence-backward (num)
  "Move backward to next sentence beginning.
Argument NUM is the positive number of sentences to move."
  (interactive "p")
  (edt-check-prefix num)
  (let* ((left nil)
	 (beg (edt-current-line))
	 (height (window-height))
	 (top-percent
	  (if (= 0 edt-top-scroll-margin) 10 edt-top-scroll-margin))
	 (bottom-percent
	  (if (= 0 edt-bottom-scroll-margin) 15 edt-bottom-scroll-margin))
	 (top-margin (/ (* height top-percent) 100))
	 (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
	 (bottom-margin (max beg (- height bottom-up-margin 1)))
	 (top (save-excursion (move-to-window-line top-margin) (point)))
	 (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
	 (far (save-excursion
		(goto-char bottom) (forward-line (- height 2)) (point))))
    (if (eobp)
	(progn
	  (error "End of buffer"))
      (backward-sentence num))
    (and (< (point) top) (recenter (min beg top-margin))))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-sentence (num)
  "Move in current direction to next sentence.
Argument NUM is the positive number of sentences to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-sentence-forward num)
    (edt-sentence-backward num)))

;;;
;;; PARAGRAPH
;;;

(defun edt-paragraph-forward (num)
  "Move forward to beginning of paragraph.
Argument NUM is the positive number of paragraphs to move."
  (interactive "p")
  (edt-check-prefix num)
  (let* ((left nil)
	 (beg (edt-current-line))
	 (height (window-height))
	 (top-percent
	  (if (= 0 edt-top-scroll-margin) 10 edt-top-scroll-margin))
	 (bottom-percent
	  (if (= 0 edt-bottom-scroll-margin) 15 edt-bottom-scroll-margin))
	 (top-margin (/ (* height top-percent) 100))
	 (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
	 (bottom-margin (max beg (- height bottom-up-margin 1)))
	 (top (save-excursion (move-to-window-line top-margin) (point)))
	 (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
	 (far (save-excursion
		(goto-char bottom) (forward-line (- height 2)) (point))))
    (while (> num 0)
      (forward-paragraph (+ num 1))
      (start-of-paragraph-text)
      (if (eolp)
	  (forward-line 1))
      (setq num (1- num)))
    (cond((> (point) far)
	  (setq left (save-excursion (forward-line height)))
	  (if (= 0 left) (recenter top-margin)
	    (recenter (- left bottom-up-margin))))
	 (t
	  (and (> (point) bottom) (recenter bottom-margin)))))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-paragraph-backward (num)
  "Move backward to beginning of paragraph.
Argument NUM is the positive number of paragraphs to move."
  (interactive "p")
  (edt-check-prefix num)
  (let* ((left nil)
	 (beg (edt-current-line))
	 (height (window-height))
	 (top-percent
	  (if (= 0 edt-top-scroll-margin) 10 edt-top-scroll-margin))
	 (bottom-percent
	  (if (= 0 edt-bottom-scroll-margin) 15 edt-bottom-scroll-margin))
	 (top-margin (/ (* height top-percent) 100))
	 (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
	 (bottom-margin (max beg (- height bottom-up-margin 1)))
	 (top (save-excursion (move-to-window-line top-margin) (point)))
	 (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
	 (far (save-excursion
		(goto-char bottom) (forward-line (- height 2)) (point))))
    (while (> num 0)
      (start-of-paragraph-text)
      (setq num (1- num)))
    (and (< (point) top) (recenter (min beg top-margin))))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-paragraph (num)
  "Move in current direction to next paragraph.
Argument NUM is the positive number of paragraphs to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-paragraph-forward num)
    (edt-paragraph-backward num)))

;;;
;;; RESTORE KEY
;;;

(defun edt-restore-key ()
  "Restore last replaced key definition.
Definition is stored in `edt-last-replaced-key-definition'."
  (interactive)
  (if (featurep 'xemacs) (setq zmacs-region-stays t))
  (if edt-last-replaced-key-definition
      (progn
        (let (edt-key-definition)
          (set 'edt-key-definition
               (read-key-sequence "Press the key to be restored: "))
	  (if (if (featurep 'emacs)
		  (string-equal "\C-m" edt-key-definition)
		(string-equal "\C-m" (events-to-keys edt-key-definition)))
              (message "Key not restored")
	    (progn
	      (define-key (current-global-map)
		edt-key-definition edt-last-replaced-key-definition)
	      (if (featurep 'emacs)
		  (message "Key definition for %s has been restored."
			   edt-key-definition)
		(message "Key definition for %s has been restored."
			 (events-to-keys edt-key-definition)))))))
    (error "No replaced key definition to restore!")))

;;;
;;; WINDOW TOP
;;;

(defun edt-window-top ()
  "Move the cursor to the top of the window."
  (interactive)
  (let ((start-column (current-column)))
    (move-to-window-line 0)
    (move-to-column start-column))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

;;;
;;; WINDOW BOTTOM
;;;

(defun edt-window-bottom ()
  "Move the cursor to the bottom of the window."
  (interactive)
  (let ((start-column (current-column)))
    (move-to-window-line (- (window-height) 2))
    (move-to-column start-column))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

;;;
;;; SCROLL WINDOW LINE
;;;

(defun edt-scroll-window-forward-line ()
  "Move window forward one line leaving cursor at position in window."
  (interactive)
  (scroll-up 1)
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-scroll-window-backward-line ()
  "Move window backward one line leaving cursor at position in window."
  (interactive)
  (scroll-down 1)
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

(defun edt-scroll-line ()
  "Move window one line in current direction."
  (interactive)
  (if (equal edt-direction-string edt-forward-string)
      (edt-scroll-window-forward-line)
    (edt-scroll-window-backward-line)))

;;;
;;; SCROLL WINDOW
;;;
;;; Scroll a window (less one line) at a time.  Leave cursor in center of
;;; window.

(defun edt-scroll-window-forward (num)
  "Scroll forward one window in buffer, less one line.
Argument NUM is the positive number of windows to move."
  (interactive "p")
  (edt-check-prefix num)
  (scroll-up (- (* (window-height) num) 2))
  (edt-line-forward (/ (- (window-height) 1) 2)))

(defun edt-scroll-window-backward (num)
  "Scroll backward one window in buffer, less one line.
Argument NUM is the positive number of windows to move."
  (interactive "p")
  (edt-check-prefix num)
  (scroll-down (- (* (window-height) num) 2))
  (edt-line-backward (/ (- (window-height) 1) 2)))

(defun edt-scroll-window (num)
  "Scroll one window in buffer, less one line, in current direction.
Argument NUM is the positive number of windows to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-scroll-window-forward num)
    (edt-scroll-window-backward num)))

;;;
;;; LINE TO BOTTOM OF WINDOW
;;;

(defun edt-line-to-bottom-of-window ()
  "Move the current line to the bottom of the window."
  (interactive)
  (recenter -1)
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

;;;
;;; LINE TO TOP OF WINDOW
;;;

(defun edt-line-to-top-of-window ()
  "Move the current line to the top of the window."
  (interactive)
  (recenter 0)
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

;;;
;;; LINE TO MIDDLE OF WINDOW
;;;

(defun edt-line-to-middle-of-window ()
  "Move window so line with cursor is in the middle of the window."
  (interactive)
  (recenter '(4))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

;;;
;;; GOTO PERCENTAGE
;;;

(defun edt-goto-percentage (num)
  "Move to specified percentage in buffer from top of buffer.
Argument NUM is the percentage into the buffer to move."
  (interactive "NGoto-percentage: ")
  (if (or (> num 100) (< num 0))
      (error "Percentage %d out of range 0 < percent < 100" num)
    (goto-char (/ (* (point-max) num) 100)))
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

;;;
;;; FILL REGION
;;;

(defun edt-fill-region ()
  "Fill selected text."
  (interactive "*")
  (edt-check-selection)
  (fill-region (point) (mark)))

;;;
;;; INDENT OR FILL REGION
;;;

(defun edt-indent-or-fill-region ()
  "Fill region in text modes, indent region in programming language modes."
  (interactive "*")
  (if (string= paragraph-start "$\\|\f")
      (indent-region (point) (mark) nil)
    (fill-region (point) (mark))))


(declare-function c-mark-function "cc-cmds" ())
;;;
;;; MARK SECTION WISELY
;;;

(defun edt-mark-section-wisely ()
  "Mark the section in a manner consistent with the `major-mode'.
Uses `mark-defun' for Emacs-Lisp and Lisp, and for Fortran,
`c-mark-function' for C,
and `mark-paragraph' for other modes."
  (interactive)
  (if edt-select-mode
      (progn
        (edt-reset))
    (progn
      (cond  ((or (eq major-mode 'emacs-lisp-mode)
		  (eq major-mode 'fortran-mode)
		  (eq major-mode 'lisp-mode))
	      (mark-defun)
	      (message "Lisp defun selected"))
	     ((eq major-mode 'c-mode)
	      (c-mark-function)
	      (message "C function selected"))
	     (t (mark-paragraph)
		(message "Paragraph selected"))))))

;;;
;;; COPY
;;;

(defun edt-copy ()
  "Copy selected region to kill ring, but don't delete it!"
  (interactive)
  (edt-check-selection)
  (copy-region-as-kill (mark) (point))
  (edt-reset)
  (message "Selected text COPIED to kill ring"))

;;;
;;; CUT or COPY
;;;

(defun edt-cut-or-copy ()
  "Cuts (or copies) selected text to kill ring.
Cuts selected text if `buffer-read-only' is nil.
Copies selected text if `buffer-read-only' is t."
  (interactive)
  (if buffer-read-only
      (edt-copy)
    (edt-cut)))

;;;
;;; DELETE ENTIRE LINE
;;;

(defun edt-delete-entire-line ()
  "Delete entire line regardless of cursor position in the line."
  (interactive "*")
  (beginning-of-line)
  (edt-delete-line 1))

;;;
;;; DUPLICATE LINE
;;;

(defun edt-duplicate-line (num)
  "Duplicate the line of text containing the cursor.
Argument NUM is the number of times to duplicate the line."
  (interactive "*p")
  (edt-check-prefix num)
  (let ((old-column (current-column))
        (count num))
    (edt-delete-entire-line)
    (edt-undelete-line)
    (while (> count 0)
      (edt-undelete-line)
      (setq count (1- count)))
    (edt-line-forward num)
    (move-to-column old-column)))

;;;
;;; DUPLICATE WORD
;;;

(defun edt-duplicate-word()
  "Duplicate word (or rest of word) found directly above cursor, if any."
  (interactive "*")
  (let ((start (point))
        (start-column (current-column)))
    (forward-line -1)
    (move-to-column start-column)
    (if (and (not (equal start (point)))
             (not (eolp)))
        (progn
          (if (and (equal ?\t (preceding-char))
                   (< start-column (current-column)))
              (backward-char))
          (let ((beg (point)))
            (edt-one-word-forward)
            (setq edt-last-copied-word (buffer-substring beg (point))))
          (forward-line)
          (move-to-column start-column)
          (insert edt-last-copied-word))
      (progn
	(if (not (equal start (point)))
	    (forward-line))
	(move-to-column start-column)
	(error "Nothing to duplicate!")))))

;;;
;;; KEY NOT ASSIGNED
;;;

(defun edt-key-not-assigned ()
  "Displays message that key has not been assigned to a function."
  (interactive)
  (error "Key not assigned"))

;;;
;;; TOGGLE CAPITALIZATION OF WORD
;;;

(defun edt-toggle-capitalization-of-word ()
  "Toggle the capitalization of the current word and move forward to next."
  (interactive "*")
  (edt-one-word-forward)
  (edt-one-word-backward)
  (edt-change-case 1)
  (edt-one-word-backward)
  (edt-one-word-forward))

;;;
;;; ELIMINATE ALL TABS
;;;

(defun edt-eliminate-all-tabs ()
  "Convert all tabs to spaces in the entire buffer."
  (interactive "*")
  (untabify (point-min) (point-max))
  (message "TABS converted to SPACES"))

;;;
;;; DISPLAY THE TIME
;;;

(defun edt-display-the-time ()
  "Display the current time."
  (interactive)
  (if (featurep 'xemacs) (setq zmacs-region-stays t))
  (message "%s" (current-time-string)))

;;;
;;; LEARN
;;;

(defun edt-learn ()
  "Learn a sequence of key strokes to bind to a key."
  (interactive)
  (if (eq defining-kbd-macro t)
      (edt-remember)
    (start-kbd-macro nil)))

;;;
;;; REMEMBER
;;;

(defun edt-remember ()
  "Store the sequence of key strokes started by `edt-learn' to a key."
  (interactive)
  (if (eq defining-kbd-macro nil)
      (error "Nothing to remember!")
    (progn
      (end-kbd-macro nil)
      (let (edt-key-definition)
	(set 'edt-key-definition
	     (read-key-sequence "Enter key for binding: "))
	(if (if (featurep 'emacs)
		(string-equal "\C-m" edt-key-definition)
	      (string-equal "\C-m" (events-to-keys edt-key-definition)))
	    (message "Key sequence not remembered")
	  (progn
	    (set 'edt-learn-macro-count (+ edt-learn-macro-count 1))
	    (setq edt-last-replaced-key-definition
		  (lookup-key (current-global-map)
			      edt-key-definition))
	    (define-key (current-global-map) edt-key-definition
	      (name-last-kbd-macro
	       (intern (concat "last-learned-sequence-"
			       (int-to-string edt-learn-macro-count)))))))))))

;;;
;;; EXIT
;;;

(defun edt-exit ()
  "Save current buffer, ask to save other buffers, and then exit Emacs."
  (interactive)
  (save-buffer)
  (save-buffers-kill-emacs))

;;;
;;; QUIT
;;;

(defun edt-quit ()
  "Quit Emacs without saving buffer modifications.
Warn user that modifications will be lost."
  (interactive)
  (let ((list (buffer-list))
	(working t))
    (while (and list working)
      (let ((buffer (car list)))
	(if (and (buffer-file-name buffer) (buffer-modified-p buffer))
            (if (edt-y-or-n-p
		 "Modifications will not be saved, continue quitting? ")
		(kill-emacs)
	      (setq working nil)))
	(setq list (cdr list))))
    (if working (kill-emacs))))

;;;
;;; SPLIT WINDOW
;;;

(defun edt-split-window ()
  "Split current window and place cursor in the new window."
  (interactive)
  (split-window)
  (other-window 1)
  (if (featurep 'xemacs) (setq zmacs-region-stays t)))

;;;
;;; COPY RECTANGLE
;;;

(defun edt-copy-rectangle ()
  "Copy a rectangle of text between mark and cursor to register."
  (interactive)
  (edt-check-selection)
  (copy-rectangle-to-register 3 (region-beginning) (region-end) nil)
  (edt-reset)
  (message "Selected rectangle COPIED to register"))

;;;
;;; CUT RECTANGLE
;;;

(defun edt-cut-rectangle-overstrike-mode ()
  "Cut a rectangle of text between mark and cursor to register.
Replace cut characters with spaces and moving cursor back to
upper left corner."
  (interactive "*")
  (edt-check-selection)
  (setq edt-rect-start-point (region-beginning))
  (picture-clear-rectangle-to-register (region-beginning) (region-end) 3)
  (set-window-point (get-buffer-window (window-buffer)) edt-rect-start-point)
  (message "Selected rectangle CUT to register"))

(defun edt-cut-rectangle-insert-mode ()
  "Cut a rectangle of text between mark and cursor to register.
Move cursor back to upper left corner."
  (interactive "*")
  (edt-check-selection)
  (setq edt-rect-start-point (region-beginning))
  (picture-clear-rectangle-to-register (region-beginning) (region-end) 3 t)
  (fixup-whitespace)
  (set-window-point (get-buffer-window (window-buffer)) edt-rect-start-point)
  (message "Selected rectangle CUT to register"))

(defun edt-cut-rectangle ()
  "Cut a rectangular region of text to register.
If overwrite mode is active, cut text is replaced with whitespace."
  (interactive "*")
  (if overwrite-mode
      (edt-cut-rectangle-overstrike-mode)
    (edt-cut-rectangle-insert-mode)))

;;;
;;; PASTE RECTANGLE
;;;

(defun edt-paste-rectangle-overstrike-mode ()
  "Paste a rectangular region of text from register, replacing text at cursor."
  (interactive "*")
  (picture-yank-rectangle-from-register 3))

(defun edt-paste-rectangle-insert-mode ()
  "Paste previously deleted rectangular region, inserting text at cursor."
  (interactive "*")
  (picture-yank-rectangle-from-register 3 t))

(defun edt-paste-rectangle ()
  "Paste a rectangular region of text.
If overwrite mode is active, existing text is replace with text from register."
  (interactive)
  (if overwrite-mode
      (edt-paste-rectangle-overstrike-mode)
    (edt-paste-rectangle-insert-mode)))

;;;
;;; DOWNCASE REGION
;;;

(defun edt-lowercase ()
  "Change specified characters to lower case.
If text selection IS active, then characters between the cursor and
mark are changed.  If text selection is NOT active, there are two
situations.  If the current direction is ADVANCE, then the word under
the cursor is changed to lower case and the cursor is moved to rest at
the beginning of the next word.  If the current direction is BACKUP,
the word prior to the word under the cursor is changed to lower case
and the cursor is left to rest at the beginning of that word."
  (interactive "*")
  (if edt-select-mode
      (progn
	(downcase-region (mark) (point)))
    (progn
      ;; Move to beginning of current word.
      (if (and
	   (not (bobp))
	   (not (eobp))
	   (not (bolp))
	   (not (eolp))
	   (not (eq ?\  (char-syntax (preceding-char))))
	   (not (memq (preceding-char) edt-word-entities))
	   (not (memq (following-char) edt-word-entities)))
	  (edt-one-word-backward))
      (if (equal edt-direction-string edt-backward-string)
	  (edt-one-word-backward))
      (let ((beg (point)))
	(edt-one-word-forward)
	(downcase-region beg (point)))
      (if (equal edt-direction-string edt-backward-string)
	  (edt-one-word-backward)))))

;;;
;;; UPCASE REGION
;;;

(defun edt-uppercase ()
  "Change specified characters to upper case.
If text selection IS active, then characters between the cursor and
mark are changed.  If text selection is NOT active, there are two
situations.  If the current direction is ADVANCE, then the word under
the cursor is changed to upper case and the cursor is moved to rest at
the beginning of the next word.  If the current direction is BACKUP,
the word prior to the word under the cursor is changed to upper case
and the cursor is left to rest at the beginning of that word."
  (interactive "*")
  (if edt-select-mode
      (progn
	(upcase-region (mark) (point)))
    (progn
      ;; Move to beginning of current word.
      (if (and
	   (not (bobp))
	   (not (eobp))
	   (not (bolp))
	   (not (eolp))
	   (not (eq ?\  (char-syntax (preceding-char))))
	   (not (memq (preceding-char) edt-word-entities))
	   (not (memq (following-char) edt-word-entities)))
	  (edt-one-word-backward))
      (if (equal edt-direction-string edt-backward-string)
	  (edt-one-word-backward))
      (let ((beg (point)))
	(edt-one-word-forward)
	(upcase-region beg (point)))
      (if (equal edt-direction-string edt-backward-string)
	  (edt-one-word-backward)))))

;;;
;;;  Functions used in loading LK-201 key mapping file.
;;;
(defvar edt-last-answer nil
  "Most recent response to `edt-y-or-n-p'.")

(defun edt-y-or-n-p (prompt &optional not-yes)
  "Prompt for a y or n answer with positive default.
Like Emacs `y-or-n-p', also accepts space as y and DEL as n.
Argument PROMPT is the prompt string.
Optional argument NOT-YES changes the default to negative."
  (message "%s[%s]" prompt (if not-yes "n" "y"))
  (let ((doit t))
    (while doit
      (setq doit nil)
      (let ((ans (read-char)))
	(cond ((or (= ans ?y) (= ans ?Y) (= ans ?\ ))
	       (setq edt-last-answer t))
	      ((or (= ans ?n) (= ans ?N) (= ans ?\C-?))
	       (setq edt-last-answer nil))
	      ((= ans ?\r) (setq edt-last-answer (not not-yes)))
	      (t
	       (setq doit t) (beep)
	       (message "Please answer y or n.  %s[%s]"
			prompt (if not-yes "n" "y")))))))
  edt-last-answer)

;;;
;;; INITIALIZATION COMMANDS.
;;;

;;;
;;;  Function used to load LK-201 key mapping file generated by edt-mapper.el.
;;;
(defun edt-load-keys (file)
  "Load the LK-201 key mapping FILE generated by edt-mapper.el.
If FILE is nil, which is the normal case, try to load a default file.
The default file names are based upon the window system, terminal
type, and version of Emacs in use: GNU Emacs or XEmacs (aka Lucid
Emacs).  If a default file does not exist, ask user if one should be
created."
  (interactive "fKey definition file: ")
  (cond (file
	 (setq file (expand-file-name file)))
	(edt-keys-file
	 (setq file (expand-file-name edt-keys-file)))
	(t
	 (setq file
	       (expand-file-name
		(concat
		 "~/.edt-" edt-emacs-variant
		 (if edt-term (concat "-" edt-term))
		 (if edt-xserver (concat "-" edt-xserver))
		 (if edt-window-system
		     (concat "-" (upcase (symbol-name edt-window-system))))
		 "-keys")))))
  (cond ((file-readable-p file)
	 (load-file file))
	(t
	 (switch-to-buffer "*scratch*")
	 (erase-buffer)
	 (insert "

     Ack!!  You're running the Enhanced EDT Emulation without loading an
     EDT key mapping file.  To create an EDT key mapping file, run the
     edt-mapper.el program.  It is safest to run it from an Emacs loaded
     without any of your own customizations found in your .emacs file, etc.
     The reason for this is that some user customizations confuse edt-mapper.
     You can do this by quitting Emacs and then invoking Emacs again as
     follows:

          emacs -q -l edt-mapper.el

     [NOTE:  If you do nothing out of the ordinary in your .emacs file, and
     the search for edt-mapper.el is successful, you can try running it now.]

     The file edt-mapper.el includes these same directions on how to
     use it!  Perhaps it's lying around here someplace. \n     ")
	 (let ((file "edt-mapper.el")
	       (found nil)
	       (path nil)
	       (search-list (append (list (expand-file-name ".")) load-path)))
	   (while (and (not found) search-list)
	     (setq path (concat (car search-list)
				(if (string-match "/$" (car search-list)) "" "/")
				file))
	     (if (and (file-exists-p path) (not (file-directory-p path)))
		 (setq found t))
	     (setq search-list (cdr search-list)))
	   (cond (found
		  (insert (format
			   "Ah yes, there it is, in \n\n       %s \n\n" path))
		  (if (edt-y-or-n-p "Do you want to run it now? ")
		      (load-file path)
		    (error "EDT Emulation not configured")))
		 (t
		  (insert "Nope, I can't seem to find it.  :-(\n\n")
		  (sit-for 20)
		  (error "EDT Emulation not configured")))))))

;;;
;;;  Turning the EDT Emulation on and off.
;;;

;;;###autoload
(defun edt-emulation-on ()
  "Turn on EDT Emulation."
  (interactive)
  ;; If using pc window system (MS-DOS), set terminal type to pc.
  ;; If not a window system (GNU) or a tty window system (XEmacs),
  ;; get terminal type.
  (if (eq edt-window-system 'pc)
      (setq edt-term "pc")
    (if (or (not edt-window-system) (eq edt-window-system 'tty))
	(setq edt-term (getenv "TERM"))))
  ;; Look for for terminal configuration file for this terminal type.
  ;; Otherwise, load the user's custom configuration file.
  (if (or (not edt-window-system) (memq edt-window-system '(pc tty)))
      (progn
	;; Load terminal-specific configuration file, if it exists for this
	;; terminal type.  Note: All DEC VT series terminals are supported
	;; by the same terminal configuration file: edt-vt100.el.
	(if (string-equal "vt" (substring edt-term 0 (min (length edt-term) 2)))
	    (setq edt-term "vt100"))
	(let ((term edt-term)
	      hyphend)
	  (while (and term
		      (not (load (concat "edt-" term) t t)))
	    ;; Strip off last hyphen and what follows, then try again
	    (if (setq hyphend (string-match "[-_][^-_]+$" term))
		(setq term (substring term 0 hyphend))
	      (setq term nil)))
	  ;; If no terminal-specific configuration file exists, load user's
	  ;; custom EDT terminal configuration file.
	  ;; If this is a pc running MS-DOS, then custom configuration files
	  ;; are not supported.  So, if the file is missing, issue an error
	  ;; message.
	  (if (null term)
	      (if (equal edt-term "pc")
		  (error "Unable to find EDT terminal specific file edt-pc.el")
		(edt-load-keys nil))
	    (setq edt-term term))))
    (edt-load-keys nil))
  ;; Make highlighting of selected text work properly for EDT commands.
  (if (featurep 'emacs)
      (progn
	(setq edt-orig-transient-mark-mode transient-mark-mode)
	(add-hook 'activate-mark-hook
		  (function
		   (lambda ()
		     (edt-select-mode t))))
	(add-hook 'deactivate-mark-hook
		  (function
		   (lambda ()
		     (edt-select-mode nil)))))
    (progn
      (add-hook 'zmacs-activate-region-hook
		(function
		 (lambda ()
		   (edt-select-mode t))))
      (add-hook 'zmacs-deactivate-region-hook
		(function
		 (lambda ()
		   (edt-select-mode nil))))))
  ;;  Load user's EDT custom key bindings file, if it exists.
  ;;  Otherwise, use the default bindings.
  (if (load "edt-user" t t)
      (edt-user-emulation-setup)
      (edt-default-emulation-setup)))

(defun edt-emulation-off()
  "Select original global key bindings, disabling EDT Emulation."
  (interactive)
  (use-global-map global-map)
  (if (not edt-keep-current-page-delimiter)
      (setq page-delimiter edt-orig-page-delimiter))
  (setq edt-direction-string "")
  (setq edt-select-mode-current nil)
  (edt-reset)
  (force-mode-line-update t)
  (if (featurep 'emacs)
    (setq transient-mark-mode edt-orig-transient-mark-mode))
  (message "Original key bindings restored; EDT Emulation disabled"))

(defun edt-default-emulation-setup (&optional user-setup)
  "Setup emulation of DEC's EDT editor.
Optional argument USER-SETUP non-nil means  called from function
`edt-user-emulation-setup'."
  ;; Setup default EDT global map by copying global map bindings.
  ;; This preserves ESC and C-x prefix bindings and other bindings we
  ;; wish to retain in EDT emulation mode keymaps.  It also permits
  ;; customization of these bindings in the EDT global maps without
  ;; disturbing the original bindings in global-map.
  (fset 'edt-default-ESC-prefix (copy-keymap 'ESC-prefix))
  (setq edt-default-global-map (copy-keymap (current-global-map)))
  (if (featurep 'emacs)
      (define-key edt-default-global-map "\e" 'edt-default-ESC-prefix)
    (define-key edt-default-global-map [escape] 'edt-default-ESC-prefix))
  (define-prefix-command 'edt-default-gold-map)
  (edt-setup-default-bindings)
  ;; If terminal has additional function keys, the terminal-specific
  ;; initialization file can assign bindings to them via the optional
  ;; function edt-setup-extra-default-bindings.
  (if (fboundp 'edt-setup-extra-default-bindings)
      (edt-setup-extra-default-bindings))
  ;; Variable needed by edt-learn.
  (setq edt-learn-macro-count 0)
  ;; Display EDT text selection active within the mode line
  (or (assq 'edt-select-mode minor-mode-alist)
      (setq minor-mode-alist
	    (cons '(edt-select-mode edt-select-mode) minor-mode-alist)))
  ;; Display EDT direction of motion within the mode line
  (or (assq 'edt-direction-string minor-mode-alist)
      (setq minor-mode-alist
	    (cons
	     '(edt-direction-string edt-direction-string) minor-mode-alist)))
  (if user-setup
      (progn
        (setq edt-user-map-configured t)
        (fset 'edt-emulation-on (symbol-function 'edt-select-user-global-map)))
    (progn
      (fset 'edt-emulation-on (symbol-function 'edt-select-default-global-map))
      (edt-select-default-global-map))))

(defun edt-user-emulation-setup ()
  "Setup user custom emulation of DEC's EDT editor."
  ;; Initialize EDT default bindings.
  (edt-default-emulation-setup t)
  ;; Setup user EDT global map by copying default EDT global map bindings.
  (fset 'edt-user-ESC-prefix (copy-keymap 'edt-default-ESC-prefix))
  (setq edt-user-global-map (copy-keymap edt-default-global-map))
  (if (featurep 'emacs)
      (define-key edt-user-global-map "\e" 'edt-user-ESC-prefix)
    (define-key edt-user-global-map [escape] 'edt-user-ESC-prefix))
  ;; If terminal has additional function keys, the user's initialization
  ;; file can assign bindings to them via the optional
  ;; function edt-setup-extra-default-bindings.
  (define-prefix-command 'edt-user-gold-map)
  (fset 'edt-user-gold-map (copy-keymap 'edt-default-gold-map))
  ;; This is a function that the user can define for custom bindings.
  ;; See etc/edt-user.doc.
  (if (fboundp 'edt-setup-user-bindings)
      (edt-setup-user-bindings))
  (edt-select-user-global-map))

(defun edt-select-default-global-map()
  "Select default EDT emulation key bindings."
  (interactive)
  (if (featurep 'emacs)
    (transient-mark-mode 1))
  (use-global-map edt-default-global-map)
  (if (not edt-keep-current-page-delimiter)
      (progn
        (setq edt-orig-page-delimiter page-delimiter)
        (setq page-delimiter "\f")))
  (setq edt-default-map-active t)
  (edt-advance)
  (setq edt-select-mode-current 'edt-select-mode-string)
  (edt-reset)
  (message "Default EDT keymap active"))

(defun edt-select-user-global-map()
  "Select user EDT emulation custom key bindings."
  (interactive)
  (if edt-user-map-configured
      (progn
	(if (featurep 'emacs)
	    (transient-mark-mode 1))
        (use-global-map edt-user-global-map)
        (if (not edt-keep-current-page-delimiter)
            (progn
              (setq edt-orig-page-delimiter page-delimiter)
              (setq page-delimiter "\f")))
        (setq edt-default-map-active nil)
        (edt-advance)
	(setq edt-select-mode-current 'edt-select-mode-string)
        (edt-reset)
        (message "User EDT custom keymap active"))
    (error "User EDT custom keymap NOT configured!")))

(defun edt-switch-global-maps ()
  "Toggle between default EDT keymap and user EDT keymap."
  (interactive)
  (if edt-default-map-active
      (edt-select-user-global-map)
    (edt-select-default-global-map)))

;;
;;  Functions used to set up DEFAULT bindings to EDT emulation functions.
;;

(defun edt-bind-function-key-default (function-key binding gold-binding)
  "Binds LK-201 function keys to default bindings in the EDT Emulator.
Argument FUNCTION-KEY is the name of the function key or keypad function key.
Argument BINDING is the Emacs function to be bound to <KEY>.
Argument GOLD-BINDING is the Emacs function to be bound to GOLD <KEY>."
  (let ((key (cdr (assoc function-key *EDT-keys*))))
    (if (and key (not (equal key "")))
	(progn
	  (define-key edt-default-global-map key binding)
	  (define-key 'edt-default-gold-map key gold-binding)))))

(defun edt-bind-key-default (key binding)
  "Bind key sequences to default bindings in the EDT Emulator.
Argument KEY is the name of a standard key or a function key.
Argument BINDING is the Emacs function to be bound to <KEY>."
  (define-key edt-default-global-map key binding))

(defun edt-bind-gold-key-default (key gold-binding)
  "Binds <GOLD> key sequences to default bindings in the EDT Emulator.
Argument KEY is the name of a standard key or a function key.
Argument GOLD-BINDING is the Emacs function to be bound to GOLD <KEY>."
  (define-key 'edt-default-gold-map key gold-binding))

;;
;;  Functions used to set up USER CUSTOM bindings to EDT emulation functions.
;;
(defun edt-bind-function-key (function-key binding gold-binding)
  "Binds LK-201 function keys to custom bindings in the EDT Emulator.
Argument FUNCTION-KEY is the name of the function key or keypad function key.
Argument BINDING is the Emacs function to be bound to <KEY>.
Argument GOLD-BINDING is the Emacs function to be bound to GOLD <KEY>."
  (let ((key (cdr (assoc function-key *EDT-keys*))))
    (if (and key (not (equal key "")))
	(progn
	  (define-key edt-user-global-map key binding)
	  (define-key 'edt-user-gold-map key gold-binding)))))

(defun edt-bind-key (key binding)
  "Bind standard key sequences to custom bindings in the EDT Emulator.
Argument KEY is the name of a key.  It can be a standard key or a function key.
Argument BINDING is the Emacs function to be bound to <KEY>."
  (define-key edt-user-global-map key binding))

;;  For backward compatibility to existing edt-user.el files.
(fset 'edt-bind-standard-key (symbol-function 'edt-bind-key))

(defun edt-bind-gold-key (key gold-binding)
  "Binds <GOLD> standard key sequences to custom bindings in the EDT Emulator.
Argument KEY is the name of a standard key or a function key.
Argument GOLD-BINDING is the Emacs function to be bound to GOLD <KEY>."
  (define-key 'edt-user-gold-map key gold-binding))

(defun edt-setup-default-bindings ()
  "Assigns default EDT Emulation keyboard bindings."

  ;; Function Key Bindings:  Regular and GOLD.

  ;; VT100/VT200/VT300 PF1 (GOLD), PF2, PF3, PF4 Keys
  (edt-bind-function-key-default "PF1"
       'edt-default-gold-map 'edt-mark-section-wisely)
  (edt-bind-function-key-default "PF2"
       'edt-electric-keypad-help 'describe-function)
  (edt-bind-function-key-default "PF3" 'edt-find-next 'edt-find)
  (edt-bind-function-key-default "PF4" 'edt-delete-line 'edt-undelete-line)

  ;; VT100/VT200/VT300 Arrow Keys
  (edt-bind-function-key-default "UP" 'edt-previous-line 'edt-window-top)
  (edt-bind-function-key-default "DOWN" 'edt-next-line 'edt-window-bottom)
  (edt-bind-function-key-default "LEFT" 'backward-char 'edt-sentence-backward)
  (edt-bind-function-key-default "RIGHT" 'forward-char 'edt-sentence-forward)

  ;; VT100/VT200/VT300 Keypad Keys
  (edt-bind-function-key-default "KP0" 'edt-line 'open-line)
  (edt-bind-function-key-default "KP1" 'edt-word 'edt-change-case)
  (edt-bind-function-key-default "KP2"
       'edt-end-of-line 'edt-delete-to-end-of-line)
  (edt-bind-function-key-default "KP3" 'edt-character 'quoted-insert)
  (edt-bind-function-key-default "KP4" 'edt-advance 'edt-bottom)
  (edt-bind-function-key-default "KP5" 'edt-backup 'edt-top)
  (edt-bind-function-key-default "KP6" 'edt-cut 'yank)
  (edt-bind-function-key-default "KP7" 'edt-page 'execute-extended-command)
  (edt-bind-function-key-default "KP8" 'edt-sect 'edt-fill-region)
  (edt-bind-function-key-default "KP9" 'edt-append 'edt-replace)
  (edt-bind-function-key-default "KP-" 'edt-delete-word 'edt-undelete-word)
  (edt-bind-function-key-default "KP,"
       'edt-delete-character 'edt-undelete-character)
  (edt-bind-function-key-default "KPP" 'edt-select 'edt-reset)
  (edt-bind-function-key-default "KPE" 'other-window 'edt-substitute)

  ;; VT200/VT300 Function Keys
  ;; (F1 through F5, on the VT220, are not programmable, so we skip
  ;; making default bindings to those keys.
  (edt-bind-function-key-default "FIND" 'edt-find-next 'edt-find)
  (edt-bind-function-key-default "INSERT" 'yank 'edt-key-not-assigned)
  (edt-bind-function-key-default "REMOVE" 'edt-cut 'edt-copy)
  (edt-bind-function-key-default "SELECT"
       'edt-toggle-select 'edt-key-not-assigned)
  (edt-bind-function-key-default "NEXT"
       'edt-sect-forward 'edt-key-not-assigned)
  (edt-bind-function-key-default "PREVIOUS"
       'edt-sect-backward 'edt-key-not-assigned)
  (edt-bind-function-key-default "F6"
       'edt-key-not-assigned 'edt-key-not-assigned)
  (edt-bind-function-key-default "F7"
       'edt-copy-rectangle 'edt-key-not-assigned)
  (edt-bind-function-key-default "F8"
       'edt-cut-rectangle-overstrike-mode 'edt-paste-rectangle-overstrike-mode)
  (edt-bind-function-key-default "F9"
       'edt-cut-rectangle-insert-mode 'edt-paste-rectangle-insert-mode)
  (edt-bind-function-key-default "F10" 'edt-cut-rectangle 'edt-paste-rectangle)
  ;; Under X, the F11 key can be bound.  If using a VT-200 or higher terminal,
  ;; the default emacs terminal support causes the VT F11 key to seem as if it
  ;; is an ESC key when in emacs.
  (edt-bind-function-key-default "F11"
       'edt-key-not-assigned 'edt-key-not-assigned)
  (edt-bind-function-key-default "F12"
       'edt-beginning-of-line 'delete-other-windows) ;BS
  (edt-bind-function-key-default "F13"
       'edt-delete-to-beginning-of-word 'edt-key-not-assigned) ;LF
  (edt-bind-function-key-default "F14"
       'edt-key-not-assigned 'edt-key-not-assigned)
  (edt-bind-function-key-default "HELP"
       'edt-electric-keypad-help 'edt-key-not-assigned)
  (edt-bind-function-key-default "DO"
       'execute-extended-command 'edt-key-not-assigned)
  (edt-bind-function-key-default "F17"
       'edt-key-not-assigned 'edt-key-not-assigned)
  (edt-bind-function-key-default "F18"
       'edt-key-not-assigned 'edt-key-not-assigned)
  (edt-bind-function-key-default "F19"
       'edt-key-not-assigned 'edt-key-not-assigned)
  (edt-bind-function-key-default "F20"
       'edt-key-not-assigned 'edt-key-not-assigned)

  ;; Control key bindings:  Regular and GOLD
  ;;
  ;; Standard EDT control key bindings conflict with standard Emacs
  ;; control key bindings.  Normally, the standard Emacs control key
  ;; bindings are left unchanged in the default EDT mode.  However, if
  ;; the variable edt-use-EDT-control-key-bindings is set to true
  ;; before invoking edt-emulation-on for the first time, then the
  ;; standard EDT bindings (with some enhancements) as defined here are
  ;; used, instead.
  (if edt-use-EDT-control-key-bindings
      (progn
        (edt-bind-key-default "\C-a" 'edt-key-not-assigned)
        (edt-bind-key-default "\C-b" 'edt-key-not-assigned)
        ;; Leave binding of C-c to an Emacs prefix key.
        (edt-bind-key-default "\C-d" 'edt-key-not-assigned)
        (edt-bind-key-default "\C-e" 'edt-key-not-assigned)
        (edt-bind-key-default "\C-f" 'edt-key-not-assigned)
        ;; Leave binding of C-g to the Emacs keyboard-quit
        (edt-bind-key-default "\C-h" 'edt-beginning-of-line)
        (edt-bind-key-default "\C-i" 'edt-tab-insert)
        (edt-bind-key-default "\C-j" 'edt-delete-to-beginning-of-word)
        (edt-bind-key-default "\C-k" 'edt-define-key)
        (edt-bind-gold-key-default  "\C-k" 'edt-restore-key)
        (edt-bind-key-default "\C-l" 'edt-form-feed-insert)
        ;; Leave binding of C-m to newline.
        (edt-bind-key-default "\C-n" 'edt-set-screen-width-80)
        (edt-bind-key-default "\C-o" 'edt-key-not-assigned)
        (edt-bind-key-default "\C-p" 'edt-key-not-assigned)
        (edt-bind-key-default "\C-q" 'edt-key-not-assigned)
        ;; Leave binding of C-r to isearch-backward.
        ;; Leave binding of C-s to isearch-forward.
        (edt-bind-key-default "\C-t" 'edt-display-the-time)
        (edt-bind-key-default "\C-u" 'edt-delete-to-beginning-of-line)
        (edt-bind-key-default "\C-v" 'redraw-display)
        (edt-bind-key-default "\C-w" 'edt-set-screen-width-132)
        ;; Leave binding of C-x as original prefix key.
        (edt-bind-key-default "\C-y" 'edt-key-not-assigned)
        ;; Leave binding of C-z to suspend-emacs.
        )
      )

  ;; GOLD bindings for a few keys.
  (edt-bind-gold-key-default  "\C-g" 'keyboard-quit); Just in case.
  (edt-bind-gold-key-default  "\C-h" 'help-for-help); Just in case.
  (edt-bind-gold-key-default  [f1] 'help-for-help)
  (edt-bind-gold-key-default  [help] 'help-for-help)
  (edt-bind-gold-key-default  "\C-\\" 'split-window-vertically)

  ;; GOLD bindings for regular keys.
  (edt-bind-gold-key-default "a" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "A" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "b" 'buffer-menu)
  (edt-bind-gold-key-default "B" 'buffer-menu)
  (edt-bind-gold-key-default "c" 'compile)
  (edt-bind-gold-key-default "C" 'compile)
  (edt-bind-gold-key-default "d" 'delete-window)
  (edt-bind-gold-key-default "D" 'delete-window)
  (edt-bind-gold-key-default "e" 'edt-exit)
  (edt-bind-gold-key-default "E" 'edt-exit)
  (edt-bind-gold-key-default "f" 'find-file)
  (edt-bind-gold-key-default "F" 'find-file)
  (edt-bind-gold-key-default "g" 'find-file-other-window)
  (edt-bind-gold-key-default "G" 'find-file-other-window)
  (edt-bind-gold-key-default "h" 'edt-electric-keypad-help)
  (edt-bind-gold-key-default "H" 'edt-electric-keypad-help)
  (edt-bind-gold-key-default "i" 'insert-file)
  (edt-bind-gold-key-default "I" 'insert-file)
  (edt-bind-gold-key-default "j" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "J" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "k" 'edt-toggle-capitalization-of-word)
  (edt-bind-gold-key-default "K" 'edt-toggle-capitalization-of-word)
  (edt-bind-gold-key-default "l" 'edt-lowercase)
  (edt-bind-gold-key-default "L" 'edt-lowercase)
  (edt-bind-gold-key-default "m" 'save-some-buffers)
  (edt-bind-gold-key-default "M" 'save-some-buffers)
  (edt-bind-gold-key-default "n" 'next-error)
  (edt-bind-gold-key-default "N" 'next-error)
  (edt-bind-gold-key-default "o" 'switch-to-buffer-other-window)
  (edt-bind-gold-key-default "O" 'switch-to-buffer-other-window)
  (edt-bind-gold-key-default "p" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "P" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "q" 'edt-quit)
  (edt-bind-gold-key-default "Q" 'edt-quit)
  (edt-bind-gold-key-default "r" 'revert-buffer)
  (edt-bind-gold-key-default "R" 'revert-buffer)
  (edt-bind-gold-key-default "s" 'save-buffer)
  (edt-bind-gold-key-default "S" 'save-buffer)
  (edt-bind-gold-key-default "t" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "T" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "u" 'edt-uppercase)
  (edt-bind-gold-key-default "U" 'edt-uppercase)
  (edt-bind-gold-key-default "v" 'find-file-other-window)
  (edt-bind-gold-key-default "V" 'find-file-other-window)
  (edt-bind-gold-key-default "w" 'write-file)
  (edt-bind-gold-key-default "W" 'write-file)
  (edt-bind-gold-key-default "x" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "X" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "y" 'edt-emulation-off)
  (edt-bind-gold-key-default "Y" 'edt-emulation-off)
  (edt-bind-gold-key-default "z" 'edt-switch-global-maps)
  (edt-bind-gold-key-default "Z" 'edt-switch-global-maps)
  (edt-bind-gold-key-default "1" 'delete-other-windows)
  (edt-bind-gold-key-default "!" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "2" 'edt-split-window)
  (edt-bind-gold-key-default "@" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "3" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "#" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "4" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "$" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "5" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "%" 'edt-goto-percentage)
  (edt-bind-gold-key-default "6" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "^" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "7" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "&" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "8" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "*" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "9" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "(" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "0" 'edt-key-not-assigned)
  (edt-bind-gold-key-default ")" 'edt-key-not-assigned)
  (edt-bind-gold-key-default " " 'undo)
  (edt-bind-gold-key-default "," 'edt-key-not-assigned)
  (edt-bind-gold-key-default "<" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "." 'edt-key-not-assigned)
  (edt-bind-gold-key-default ">" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "/" 'query-replace)
  (edt-bind-gold-key-default "?" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "\\" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "|" 'edt-key-not-assigned)
  (edt-bind-gold-key-default ";" 'edt-key-not-assigned)
  (edt-bind-gold-key-default ":" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "'" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "\"" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "-" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "_" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "=" 'goto-line)
  (edt-bind-gold-key-default "+" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "[" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "{" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "]" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "}" 'edt-key-not-assigned)
  (edt-bind-gold-key-default "`" 'what-line)
  (edt-bind-gold-key-default "~" 'edt-key-not-assigned)
)

;;;
;;; DEFAULT EDT KEYPAD HELP
;;;

;;;
;;; Upper case commands in the keypad diagram below indicate that the
;;; emulation should look and feel very much like EDT.  Lower case
;;; commands are enhancements and/or additions to the EDT keypad
;;; commands or are native Emacs commands.
;;;

(defun edt-keypad-help ()
  "DEFAULT EDT Keypad Active.

   F7: Copy Rectangle             +----------+----------+----------+----------+
   F8: Cut Rect Overstrike        |Prev Line |Next Line |Bkwd Char |Frwd Char |
 G-F8: Paste Rect Overstrike      |   (UP)   |  (DOWN)  |  (LEFT)  | (RIGHT)  |
   F9: Cut Rect Insert            |Window Top|Window Bot|Bkwd Sent |Frwd Sent |
 G-F9: Paste Rect Insert          +----------+----------+----------+----------+
  F10: Cut Rectangle
G-F10: Paste Rectangle
  F11: ESC
  F12: Begining of Line           +----------+----------+----------+----------+
G-F12: Delete Other Windows       |   GOLD   |   HELP   |  FNDNXT  |  DEL L   |
  F13: Delete to Begin of Word    |   (PF1)  |   (PF2)  |   (PF3)  |  (PF4)   |
 HELP: Keypad Help                |Mark Wisel|Desc Funct|   FIND   |  UND L   |
G-HELP: Emacs Help                +----------+----------+----------+----------+
   DO: Execute extended command   |   PAGE   |   SECT   |  APPEND  |  DEL W   |
  C-g: Keyboard Quit              |    (7)   |    (8)   |    (9)   |   (-)    |
G-C-g: Keyboard Quit              |Ex Ext Cmd|Fill Regio| REPLACE  |  UND W   |
  C-h: Beginning of Line          +----------+----------+----------+----------+
G-C-h: Emacs Help                 |  ADVANCE |  BACKUP  |   CUT    |  DEL C   |
  C-i: Tab Insert                 |    (4)   |    (5)   |    (6)   |   (,)    |
  C-j: Delete to Begin of Word    |   BOTTOM |    TOP   |   Yank   |  UND C   |
  C-k: Define Key                 +----------+----------+----------+----------+
G-C-k: Restore Key                |   WORD   |    EOL   |   CHAR   |   Next   |
  C-l: Form Feed Insert           |    (1)   |    (2)   |    (3)   |  Window  |
  C-n: Set Screen Width 80        | CHNGCASE |  DEL EOL |Quoted Ins|          !
  C-r: Isearch Backward           +---------------------+----------+  (ENTER) |
  C-s: Isearch Forward            |         LINE        |  SELECT  |          !
  C-t: Display the Time           |         (0)         |    (.)   |   SUBS   |
  C-u: Delete to Begin of Line    |      Open Line      |   RESET  |          |
  C-v: Redraw Display             +---------------------+----------+----------+
  C-w: Set Screen Width 132
  C-z: Suspend Emacs                    +----------+----------+----------+
G-C-\\: Split Window                     |  FNDNXT  |   Yank   |   CUT    |
                                        |  (FIND)  | (INSERT) | (REMOVE) |
  G-b: Buffer Menu                      |   FIND   |          |   COPY   |
  G-c: Compile                          +----------+----------+----------+
  G-d: Delete Window                    |SELECT/RES|SECT BACKW|SECT FORWA|
  G-e: Exit                             | (SELECT) |(PREVIOUS)|  (NEXT)  |
  G-f: Find File                        |          |          |          |
  G-g: Find File Other Window           +----------+----------+----------+
  G-h: Keypad Help
  G-i: Insert File
  G-k: Toggle Capitalization Word
  G-l: Downcase Region
  G-m: Save Some Buffers
  G-n: Next Error
  G-o: Switch to Next Window
  G-q: Quit
  G-r: Revert File
  G-s: Save Buffer
  G-u: Upcase Region
  G-v: Find File Other Window
  G-w: Write file
  G-y: EDT Emulation OFF
  G-z: Switch to User EDT Key Bindings
  G-1: Delete Other Windows
  G-2: Split Window
  G-%: Go to Percentage
  G- : Undo  (GOLD Spacebar)
  G-=: Go to Line
  G-`: What line
  G-/: Query-Replace"

  (interactive)
  (describe-function 'edt-keypad-help))

(defun edt-electric-helpify (fun)
  (let ((name "*Help*"))
    (if (save-window-excursion
          (let* ((p (symbol-function 'print-help-return-message))
                 (b (get-buffer name))
                 (m (buffer-modified-p b)))
            (and b (not (get-buffer-window b))
                 (setq b nil))
            (unwind-protect
                (progn
                  (message "%s..." (capitalize (symbol-name fun)))
                  (and b
                       (save-excursion
                         (set-buffer b)
                         (set-buffer-modified-p t)))
                  (fset 'print-help-return-message 'ignore)
                  (call-interactively fun)
                  (and (get-buffer name)
                       (get-buffer-window (get-buffer name))
                       (or (not b)
                           (not (eq b (get-buffer name)))
                           (not (buffer-modified-p b)))))
              (fset 'print-help-return-message p)
              (and b (buffer-name b)
                   (save-excursion
                     (set-buffer b)
                     (set-buffer-modified-p m))))))
        (with-electric-help 'delete-other-windows name t))))

(defun edt-electric-keypad-help ()
  "Display default EDT bindings."
  (interactive)
  (edt-electric-helpify 'edt-keypad-help))

(defun edt-electric-user-keypad-help ()
  "Display user custom EDT bindings."
  (interactive)
  (edt-electric-helpify 'edt-user-keypad-help))

;;;
;;; EDT emulation screen width commands.
;;;
;; Some terminals require modification of terminal attributes when
;; changing the number of columns displayed, hence the fboundp tests
;; below.  These functions are defined in the corresponding terminal
;; specific file, if needed.

(defun edt-set-screen-width-80 ()
  "Set screen width to 80 columns."
  (interactive)
  (if (fboundp 'edt-set-term-width-80)
      (edt-set-term-width-80))
  (set-frame-width nil 80)
  (message "Terminal width 80"))

(defun edt-set-screen-width-132 ()
  "Set screen width to 132 columns."
  (interactive)
  (if (fboundp 'edt-set-term-width-132)
      (edt-set-term-width-132))
  (set-frame-width nil 132)
  (message "Terminal width 132"))

(provide 'edt)

;; arch-tag: 18d1c54f-6900-4078-8bbc-7c2292f48941
;;; edt.el ends here