aboutsummaryrefslogtreecommitdiffstats
path: root/vim-mode/vim_mode.pl
blob: dd83f8a267b3331e7622a1707212ee790cb7fd44 (plain) (blame)
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
=pod

=head1 NAME

vim_mode.pl

=head1 DESCRIPTION

An Irssi script to emulate some of the vi(m) features for the Irssi inputline.

=head1 INSTALLATION

Copy into your F<~/.irssi/scripts/> directory and load with
C</SCRIPT LOAD vim_mode.pl>. You may wish to have it autoload in one of the
L<usual ways|https://github.com/shabble/irssi-docs/wiki/Guide#Autorunning_Scripts>.

=head2 DEPENDENCIES

For proper :ex mode support, vim-mode requires the installation of F<uberprompt.pl>
Uberprompt can be downloaded from:

L<https://github.com/shabble/irssi-scripts/raw/master/prompt_info/uberprompt.pl>

and follow the instructions at the top of that file for installation.

If you don't need Ex-mode, you can run vim_mode.pl without the
uberprompt.pl script, but it is strongly recommended that you use it.

=head3 Irssi requirements

0.8.12 and above should work fine. However the following features are
disabled in irssi < 0.8.13:

=over 4

=item * C<j> C<k> (only with count, they work fine without count in older versions)

=item * C<gg>, C<G>

=back

It is intended to work with at Irssi  0.8.12 and later versions. However some
features are disabled in older versions (see below for details).

Perl >= 5.8.1 is recommended for UTF-8 support (which can be disabled if
necessary).  Please report bugs in older versions as well, we'll try to fix
them.  Later versions of Perl are also faster, which is probably beneficial
to a script of this size and complexity.

=head2 SETUP

Vim Mode provides certain feedback to the user, such as the currently active
mode (command, insert, ex), and if switching buffers, which buffer(s) currently
match the search terms.

There are two ways to go about displaying this information, as described in
the following sections:

=head3 Statusbar Items

Run the following command to add a statusbar item that shows which mode
you're in.

C</statusbar window add vim_mode>

And the following to let C<:b [str]> display a list of channels matching your
search string.

C</statusbar window add vim_windows>

B<Note:> Remember to C</save> after adding these statusbar items to make them
permanent.

B<Note:> If you would rather have these statusbar items on your prompt
line rather than thte window statusbar, please follow these steps.

For technical reasons, I<uberprompt> must occasionally call C</statusbar prompt
reset>, which will remove or deactivate any manually added items on the prompt
statusbar.  To get around this, uberprompt provides two command hooks,
C<uberprompt_load_hook> and C<uberprompt_unload_hook>.  Both of these settings
can contain one (or more, using C</EVAL>) commands to be executed when the prompt
is enabled and disabled, respectively.

See the L<uberprompt documentation|https://github.com/shabble/irssi-scripts/blob/master/prompt_info/README.pod> for further details.

