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
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
|
;;; viper.el --- A full-featured Vi emulator.
;; a VI Plan for Emacs Rescue,
;; and a venomous VI PERil.
;; Viper Is also a Package for Emacs Rebels.
;;
;; Keywords: emulations
;; Author: Michael Kifer <kifer@cs.sunysb.edu>
(defconst viper-version "2.82 of October 12, 1995"
"The current version of Viper")
;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
;; 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 2, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
;;; Commentary:
;; Viper is a full-featured Vi emulator for Emacs 19. It emulates and
;; improves upon the standard features of Vi and, at the same time, allows
;; full access to all Emacs facilities. Viper supports multiple undo,
;; file name completion, command, file, and search history and it extends
;; Vi in many other ways. Viper is highly customizable through the various
;; hooks, user variables, and keymaps. It is implemented as a collection
;; of minor modes and it is designed to provide full access to all Emacs
;; major and minor modes.
;;
;;; History
;;
;; Viper is a new name for a package formerly known as VIP-19,
;; which was a successor of VIP version 3.5 by Masahiko Sato
;; <ms@sail.stanford.edu> and VIP version 4.2 by Aamod Sane
;; <sane@cs.uiuc.edu>. Some ideas from vip 4.4.2 by Aamod Sane
;; were also shamelessly plagiarized.
;;
;; Viper maintains some degree of compatibility with these older
;; packages. See the documentation for customization.
;;
;; The main difference between Viper and these older packages are:
;;
;; 1. Viper emulates Vi at several levels, from almost complete conformity
;; to a rather loose Vi-compliance.
;;
;; 2. Viper provides full access to all major and minor modes of Emacs
;; without the need to type extra keys.
;; The older versions of VIP (and other Vi emulators) do not work with
;; some major and minor modes.
;;
;; 3. Viper supports vi-style undo.
;;
;; 4. Viper fully emulates (and improves upon) vi's replacement mode.
;;
;; 5. Viper has a better interface to ex, including command, variable, and
;; file name completion.
;;
;; 6. Viper uses native Emacs history and completion features; it doesn't
;; rely on other packages (such as gmhist.el and completer.el) to provide
;; these features.
;;
;; 7. Viper supports Vi-style editing in the minibuffer, by allowing the
;; user to switch from Insert state to Vi state to Replace state, etc.
;;
;; 8. Viper keeps history of recently inserted pieces of text and recently
;; executed Vi-style destructive commands, such as `i', `d', etc.
;; These pieces of text can be inserted in later insertion commands;
;; the previous destructive commands can be re-executed.
;;
;; 9. Viper has Vi-style keyboard macros, which enhances the similar
;; facility in the original Vi.
;; First, one can execute any Emacs command while defining a
;; macro, not just the Vi commands. Second, macros are defined in a
;; WYSYWYG mode, using an interface to Emacs' WYSIWYG style of defining
;; macros. Third, in Viper, one can define macros that are specific to
;; a given buffer, a given major mode, or macros defined for all buffers.
;; The same macro name can have several different definitions:
;; one global, several definitions for various major modes, and
;; definitions for specific buffers.
;; Bffer-specific definitions override mode-specific
;; definitions, which, in turn, override global definitions.
;;
;;
;;; Installation:
;; -------------
;;
;; (require 'viper)
;;
;;; Acknowledgements:
;; -----------------
;; Bug reports and ideas contributed by the following users
;; have helped improve Viper and the various versions of VIP.
;; See the on-line manual for a complete list of contributors.
;;
;;
;;; Notes:
;;
;; 1. Major modes.
;; In most cases, Viper handles major modes correctly, i.e., they come up
;; in the right state (either vi-state or emacs-state). For instance, text
;; files come up in vi-state, while, say, Dired appears in emacs-state by
;; default.
;; However, some modes do not appear in the right mode in the beginning,
;; usually because they neglect to follow Emacs conventions (e.g., they don't
;; use kill-all-local-variables when they start). Some major modes
;; may fail to come up in emacs-state if they call hooks, such as
;; text-hook, for no good reason.
;;
;; As an immediate solution, you can hit C-z to bring about the right mode.
;; An interim solution is to add an appropriate hook to the mode like this:
;;
;; (add-hook 'your-favorite-mode 'viper-mode)
;; or
;; (add-hook 'your-favorite-mode 'vip-change-state-to-emacs)
;;
;; whichever applies. The right thing to do, however, is to complain to the
;; author of the respective package. (Sometimes they also neglect to equip
;; their modes with hooks, which is one more reason for complaining.)
;;
;; 2. Keymap handling
;; Because Emacs 19 has an elegant mechanism for turning minor mode keymaps
;; on and off, implementation of Viper has been greatly simplified. Viper
;; has several minor modes.
;;
;; Viper's Vi state consists of seven minor modes:
;;
;; vip-vi-intercept-minor-mode
;; vip-vi-local-user-minor-mode
;; vip-vi-global-user-minor-mode
;; vip-vi-kbd-minor-mode
;; vip-vi-state-modifier-minor-mode
;; vip-vi-diehard-minor-mode
;; vip-vi-basic-minor-mode
;;
;; Bindings done to the keymap of the first mode overshadow those done to
;; the second, which, in turn, overshadows those done to the third, etc.
;;
;; The last vip-vi-basic-minor-mode contains most of the usual Vi bindings
;; in its edit mode. This mode provides access to all Emacs facilities.
;; Novice users, however, may want to set their vip-expert-level to 1
;; in their .vip file. This will enable vip-vi-diehard-minor-mode. This
;; minor mode's bindings make Viper simulate the usual Vi very closely.
;; For instance, C-c will not have its standard Emacs binding
;; and so many of the goodies of Emacs are not available.
;;
;; An skilled user, should set vip-expert-level to at least 3. This will
;; enable ;; C-c and many Emacs facilities will become available.
;; In this case, vip-vi-diehard-minor-mode is inactive.
;;
;; Viper gurus should have at least
;; (setq vip-expert-level 4)
;; in their ~/.vip files. This will unsuppress all Emacs keys that are not
;; essential for VI-style editing.
;; Pick-and-choose users may want to put
;; (setq vip-expert-level 5)
;; in ~/.vip. Viper will then leave it up to the user to set the variables
;; vip-want-* See vip-set-expert-level for details.
;;
;; The very first minor mode, vip-vi-intercept-minor-mode, is of no
;; concern for the user. It is needed to bind Viper's vital keys, such as
;; ESC and C-z.
;;
;; The second mode, vip-vi-local-user-minor-mode, usually has an
;; empty keymap. However, the user can set bindings in this keymap, which
;; will overshadow the corresponding bindings in the other two minor
;; modes. This is useful, for example, for setting up ZZ in gnus,
;; rmail, mh-e, etc., to send message instead of saving it in a file.
;; Likewise, in Dired mode, you may want to bind ZN and ZP to commands
;; that would visit the next or the previous file in the Dired buffer.
;; Setting local keys is tricky, so don't do it directly. Instead, use
;; vip-add-local-keys function (see its doc).
;;
;; The third minor mode, vip-vi-global-user-minor-mode, is also intended
;; for the users but, unlike vip-vi-local-user-minor-mode, its key
;; bindings are seen in all Viper buffers. This mode keys can be done
;; with define-key command.
;;
;; The fourth minor mode, vip-vi-kbd-minor-mode, is used by keyboard
;; macros. Users are NOT supposed to modify this keymap directly.
;;
;; The fifth mode, vip-vi-state-modifier-minor-mode, can be used to set
;; key bindings that are visible in some major modes but not in others.
;;
;; Users are allowed to modify keymaps that belong to
;; vip-vi-local-user-minor-mode, vip-vi-global-user-minor-mode,
;; and vip-vi-state-modifier-minor-mode only.
;;
;; Viper's Insert state also has seven minor modes:
;;
;; vip-insert-intercept-minor-mode
;; vip-insert-local-user-minor-mode
;; vip-insert-global-user-minor-mode
;; vip-insert-kbd-minor-mode
;; vip-insert-state-modifier-minor-mode
;; vip-insert-diehard-minor-mode
;; vip-insert-basic-minor-mode
;;
;; As with VI's editing modes, the first mode, vip-insert-intercept-minor-mode
;; is used to bind vital keys that are not to be changed by the user.
;;
;; The next mode, vip-insert-local-user-minor-mode, is used to customize
;; bindings in the insert state of Viper. The third mode,
;; vip-insert-global-user-minor-mode is like
;; vip-insert-local-user-minor-mode, except that its bindings are seen in
;; all Viper buffers. As with vip-vi-local-user-minor-mode, its bindings
;; should be done via the function vip-add-local-keys. Bindings for
;; vip-insert-global-user-minor-mode can be set with the define-key command.
;;
;; The next minor mode, vip-insert-kbd-minor-mode,
;; is used for keyboard VI-style macros defined with :map!.
;;
;; The fifth minor mode, vip-insert-state-modifier-minor-mode, is like
;; vip-vi-state-modifier-minor-mode, except that it is used in the Insert
;; state; it can be used to modify keys in a mode-specific fashion.
;;
;; The minor mode vip-insert-diehard-minor-mode is in effect when
;; the user wants a high degree of Vi compatibility (a bad idea, really!).
;; The last minor mode, vip-insert-basic-minor-mode, is always in effect
;; when Viper is in insert state. It binds a small number of keys needed for
;; Viper's operation.
;;
;; Finally, Viper provides minor modes for overriding bindings set by Emacs
;; modes when Viper is in Emacs state:
;;
;; vip-emacs-local-user-minor-mode
;; vip-emacs-global-user-minor-mode
;; vip-emacs-kbd-minor-mode
;; vip-emacs-state-modifier-minor-mode
;;
;; These minor modes are in effect when Viper is in Emacs state. The keymap
;; associated with vip-emacs-global-user-minor-mode,
;; vip-emacs-global-user-map, overrides the global and local keymaps as
;; well as the minor mode keymaps set by other modes. The keymap of
;; vip-emacs-local-user-minor-mode, vip-emacs-local-user-map, overrides
;; everything, but it is used on a per buffer basis.
;; The keymap associated with vip-emacs-state-modifier-minor-mode
;; overrides keys on a per-major-mode basis. The mode
;; vip-emacs-kbd-minor-mode is used to define Vi-style macros in Emacs
;; state.
;;
;; 3. There is also one minor mode that is used when Viper is in its
;; replace-state (used for commands like cw, C, etc.). This mode is
;; called
;;
;; vip-replace-minor-mode
;;
;; and its keymap is vip-replace-map. Replace minor mode is always
;; used in conjunction with the minor modes for insert-state, and its
;; keymap overshadows the keymaps for insert minor modes.
;;
;; 4. Defining buffer-local bindings in Vi and Insert modes.
;; As mentioned before, sometimes, it is convenient to have
;; buffer-specific of mode-specific key bindings in Vi and insert modes.
;; Viper provides a special function, vip-add-local-keys, to do precisely
;; this. For instance, is you need to add couple of mode-specific bindings
;; to Insert mode, you can put
;;
;; (vip-add-local-keys 'insert-state '((key1 . func1) (key2 .func2)))
;;
;; somewhere in a hook of this major mode. If you put something like this
;; in your own elisp function, this will define bindings specific to the
;; buffer that was current at the time of the call to vip-add-local-keys.
;; The only thing to make sure here is that the major mode of this buffer
;; is written according to Emacs conventions, which includes a call to
;; (kill-all-local-variables). See vip-add-local-keys for more details.
;;
;;
;; TO DO (volunteers?):
;;
;; 1. Some of the code that is inherited from VIP-3.5 is rather
;; convoluted. Instead of vip-command-argument, keymaps should bind the
;; actual commands. E.g., "dw" should be bound to a generic command
;; vip-delete that will delete things based on the value of
;; last-command-char. This would greatly simplify the logic and the code.
;;
;; 2. Somebody should venture to write a customization package a la
;; options.el that would allow the user to change values of variables
;; that meet certain specs (e.g., match a regexp) and whose doc string
;; starts with a '*'. Then, the user should be offered to save
;; variables that were changed. This will make user's customization job
;; much easier.
;;
(require 'advice)
(require 'cl)
(require 'ring)
(require 'viper-util)
;;; Variables
;; Is t until viper-mode executes for the very first time.
;; Prevents recursive descend into startup messages.
(defvar vip-first-time t)
(defvar vip-expert-level 0
"User's expert level.
The minor mode vip-vi-diehard-minor-mode is in effect when
vip-expert-level is 1 or 2 or when vip-want-emacs-keys-in-vi is t.
The minor mode vip-insert-diehard-minor-mode is in effect when
vip-expert-level is 1 or 2 or if vip-want-emacs-keys-in-insert is t.
Use `M-x vip-set-expert-level' to change this.")
;; Max expert level supported by Viper. This is NOT a user option.
;; It is here to make it hard for the user from resetting it.
(defconst vip-max-expert-level 5)
;; Contains user settings for vars affected by vip-set-expert-level function.
;; Not a user option.
(defvar vip-saved-user-settings nil)
;;; Viper minor modes
;; This is not local in Emacs, so we make it local.
(make-variable-buffer-local 'minor-mode-map-alist)
;; Mode for vital things like \e, C-z.
(vip-deflocalvar vip-vi-intercept-minor-mode nil)
(vip-deflocalvar vip-vi-basic-minor-mode nil
"Viper's minor mode for Vi bindings.")
(vip-deflocalvar vip-vi-local-user-minor-mode nil
"Auxiliary minor mode for user-defined local bindings in Vi state.")
(vip-deflocalvar vip-vi-global-user-minor-mode nil
"Auxiliary minor mode for user-defined global bindings in Vi state.")
(vip-deflocalvar vip-vi-state-modifier-minor-mode nil
"Minor mode used to make major-mode-specific modification to Vi state.")
(vip-deflocalvar vip-vi-diehard-minor-mode nil
"This minor mode is in effect when the user wants Viper to be Vi.")
(vip-deflocalvar vip-vi-kbd-minor-mode nil
"Minor mode for Ex command macros in Vi state.
The corresponding keymap stores key bindings of Vi macros defined with
the Ex command :map.")
;; Mode for vital things like \e, C-z.
(vip-deflocalvar vip-insert-intercept-minor-mode nil)
(vip-deflocalvar vip-insert-basic-minor-mode nil
"Viper's minor mode for bindings in Insert mode.")
(vip-deflocalvar vip-insert-local-user-minor-mode nil
"Auxiliary minor mode for buffer-local user-defined bindings in Insert state.
This is a way to overshadow normal Insert mode bindings locally to certain
designated buffers.")
(vip-deflocalvar vip-insert-global-user-minor-mode nil
"Auxiliary minor mode for global user-defined bindings in Insert state.")
(vip-deflocalvar vip-insert-state-modifier-minor-mode nil
"Minor mode used to make major-mode-specific modification to Insert state.")
(vip-deflocalvar vip-insert-diehard-minor-mode nil
"Minor mode that simulates Vi very closely.
Not recommened, except for the novice user.")
(vip-deflocalvar vip-insert-kbd-minor-mode nil
"Minor mode for Ex command macros Insert state.
The corresponding keymap stores key bindings of Vi macros defined with
the Ex command :map!.")
(vip-deflocalvar vip-replace-minor-mode nil
"Minor mode in effect in replace state (cw, C, and the like commands).")
;; Mode for vital things like \C-z and \C-x)
;; This is t, by default. So, any new buffer will have C-z defined as
;; switch to Vi, unless we switched states in this buffer
(vip-deflocalvar vip-emacs-intercept-minor-mode t)
(vip-deflocalvar vip-emacs-local-user-minor-mode t
"Minor mode for local user bindings effective in Emacs state.
Users can use it to override Emacs bindings when Viper is in its Emacs
state.")
(vip-deflocalvar vip-emacs-global-user-minor-mode t
"Minor mode for global user bindings in effect in Emacs state.
Users can use it to override Emacs bindings when Viper is in its Emacs
state.")
(vip-deflocalvar vip-emacs-kbd-minor-mode t
"Minor mode for Vi style macros in Emacs state.
The corresponding keymap stores key bindings of Vi macros defined with
`vip-record-kbd-macro' command. There is no Ex-level command to do this
interactively.")
(vip-deflocalvar vip-emacs-state-modifier-minor-mode t
"Minor mode used to make major-mode-specific modification to Emacs state.
For instance, a Vi purist may want to bind `dd' in Dired mode to a function
that deletes a file.")
;;; ISO characters
(vip-deflocalvar vip-automatic-iso-accents nil
"*If non-nil, ISO accents will be turned on in insert/replace emacs states and turned off in vi-state.
For some users, this behavior may be too primitive. In this case, use
insert/emacs/vi state hooks.")
;;; Emacs keys in other states.
(defvar vip-want-emacs-keys-in-insert t
"*Set to nil if you want complete Vi compatibility in insert mode.
Complete compatibility with Vi is not recommended for power use of Viper.")
(defvar vip-want-emacs-keys-in-vi t
"*Set to nil if you want complete Vi compatibility in Vi mode.
Full Vi compatibility is not recommended for power use of Viper.")
;; VI-style Undo
;; Used to 'undo' complex commands, such as replace and insert commands.
(vip-deflocalvar vip-undo-needs-adjustment nil)
(put 'vip-undo-needs-adjustment 'permanent-local t)
;; A mark that Viper puts on buffer-undo-list. Marks the beginning of a
;; complex command that must be undone atomically. If inserted, it is
;; erased by vip-change-state-to-vi and vip-repeat.
(defconst vip-buffer-undo-list-mark 'viper)
(defvar vip-keep-point-on-undo nil
"*Non-nil means not to move point while undoing commands.
This style is different from Emacs and Vi. Try it to see if
it better fits your working style.")
;; Replace mode and changing text
;; Viper's own after/before change functions, which get vip-add-hook'ed to
;; Emacs's
(vip-deflocalvar vip-after-change-functions nil "")
(vip-deflocalvar vip-before-change-functions nil "")
(vip-deflocalvar vip-post-command-hooks nil "")
(vip-deflocalvar vip-pre-command-hooks nil "")
;; Can be used to pass global states around for short period of time
(vip-deflocalvar vip-intermediate-command nil "")
;; Indicates that the current destructive command has started in replace mode.
(vip-deflocalvar vip-began-as-replace nil "")
(defvar vip-replace-overlay-cursor-color "Red"
"*Cursor color to use in Replace state")
(vip-deflocalvar vip-replace-overlay nil "")
(put 'vip-replace-overlay 'permanent-local t)
(if (vip-window-display-p)
(progn
(add-to-list 'facemenu-unlisted-faces 'vip-replace-overlay-face)
(make-face 'vip-replace-overlay-face)
(or (face-differs-from-default-p 'vip-replace-overlay-face)
(progn
(if (vip-can-use-colors "darkseagreen2" "Black")
(progn
(set-face-background
'vip-replace-overlay-face "darkseagreen2")
(set-face-foreground 'vip-replace-overlay-face "Black")))
(set-face-underline-p 'vip-replace-overlay-face t))
)))
(defvar vip-replace-overlay-face 'vip-replace-overlay-face
"*Face for highlighting replace regions on a window display.")
(defvar vip-replace-region-end-delimiter "$"
"A string marking the end of replacement regions.
It is used only with TTYs or if `vip-use-replace-region-delimiters'
is non-nil.")
(defvar vip-replace-region-start-delimiter ""
"A string marking the beginning of replacement regions.
It is used only with TTYs or if `vip-use-replace-region-delimiters'
is non-nil.")
(defvar vip-use-replace-region-delimiters
(or (not (vip-window-display-p)) (not (vip-color-display-p)))
"*If non-nil, Viper will always use `vip-replace-region-end-delimiter' and
`vip-replace-region-start-delimiter' to delimit replacement regions, even on
color displays. By default, the delimiters are used only on TTYs or
monochrome displays.")
;; XEmacs requires glyphs
(if vip-xemacs-p
(progn
(or (glyphp vip-replace-region-end-delimiter)
(setq vip-replace-region-end-delimiter
(make-glyph vip-replace-region-end-delimiter)))
(or (glyphp vip-replace-region-start-delimiter)
(setq vip-replace-region-start-delimiter
(make-glyph vip-replace-region-start-delimiter)))
))
;; These are local marker that must be initialized to nil and moved with
;; `vip-move-marker-locally'
;;
;; Remember the last position inside the replace region.
(vip-deflocalvar vip-last-posn-in-replace-region nil)
;; Remember the last position while inserting
(vip-deflocalvar vip-last-posn-while-in-insert-state nil)
(put 'vip-last-posn-in-replace-region 'permanent-local t)
(put 'vip-last-posn-while-in-insert-state 'permanent-local t)
(vip-deflocalvar vip-sitting-in-replace nil "")
(put 'vip-sitting-in-replace 'permanent-local t)
;; Remember the number of characters that have to be deleted in replace
;; mode to compensate for the inserted characters.
(vip-deflocalvar vip-replace-chars-to-delete 0 "")
(vip-deflocalvar vip-replace-chars-deleted 0 "")
;; Insertion ring and command ring
(defvar vip-insertion-ring-size 14
"The size of the insertion ring.")
;; The insertion ring.
(defvar vip-insertion-ring nil)
;; This is temp insertion ring. Used to do rotation for display purposes.
;; When rotation just started, it is initialized to vip-insertion-ring.
(defvar vip-temp-insertion-ring nil)
(defvar vip-last-inserted-string-from-insertion-ring "")
(defvar vip-command-ring-size 14
"The size of the command ring.")
;; The command ring.
(defvar vip-command-ring nil)
;; This is temp command ring. Used to do rotation for display purposes.
;; When rotation just started, it is initialized to vip-command-ring.
(defvar vip-temp-command-ring nil)
;; Modes and related variables
;; Current mode. One of: `emacs-state', `vi-state', `insert-state'
(vip-deflocalvar vip-current-state 'emacs-state)
(defvar vip-toggle-key "\C-z"
"The key used to change states from emacs to Vi and back.
In insert mode, this key also functions as Meta.
Must be set in .vip file or prior to loading Viper.
This setting cannot be changed interactively.")
(defvar vip-ESC-key "\e"
"Key used to ESC.
Must be set in .vip file or prior to loading Viper.
This setting cannot be changed interactively.")
(defvar vip-no-multiple-ESC t
"*If true, multiple ESC in Vi mode will cause bell to ring.
\_ is then mapped to Meta.
This is set to t on a windowing terminal and to 'twice on a dumb
terminal (unless the user level is 1, 2, or 5). On a dumb terminal, this
enables cursor keys and is generally more convenient, as terminals usually
don't have a convenient Meta key.
Setting vip-no-multiple-ESC to nil will allow as many multiple ESC,
as is allowed by the major mode in effect.")
(defvar vip-want-ctl-h-help nil
"*If t then C-h is bound to help-command in insert mode, if nil then it is
bound to delete-backward-char.")
;; Autoindent in insert
;; Variable that keeps track of whether C-t has been pressed.
(vip-deflocalvar vip-cted nil "")
;; Preserve the indent value, used by C-d in insert mode.
(vip-deflocalvar vip-current-indent 0)
;; Whether to preserve the indent, used by C-d in insert mode.
(vip-deflocalvar vip-preserve-indent nil)
(defconst vip-auto-indent nil
"*Autoindent if t.")
(defconst vip-shift-width 8
"*The shiftwidth variable.")
;; Variables for repeating destructive commands
(defconst vip-keep-point-on-repeat t
"*If t, don't move point when repeating previous command.
This is useful for doing repeated changes with the '.' key.
The user can change this to nil, if she likes when the cursor moves
to a new place after repeating previous Vi command.")
;; Remember insert point as a marker. This is a local marker that must be
;; initialized to nil and moved with `vip-move-marker-locally'.
(vip-deflocalvar vip-insert-point nil)
(put 'vip-insert-point 'permanent-local t)
;; This remembers the point before dabbrev-expand was called.
;; If vip-insert-point turns out to be bigger than that, it is reset
;; back to vip-pre-command-point.
;; The reason this is needed is because dabbrev-expand (and possibly
;; others) may jump to before the insertion point, delete something and
;; then reinsert a bigger piece. For instance: bla^blo
;; If dabbrev-expand is called after `blo' and ^ undicates vip-insert-point,
;; then point jumps to the beginning of `blo'. If expansion is found, `blablo'
;; is deleted, and we have |^, where | denotes point. Next, dabbrev-expand
;; will insert the expansion, and we get: blablo^
;; Whatever we insert next goes before the ^, i.e., before the
;; vip-insert-point marker. So, Viper will think that nothing was
;; inserted. Remembering the orig position of the marker circumvents the
;; problem.
;; We don't know of any command, except dabbrev-expand, that has the same
;; problem. However, the same trick can be used if such a command is
;; discovered later.
;;
(vip-deflocalvar vip-pre-command-point nil)
(put 'vip-pre-command-point 'permanent-local t) ; this is probably an overkill
;; This is used for saving inserted text.
(defvar vip-last-insertion nil)
;; Remembers the last replaced region.
(defvar vip-last-replace-region "")
;; Remember com point as a marker.
;; This is a local marker. Should be moved with `vip-move-marker-locally'
(vip-deflocalvar vip-com-point nil)
;; If non-nil, the value is a list (M-COM VAL COM REG inserted-text cmd-keys)
;; It is used to re-execute last destructive command.
;; M-COM is a Lisp symbol representing the function to be executed.
;; VAL is the prefix argument that was used with that command.
;; COM is an internal descriptor, such as ?r, ?c, ?C, which contains
;; additional information on how the function in M-COM is to be handled.
;; REG is the register used by command
;; INSERTED-TEXT is text inserted by that command (in case of o, c, C, i, r
;; commands).
;; COMMAND-KEYS are the keys that were typed to invoke the command.
(defvar vip-d-com nil)
;; The character remembered by the Vi `r' command.
(defvar vip-d-char nil)
;; Name of register to store deleted or yanked strings
(defvar vip-use-register nil)
;; Variables for Moves and Searches
;; For use by `;' command.
(defvar vip-f-char nil)
;; For use by `.' command.
(defvar vip-F-char nil)
;; For use by `;' command.
(defvar vip-f-forward nil)
;; For use by `;' command.
(defvar vip-f-offset nil)
;; Last search string
(defvar vip-s-string "")
(defvar vip-quote-string "> "
"String inserted at the beginning of quoted region.")
;; If t, search is forward.
(defvar vip-s-forward nil)
(defconst vip-case-fold-search nil
"*If t, search ignores cases.")
(defconst vip-re-search t
"*If t, search is reg-exp search, otherwise vanilla search.")
(defconst vip-re-query-replace t
"*If t then do regexp replace, if nil then do string replace.")
(defconst vip-re-replace t
"*If t, do regexp replace. nil means do string replace.")
(vip-deflocalvar vip-ex-style-motion t
"*Ex-style: the commands l,h do not cross lines, etc.")
(vip-deflocalvar vip-ex-style-editing-in-insert t
"*The keys ^H, ^? don't jump lines in insert, ESC moves cursor back, etc.
Note: this doesn't preclude ^H and ^? from deleting characters by moving
past the insertion point. This is a feature, not a bug. ")
(vip-deflocalvar vip-delete-backwards-in-replace nil
"*If t, DEL key will delete characters while moving the cursor backwards.
If nil, the cursor will move backwards without deleting anything.")
(defconst vip-buffer-search-char nil
"*Key bound for buffer-searching.")
(defconst vip-search-wrap-around-t t
"*If t, search wraps around.")
(vip-deflocalvar vip-related-files-and-buffers-ring nil
"*Ring of file and buffer names that are considered to be related to the
current buffer.
These buffers can be cycled through via :R and :P commands.")
(put 'vip-related-files-and-buffers-ring 'permanent-local t)
;; Used to find out if we are done with searching the current buffer.
(vip-deflocalvar vip-local-search-start-marker nil)
;; As above, but global
(defvar vip-search-start-marker (make-marker))
;; the search overlay
(vip-deflocalvar vip-search-overlay nil)
(defvar vip-heading-start
(concat "^\\s-*(\\s-*defun\\s-\\|" ; lisp
"^{\\s-*$\\|^[_a-zA-Z][^()]*[()].*{\\s-*$\\|" ; C/C++
"^\\s-*class.*{\\|^\\s-*struct.*{\\|^\\s-*enum.*{\\|"
"^\\\\[sb][a-z]*{.*}\\s-*$\\|" ; latex
"^@node\\|@table\\|^@m?enu\\|^@itemize\\|^@if\\|" ; texinfo
"^.+:-") ; prolog
"*Regexps for Headings. Used by \[\[ and \]\].")
(defvar vip-heading-end
(concat "^}\\|" ; C/C++
"^\\\\end{\\|" ; latex
"^@end \\|" ; texinfo
")\n\n[ \t\n]*\\|" ; lisp
"\\.\\s-*$") ; prolog
"*Regexps to end Headings/Sections. Used by \[\].")
;; These two vars control the interaction of jumps performed by ' and `.
;; In this new version, '' doesn't erase the marks set by ``, so one can
;; use both kinds of jumps interchangeably and without loosing positions
;; inside the lines.
;; Remembers position of the last jump done using ``'.
(vip-deflocalvar vip-last-jump nil)
;; Remembers position of the last jump done using `''.
(vip-deflocalvar vip-last-jump-ignore 0)
;; Some common error messages
(defconst vip-SpuriousText "Spurious text after command" "")
(defconst vip-BadExCommand "Not an editor command" "")
(defconst vip-InvalidCommandArgument "Invalid command argument" "")
(defconst vip-NoPrevSearch "No previous search string" "")
(defconst vip-EmptyRegister "`%c': Nothing in this register" "")
(defconst vip-InvalidRegister "`%c': Invalid register" "")
(defconst vip-EmptyTextmarker "`%c': Text marker doesn't point anywhere" "")
(defconst vip-InvalidTextmarker "`%c': Invalid text marker" "")
(defconst vip-InvalidViCommand "Invalid command" "")
(defconst vip-BadAddress "Ill-formed address" "")
(defconst vip-FirstAddrExceedsSecond "First address exceeds second" "")
(defconst vip-NoFileSpecified "No file specified" "")
;; History variables
;; History of search strings.
(defvar vip-search-history (list ""))
;; History of query-replace strings used as a source.
(defvar vip-replace1-history nil)
;; History of query-replace strings used as replacement.
(defvar vip-replace2-history nil)
;; History of region quoting strings.
(defvar vip-quote-region-history (list vip-quote-string))
;; History of Ex-style commands.
(defvar vip-ex-history nil)
;; History of shell commands.
(defvar vip-shell-history nil)
;; Last shell command. There are two of these, one for Ex (in viper-ex)
;; and one for Vi.
;; Last shell command executed with ! command.
(defvar vip-last-shell-com nil)
;;; Miscellaneous
;; don't bark when mark is inactive
(setq mark-even-if-inactive t)
(defvar vip-inhibit-startup-message nil
"Whether Viper startup message should be inhibited.")
(defvar vip-always t
"t means, arrange that vi-state will be a default.")
(defvar vip-custom-file-name "~/.vip"
"Viper customisation file.
This variable must be set _before_ loading Viper.")
(defvar vip-spell-function 'ispell-region
"Spell function used by #s<move> command to spell.")
(defvar vip-tags-file-name "TAGS"
"The tags file used by Viper.")
;; Minibuffer
(defvar vip-vi-style-in-minibuffer t
"If t, use vi-style editing in minibuffer.
Should be set in `~/.vip' file.")
;; overlay used in the minibuffer to indicate which state it is in
(vip-deflocalvar vip-minibuffer-overlay nil)
;; Hook, specific to Viper, which is run just *before* exiting the minibuffer.
;; Beginning with Emacs 19.26, the standard `minibuffer-exit-hook' is run
;; *after* exiting the minibuffer
(defvar vip-minibuffer-exit-hook nil)
(vip-deflocalvar vip-vi-minibuffer-minor-mode nil
"Minor mode that forces Vi-style when the Minibuffer is in Vi state.")
(vip-deflocalvar vip-insert-minibuffer-minor-mode nil
"Minor mode that forces Vi-style when the Minibuffer is in Insert state.")
;; setup emacs-supported vi-style feel
(setq next-line-add-newlines nil
require-final-newline t)
(make-variable-buffer-local 'require-final-newline)
;; Mode line
(defconst vip-vi-state-id "<V> "
"Mode line tag identifying the Vi mode of Viper.")
(defconst vip-emacs-state-id "<E> "
"Mode line tag identifying the Emacs mode of Viper.")
(defconst vip-insert-state-id "<I> "
"Mode line tag identifying the Insert mode of Viper.")
(defconst vip-replace-state-id "<R> "
"Mode line tag identifying the Replace mode of Viper.")
;; Viper changes the default mode-line-buffer-identification
(setq-default mode-line-buffer-identification '(" %b"))
;; Variable displaying the current Viper state in the mode line.
(vip-deflocalvar vip-mode-string vip-emacs-state-id)
(or (memq 'vip-mode-string global-mode-string)
(setq global-mode-string
(append '("" vip-mode-string) (cdr global-mode-string))))
(defvar vip-vi-state-hook nil
"*Hooks run just before the switch to Vi mode is completed.")
(defvar vip-insert-state-hook nil
"*Hooks run just before the switch to Insert mode is completed.")
(defvar vip-replace-state-hook nil
"*Hooks run just before the switch to Replace mode is completed.")
(defvar vip-emacs-state-hook nil
"*Hooks run just before the switch to Emacs mode is completed.")
(defvar vip-load-hook nil
"Hooks run just after loading Viper.")
;; Generic predicates
;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
;; generate test functions
;; given symbol foo, foo-p is the test function, foos is the set of
;; Viper command keys
;; (macroexpand '(vip-test-com-defun foo))
;; (defun foo-p (com) (consp (memq (if (< com 0) (- com) com) foos)))
(defmacro vip-test-com-defun (name)
(let* ((snm (symbol-name name))
(nm-p (intern (concat snm "-p")))
(nms (intern (concat snm "s"))))
(` (defun (, nm-p) (com)
(consp (memq (if (< com 0) (- com) com) (, nms)))))))
;; Variables for defining VI commands
;; Modifying commands that can be prefixes to movement commands
(defconst vip-prefix-commands '(?c ?d ?y ?! ?= ?# ?< ?> ?\"))
(vip-test-com-defun vip-prefix-command)
;; Commands that are pairs eg. dd. r and R here are a hack
(defconst vip-charpair-commands '(?c ?d ?y ?! ?= ?< ?> ?r ?R))
(vip-test-com-defun vip-charpair-command)
(defconst vip-movement-commands '(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
?H ?M ?n ?t ?T ?w ?W ?$ ?%
?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?`
?; ?, ?0 ?? ?/
)
"Movement commands")
(vip-test-com-defun vip-movement-command)
;; Commands that can be repeated by .(dotted)
(defconst vip-dotable-commands '(?c ?d ?C ?D ?> ?<))
(vip-test-com-defun vip-dotable-command)
;; Commands that can follow a #
(defconst vip-hash-cmds '(?c ?C ?g ?q ?S))
(vip-test-com-defun vip-hash-cmd)
;; Commands that may have registers as prefix
(defconst vip-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X))
(vip-test-com-defun vip-regsuffix-command)
;;; Arrange the keymaps
(require 'viper-keym)
;;;; CODE
;; changing mode
;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
(defun vip-change-state (new-state)
;; Keep vip-post/pre-command-hooks fresh.
;; We remove then add vip-post/pre-command-sentinel since it is very
;; desirable that noone gets in-between
(remove-hook 'post-command-hook 'vip-post-command-sentinel)
(add-hook 'post-command-hook 'vip-post-command-sentinel)
(remove-hook 'pre-command-hook 'vip-pre-command-sentinel)
(add-hook 'pre-command-hook 'vip-pre-command-sentinel t)
;; These hooks will be added back if switching to insert/replace mode
(vip-remove-hook 'vip-post-command-hooks
'vip-insert-state-post-command-sentinel)
(vip-remove-hook 'vip-pre-command-hooks
'vip-insert-state-pre-command-sentinel)
(cond ((eq new-state 'vi-state)
(cond ((member vip-current-state '(insert-state replace-state))
;; move vip-last-posn-while-in-insert-state
;; This is a normal hook that is executed in insert/replace
;; states after each command. In Vi/Emacs state, it does
;; nothing. We need to execute it here to make sure that
;; the last posn was recorded when we hit ESC.
;; It may be left unrecorded if the last thing done in
;; insert/repl state was dabbrev-expansion or abbrev
;; expansion caused by hitting ESC
(vip-insert-state-post-command-sentinel)
(condition-case conds
(progn
(vip-save-last-insertion
vip-insert-point
vip-last-posn-while-in-insert-state)
(if vip-began-as-replace
(setq vip-began-as-replace nil)
;; repeat insert commands if numerical arg > 1
(save-excursion
(vip-repeat-insert-command))))
(error
(vip-message-conditions conds)))
(if (> (length vip-last-insertion) 0)
(vip-push-onto-ring vip-last-insertion
'vip-insertion-ring))
(if vip-ex-style-editing-in-insert
(or (bolp) (backward-char 1))))
))
;; insert or replace
((memq new-state '(insert-state replace-state))
(if (memq vip-current-state '(emacs-state vi-state))
(vip-move-marker-locally 'vip-insert-point (point)))
(vip-move-marker-locally 'vip-last-posn-while-in-insert-state (point))
(vip-add-hook 'vip-post-command-hooks
'vip-insert-state-post-command-sentinel t)
(vip-add-hook 'vip-pre-command-hooks
'vip-insert-state-pre-command-sentinel t))
) ; outermost cond
;; Nothing needs to be done to switch to emacs mode! Just set some
;; variables, which is already done in vip-change-state-to-emacs!
(setq vip-current-state new-state)
(vip-normalize-minor-mode-map-alist)
(vip-adjust-keys-for new-state)
(vip-set-mode-vars-for new-state)
(vip-refresh-mode-line)
)
(defun vip-adjust-keys-for (state)
"Make necessary adjustments to keymaps before entering STATE."
(cond ((memq state '(insert-state replace-state))
(if vip-auto-indent
(progn
(define-key vip-insert-basic-map "\C-m" 'vip-autoindent)
(if vip-want-emacs-keys-in-insert
;; expert
(define-key vip-insert-basic-map "\C-j" nil)
;; novice
(define-key vip-insert-basic-map "\C-j" 'vip-autoindent))))
(setq vip-insert-diehard-minor-mode
(not vip-want-emacs-keys-in-insert))
(if vip-want-ctl-h-help
(progn
(define-key vip-insert-basic-map "\C-h" 'help-command)
(define-key vip-replace-map "\C-h" 'help-command))
(define-key vip-insert-basic-map
"\C-h" 'vip-del-backward-char-in-insert)
(define-key vip-replace-map
"\C-h" 'vip-del-backward-char-in-replace)))
(t
(setq vip-vi-diehard-minor-mode (not vip-want-emacs-keys-in-vi))
(if vip-want-ctl-h-help
(define-key vip-vi-basic-map "\C-h" 'help-command)
(define-key vip-vi-basic-map "\C-h" 'vip-backward-char)))
))
;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
;; This ensures that Viper bindings are in effect, regardless of which minor
;; modes were turned on by the user or by other packages.
(defun vip-normalize-minor-mode-map-alist ()
(setq minor-mode-map-alist
(vip-append-filter-alist
(list
(cons 'vip-vi-intercept-minor-mode vip-vi-intercept-map)
(cons 'vip-vi-minibuffer-minor-mode vip-minibuffer-map)
(cons 'vip-vi-local-user-minor-mode vip-vi-local-user-map)
(cons 'vip-vi-kbd-minor-mode vip-vi-kbd-map)
(cons 'vip-vi-global-user-minor-mode vip-vi-global-user-map)
(cons 'vip-vi-state-modifier-minor-mode
(if (keymapp
(cdr (assoc major-mode vip-vi-state-modifier-alist)))
(cdr (assoc major-mode vip-vi-state-modifier-alist))
vip-empty-keymap))
(cons 'vip-vi-diehard-minor-mode vip-vi-diehard-map)
(cons 'vip-vi-basic-minor-mode vip-vi-basic-map)
(cons 'vip-insert-intercept-minor-mode vip-insert-intercept-map)
(cons 'vip-replace-minor-mode vip-replace-map)
;; vip-insert-minibuffer-minor-mode must come after
;; vip-replace-minor-mode
(cons 'vip-insert-minibuffer-minor-mode
vip-minibuffer-map)
(cons 'vip-insert-local-user-minor-mode
vip-insert-local-user-map)
(cons 'vip-insert-kbd-minor-mode vip-insert-kbd-map)
(cons 'vip-insert-global-user-minor-mode
vip-insert-global-user-map)
(cons 'vip-insert-state-modifier-minor-mode
(if (keymapp
(cdr
(assoc major-mode vip-insert-state-modifier-alist)))
(cdr
(assoc major-mode vip-insert-state-modifier-alist))
vip-empty-keymap))
(cons 'vip-insert-diehard-minor-mode vip-insert-diehard-map)
(cons 'vip-insert-basic-minor-mode vip-insert-basic-map)
(cons 'vip-emacs-intercept-minor-mode
vip-emacs-intercept-map)
(cons 'vip-emacs-local-user-minor-mode
vip-emacs-local-user-map)
(cons 'vip-emacs-kbd-minor-mode vip-emacs-kbd-map)
(cons 'vip-emacs-global-user-minor-mode
vip-emacs-global-user-map)
(cons 'vip-emacs-state-modifier-minor-mode
(if (keymapp
(cdr
(assoc major-mode vip-emacs-state-modifier-alist)))
(cdr
(assoc major-mode vip-emacs-state-modifier-alist))
vip-empty-keymap))
)
minor-mode-map-alist)))
;; Viper mode-changing commands and utilities
;; Modifies mode-line-buffer-identification.
(defun vip-refresh-mode-line ()
(setq vip-mode-string
(cond ((eq vip-current-state 'emacs-state) vip-emacs-state-id)
((eq vip-current-state 'vi-state) vip-vi-state-id)
((eq vip-current-state 'replace-state) vip-replace-state-id)
((eq vip-current-state 'insert-state) vip-insert-state-id)))
;; Sets Viper mode string in global-mode-string
(force-mode-line-update))
;;;###autoload
(defun viper-mode ()
"Turn on Viper emulation of Vi."
(interactive)
(if (not noninteractive)
(progn
(if vip-first-time ; This check is important. Without it, startup and
(progn ; expert-level msgs mix up when viper-mode recurses
(setq vip-first-time nil)
(if (not vip-inhibit-startup-message)
(save-window-excursion
(setq vip-inhibit-startup-message t)
(delete-other-windows)
(switch-to-buffer "Viper Startup Message")
(erase-buffer)
(insert
(substitute-command-keys
"Viper Is a Package for Emacs Rebels.
It is also a VI Plan for Emacs Rescue and a venomous VI PERil.
Technically speaking, Viper is a Vi emulation package for GNU Emacs 19 and
XEmacs 19. It supports virtually all of Vi and Ex functionality, extending
and improving upon much of it.
1. Viper supports Vi at several levels. Level 1 is the closest to
Vi, level 5 provides the most flexibility to depart from many Vi
conventions.
You will be asked to specify your user level in a following screen.
If you select user level 1 then the keys ^X, ^C, ^Z, and ^G will
behave as in VI, to smooth transition to Viper for the beginners.
However, to use Emacs productively, you are advised to reach user
level 3 or higher.
If your user level is 2 or higher, ^X and ^C will invoke Emacs
functions,as usual in Emacs; ^Z will toggle vi/emacs modes, and
^G will be the usual Emacs's keyboard-quit (something like ^C in VI).
2. Vi exit functions (e.g., :wq, ZZ) work on INDIVIDUAL files -- they
do not cause Emacs to quit, except at user level 1 (a novice).
3. ^X^C EXITS EMACS.
4. Viper supports multiple undo: `u' will undo. Typing `.' will repeat
undo. Another `u' changes direction.
6. Emacs Meta functions are invoked by typing `_' or `\\ ESC'.
On a window system, the best way is to use the Meta-key.
7. Try \\[keyboard-quit] and \\[abort-recursive-edit] repeatedly,
if something funny happens. This would abort the current editing
command.
You can get more information on Viper by:
a. Typing `:help' in Vi state
b. Printing Viper manual, found in ./etc/viper.dvi
c. Printing ViperCard, the Quick Reference, found in ./etc/viperCard.dvi
This startup message appears whenever you load Viper, unless you type `y' now."
))
(goto-char (point-min))
(if (y-or-n-p "Inhibit Viper startup message? ")
(vip-save-setting
'vip-inhibit-startup-message
"Viper startup message inhibited"
vip-custom-file-name t))
(kill-buffer (current-buffer))))
(message " ")
(vip-set-expert-level 'dont-change-unless)))
(vip-change-state-to-vi))))
;;;###autoload
(defalias 'vip-mode 'viper-mode)
;; Switch from Insert state to Vi state.
(defun vip-exit-insert-state ()
(interactive)
(vip-change-state-to-vi))
(defun vip-set-mode-vars-for (state)
"Sets Viper minor mode variables to put Viper's state STATE in effect."
;; Emacs state
(setq vip-vi-minibuffer-minor-mode nil
vip-insert-minibuffer-minor-mode nil
vip-vi-intercept-minor-mode nil
vip-insert-intercept-minor-mode nil
vip-vi-local-user-minor-mode nil
vip-vi-kbd-minor-mode nil
vip-vi-global-user-minor-mode nil
vip-vi-state-modifier-minor-mode nil
vip-vi-diehard-minor-mode nil
vip-vi-basic-minor-mode nil
vip-replace-minor-mode nil
vip-insert-local-user-minor-mode nil
vip-insert-kbd-minor-mode nil
vip-insert-global-user-minor-mode nil
vip-insert-state-modifier-minor-mode nil
vip-insert-diehard-minor-mode nil
vip-insert-basic-minor-mode nil
vip-emacs-intercept-minor-mode t
vip-emacs-local-user-minor-mode t
vip-emacs-kbd-minor-mode (not (vip-is-in-minibuffer))
vip-emacs-global-user-minor-mode t
vip-emacs-state-modifier-minor-mode t
)
;; Vi state
(if (eq state 'vi-state) ; adjust for vi-state
(setq
vip-vi-intercept-minor-mode t
vip-vi-minibuffer-minor-mode (vip-is-in-minibuffer)
vip-vi-local-user-minor-mode t
vip-vi-kbd-minor-mode (not (vip-is-in-minibuffer))
vip-vi-global-user-minor-mode t
vip-vi-state-modifier-minor-mode t
;; don't let the diehard keymap block command completion
;; and other things in the minibuffer
vip-vi-diehard-minor-mode (not
(or vip-want-emacs-keys-in-vi
(vip-is-in-minibuffer)))
vip-vi-basic-minor-mode t
vip-emacs-intercept-minor-mode nil
vip-emacs-local-user-minor-mode nil
vip-emacs-kbd-minor-mode nil
vip-emacs-global-user-minor-mode nil
vip-emacs-state-modifier-minor-mode nil
))
;; Insert and Replace states
(if (member state '(insert-state replace-state))
(setq
vip-insert-intercept-minor-mode t
vip-replace-minor-mode (eq state 'replace-state)
vip-insert-minibuffer-minor-mode (vip-is-in-minibuffer)
vip-insert-local-user-minor-mode t
vip-insert-kbd-minor-mode (not (vip-is-in-minibuffer))
vip-insert-global-user-minor-mode t
vip-insert-state-modifier-minor-mode t
;; don't let the diehard keymap block command completion
;; and other things in the minibuffer
vip-insert-diehard-minor-mode (not
(or vip-want-emacs-keys-in-insert
(vip-is-in-minibuffer)))
vip-insert-basic-minor-mode t
vip-emacs-intercept-minor-mode nil
vip-emacs-local-user-minor-mode nil
vip-emacs-kbd-minor-mode nil
vip-emacs-global-user-minor-mode nil
vip-emacs-state-modifier-minor-mode nil
))
;; minibuffer faces
(if (vip-window-display-p)
(setq vip-minibuffer-current-face
(cond ((eq state 'emacs-state) vip-minibuffer-emacs-face)
((eq state 'vi-state) vip-minibuffer-vi-face)
((memq state '(insert-state replace-state))
vip-minibuffer-insert-face))))
(if (vip-is-in-minibuffer)
(vip-set-minibuffer-overlay))
)
;; This also takes care of the annoying incomplete lines in files.
;; Also, this fixes `undo' to work vi-style for complex commands.
(defun vip-change-state-to-vi ()
"Change Viper state to Vi."
(interactive)
(if (and vip-first-time (not (vip-is-in-minibuffer)))
(viper-mode)
(if overwrite-mode (overwrite-mode nil))
(if abbrev-mode (expand-abbrev))
(if (and auto-fill-function (> (current-column) fill-column))
(funcall auto-fill-function))
;; don't leave whitespace lines around
(if (and (memq last-command
'(vip-autoindent
vip-open-line vip-Open-line
vip-replace-state-exit-cmd))
(vip-over-whitespace-line))
(indent-to-left-margin))
(vip-add-newline-at-eob-if-necessary)
(if vip-undo-needs-adjustment (vip-adjust-undo))
(vip-change-state 'vi-state)
;; always turn off iso-accents-mode, or else we won't be able to use the
;; keys `,',^ in Vi state, as they will do accents instead of Vi actions.
(if (and (boundp 'iso-accents-mode) iso-accents-mode)
(iso-accents-mode -1))
;; Protection against user errors in hooks
(condition-case conds
(run-hooks 'vip-vi-state-hook)
(error
(vip-message-conditions conds)))))
(defun vip-change-state-to-insert ()
"Change Viper state to Insert."
(interactive)
(vip-change-state 'insert-state)
(if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
(iso-accents-mode 1)) ; turn iso accents on
;; Protection against user errors in hooks
(condition-case conds
(run-hooks 'vip-insert-state-hook)
(error
(vip-message-conditions conds))))
(defsubst vip-downgrade-to-insert ()
(setq vip-current-state 'insert-state
vip-replace-minor-mode nil)
)
;; Change to replace state. When the end of replacement region is reached,
;; replace state changes to insert state.
(defun vip-change-state-to-replace (&optional non-R-cmd)
(vip-change-state 'replace-state)
(if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
(iso-accents-mode 1)) ; turn iso accents on
;; Run insert-state-hook
(condition-case conds
(run-hooks 'vip-insert-state-hook 'vip-replace-state-hook)
(error
(vip-message-conditions conds)))
(if non-R-cmd
(vip-start-replace)
;; 'R' is implemented using Emacs's overwrite-mode
(vip-start-R-mode))
)
(defun vip-change-state-to-emacs ()
"Change Viper state to Emacs."
(interactive)
(vip-change-state 'emacs-state)
(if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
(iso-accents-mode 1)) ; turn iso accents on
;; Protection agains user errors in hooks
(condition-case conds
(run-hooks 'vip-emacs-state-hook)
(error
(vip-message-conditions conds))))
;; escape to emacs mode termporarily
(defun vip-escape-to-emacs (arg &optional events)
"Escape to Emacs state from Vi state for one Emacs command.
ARG is used as the prefix value for the executed command. If
EVENTS is a list of events, which become the beginning of the command."
(interactive "P")
(vip-escape-to-state arg events 'emacs-state))
;; escape to Vi mode termporarily
(defun vip-escape-to-vi ()
"Escape from Emacs state to Vi state for one Vi 1-character command.
This doesn't work with prefix arguments or most complex commands like
cw, dw, etc. But it does work with some 2-character commands,
like dd or dr."
(interactive)
(vip-escape-to-state nil nil 'vi-state))
;; Escape to STATE mode for one Emacs command.
(defun vip-escape-to-state (arg events state)
(let (com key prefix-arg)
;; this temporarily turns off Viper's minor mode keymaps
(vip-set-mode-vars-for state)
(vip-normalize-minor-mode-map-alist)
(if events (vip-set-unread-command-events events))
;; protect against keyboard quit and other errors
(condition-case nil
(progn
(unwind-protect
(progn
(setq com (key-binding (setq key
(if vip-xemacs-p
(read-key-sequence nil)
(read-key-sequence nil t)))))
;; In case of indirection--chase definitions.
;; Have to do it here because we execute this command under
;; different keymaps, so command-execute may not do the
;; right thing there
(while (vectorp com) (setq com (key-binding com))))
nil)
;; exec command in the right Viper state
;; otherwise, if we switch buffers in the escaped command,
;; Viper's mode vars will remain those of `state'. When we return
;; to the orig buffer, the bindings will be screwed up.
(vip-set-mode-vars-for vip-current-state)
;; this-command, last-command-char, last-command-event
(setq this-command com)
(if vip-xemacs-p ; XEmacs represents key sequences as vectors
(setq last-command-event (vip-seq-last-elt key)
last-command-char (event-to-character last-command-event))
;; Emacs represents them as sequences (str or vec)
(setq last-command-event (vip-seq-last-elt key)
last-command-char last-command-event))
(if (commandp com)
(progn
(setq prefix-arg arg)
(command-execute com)))
)
(quit (ding))
(error (beep 1))))
(vip-set-mode-vars-for vip-current-state)) ; set state in new buffer
(defun vip-exec-form-in-emacs (form)
"Execute FORM in Emacs, temporarily disabling Viper's minor modes.
Similar to vip-escape-to-emacs, but accepts forms rather than keystrokes."
(let ((buff (current-buffer))
result)
(vip-set-mode-vars-for 'emacs-state)
(setq result (eval form))
(if (not (equal buff (current-buffer))) ; cmd switched buffer
(save-excursion
(set-buffer buff)
(vip-set-mode-vars-for vip-current-state)))
(vip-set-mode-vars-for vip-current-state)
result))
;; This is needed because minor modes sometimes override essential Viper
;; bindings. By letting Viper know which files these modes are in, it will
;; arrange to reorganize minor-mode-map-alist so that things will work right.
(defun vip-harness-minor-mode (load-file)
"Familiarize Viper with a minor mode defined in LOAD_FILE.
Minor modes that have their own keymaps may overshadow Viper keymaps.
This function is designed to make Viper aware of the packages that define
such minor modes.
Usage:
(vip-harness-minor-mode load-file)
LOAD-FILE is a name of the file where the specific minor mode is defined.
Suffixes such as .el or .elc should be stripped."
(interactive "sEnter name of the load file: ")
(vip-eval-after-load load-file '(vip-normalize-minor-mode-map-alist))
;; Change the default for minor-mode-map-alist each time a harnessed minor
;; mode adds its own keymap to the a-list.
(vip-eval-after-load
load-file '(setq-default minor-mode-map-alist minor-mode-map-alist))
)
(defun vip-ESC (arg)
"Emulate ESC key in Emacs.
Prevents multiple escape keystrokes if vip-no-multiple-ESC is true. In that
case \@ will be bound to ESC. If vip-no-multiple-ESC is 'twice double ESC
would dings in vi-state. Other ESC sequences are emulated via the current
Emacs's major mode keymap. This is more convenient on dumb terminals and in
Emacs -nw, since this won't block functional keys such as up,down,
etc. Meta key also will work. When vip-no-multiple-ESC is nil, ESC key
behaves as in Emacs, any number of multiple escapes is allowed."
(interactive "P")
(let (char)
(cond ((and (not vip-no-multiple-ESC) (eq vip-current-state 'vi-state))
(setq char (vip-read-char-exclusive))
(vip-escape-to-emacs arg (list ?\e char) ))
((and (eq vip-no-multiple-ESC 'twice)
(eq vip-current-state 'vi-state))
(setq char (vip-read-char-exclusive))
(if (= char (string-to-char vip-ESC-key))
(ding)
(vip-escape-to-emacs arg (list ?\e char) )))
(t (ding)))
))
(defun vip-alternate-ESC (arg)
"ESC key without checking for multiple keystrokes."
(interactive "P")
(vip-escape-to-emacs arg '(?\e)))
;; Intercept ESC sequences on dumb terminals.
;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
;; Check if last key was ESC and if so try to reread it as a function key.
;; But only if there are characters to read during a very short time.
;; Returns the last event, if any.
(defun vip-envelop-ESC-key ()
(let ((event last-input-event)
(keyseq [nil])
inhibit-quit)
(if (vip-ESC-event-p event)
(progn
(if (vip-fast-keysequence-p)
(progn
(let (minor-mode-map-alist)
(vip-set-unread-command-events event)
(setq keyseq
(funcall
(ad-get-orig-definition 'read-key-sequence) nil))
) ; let
;; If keyseq translates into something that still has ESC
;; at the beginning, separate ESC from the rest of the seq.
;; In XEmacs we check for events that are keypress meta-key
;; and convert them into [escape key]
;;
;; This is needed for the following reason:
;; If ESC is the first symbol, we interpret it as if the
;; user typed ESC and then quickly some other symbols.
;; If ESC is not the first one, then the key sequence
;; entered was apparently translated into a function key or
;; something (e.g., one may have
;; (define-key function-key-map "\e[192z" [f11])
;; which would translate the escape-sequence generated by
;; f11 in an xterm window into the symbolic key f11.
;;
;; If `first-key' is not an ESC event, we make it into the
;; last-command-event in order to pretend that this key was
;; pressed. This is needed to allow arrow keys to be bound to
;; macros. Otherwise, vip-exec-mapped-kbd-macro will think that
;; the last event was ESC and so it'll execute whatever is
;; bound to ESC. (Viper macros can't be bound to
;; ESC-sequences).
(let* ((first-key (elt keyseq 0))
(key-mod (event-modifiers first-key)))
(cond ((vip-ESC-event-p first-key)
;; put keys following ESC on the unread list
;; and return ESC as the key-sequence
(vip-set-unread-command-events (subseq keyseq 1))
(setq last-input-event event
keyseq (if vip-emacs-p
"\e"
(vector (character-to-event ?\e)))))
((and vip-xemacs-p
(key-press-event-p first-key)
(equal '(meta) key-mod))
(vip-set-unread-command-events
(vconcat (vector
(character-to-event (event-key first-key)))
(subseq keyseq 1)))
(setq last-input-event event
keyseq (vector (character-to-event ?\e))))
((eventp first-key)
(setq last-command-event first-key))
))
) ; end progn
;; this is escape event with nothing after it
;; put in unread-command-event and then re-read
(vip-set-unread-command-events event)
(setq keyseq
(funcall (ad-get-orig-definition 'read-key-sequence) nil))
))
;; not an escape event
(setq keyseq (vector event)))
keyseq))
(defadvice read-key-sequence (around vip-read-keyseq-ad activate)
"Harness to work for Viper. This advice is harmless---don't worry!"
(let (inhibit-quit event keyseq)
(setq keyseq ad-do-it)
(setq event (if vip-xemacs-p
(elt keyseq 0) ; XEmacs returns vector of events
(elt (listify-key-sequence keyseq) 0)))
(if (vip-ESC-event-p event)
(let (unread-command-events)
(vip-set-unread-command-events keyseq)
(if (vip-fast-keysequence-p)
(let ((vip-vi-global-user-minor-mode nil)
(vip-vi-local-user-minor-mode nil)
(vip-replace-minor-mode nil) ; actually unnecessary
(vip-insert-global-user-minor-mode nil)
(vip-insert-local-user-minor-mode nil))
(setq keyseq ad-do-it))
(setq keyseq ad-do-it))))
keyseq))
(defadvice describe-key (before vip-read-keyseq-ad protect activate)
"Force to read key via `read-key-sequence'."
(interactive (list (vip-events-to-keys
(read-key-sequence "Describe key: ")))))
(defadvice describe-key-briefly (before vip-read-keyseq-ad protect activate)
"Force to read key via `read-key-sequence'."
(interactive (list (vip-events-to-keys
(read-key-sequence "Describe key briefly: ")))))
;; Listen to ESC key.
;; If a sequence of keys starting with ESC is issued with very short delays,
;; interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key.
(defun vip-intercept-ESC-key ()
"Function that implements ESC key in Viper emulation of Vi."
(interactive)
(let ((cmd (or (key-binding (vip-envelop-ESC-key))
'(lambda () (interactive) (error "")))))
;; call the actual function to execute ESC (if no other symbols followed)
;; or the key bound to the ESC sequence (if the sequence was issued
;; with very short delay between characters.
(if (eq cmd 'vip-intercept-ESC-key)
(setq cmd
(cond ((eq vip-current-state 'vi-state)
'vip-ESC)
((eq vip-current-state 'insert-state)
'vip-exit-insert-state)
((eq vip-current-state 'replace-state)
'vip-replace-state-exit-cmd)
(t 'vip-change-state-to-vi)
)))
(call-interactively cmd)))
;; prefix argument for Vi mode
;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
;; represents the numeric value of the prefix argument and COM represents
;; command prefix such as "c", "d", "m" and "y".
;; Compute numeric prefix arg value.
;; Invoked by CHAR. COM is the command part obtained so far.
(defun vip-prefix-arg-value (event com)
(let (value)
;; read while number
(while (and (numberp event) (>= event ?0) (<= event ?9))
(setq value (+ (* (if (numberp value) value 0) 10) (- event ?0)))
(setq event (vip-read-event-convert-to-char)))
(setq prefix-arg value)
(if com (setq prefix-arg (cons prefix-arg com)))
(while (eq event ?U)
(vip-describe-arg prefix-arg)
(setq event (vip-read-event-convert-to-char)))
(vip-set-unread-command-events event)))
;; Vi operator as prefix argument."
(defun vip-prefix-arg-com (char value com)
(let ((cont t))
(while (and cont
(memq char
(list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
vip-buffer-search-char)))
(if com
;; this means that we already have a command character, so we
;; construct a com list and exit while. however, if char is "
;; it is an error.
(progn
;; new com is (CHAR . OLDCOM)
(if (memq char '(?# ?\")) (error ""))
(setq com (cons char com))
(setq cont nil))
;; If com is nil we set com as char, and read more. Again, if char
;; is ", we read the name of register and store it in vip-use-register.
;; if char is !, =, or #, a complete com is formed so we exit the
;; while loop.
(cond ((memq char '(?! ?=))
(setq com char)
(setq char (read-char))
(setq cont nil))
((= char ?#)
;; read a char and encode it as com
(setq com (+ 128 (read-char)))
(setq char (read-char)))
((= char ?\")
(let ((reg (read-char)))
(if (vip-valid-register reg)
(setq vip-use-register reg)
(error ""))
(setq char (read-char))))
(t
(setq com char)
(setq char (vip-read-char-exclusive)))))))
(if (atom com)
;; com is a single char, so we construct prefix-arg
;; and if char is ?, describe prefix arg, otherwise exit by
;; pushing the char back
(progn
(setq prefix-arg (cons value com))
(while (= char ?U)
(vip-describe-arg prefix-arg)
(setq char (read-char)))
(vip-set-unread-command-events char)
)
;; as com is non-nil, this means that we have a command to execute
(if (memq (car com) '(?r ?R))
;; execute apropriate region command.
(let ((char (car com)) (com (cdr com)))
(setq prefix-arg (cons value com))
(if (= char ?r) (vip-region prefix-arg)
(vip-Region prefix-arg))
;; reset prefix-arg
(setq prefix-arg nil))
;; otherwise, reset prefix arg and call appropriate command
(setq value (if (null value) 1 value))
(setq prefix-arg nil)
(cond ((equal com '(?c . ?c)) (vip-line (cons value ?C)))
((equal com '(?d . ?d)) (vip-line (cons value ?D)))
((equal com '(?d . ?y)) (vip-yank-defun))
((equal com '(?y . ?y)) (vip-line (cons value ?Y)))
((equal com '(?< . ?<)) (vip-line (cons value ?<)))
((equal com '(?> . ?>)) (vip-line (cons value ?>)))
((equal com '(?! . ?!)) (vip-line (cons value ?!)))
((equal com '(?= . ?=)) (vip-line (cons value ?=)))
(t (error ""))))))
(defun vip-describe-arg (arg)
(let (val com)
(setq val (vip-P-val arg)
com (vip-getcom arg))
(if (null val)
(if (null com)
(message "Value is nil, and command is nil")
(message "Value is nil, and command is `%c'" com))
(if (null com)
(message "Value is `%d', and command is nil" val)
(message "Value is `%d', and command is `%c'" val com)))))
(defun vip-digit-argument (arg)
"Begin numeric argument for the next command."
(interactive "P")
(vip-leave-region-active)
(vip-prefix-arg-value last-command-char
(if (consp arg) (cdr arg) nil)))
(defun vip-command-argument (arg)
"Accept a motion command as an argument."
(interactive "P")
(condition-case nil
(vip-prefix-arg-com
last-command-char
(cond ((null arg) nil)
((consp arg) (car arg))
((numberp arg) arg)
(t (error vip-InvalidCommandArgument)))
(cond ((null arg) nil)
((consp arg) (cdr arg))
((numberp arg) nil)
(t (error vip-InvalidCommandArgument))))
(quit (setq vip-use-register nil)
(signal 'quit nil)))
(vip-deactivate-mark))
;; Get value part of prefix-argument ARG.
(defsubst vip-p-val (arg)
(cond ((null arg) 1)
((consp arg)
(if (or (null (car arg)) (equal (car arg) '(nil)))
1 (car arg)))
(t arg)))
;; Get raw value part of prefix-argument ARG.
(defsubst vip-P-val (arg)
(cond ((consp arg) (car arg))
(t arg)))
;; Get com part of prefix-argument ARG.
(defsubst vip-getcom (arg)
(cond ((null arg) nil)
((consp arg) (cdr arg))
(t nil)))
;; Get com part of prefix-argument ARG and modify it.
(defun vip-getCom (arg)
(let ((com (vip-getcom arg)))
(cond ((equal com ?c) ?C)
((equal com ?d) ?D)
((equal com ?y) ?Y)
(t com))))
;; repeat last destructive command
;; Append region to text in register REG.
;; START and END are buffer positions indicating what to append.
(defsubst vip-append-to-register (reg start end)
(set-register reg (concat (if (stringp (get-register reg))
(get-register reg) "")
(buffer-substring start end))))
;; define functions to be executed
;; invoked by C command
(defun vip-exec-change (m-com com)
;; handle C cmd at the eol and at eob.
(if (or (and (eolp) (= vip-com-point (point)))
(= vip-com-point (point-max)))
(progn
(insert " ")(backward-char 1)))
(if (= vip-com-point (point))
(vip-forward-char-carefully))
(if (= com ?c)
(vip-change vip-com-point (point))
(vip-change-subr vip-com-point (point))))
;; this is invoked by vip-substitute-line
(defun vip-exec-Change (m-com com)
(save-excursion
(set-mark vip-com-point)
(vip-enlarge-region (mark t) (point))
(if vip-use-register
(progn
(cond ((vip-valid-register vip-use-register '(letter digit))
;;(vip-valid-register vip-use-register '(letter)
(copy-to-register
vip-use-register (mark t) (point) nil))
((vip-valid-register vip-use-register '(Letter))
(vip-append-to-register
(downcase vip-use-register) (mark t) (point)))
(t (setq vip-use-register nil)
(error vip-InvalidRegister vip-use-register)))
(setq vip-use-register nil)))
(delete-region (mark t) (point)))
(open-line 1)
(if (= com ?C) (vip-change-mode-to-insert) (vip-yank-last-insertion)))
(defun vip-exec-delete (m-com com)
(if vip-use-register
(progn
(cond ((vip-valid-register vip-use-register '(letter digit))
;;(vip-valid-register vip-use-register '(letter))
(copy-to-register
vip-use-register vip-com-point (point) nil))
((vip-valid-register vip-use-register '(Letter))
(vip-append-to-register
(downcase vip-use-register) vip-com-point (point)))
(t (setq vip-use-register nil)
(error vip-InvalidRegister vip-use-register)))
(setq vip-use-register nil)))
(setq last-command
(if (eq last-command 'd-command) 'kill-region nil))
(kill-region vip-com-point (point))
(setq this-command 'd-command)
(if vip-ex-style-motion
(if (and (eolp) (not (bolp))) (backward-char 1))))
(defun vip-exec-Delete (m-com com)
(save-excursion
(set-mark vip-com-point)
(vip-enlarge-region (mark t) (point))
(if vip-use-register
(progn
(cond ((vip-valid-register vip-use-register '(letter digit))
;;(vip-valid-register vip-use-register '(letter))
(copy-to-register
vip-use-register (mark t) (point) nil))
((vip-valid-register vip-use-register '(Letter))
(vip-append-to-register
(downcase vip-use-register) (mark t) (point)))
(t (setq vip-use-register nil)
(error vip-InvalidRegister vip-use-register)))
(setq vip-use-register nil)))
(setq last-command
(if (eq last-command 'D-command) 'kill-region nil))
(kill-region (mark t) (point))
(if (eq m-com 'vip-line) (setq this-command 'D-command)))
(back-to-indentation))
(defun vip-exec-yank (m-com com)
(if vip-use-register
(progn
(cond ((vip-valid-register vip-use-register '(letter digit))
;; (vip-valid-register vip-use-register '(letter))
(copy-to-register
vip-use-register vip-com-point (point) nil))
((vip-valid-register vip-use-register '(Letter))
(vip-append-to-register
(downcase vip-use-register) vip-com-point (point)))
(t (setq vip-use-register nil)
(error vip-InvalidRegister vip-use-register)))
(setq vip-use-register nil)))
(setq last-command nil)
(copy-region-as-kill vip-com-point (point))
(goto-char vip-com-point))
(defun vip-exec-Yank (m-com com)
(save-excursion
(set-mark vip-com-point)
(vip-enlarge-region (mark t) (point))
(if vip-use-register
(progn
(cond ((vip-valid-register vip-use-register '(letter digit))
(copy-to-register
vip-use-register (mark t) (point) nil))
((vip-valid-register vip-use-register '(Letter))
(vip-append-to-register
(downcase vip-use-register) (mark t) (point)))
(t (setq vip-use-register nil)
(error vip-InvalidRegister vip-use-register)))
(setq vip-use-register nil)))
(setq last-command nil)
(copy-region-as-kill (mark t) (point)))
(vip-deactivate-mark)
(goto-char vip-com-point))
(defun vip-exec-bang (m-com com)
(save-excursion
(set-mark vip-com-point)
(vip-enlarge-region (mark t) (point))
(shell-command-on-region
(mark t) (point)
(if (= com ?!)
(setq vip-last-shell-com
(vip-read-string-with-history
"!"
nil
'vip-shell-history
(car vip-shell-history)
))
vip-last-shell-com)
t)))
(defun vip-exec-equals (m-com com)
(save-excursion
(set-mark vip-com-point)
(vip-enlarge-region (mark t) (point))
(if (> (mark t) (point)) (exchange-point-and-mark))
(indent-region (mark t) (point) nil)))
(defun vip-exec-shift (m-com com)
(save-excursion
(set-mark vip-com-point)
(vip-enlarge-region (mark t) (point))
(if (> (mark t) (point)) (exchange-point-and-mark))
(indent-rigidly (mark t) (point)
(if (= com ?>)
vip-shift-width
(- vip-shift-width))))
;; return point to where it was before shift
(goto-char vip-com-point))
;; this is needed because some commands fake com by setting it to ?r, which
;; denotes repeated insert command.
(defsubst vip-exec-dummy (m-com com)
nil)
(defun vip-exec-buffer-search (m-com com)
(setq vip-s-string (buffer-substring (point) vip-com-point))
(setq vip-s-forward t)
(setq vip-search-history (cons vip-s-string vip-search-history))
(vip-search vip-s-string vip-s-forward 1))
(defvar vip-exec-array (make-vector 128 nil))
;; Using a dispatch array allows adding functions like buffer search
;; without affecting other functions. Buffer search can now be bound
;; to any character.
(aset vip-exec-array ?c 'vip-exec-change)
(aset vip-exec-array ?C 'vip-exec-Change)
(aset vip-exec-array ?d 'vip-exec-delete)
(aset vip-exec-array ?D 'vip-exec-Delete)
(aset vip-exec-array ?y 'vip-exec-yank)
(aset vip-exec-array ?Y 'vip-exec-Yank)
(aset vip-exec-array ?r 'vip-exec-dummy)
(aset vip-exec-array ?! 'vip-exec-bang)
(aset vip-exec-array ?< 'vip-exec-shift)
(aset vip-exec-array ?> 'vip-exec-shift)
(aset vip-exec-array ?= 'vip-exec-equals)
;; This function is called by various movement commands to execute a
;; destructive command on the region specified by the movement command. For
;; instance, if the user types cw, then the command vip-forward-word will
;; call vip-execute-com to execute vip-exec-change, which eventually will
;; call vip-change to invoke the replace mode on the region.
;;
;; The list (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS) is set to
;; vip-d-com for later use by vip-repeat.
(defun vip-execute-com (m-com val com)
(let ((reg vip-use-register))
;; this is the special command `#'
(if (> com 128)
(vip-special-prefix-com (- com 128))
(let ((fn (aref vip-exec-array (if (< com 0) (- com) com))))
(if (null fn)
(error "%c: %s" com vip-InvalidViCommand)
(funcall fn m-com com))))
(if (vip-dotable-command-p com)
(vip-set-destructive-command
(list m-com val
(if (memq com (list ?c ?C ?!)) (- com) com)
reg nil nil)))
))
(defun vip-repeat (arg)
"Re-execute last destructive command.
Use the info in vip-d-com, which has the form
\(com val ch reg inserted-text command-keys\),
where `com' is the command to be re-executed, `val' is the
argument to `com', `ch' is a flag for repeat, and `reg' is optional;
if it exists, it is the name of the register for `com'.
If the prefix argument, ARG, is non-nil, it is used instead of `val'."
(interactive "P")
(let ((save-point (point)) ; save point before repeating prev cmd
;; Pass along that we are repeating a destructive command
;; This tells vip-set-destructive-command not to update
;; vip-command-ring
(vip-intermediate-command 'vip-repeat))
(if (eq last-command 'vip-undo)
;; if the last command was vip-undo, then undo-more
(vip-undo-more)
;; otherwise execute the command stored in vip-d-com. if arg is non-nil
;; its prefix value is used as new prefix value for the command.
(let ((m-com (car vip-d-com))
(val (vip-P-val arg))
(com (nth 2 vip-d-com))
(reg (nth 3 vip-d-com)))
(if (null val) (setq val (nth 1 vip-d-com)))
(if (null m-com) (error "No previous command to repeat."))
(setq vip-use-register reg)
(if (nth 4 vip-d-com) ; text inserted by command
(setq vip-last-insertion (nth 4 vip-d-com)
vip-d-char (nth 4 vip-d-com)))
(funcall m-com (cons val com))
(if (and vip-keep-point-on-repeat (< save-point (point)))
(goto-char save-point)) ; go back to before repeat.
(if (and (eolp) (not (bolp)))
(backward-char 1))
))
(if vip-undo-needs-adjustment (vip-adjust-undo)) ; take care of undo
;; If the prev cmd was rotating the command ring, this means that `.' has
;; just executed a command from that ring. So, push it on the ring again.
;; If we are just executing previous command , then don't push vip-d-com
;; because vip-d-com is not fully constructed in this case (its keys and
;; the inserted text may be nil). Besides, in this case, the command
;; executed by `.' is already on the ring.
(if (eq last-command 'vip-display-current-destructive-command)
(vip-push-onto-ring vip-d-com 'vip-command-ring))
(vip-deactivate-mark)
))
(defun vip-repeat-from-history ()
"Repeat a destructive command from history.
Doesn't change vip-command-ring in any way, so `.' will work as before
executing this command.
This command is supposed to be bound to a two-character Vi macro where
the second character is a digit 0 to 9. The digit indicates which
history command to execute. `<char>0' is equivalent to `.', `<char>1'
invokes the command before that, etc."
(interactive)
(let* ((vip-intermediate-command 'repeating-display-destructive-command)
(idx (cond (vip-this-kbd-macro
(string-to-number
(symbol-name (elt vip-this-kbd-macro 1))))
(t 0)))
(num idx)
(vip-d-com vip-d-com))
(or (and (numberp num) (<= 0 num) (<= num 9))
(setq idx 0
num 0)
(message
"`vip-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'"))
(while (< 0 num)
(setq vip-d-com (vip-special-ring-rotate1 vip-command-ring -1))
(setq num (1- num)))
(vip-repeat nil)
(while (> idx num)
(vip-special-ring-rotate1 vip-command-ring 1)
(setq num (1+ num)))
))
;; This command is invoked interactively by the key sequence #<char>
(defun vip-special-prefix-com (char)
(cond ((= char ?c)
(downcase-region (min vip-com-point (point))
(max vip-com-point (point))))
((= char ?C)
(upcase-region (min vip-com-point (point))
(max vip-com-point (point))))
((= char ?g)
(push-mark vip-com-point t)
(vip-global-execute))
((= char ?q)
(push-mark vip-com-point t)
(vip-quote-region))
((= char ?s) (funcall vip-spell-function vip-com-point (point)))
(t (error "#%c: %s" char vip-InvalidViCommand))))
;; undoing
(defun vip-undo ()
"Undo previous change."
(interactive)
(message "undo!")
(let ((modified (buffer-modified-p))
(before-undo-pt (point-marker))
(after-change-functions after-change-functions)
undo-beg-posn undo-end-posn)
;; no need to remove this hook, since this var has scope inside a let.
(add-hook 'after-change-functions
'(lambda (beg end len)
(setq undo-beg-posn beg
undo-end-posn (or end beg))))
(undo-start)
(undo-more 2)
(setq undo-beg-posn (or undo-beg-posn before-undo-pt)
undo-end-posn (or undo-end-posn undo-beg-posn))
(goto-char undo-beg-posn)
(sit-for 0)
(if (and vip-keep-point-on-undo
(pos-visible-in-window-p before-undo-pt))
(progn
(push-mark (point-marker) t)
(vip-sit-for-short 300)
(goto-char undo-end-posn)
(vip-sit-for-short 300)
(if (and (> (abs (- undo-beg-posn before-undo-pt)) 1)
(> (abs (- undo-end-posn before-undo-pt)) 1))
(goto-char before-undo-pt)
(goto-char undo-beg-posn)))
(push-mark before-undo-pt t))
(if (and (eolp) (not (bolp))) (backward-char 1))
(if (not modified) (set-buffer-modified-p t)))
(setq this-command 'vip-undo))
;; Continue undoing previous changes.
(defun vip-undo-more ()
(message "undo more!")
(condition-case nil
(undo-more 1)
(error (beep)
(message "No further undo information in this buffer")))
(if (and (eolp) (not (bolp))) (backward-char 1))
(setq this-command 'vip-undo))
;; The following two functions are used to set up undo properly.
;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
;; they are undone all at once.
(defun vip-adjust-undo ()
(let ((inhibit-quit t)
tmp tmp2)
(setq vip-undo-needs-adjustment nil)
(if (listp buffer-undo-list)
(if (setq tmp (memq vip-buffer-undo-list-mark buffer-undo-list))
(progn
(setq tmp2 (cdr tmp)) ; the part after mark
;; cut tail from buffer-undo-list temporarily by direct
;; manipulation with pointers in buffer-undo-list
(setcdr tmp nil)
(setq buffer-undo-list (delq nil buffer-undo-list))
(setq buffer-undo-list
(delq vip-buffer-undo-list-mark buffer-undo-list))
;; restore tail of buffer-undo-list
(setq buffer-undo-list (nconc buffer-undo-list tmp2)))
(setq buffer-undo-list (delq nil buffer-undo-list))))))
(defun vip-set-complex-command-for-undo ()
(if (listp buffer-undo-list)
(if (not vip-undo-needs-adjustment)
(let ((inhibit-quit t))
(setq buffer-undo-list
(cons vip-buffer-undo-list-mark buffer-undo-list))
(setq vip-undo-needs-adjustment t)))))
(defun vip-display-current-destructive-command ()
(let ((text (nth 4 vip-d-com))
(keys (nth 5 vip-d-com))
(max-text-len 30))
(setq this-command 'vip-display-current-destructive-command)
(message " `.' runs %s%s"
(concat "`" (vip-array-to-string keys) "'")
(vip-abbreviate-string text max-text-len
" inserting `" "'" " ......."))
))
;; don't change vip-d-com if it was vip-repeat command invoked with `.'
;; or in some other way (non-interactively).
(defun vip-set-destructive-command (list)
(or (eq vip-intermediate-command 'vip-repeat)
(progn
(setq vip-d-com list)
(setcar (nthcdr 5 vip-d-com)
(vip-array-to-string (this-command-keys)))
(vip-push-onto-ring vip-d-com 'vip-command-ring))))
(defun vip-prev-destructive-command (next)
"Find previous destructive command in the history of destructive commands.
With prefix argument, find next destructive command."
(interactive "P")
(let (cmd vip-intermediate-command)
(if (eq last-command 'vip-display-current-destructive-command)
;; repeated search through command history
(setq vip-intermediate-command 'repeating-display-destructive-command)
;; first search through command history--set temp ring
(setq vip-temp-command-ring (copy-list vip-command-ring)))
(setq cmd (if next
(vip-special-ring-rotate1 vip-temp-command-ring 1)
(vip-special-ring-rotate1 vip-temp-command-ring -1)))
(if (null cmd)
()
(setq vip-d-com cmd))
(vip-display-current-destructive-command)))
(defun vip-next-destructive-command ()
"Find next destructive command in the history of destructive commands."
(interactive)
(vip-prev-destructive-command 'next))
(defun vip-insert-prev-from-insertion-ring (arg)
"Cycle through insertion ring in the direction of older insertions.
Undoes previous insertion and inserts new.
With prefix argument, cycles in the direction of newer elements.
In minibuffer, this command executes whatever the invocation key is bound
to in the global map, instead of cycling through the insertion ring."
(interactive "P")
(let (vip-intermediate-command)
(if (eq last-command 'vip-insert-from-insertion-ring)
(progn ; repeated search through insertion history
(setq vip-intermediate-command 'repeating-insertion-from-ring)
(if (eq vip-current-state 'replace-state)
(undo 1)
(if vip-last-inserted-string-from-insertion-ring
(backward-delete-char
(length vip-last-inserted-string-from-insertion-ring))))
)
;;first search through insertion history
(setq vip-temp-insertion-ring (copy-list vip-insertion-ring)))
(setq this-command 'vip-insert-from-insertion-ring)
;; so that things will be undone properly
(setq buffer-undo-list (cons nil buffer-undo-list))
(setq vip-last-inserted-string-from-insertion-ring
(vip-special-ring-rotate1 vip-temp-insertion-ring (if arg 1 -1)))
;; this change of vip-intermediate-command must come after
;; vip-special-ring-rotate1, so that the ring will rotate, but before the
;; insertion.
(setq vip-intermediate-command nil)
(if vip-last-inserted-string-from-insertion-ring
(insert vip-last-inserted-string-from-insertion-ring))
))
(defun vip-insert-next-from-insertion-ring ()
"Cycle through insertion ring in the direction of older insertions.
Undo previous insertion and inserts new."
(interactive)
(vip-insert-prev-from-insertion-ring 'next))
;; some region utilities
;; If at the last line of buffer, add \\n before eob, if newline is missing.
(defun vip-add-newline-at-eob-if-necessary ()
(save-excursion
(end-of-line)
;; make sure all lines end with newline, unless in the minibuffer or
;; when requested otherwise (require-final-newline is nil)
(if (and
(eobp)
(not (bolp))
require-final-newline
(not (vip-is-in-minibuffer)))
(insert "\n"))))
(defun vip-yank-defun ()
(mark-defun)
(copy-region-as-kill (point) (mark t)))
;; Enlarge region between BEG and END.
(defun vip-enlarge-region (beg end)
(or beg (setq beg end)) ; if beg is nil, set to end
(or end (setq end beg)) ; if end is nil, set to beg
(if (< beg end)
(progn (goto-char beg) (set-mark end))
(goto-char end)
(set-mark beg))
(beginning-of-line)
(exchange-point-and-mark)
(if (or (not (eobp)) (not (bolp))) (forward-line 1))
(if (not (eobp)) (beginning-of-line))
(if (> beg end) (exchange-point-and-mark)))
;; Quote region by each line with a user supplied string.
(defun vip-quote-region ()
(setq vip-quote-string
(vip-read-string-with-history
"Quote string: "
nil
'vip-quote-region-history
vip-quote-string))
(vip-enlarge-region (point) (mark t))
(if (> (point) (mark t)) (exchange-point-and-mark))
(insert vip-quote-string)
(beginning-of-line)
(forward-line 1)
(while (and (< (point) (mark t)) (bolp))
(insert vip-quote-string)
(beginning-of-line)
(forward-line 1)))
;; Tells whether BEG is on the same line as END.
;; If one of the args is nil, it'll return nil.
(defun vip-same-line (beg end)
(let ((selective-display nil))
(cond ((and beg end)
;; This 'if' is needed because Emacs treats the next empty line
;; as part of the previous line.
(if (or (> beg (point-max)) (> end (point-max))) ; out of range
()
(if (and (> end beg) (= (vip-line-pos 'start) end))
(setq end (min (1+ end) (point-max))))
(if (and (> beg end) (= (vip-line-pos 'start) beg))
(setq beg (min (1+ beg) (point-max))))
(<= (count-lines beg end) 1) ))
(t nil))
))
;; Check if the string ends with a newline.
(defun vip-end-with-a-newline-p (string)
(or (string= string "")
(= (vip-seq-last-elt string) ?\n)))
(defun vip-tmp-insert-at-eob (msg)
(let ((savemax (point-max)))
(goto-char savemax)
(insert msg)
(sit-for 2)
(goto-char savemax) (delete-region (point) (point-max))
))
;;; Minibuffer business
(defsubst vip-set-minibuffer-style ()
(add-hook 'minibuffer-setup-hook 'vip-minibuffer-setup-sentinel))
(defun vip-minibuffer-setup-sentinel ()
(let ((hook (if vip-vi-style-in-minibuffer
'vip-change-state-to-insert
'vip-change-state-to-emacs)))
(funcall hook)
;; Make sure the minibufer overlay is kept up-to-date. In XEmacs also
;; guards against the possibility of detaching this overlay.
(vip-add-hook 'vip-post-command-hooks 'vip-move-minibuffer-overlay)
))
;; Interpret last event in the local map
(defun vip-exit-minibuffer ()
(interactive)
(let (command)
(setq command (local-key-binding (char-to-string last-command-char)))
(if command
(command-execute command)
(exit-minibuffer))))
(defun vip-set-search-face ()
(if (not (vip-window-display-p))
()
(defvar vip-search-face
(progn
(add-to-list 'facemenu-unlisted-faces 'vip-search-face)
(make-face 'vip-search-face)
(or (face-differs-from-default-p 'vip-search-face)
;; face wasn't set in .vip or .Xdefaults
(if (vip-can-use-colors "Black" "khaki")
(progn
(set-face-background 'vip-search-face "khaki")
(set-face-foreground 'vip-search-face "Black"))
(copy-face 'italic 'vip-search-face)
(set-face-underline-p 'vip-search-face t)))
'vip-search-face)
"*Face used to flash out the search pattern.")
))
(defun vip-set-minibuffer-faces ()
(if (not (vip-window-display-p))
()
(defvar vip-minibuffer-emacs-face
(progn
(add-to-list 'facemenu-unlisted-faces 'vip-minibuffer-emacs-face)
(make-face 'vip-minibuffer-emacs-face)
(or (face-differs-from-default-p 'vip-minibuffer-emacs-face)
;; face wasn't set in .vip or .Xdefaults
(if vip-vi-style-in-minibuffer
;; emacs state is an exception in the minibuffer
(if (vip-can-use-colors "darkseagreen2" "Black")
(progn
(set-face-background
'vip-minibuffer-emacs-face "darkseagreen2")
(set-face-foreground
'vip-minibuffer-emacs-face "Black"))
(copy-face 'highlight 'vip-minibuffer-emacs-face))
;; emacs state is the main state in the minibuffer
(if (vip-can-use-colors "Black" "pink")
(progn
(set-face-background 'vip-minibuffer-emacs-face "pink")
(set-face-foreground
'vip-minibuffer-emacs-face "Black"))
(copy-face 'italic 'vip-minibuffer-emacs-face))
))
'vip-minibuffer-emacs-face)
"Face used in the Minibuffer when it is in Emacs state.")
(defvar vip-minibuffer-insert-face
(progn
(add-to-list 'facemenu-unlisted-faces 'vip-minibuffer-insert-face)
(make-face 'vip-minibuffer-insert-face)
(or (face-differs-from-default-p 'vip-minibuffer-insert-face)
(if vip-vi-style-in-minibuffer
(if (vip-can-use-colors "Black" "pink")
(progn
(set-face-background 'vip-minibuffer-insert-face "pink")
(set-face-foreground
'vip-minibuffer-insert-face "Black"))
(copy-face 'italic 'vip-minibuffer-insert-face))
;; If Insert state is an exception
(if (vip-can-use-colors "darkseagreen2" "Black")
(progn
(set-face-background
'vip-minibuffer-insert-face "darkseagreen2")
(set-face-foreground
'vip-minibuffer-insert-face "Black"))
(copy-face 'highlight 'vip-minibuffer-insert-face))
(vip-italicize-face 'vip-minibuffer-insert-face)))
'vip-minibuffer-insert-face)
"Face used in the Minibuffer when it is in Insert state.")
(defvar vip-minibuffer-vi-face
(progn
(add-to-list 'facemenu-unlisted-faces 'vip-minibuffer-vi-face)
(make-face 'vip-minibuffer-vi-face)
(or (face-differs-from-default-p 'vip-minibuffer-vi-face)
(if vip-vi-style-in-minibuffer
(if (vip-can-use-colors "Black" "grey")
(progn
(set-face-background 'vip-minibuffer-vi-face "grey")
(set-face-foreground 'vip-minibuffer-vi-face "Black"))
(copy-face 'bold 'vip-minibuffer-vi-face))
(copy-face 'bold 'vip-minibuffer-vi-face)
(invert-face 'vip-minibuffer-vi-face)))
'vip-minibuffer-vi-face)
"Face used in the Minibuffer when it is in Vi state.")
;; the current face used in the minibuffer
(vip-deflocalvar vip-minibuffer-current-face vip-minibuffer-emacs-face "")
))
;;; Reading string with history
(defun vip-read-string-with-history (prompt &optional initial
history-var default keymap)
;; Reads string, prompting with PROMPT and inserting the INITIAL
;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
;; input is an empty string. Uses KEYMAP, if given, or the
;; minibuffer-local-map.
;; Default value is displayed until the user types something in the
;; minibuffer.
(let ((minibuffer-setup-hook
'(lambda ()
(if (stringp initial)
(progn
;; don't wait if we have unread events or in kbd macro
(or unread-command-events
executing-kbd-macro
(sit-for 840))
(erase-buffer)
(insert initial)))
(vip-minibuffer-setup-sentinel)))
(val "")
(padding "")
temp-msg)
(setq keymap (or keymap minibuffer-local-map)
initial (or initial "")
temp-msg (if default
(format "(default: %s) " default)
""))
(setq vip-incomplete-ex-cmd nil)
(setq val (read-from-minibuffer prompt
(concat temp-msg initial val padding)
keymap nil history-var))
(setq minibuffer-setup-hook nil
padding (vip-array-to-string (this-command-keys))
temp-msg "")
;; the following tries to be smart about what to put in history
(if (not (string= val (car (eval history-var))))
(set history-var (cons val (eval history-var))))
(if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
(string= (nth 0 (eval history-var)) ""))
(set history-var (cdr (eval history-var))))
;; if the user enters nothing but the prev cmd wasn't vip-ex or
;; vip-command-argument, this means that the user typed something then
;; erased. Return "" in this case, not the default---default is too
;; confusing in this case
(cond ((and (string= val "")
(not (memq last-command
(list 'vip-ex 'vip-command-argument t))))
"")
((string= val "") (or default ""))
(t val))
))
;; insertion commands
;; Called when state changes from Insert Vi command mode.
;; Repeats the insertion command if Insert state was entered with prefix
;; argument > 1.
(defun vip-repeat-insert-command ()
(let ((i-com (car vip-d-com))
(val (nth 1 vip-d-com))
(char (nth 2 vip-d-com)))
(if (and val (> val 1)) ; first check that val is non-nil
(progn
(setq vip-d-com (list i-com (1- val) ?r nil nil nil))
(vip-repeat nil)
(setq vip-d-com (list i-com val char nil nil nil))
))))
(defun vip-insert (arg)
"Insert before point."
(interactive "P")
(vip-set-complex-command-for-undo)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(vip-set-destructive-command (list 'vip-insert val ?r nil nil nil))
(if com
(vip-loop val (vip-yank-last-insertion))
(vip-change-state-to-insert))))
(defun vip-append (arg)
"Append after point."
(interactive "P")
(vip-set-complex-command-for-undo)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(vip-set-destructive-command (list 'vip-append val ?r nil nil nil))
(if (not (eolp)) (forward-char))
(if (equal com ?r)
(vip-loop val (vip-yank-last-insertion))
(vip-change-state-to-insert))))
(defun vip-Append (arg)
"Append at end of line."
(interactive "P")
(vip-set-complex-command-for-undo)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(vip-set-destructive-command (list 'vip-Append val ?r nil nil nil))
(end-of-line)
(if (equal com ?r)
(vip-loop val (vip-yank-last-insertion))
(vip-change-state-to-insert))))
(defun vip-Insert (arg)
"Insert before first non-white."
(interactive "P")
(vip-set-complex-command-for-undo)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(vip-set-destructive-command (list 'vip-Insert val ?r nil nil nil))
(back-to-indentation)
(if (equal com ?r)
(vip-loop val (vip-yank-last-insertion))
(vip-change-state-to-insert))))
(defun vip-open-line (arg)
"Open line below."
(interactive "P")
(vip-set-complex-command-for-undo)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(vip-set-destructive-command (list 'vip-open-line val ?r nil nil nil))
(let ((col (current-indentation)))
(if (equal com ?r)
(vip-loop val
(progn
(end-of-line)
(newline 1)
(if vip-auto-indent
(progn (setq vip-cted t) (indent-to col)))
(vip-yank-last-insertion)))
(end-of-line)
(newline 1)
(if vip-auto-indent (progn (setq vip-cted t) (indent-to col)))
(vip-change-state-to-insert)
))))
(defun vip-Open-line (arg)
"Open line above."
(interactive "P")
(vip-set-complex-command-for-undo)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(vip-set-destructive-command (list 'vip-Open-line val ?r nil nil nil))
(let ((col (current-indentation)))
(if (equal com ?r)
(vip-loop val
(progn
(beginning-of-line)
(open-line 1)
(if vip-auto-indent
(progn (setq vip-cted t) (indent-to col)))
(vip-yank-last-insertion)))
(beginning-of-line)
(open-line 1)
(if vip-auto-indent (progn (setq vip-cted t) (indent-to col)))
(vip-change-state-to-insert)))))
(defun vip-open-line-at-point (arg)
"Open line at point."
(interactive "P")
(vip-set-complex-command-for-undo)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(vip-set-destructive-command
(list 'vip-open-line-at-point val ?r nil nil nil))
(if (equal com ?r)
(vip-loop val
(progn
(open-line 1)
(vip-yank-last-insertion)))
(open-line 1)
(vip-change-state-to-insert))))
(defun vip-substitute (arg)
"Substitute characters."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(push-mark nil t)
(forward-char val)
(if (equal com ?r)
(vip-change-subr (mark t) (point))
(vip-change (mark t) (point)))
(vip-set-destructive-command (list 'vip-substitute val ?r nil nil nil))
))
(defun vip-substitute-line (arg)
"Substitute lines."
(interactive "p")
(vip-set-complex-command-for-undo)
(vip-line (cons arg ?C)))
;; Prepare for replace
(defun vip-start-replace ()
(setq vip-began-as-replace t
vip-sitting-in-replace t
vip-replace-chars-to-delete 0
vip-replace-chars-deleted 0)
(vip-add-hook 'vip-after-change-functions 'vip-replace-mode-spy-after t)
(vip-add-hook 'vip-before-change-functions 'vip-replace-mode-spy-before t)
;; this will get added repeatedly, but no harm
(add-hook 'after-change-functions 'vip-after-change-sentinel t)
(add-hook 'before-change-functions 'vip-before-change-sentinel t)
(vip-move-marker-locally 'vip-last-posn-in-replace-region
(vip-replace-start))
(vip-add-hook
'vip-post-command-hooks 'vip-replace-state-post-command-sentinel t)
(vip-add-hook
'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
)
;; Runs vip-after-change-functions inside after-change-functions
(defun vip-after-change-sentinel (beg end len)
(let ((list vip-after-change-functions))
(while list
(funcall (car list) beg end len)
(setq list (cdr list)))))
;; Runs vip-before-change-functions inside before-change-functions
(defun vip-before-change-sentinel (beg end)
(let ((list vip-before-change-functions))
(while list
(funcall (car list) beg end)
(setq list (cdr list)))))
(defsubst vip-post-command-sentinel ()
(run-hooks 'vip-post-command-hooks))
(defsubst vip-pre-command-sentinel ()
(run-hooks 'vip-pre-command-hooks))
;; Needed so that Viper will be able to figure the last inserted
;; chunk of text with reasonable accuracy.
(defsubst vip-insert-state-post-command-sentinel ()
(if (and (memq vip-current-state '(insert-state replace-state))
vip-insert-point
(>= (point) vip-insert-point))
(setq vip-last-posn-while-in-insert-state (point-marker)))
(if (and (eq this-command 'dabbrev-expand)
(integerp vip-pre-command-point)
(> vip-insert-point vip-pre-command-point))
(move-marker vip-insert-point vip-pre-command-point))
)
(defsubst vip-insert-state-pre-command-sentinel ()
(if (and (eq this-command 'dabbrev-expand)
(markerp vip-insert-point)
(marker-position vip-insert-point))
(setq vip-pre-command-point (marker-position vip-insert-point))))
(defsubst vip-R-state-post-command-sentinel ()
;; Restoring cursor color is needed despite
;; vip-replace-state-pre-command-sentinel: When you jump to another buffer in
;; another frame, the pre-command hook won't change cursor color to default
;; in that other frame. So, if the second frame cursor was red and we set
;; the point outside the replacement region, then the cursor color will
;; remain red. Restoring the default, below, prevents this.
(if (and (<= (vip-replace-start) (point))
(<= (point) (vip-replace-end)))
(vip-change-cursor-color vip-replace-overlay-cursor-color)
(vip-restore-cursor-color)
))
;; to speed up, don't change cursor color before self-insert
;; and common move commands
(defsubst vip-replace-state-pre-command-sentinel ()
(or (memq this-command '(self-insert-command))
(memq (vip-event-key last-command-event)
'(up down left right (meta f) (meta b)
(control n) (control p) (control f) (control b)))
(vip-restore-cursor-color)))
(defun vip-replace-state-post-command-sentinel ()
;; Restoring cursor color is needed despite
;; vip-replace-state-pre-command-sentinel: When one jumps to another buffer
;; in another frame, the pre-command hook won't change cursor color to
;; default in that other frame. So, if the second frame cursor was red and
;; we set the point outside the replacement region, then the cursor color
;; will remain red. Restoring the default, below, fixes this problem.
;;
;; We optimize for self-insert-command's here, since they either don't change
;; cursor color or, if they terminate replace mode, the color will be changed
;; in vip-finish-change
(or (memq this-command '(self-insert-command))
(vip-restore-cursor-color))
(cond
((eq vip-current-state 'replace-state)
;; delete characters to compensate for inserted chars.
(let ((replace-boundary (vip-replace-end)))
(save-excursion
(goto-char vip-last-posn-in-replace-region)
(delete-char vip-replace-chars-to-delete)
(setq vip-replace-chars-to-delete 0
vip-replace-chars-deleted 0)
;; terminate replace mode if reached replace limit
(if (= vip-last-posn-in-replace-region
(vip-replace-end))
(vip-finish-change vip-last-posn-in-replace-region)))
(if (and (<= (vip-replace-start) (point))
(<= (point) replace-boundary))
(progn
;; the state may have changed in vip-finish-change above
(if (eq vip-current-state 'replace-state)
(vip-change-cursor-color vip-replace-overlay-cursor-color))
(setq vip-last-posn-in-replace-region (point-marker))))
))
(t ;; terminate replace mode if changed Viper states.
(vip-finish-change vip-last-posn-in-replace-region))))
;; checks how many chars were deleted by the last change
(defun vip-replace-mode-spy-before (beg end)
(setq vip-replace-chars-deleted
(- end beg
(max 0 (- end (vip-replace-end)))
(max 0 (- (vip-replace-start) beg))
)))
;; Invoked as an after-change-function to set up parameters of the last change
(defun vip-replace-mode-spy-after (beg end length)
(if (memq vip-intermediate-command '(repeating-insertion-from-ring))
(progn
(setq vip-replace-chars-to-delete 0)
(vip-move-marker-locally
'vip-last-posn-in-replace-region (point)))
(let (beg-col end-col real-end chars-to-delete)
(setq real-end (min end (vip-replace-end)))
(save-excursion
(goto-char beg)
(setq beg-col (current-column))
(goto-char real-end)
(setq end-col (current-column)))
;; If beg of change is outside the replacement region, then don't
;; delete anything in the repl region (set chars-to-delete to 0).
;;
;; This works fine except that we have to take special care of
;; dabbrev-expand. The problem stems from new-dabbrev.el, which
;; sometimes simply shifts the repl region rightwards, without
;; deleting an equal amount of characters.
;;
;; The reason why new-dabbrev.el causes this are this:
;; if one dinamically completes a partial word that starts before the
;; replacement region (but ends inside)then new-dabbrev.el first
;; moves cursor backwards, to the beginning of the word to be
;; completed (say, pt A). Then it inserts the
;; completed word and then deletes the old, incomplete part.
;; Since the complete word is inserted at position before the repl
;; region, the next If-statement would have set chars-to-delete to 0
;; unless we check for the current command, which must be
;; dabbrev-expand.
;;
;; We should be able deal with these problems in a better way
;; when emacs will have overlays with sticky back ends.
;; In fact, it would be also useful to add overlays for insert
;; regions as well, since this will let us capture the situation when
;; dabbrev-expand goes back past the insertion point to find the
;; beginning of the word to be expanded.
(if (or (and (<= (vip-replace-start) beg)
(<= beg (vip-replace-end)))
(and (= length 0) (eq this-command 'dabbrev-expand)))
(setq chars-to-delete
(max (- end-col beg-col) (- real-end beg) 0))
(setq chars-to-delete 0))
;; if beg = last change position, it means that we are within the
;; same command that does multiple changes. Moreover, it means
;; that we have two subsequent changes (insert/delete) that
;; complement each other.
(if (= beg (marker-position vip-last-posn-in-replace-region))
(setq vip-replace-chars-to-delete
(- (+ chars-to-delete vip-replace-chars-to-delete)
vip-replace-chars-deleted))
(setq vip-replace-chars-to-delete chars-to-delete))
(vip-move-marker-locally
'vip-last-posn-in-replace-region
(max (if (> end (vip-replace-end)) (vip-replace-start) end)
(or (marker-position vip-last-posn-in-replace-region)
(vip-replace-start))
))
(setq vip-replace-chars-to-delete
(max 0 (min vip-replace-chars-to-delete
(- (vip-replace-end)
vip-last-posn-in-replace-region))))
)))
;; Delete stuff between posn and the end of vip-replace-overlay-marker, if
;; posn is within the overlay.
(defun vip-finish-change (posn)
(vip-remove-hook 'vip-after-change-functions 'vip-replace-mode-spy-after)
(vip-remove-hook 'vip-before-change-functions 'vip-replace-mode-spy-before)
(vip-remove-hook 'vip-post-command-hooks
'vip-replace-state-post-command-sentinel)
(vip-remove-hook
'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
(vip-restore-cursor-color)
(setq vip-sitting-in-replace nil) ; just in case we'll need to know it
(save-excursion
(if (and
vip-replace-overlay
(>= posn (vip-replace-start))
(< posn (vip-replace-end)))
(delete-region posn (vip-replace-end)))
)
(if (eq vip-current-state 'replace-state)
(vip-downgrade-to-insert))
;; replace mode ended => nullify vip-last-posn-in-replace-region
(vip-move-marker-locally 'vip-last-posn-in-replace-region nil)
(vip-hide-replace-overlay)
(vip-refresh-mode-line)
(vip-put-string-on-kill-ring vip-last-replace-region)
)
;; Make STRING be the first element of the kill ring.
(defun vip-put-string-on-kill-ring (string)
(setq kill-ring (cons string kill-ring))
(if (> (length kill-ring) kill-ring-max)
(setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
(setq kill-ring-yank-pointer kill-ring))
(defun vip-finish-R-mode ()
(vip-remove-hook 'vip-post-command-hooks 'vip-R-state-post-command-sentinel)
(vip-remove-hook
'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
(vip-downgrade-to-insert))
(defun vip-start-R-mode ()
;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
(overwrite-mode 1)
(vip-add-hook
'vip-post-command-hooks 'vip-R-state-post-command-sentinel t)
(vip-add-hook
'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
)
(defun vip-replace-state-exit-cmd ()
"Binding for keys that cause Replace state to switch to Vi or to Insert.
These keys are ESC, RET, and LineFeed"
(interactive)
(if overwrite-mode ;; If you are in replace mode invoked via 'R'
(vip-finish-R-mode)
(vip-finish-change vip-last-posn-in-replace-region))
(let (com)
(if (eq this-command 'vip-intercept-ESC-key)
(setq com 'vip-exit-insert-state)
(vip-set-unread-command-events last-input-char)
(setq com (key-binding (read-key-sequence nil))))
(condition-case conds
(command-execute com)
(error
(vip-message-conditions conds)))
)
(vip-hide-replace-overlay))
;; This is the function bound to 'R'---unlimited replace.
;; Similar to Emacs's own overwrite-mode.
(defun vip-overwrite (arg)
"Begin overwrite mode."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getcom arg)) (len))
(vip-set-destructive-command (list 'vip-overwrite val ?r nil nil nil))
(if com
(progn
;; Viper saves inserted text in vip-last-insertion
(setq len (length vip-last-insertion))
(delete-char len)
(vip-loop val (vip-yank-last-insertion)))
(setq last-command 'vip-overwrite)
(vip-set-complex-command-for-undo)
(vip-set-replace-overlay (point) (vip-line-pos 'end))
(vip-change-state-to-replace)
)))
;; line commands
(defun vip-line (arg)
(let ((val (car arg))
(com (cdr arg)))
(vip-move-marker-locally 'vip-com-point (point))
(if (not (eobp))
(vip-next-line-carefully (1- val)))
;; this ensures that dd, cc, D, yy will do the right thing on the last
;; line of buffer when this line has no \n.
(vip-add-newline-at-eob-if-necessary)
(vip-execute-com 'vip-line val com))
(if (and (eobp) (not (bobp))) (forward-line -1))
)
(defun vip-yank-line (arg)
"Yank ARG lines (in Vi's sense)."
(interactive "P")
(let ((val (vip-p-val arg)))
(vip-line (cons val ?Y))))
;; region commands
(defun vip-region (arg)
"Execute command on a region."
(interactive "P")
(let ((val (vip-P-val arg))
(com (vip-getcom arg)))
(vip-move-marker-locally 'vip-com-point (point))
(exchange-point-and-mark)
(vip-execute-com 'vip-region val com)))
(defun vip-Region (arg)
"Execute command on a Region."
(interactive "P")
(let ((val (vip-P-val arg))
(com (vip-getCom arg)))
(vip-move-marker-locally 'vip-com-point (point))
(exchange-point-and-mark)
(vip-execute-com 'vip-Region val com)))
(defun vip-replace-char (arg)
"Replace the following ARG chars by the character read."
(interactive "P")
(if (and (eolp) (bolp)) (error "No character to replace here"))
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(vip-replace-char-subr com val)
(if (and (eolp) (not (bolp))) (forward-char 1))
(vip-set-destructive-command
(list 'vip-replace-char val ?r nil vip-d-char nil))
))
(defun vip-replace-char-subr (com arg)
(let ((take-care-of-iso-accents
(and (boundp 'iso-accents-mode) vip-automatic-iso-accents))
char)
(setq char (if (equal com ?r)
vip-d-char
(read-char)))
(if (and take-care-of-iso-accents (memq char '(?' ?\" ?^ ?~)))
;; get European characters
(progn
(iso-accents-mode 1)
(vip-set-unread-command-events char)
(setq char (aref (read-key-sequence nil) 0))
(iso-accents-mode -1)))
(delete-char arg t)
(setq vip-d-char char)
(vip-loop (if (> arg 0) arg (- arg))
(if (eq char ?\C-m) (insert "\n") (insert char)))
(backward-char arg)))
;; basic cursor movement. j, k, l, h commands.
(defun vip-forward-char (arg)
"Move point right ARG characters (left if ARG negative).
On reaching end of line, stop and signal error."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(if vip-ex-style-motion
(progn
;; the boundary condition check gets weird here because
;; forward-char may be the parameter of a delete, and 'dl' works
;; just like 'x' for the last char on a line, so we have to allow
;; the forward motion before the 'vip-execute-com', but, of
;; course, 'dl' doesn't work on an empty line, so we have to
;; catch that condition before 'vip-execute-com'
(if (and (eolp) (bolp)) (error "") (forward-char val))
(if com (vip-execute-com 'vip-forward-char val com))
(if (eolp) (progn (backward-char 1) (error ""))))
(forward-char val)
(if com (vip-execute-com 'vip-forward-char val com)))))
(defun vip-backward-char (arg)
"Move point left ARG characters (right if ARG negative).
On reaching beginning of line, stop and signal error."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(if vip-ex-style-motion
(progn
(if (bolp) (error "") (backward-char val))
(if com (vip-execute-com 'vip-backward-char val com)))
(backward-char val)
(if com (vip-execute-com 'vip-backward-char val com)))))
;; Like forward-char, but doesn't move at end of buffer.
(defun vip-forward-char-carefully (&optional arg)
(setq arg (or arg 1))
(if (>= (point-max) (+ (point) arg))
(forward-char arg)
(goto-char (point-max))))
;; Like backward-char, but doesn't move at end of buffer.
(defun vip-backward-char-carefully (&optional arg)
(setq arg (or arg 1))
(if (<= (point-min) (- (point) arg))
(backward-char arg)
(goto-char (point-min))))
(defun vip-next-line-carefully (arg)
(condition-case nil
(next-line arg)
(error nil)))
;;; Word command
;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators
;; for word movement. When executed with a destructive command, \n is
;; usually left untouched for the last word.
;; Viper uses syntax table to determine what is a word and what is a
;; separator. However, \n is always a separator. Also, if vip-syntax-preference
;; is 'vi, then `_' is part of the word.
;; skip only one \n
(defun vip-skip-separators (forward)
(if forward
(progn
(vip-skip-all-separators-forward 'within-line)
(if (looking-at "\n")
(progn
(forward-char)
(vip-skip-all-separators-forward 'within-line))))
(vip-skip-all-separators-backward 'within-line)
(backward-char)
(if (looking-at "\n")
(vip-skip-all-separators-backward 'within-line)
(forward-char))))
(defun vip-forward-word-kernel (val)
(while (> val 0)
(cond ((vip-looking-at-alpha)
(vip-skip-alpha-forward "_")
(vip-skip-separators t))
((vip-looking-at-separator)
(vip-skip-separators t))
((not (vip-looking-at-alphasep))
(vip-skip-nonalphasep-forward)
(vip-skip-separators t)))
(setq val (1- val))))
;; first search backward for pat. Then skip chars backwards using aux-pat
(defun vip-fwd-skip (pat aux-pat lim)
(if (and (save-excursion
(re-search-backward pat lim t))
(= (point) (match-end 0)))
(goto-char (match-beginning 0)))
(skip-chars-backward aux-pat lim)
(if (= (point) lim)
(vip-forward-char-carefully))
)
(defun vip-forward-word (arg)
"Forward word."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(vip-forward-word-kernel val)
(if com (progn
(cond ((memq com (list ?c (- ?c)))
(vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
;; Yank words including the whitespace, but not newline
((memq com (list ?y (- ?y)))
(vip-fwd-skip "\n[ \t]*" "" vip-com-point))
((vip-dotable-command-p com)
(vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
(vip-execute-com 'vip-forward-word val com)))))
(defun vip-forward-Word (arg)
"Forward word delimited by white characters."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(vip-loop val
(progn
(vip-skip-nonseparators 'forward)
(vip-skip-separators t)))
(if com (progn
(cond ((memq com (list ?c (- ?c)))
(vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
;; Yank words including the whitespace, but not newline
((memq com (list ?y (- ?y)))
(vip-fwd-skip "\n[ \t]*" "" vip-com-point))
((vip-dotable-command-p com)
(vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
(vip-execute-com 'vip-forward-Word val com)))))
;; this is a bit different from Vi, but Vi's end of word
;; makes no sense whatsoever
(defun vip-end-of-word-kernel ()
(if (vip-end-of-word-p) (forward-char))
(if (vip-looking-at-separator)
(vip-skip-all-separators-forward))
(cond ((vip-looking-at-alpha) (vip-skip-alpha-forward "_"))
((not (vip-looking-at-alphasep)) (vip-skip-nonalphasep-forward)))
(vip-backward-char-carefully))
(defun vip-end-of-word-p ()
(or (eobp)
(save-excursion
(cond ((vip-looking-at-alpha)
(forward-char)
(not (vip-looking-at-alpha)))
((not (vip-looking-at-alphasep))
(forward-char)
(vip-looking-at-alphasep))))))
(defun vip-end-of-word (arg &optional careful)
"Move point to end of current word."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(vip-loop val (vip-end-of-word-kernel))
(if com
(progn
(forward-char)
(vip-execute-com 'vip-end-of-word val com)))))
(defun vip-end-of-Word (arg)
"Forward to end of word delimited by white character."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(vip-loop val
(progn
(vip-end-of-word-kernel)
(vip-skip-nonseparators 'forward)
(backward-char)))
(if com
(progn
(forward-char)
(vip-execute-com 'vip-end-of-Word val com)))))
(defun vip-backward-word-kernel (val)
(while (> val 0)
(backward-char)
(cond ((vip-looking-at-alpha)
(vip-skip-alpha-backward "_"))
((vip-looking-at-separator)
(forward-char)
(vip-skip-separators nil)
(backward-char)
(cond ((vip-looking-at-alpha)
(vip-skip-alpha-backward "_"))
((not (vip-looking-at-alphasep))
(vip-skip-nonalphasep-backward))
(t (forward-char))))
((not (vip-looking-at-alphasep))
(vip-skip-nonalphasep-backward)))
(setq val (1- val))))
(defun vip-backward-word (arg)
"Backward word."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if com
(let (i)
(if (setq i (save-excursion (backward-char) (looking-at "\n")))
(backward-char))
(vip-move-marker-locally 'vip-com-point (point))
(if i (forward-char))))
(vip-backward-word-kernel val)
(if com (vip-execute-com 'vip-backward-word val com))))
(defun vip-backward-Word (arg)
"Backward word delimited by white character."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if com
(let (i)
(if (setq i (save-excursion (backward-char) (looking-at "\n")))
(backward-char))
(vip-move-marker-locally 'vip-com-point (point))
(if i (forward-char))))
(vip-loop val
(progn
(vip-skip-separators nil)
(vip-skip-nonseparators 'backward)))
(if com (vip-execute-com 'vip-backward-Word val com))))
;; line commands
(defun vip-beginning-of-line (arg)
"Go to beginning of line."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(beginning-of-line val)
(if com (vip-execute-com 'vip-beginning-of-line val com))))
(defun vip-bol-and-skip-white (arg)
"Beginning of line at first non-white character."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(forward-to-indentation (1- val))
(if com (vip-execute-com 'vip-bol-and-skip-white val com))))
(defun vip-goto-eol (arg)
"Go to end of line."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(end-of-line val)
(if com (vip-execute-com 'vip-goto-eol val com))
(if vip-ex-style-motion
(if (and (eolp) (not (bolp))
;; a fix for vip-change-to-eol
(not (equal vip-current-state 'insert-state)))
(backward-char 1)
))))
(defun vip-goto-col (arg)
"Go to ARG's column."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(save-excursion
(end-of-line)
(if (> val (1+ (current-column))) (error "")))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(beginning-of-line)
(forward-char (1- val))
(if com (vip-execute-com 'vip-goto-col val com))))
(defun vip-next-line (arg)
"Go to next line."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getCom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(next-line val)
(if vip-ex-style-motion
(if (and (eolp) (not (bolp))) (backward-char 1)))
(setq this-command 'next-line)
(if com (vip-execute-com 'vip-next-line val com))))
(defun vip-next-line-at-bol (arg)
"Next line at beginning of line."
(interactive "P")
(vip-leave-region-active)
(save-excursion
(end-of-line)
(if (eobp) (error "Last line in buffer")))
(let ((val (vip-p-val arg))
(com (vip-getCom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(forward-line val)
(back-to-indentation)
(if com (vip-execute-com 'vip-next-line-at-bol val com))))
(defun vip-previous-line (arg)
"Go to previous line."
(interactive "P")
(vip-leave-region-active)
(let ((val (vip-p-val arg))
(com (vip-getCom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(previous-line val)
(if vip-ex-style-motion
(if (and (eolp) (not (bolp))) (backward-char 1)))
(setq this-command 'previous-line)
(if com (vip-execute-com 'vip-previous-line val com))))
(defun vip-previous-line-at-bol (arg)
"Previous line at beginning of line."
(interactive "P")
(vip-leave-region-active)
(save-excursion
(beginning-of-line)
(if (bobp) (error "First line in buffer")))
(let ((val (vip-p-val arg))
(com (vip-getCom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(forward-line (- val))
(back-to-indentation)
(if com (vip-execute-com 'vip-previous-line val com))))
(defun vip-change-to-eol (arg)
"Change to end of line."
(interactive "P")
(vip-goto-eol (cons arg ?c)))
(defun vip-kill-line (arg)
"Delete line."
(interactive "P")
(vip-goto-eol (cons arg ?d)))
(defun vip-erase-line (arg)
"Erase line."
(interactive "P")
(vip-beginning-of-line (cons arg ?d)))
;;; Moving around
(defun vip-goto-line (arg)
"Go to ARG's line. Without ARG go to end of buffer."
(interactive "P")
(let ((val (vip-P-val arg))
(com (vip-getCom arg)))
(vip-move-marker-locally 'vip-com-point (point))
(vip-deactivate-mark)
(push-mark nil t)
(if (null val)
(goto-char (point-max))
(goto-char (point-min))
(forward-line (1- val)))
;; positioning is done twice: before and after command execution
(if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
(back-to-indentation)
(if com (vip-execute-com 'vip-goto-line val com))
(if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
(back-to-indentation)
))
;; Find ARG's occurrence of CHAR on the current line.
;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
;; adjust point after search.
(defun vip-find-char (arg char forward offset)
(or (char-or-string-p char) (error ""))
(let ((arg (if forward arg (- arg)))
(cmd (if (eq vip-intermediate-command 'vip-repeat)
(nth 5 vip-d-com)
(vip-array-to-string (this-command-keys))))
point)
(save-excursion
(save-restriction
(if (> arg 0)
(narrow-to-region
;; forward search begins here
(if (eolp) (error "Command `%s': At end of line" cmd) (point))
;; forward search ends here
(progn (end-of-line) (point)))
(narrow-to-region
;; backward search begins from here
(if (bolp)
(error "Command `%s': At beginning of line" cmd) (point))
;; backward search ends here
(progn (beginning-of-line) (point))))
;; if arg > 0, point is forwarded before search.
(if (> arg 0) (goto-char (1+ (point-min)))
(goto-char (point-max)))
(if (let ((case-fold-search nil))
(search-forward (char-to-string char) nil 0 arg))
(setq point (point))
(error "Command `%s': `%c' not found" cmd char))))
(goto-char (+ point (if (> arg 0) (if offset -2 -1) (if offset 1 0))))))
(defun vip-find-char-forward (arg)
"Find char on the line.
If called interactively read the char to find from the terminal, and if
called from vip-repeat, the char last used is used. This behaviour is
controlled by the sign of prefix numeric value."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if (> val 0)
;; this means that the function was called interactively
(setq vip-f-char (read-char)
vip-f-forward t
vip-f-offset nil)
;; vip-repeat --- set vip-F-char from command-keys
(setq vip-F-char (if (stringp (nth 5 vip-d-com))
(vip-seq-last-elt (nth 5 vip-d-com))
vip-F-char)
vip-f-char vip-F-char)
(setq val (- val)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t nil)
(setq val (- val))
(if com
(progn
(setq vip-F-char vip-f-char) ; set new vip-F-char
(forward-char)
(vip-execute-com 'vip-find-char-forward val com)))))
(defun vip-goto-char-forward (arg)
"Go up to char ARG forward on line."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if (> val 0)
;; this means that the function was called interactively
(setq vip-f-char (read-char)
vip-f-forward t
vip-f-offset t)
;; vip-repeat --- set vip-F-char from command-keys
(setq vip-F-char (if (stringp (nth 5 vip-d-com))
(vip-seq-last-elt (nth 5 vip-d-com))
vip-F-char)
vip-f-char vip-F-char)
(setq val (- val)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t t)
(setq val (- val))
(if com
(progn
(setq vip-F-char vip-f-char) ; set new vip-F-char
(forward-char)
(vip-execute-com 'vip-goto-char-forward val com)))))
(defun vip-find-char-backward (arg)
"Find char ARG on line backward."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if (> val 0)
;; this means that the function was called interactively
(setq vip-f-char (read-char)
vip-f-forward nil
vip-f-offset nil)
;; vip-repeat --- set vip-F-char from command-keys
(setq vip-F-char (if (stringp (nth 5 vip-d-com))
(vip-seq-last-elt (nth 5 vip-d-com))
vip-F-char)
vip-f-char vip-F-char)
(setq val (- val)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(vip-find-char
val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil nil)
(setq val (- val))
(if com
(progn
(setq vip-F-char vip-f-char) ; set new vip-F-char
(vip-execute-com 'vip-find-char-backward val com)))))
(defun vip-goto-char-backward (arg)
"Go up to char ARG backward on line."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if (> val 0)
;; this means that the function was called interactively
(setq vip-f-char (read-char)
vip-f-forward nil
vip-f-offset t)
;; vip-repeat --- set vip-F-char from command-keys
(setq vip-F-char (if (stringp (nth 5 vip-d-com))
(vip-seq-last-elt (nth 5 vip-d-com))
vip-F-char)
vip-f-char vip-F-char)
(setq val (- val)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil t)
(setq val (- val))
(if com
(progn
(setq vip-F-char vip-f-char) ; set new vip-F-char
(vip-execute-com 'vip-goto-char-backward val com)))))
(defun vip-repeat-find (arg)
"Repeat previous find command."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(vip-deactivate-mark)
(if com (vip-move-marker-locally 'vip-com-point (point)))
(vip-find-char val vip-f-char vip-f-forward vip-f-offset)
(if com
(progn
(if vip-f-forward (forward-char))
(vip-execute-com 'vip-repeat-find val com)))))
(defun vip-repeat-find-opposite (arg)
"Repeat previous find command in the opposite direction."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(vip-deactivate-mark)
(if com (vip-move-marker-locally 'vip-com-point (point)))
(vip-find-char val vip-f-char (not vip-f-forward) vip-f-offset)
(if com
(progn
(if vip-f-forward (forward-char))
(vip-execute-com 'vip-repeat-find-opposite val com)))))
;; window scrolling etc.
(defun vip-other-window (arg)
"Switch to other window."
(interactive "p")
(other-window arg)
(or (not (eq vip-current-state 'emacs-state))
(string= (buffer-name (current-buffer)) " *Minibuf-1*")
(vip-change-state-to-vi)))
(defun vip-window-top (arg)
"Go to home window line."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getCom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(push-mark nil t)
(move-to-window-line (1- val))
;; positioning is done twice: before and after command execution
(if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
(back-to-indentation)
(if com (vip-execute-com 'vip-window-top val com))
(if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
(back-to-indentation)
))
(defun vip-window-middle (arg)
"Go to middle window line."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getCom arg))
lines)
(if com (vip-move-marker-locally 'vip-com-point (point)))
(push-mark nil t)
(if (not (pos-visible-in-window-p (point-max)))
(move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
(setq lines (count-lines (window-start) (point-max)))
(move-to-window-line (+ (/ lines 2) (1- val))))
;; positioning is done twice: before and after command execution
(if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
(back-to-indentation)
(if com (vip-execute-com 'vip-window-middle val com))
(if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
(back-to-indentation)
))
(defun vip-window-bottom (arg)
"Go to last window line."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getCom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(push-mark nil t)
(move-to-window-line (- val))
;; positioning is done twice: before and after command execution
(if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
(back-to-indentation)
(if com (vip-execute-com 'vip-window-bottom val com))
(if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
(back-to-indentation)
))
(defun vip-line-to-top (arg)
"Put current line on the home line."
(interactive "p")
(recenter (1- arg)))
(defun vip-line-to-middle (arg)
"Put current line on the middle line."
(interactive "p")
(recenter (+ (1- arg) (/ (1- (window-height)) 2))))
(defun vip-line-to-bottom (arg)
"Put current line on the last line."
(interactive "p")
(recenter (- (window-height) (1+ arg))))
;; paren match
;; must correct this to only match ( to ) etc. On the other hand
;; it is good that paren match gets confused, because that way you
;; catch _all_ imbalances.
(defun vip-paren-match (arg)
"Go to the matching parenthesis."
(interactive "P")
(let ((com (vip-getcom arg)))
(if (numberp arg)
(if (or (> arg 99) (< arg 1))
(error "Prefix must be between 1 and 99")
(goto-char
(if (> (point-max) 80000)
(* (/ (point-max) 100) arg)
(/ (* (point-max) arg) 100)))
(back-to-indentation))
(let (lim)
(if (and (eolp) (not (bolp))) (forward-char -1))
(save-excursion
(end-of-line)
(setq lim (point)))
(if (re-search-forward "[][(){}]" lim t)
(backward-char)
(error "No matching character on line")))
(cond ((looking-at "[\(\[{]")
(if com (vip-move-marker-locally 'vip-com-point (point)))
(forward-sexp 1)
(if com
(vip-execute-com 'vip-paren-match nil com)
(backward-char)))
((looking-at "[])}]")
(forward-char)
(if com (vip-move-marker-locally 'vip-com-point (point)))
(backward-sexp 1)
(if com (vip-execute-com 'vip-paren-match nil com)))
(t (error ""))))))
;; sentence ,paragraph and heading
(defun vip-forward-sentence (arg)
"Forward sentence."
(interactive "P")
(push-mark nil t)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(forward-sentence val)
(if com (vip-execute-com 'vip-forward-sentence nil com))))
(defun vip-backward-sentence (arg)
"Backward sentence."
(interactive "P")
(push-mark nil t)
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(backward-sentence val)
(if com (vip-execute-com 'vip-backward-sentence nil com))))
(defun vip-forward-paragraph (arg)
"Forward paragraph."
(interactive "P")
(push-mark nil t)
(let ((val (vip-p-val arg))
(com (vip-getCom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(forward-paragraph val)
(if com (vip-execute-com 'vip-forward-paragraph nil com))))
(defun vip-backward-paragraph (arg)
"Backward paragraph."
(interactive "P")
(push-mark nil t)
(let ((val (vip-p-val arg))
(com (vip-getCom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(backward-paragraph val)
(if com (vip-execute-com 'vip-backward-paragraph nil com))))
;; should be mode-specific etc.
(defun vip-prev-heading (arg)
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getCom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(re-search-backward vip-heading-start nil t val)
(goto-char (match-beginning 0))
(if com (vip-execute-com 'vip-prev-heading nil com))))
(defun vip-heading-end (arg)
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getCom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(re-search-forward vip-heading-end nil t val)
(goto-char (match-beginning 0))
(if com (vip-execute-com 'vip-heading-end nil com))))
(defun vip-next-heading (arg)
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getCom arg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(end-of-line)
(re-search-forward vip-heading-start nil t val)
(goto-char (match-beginning 0))
(if com (vip-execute-com 'vip-next-heading nil com))))
;; scrolling
(setq scroll-step 1)
(defun vip-scroll (arg)
"Scroll to next screen."
(interactive "p")
(if (> arg 0)
(while (> arg 0)
(scroll-up)
(setq arg (1- arg)))
(while (> 0 arg)
(scroll-down)
(setq arg (1+ arg)))))
(defun vip-scroll-back (arg)
"Scroll to previous screen."
(interactive "p")
(vip-scroll (- arg)))
(defun vip-scroll-down (arg)
"Pull down half screen."
(interactive "P")
(condition-case nil
(if (null arg)
(scroll-down (/ (window-height) 2))
(scroll-down arg))
(error (beep 1)
(message "Beginning of buffer")
(goto-char (point-min)))))
(defun vip-scroll-down-one (arg)
"Scroll up one line."
(interactive "p")
(scroll-down arg))
(defun vip-scroll-up (arg)
"Pull up half screen."
(interactive "P")
(condition-case nil
(if (null arg)
(scroll-up (/ (window-height) 2))
(scroll-up arg))
(error (beep 1)
(message "End of buffer")
(goto-char (point-max)))))
(defun vip-scroll-up-one (arg)
"Scroll down one line."
(interactive "p")
(scroll-up arg))
;; searching
(defun vip-if-string (prompt)
(let ((s (vip-read-string-with-history
prompt
nil ; no initial
'vip-search-history
(car vip-search-history))))
(if (not (string= s ""))
(setq vip-s-string s))))
(defun vip-toggle-search-style (arg)
"Toggle the value of vip-case-fold-search/vip-re-search.
Without prefix argument, will ask which search style to toggle. With prefix
arg 1,toggles vip-case-fold-search; with arg 2 toggles vip-re-search.
Although this function is bound to \\[vip-toggle-search-style], the most
convenient way to use it is to bind `//' to the macro
`1 M-x vip-toggle-search-style' and `///' to
`2 M-x vip-toggle-search-style'. In this way, hitting `//' quickly will
toggle case-fold-search and hitting `/' three times witth toggle regexp
search. Macros are more convenient in this case because they don't affect
the Emacs binding of `/'."
(interactive "P")
(let (msg)
(cond ((or (eq arg 1)
(and (null arg)
(y-or-n-p (format "Search style: '%s'. Want '%s'? "
(if vip-case-fold-search
"case-insensitive" "case-sensitive")
(if vip-case-fold-search
"case-sensitive"
"case-insensitive")))))
(setq vip-case-fold-search (null vip-case-fold-search))
(if vip-case-fold-search
(setq msg "Search becomes case-insensitive")
(setq msg "Search becomes case-sensitive")))
((or (eq arg 2)
(and (null arg)
(y-or-n-p (format "Search style: '%s'. Want '%s'? "
(if vip-re-search
"regexp-search" "vanilla-search")
(if vip-re-search
"vanilla-search"
"regexp-search")))))
(setq vip-re-search (null vip-re-search))
(if vip-re-search
(setq msg "Search becomes regexp-style")
(setq msg "Search becomes vanilla-style")))
(t
(setq msg "Search style remains unchanged")))
(prin1 msg t)))
(defun vip-search-forward (arg)
"Search a string forward.
ARG is used to find the ARG's occurrence of the string.
Null string will repeat previous search."
(interactive "P")
(let ((val (vip-P-val arg))
(com (vip-getcom arg))
(old-str vip-s-string))
(setq vip-s-forward t)
(vip-if-string "/")
;; this is not used at present, but may be used later
(if (or (not (equal old-str vip-s-string))
(not (markerp vip-local-search-start-marker))
(not (marker-buffer vip-local-search-start-marker)))
(setq vip-local-search-start-marker (point-marker)))
(vip-search vip-s-string t val)
(if com
(progn
(vip-move-marker-locally 'vip-com-point (mark t))
(vip-execute-com 'vip-search-next val com)))))
(defun vip-search-backward (arg)
"Search a string backward.
ARG is used to find the ARG's occurrence of the string.
Null string will repeat previous search."
(interactive "P")
(let ((val (vip-P-val arg))
(com (vip-getcom arg))
(old-str vip-s-string))
(setq vip-s-forward nil)
(vip-if-string "?")
;; this is not used at present, but may be used later
(if (or (not (equal old-str vip-s-string))
(not (markerp vip-local-search-start-marker))
(not (marker-buffer vip-local-search-start-marker)))
(setq vip-local-search-start-marker (point-marker)))
(vip-search vip-s-string nil val)
(if com
(progn
(vip-move-marker-locally 'vip-com-point (mark t))
(vip-execute-com 'vip-search-next val com)))))
;; Search for COUNT's occurrence of STRING.
;; Search is forward if FORWARD is non-nil, otherwise backward.
;; INIT-POINT is the position where search is to start.
;; Arguments:
;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
(defun vip-search (string forward arg
&optional no-offset init-point fail-if-not-found)
(if (not (equal string ""))
(let ((val (vip-p-val arg))
(com (vip-getcom arg))
(offset (not no-offset))
(case-fold-search vip-case-fold-search)
(start-point (or init-point (point))))
(vip-deactivate-mark)
(if forward
(condition-case nil
(progn
(if offset (vip-forward-char-carefully))
(if vip-re-search
(progn
(re-search-forward string nil nil val)
(re-search-backward string))
(search-forward string nil nil val)
(search-backward string))
;; don't wait and don't flash in macros
(or executing-kbd-macro
(vip-flash-search-pattern))
(if (not (equal start-point (point)))
(push-mark start-point t)))
(search-failed
(if (and (not fail-if-not-found) vip-search-wrap-around-t)
(progn
(message "Search wrapped around end of buffer")
(goto-char (point-min))
(vip-search string forward (cons 1 com) t start-point 'fail)
;; don't wait in macros
(or executing-kbd-macro (sit-for 2))
;; delete the wrap-around message
(message "")
)
(goto-char start-point)
(error "`%s': %s not found"
string
(if vip-re-search "Pattern" "String"))
)))
;; backward
(condition-case nil
(progn
(if vip-re-search
(re-search-backward string nil nil val)
(search-backward string nil nil val))
;; don't wait and don't flash in macros
(or executing-kbd-macro
(vip-flash-search-pattern))
(if (not (equal start-point (point)))
(push-mark start-point t)))
(search-failed
(if (and (not fail-if-not-found) vip-search-wrap-around-t)
(progn
(message "Search wrapped around beginning of buffer")
(goto-char (point-max))
(vip-search string forward (cons 1 com) t start-point 'fail)
;; don't wait in macros
(or executing-kbd-macro (sit-for 2))
;; delete the wrap-around message
(message "")
)
(goto-char start-point)
(error "`%s': %s not found"
string
(if vip-re-search "Pattern" "String"))
)))))))
(defun vip-search-next (arg)
"Repeat previous search."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if (null vip-s-string) (error vip-NoPrevSearch))
(vip-search vip-s-string vip-s-forward arg)
(if com
(progn
(vip-move-marker-locally 'vip-com-point (mark t))
(vip-execute-com 'vip-search-next val com)))))
(defun vip-search-Next (arg)
"Repeat previous search in the reverse direction."
(interactive "P")
(let ((val (vip-p-val arg))
(com (vip-getcom arg)))
(if (null vip-s-string) (error vip-NoPrevSearch))
(vip-search vip-s-string (not vip-s-forward) arg)
(if com
(progn
(vip-move-marker-locally 'vip-com-point (mark t))
(vip-execute-com 'vip-search-Next val com)))))
;; Search contents of buffer defined by one of Viper's motion commands.
;; Repeatable via `n' and `N'.
(defun vip-buffer-search-enable (&optional c)
(cond (c (setq vip-buffer-search-char c))
((null vip-buffer-search-char)
(setq vip-buffer-search-char ?g)))
(define-key vip-vi-basic-map
(char-to-string vip-buffer-search-char) 'vip-command-argument)
(aset vip-exec-array vip-buffer-search-char 'vip-exec-buffer-search)
(setq vip-prefix-commands (cons vip-buffer-search-char vip-prefix-commands)))
;; This is a Viper wraper for isearch-forward.
(defun vip-isearch-forward (arg)
"Do incremental search forward."
(interactive "P")
;; emacs bug workaround
(if (listp arg) (setq arg (car arg)))
(vip-exec-form-in-emacs (list 'isearch-forward arg)))
;; This is a Viper wraper for isearch-backward."
(defun vip-isearch-backward (arg)
"Do incremental search backward."
(interactive "P")
;; emacs bug workaround
(if (listp arg) (setq arg (car arg)))
(vip-exec-form-in-emacs (list 'isearch-backward arg)))
;; visiting and killing files, buffers
(defun vip-switch-to-buffer ()
"Switch to buffer in the current window."
(interactive)
(let (buffer)
(setq buffer
(read-buffer
(format "Switch to buffer in this window \(%s\): "
(buffer-name (other-buffer (current-buffer))))))
(switch-to-buffer buffer)
))
(defun vip-switch-to-buffer-other-window ()
"Switch to buffer in another window."
(interactive)
(let (buffer)
(setq buffer
(read-buffer
(format "Switch to buffer in another window \(%s\): "
(buffer-name (other-buffer (current-buffer))))))
(switch-to-buffer-other-window buffer)
))
(defun vip-kill-buffer ()
"Kill a buffer."
(interactive)
(let (buffer buffer-name)
(setq buffer-name
(read-buffer
(format "Kill buffer \(%s\): "
(buffer-name (current-buffer)))))
(setq buffer
(if (null buffer-name)
(current-buffer)
(get-buffer buffer-name)))
(if (null buffer) (error "`%s': No such buffer" buffer-name))
(if (or (not (buffer-modified-p buffer))
(y-or-n-p
(format
"Buffer `%s' is modified, are you sure you want to kill it? "
buffer-name)))
(kill-buffer buffer)
(error "Buffer not killed"))))
(defvar vip-smart-suffix-list '("" "tex" "c" "cc" "el" "p")
"*List of suffixes that Viper automatically tries to append to filenames ending with a `.'.
This is useful when you the current directory contains files with the same
prefix and many different suffixes. Usually, only one of the suffixes
represents an editable file. However, file completion will stop at the `.'
The smart suffix feature lets you hit RET in such a case, and Viper will
select the appropriate suffix.
Suffixes are tried in the order given and the first suffix for which a
corresponding file exists is selected. If no file exists for any of the
suffixes, the user is asked to confirm.
To turn this feature off, set this variable to nil.")
;; Try to add suffix to files ending with a `.'
;; Useful when the user hits RET on a non-completed file name.
(defun vip-file-add-suffix ()
(let ((count 0)
(len (length vip-smart-suffix-list))
(file (buffer-string))
found key cmd suff)
(goto-char (point-max))
(if (and vip-smart-suffix-list (string-match "\\.$" file))
(progn
(while (and (not found) (< count len))
(setq suff (nth count vip-smart-suffix-list)
count (1+ count))
(if (file-exists-p (format "%s%s" file suff))
(progn
(setq found t)
(insert suff))))
(if found
()
(vip-tmp-insert-at-eob " [Please complete file name]")
(unwind-protect
(while (not (memq cmd '(exit-minibuffer vip-exit-minibuffer)))
(setq cmd
(key-binding (setq key (read-key-sequence nil))))
(cond ((eq cmd 'self-insert-command)
(if vip-xemacs-p
(insert (events-to-keys key))
(insert key)))
((memq cmd '(exit-minibuffer vip-exit-minibuffer))
nil)
(t (command-execute cmd)))
)))
))
))
;; Advice for use in find-file and read-file-name commands.
(defadvice exit-minibuffer (before vip-exit-minibuffer-advice activate)
"Run `vip-minibuffer-exit-hook' just before exiting the minibuffer."
(run-hooks 'vip-minibuffer-exit-hook))
(defadvice find-file (before vip-add-suffix-advice activate)
"Use `read-file-name' for reading arguments."
(interactive (list (read-file-name "Find file: "
nil default-directory))))
(defadvice find-file-other-window (before vip-add-suffix-advice activate)
"Use `read-file-name' for reading arguments."
(interactive (list (read-file-name "Find file in other window: "
nil default-directory))))
(defadvice find-file-other-frame (before vip-add-suffix-advice activate)
"Use `read-file-name' for reading arguments."
(interactive (list (read-file-name "Find file in other frame: "
nil default-directory))))
(defadvice read-file-name (around vip-suffix-advice activate)
"Tell `exit-minibuffer' to run `vip-file-add-suffix' as a hook."
(let ((vip-minibuffer-exit-hook 'vip-file-add-suffix))
ad-do-it))
;; yank and pop
(defsubst vip-yank (text)
"Yank TEXT silently. This works correctly with Emacs's yank-pop command."
(insert text)
(setq this-command 'yank))
(defun vip-put-back (arg)
"Put back after point/below line."
(interactive "P")
(let ((val (vip-p-val arg))
(text (if vip-use-register
(cond ((vip-valid-register vip-use-register '(digit))
(current-kill (- vip-use-register ?1) 'do-not-rotate))
((vip-valid-register vip-use-register)
(get-register (downcase vip-use-register)))
(t (error vip-InvalidRegister vip-use-register)))
(current-kill 0))))
(if (null text)
(if vip-use-register
(let ((reg vip-use-register))
(setq vip-use-register nil)
(error vip-EmptyRegister reg))
(error "")))
(setq vip-use-register nil)
(if (vip-end-with-a-newline-p text)
(progn
(if (eobp)
(insert "\n")
(forward-line 1))
(beginning-of-line))
(if (not (eolp)) (vip-forward-char-carefully)))
(set-marker (vip-mark-marker) (point) (current-buffer))
(vip-set-destructive-command
(list 'vip-put-back val nil vip-use-register nil nil))
(vip-loop val (vip-yank text)))
;; Vi puts cursor on the last char when the yanked text doesn't contain a
;; newline; it leaves the cursor at the beginning when the text contains
;; a newline
(if (vip-same-line (point) (mark))
(or (= (point) (mark)) (vip-backward-char-carefully))
(exchange-point-and-mark)
(if (bolp)
(back-to-indentation)))
(vip-deactivate-mark))
(defun vip-Put-back (arg)
"Put back at point/above line."
(interactive "P")
(let ((val (vip-p-val arg))
(text (if vip-use-register
(cond ((vip-valid-register vip-use-register '(digit))
(current-kill (- vip-use-register ?1) 'do-not-rotate))
((vip-valid-register vip-use-register)
(get-register (downcase vip-use-register)))
(t (error vip-InvalidRegister vip-use-register)))
(current-kill 0))))
(if (null text)
(if vip-use-register
(let ((reg vip-use-register))
(setq vip-use-register nil)
(error vip-EmptyRegister reg))
(error "")))
(setq vip-use-register nil)
(if (vip-end-with-a-newline-p text) (beginning-of-line))
(vip-set-destructive-command
(list 'vip-Put-back val nil vip-use-register nil nil))
(set-marker (vip-mark-marker) (point) (current-buffer))
(vip-loop val (vip-yank text)))
;; Vi puts cursor on the last char when the yanked text doesn't contain a
;; newline; it leaves the cursor at the beginning when the text contains
;; a newline
(if (vip-same-line (point) (mark))
(or (= (point) (mark)) (vip-backward-char-carefully))
(exchange-point-and-mark)
(if (bolp)
(back-to-indentation)))
(vip-deactivate-mark))
;; Copy region to kill-ring.
;; If BEG and END do not belong to the same buffer, copy empty region.
(defun vip-copy-region-as-kill (beg end)
(condition-case nil
(copy-region-as-kill beg end)
(error (copy-region-as-kill beg beg))))
;; Saves last inserted text for possible use by vip-repeat command.
(defun vip-save-last-insertion (beg end)
(setq vip-last-insertion (buffer-substring beg end))
(or (< (length vip-d-com) 5)
(setcar (nthcdr 4 vip-d-com) vip-last-insertion))
(or (null vip-command-ring)
(ring-empty-p vip-command-ring)
(progn
(setcar (nthcdr 4 (vip-current-ring-item vip-command-ring))
vip-last-insertion)
;; del most recent elt, if identical to the second most-recent
(vip-cleanup-ring vip-command-ring)))
)
(defsubst vip-yank-last-insertion ()
"Inserts the text saved by the previous vip-save-last-insertion command."
(condition-case nil
(insert vip-last-insertion)
(error nil)))
(defun vip-delete-char (arg)
"Delete character."
(interactive "P")
(let ((val (vip-p-val arg)))
(vip-set-destructive-command (list 'vip-delete-char val nil nil nil nil))
(if (> val 1)
(save-excursion
(let ((here (point)))
(end-of-line)
(if (> val (- (point) here))
(setq val (- (point) here))))))
(if (and (eq val 0) (not vip-ex-style-motion)) (setq val 1))
(if (and vip-ex-style-motion (eolp))
(if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch
(if vip-use-register
(progn
(cond ((vip-valid-register vip-use-register '((Letter)))
(vip-append-to-register
(downcase vip-use-register) (point) (- (point) val)))
((vip-valid-register vip-use-register)
(copy-to-register
vip-use-register (point) (- (point) val) nil))
(t (error vip-InvalidRegister vip-use-register)))
(setq vip-use-register nil)))
(if vip-ex-style-motion
(progn
(delete-char val t)
(if (and (eolp) (not (bolp))) (backward-char 1)))
(if (eolp)
(delete-backward-char val t)
(delete-char val t)))))
(defun vip-delete-backward-char (arg)
"Delete previous character. On reaching beginning of line, stop and beep."
(interactive "P")
(let ((val (vip-p-val arg)))
(vip-set-destructive-command
(list 'vip-delete-backward-char val nil nil nil nil))
(if (> val 1)
(save-excursion
(let ((here (point)))
(beginning-of-line)
(if (> val (- here (point)))
(setq val (- here (point)))))))
(if vip-use-register
(progn
(cond ((vip-valid-register vip-use-register '(Letter))
(vip-append-to-register
(downcase vip-use-register) (point) (+ (point) val)))
((vip-valid-register vip-use-register)
(copy-to-register
vip-use-register (point) (+ (point) val) nil))
(t (error vip-InvalidRegister vip-use-register)))
(setq vip-use-register nil)))
(if (bolp) (ding)
(delete-backward-char val t))))
(defun vip-del-backward-char-in-insert ()
"Delete 1 char backwards while in insert mode."
(interactive)
(if (and vip-ex-style-editing-in-insert (bolp))
(beep 1)
(delete-backward-char 1 t)))
(defun vip-del-backward-char-in-replace ()
"Delete one character in replace mode.
If `vip-delete-backwards-in-replace' is t, then DEL key actually deletes
charecters. If it is nil, then the cursor just moves backwards, similarly
to Vi. The variable `vip-ex-style-editing-in-insert', if t, doesn't let the
cursor move past the beginning of line."
(interactive)
(cond (vip-delete-backwards-in-replace
(cond ((not (bolp))
(delete-backward-char 1 t))
(vip-ex-style-editing-in-insert
(beep 1))
((bobp)
(beep 1))
(t
(delete-backward-char 1 t))))
(vip-ex-style-editing-in-insert
(if (bolp)
(beep 1)
(backward-char 1)))
(t
(backward-char 1))))
;; join lines.
(defun vip-join-lines (arg)
"Join this line to next, if ARG is nil. Otherwise, join ARG lines."
(interactive "*P")
(let ((val (vip-P-val arg)))
(vip-set-destructive-command (list 'vip-join-lines val nil nil nil nil))
(vip-loop (if (null val) 1 (1- val))
(progn
(end-of-line)
(if (not (eobp))
(progn
(forward-line 1)
(delete-region (point) (1- (point)))
(fixup-whitespace)))))))
;; Replace state
(defun vip-change (beg end)
(if (markerp beg) (setq beg (marker-position beg)))
(if (markerp end) (setq end (marker-position end)))
;; beg is sometimes (mark t), which may be nil
(or beg (setq beg end))
(vip-set-complex-command-for-undo)
(if vip-use-register
(progn
(copy-to-register vip-use-register beg end nil)
(setq vip-use-register nil)))
(vip-set-replace-overlay beg end)
(setq last-command nil) ; separate repl text from prev kills
(if (= (vip-replace-start) (point-max))
(error "End of buffer"))
(setq vip-last-replace-region
(buffer-substring (vip-replace-start)
(vip-replace-end)))
;; protect against error while inserting "@" and other disasters
;; (e.g., read-only buff)
(condition-case conds
(if (vip-same-line (vip-replace-start)
(vip-replace-end))
(progn
;; tabs cause problems in replace, so untabify
(goto-char (vip-replace-end))
(insert-before-markers "@") ; put placeholder after the TAB
(untabify (vip-replace-start) (point))
;; del @, don't put on kill ring
(delete-backward-char 1)
(vip-set-replace-overlay-glyphs
vip-replace-region-start-delimiter
vip-replace-region-end-delimiter)
;; this move takes care of the last posn in the overlay, which
;; has to be shifted because of insert. We can't simply insert
;; "$" before-markers because then overlay-start will shift the
;; beginning of the overlay in case we are replacing a single
;; character. This fixes the bug with `s' and `cl' commands.
(vip-move-replace-overlay (vip-replace-start) (point))
(goto-char (vip-replace-start))
(vip-change-state-to-replace t))
(kill-region (vip-replace-start)
(vip-replace-end))
(vip-restore-cursor-color)
(vip-change-state-to-insert))
(error ;; make sure that the overlay doesn't stay.
;; go back to the original point
(goto-char (vip-replace-start))
(vip-hide-replace-overlay)
(vip-message-conditions conds))))
(defun vip-change-subr (beg end)
;; beg is sometimes (mark t), which may be nil
(or beg (setq beg end))
(if vip-use-register
(progn
(copy-to-register vip-use-register beg end nil)
(setq vip-use-register nil)))
(kill-region beg end)
(setq this-command 'vip-change)
(vip-yank-last-insertion))
(defun vip-toggle-case (arg)
"Toggle character case."
(interactive "P")
(let ((val (vip-p-val arg)) (c))
(vip-set-destructive-command (list 'vip-toggle-case val nil nil nil nil))
(while (> val 0)
(setq c (following-char))
(delete-char 1 nil)
(if (eq c (upcase c))
(insert-char (downcase c) 1)
(insert-char (upcase c) 1))
(if (eolp) (backward-char 1))
(setq val (1- val)))))
;; query replace
(defun vip-query-replace ()
"Query replace.
If a null string is suplied as the string to be replaced,
the query replace mode will toggle between string replace
and regexp replace."
(interactive)
(let (str)
(setq str (vip-read-string-with-history
(if vip-re-query-replace "Query replace regexp: "
"Query replace: ")
nil ; no initial
'vip-replace1-history
(car vip-replace1-history) ; default
))
(if (string= str "")
(progn
(setq vip-re-query-replace (not vip-re-query-replace))
(message "Query replace mode changed to %s"
(if vip-re-query-replace "regexp replace"
"string replace")))
(if vip-re-query-replace
(query-replace-regexp
str
(vip-read-string-with-history
(format "Query replace regexp `%s' with: " str)
nil ; no initial
'vip-replace1-history
(car vip-replace1-history) ; default
))
(query-replace
str
(vip-read-string-with-history
(format "Query replace `%s' with: " str)
nil ; no initial
'vip-replace1-history
(car vip-replace1-history) ; default
))))))
;; marking
(defun vip-mark-beginning-of-buffer ()
"Mark beginning of buffer."
(interactive)
(push-mark (point))
(goto-char (point-min))
(exchange-point-and-mark)
(message "Mark set at the beginning of buffer"))
(defun vip-mark-end-of-buffer ()
"Mark end of buffer."
(interactive)
(push-mark (point))
(goto-char (point-max))
(exchange-point-and-mark)
(message "Mark set at the end of buffer"))
(defun vip-mark-point ()
"Set mark at point of buffer."
(interactive)
(let ((char (vip-read-char-exclusive)))
(cond ((and (<= ?a char) (<= char ?z))
(point-to-register (1+ (- char ?a))))
((= char ?<) (vip-mark-beginning-of-buffer))
((= char ?>) (vip-mark-end-of-buffer))
((= char ?.) (vip-set-mark-if-necessary))
((= char ?,) (vip-cycle-through-mark-ring))
((= char ?D) (mark-defun))
(t (error ""))
)))
;; Algorithm: If first invocation of this command save mark on ring, goto
;; mark, M0, and pop the most recent elt from the mark ring into mark,
;; making it into the new mark, M1.
;; Push this mark back and set mark to the original point position, p1.
;; So, if you hit '' or `` then you can return to p1.
;;
;; If repeated command, pop top elt from the ring into mark and
;; jump there. This forgets the position, p1, and puts M1 back into mark.
;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
;; the ring into mark. Push M2 back on the ring and set mark to M0.
;; etc.
(defun vip-cycle-through-mark-ring ()
"Visit previous locations on the mark ring.
One can use `` and '' to temporarily jump 1 step back."
(let* ((sv-pt (point)))
;; if repeated `m,' command, pop the previously saved mark.
;; Prev saved mark is actually prev saved point. It is used if the
;; user types `` or '' and is discarded
;; from the mark ring by the next `m,' command.
;; In any case, go to the previous or previously saved mark.
;; Then push the current mark (popped off the ring) and set current
;; point to be the mark. Current pt as mark is discarded by the next
;; m, command.
(if (eq last-command 'vip-cycle-through-mark-ring)
()
;; save current mark if the first iteration
(setq mark-ring (delete (vip-mark-marker) mark-ring))
(if (mark t)
(push-mark (mark t) t)) )
(pop-mark)
(set-mark-command 1)
;; don't duplicate mark on the ring
(setq mark-ring (delete (vip-mark-marker) mark-ring))
(push-mark sv-pt t)
(vip-deactivate-mark)
(setq this-command 'vip-cycle-through-mark-ring)
))
(defun vip-goto-mark (arg)
"Go to mark."
(interactive "P")
(let ((char (read-char))
(com (vip-getcom arg)))
(vip-goto-mark-subr char com nil)))
(defun vip-goto-mark-and-skip-white (arg)
"Go to mark and skip to first non-white character on line."
(interactive "P")
(let ((char (read-char))
(com (vip-getCom arg)))
(vip-goto-mark-subr char com t)))
(defun vip-goto-mark-subr (char com skip-white)
(if (eobp)
(if (bobp)
(error "Empty buffer")
(backward-char 1)))
(cond ((vip-valid-register char '(letter))
(let* ((buff (current-buffer))
(reg (1+ (- char ?a)))
(text-marker (get-register reg)))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(if (not (vip-valid-marker text-marker))
(error (format vip-EmptyTextmarker char)))
(if (and (vip-same-line (point) vip-last-jump)
(= (point) vip-last-jump-ignore))
(push-mark vip-last-jump t)
(push-mark nil t)) ; no msg
(vip-register-to-point reg)
(setq vip-last-jump (point-marker))
(cond (skip-white
(back-to-indentation)
(setq vip-last-jump-ignore (point))))
(if com
(if (equal buff (current-buffer))
(vip-execute-com (if skip-white
'vip-goto-mark-and-skip-white
'vip-goto-mark)
nil com)
(switch-to-buffer buff)
(goto-char vip-com-point)
(vip-change-state-to-vi)
(error "")))))
((and (not skip-white) (= char ?`))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(if (and (vip-same-line (point) vip-last-jump)
(= (point) vip-last-jump-ignore))
(goto-char vip-last-jump))
(if (= (point) (mark t)) (pop-mark))
(exchange-point-and-mark)
(setq vip-last-jump (point-marker)
vip-last-jump-ignore 0)
(if com (vip-execute-com 'vip-goto-mark nil com)))
((and skip-white (= char ?'))
(if com (vip-move-marker-locally 'vip-com-point (point)))
(if (and (vip-same-line (point) vip-last-jump)
(= (point) vip-last-jump-ignore))
(goto-char vip-last-jump))
(if (= (point) (mark t)) (pop-mark))
(exchange-point-and-mark)
(setq vip-last-jump (point))
(back-to-indentation)
(setq vip-last-jump-ignore (point))
(if com (vip-execute-com 'vip-goto-mark-and-skip-white nil com)))
(t (error vip-InvalidTextmarker char))))
(defun vip-insert-tab ()
(interactive)
(insert-tab))
(defun vip-exchange-point-and-mark ()
(interactive)
(exchange-point-and-mark)
(back-to-indentation))
;; Input Mode Indentation
(defun vip-forward-indent ()
"Indent forward -- `C-t' in Vi."
(interactive)
(setq vip-cted t)
(indent-to (+ (current-column) vip-shift-width)))
(defun vip-backward-indent ()
"Backtab, C-d in VI"
(interactive)
(if vip-cted
(let ((p (point)) (c (current-column)) bol (indent t))
(if (vip-looking-back "[0^]")
(progn
(if (= ?^ (preceding-char)) (setq vip-preserve-indent t))
(delete-backward-char 1)
(setq p (point))
(setq indent nil)))
(save-excursion
(beginning-of-line)
(setq bol (point)))
(if (re-search-backward "[^ \t]" bol 1) (forward-char))
(delete-region (point) p)
(if indent
(indent-to (- c vip-shift-width)))
(if (or (bolp) (vip-looking-back "[^ \t]"))
(setq vip-cted nil)))))
(defun vip-autoindent ()
"Auto Indentation, Vi-style."
(interactive)
(let ((col (current-indentation)))
(if (not vip-preserve-indent)
(setq vip-current-indent col)
(setq vip-preserve-indent nil))
;; don't leave whitespace lines around
(if (memq last-command
'(vip-autoindent
vip-open-line vip-Open-line
vip-replace-state-exit-cmd))
(indent-to-left-margin))
(newline 1)
(if vip-auto-indent
(progn
(setq vip-cted t)
(indent-to vip-current-indent)))))
;; Viewing registers
(defun vip-ket-function (arg)
"Function called by \], the ket. View registers and call \]\]."
(interactive "P")
(let ((reg (read-char)))
(cond ((vip-valid-register reg '(letter Letter))
(view-register (downcase reg)))
((vip-valid-register reg '(digit))
(let ((text (current-kill (- reg ?1) 'do-not-rotate)))
(save-excursion
(set-buffer (get-buffer-create "*Output*"))
(delete-region (point-min) (point-max))
(insert (format "Register %c contains the string:\n" reg))
(insert text)
(goto-char (point-min)))
(display-buffer "*Output*")))
((= ?\] reg)
(vip-next-heading arg))
(t (error
vip-InvalidRegister reg)))))
(defun vip-brac-function (arg)
"Function called by \[, the brac. View textmarkers and call \[\["
(interactive "P")
(let ((reg (read-char)))
(cond ((= ?\[ reg)
(vip-prev-heading arg))
((= ?\] reg)
(vip-heading-end arg))
((vip-valid-register reg '(letter))
(let* ((val (get-register (1+ (- reg ?a))))
(buf (if (not val)
(error
(format vip-EmptyTextmarker reg))
(marker-buffer val)))
(pos (marker-position val))
line-no text (s pos) (e pos))
(save-excursion
(set-buffer (get-buffer-create "*Output*"))
(delete-region (point-min) (point-max))
(if (and buf pos)
(progn
(save-excursion
(set-buffer buf)
(setq line-no (1+ (count-lines (point-min) val)))
(goto-char pos)
(beginning-of-line)
(if (re-search-backward "[^ \t]" nil t)
(progn
(beginning-of-line)
(setq s (point))))
(goto-char pos)
(forward-line 1)
(if (re-search-forward "[^ \t]" nil t)
(progn
(end-of-line)
(setq e (point))))
(setq text (buffer-substring s e))
(setq text (format "%s<%c>%s"
(substring text 0 (- pos s))
reg (substring text (- pos s)))))
(insert
(format
"Textmarker `%c' is in buffer `%s' at line %d.\n"
reg (buffer-name buf) line-no))
(insert (format "Here is some text around %c:\n\n %s"
reg text)))
(insert (format vip-EmptyTextmarker reg)))
(goto-char (point-min)))
(display-buffer "*Output*")))
(t (error vip-InvalidTextmarker reg)))))
;; commands in insertion mode
(defun vip-delete-backward-word (arg)
"Delete previous word."
(interactive "p")
(save-excursion
(push-mark nil t)
(backward-word arg)
(delete-region (point) (mark t))
(pop-mark)))
(defun vip-set-expert-level (&optional dont-change-unless)
"Sets the expert level for a Viper user.
Can be called interactively to change (temporarily or permanently) the
current expert level.
The optional argument DONT-CHANGE-UNLESS if not nil, says that
the level should not be changed, unless its current value is
meaningless (i.e., not one of 1,2,3,4,5).
User level determines the setting of Viper variables that are most
sensitive for VI-style look-and-feel."
(interactive)
(if (not (numberp vip-expert-level)) (setq vip-expert-level 0))
(save-window-excursion
(delete-other-windows)
;; if 0 < vip-expert-level < vip-max-expert-level
;; & dont-change-unless = t -- use it; else ask
(vip-ask-level dont-change-unless))
(setq vip-always t
vip-ex-style-motion t
vip-ex-style-editing-in-insert t
vip-want-ctl-h-help nil)
(cond
;; a novice or a beginner
((eq vip-expert-level 1)
(global-set-key vip-toggle-key ;; in emacs-state
(if (vip-window-display-p)
'vip-iconify
'suspend-emacs))
(setq vip-no-multiple-ESC t
vip-re-search t
vip-vi-style-in-minibuffer t
vip-search-wrap-around-t t
vip-want-emacs-keys-in-vi nil
vip-want-emacs-keys-in-insert nil))
;; an intermediate to guru
((and (> vip-expert-level 1) (< vip-expert-level 5))
(setq vip-no-multiple-ESC (if (vip-window-display-p) t 'twice)
vip-want-emacs-keys-in-vi t
vip-want-emacs-keys-in-insert (> vip-expert-level 2))
(if (eq vip-expert-level 4) ; respect user's ex-style motions
; and vip-no-multiple-ESC
(progn
(setq-default vip-ex-style-editing-in-insert
(cdr (assoc 'vip-ex-style-editing-in-insert
vip-saved-user-settings))
vip-ex-style-motion
(cdr (assoc 'vip-ex-style-motion
vip-saved-user-settings)))
(setq vip-ex-style-motion
(cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
vip-ex-style-editing-in-insert
(cdr (assoc 'vip-ex-style-editing-in-insert
vip-saved-user-settings))
vip-re-search
(cdr (assoc 'vip-re-search vip-saved-user-settings))
vip-no-multiple-ESC
(cdr (assoc 'vip-no-multiple-ESC
vip-saved-user-settings))))))
;; A wizard
;; Ideally, if 5 is selected, a buffer should pop up to let the
;; user toggle variable values.
(t (setq-default vip-ex-style-editing-in-insert
(cdr (assoc 'vip-ex-style-editing-in-insert
vip-saved-user-settings))
vip-ex-style-motion
(cdr (assoc 'vip-ex-style-motion
vip-saved-user-settings)))
(setq vip-want-ctl-h-help
(cdr (assoc 'vip-want-ctl-h-help vip-saved-user-settings))
vip-always
(cdr (assoc 'vip-always vip-saved-user-settings))
vip-no-multiple-ESC
(cdr (assoc 'vip-no-multiple-ESC vip-saved-user-settings))
vip-ex-style-motion
(cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
vip-ex-style-editing-in-insert
(cdr (assoc 'vip-ex-style-editing-in-insert
vip-saved-user-settings))
vip-re-search
(cdr (assoc 'vip-re-search vip-saved-user-settings))
vip-want-emacs-keys-in-vi
(cdr (assoc 'vip-want-emacs-keys-in-vi
vip-saved-user-settings))
vip-want-emacs-keys-in-insert
(cdr (assoc 'vip-want-emacs-keys-in-insert
vip-saved-user-settings)))))
(vip-set-mode-vars-for vip-current-state)
(if (or vip-always
(and (> vip-expert-level 0) (> 5 vip-expert-level)))
(vip-set-hooks)))
;; Ask user expert level.
(defun vip-ask-level (dont-change-unless)
(let ((ask-buffer " *vip-ask-level*")
level-changed repeated)
(save-window-excursion
(switch-to-buffer ask-buffer)
(or (eq this-command 'vip-set-expert-level)
(and
(<= vip-expert-level vip-max-expert-level)
(>= vip-expert-level 1))
(progn
(insert "
*** Important Notice for VIP users***
This is VIPER
@joke
Viper Is a Package for Emacs Rebels,
a VI Plan for Emacs Rescue,
and a venomous VI PERil.
@end joke
Technically speaking, Viper is a new Vi emulator that replaces
the old VIP package.
Viper emulates Vi much better than VIP. It also significantly
extends and improves upon Vi in many useful ways.
Although many VIP settings in your ~/.vip are compatible with Viper,
you may have to change some of them. Please refer to the documentation,
which can be obtained by executing
:help
when Viper is in Vi state.
If you will be so lucky as to find a bug, report it via the command
:submitReport
Type any key to continue... ")
(read-char)
(erase-buffer)))
(while (or (> vip-expert-level vip-max-expert-level)
(< vip-expert-level 1)
(null dont-change-unless))
(erase-buffer)
(if repeated
(progn
(message "Invalid user level")
(beep 1))
(setq repeated t))
(setq dont-change-unless t
level-changed t)
(insert "
Please specify your level of familiarity with the venomous VI PERil
(and the VI Plan for Emacs Rescue).
You can change it at any time by typing `M-x vip-set-expert-level RET'
1 -- BEGINNER: Almost all Emacs features are suppressed.
Feels almost like straight Vi. File name completion and
command history in the minibuffer are thrown in as a bonus.
To use Emacs productively, you must reach level 3 or higher.
2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
so most Emacs commands can be used when Viper is in Vi state.
Good progress---you are well on the way to level 3!
3 -- GRAND MASTER: Like 3, but most Emacs commands are available also
in Viper's insert state.
4 -- GURU: Like 3, but user settings are respected for vip-no-multiple-ESC,
vip-re-search, vip-ex-style-motion, & vip-ex-style-editing-in-insert
variables. Adjust these settings to your taste.
5 -- WIZARD: Like 4, but user settings are also respected for vip-always,
vip-want-ctl-h-help, vip-want-emacs-keys-in-vi, and
vip-want-emacs-keys-in-insert. Adjust these to your taste.
Please, specify your level now: ")
(setq vip-expert-level (- (vip-read-char-exclusive) ?0))
) ; end while
;; tell the user if level was changed
(and level-changed
(progn
(insert
(format "\n\n\n\n\n\t\tYou have selected user level %d"
vip-expert-level))
(if (y-or-n-p "Do you wish to make this change permanent? ")
;; save the setting for vip-expert-level
(vip-save-setting
'vip-expert-level
(format "Saving user level %d ..." vip-expert-level)
vip-custom-file-name))
))
(bury-buffer) ; remove ask-buffer from screen
(message "")
)))
(defun viper-version ()
(interactive)
(message "Viper version is %s" viper-version))
(defalias 'vip-version 'viper-version)
(defun vip-nil ()
(interactive)
(beep 1))
;; Returns t, if the string before point matches the regexp STR.
(defsubst vip-looking-back (str)
(and (save-excursion (re-search-backward str nil t))
(= (point) (match-end 0))))
;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
(defun vip-register-to-point (char &optional enforce-buffer)
"Like jump-to-register, but switches to another buffer in another window."
(interactive "cViper register to point: ")
(let ((val (get-register char)))
(cond
((and (fboundp 'frame-configuration-p)
(frame-configuration-p val))
(set-frame-configuration val))
((window-configuration-p val)
(set-window-configuration val))
((vip-valid-marker val)
(if (and enforce-buffer
(not (equal (current-buffer) (marker-buffer val))))
(error (concat vip-EmptyTextmarker " in this buffer")
(1- (+ char ?a))))
(pop-to-buffer (marker-buffer val))
(goto-char val))
((and (consp val) (eq (car val) 'file))
(find-file (cdr val)))
(t
(error vip-EmptyTextmarker (1- (+ char ?a)))))))
(defun vip-save-kill-buffer ()
"Save then kill current buffer. "
(interactive)
(if (< vip-expert-level 2)
(save-buffers-kill-emacs)
(save-buffer)
(kill-buffer (current-buffer))))
;;; Bug Report
(defun vip-submit-report ()
"Submit bug report on Viper."
(interactive)
(let ((reporter-prompt-for-summary-p t)
(vip-device-type (vip-device-type))
color-display-p frame-parameters
minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
varlist salutation window-config)
;; If mode info is needed, add variable to `let' and then set it below,
;; like we did with color-display-p.
(setq color-display-p (if (vip-window-display-p)
(vip-color-display-p)
'non-x)
minibuffer-vi-face (if (vip-window-display-p)
(vip-get-face vip-minibuffer-vi-face)
'non-x)
minibuffer-insert-face (if (vip-window-display-p)
(vip-get-face vip-minibuffer-insert-face)
'non-x)
minibuffer-emacs-face (if (vip-window-display-p)
(vip-get-face vip-minibuffer-emacs-face)
'non-x)
frame-parameters (if (fboundp 'frame-parameters)
(frame-parameters (selected-frame))))
(setq varlist (list 'vip-vi-minibuffer-minor-mode
'vip-insert-minibuffer-minor-mode
'vip-vi-intercept-minor-mode
'vip-vi-local-user-minor-mode
'vip-vi-kbd-minor-mode
'vip-vi-global-user-minor-mode
'vip-vi-state-modifier-minor-mode
'vip-vi-diehard-minor-mode
'vip-vi-basic-minor-mode
'vip-replace-minor-mode
'vip-insert-intercept-minor-mode
'vip-insert-local-user-minor-mode
'vip-insert-kbd-minor-mode
'vip-insert-global-user-minor-mode
'vip-insert-state-modifier-minor-mode
'vip-insert-diehard-minor-mode
'vip-insert-basic-minor-mode
'vip-emacs-intercept-minor-mode
'vip-emacs-local-user-minor-mode
'vip-emacs-kbd-minor-mode
'vip-emacs-global-user-minor-mode
'vip-emacs-state-modifier-minor-mode
'vip-automatic-iso-accents
'vip-want-emacs-keys-in-insert
'vip-want-emacs-keys-in-vi
'vip-keep-point-on-undo
'vip-no-multiple-ESC
'vip-ESC-key
'vip-want-ctl-h-help
'vip-ex-style-editing-in-insert
'vip-delete-backwards-in-replace
'vip-vi-style-in-minibuffer
'vip-vi-state-hook
'vip-insert-state-hook
'vip-replace-state-hook
'vip-emacs-state-hook
'ex-cycle-other-window
'ex-cycle-through-non-files
'vip-expert-level
'major-mode
'vip-device-type
'color-display-p
'frame-parameters
'minibuffer-vi-face
'minibuffer-insert-face
'minibuffer-emacs-face
))
(setq salutation "
Congratulations! You may have unearthed a bug in Viper!
Please mail a concise, accurate summary of the problem to the address above.
-------------------------------------------------------------------")
(setq window-config (current-window-configuration))
(with-output-to-temp-buffer " *vip-info*"
(switch-to-buffer " *vip-info*")
(delete-other-windows)
(princ "
PLEASE FOLLOW THESE PROCEDURES
------------------------------
Before reporting a bug, please verify that it is related to Viper, and is
not cause by other packages you are using.
Don't report compilation warnings, unless you are certain that there is a
problem. These warnings are normal and unavoidable.
Please note that users should not modify variables and keymaps other than
those advertised in the manual. Such `customization' is likely to crash
Viper, as it would any other improperly customized Emacs package.
If you are reporting an error message received while executing one of the
Viper commands, type:
M-x set-variable <Return> debug-on-error <Return> t <Return>
Then reproduce the error. The above command will cause Emacs to produce a
back trace of the execution that leads to the error. Please include this
trace in your bug report.
If you believe that one of Viper's commands goes into an infinite loop
\(e.g., Emacs freezes\), type:
M-x set-variable <Return> debug-on-quit <Return> t <Return>
Then reproduce the problem. Wait for a few seconds, then type C-g to abort
the current command. Include the resulting back trace in the bug report.
Mail anyway (y or n)? ")
(if (y-or-n-p "Mail anyway? ")
()
(set-window-configuration window-config)
(error "Bug report aborted")))
(require 'reporter)
(set-window-configuration window-config)
(reporter-submit-bug-report "kifer@cs.sunysb.edu"
(vip-version)
varlist
nil 'delete-other-windows
salutation)
))
;; Smoothes out the difference between Emacs' unread-command-events
;; and XEmacs unread-command-event. Arg is a character, an event, a list of
;; events or a sequence of keys.
;;
;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
;; symbol in unread-command-events list may cause Emacs to turn this symbol
;; into an event. Below, we delete nil from event lists, since nil is the most
;; common symbol that might appear in this wrong context.
(defun vip-set-unread-command-events (arg)
(if vip-emacs-p
(setq
unread-command-events
(let ((new-events
(cond ((eventp arg) (list arg))
((listp arg) arg)
((sequencep arg)
(listify-key-sequence arg))
(t (error
"vip-set-unread-command-events: Invalid argument, %S"
arg)))))
(if (not (eventp nil))
(setq new-events (delq nil new-events)))
(append new-events unread-command-events)))
;; XEmacs
(setq
unread-command-events
(append
(cond ((numberp arg) (list (character-to-event arg)))
((eventp arg) (list arg))
((stringp arg) (mapcar 'character-to-event arg))
((vectorp arg) (append arg nil)) ; turn into list
((listp arg) (vip-eventify-list-xemacs arg))
(t (error
"vip-set-unread-command-events: Invalid argument, %S" arg)))
unread-command-events))))
;; list is assumed to be a list of events of characters
(defun vip-eventify-list-xemacs (lis)
(mapcar
(function (lambda (elt)
(cond ((numberp elt) (character-to-event elt))
((eventp elt) elt)
(t (error
"vip-eventify-list-xemacs: can't convert to event, %S"
elt)))))
lis))
;;; Bring in the rest of the files
(require 'viper-mous)
(require 'viper-macs)
(require 'viper-ex)
;; The following is provided for compatibility with older VIP's
(defalias 'vip-change-mode-to-vi 'vip-change-state-to-vi)
(defalias 'vip-change-mode-to-insert 'vip-change-state-to-insert)
(defalias 'vip-change-mode-to-emacs 'vip-change-state-to-emacs)
;;; Load .vip and set up hooks
;; This hook designed to enable Vi-style editing in comint-based modes."
(defun vip-comint-mode-hook ()
(setq require-final-newline nil)
(setq vip-ex-style-editing-in-insert nil
vip-ex-style-motion nil)
(vip-add-local-keys 'vi-state
'(("\C-m" . comint-send-input) ; return
("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
(vip-add-local-keys 'insert-state
'(("\C-m" . comint-send-input) ; return
("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
)
;; This sets major mode hooks to make them come up in vi-state.
(defun vip-set-hooks ()
;; It is of course a misnomer to call viper-mode a `major mode'.
;; However, this has the effect that if the user didn't specify the
;; default mode, new buffers that fall back on the default will come up
;; in Fundamental Mode and Vi state.
(setq default-major-mode 'viper-mode)
;; The following major modes should come up in vi-state
(defadvice fundamental-mode (after vip-fundamental-mode-ad activate)
"Run `vip-change-state-to-vi' on entry."
(vip-change-state-to-vi))
(defvar help-mode-hook nil)
(add-hook 'help-mode-hook 'viper-mode)
(defvar emacs-lisp-mode-hook nil)
(add-hook 'emacs-lisp-mode-hook 'viper-mode)
(defvar html-mode-hook nil)
(add-hook 'html-mode-hook 'viper-mode)
(defvar lisp-mode-hook nil)
(add-hook 'lisp-mode-hook 'viper-mode)
(defvar bibtex-mode-hook nil)
(add-hook 'bibtex-mode-hook 'viper-mode)
(defvar cc-mode-hook nil)
(add-hook 'cc-mode-hook 'viper-mode)
(defvar c-mode-hook nil)
(add-hook 'c-mode-hook 'viper-mode)
(defvar c++-mode-hook nil)
(add-hook 'c++-mode-hook 'viper-mode)
(defvar lisp-interaction-mode-hook nil)
(add-hook 'lisp-interaction-mode-hook 'viper-mode)
(defvar text-mode-hook nil)
(add-hook 'text-mode-hook 'viper-mode)
(add-hook 'completion-list-mode-hook 'viper-mode)
(add-hook 'compilation-mode-hook 'viper-mode)
(defvar emerge-startup-hook nil)
(add-hook 'emerge-startup-hook 'vip-change-state-to-emacs)
;; Run vip-change-state-to-vi after quitting emerge.
(vip-eval-after-load
"emerge"
'(defadvice emerge-quit (after vip-emerge-advice activate)
"Run `vip-change-state-to-vi' after quitting emerge."
(vip-change-state-to-vi)))
;; In case Emerge was loaded before Viper.
(defadvice emerge-quit (after vip-emerge-advice activate)
"Run `vip-change-state-to-vi' after quitting emerge."
(vip-change-state-to-vi))
(vip-eval-after-load
"asm-mode"
'(defadvice asm-mode (after vip-asm-mode-ad activate)
"Run `vip-change-state-to-vi' on entry."
(vip-change-state-to-vi)))
;; passwd.el sets up its own buffer, which turns up in Vi mode,
;; thus overriding the local map. We don't need Vi mode here.
(vip-eval-after-load
"passwd"
'(defadvice read-passwd-1 (before vip-passwd-ad activate)
"Switch to emacs state while reading password."
(vip-change-state-to-emacs)))
;; Emacs shell, ange-ftp, and comint-based modes
(defvar comint-mode-hook nil)
(add-hook 'comint-mode-hook 'vip-change-state-to-insert)
(add-hook 'comint-mode-hook 'vip-comint-mode-hook)
;; Shell scripts
(defvar sh-mode-hook nil)
(add-hook 'sh-mode-hook 'viper-mode)
;; Dired
;; This is only necessary when the user uses vip-modify-major-mode
(add-hook 'dired-mode-hook 'vip-change-state-to-emacs)
(if vip-emacs-p
(progn
(defvar view-mode-hook nil
"View hook. Run after view mode.")
(add-hook 'view-mode-hook 'vip-change-state-to-emacs))
(defadvice view-minor-mode (after vip-view-ad activate)
"Switch to Emacs state in View mode."
(vip-change-state-to-emacs))
(defvar view-hook nil
"View hook. Run after view mode.")
(add-hook 'view-hook 'vip-change-state-to-emacs))
;; For VM users.
;; Put summary and other VM buffers in Emacs state.
(defvar vm-mode-hooks nil
"This hook is run after vm is started.")
(defvar vm-summary-mode-hooks nil
"This hook is run after vm switches to summary mode.")
(add-hook 'vm-mode-hooks 'vip-change-state-to-emacs)
(add-hook 'vm-summary-mode-hooks 'vip-change-state-to-emacs)
;; For RMAIL users.
;; Put buf in Emacs state after edit.
(vip-eval-after-load
"rmailedit"
'(defadvice rmail-cease-edit (after vip-rmail-advice activate)
"Switch to emacs state when done editing message."
(vip-change-state-to-emacs)))
;; In case RMAIL was loaded before Viper.
(defadvice rmail-cease-edit (after vip-rmail-advice activate)
"Switch to emacs state when done editing message."
(vip-change-state-to-emacs))
) ; vip-set-hooks
;; Set some useful macros
;; These must be before we load .vip, so the user can unrecord them.
;; repeat the 2nd previous command without rotating the command history
(vip-record-kbd-macro
(vector vip-repeat-from-history-key '\1) 'vi-state
[(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
;; repeat the 3d previous command without rotating the command history
(vip-record-kbd-macro
(vector vip-repeat-from-history-key '\2) 'vi-state
[(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
;; toggle case sensitivity in search
(vip-record-kbd-macro
"//" 'vi-state
[1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return] 't)
;; toggle regexp/vanila search
(vip-record-kbd-macro
"///" 'vi-state
[2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return] 't)
;; ~/.vip is loaded if it exists
(if (and (file-exists-p vip-custom-file-name)
(not noninteractive))
(load vip-custom-file-name))
;; VIP compatibility: merge whatever the user has in vip-mode-map into
;; Viper's basic map.
(vip-add-keymap vip-mode-map vip-vi-global-user-map)
;; Applying Viper customization -- runs after (load .vip)
;; Save user settings or Viper defaults for vars controled by vip-expert-level
(setq vip-saved-user-settings
(list (cons 'vip-want-ctl-h-help vip-want-ctl-h-help)
(cons 'vip-always vip-always)
(cons 'vip-no-multiple-ESC vip-no-multiple-ESC)
(cons 'vip-ex-style-motion vip-ex-style-motion)
(cons 'vip-ex-style-editing-in-insert
vip-ex-style-editing-in-insert)
(cons 'vip-want-emacs-keys-in-vi vip-want-emacs-keys-in-vi)
(cons 'vip-want-emacs-keys-in-insert vip-want-emacs-keys-in-insert)
(cons 'vip-re-search vip-re-search)))
(vip-set-minibuffer-style)
(vip-set-minibuffer-faces)
(vip-set-search-face)
(if vip-buffer-search-char
(vip-buffer-search-enable))
(vip-update-alphanumeric-class)
;;; Familiarize Viper with some minor modes that have their own keymaps
(vip-harness-minor-mode "compile")
(vip-harness-minor-mode "outline")
(vip-harness-minor-mode "allout")
(vip-harness-minor-mode "xref")
(vip-harness-minor-mode "lmenu")
(vip-harness-minor-mode "vc")
(vip-harness-minor-mode "ltx-math") ; LaTeX-math-mode in AUC-TeX
(vip-harness-minor-mode "latex") ; which is in one of these two files
(vip-harness-minor-mode "cyrillic")
(vip-harness-minor-mode "russian")
(vip-harness-minor-mode "view-less")
;; Intercept maps could go in viper-keym.el
;; We keep them here in case someone redefines them in ~/.vip
(define-key vip-vi-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
(define-key vip-insert-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
;; This is taken care of by vip-insert-global-user-map.
;;(define-key vip-replace-map vip-ESC-key 'vip-intercept-ESC-key)
(define-key vip-insert-intercept-map vip-toggle-key 'vip-alternate-ESC)
;; The default vip-toggle-key is \C-z; for the novice, it suspends or
;; iconifies Emacs
(define-key vip-vi-intercept-map vip-toggle-key
'(lambda () (interactive)
(if (and (< vip-expert-level 2) (equal vip-toggle-key "\C-z"))
(if (vip-window-display-p) (vip-iconify) (suspend-emacs))
(vip-change-state-to-emacs))))
(define-key vip-emacs-intercept-map vip-toggle-key 'vip-change-state-to-vi)
(if (or vip-always
(and (< vip-expert-level 5) (> vip-expert-level 0)))
(vip-set-hooks))
;; Let all minor modes take effect after loading
;; this may not be enough, so we also set default minor-mode-alist.
;; Without setting the default, new buffers that come up in emacs mode have
;; minor-mode-map-alist = nil, unless we call vip-change-state-*
(if (eq vip-current-state 'emacs-state)
(progn
(vip-change-state-to-emacs)
(setq-default minor-mode-map-alist minor-mode-map-alist)
))
(run-hooks 'vip-load-hook) ; the last chance to change something
(provide 'viper)
(provide 'vip19)
(provide 'vip)
;;; viper.el ends here
|