For I<right-aligned> items (that is, after the input field:

=over 4

=item 1 C</alias vm_add /^statusbar prompt add -after input -alignment right vim_mode>

=item 2 C</alias vm_del /^statusbar prompt remove vim_mode>

=item 3 C</set uberprompt_load_hook /^vm_add>

=item 4 C</set uberprompt_unload_hook /^vm_del>

=back

For I<left-aligned> items (before the prompt):

=over 4

=item 1 C</alias vm_add /^statusbar prompt add -before prompt -alignment left vim_mode>

=item 2 C</alias vm_del /^statusbar prompt remove vim_mode>

=item 3 C</set uberprompt_load_hook /^vm_add>

=item 4 C</set uberprompt_unload_hook /^vm_del>

=back

If you wish to add both C<vim_mode> and C<vim_windows> items, replace steps 1 and 2
above with the following (right-aligned):

=over 4

=item 1 C</alias vm_add /^eval /^statusbar prompt add -after input -alightment right vim_mode ; /^statusbar prompt add -after input -alignment right vim_windows>

=item 2 C</alias vm_del /^eval /^statusbar prompt remove vim_mode ; /^statusbar prompt remove vim_windows>

=back

and then complete stages 3 and 4 as above.  Replace the C<-after> and C<-alignment>
to suit your location preferences.

B<Note:> It is also possible to place the items between the prompt and input field,
by specifying C<-after prompt> or C<-before input> as appropriate.

=head3 Expando Variables

Vim mode augments the existing Irssi expando (automatic variables) with two
additional ones: C<$vim_cmd_mode> and C<$vim_wins>.

C<$vim_cmd_mode> is the equivalent of the C<vim_mode> statusbar item, and
C<$vim_wins> is the counterpart of C<vim_windows>.

They can be added to your theme, or inserted into your uberprompt using
a command such as:

"C</set uberprompt_format [$vim_cmd_mode] $*$uber] >"

=head3 FILE-BASED CONFIGURATION

Additionally to the irssi settings described in L<settings|/SETTINGS>, vim_mode
can be configured through an external configuration file named "vim_moderc"
located in F<~/.irssi/vim_moderc>. If available it's loaded on startup and every
supported ex-command is run. Its syntax is similar to "vimrc". To (re)load it
while vim_mode is running use C<:so[urce]>.

Currently Supported ex-commands:

=over 4

=item * C<:map>

=back

=head1 USAGE

The following section is divided into the different modes as supported by Vim itself:

=head2 COMMAND MODE

It supports most commonly used command mode features:

=over 2

=item * Insert/Command mode.

C<Esc> and C<Ctrl-C> enter command mode.  C</set vim_mode_cmd_seq j> allows
to use C<jj> as Escape (any other character can be used as well).

=item * Cursor motion:

C<h l 0 ^ $ E<lt>SpaceE<gt> E<lt>BSE<gt> f t F T>

=item * History motion:

C<j k gg G> C<gg> moves to the oldest (first) history line.  C<G> without a
count moves to the current input line, with a count it goes to the I<count-th>
history line (1 is the oldest).

=item * Cursor word motion:

C<w b ge e W gE B E>

=item * Word objects (only the following work yet):

C<aw aW>

=item * Yank and paste:

 C<y p P>

=item * Change and delete:

C<c d>

=item * Delete at cursor:

C<x X>

=item * Replace at cursor:

C<r>

=item * Insert mode:

C<i a I A>

=item * Switch case:

C<~>

=item * Repeat change:

C<.>

=item * Repeat

C<ftFT: ; ,>

=item * Registers:

C<"a-"z "" "0 "* "+ "_> (black hole)

=item * Line-wise shortcuts:

C<dd cc yy>

=item * Shortcuts:

C<s S C D>

=item * Scroll the scrollback buffer:

C<Ctrl-E Ctrl-D Ctrl-Y Ctrl-U Ctrl-F Ctrl-B>

=item * Switch to last active window:

C<Ctrl-6/Ctrl-^>

=item * Switch split windows:

<Ctrl-W j Ctrl-W k>

=item * Undo/Redo:

C<u Ctrl-R>

=back

Counts and combinations work as well, e.g. C<d5fx> or C<3iabcE<lt>escE<gt>>. Counts also work with mapped ex-commands (see below), e.g. if you map C<gb> to do C<:bn>, then C<2gb> will switch to the second next buffer.  Repeat also supports counts.

=head3 REGISTERS

=over 4

=item * Appending to register with C<"A-"Z>

=item * C<""> is the default yank/delete register.

=item * C<"0> contains the last yank (if no register was specified).

=item * The special registers C<"* "+> both contain irssi's internal cut-buffer.

=back

=head2 INSERT MODE

The following insert mode mappings are supported:

=over 4

=item * Insert register content: Ctrl-R x (where x is the register to insert)

=back

=head2 EX-MODE

Ex-mode (activated by C<:> in command mode) supports the following commands:

=over 4

=item * Command History:

C<E<lt>uparrowE<gt>> - cycle backwards through history

C<E<lt>downarrowE<gt>> - cycle forwards through history

C<:eh> - show ex history

=item * Switching buffers:

C<:[N]b [N]> - switch to channel number

C<:b#>       - switch to last channel

C<:b> E<lt>partial-channel-nameE<gt>

C<:b> <partial-server>/<partial-channel>

C<:buffer {args}> (same as C<:b>)

C<:[N]bn[ext] [N]> - switch to next window

C<:[N]bp[rev] [N]> - switch to previous window

=item * Close window:

C<:[N]bd[elete] [N]>

=item * Display windows:

C<:ls>, C<:buffers>

=item * Display registers:

C<:reg[isters] {args}>, C<:di[splay] {args}>

=item * Display undolist:

C<:undol[ist]> (mostly used for debugging)

=item * Source files:

C<:so[urce]> - only sources vim_moderc at the moment,
                         F<{file}> not supported

=item * Mappings:

C<:map> - display custom mappings

=item * Saving mappings:

C<:mkv[imrc][!]> - like in Vim, but [file] not supported

=item * Substitution:

C<:s///> - I<i> and I<g> are supported as flags, only C<///> can be used as
eparator, and it uses Perl regex syntax instead of Vim syntax.

=item * Settings:

C<:se[t]> - display all options

C<:se[t] {option}>         - display all matching options

C<:se[t] {option} {value}> - change option to value

=back

=head3 MAPPINGS

=head4 Commands

=over 4

=item * C<:map {lhs}>       - display mappings starting with {lhs}

=item * C<:map {lhs} {rhs}> - add mapping

=item * C<:unm[ap] {lhs}>   - remove custom mapping

=back

=head4 Parameters

I<{lhs}> is the key combination to be mapped, I<{rhs}> the target. The
C<E<lt>E<gt>> notation is used

(e.g. C<E<lt>C-FE<gt>> is I<Ctrl-F>), case is ignored.
 Supported C<E<lt>E<gt>> keys are:

=over 4

=item * C<E<lt>C-AE<gt>> - C<E<lt>C-ZE<gt>>,

=item * C<E<lt>C-^E<gt>>, C<E<lt>C-6E<gt>>

=item * C<E<lt>SpaceE<gt>>

=item * C<E<lt>CRE<gt>> - Enter

=item * C<E<lt>BSE<gt>> - Backspace

=item * C<E<lt>NopE<gt>> - No-op (Do Nothing).

=back

Mapping ex-mode and irssi commands is supported. When mapping ex-mode commands
the trailing C<E<lt>CrE<gt>> is not necessary. Only default mappings can be used
in I<{rhs}>.

Examples:

=over 4

=item * C<:map w  W>      - to remap w to work like W

=item * C<:map gb :bnext> - to map gb to call :bnext

=item * C<:map gB :bprev>

=item * C<:map g1 :b 1>   - to map g1 to switch to buffer 1

=item * C<:map gb :b>     - to map gb to :b, 1gb switches to buffer 1, 5gb to 5

=item * C<:map E<lt>C-LE<gt> /clear> - map Ctrl-L to irssi command /clear

=item * C<:map E<lt>C-GE<gt> /window goto 1>

=item * C<:map E<lt>C-EE<gt> <Nop>> - disable E<lt>C-EE<gt>, it does nothing now

=item * C<:unmap E<lt>C-EE<gt>> - restore default behavior of C<E<lt>C-EE<gt>>
after disabling it

=back

Note that you must use C</> for irssi commands (like C</clear>), the current value
of Irssi's cmdchars does B<not> work! This is necessary do differentiate between
ex-commands and irssi commands.

=head2 SETTINGS

The settings are stored as irssi settings and can be set using C</set> as usual
(prepend C<vim_mode_> to setting name) or using the C<:set> ex-command. The
following settings are available:

=over 4

=item * utf8 - Support UTF-8 characters, boolean, default on

=item * debug - Enable debug output, boolean, default off

=item * cmd_seq - Char that when double-pressed simulates C<E<lt>EscE<gt>>, string, default '' (disabled)

=item * start_cmd - Start every line in command mode, boolean, default off

=item * max_undo_lines - Sze of the undo buffer. Integer, default 50 items.

=item * ex_history_size - Number of items stored in the ex-mode history. Integer, default 100.

=item * prompt_leading_space - Ddetermines whether ex mode prepends a space to the displayed input. Boolean, default on

=back

In contrast to irssi's settings, C<:set> accepts 0 and 1 as values for boolean
settings, but only vim_mode's settings can be set/displayed.

Examples:

   :set cmd_seq=j   # set cmd_seq to j
   :set cmd_seq=    # disable cmd_seq
   :set debug=on    # enable debug
   :set debug=off   # disable debug

=head1 SUPPORT

Any behavior different from Vim (unless explicitly documented) should be
considered a bug and reported.

B<NOTE:> This script is still under heavy development, and there may be bugs.
Please submit reproducible sequences to the bug-tracker at:
L<http://github.com/shabble/irssi-scripts/issues/new>

or contact rudi_s or shabble on irc.freenode.net (#irssi and #irssi_vim)

=head1 AUTHORS

Copyright E<copy> 2010-2011 Tom Feist C<E<lt>shabble+irssi@metavore.orgE<gt>> and

Copyright E<copy> 2010-2011 Simon Ruderich C<E<lt>simon@ruderich.orgE<gt>>

=head1 THANKS

Particular thanks go to

=over 4

=item * estragib: a lot of testing and many bug reports and feature requests

=item * iaj: testing

=item * tmr: explaining how various bits of vim work

=back

as well as the rest of C<#irssi> and C<#irssi_vim> on Freenode IRC.

=head1 LICENCE

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

=head1 BUGS

=over 4

=item *

count before register doesn't work: e.g. 3"ap doesn't work, but "a3p does

=item *

mapping an incomplete ex-command doesn't open the ex-mode with the partial
command (e.g. C<:map gb :b> causes an error instead of opening the ex-mode and
displaying C<:bE<lt>cursorE<gt>>)

=item *

 undo/redo cursor positions are mostly wrong

=back

=head1 TODO

=over 4

=item *

Make sure the input line is empty when entering ex mode. Stuff hanging around
just looks silly.

=item *

History:

=over 4

=item *

 C< * /,?,n,N> to search through history (like rl_history_search.pl)

=back

=item *

Window switching (C<:b>)

=over 4

=item *

Tab completion of window(-item) names

=item *

non-sequential matches(?)

=back

=back

See also the TODO file at
L<github|https://github.com/shabble/irssi-scripts/blob/master/vim-mode/TODO> for
many many more things.

=head2 WONTFIX

Things we're not ever likely to do:

=over 4

=item * Macros

=back

=cut

use strict;
use warnings;

use Encode;
use List::Util;

use Irssi;
use Irssi::TextUI;              # for sbar_items_redraw
use Irssi::Irc;                 # necessary for 0.8.14



our $VERSION = "1.0.2";
our %IRSSI   =
  (
   authors         => "Tom Feist (shabble), Simon Ruderich (rudi_s)",
   contact         => 'shabble+irssi@metavore.org, '
                    . 'shabble@#irssi/Freenode, simon@ruderich.org'
                    . 'rudi_s@#irssi/Freenode',
   name            => "vim_mode",
   description     => "Give Irssi Vim-like commands for editing the inputline",
   license         => "MIT",
   changed         => "28/9/2010"
  );


# CONSTANTS

# command mode
sub M_CMD () { 1 }
# insert mode
sub M_INS () { 0 }
# extended mode (after a :?)
sub M_EX  () { 2 }

# operator command
sub C_OPERATOR   () { 0 }
# normal command, no special handling necessary
sub C_NORMAL     () { 1 }
# command taking another key as argument
sub C_NEEDSKEY   () { 2 }
# text-object command (i a)
sub C_TEXTOBJECT () { 3 }
# commands entering insert mode
sub C_INSERT     () { 4 }
# ex-mode commands
sub C_EX         () { 5 }
# irssi commands
sub C_IRSSI      () { 6 }
# does nothing
sub C_NOP        () { 7 }

# setting types, match irssi types as they are stored as irssi settings
sub S_BOOL () { 0 }
sub S_INT  () { 1 }
sub S_STR  () { 2 }

# word and non-word regex, keep in sync with setup_changed()!
my $word     = qr/[\w_]/o;
my $non_word = qr/[^\w_\s]/o;

# COMMANDS

# All available commands in command mode, they are stored with a char as key,
# but this is not necessarily the key the command is currently mapped to.
my $commands
  = {
     # operators
     c => { char => 'c', func => \&cmd_operator_c, type => C_OPERATOR,
            repeatable => 1 },
     d => { char => 'd', func => \&cmd_operator_d, type => C_OPERATOR,
            repeatable => 1 },
     y => { char => 'y', func => \&cmd_operator_y, type => C_OPERATOR,
            repeatable => 1 },

     # arrow like movement
      h     => { char => 'h',       func => \&cmd_h, type => C_NORMAL },
      l     => { char => 'l',       func => \&cmd_l, type => C_NORMAL },
     "\x08" => { char => '<BS>',    func => \&cmd_h, type => C_NORMAL },
     "\x7F" => { char => '<BS>',    func => \&cmd_h, type => C_NORMAL },
     ' '    => { char => '<Space>', func => \&cmd_l, type => C_NORMAL },
     # history movement
     j      => { char => 'j',  func => \&cmd_j,  type => C_NORMAL,
                 no_operator => 1 },
     k      => { char => 'k',  func => \&cmd_k,  type => C_NORMAL,
                 no_operator => 1 },
     gg     => { char => 'gg', func => \&cmd_gg, type => C_NORMAL,
                 no_operator => 1 },
     G      => { char => 'G',  func => \&cmd_G,  type => C_NORMAL,
                 needs_count => 1, no_operator => 1 },
     # char movement, take an additional parameter and use $movement
      f  => { char => 'f', func => \&cmd_f, type => C_NEEDSKEY,
              selection_needs_move_right => 1 },
      t  => { char => 't', func => \&cmd_t, type => C_NEEDSKEY,
              selection_needs_move_right => 1 },
      F  => { char => 'F', func => \&cmd_F, type => C_NEEDSKEY },
      T  => { char => 'T', func => \&cmd_T, type => C_NEEDSKEY },
     ';' => { char => ';', func => \&cmd_semicolon, type => C_NORMAL },
     ',' => { char => ',', func => \&cmd_comma, type => C_NORMAL },
     # word movement
     w  => { char => 'w',  func => \&cmd_w,  type => C_NORMAL },
     b  => { char => 'b',  func => \&cmd_b,  type => C_NORMAL },
     e  => { char => 'e',  func => \&cmd_e,  type => C_NORMAL,
             selection_needs_move_right => 1 },
     ge => { char => 'ge', func => \&cmd_ge, type => C_NORMAL,
             selection_needs_move_right => 1 },
     W  => { char => 'W',  func => \&cmd_W,  type => C_NORMAL },
     B  => { char => 'B',  func => \&cmd_B,  type => C_NORMAL },
     E  => { char => 'E',  func => \&cmd_E,  type => C_NORMAL },
     gE => { char => 'gE', func => \&cmd_gE, type => C_NORMAL,
             selection_needs_move_right => 1 },
     # text-objects, leading _ means can't be mapped!
     _i => { char => 'i', func => \&cmd__i,      type => C_TEXTOBJECT },
     _a => { char => 'a', func => \&cmd__a,      type => C_TEXTOBJECT },
     # line movement
     '0' => { char => '0', func => \&cmd_0,      type => C_NORMAL },
     '^' => { char => '^', func => \&cmd_caret,  type => C_NORMAL },
     '$' => { char => '$', func => \&cmd_dollar, type => C_NORMAL },
     # delete chars
     x => { char => 'x', func => \&cmd_x, type => C_NORMAL,
            repeatable => 1, no_operator => 1 },
     X => { char => 'X', func => \&cmd_X, type => C_NORMAL,
            repeatable => 1, no_operator => 1 },
          # C_NORMAL is correct, operator c takes care of insert mode
     s => { char => 's', func => \&cmd_s, type => C_NORMAL,
            repeatable => 1, no_operator => 1 },
          # C_NORMAL is correct, operator c takes care of insert mode
     S => { char => 'S', func => \&cmd_S, type => C_NORMAL,
            repeatable => 1, no_operator => 1 },
     # insert mode
     i => { char => 'i', func => \&cmd_i, type => C_INSERT,
            repeatable => 1, no_operator => 1 },
     I => { char => 'I', func => \&cmd_I, type => C_INSERT,
            repeatable => 1, no_operator => 1 },
     a => { char => 'a', func => \&cmd_a, type => C_INSERT,
            repeatable => 1, no_operator => 1 },
     A => { char => 'A', func => \&cmd_A, type => C_INSERT,
            repeatable => 1, no_operator => 1 },
     # replace
     r => { char => 'r', func => \&cmd_r, type => C_NEEDSKEY,
            repeatable => 1, no_operator => 1 },
     # paste
     p => { char => 'p', func => \&cmd_p, type => C_NORMAL,
            repeatable => 1, no_operator => 1 },
     P => { char => 'P', func => \&cmd_P, type => C_NORMAL,
            repeatable => 1, no_operator => 1 },
     # to end of line
     C => { char => 'C', func => \&cmd_C, type => C_NORMAL,
            repeatable => 1, no_operator => 1 },
     D => { char => 'D', func => \&cmd_D, type => C_NORMAL,
            repeatable => 1, no_operator => 1 },
     # scrolling
     "\x05" => { char => '<C-E>', func => \&cmd_ctrl_d, type => C_NORMAL,
                 no_operator => 1 },
     "\x04" => { char => '<C-D>', func => \&cmd_ctrl_d, type => C_NORMAL,
                 needs_count => 1, no_operator => 1 },
     "\x19" => { char => '<C-Y>', func => \&cmd_ctrl_u, type => C_NORMAL,
                 no_operator => 1 },
     "\x15" => { char => '<C-U>', func => \&cmd_ctrl_u, type => C_NORMAL,
                 needs_count => 1, no_operator => 1 },
     "\x06" => { char => '<C-F>', func => \&cmd_ctrl_f, type => C_NORMAL,
                 no_operator => 1 },
     "\x02" => { char => '<C-B>', func => \&cmd_ctrl_b, type => C_NORMAL,
                 no_operator => 1 },
     # window switching
     "\x17j" => { char => '<C-W>j', func => \&cmd_ctrl_wj, type => C_NORMAL,
                  no_operator => 1 },
     "\x17k" => { char => '<C-W>k', func => \&cmd_ctrl_wk, type => C_NORMAL,
                  no_operator => 1 },
     "\x1e"  => { char => '<C-^>',  func => \&cmd_ctrl_6,  type => C_NORMAL,
                  no_operator => 1 },
     # misc
     '~'  => { char => '~', func => \&cmd_tilde, type => C_NORMAL,
               repeatable => 1, no_operator => 1 },
     '"'  => { char => '"', func => \&cmd_register, type => C_NEEDSKEY,
               no_operator => 1 },
     '.'  => { char => '.', type => C_NORMAL, repeatable => 1,
               no_operator => 1 },
     ':'  => { char => ':', type => C_NORMAL },
     "\n" => { char => '<CR>', type => C_NORMAL }, # return
     # undo
     'u'    => { char => 'u',     func => \&cmd_undo, type => C_NORMAL,
                 no_operator => 1 },
     "\x12" => { char => '<C-R>', func => \&cmd_redo, type => C_NORMAL,
                 no_operator => 1 },
    };

# All available commands in Ex-Mode.
my $commands_ex
  = {
     # arrow keys - not actually used, see handle_input_buffer()
     # TODO: make these work.
     "\e[A"    => { char => ':exprev',    func => \&ex_history_back,
                    type => C_EX },
     "\e[B"    => { char => ':exnext',    func => \&ex_history_fwd,
                    type => C_EX },

     # normal Ex mode commands.
     eh        => { char => ':exhist',    func => \&ex_history_show,
                    type => C_EX },
     s         => { char => ':s',         func => \&ex_substitute,
                    type => C_EX },
     bnext     => { char => ':bnext',     func => \&ex_bnext,
                    type => C_EX, uses_count => 1 },
     bn        => { char => ':bn',        func => \&ex_bnext,
                    type => C_EX, uses_count => 1 },
     bprev     => { char => ':bprev',     func => \&ex_bprev,
                    type => C_EX, uses_count => 1 },
     bp        => { char => ':bp',        func => \&ex_bprev,
                    type => C_EX, uses_count => 1 },
     bdelete   => { char => ':bdelete',   func => \&ex_bdelete,
                    type => C_EX, uses_count => 1 },
     bd        => { char => ':bd',        func => \&ex_bdelete,
                    type => C_EX, uses_count => 1 },
     buffer    => { char => ':buffer',    func => \&ex_buffer,
                    type => C_EX, uses_count => 1 },
     b         => { char => ':b',         func => \&ex_buffer,
                    type => C_EX, uses_count => 1 },
     registers => { char => ':registers', func => \&ex_registers,
                    type => C_EX },
     reg       => { char => ':reg',       func => \&ex_registers,
                    type => C_EX },
     display   => { char => ':display',   func => \&ex_registers,
                    type => C_EX },
     di        => { char => ':di',        func => \&ex_registers,
                    type => C_EX },
     buffers   => { char => ':buffer',    func => \&ex_buffers,
                    type => C_EX },
     ls        => { char => ':ls',        func => \&ex_buffers,
                    type => C_EX },
     undolist  => { char => ':undolist',  func => \&ex_undolist,
                    type => C_EX },
     undol     => { char => ':undol',     func => \&ex_undolist,
                    type => C_EX },
     map       => { char => ':map',       func => \&ex_map,
                    type => C_EX },
     unmap     => { char => ':unmap',     func => \&ex_unmap,
                    type => C_EX },
     unm       => { char => ':unm',       func => \&ex_unmap,
                    type => C_EX },
     source    => { char => ':source',    func => \&ex_source,
                    type => C_EX },
     so        => { char => ':so',        func => \&ex_source,
                    type => C_EX },
     mkvimrc   => { char => ':mkvimrc',   func => \&ex_mkvimrc,
                    type => C_EX },
     mkv       => { char => ':mkv',       func => \&ex_mkvimrc,
                    type => C_EX },
     se        => { char => ':se',        func => \&ex_set,
                    type => C_EX },
     set       => { char => ':set',       func => \&ex_set,
                    type => C_EX },
     itemnext  => { char => ':itemnext',  func => \&ex_item_next,
                    type => C_EX },
     inext     => { char => ':itemnext',  func => \&ex_item_next,
                    type => C_EX },
     itemprev  => { char => ':itemprev',  func => \&ex_item_prev,
                    type => C_EX },
     iprev     => { char => ':itemprev',  func => \&ex_item_prev,
                    type => C_EX },
     servernext  => { char => ':servernext', func => \&ex_server_next,
                    type => C_EX },
     snext       => { char => ':servernext', func => \&ex_server_next,
                    type => C_EX },
     serverprev  => { char => ':serverprev', func => \&ex_server_prev,
                    type => C_EX },
     sprev       => { char => ':serverprev', func => \&ex_server_prev,
                    type => C_EX },

    };

# MAPPINGS

# default command mode mappings
my $maps = {};

# current imap still pending (first character entered)
my $imap = undef;

# maps for insert mode
my $imaps
  = {
     # CTRL-R, insert register
     "\x12" => { map  => undef, func => \&insert_ctrl_r },
    };


# GLOBAL VARIABLES

# all vim_mode settings, must be enabled in vim_mode_init() before usage
my $settings
  = {
     # print debug output
     debug          => { type => S_BOOL, value => 0 },
     # use UTF-8 internally for string calculations/manipulations
     utf8           => { type => S_BOOL, value => 1 },
     # esc-shortcut in insert mode
     cmd_seq        => { type => S_STR,  value => '' },
     # start every line in command mode
     start_cmd      => { type => S_BOOL, value => 0 },
     # not used yet
     max_undo_lines => { type => S_INT,  value => 50 },
     # size of history buffer for Ex mode.
     ex_history_size => { type => S_INT, value => 100 },
     # prompt_leading_space
     prompt_leading_space => { type => S_BOOL, value => 1 },
     # <Leader> value for prepending to commands.
     map_leader     => { type => S_STR,  value => '\\' },
     # timeout for keys following esc. In milliseconds.
     esc_buf_timeout => { type => S_INT, value =>  10 },

    };

sub DEBUG { $settings->{debug}->{value} }

# buffer to keep track of the last N keystrokes, used for Esc detection and
# insert mode mappings
my @input_buf;
my $input_buf_timer;
my $input_buf_enabled = 0;

# insert mode repeat buffer, used to repeat (.) last insert
my @insert_buf;

# flag to allow us to emulate keystrokes without re-intercepting them
my $should_ignore = 0;

# ex mode buffer
my @ex_buf;

# ex mode history storage.
my @ex_history;
my $ex_history_index = 0;

# we are waiting for another mapped key (e.g. g pressed, but there are
# multiple mappings like gg gE etc.)
my $pending_map = undef;

# for commands like 10x
my $numeric_prefix = undef;
# current operator as $command hash
my $operator = undef;
# vi movements, only used when a movement needs more than one key (like f t).
my $movement = undef;
# last vi command, used by .
my $last
  = {
     'cmd' => $commands->{i}, # = i to support . when loading the script
     'numeric_prefix' => undef,
     'operator' => undef,
     'movement' => undef,
     'register' => '"',
    };
# last ftFT movement, used by ; and ,
my $last_ftFT
  = {
     type => undef, # f, t, F or T
     char => undef,
    };

# what Vi mode we're in. We start in insert mode.
my $mode = M_INS;

# current active register
my $register = '"';

# vi registers
my $registers
  = {
     '"' => '', # default register
     '0' => '', # yank register
     '+' => '', # contains irssi's cut buffer
     '*' => '', # same
     '_' => '', # black hole register, always empty
    };

# index into the history list (for j,k)
my $history_index = undef;
# current line, necessary for j,k or the current input line gets destroyed
my $history_input = undef;
# position in input line
my $history_pos = 0;

# Undo/redo buffer.

my @undo_buffer;
my $undo_index = undef;

# tab completion state vars

my @tab_candidates;
my $completion_active = 0;
my $completion_string = '';

sub script_is_loaded {
    return exists($Irssi::Script::{shift(@_) . '::'});
}




# INSERT MODE COMMANDS

sub insert_ctrl_r {
    my ($key) = @_;
    _debug("ctrl-r called");
    my $char = chr($key);
    _debug("ctrl-r called with $char");

    return if not defined $registers->{$char} or $registers->{$char} eq '';

    my $pos = _insert_at_position($registers->{$char}, 1, _input_pos());
    _input_pos($pos + 1);
}


# COMMAND MODE OPERATORS

sub cmd_operator_c {
    my ($old_pos, $new_pos, $move_cmd, $repeat) = @_;

    # Changing a word or WORD doesn't delete the last space before a word, but
    # not if we are on that whitespace before the word.
    if ($move_cmd and ($move_cmd == $commands->{w} or
                       $move_cmd == $commands->{W})) {
        my $input = _input();
        if ($new_pos - $old_pos > 1 and
                substr($input, $new_pos - 1, 1) =~ /\s/) {
            $new_pos--;
        }
    }

    cmd_operator_d($old_pos, $new_pos, $move_cmd, $repeat, 1);

    if (!$repeat) {
        _update_mode(M_INS);
    } else {
        my $pos = _input_pos();
        $pos = _insert_buffer(1, $pos);
        _input_pos($pos);
    }
}

sub cmd_operator_d {
    my ($old_pos, $new_pos, $move_cmd, $repeat, $change) = @_;

    my ($pos, $length) = _get_pos_and_length($old_pos, $new_pos, $move_cmd);

    # Remove the selected string from the input.
    my $input = _input();
    my $string = substr $input, $pos, $length, '';
    if ($register =~ /[A-Z]/) {
        $registers->{lc $register} .= $string;
        print "Deleted into $register: ", $registers->{lc $register} if DEBUG;
    } else {
        $registers->{$register} = $string;
        print "Deleted into $register: ", $registers->{$register} if DEBUG;
    }
    _input($input);

    # Prevent moving after the text when we delete the last character. But not
    # when changing (C).
    $pos-- if $pos == length($input) and !$change;

    _input_pos($pos);
}

sub cmd_operator_y {
    my ($old_pos, $new_pos, $move_cmd, $repeat) = @_;

    my ($pos, $length) = _get_pos_and_length($old_pos, $new_pos, $move_cmd);

    # Extract the selected string and put it in the " register.
    my $input = _input();
    my $string = substr $input, $pos, $length;
    if ($register =~ /[A-Z]/) {
        $registers->{lc $register} .= $string;
        print "Yanked into $register: ", $registers->{lc $register} if DEBUG;
    } else {
        $registers->{$register} = $string;
        print "Yanked into $register: ", $registers->{$register} if DEBUG;
        if ($register eq '"') {
            $registers->{0} = $string;
            print "Yanked into 0: ", $registers->{0} if DEBUG;
        }
    }

    # Always move to the lower position.
    if ($old_pos > $new_pos) {
        _input_pos($new_pos);
    } else {
        _input_pos($old_pos);
    }
}

sub _get_pos_and_length {
    my ($old_pos, $new_pos, $move_cmd) = @_;

    my $length = $new_pos - $old_pos;
    # We need a positive length and $old_pos must be smaller.
    if ($length < 0) {
        $old_pos = $new_pos;
        $length *= -1;
    }

    # Some commands don't move one character after the deletion area which is
    # necessary for all commands moving to the right. Fix it.
    if ($move_cmd->{selection_needs_move_right}) {
        $length += 1;
    }

    return ($old_pos, $length);
}

# COMMAND MODE COMMANDS

sub cmd_h {
    my ($count, $pos, $repeat) = @_;

    $pos -= $count;
    $pos = 0 if $pos < 0;
    return (undef, $pos);
}

sub cmd_l {
    my ($count, $pos, $repeat) = @_;

    my $length = _input_len();
    $pos += $count;
    $pos = _fix_input_pos($pos, $length);
    return (undef, $pos);
}

# later history (down)
sub cmd_j {
    my ($count, $pos, $repeat) = @_;

    if (Irssi::version < 20090117) {
        # simulate a down-arrow
        _emulate_keystrokes(0x1b, 0x5b, 0x42);
        return (undef, undef);
    }

    my @history = Irssi::active_win->get_history_lines();

    if (defined $history_index) {
        $history_index += $count;
        print "History Index: $history_index" if DEBUG;
    # Prevent destroying the current input when pressing j after entering
    # command mode. Not exactly like in default irssi, but simplest solution
    # (and S can be used to clear the input line fast, which is what <down>
    # does in plain irssi).
    } else {
        return (undef, undef);
    }

    if ($history_index > $#history) {
        # Restore the input line.
        _input($history_input);
        _input_pos($history_pos);
        $history_index = $#history + 1;
    } elsif ($history_index >= 0) {
        my $history = $history[$history_index];
        # History is not in UTF-8!
        if ($settings->{utf8}->{value}) {
            $history = decode_utf8($history);
        }
        _input($history);
        _input_pos(0);
    }
    return (undef, undef);
}

# earlier history (up)
sub cmd_k {
    my ($count, $pos, $repeat) = @_;

    if (Irssi::version < 20090117) {
        # simulate an up-arrow
        _emulate_keystrokes(0x1b, 0x5b, 0x41);
        return (undef, undef);
    }

    my @history = Irssi::active_win->get_history_lines();

    if (defined $history_index) {
        $history_index -= $count;
        $history_index = 0 if $history_index < 0;
    } else {
        $history_index = $#history;
        $history_input = _input();
        $history_pos = _input_pos();
    }
    print "History Index: $history_index" if DEBUG;
    if ($history_index >= 0) {
        my $history = $history[$history_index];
        # History is not in UTF-8!
        if ($settings->{utf8}->{value}) {
            $history = decode_utf8($history);
        }
        _input($history);
        _input_pos(0);
    }
    return (undef, undef);
}

sub cmd_G {
    my ($count, $pos, $repeat) = @_;

    if (Irssi::version < 20090117) {
        _warn("G and gg not supported in irssi < 0.8.13");
        return;
    }

    my @history = Irssi::active_win->get_history_lines();

    # Go to the current input line if no count was given or it's too big.
    if (not $count or $count - 1 >= scalar @history) {
        if (defined $history_input and defined $history_pos) {
            _input($history_input);
            _input_pos($history_pos);
            $history_index = undef;
        }
        return;
    } else {
        # Save input line so it doesn't get lost.
        if (not defined $history_index) {
            $history_input = _input();
            $history_pos = _input_pos();
        }
        $history_index = $count - 1;
    }

    my $history = $history[$history_index];
    # History is not in UTF-8!
    if ($settings->{utf8}->{value}) {
        $history = decode_utf8($history);
    }
    _input($history);
    _input_pos(0);

    return (undef, undef);
}

sub cmd_gg {
    my ($count, $pos, $repeat) = @_;

    return cmd_G(1, $pos, $repeat);
}

sub cmd_f {
    my ($count, $pos, $repeat, $char) = @_;

    $pos = _next_occurrence(_input(), $char, $count, $pos);

    $last_ftFT = { type => 'f', char => $char };
    return (undef, $pos);
}

sub cmd_t {
    my ($count, $pos, $repeat, $char) = @_;

    $pos = _next_occurrence(_input(), $char, $count, $pos);
    if (defined $pos) {
        $pos--;
    }

    $last_ftFT = { type => 't', char => $char };
    return (undef, $pos);
}

sub cmd_F {
    my ($count, $pos, $repeat, $char) = @_;

    my $input = reverse _input();
    $pos = _next_occurrence($input, $char, $count, length($input) - $pos - 1);
    if (defined $pos) {
        $pos = length($input) - $pos - 1;
    }

    $last_ftFT = { type => 'F', char => $char };
    return (undef, $pos);
}

sub cmd_T {
    my ($count, $pos, $repeat, $char) = @_;

    my $input = reverse _input();
    $pos = _next_occurrence($input, $char, $count, length($input) - $pos - 1);
    if (defined $pos) {
        $pos = length($input) - $pos - 1 + 1;
    }

    $last_ftFT = { type => 'T', char => $char };
    return (undef, $pos);
}

# Find $count-th next occurrence of $char.
sub _next_occurrence {
    my ($input, $char, $count, $pos) = @_;

    while ($count-- > 0) {
        $pos = index $input, $char, $pos + 1;
        if ($pos == -1) {
            return undef;
        }
    }
    return $pos;
}

sub cmd_semicolon {
    my ($count, $pos, $repeat) = @_;

    return (undef, undef) if not defined $last_ftFT->{type};

    (undef, $pos)
        = $commands->{$last_ftFT->{type}}
                   ->{func}($count, $pos, $repeat, $last_ftFT->{char});
    return (undef, $pos);
}

sub cmd_comma {
    my ($count, $pos, $repeat) = @_;

    return (undef, undef) if not defined $last_ftFT->{type};

    # Change direction.
    my $save = $last_ftFT->{type};
    my $type = $save;
    $type =~ tr/ftFT/FTft/;

    (undef, $pos)
        = $commands->{$type}
                   ->{func}($count, $pos, $repeat, $last_ftFT->{char});
    # Restore type as the move functions overwrites it.
    $last_ftFT->{type} = $save;
    return (undef, $pos);
}

sub cmd_w {
    my ($count, $pos, $repeat) = @_;

    my $input = _input();
    $pos = _beginning_of_word($input, $count, $pos);
    $pos = _fix_input_pos($pos, length $input);
    return (undef, $pos);
}

sub cmd_b {
    my ($count, $pos, $repeat) = @_;

    my $input = reverse _input();
    $pos = length($input) - $pos - 1;
    $pos = 0 if ($pos < 0);

    $pos = _end_of_word($input, $count, $pos);
    $pos = length($input) - $pos - 1;
    $pos = 0 if ($pos < 0);
    return (undef, $pos);
}

sub cmd_e {
    my ($count, $pos, $repeat) = @_;

    my $input = _input();
    $pos = _end_of_word($input, $count, $pos);
    $pos = _fix_input_pos($pos, length $input);
    return (undef, $pos);
}

sub cmd_ge {
    my ($count, $pos, $repeat, $char) = @_;

    my $input = reverse _input();
    $pos = length($input) - $pos - 1;
    $pos = 0 if ($pos < 0);

    $pos = _beginning_of_word($input, $count, $pos);
    $pos = length($input) - $pos - 1;
    $pos = 0 if ($pos < 0);

    return (undef, $pos);
}

# Go to the beginning of $count-th word, like vi's w.
sub _beginning_of_word {
    my ($input, $count, $pos) = @_;

    while ($count-- > 0) {
        # Go to end of next word/non-word.
        if (substr($input, $pos) =~ /^$word+/ or
            substr($input, $pos) =~ /^$non_word+/) {
            $pos += $+[0];
        }
        # And skip over any whitespace if necessary. This also happens when
        # we're inside whitespace.
        if (substr($input, $pos) =~ /^\s+/) {
            $pos += $+[0];
        }
    }

    return $pos;
}

# Go to the end of $count-th word, like vi's e.
sub _end_of_word {
    my ($input, $count, $pos) = @_;

    while ($count-- > 0 and length($input) > $pos) {
        my $skipped = 0;
        # Skip over whitespace if in the middle of it or directly after the
        # current word/non-word.
        if (substr($input, $pos + 1) =~ /^\s+/) {
            $pos += $+[0] + 1;
            $skipped = 1;
        }
        elsif (substr($input, $pos) =~ /^\s+/) {
            $pos += $+[0];
            $skipped = 1;
        }
        # We are inside a word/non-word, skip to the end of it.
        if (substr($input, $pos) =~ /^$word{2,}/ or
            substr($input, $pos) =~ /^$non_word{2,}/) {
            $pos += $+[0] - 1;
        # We are the border of word/non-word. Skip to the end of the next one.
        # But not if we've already jumped over whitespace because there could
        # be only one word/non-word char after the whitespace.
        } elsif (!$skipped and (substr($input, $pos) =~ /^$word($non_word+)/ or
                                substr($input, $pos) =~ /^$non_word($word+)/)) {
            $pos += $+[0] - 1;
        }
    }

    # Necessary for correct deletion at the end of the line.
    if (length $input == $pos + 1) {
        $pos++;
    }

    return $pos;
}

sub cmd_W {
    my ($count, $pos, $repeat) = @_;

    my $input = _input();
    $pos = _beginning_of_WORD($input, $count, $pos);
    $pos = _fix_input_pos($pos, length $input);
    return (undef, $pos);
}

sub cmd_B {
    my ($count, $pos, $repeat) = @_;

    my $input = reverse _input();
    $pos = _end_of_WORD($input, $count, length($input) - $pos - 1);
    if ($pos == -1) {
        return cmd_0();
    } else {
        return (undef, length($input) - $pos - 1);
    }
}

sub cmd_E {
    my ($count, $pos, $repeat) = @_;

    $pos = _end_of_WORD(_input(), $count, $pos);
    if ($pos == -1) {
        return cmd_dollar();
    } else {
        return (undef, $pos);
    }
}

sub cmd_gE {
    my ($count, $pos, $repeat, $char) = @_;

    my $input = reverse _input();
    $pos = _beginning_of_WORD($input, $count, length($input) - $pos - 1);
    if ($pos == -1 or length($input) - $pos - 1 == -1) {
        return cmd_0();
    } else {
        $pos = length($input) - $pos - 1;
    }

    return (undef, $pos);
}

# Go to beginning of $count-th WORD, like vi's W.
sub _beginning_of_WORD {
    my ($input, $count, $pos) = @_;

    # Necessary for correct movement between two words with only one
    # whitespace.
    if (substr($input, $pos) =~ /^\s\S/) {
        $pos++;
        $count--;
    }

    while ($count-- > 0 and length($input) > $pos) {
        if (substr($input, $pos + 1) !~ /\s+/) {
            return length($input);
        }
        $pos += $+[0] + 1;
    }

    return $pos;
}

# Go to end of $count-th WORD, like vi's E.
sub _end_of_WORD {
    my ($input, $count, $pos) = @_;

    return $pos if $pos >= length($input);

    # We are inside a WORD, skip to the end of it.
    if (substr($input, $pos + 1) =~ /^\S+(\s)/) {
        $pos += $-[1];
        $count--;
    }

    while ($count-- > 0) {
        if (substr($input, $pos) !~ /\s+\S+(\s+)/) {
            return -1;
        }
        $pos += $-[1] - 1;
    }
    return $pos;
}

sub cmd__i {
    my ($count, $pos, $repeat, $char) = @_;

    _warn("i_ not implemented yet");
    return (undef, undef);
}

sub cmd__a {
    my ($count, $pos, $repeat, $char) = @_;

    my $cur_pos;
    my $input = _input();

    # aw and aW
    if ($char eq 'w' or $char eq 'W') {
        while ($count-- > 0 and length($input) > $pos) {
            if (substr($input, $pos, 1) =~ /\s/) {
                # Any whitespace before the word/WORD must be removed.
                if (not defined $cur_pos) {
                    $cur_pos = _find_regex_before($input, '\S', $pos, 0);
                    if ($cur_pos < 0) {
                        $cur_pos = 0;
                    } else {
                        $cur_pos++;
                    }
                }
                # Move before the word/WORD.
                if (substr($input, $pos + 1) =~ /^\s+/) {
                    $pos += $+[0];
                }
                # And delete the word.
                if ($char eq 'w') {
                    if (substr($input, $pos) =~ /^\s($word+|$non_word+)/) {
                        $pos += $+[0];
                    } else {
                        $pos = length($input);
                    }
                # WORD
                } else {
                    if (substr($input, $pos + 1) =~ /\s/) {
                        $pos += $-[0] + 1;
                    } else {
                        $pos = length($input);
                    }
                }

            # word
            } elsif ($char eq 'w') {
                # Start at the beginning of this WORD.
                if (not defined $cur_pos and $pos > 0
                    and substr($input, $pos - 1, 2)
                    !~ /(\s.|$word$non_word|$non_word$word)/) {

                    $cur_pos = _find_regex_before
                      (
                       $input,
                       "^($word+$non_word|$non_word+$word|$word+\\s|$non_word+\\s)",
                       $pos, 1
                      );

                    if ($cur_pos < 0) {
                        $cur_pos = 0;
                    } else {
                        $cur_pos += 2;
                    }
                }
                # Delete to the end of the word.
                if (substr($input, $pos) =~
                    /^($word+$non_word|$non_word+$word|$word+\s+\S|$non_word+\s+\S)/) {

                    $pos += $+[0] - 1;
                } else {
                    $pos = length($input);
                    # If we are at the end of the line, whitespace before
                    # the word is also deleted.
                    my $new_pos = _find_regex_before
                      ($input,
                       "^($word+\\s+|$non_word+\\s+)",
                       $pos, 1);

                    if ($new_pos != -1 and
                        (not defined $cur_pos or
                         $cur_pos > $new_pos + 1)) {

                        $cur_pos = $new_pos + 1;
                    }
                }

            # WORD
            } else {
                # Start at the beginning of this WORD.
                if (not defined $cur_pos and $pos > 0 and
                        substr($input, $pos - 1, 1) !~ /\s/) {
                    $cur_pos = _find_regex_before($input, '\s', $pos - 1, 0);
                    if ($cur_pos < 0) {
                        $cur_pos = 0;
                    } else {
                        $cur_pos++;
                    }
                }
                # Delete to the end of the word.
                if (substr($input, $pos + 1) =~ /^\S*\s+\S/) {
                    $pos += $+[0];
                } else {
                    $pos = length($input);
                    # If we are at the end of the line, whitespace before
                    # the WORD is also deleted.
                    my $new_pos = _find_regex_before($input, '\s+', $pos, 1);
                    if (not defined $cur_pos or $cur_pos > $new_pos + 1) {
                        $cur_pos = $new_pos + 1;
                    }
                }
            }
        }
    }

    return ($cur_pos, $pos);
}

# Find regex as close as possible before the current position. If $end is true
# the end of the match is returned, otherwise the beginning.
sub _find_regex_before {
    my ($input, $regex, $pos, $end) = @_;

    $input = reverse $input;
    $pos = length($input) - $pos - 1;
    $pos = 0 if $pos < 0;

    if (substr($input, $pos) =~ /$regex/) {
        if (!$end) {
            $pos += $-[0];
        } else {
            $pos += $+[0];
        }
        return length($input) - $pos - 1;
    } else {
        return -1;
    }
}

sub cmd_0 {
    return (undef, 0);
}

sub cmd_caret {
    my $input = _input();
    my $pos;
    # No whitespace at all.
    if ($input !~ m/^\s/) {
        $pos = 0;
    # Some non-whitespace, go to first one.
    } elsif ($input =~ m/[^\s]/) {
        $pos = $-[0];
    # Only whitespace, go to the end.
    } else {
        $pos = _fix_input_pos(length $input, length $input);
    }
    return (undef, $pos);
}

sub cmd_dollar {
    my $length = _input_len();
    return (undef, _fix_input_pos($length, $length));
}

sub cmd_x {
    my ($count, $pos, $repeat) = @_;

    cmd_operator_d($pos, $pos + $count, $commands->{x}, $repeat);
    return (undef, undef);
}

sub cmd_X {
    my ($count, $pos, $repeat) = @_;

    return (undef, undef) if $pos == 0;

    my $new = $pos - $count;
    $new = 0 if $new < 0;
    cmd_operator_d($pos, $new, $commands->{X}, $repeat);
    return (undef, undef);
}

sub cmd_s {
    my ($count, $pos, $repeat) = @_;

    $operator = $commands->{c};
    return (undef, $pos + $count);
}

sub cmd_S {
    my ($count, $pos, $repeat) = @_;

    $operator = $commands->{c};
    return (0, _input_len());
}

sub cmd_i {
    my ($count, $pos, $repeat) = @_;

    if (!$repeat) {
        _update_mode(M_INS);
    } else {
        $pos = _insert_buffer($count, $pos);
    }
    return (undef, $pos);
}

sub cmd_I {
    my ($count, $pos, $repeat) = @_;

    $pos = cmd_caret();
    if (!$repeat) {
        _update_mode(M_INS);
    } else {
        $pos = _insert_buffer($count, $pos);
    }
    return (undef, $pos);
}

sub cmd_a {
    my ($count, $pos, $repeat) = @_;

    # Move after current character. Can't use cmd_l() because we need to mover
    # after last character at the end of the line.
    my $length = _input_len();
    $pos += 1;
    $pos = $length if $pos > $length;

    if (!$repeat) {
        _update_mode(M_INS);
    } else {
        $pos = _insert_buffer($count, $pos);
    }
    return (undef, $pos);
}

sub cmd_A {
    my ($count, $pos, $repeat) = @_;

    $pos = _input_len();

    if (!$repeat) {
        _update_mode(M_INS);
    } else {
        $pos = _insert_buffer($count, $pos);
    }
    return (undef, $pos);
}

# Add @insert_buf to _input() at the given position.
sub _insert_buffer {
    my ($count, $pos) = @_;
    return _insert_at_position(join('', @insert_buf), $count, $pos);
}

sub _insert_at_position {
    my ($string, $count, $pos) = @_;

    $string = $string x $count;

    my $input = _input();
    # Check if we are not at the end of the line to prevent substr outside of
    # string error.
    if (length $input > $pos) {
        substr($input, $pos, 0) = $string;
    } else {
        $input .= $string;
    }
    _input($input);

    return $pos - 1 + length $string;
}

sub cmd_r {
    my ($count, $pos, $repeat, $char) = @_;

    my $input = _input();

    # Abort if at end of the line.
    return (undef, undef) if length($input) < $pos + $count;

    substr $input, $pos, $count, $char x $count;
    _input($input);
    return (undef, $pos + $count - 1);
}

sub cmd_p {
    my ($count, $pos, $repeat) = @_;
    $pos = _paste_at_position($count, $pos + 1);
    return (undef, $pos);
}

sub cmd_P {
    my ($count, $pos, $repeat) = @_;
    $pos = _paste_at_position($count, $pos);
    return (undef, $pos);
}

sub _paste_at_position {
    my ($count, $pos) = @_;

    return if $registers->{$register} eq '';
    return _insert_at_position($registers->{$register}, $count, $pos);
}

sub cmd_C {
    my ($count, $pos, $repeat) = @_;

    $operator = $commands->{c};
    return (undef, _input_len());
}

sub cmd_D {
    my ($count, $pos, $repeat) = @_;

    $operator = $commands->{d};
    return (undef, _input_len());
}

sub cmd_ctrl_d {
    my ($count, $pos, $repeat) = @_;

    my $window = Irssi::active_win();
    # no count = half of screen
    if (not defined $count) {
        $count = $window->{height} / 2;
    }
    $window->view()->scroll($count);

    Irssi::statusbar_items_redraw('more');
    return (undef, undef);
}

sub cmd_ctrl_u {
    my ($count, $pos, $repeat) = @_;

    my $window = Irssi::active_win();
    # no count = half of screen
    if (not defined $count) {
        $count = $window->{height} / 2;
    }
    $window->view()->scroll($count * -1);

    Irssi::statusbar_items_redraw('more');
    return (undef, undef);
}

sub cmd_ctrl_f {
    my ($count, $pos, $repeat) = @_;

    my $window = Irssi::active_win();
    $window->view()->scroll($count * $window->{height});

    Irssi::statusbar_items_redraw('more');
    return (undef, undef);
}

sub cmd_ctrl_b {
    my ($count, $pos, $repeat) = @_;

    return cmd_ctrl_f($count * -1, $pos, $repeat);
}

sub cmd_ctrl_wj {
    my ($count, $pos, $repeat) = @_;

    while ($count-- > 0) {
        Irssi::command('window down');
    }

    return (undef, undef);
}

sub cmd_ctrl_wk {
    my ($count, $pos, $repeat) = @_;

    while ($count-- > 0) {
        Irssi::command('window up');
    }

    return (undef, undef);
}

sub cmd_ctrl_6 {
    # like :b#
    Irssi::command('window last');
    return (undef, undef);
}

sub cmd_tilde {
    my ($count, $pos, $repeat) = @_;

    my $input = _input();
    my $string = substr $input, $pos, $count;
    $string =~ s/(.)/(uc($1) eq $1) ? lc($1) : uc($1)/ge;
    substr $input, $pos, $count, $string;

    _input($input);
    return (undef, _fix_input_pos($pos + $count, length $input));
}

sub cmd_register {
    my ($count, $pos, $repeat, $char) = @_;

    if (not exists $registers->{$char} and not exists $registers->{lc $char}) {
        print "Wrong register $char, ignoring." if DEBUG;
        return (undef, undef);
    }

    # make sure black hole register is always empty
    if ($char eq '_') {
        $registers->{_} = '';
    }

    # + and * contain both irssi's cut-buffer
    if ($char eq '+' or $char eq '*') {
        $registers->{'+'} = Irssi::parse_special('$U');
        $registers->{'*'} = $registers->{'+'};
    }

    $register = $char;
    print "Changing register to $register" if DEBUG;
    return (undef, undef);
}

sub cmd_undo {
    print "Undo!" if DEBUG;

    if ($undo_index != $#undo_buffer) {
        $undo_index++;
        _restore_undo_entry($undo_index);
        print "Undoing entry index: $undo_index of " . scalar(@undo_buffer)
            if DEBUG;
    } else {
        print "No further undo." if DEBUG;
    }
    return (undef, undef);
}

sub cmd_redo {
    print "Redo!" if DEBUG;

    if ($undo_index != 0) {
        $undo_index--;
        print "Undoing entry index: $undo_index of " . scalar(@undo_buffer)
            if DEBUG;
        _restore_undo_entry($undo_index);
    } else {
        print "No further Redo." if DEBUG;
    }
    return (undef, undef);
}

# Adapt the input position depending if an operator is active or not.
sub _fix_input_pos {
    my ($pos, $length) = @_;

    # Allow moving past the last character when an operator is active to allow
    # correct handling of last character in line.
    if ($operator) {
        $pos = $length if $pos > $length;
    # Otherwise forbid it.
    } else {
        $pos = $length - 1 if $pos > $length - 1;
    }

    return $pos;
}


# EX MODE COMMANDS

sub cmd_ex_command {
    my $arg_str = join '', @ex_buf;

    if ($arg_str !~ /^(\d*)?([a-z]+)/) {
        return _warn("Invalid Ex-mode command!");
    }

    # Abort if command doesn't exist or used with count for unsupported
    # commands.
    if (not exists $commands_ex->{$2} or
        ($1 ne '' and not $commands_ex->{$2}->{uses_count})) {
        return _warn("Ex-mode $1$2 doesn't exist!");
    }

    # add this item to the ex mode history
    ex_history_add($arg_str);
    $ex_history_index = 0; # and reset the history position.

    my $count = $1;
    if ($count eq '') {
        $count = undef;
    }
    $commands_ex->{$2}->{func}($arg_str, $count);
}

sub ex_substitute {
    my ($arg_str, $count) = @_;

    # :s///
    if ($arg_str =~ m|^s/(.+)/(.*)/([ig]*)|) {
        my ($search, $replace, $flags) = ($1, $2, $3);
        print "Searching for $search, replace: $replace, flags; $flags"
          if DEBUG;

        my $rep_fun = sub { $replace };

        my $line = _input();
        my @re_flags = split '', defined $flags?$flags:'';

        if (scalar grep { $_ eq 'i' } @re_flags) {
            $search = '(?i)' . $search;
        }

        print "Search is $search" if DEBUG;

        my $re_pattern = qr/$search/;

        if (scalar grep { $_ eq 'g' } @re_flags) {
            $line =~ s/$re_pattern/$rep_fun->()/eg;
        } else {
            print "Single replace: $replace" if DEBUG;
            $line =~ s/$re_pattern/$rep_fun->()/e;
        }

        print "New line is: $line" if DEBUG;
        _input($line);
    } else {
        _warn_ex('s');
    }
}

sub ex_bnext {
    my ($arg_str, $count) = @_;

    if (not defined $count) {
        if ($arg_str =~ /^bn(?:ext)?\s(\d+)$/) {
            $count = $1;
        } else {
            $count = 1;
        }
    }

    while ($count-- > 0) {
        Irssi::command('window next');
    }
}

sub ex_bprev {
    my ($arg_str, $count) = @_;

    if (not defined $count) {
        if ($arg_str =~ /^bp(?:rev)?\s(\d+)$/) {
            $count = $1;
        } else {
            $count = 1;
        }
    }

    while ($count-- > 0) {
        Irssi::command('window previous');
    }
}

sub ex_bdelete {
    my ($arg_str, $count) = @_;

    if (not defined $count) {
        if ($arg_str =~ /^bd(?:elete)?\s(\d+)$/) {
            $count = $1;
        }
    }

    if (defined $count) {
        my $window = Irssi::window_find_refnum($count);
        if (not $window) {
            return;
        }
        $window->set_active();
    }
    Irssi::command('window close');
}

sub ex_buffer {
    my ($arg_str, $count) = @_;

    # :b[buffer] {args}
    if ($arg_str =~ m|^b(?:uffer)?\s*(.+)$| or defined $count) {
        my $window;
        my $item;
        my $buffer = $1;

        # :[N]:b[uffer]
        if (defined $count) {
            $window = Irssi::window_find_refnum($count);
        # Go to window number.
        } elsif ($buffer =~ /^[0-9]+$/) {
            $window = Irssi::window_find_refnum($buffer);
        # Go to previous window.
        } elsif ($buffer eq '#') {
            Irssi::command('window last');
        # Go to best regex matching window.
        } else {
            eval {
                my $matches = _matching_windows($buffer);
                if (scalar @$matches > 0) {
                    $window = @$matches[0]->{window};
                    $item = @$matches[0]->{item};
                }
            };
            # Catch errors in /$buffer/ regex.
            if ($@) {
                _warn($@);
            }
        }

        if ($window) {
            $window->set_active();
            if ($item) {
                $item->set_active();
            }
        }
    } else {
        _warn_ex('buffer');
    }
}

sub ex_item_next {
    my ($arg_str, $count) = @_;
    my $win = Irssi::active_win;
    $count = 1 unless defined $count;

    $win->item_next for (1..$count);
}

sub ex_item_prev {
    my ($arg_str, $count) = @_;
    my $win = Irssi::active_win;
    $count = 1 unless defined $count;

    $win->item_prev for (1..$count);
}

# TODO: factor out the shared search code for server next/prev.
sub ex_server_next {
    my ($arg_str, $count) = @_;

    my @server_ids = map { $_->{tag} . "\x1d" . $_->{nick} } Irssi::servers;
    my $server = Irssi::active_server;

    return unless $server;

    my $current_id = $server->{tag} . "\x1d" . $server->{nick};
    my $next = 0;
    my $server_count = scalar @server_ids;
    for my $i (0..$server_count - 1) {
        my $s_id = $server_ids[$i];
        if (defined($s_id) and ($s_id eq $current_id)) {
            print "Found match at $i" if DEBUG;
            $next = ($i + 1) % $server_count;
            last;
        }
    }

    my $next_server = $server_ids[$next];
    $next_server =~ s|^([^\x1d]+)\x1d.*$|$1|;

    print "Changing to server: $next: $next_server" if DEBUG;
    Irssi::command("window server $next_server");
}

sub ex_server_prev {
    my ($arg_str, $count) = @_;

    my @server_ids = map { $_->{tag} . "\x1d" . $_->{nick} } Irssi::servers;
    my $server = Irssi::active_server;

    return unless $server;

    my $current_id = $server->{tag} . "\x1d" . $server->{nick};
    my $prev = 0;
    my $server_count = scalar @server_ids;

    for my $i (0..$server_count - 1) {
        my $s_id = $server_ids[$i];
        if (defined($s_id) and ($s_id eq $current_id)) {
            print "Found match at $i" if DEBUG;
            $prev = ($i - 1) % $server_count;
            last;
        }
    }

    my $prev_server = $server_ids[$prev];
    $prev_server =~ s|^([^\x1d]+)\x1d.*$|$1|;

    print "Changing to server: $prev: $prev_server" if DEBUG;
    Irssi::command("window server $prev_server");

}

sub ex_registers {
    my ($arg_str, $count) = @_;

    # :reg[isters] {arg} and :di[splay] {arg}
    if ($arg_str =~ /^(?:reg(?:isters)?|di(?:splay)?)(?:\s+(.+)$)?/) {
        my @regs;
        if ($1) {
            my $regs = $1;
            $regs =~ s/\s+//g;
            @regs = split //, $regs;
        } else {
            @regs = keys %$registers;
        }

        # Update "+ and "* registers so correct values are displayed.
        $registers->{'+'} = Irssi::parse_special('$U');
        $registers->{'*'} = $registers->{'+'};

        my @empty_regs;
        my $special_regs = { '+' => 1, '*' => 1, '_' => 1, '"' => 1, '0' => 1 };

        my $active_window = Irssi::active_win;
        foreach my $key (sort @regs) {
            next if $key eq '_'; # skip black hole
            if (defined $registers->{$key}) {
                my $register_val = $registers->{$key};
                if (length $register_val or exists $special_regs->{$key}) {
                    $register_val =~ s/%/%%/g;
                    $active_window->print("register $key: $register_val");
                } else {
                    push @empty_regs, $key;
                }
            }
        }

        # coalesce empty registers into a single line.
        if (@empty_regs) {
            my @runs;
            my $run_start;
            foreach my $i (0..$#empty_regs) {
                my $cur  = $empty_regs[$i];
                my $next = $empty_regs[$i+1];

                $run_start = $cur unless $run_start;
                if (defined $next and ord($cur) + 1 == ord($next)) {
                    # extend range.
                } else {
                    # terminate range and restart
                    my $run_str = $run_start;

                    if ($cur ne $run_start) {
                        $run_str .= "-$cur";
                    }
                    push @runs, $run_str;
                    $run_start = undef;
                }
            }
            $active_window->print("Empty registers: " . join(', ', @runs));
        }
    } else {
        _warn_ex(':registers');
    }
}

sub ex_buffers {
    my ($arg_str, $count) = @_;

    Irssi::command('window list');
}

sub ex_undolist {
    my ($arg_str, $count) = @_;

    _print_undo_buffer();
}

sub ex_map {
    my ($arg_str, $count) = @_;

    # :map {lhs} {rhs}
    if ($arg_str =~ /^map (\S+) (\S.*)$/) {
        my $lhs = _parse_mapping($1);
        my $rhs = $2;

        if (not defined $lhs) {
            return _warn_ex('map', 'invalid {lhs}');
        }

        # Add new mapping.
        my $command;
        # Ex-mode command
        if (index($rhs, ':') == 0) {
            $rhs =~ /^:(\S+)(\s.+)?$/;
            if (not exists $commands_ex->{$1}) {
                return _warn_ex('map', "$rhs not found");
            } else {
                $command = { char => $rhs,
                             func => $commands_ex->{$1}->{func},
                             type => C_EX,
                           };
            }
        # Irssi command
        } elsif (index($rhs, '/') == 0) {
            $command = { char => $rhs,
                         func => substr($rhs, 1),
                         type => C_IRSSI,
                       };
        # <Nop> does nothing
        } elsif (lc $rhs eq '<nop>') {
            $command = { char => '<Nop>',
                         func => undef,
                         type => C_NOP,
                       };
        # command-mode command
        } else {
            $rhs = _parse_mapping($2);
            if (not defined $rhs) {
                return _warn_ex('map', 'invalid {rhs}');
            } elsif (not exists $commands->{$rhs}) {
                return _warn_ex('map', "$2 not found");
            } else {
                $command = $commands->{$rhs};
            }
        }
        add_map($lhs, $command);

    # :map [lhs]
    } elsif ($arg_str =~ m/^map\s*$/ or $arg_str =~ m/^map (\S+)$/) {
        # Necessary for case insensitive matchings. lc alone won't work.
        my $search = $1;
        $search = '' if not defined $search;
        $search = _parse_mapping_reverse(_parse_mapping($search));

        my $active_window = Irssi::active_win();
        foreach my $key (sort keys %$maps) {
            my $map = $maps->{$key};
            my $cmd = $map->{cmd};
            if (defined $cmd) {
                next if $map->{char} eq $cmd->{char}; # skip default mappings
                # FIXME: Hack so <C-H> doesn't show up as mapped to <BS>.
                next if $map->{char} eq '<C-H>' and $cmd->{char} eq '<BS>';
                next if $map->{char} !~ /^\Q$search\E/; # skip non-matches
                $active_window->print(sprintf "%-15s %s", $map->{char},
                                                          $cmd->{char});
            }
        }
    } else {
        _warn_ex('map');
    }
}
sub ex_unmap {
    my ($arg_str, $count) = @_;

    # :unm[ap] {lhs}
    if ($arg_str !~ /^unm(?:ap)? (\S+)$/) {
        return _warn_ex('unmap');
    }

    my $lhs = _parse_mapping($1);
    if (not defined $lhs) {
        return _warn_ex('unmap', 'invalid {lhs}');
    # Prevent unmapping of unknown or default mappings.
    } elsif (not exists $maps->{$lhs} or not defined $maps->{$lhs}->{cmd} or
             ($commands->{$lhs} and $maps->{$lhs}->{cmd} == $commands->{$lhs})) {
        return _warn_ex('unmap', "$1 not found");
    }

    delete_map($lhs);
}
sub _parse_mapping {
    my ($string) = @_;

    $string =~ s/<([^>]+)>/_parse_mapping_bracket($1)/ge;
    _debug("Parse mapping: $string");
    if (index($string, '<invalid>') != -1) {
        return undef;
    }
    return $string;
}
sub _parse_mapping_bracket {
    my ($string) = @_;

    $string = lc $string;

    # <C-X>, get corresponding CTRL char.
    if ($string =~ /^c-([a-z])$/i) {
        $string = chr(ord($1) - 96);
    # <C-6> and <C-^>
    } elsif ($string =~ /^c-[6^]$/i) {
        $string = chr(30);
    # <Space>
    } elsif ($string eq 'space') {
        $string = ' ';
    # <CR>
    } elsif ($string eq 'cr') {
        $string = "\n";
    # <BS>
    } elsif ($string eq 'bs') {
        $string = chr(127);
    } elsif ($string eq 'leader') {
        $string = $settings->{map_leader}->{value};
    # Invalid char, return special string to recognize the error.
    } else {
        $string = '<invalid>';
    }
    return $string;
}
sub _parse_mapping_reverse {
    my ($string) = @_;

    if (not defined $string) {
        _warn("Unable to reverse-map command: " . join('', @ex_buf));
        return;
    }

    my $escaped_leader = quotemeta($settings->{map_leader}->{value});
    $string =~ s/$escaped_leader/<Leader>/g;

    # Convert char to <char-name>.
    $string =~ s/ /<Space>/g;
    $string =~ s/\n/<CR>/g;
    $string =~ s/\x7F/<BS>/g;
    # Convert Ctrl-X to <C-X>.
    $string =~ s/([\x01-\x1A])/"<C-" . chr(ord($1) + 64) . ">"/ge;
    # Convert Ctrl-6 and Ctrl-^ to <C-^>.
    $string =~ s/\x1E/<C-^>/g;

    return $string;
}
sub _parse_partial_command_reverse {
    my ($string) = @_;

    my $escaped_leader = quotemeta($settings->{map_leader}->{value});
    $string =~ s/$escaped_leader/<Leader>/g;

    # Convert Ctrl-X to ^X.
    $string =~ s/([\x01-\x1A])/"^" . chr(ord($1) + 64)/ge;
    # Convert Ctrl-6 and Ctrl-^ to <C-^>.
    $string =~ s/\x1E/^^/g;

    return $string;
}

sub ex_source {
    my ($arg_str, $count) = @_;

    # :so[urce], but only loads the vim_moderc file at the moment

    open my $file, '<', Irssi::get_irssi_dir() . '/vim_moderc' or return;

    while (my $line = <$file>) {
        next if $line =~ /^\s*$/ or $line =~ /^\s*"/;

        chomp $line;
        # :map {lhs} {rhs}, keep in sync with ex_map()
        if ($line =~ /^\s*map (\S+) (\S.*)$/) {
            ex_map($line);
        } else {
            _warn_ex('source', "command not supported: $line");
        }
    }
}

sub ex_mkvimrc {
    my ($arg_str, $count) = @_;

    # :mkv[imrc][!], [file] not supported

    my $vim_moderc = Irssi::get_irssi_dir(). '/vim_moderc';
    if (-f $vim_moderc and $arg_str !~ /^mkv(?:imrc)?!$/) {
        return _warn_ex('mkvimrc', "$vim_moderc already exists");
    }

    open my $file, '>', $vim_moderc or return;

    # copied from ex_map()
    foreach my $key (sort keys %$maps) {
        my $map = $maps->{$key};
        my $cmd = $map->{cmd};
        if (defined $cmd) {
            next if $map->{char} eq $cmd->{char}; # skip default mappings
            print $file "map $map->{char} $cmd->{char}\n";
        }
    }

    close $file;
}

sub ex_set {
    my ($arg_str, $count) = @_;

    # :se[t] [option] [value]
    if ($arg_str =~ /^se(?:t)?(?:\s([^=]+)(?:=(.*)$)?)?/) {
        # :se[t] {option} {value}
        if (defined $1 and defined $2) {
            if (not exists $settings->{$1}) {
                return _warn_ex('map', "setting '$1' not found");
            }
            my $name = $1;
            my $value = $2;
            # Also accept numeric values for boolean options.
            if ($settings->{$name}->{type} == S_BOOL) {
                if ($value =~ /^(on|off)$/i) {
                    $value = lc $value eq 'on' ? 1 : 0;
                } elsif ($value eq '') {
                    $value = 0;
                }
            }
            _setting_set($name, $value);
            setup_changed();

        # :se[t] [option]
        } else {
            my $search = defined $1 ? $1 : '';
            my $active_window = Irssi::active_win();
            foreach my $setting (sort keys %$settings) {
                next if $setting !~ /^\Q$search\E/; # skip non-matches
                my $value = $settings->{$setting}->{value};
                # Irssi only accepts 'on' and 'off' as values for boolean
                # options.
                if ($settings->{$setting}->{type} == S_BOOL) {
                    $value = $value ? 'on' : 'off';
                }
                $active_window->print($setting . '=' . $value);
            }
        }
    } else {
        _warn_ex('map');
    }
}

sub _warn_ex {
    my ($command, $description) = @_;
    my $message = "Error in ex-mode command $command";
    if (defined $description) {
        $message .= ": $description";
    }
    _warn($message);
}

sub _matching_windows {
    my ($buffer) = @_;

    my $server;

    if ($buffer =~ m{^(.+)/(.+)}) {
        $server = $1;
        $buffer = $2;
    }

    print ":b searching for channel $buffer" if DEBUG;
    print ":b on server $server" if $server and DEBUG;

    my @matches;
    foreach my $window (Irssi::windows()) {
        # Matching window names.
        if ($window->{name} =~ /$buffer/i) {
            my $win_ratio = ($+[0] - $-[0]) / length($window->{name});
            push @matches, { window => $window,
                               item => undef,
                              ratio => $win_ratio,
                               text => $window->{name} };
            print ":b $window->{name}: $win_ratio" if DEBUG;
        }
        # Matching Window item names (= channels).
        foreach my $item ($window->items()) {
            # Wrong server.
            if ($server and (!$item->{server} or
                              $item->{server}->{chatnet} !~ /^$server/i)) {
                next;
            }
            if ($item->{name} =~ /$buffer/i) {
                my $length = length($item->{name});
                $length-- if index($item->{name}, '#') == 0;
                my $item_ratio = ($+[0] - $-[0]) / $length;
                push @matches, { window => $window,
                                   item => $item,
                                  ratio => $item_ratio,
                                   text => $item->{name} };
                print ":b $window->{name} $item->{name}: $item_ratio" if DEBUG;
            }
        }
    }

    @matches = sort {$b->{ratio} <=> $a->{ratio}} @matches;

    return \@matches;
}


# STATUS ITEMS

#TODO: give these things better names.
sub vim_mode_cmd {

    my $mode_str = '';
    if ($mode == M_INS) {
        $mode_str = 'Insert';
    } elsif ($mode == M_EX) {
        $mode_str = '%_Ex%_';
    } else {
        $mode_str = '%_Command%_';
        if ($register ne '"' or $numeric_prefix or $operator or $movement or
            $pending_map) {
            my $partial = '';
            if ($register ne '"') {
                $partial .= '"' . $register;
            }
            if ($numeric_prefix) {
                $partial .= $numeric_prefix;
            }
            if ($operator) {
                $partial .= $operator->{char};
            }
            if ($movement) {
                $partial .= $movement->{char};
            }
            if (defined $pending_map) {
                $partial .= $pending_map;
            }
            $partial = _parse_partial_command_reverse($partial);
            $partial =~ s/\\/\\\\\\\\/g;
            $mode_str .= " ($partial)";
        }
    }
    return $mode_str;
}

sub vim_wins_data {
    my $windows = '';

    # A little code duplication of cmd_ex_command(), but \s+ instead of \s* so
    # :bd doesn't display buffers matching d.
    my $arg_str = join '', @ex_buf;
    if ($arg_str =~ m|^b(?:uffer)?\s+(.+)$|) {
        my $buffer = $1;
        if ($buffer !~ /^[0-9]$/ and $buffer ne '#') {
            # Display matching windows.
            eval {
                my $matches = _matching_windows($buffer);
                $windows = join ',', map { $_->{text} } @$matches;
            };
            # Catch errors in /$buffer/ regex.
            if ($@) {
                _warn($@);
            }
        }
    }
    return $windows;
}

sub vim_exp_mode {
    my ($server, $witem, $arg) = @_;
    return vim_mode_cmd();
}

sub vim_exp_wins {
    my ($server, $witem, $arg) = @_;
    return vim_wins_data();
}

# vi mode status item.
sub vim_mode_cb {
    my ($sb_item, $get_size_only) = @_;
    my $mode_str = vim_mode_cmd();
    $sb_item->default_handler($get_size_only, "{sb $mode_str}", '', 0);
}

# :b window list item.
sub b_windows_cb {
    my ($sb_item, $get_size_only) = @_;

    my $windows = vim_wins_data();

    $sb_item->default_handler($get_size_only, "{sb $windows}", '', 0);
}


# INPUT HANDLING

sub got_key {
    my ($key) = @_;

    return if ($should_ignore);

    # Esc key
    if ($key == 27) {
        print "Esc seen, starting buffer" if DEBUG;
        $input_buf_enabled = 1;

        # NOTE: this timeout might be too low on laggy systems, but
        # it comes at the cost of keystroke latency for things that
        # contain escape sequences (arrow keys, etc)
        my $esc_buf_timeout = $settings->{esc_buf_timeout}->{value};

        $input_buf_timer
          = Irssi::timeout_add_once($esc_buf_timeout,
                                    \&handle_input_buffer, undef);

        print "Buffer Timer tag: $input_buf_timer" if DEBUG;

    } elsif ($mode == M_INS) {

        if ($key == 3) { # Ctrl-C enters command mode
            _update_mode(M_CMD);
            _stop();
            return;

        } elsif ($key == 10) { # enter.
            _commit_line();

        } elsif ($input_buf_enabled and $imap) {
            print "Imap $imap active" if DEBUG;
            my $map = $imaps->{$imap};
            if (not defined $map->{map} or chr($key) eq $map->{map}) {
                $map->{func}($key);
                # Clear the buffer so the imap is not printed.
                @input_buf = ();
            } else {
                push @input_buf, $key;
            }
            flush_input_buffer();
            _stop();
            $imap = undef;
            return;

        } elsif (exists $imaps->{chr($key)}) {
            print "Imap " . chr($key) . " seen, starting buffer" if DEBUG;

            # start imap pending mode
            $imap = chr($key);

            $input_buf_enabled = 1;
            push @input_buf, $key;
            $input_buf_timer
              = Irssi::timeout_add_once(1000, \&flush_input_buffer, undef);

            _stop();
            return;

        # Pressing delete resets insert mode repetition (8 = BS, 127 = DEL).
        # TODO: maybe allow it
        } elsif ($key == 8 || $key == 127) {
            @insert_buf = ();
        # All other entered characters need to be stored to allow repeat of
        # insert mode. Ignore delete and control characters.
        } elsif ($key > 31) {
            push @insert_buf, chr($key);
        }
    }

    if ($input_buf_enabled) {
        push @input_buf, $key;
        _stop();
        return;
    }

    if ($mode == M_CMD) {
        my $should_stop = handle_command_cmd($key);
        _stop() if $should_stop;
        Irssi::statusbar_items_redraw("vim_mode");

    } elsif ($mode == M_EX) {

        if ($key == 3) { # C-c cancels Ex mdoe as well.
            _update_mode(M_CMD);
            _stop();
            return;
        }

        handle_command_ex($key);
    }
}

# TODO: merge this with 'flush_input_buffer' below.

sub handle_input_buffer {

    #Irssi::timeout_remove($input_buf_timer);
    $input_buf_timer = undef;
    # see what we've collected.
    print "Input buffer contains: ", join(", ", @input_buf) if DEBUG;

    if (@input_buf == 1 && $input_buf[0] == 27) {

        print "Enter Command Mode" if DEBUG;
        _update_mode(M_CMD);

    } else {
        # we have more than a single esc, implying an escape sequence
        # (meta-* or esc-*)

        # currently, we only extract escape sequences if:
        # a) we're in ex mode
        # b) they're arrow keys (for history control)

        if ($mode == M_EX) {
            # ex mode
            my $key_str = join '', map { chr } @input_buf;
            if ($key_str =~ m/^\e\[([ABCD])/) {
                my $arrow = $1;
                _debug( "Arrow key: $arrow");
                if ($arrow eq 'A') { # up
                    ex_history_back();
                } elsif ($arrow eq 'B') { # down
                    ex_history_fwd();
                } else {
                    $arrow =~ s/C/right/;
                    $arrow =~ s/D/left/;
                    _debug("Arrow key $arrow not supported");
                }
            }
        } else {
            # otherwise, we just forward them to irssi.
            _emulate_keystrokes(@input_buf);
        }

        # Clear insert buffer, pressing "special" keys (like arrow keys)
        # resets it.
        @insert_buf = ();
    }

    @input_buf = ();
    $input_buf_enabled = 0;
}

sub flush_input_buffer {
    Irssi::timeout_remove($input_buf_timer) if defined $input_buf_timer;
    $input_buf_timer = undef;
    # see what we've collected.
    print "Input buffer flushed" if DEBUG;

    # Add the characters to @insert_buf so they can be repeated.
    push @insert_buf, map chr, @input_buf;

    _emulate_keystrokes(@input_buf);

    @input_buf = ();
    $input_buf_enabled = 0;

    $imap = undef;
}

sub flush_pending_map {
    my ($old_pending_map) = @_;

    print "flush_pending_map(): ", $pending_map, ' ', $old_pending_map
        if DEBUG;

    return if not defined $pending_map or
              $pending_map ne $old_pending_map;

    handle_command_cmd(undef);
    Irssi::statusbar_items_redraw("vim_mode");
}

sub handle_numeric_prefix {
    my ($char) = @_;
    my $num = 0+$char;

    if (defined $numeric_prefix) {
        $numeric_prefix *= 10;
        $numeric_prefix += $num;
    } else {
        $numeric_prefix = $num;
    }
}

sub handle_command_cmd {
    my ($key) = @_;

    my $pending_map_flushed = 0;

    my $char;
    if (defined $key) {
        $char = chr($key);
    # We were called from flush_pending_map().
    } else {
        $char = $pending_map;
        $key = 0;
        $pending_map_flushed = 1;
    }

    # Counts
    if (!$movement and !$pending_map and
        ($char =~ m/[1-9]/ or ($numeric_prefix && $char =~ m/[0-9]/))) {
        print "Processing numeric prefix: $char" if DEBUG;
        handle_numeric_prefix($char);
        return 1; # call _stop()
    }

    if (defined $pending_map and not $pending_map_flushed) {
        $pending_map = $pending_map . $char;
        $char = $pending_map;
    }

    my $map;
    if ($movement) {
        $map = { char => $movement->{char},
                 cmd => $movement,
                 maps => {},
               };

    } elsif (exists $maps->{$char}) {
        $map = $maps->{$char};

        # We have multiple mappings starting with this key sequence.
        if (!$pending_map_flushed and scalar keys %{$map->{maps}} > 0) {
            if (not defined $pending_map) {
                $pending_map = $char;
            }

            # The current key sequence has a command mapped to it, run if
            # after a timeout.
            if (defined $map->{cmd}) {
                Irssi::timeout_add_once(1000, \&flush_pending_map,
                                              $pending_map);
            }
            return 1; # call _stop()
        }

    } else {
        print "No mapping found for $char" if DEBUG;
        $pending_map = undef;
        $numeric_prefix = undef;
        return 1; # call _stop()
    }

    $pending_map = undef;

    my $cmd = $map->{cmd};

    # Make sure we have a valid $cmd.
    if (not defined $cmd) {
        print "Bug in pending_map_flushed() $map->{char}" if DEBUG;
        return 1; # call _stop()
    }

    # Ex-mode commands can also be bound in command mode.
    if ($cmd->{type} == C_EX) {
        print "Processing ex-command: $map->{char} ($cmd->{char})" if DEBUG;

        $cmd->{func}->(substr($cmd->{char}, 1), $numeric_prefix);
        $numeric_prefix = undef;

        return 1; # call _stop()
    # As can irssi commands.
    } elsif ($cmd->{type} == C_IRSSI) {
        print "Processing irssi-command: $map->{char} ($cmd->{char})" if DEBUG;

        _command_with_context($cmd->{func});

        $numeric_prefix = undef;
        return 1; # call _stop();
    # <Nop> does nothing.
    } elsif ($cmd->{type} == C_NOP) {
        print "Processing <Nop>: $map->{char}" if DEBUG;

        $numeric_prefix = undef;
        return 1; # call _stop();
    }

    # text-objects (i a) are simulated with $movement
    if (!$movement and ($cmd->{type} == C_NEEDSKEY or
                        ($operator and ($char eq 'i' or $char eq 'a')))) {
        print "Processing movement: $map->{char} ($cmd->{char})" if DEBUG;
        if ($char eq 'i') {
            $movement = $commands->{_i};
        } elsif ($char eq 'a') {
            $movement = $commands->{_a};
        } else {
            $movement = $cmd;
        }

    } elsif (!$movement and $cmd->{type} == C_OPERATOR) {
        print "Processing operator: $map->{char} ($cmd->{char})" if DEBUG;
        # Abort operator if we already have one pending.
        if ($operator) {
            # But allow cc/dd/yy.
            if ($operator == $cmd) {
                print "Processing line operator: ",
                  $map->{char}, " (",
                  $cmd->{char} ,")"
                    if DEBUG;

                my $pos = _input_pos();
                $cmd->{func}->(0, _input_len(), undef, 0);
                # Restore position for yy.
                if ($cmd == $commands->{y}) {
                    _input_pos($pos);
                # And save undo for other operators.
                } else {
                    _add_undo_entry(_input(), _input_pos());
                }
                if ($register ne '"') {
                    print 'Changing register to "' if DEBUG;
                    $register = '"';
                }
            }
            $numeric_prefix = undef;
            $operator = undef;
            $movement = undef;
        # Set new operator.
        } else {
            $operator = $cmd;
        }

    # Start Ex mode.
    } elsif ($cmd == $commands->{':'}) {

        if (not script_is_loaded('uberprompt')) {
            _warn("Warning: Ex mode requires the 'uberprompt' script. " .
                    "Please load it and try again.");
        } else {
            _update_mode(M_EX);
            _set_prompt(':');
        }

    # Enter key sends the current input line in command mode as well.
    } elsif ($key == 10) {
        _commit_line();
        return 0; # don't call _stop()

    } else {
        print "Processing command: $map->{char} ($cmd->{char})" if DEBUG;

        my $skip = 0;
        my $repeat = 0;

        if (!$movement) {
            # . repeats the last command.
            if ($cmd == $commands->{'.'} and defined $last->{cmd}) {
                $cmd = $last->{cmd};
                $char = $last->{char};
                # If . is given a count then it replaces original count.
                if (not defined $numeric_prefix) {
                    $numeric_prefix = $last->{numeric_prefix};
                }
                $operator = $last->{operator};
                $movement = $last->{movement};
                $register = $last->{register};
                $repeat = 1;
            } elsif ($cmd == $commands->{'.'}) {
                print '. pressed but $last->{char} not set' if DEBUG;
                $skip = 1;
            }
        }

        # Ignore invalid operator/command combinations.
        if ($operator and $cmd->{no_operator}) {
            print "Invalid operator/command: $operator->{char} $cmd->{char}"
                if DEBUG;
            $skip = 1;
        }

        if ($skip) {
            print "Skipping movement and operator." if DEBUG;
        } else {
            # Make sure count is at least 1 except for functions which need to
            # know if no count was used.
            if (not $numeric_prefix and not $cmd->{needs_count}) {
                $numeric_prefix = 1;
            }

            my $cur_pos = _input_pos();

            # If defined $cur_pos will be changed to this.
            my $old_pos;
            # Position after the move.
            my $new_pos;
            # Execute the movement (multiple times).
            if (not $movement) {
                ($old_pos, $new_pos)
                    = $cmd->{func}->($numeric_prefix, $cur_pos, $repeat);
            } else {
                ($old_pos, $new_pos)
                    = $cmd->{func}->($numeric_prefix, $cur_pos, $repeat,
                                     $char);
            }
            if (defined $old_pos) {
                print "Changing \$cur_pos from $cur_pos to $old_pos" if DEBUG;
                $cur_pos = $old_pos;
            }
            if (defined $new_pos) {
                _input_pos($new_pos);
            } else {
                $new_pos = _input_pos();
            }

            # Update input position of last undo entry so that undo/redo
            # restores correct position.
            if (@undo_buffer and _input() eq $undo_buffer[0]->[0] and
                ((defined $operator and $operator == $commands->{d}) or
                 $cmd->{repeatable})) {
                print "Updating history position: $undo_buffer[0]->[0]"
                    if DEBUG;
                $undo_buffer[0]->[1] = $cur_pos;
            }

            # If we have an operator pending then run it on the handled text.
            # But only if the movement changed the position (this prevents
            # problems with e.g. f when the search string doesn't exist).
            if ($operator and $cur_pos != $new_pos) {
                print "Processing operator: ", $operator->{char} if DEBUG;
                $operator->{func}->($cur_pos, $new_pos, $cmd, $repeat);
            }

            # Save an undo checkpoint here for operators, all repeatable
            # movements, operators and repetition.
            if ((defined $operator and $operator == $commands->{d}) or
                $cmd->{repeatable}) {
                # TODO: why do history entries still show up in undo
                # buffer? Is avoiding the commands here insufficient?

                _add_undo_entry(_input(), _input_pos());
            }

            # Store command, necessary for .
            if ($operator or $cmd->{repeatable}) {
                $last->{cmd} = $cmd;
                $last->{char} = $char;
                $last->{numeric_prefix} = $numeric_prefix;
                $last->{operator} = $operator;
                $last->{movement} = $movement;
                $last->{register} = $register;
            }
        }

        # Reset the count unless we go into insert mode, _update_mode() needs
        # to know it when leaving insert mode to support insert with counts
        # (like 3i).
        if ($repeat or $cmd->{type} != C_INSERT) {
            $numeric_prefix = undef;
        }
        $operator = undef;
        $movement = undef;

        if ($cmd != $commands->{'"'} and $register ne '"') {
            print 'Changing register to "' if DEBUG;
            $register = '"';
        }

    }

    return 1; # call _stop()
}

sub handle_command_ex {
    my ($key) = @_;

    # BS key (8) or DEL key (127) - remove last character.
    if ($key == 8 || $key == 127) {
        print "Delete" if DEBUG;
        if (@ex_buf > 0) {
            pop @ex_buf;
            _set_prompt(':' . join '', @ex_buf);
        # Backspacing over : exits ex-mode.
        } else {
            _update_mode(M_CMD);
        }

    # Return key - execute command
    } elsif ($key == 10) {
        print "Run ex-mode command" if DEBUG;
        cmd_ex_command();
        _update_mode(M_CMD);

    } elsif ($key == 9) { # TAB
        print "Tab pressed" if DEBUG;
        print "Ex buf contains: " . join('', @ex_buf) if DEBUG;
        @tab_candidates = _tab_complete(join('', @ex_buf), [keys %$commands_ex]);
        _debug("Candidates: " . join(", ", @tab_candidates));
        if (@tab_candidates == 1) {
            @ex_buf = ( split('', $tab_candidates[0]), ' ');
            _set_prompt(':' . join '', @ex_buf);
        }
    # Ignore control characters for now.
    } elsif ($key > 0 && $key < 32) {
        # TODO: use them later, e.g. completion

    # Append entered key
    } else {
        if ($key != -1) {
            # check we're not called from an ex_history_* function
            push @ex_buf, chr $key;
        }
        _set_prompt(':' . join '', @ex_buf);
    }

    Irssi::statusbar_items_redraw("vim_windows");

    _stop();
}

sub _tab_complete {
    my ($input, $source) = @_;
    my @out;
    foreach my $item (@$source) {
        if ($item =~ m/^\Q$input\E/) {
            push @out, $item;
        }
    }

    return sort { $a cmp $b } @out;
}

sub vim_mode_init {
    Irssi::signal_add_first 'gui key pressed' => \&got_key;
    Irssi::statusbar_item_register ('vim_mode',    0, 'vim_mode_cb');
    Irssi::statusbar_item_register ('vim_windows', 0, 'b_windows_cb');

    Irssi::expando_create('vim_cmd_mode' => \&vim_exp_mode, {});
    Irssi::expando_create('vim_wins'     => \&vim_exp_wins, {});


    # Register all available settings.
    foreach my $name (keys %$settings) {
        _setting_register($name);
    }

    foreach my $char ('a' .. 'z') {
        $registers->{$char} = '';
    }

    setup_changed();

    Irssi::signal_add 'setup changed' => \&setup_changed;

    # Add all default mappings.
    foreach my $char (keys %$commands) {
        next if $char =~ /^_/; # skip private commands (text-objects for now)
        add_map($char, $commands->{$char});
    }

    # Load the vim_moderc file if it exists.
    ex_source('source');

    setup_changed();
    _reset_undo_buffer();

    if ($settings->{start_cmd}->{value}) {
        _update_mode(M_CMD);
    } else {
        _update_mode(M_INS);
    }
}

sub setup_changed {
    my $value;

    if ($settings->{cmd_seq}->{value} ne '') {
        delete $imaps->{$settings->{cmd_seq}->{value}};
    }
    $value = _setting_get('cmd_seq');
    if ($value eq '') {
        $settings->{cmd_seq}->{value} = $value;
    } else {
        if (length $value == 1) {
            $imaps->{$value} = { 'map'  => $value,
                                 'func' => sub { _update_mode(M_CMD) }
                               };
            $settings->{cmd_seq}->{value} = $value;
        } else {
            _warn("Error: vim_mode_cmd_seq must be a single character");
            # Restore the value so $settings and irssi settings are
            # consistent.
            _setting_set('cmd_seq', $settings->{cmd_seq}->{value});
        }
    }

    my $new_utf8 = _setting_get('utf8');
    if ($new_utf8 != $settings->{utf8}->{value}) {
        # recompile the patterns when switching to/from utf-8
        $word     = qr/[\w_]/o;
        $non_word = qr/[^\w_\s]/o;

        $settings->{utf8}->{value} = $new_utf8;
    }
    if ($new_utf8 and (!$^V or $^V lt v5.8.1)) {
        _warn("Warning: UTF-8 isn't supported very well in perl < 5.8.1! " .
              "Please disable the vim_mode_utf8 setting.");
    }

    # Sync $settings with current irssi values.
    foreach my $name (keys %$settings) {
        # These were already handled above.
        next if $name eq 'cmd_seq' or $name eq 'cmd_seq';

        $settings->{$name}->{value} = _setting_get($name);
    }
}

sub UNLOAD {
    Irssi::signal_remove('gui key pressed' => \&got_key);
    Irssi::signal_remove('setup changed' => \&setup_changed);
    Irssi::statusbar_item_unregister ('vim_mode');
    Irssi::statusbar_item_unregister ('vim_windows');
}

sub _add_undo_entry {
    my ($line, $pos) = @_;

    # If we aren't at the top of the history stack, then drop newer entries as
    # we can't branch (yet).
    while ($undo_index > 0) {
        shift @undo_buffer;
        $undo_index--;
    }

    # check it's not a dupe of the list head
    my $current = $undo_buffer[$undo_index];
    if ($line eq $current->[0] && $pos == $current->[1]) {
        print "Not adding duplicate to undo list" if DEBUG;
    } elsif ($line eq $current->[0]) {
        print "Updating position of undo list at $undo_index" if DEBUG;
        $undo_buffer[$undo_index]->[1] = $pos;
    } else {
        print "adding $line ($pos) to undo list" if DEBUG;
        # add to the front of the buffer
        unshift @undo_buffer, [$line, $pos];
        $undo_index = 0;
    }
    my $max = $settings->{max_undo_lines}->{value};
}

sub _restore_undo_entry {
    my $entry = $undo_buffer[$undo_index];
    _input($entry->[0]);
    _input_pos($entry->[1]);
}

sub _print_undo_buffer {

    my $i = 0;
    my @buf;
    foreach my $entry (@undo_buffer) {
        my $str = '';
        if ($i == $undo_index) {
            $str .= '* ';
        } else {
            $str .= '  ';
        }
        my ($line, $pos) = @$entry;
        substr($line, $pos, 0) = '*';
        # substr($line, $pos+3, 0) = '%_';

        $str .= sprintf('%02d %s [%d]', $i, $line, $pos);
        push @buf, $str;
        $i++;
    }
    print "------ undo buffer ------";
    print join("\n", @buf);
    print "------------------ ------";

}

sub _reset_undo_buffer {
    my ($line, $pos) = @_;
    $line = _input()     unless defined $line;
    $pos  = _input_pos() unless defined $pos;

    print "Clearing undo buffer" if DEBUG;
    @undo_buffer = ([$line, $pos]);
    $undo_index  = 0;
}

sub add_map {
    my ($keys, $command) = @_;

    # To allow multiple mappings starting with the same key (like gg, ge, gE)
    # also create maps for the keys "leading" to this key (g in this case, but
    # can be longer for this like ,ls). When looking for the mapping these
    # "leading" maps are followed.
    my $tmp = $keys;
    while (length $tmp > 1) {
        my $map = substr $tmp, -1, 1, '';
        if (not exists $maps->{$tmp}) {
            $maps->{$tmp} = { char => _parse_mapping_reverse($tmp),
                               cmd => undef,
                              maps => {}
                            };
        }
        if (not exists $maps->{$tmp}->{maps}->{$tmp . $map}) {
            $maps->{$tmp}->{maps}->{$tmp . $map} = undef;
        }
    }

    if (not exists $maps->{$keys}) {
        $maps->{$keys} = { char => undef,
                            cmd => undef,
                           maps => {}
                         };
    }
    $maps->{$keys}->{char} = _parse_mapping_reverse($keys);
    $maps->{$keys}->{cmd} = $command;
}

sub delete_map {
    my ($keys) = @_;

    # Abort for non-existent mappings or placeholder mappings.
    return if not exists $maps->{$keys} or not defined $maps->{$keys}->{cmd};

    my @add = ();

    # If no maps need the current key, then remove it and all other
    # unnecessary keys in the "tree".
    if (keys %{$maps->{$keys}->{maps}} == 0) {
        my $tmp = $keys;
        while (length $tmp > 1) {
            my $map = substr $tmp, -1, 1, '';
            delete $maps->{$tmp}->{maps}->{$tmp . $map};
            if (not $maps->{$tmp}->{cmd} and keys %{$maps->{$tmp}->{maps}} == 0) {
                push @add, $tmp;
                delete $maps->{$tmp};
            } else {
                last;
            }
        }
    }

    if (keys %{$maps->{$keys}->{maps}} > 0) {
        $maps->{$keys}->{cmd} = undef;
    } else {
        delete $maps->{$keys};
    }
    push @add, $keys;

    # Restore default keybindings in case we :unmapped a <Nop> or a remapped
    # key.
    foreach my $key (@add) {
        if (exists $commands->{$key}) {
            add_map($key, $commands->{$key});
        }
    }
}


sub _commit_line {
    _update_mode(M_INS);

    # separate from call above as _update_mode() does additional internal work
    # and we need to make sure it gets correctly called.
    _update_mode(M_CMD) if $settings->{start_cmd}->{value};

    _reset_undo_buffer('', 0);
}

sub _input {
    my ($data) = @_;

    my $current_data = Irssi::parse_special('$L', 0, 0);

    if ($settings->{utf8}->{value}) {
        $current_data = decode_utf8($current_data);
    }

    if (defined $data) {
        if ($settings->{utf8}->{value}) {
            Irssi::gui_input_set(encode_utf8($data));
        } else {
            Irssi::gui_input_set($data);
        }
    } else {
        $data = $current_data;
    }

    return $data;
}

sub _input_len {
    return length _input();
}

sub _input_pos {
    my ($pos) = @_;
    my $cur_pos = Irssi::gui_input_get_pos();
    # my $dpos = defined $pos?$pos:'undef';
    # my @call = caller(1);
    # my $cfunc = $call[3];
    # $cfunc =~ s/^.*?::([^:]+)$/$1/;
    # print "pos called from line: $call[2] sub: $cfunc pos: $dpos, cur_pos: $cur_pos"
    #   if DEBUG;

    if (defined $pos) {
        #print "Input pos being set from $cur_pos to $pos" if DEBUG;
        Irssi::gui_input_set_pos($pos) if $pos != $cur_pos;
    } else {
        $pos = $cur_pos;
        #print "Input pos retrieved as $pos" if DEBUG;
    }

    return $pos;
}

sub _emulate_keystrokes {
    my @keys = @_;
    $should_ignore = 1;
    for my $key (@keys) {
        Irssi::signal_emit('gui key pressed', $key);
    }
    $should_ignore = 0;
}

sub _stop() {
    Irssi::signal_stop_by_name('gui key pressed');
}

sub _update_mode {
    my ($new_mode) = @_;

    my $pos;

    if ($mode == M_INS and $new_mode == M_CMD) {
        # Support counts with insert modes, like 3i.
        if ($numeric_prefix and $numeric_prefix > 1) {
            $pos = _insert_buffer($numeric_prefix - 1, _input_pos());
            _input_pos($pos);
            $numeric_prefix = undef;

        # In insert mode we are "between" characters, in command mode "on top"
        # of keys. When leaving insert mode we have to move on key left to
        # accomplish that.
        } else {
            $pos = _input_pos();
            if ($pos != 0) {
                _input_pos($pos - 1);
            }
        }
        # Store current line to allow undo of i/a/I/A.
        _add_undo_entry(_input(), _input_pos());

    # Change mode to i to support insert mode repetition. This doesn't affect
    # commands like i/a/I/A because handle_command_cmd() sets $last->{cmd}.
    # It's necessary when pressing enter so the next line can be repeated.
    } elsif ($mode == M_CMD and $new_mode == M_INS) {
        $last->{cmd} = $commands->{i};
    # Make sure prompt is cleared when leaving ex mode.
    } elsif ($mode == M_EX and $new_mode != M_EX) {
        _set_prompt('');
    }

    $mode = $new_mode;
    if ($mode == M_INS) {
        $history_index = undef;
        $register = '"';
        @insert_buf = ();
    # Reset every command mode related status as a fallback in case something
    # goes wrong.
    } elsif ($mode == M_CMD) {
        $numeric_prefix = undef;
        $operator = undef;
        $movement = undef;
        $register = '"';

        $pending_map = undef;

        # Also clear ex-mode buffer.
        @ex_buf = ();
    }

    Irssi::statusbar_items_redraw("vim_mode");
    Irssi::statusbar_items_redraw ('uberprompt');

}

sub _set_prompt {
    my $msg = shift;

    # add a leading space unless we're trying to clear it entirely.
    if  (length($msg) and $settings->{prompt_leading_space}->{value}) {
        $msg = ' ' . $msg;
    }

    # escape % symbols. This prevents any _set_prompt calls from using
    # colouring sequences.
    $msg =~ s/%/%%/g;

    Irssi::signal_emit('change prompt', $msg, 'UP_INNER');
}

sub _setting_get {
    my ($name) = @_;

    my $type = $settings->{$name}->{type};
    $name = "vim_mode_$name";

    my $ret = undef;

    if ($type == S_BOOL) {
        $ret = Irssi::settings_get_bool($name);
    } elsif ($type == S_INT) {
        $ret = Irssi::settings_get_int($name);
    } elsif ($type == S_STR) {
        $ret = Irssi::settings_get_str($name);
    } else {
        _warn("Unknown setting type '$type', please report.");
    }

    return $ret;
}

sub _setting_set {
    my ($name, $value) = @_;

    my $type = $settings->{$name}->{type};
    $name = "vim_mode_$name";

    if ($type == S_BOOL) {
        Irssi::settings_set_bool($name, $value);
    } elsif ($type == S_INT) {
        Irssi::settings_set_int($name, $value);
    } elsif ($type == S_STR) {
        Irssi::settings_set_str($name, $value);
    } else {
        _warn("Unknown setting type '$type', please report.");
    }
}
sub _setting_register {
    my ($name) = @_;

    my $value = $settings->{$name}->{value};
    my $type  = $settings->{$name}->{type};
    $name = "vim_mode_$name";

    if ($type == S_BOOL) {
        Irssi::settings_add_bool('vim_mode', $name, $value);
    } elsif ($type == S_INT) {
        Irssi::settings_add_int('vim_mode', $name, $value);
    } elsif ($type == S_STR) {
        Irssi::settings_add_str('vim_mode', $name, $value);
    } else {
        _warn("Unknown setting type '$type', please report.");
    }
}

sub _warn {
    my ($warning) = @_;

    print '%_vim_mode: ', $warning, '%_';
}

sub _debug {
    return unless DEBUG;

    my ($format, @args) = @_;
    my $str = sprintf($format, @args);
    print $str;
}

sub _command_with_context {
    my ($command) = @_;
    my $context;
    my $window = Irssi::active_win;
    if (defined $window) {
        my $witem = $window->{active};
        if (defined $witem and ref($witem) eq 'Irssi::Windowitem') {
            $context = $witem;
        } else {
            $context = $window;
        }
    } else {
        my $server = Irssi::active_server;
        if (defined $server) {
            $context = $server;
        }
    }
    if (defined $context) {
        print "Command $command Using context: " . ref($context) if DEBUG;
        $context->command($command);
    } else {
        print "Command $command has no context" if DEBUG;
        Irssi::command($command);
    }
}

sub ex_history_add {
    my ($line) = @_;

    # check it's not an exact dupe of the previous history line

    my $last_hist = $ex_history[$ex_history_index];
    $last_hist = '' unless defined $last_hist;

    return if $last_hist eq $line;

    _debug("Adding $line to ex command history");

    # add it to the history
    unshift @ex_history, $line;

    if ($settings->{ex_history_size}->{value} < @ex_history) {
        pop @ex_history; # junk the last entry if we've hit the max.
    }
}

sub ex_history_fwd {

    my $hist_max = $#ex_history;
    $ex_history_index++;
    if ($ex_history_index > $hist_max) {
        $ex_history_index = 0;
        _debug("ex history hit top, wrapping to 0");
    }

    my $line = $ex_history[$ex_history_index];
    $line = '' if not defined $line;

    _debug("Ex history line: $line");

    @ex_buf = split '', $line;
    handle_command_ex(-1);
}

sub ex_history_back {
    my $hist_max = $#ex_history;
    $ex_history_index--;
    if ($ex_history_index == -1) {
        $ex_history_index = $hist_max;
        _debug("ex history hit bottom, wrapping to $hist_max");

    }

    my $line = $ex_history[$ex_history_index];
    $line = '' if not defined $line;

    _debug("Ex history line: $line");
    @ex_buf = split '', $line;
    handle_command_ex(-1);

}

sub ex_history_show {
    my $win = Irssi::active_win();
    $win->print("Ex command history:");
    for my $i (0 .. $#ex_history) {
        my $flag = $i == $ex_history_index
          ? ' <'
          : '';
        $win->print("$i " . $ex_history[$i] . $flag);
    }
}
vim_mode_init();