1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
|
// ex_getln.c: Functions for entering and editing an Ex command line.
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "klib/kvec.h"
#include "nvim/api/extmark.h"
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#include "nvim/api/vim.h"
#include "nvim/ascii_defs.h"
#include "nvim/autocmd.h"
#include "nvim/autocmd_defs.h"
#include "nvim/buffer.h"
#include "nvim/buffer_defs.h"
#include "nvim/charset.h"
#include "nvim/cmdexpand.h"
#include "nvim/cmdexpand_defs.h"
#include "nvim/cmdhist.h"
#include "nvim/cursor.h"
#include "nvim/digraph.h"
#include "nvim/drawscreen.h"
#include "nvim/edit.h"
#include "nvim/errors.h"
#include "nvim/eval.h"
#include "nvim/eval/typval.h"
#include "nvim/eval/vars.h"
#include "nvim/ex_cmds.h"
#include "nvim/ex_cmds_defs.h"
#include "nvim/ex_docmd.h"
#include "nvim/ex_eval.h"
#include "nvim/ex_getln.h"
#include "nvim/extmark.h"
#include "nvim/garray.h"
#include "nvim/garray_defs.h"
#include "nvim/getchar.h"
#include "nvim/gettext_defs.h"
#include "nvim/globals.h"
#include "nvim/highlight_defs.h"
#include "nvim/highlight_group.h"
#include "nvim/keycodes.h"
#include "nvim/macros_defs.h"
#include "nvim/map_defs.h"
#include "nvim/mapping.h"
#include "nvim/mark.h"
#include "nvim/mark_defs.h"
#include "nvim/mbyte.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/memory_defs.h"
#include "nvim/message.h"
#include "nvim/mouse.h"
#include "nvim/move.h"
#include "nvim/normal.h"
#include "nvim/ops.h"
#include "nvim/option.h"
#include "nvim/option_defs.h"
#include "nvim/option_vars.h"
#include "nvim/os/input.h"
#include "nvim/os/os.h"
#include "nvim/path.h"
#include "nvim/popupmenu.h"
#include "nvim/pos_defs.h"
#include "nvim/profile.h"
#include "nvim/regexp.h"
#include "nvim/regexp_defs.h"
#include "nvim/search.h"
#include "nvim/state.h"
#include "nvim/state_defs.h"
#include "nvim/strings.h"
#include "nvim/types_defs.h"
#include "nvim/ui.h"
#include "nvim/ui_defs.h"
#include "nvim/undo.h"
#include "nvim/undo_defs.h"
#include "nvim/usercmd.h"
#include "nvim/vim_defs.h"
#include "nvim/viml/parser/expressions.h"
#include "nvim/viml/parser/parser.h"
#include "nvim/viml/parser/parser_defs.h"
#include "nvim/window.h"
/// Last value of prompt_id, incremented when doing new prompt
static unsigned last_prompt_id = 0;
// Struct to store the viewstate during 'incsearch' highlighting and 'inccommand' preview.
typedef struct {
colnr_T vs_curswant;
colnr_T vs_leftcol;
colnr_T vs_skipcol;
linenr_T vs_topline;
int vs_topfill;
linenr_T vs_botline;
int vs_empty_rows;
} viewstate_T;
// Struct to store the state of 'incsearch' highlighting.
typedef struct {
pos_T search_start; // where 'incsearch' starts searching
pos_T save_cursor;
viewstate_T init_viewstate;
viewstate_T old_viewstate;
pos_T match_start;
pos_T match_end;
bool did_incsearch;
bool incsearch_postponed;
optmagic_T magic_overruled_save;
} incsearch_state_T;
typedef struct {
VimState state;
int firstc;
int count;
int indent;
int c;
bool gotesc; // true when <ESC> just typed
bool do_abbr; // when true check for abbr.
char *lookfor; // string to match
int lookforlen;
int hiscnt; // current history line in use
int save_hiscnt; // history line before attempting
// to jump to next match
int histype; // history type to be used
incsearch_state_T is_state;
bool did_wild_list; // did wild_list() recently
int wim_index; // index in wim_flags[]
int save_msg_scroll;
int save_State; // remember State when called
int prev_cmdpos;
char *save_p_icm;
bool some_key_typed; // one of the keys was typed
// mouse drag and release events are ignored, unless they are
// preceded with a mouse down event
bool ignore_drag_release;
bool break_ctrl_c;
expand_T xpc;
OptInt *b_im_ptr;
buf_T *b_im_ptr_buf; ///< buffer where b_im_ptr is valid
} CommandLineState;
typedef struct {
u_header_T *save_b_u_oldhead;
u_header_T *save_b_u_newhead;
u_header_T *save_b_u_curhead;
int save_b_u_numhead;
bool save_b_u_synced;
int save_b_u_seq_last;
int save_b_u_save_nr_last;
int save_b_u_seq_cur;
time_t save_b_u_time_cur;
int save_b_u_save_nr_cur;
char *save_b_u_line_ptr;
linenr_T save_b_u_line_lnum;
colnr_T save_b_u_line_colnr;
} CpUndoInfo;
typedef struct {
buf_T *buf;
OptInt save_b_p_ul;
int save_b_p_ma;
int save_b_changed;
pos_T save_b_op_start;
pos_T save_b_op_end;
varnumber_T save_changedtick;
CpUndoInfo undo_info;
} CpBufInfo;
typedef struct {
win_T *win;
pos_T save_w_cursor;
viewstate_T save_viewstate;
int save_w_p_cul;
int save_w_p_cuc;
} CpWinInfo;
typedef struct {
kvec_t(CpWinInfo) win_info;
kvec_t(CpBufInfo) buf_info;
bool save_hls;
cmdmod_T save_cmdmod;
garray_T save_view;
} CpInfo;
/// Return value when handling keys in command-line mode.
enum {
CMDLINE_NOT_CHANGED = 1,
CMDLINE_CHANGED = 2,
GOTO_NORMAL_MODE = 3,
PROCESS_NEXT_KEY = 4,
};
/// The current cmdline_info. It is initialized in getcmdline() and after that
/// used by other functions. When invoking getcmdline() recursively it needs
/// to be saved with save_cmdline() and restored with restore_cmdline().
static CmdlineInfo ccline;
static int new_cmdpos; // position set by set_cmdline_pos()
/// currently displayed block of context
static Array cmdline_block = ARRAY_DICT_INIT;
/// Flag for command_line_handle_key to ignore <C-c>
///
/// Used if it was received while processing highlight function in order for
/// user interrupting highlight function to not interrupt command-line.
static bool getln_interrupted_highlight = false;
static int cedit_key = -1; ///< key value of 'cedit' option
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ex_getln.c.generated.h"
#endif
static handle_T cmdpreview_bufnr = 0;
static int cmdpreview_ns = 0;
static const char e_active_window_or_buffer_changed_or_deleted[]
= N_("E199: Active window or buffer changed or deleted");
static void trigger_cmd_autocmd(int typechar, event_T evt)
{
char typestr[2] = { (char)typechar, NUL };
apply_autocmds(evt, typestr, typestr, false, curbuf);
}
static void save_viewstate(win_T *wp, viewstate_T *vs)
FUNC_ATTR_NONNULL_ALL
{
vs->vs_curswant = wp->w_curswant;
vs->vs_leftcol = wp->w_leftcol;
vs->vs_skipcol = wp->w_skipcol;
vs->vs_topline = wp->w_topline;
vs->vs_topfill = wp->w_topfill;
vs->vs_botline = wp->w_botline;
vs->vs_empty_rows = wp->w_empty_rows;
}
static void restore_viewstate(win_T *wp, viewstate_T *vs)
FUNC_ATTR_NONNULL_ALL
{
wp->w_curswant = vs->vs_curswant;
wp->w_leftcol = vs->vs_leftcol;
wp->w_skipcol = vs->vs_skipcol;
wp->w_topline = vs->vs_topline;
wp->w_topfill = vs->vs_topfill;
wp->w_botline = vs->vs_botline;
wp->w_empty_rows = vs->vs_empty_rows;
}
static void init_incsearch_state(incsearch_state_T *s)
{
s->match_start = curwin->w_cursor;
s->did_incsearch = false;
s->incsearch_postponed = false;
s->magic_overruled_save = magic_overruled;
clearpos(&s->match_end);
s->save_cursor = curwin->w_cursor; // may be restored later
s->search_start = curwin->w_cursor;
save_viewstate(curwin, &s->init_viewstate);
save_viewstate(curwin, &s->old_viewstate);
}
static void set_search_match(pos_T *t)
{
// First move cursor to end of match, then to the start. This
// moves the whole match onto the screen when 'nowrap' is set.
t->lnum += search_match_lines;
t->col = search_match_endcol;
if (t->lnum > curbuf->b_ml.ml_line_count) {
t->lnum = curbuf->b_ml.ml_line_count;
coladvance(curwin, MAXCOL);
}
}
// Return true when 'incsearch' highlighting is to be done.
// Sets search_first_line and search_last_line to the address range.
static bool do_incsearch_highlighting(int firstc, int *search_delim, incsearch_state_T *s,
int *skiplen, int *patlen)
FUNC_ATTR_NONNULL_ALL
{
char *p;
bool delim_optional = false;
const char *dummy;
bool retval = false;
magic_T magic = 0;
*skiplen = 0;
*patlen = ccline.cmdlen;
if (!p_is || cmd_silent) {
return false;
}
// by default search all lines
search_first_line = 0;
search_last_line = MAXLNUM;
if (firstc == '/' || firstc == '?') {
*search_delim = firstc;
return true;
}
if (firstc != ':') {
return false;
}
emsg_off++;
exarg_T ea = {
.line1 = 1,
.line2 = 1,
.cmd = ccline.cmdbuff,
.addr_type = ADDR_LINES,
};
cmdmod_T dummy_cmdmod;
parse_command_modifiers(&ea, &dummy, &dummy_cmdmod, true);
char *cmd = skip_range(ea.cmd, NULL);
if (vim_strchr("sgvl", (uint8_t)(*cmd)) == NULL) {
goto theend;
}
// Skip over "substitute" to find the pattern separator.
for (p = cmd; ASCII_ISALPHA(*p); p++) {}
if (*skipwhite(p) == NUL) {
goto theend;
}
if (strncmp(cmd, "substitute", (size_t)(p - cmd)) == 0
|| strncmp(cmd, "smagic", (size_t)(p - cmd)) == 0
|| strncmp(cmd, "snomagic", (size_t)MAX(p - cmd, 3)) == 0
|| strncmp(cmd, "vglobal", (size_t)(p - cmd)) == 0) {
if (*cmd == 's' && cmd[1] == 'm') {
magic_overruled = OPTION_MAGIC_ON;
} else if (*cmd == 's' && cmd[1] == 'n') {
magic_overruled = OPTION_MAGIC_OFF;
}
} else if (strncmp(cmd, "sort", (size_t)MAX(p - cmd, 3)) == 0) {
// skip over ! and flags
if (*p == '!') {
p = skipwhite(p + 1);
}
while (ASCII_ISALPHA(*(p = skipwhite(p)))) {
p++;
}
if (*p == NUL) {
goto theend;
}
} else if (strncmp(cmd, "vimgrep", (size_t)MAX(p - cmd, 3)) == 0
|| strncmp(cmd, "vimgrepadd", (size_t)MAX(p - cmd, 8)) == 0
|| strncmp(cmd, "lvimgrep", (size_t)MAX(p - cmd, 2)) == 0
|| strncmp(cmd, "lvimgrepadd", (size_t)MAX(p - cmd, 9)) == 0
|| strncmp(cmd, "global", (size_t)(p - cmd)) == 0) {
// skip over "!/".
if (*p == '!') {
p++;
if (*skipwhite(p) == NUL) {
goto theend;
}
}
if (*cmd != 'g') {
delim_optional = true;
}
} else {
goto theend;
}
p = skipwhite(p);
int delim = (delim_optional && vim_isIDc((uint8_t)(*p))) ? ' ' : *p++;
*search_delim = delim;
char *end = skip_regexp_ex(p, delim, magic_isset(), NULL, NULL, &magic);
bool use_last_pat = end == p && *end == delim;
if (end == p && !use_last_pat) {
goto theend;
}
// Don't do 'hlsearch' highlighting if the pattern matches everything.
if (!use_last_pat) {
char c = *end;
*end = NUL;
bool empty = empty_pattern_magic(p, (size_t)(end - p), magic);
*end = c;
if (empty) {
goto theend;
}
}
// found a non-empty pattern or //
*skiplen = (int)(p - ccline.cmdbuff);
*patlen = (int)(end - p);
// parse the address range
pos_T save_cursor = curwin->w_cursor;
curwin->w_cursor = s->search_start;
parse_cmd_address(&ea, &dummy, true);
if (ea.addr_count > 0) {
// Allow for reverse match.
search_first_line = MIN(ea.line2, ea.line1);
search_last_line = MAX(ea.line2, ea.line1);
} else if (cmd[0] == 's' && cmd[1] != 'o') {
// :s defaults to the current line
search_first_line = curwin->w_cursor.lnum;
search_last_line = curwin->w_cursor.lnum;
}
curwin->w_cursor = save_cursor;
retval = true;
theend:
emsg_off--;
return retval;
}
// May do 'incsearch' highlighting if desired.
static void may_do_incsearch_highlighting(int firstc, int count, incsearch_state_T *s)
{
pos_T end_pos;
int skiplen, patlen;
int search_delim;
// Parsing range may already set the last search pattern.
// NOTE: must call restore_last_search_pattern() before returning!
save_last_search_pattern();
if (!do_incsearch_highlighting(firstc, &search_delim, s, &skiplen,
&patlen)) {
restore_last_search_pattern();
finish_incsearch_highlighting(false, s, true);
return;
}
// if there is a character waiting, search and redraw later
if (char_avail()) {
restore_last_search_pattern();
s->incsearch_postponed = true;
return;
}
s->incsearch_postponed = false;
if (search_first_line == 0) {
// start at the original cursor position
curwin->w_cursor = s->search_start;
} else if (search_first_line > curbuf->b_ml.ml_line_count) {
// start after the last line
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
curwin->w_cursor.col = MAXCOL;
} else {
// start at the first line in the range
curwin->w_cursor.lnum = search_first_line;
curwin->w_cursor.col = 0;
}
int found; // do_search() result
// Use the previous pattern for ":s//".
char next_char = ccline.cmdbuff[skiplen + patlen];
bool use_last_pat = patlen == 0 && skiplen > 0
&& ccline.cmdbuff[skiplen - 1] == next_char;
// If there is no pattern, don't do anything.
if (patlen == 0 && !use_last_pat) {
found = 0;
set_no_hlsearch(true); // turn off previous highlight
redraw_all_later(UPD_SOME_VALID);
} else {
int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
ui_busy_start();
ui_flush();
emsg_off++; // So it doesn't beep if bad expr
// Set the time limit to half a second.
proftime_T tm = profile_setlimit(500);
if (!p_hls) {
search_flags += SEARCH_KEEP;
}
if (search_first_line != 0) {
search_flags += SEARCH_START;
}
ccline.cmdbuff[skiplen + patlen] = NUL;
searchit_arg_T sia = {
.sa_tm = &tm,
};
found = do_search(NULL, firstc == ':' ? '/' : firstc, search_delim,
ccline.cmdbuff + skiplen, (size_t)patlen, count,
search_flags, &sia);
ccline.cmdbuff[skiplen + patlen] = next_char;
emsg_off--;
if (curwin->w_cursor.lnum < search_first_line
|| curwin->w_cursor.lnum > search_last_line) {
// match outside of address range
found = 0;
curwin->w_cursor = s->search_start;
}
// if interrupted while searching, behave like it failed
if (got_int) {
vpeekc(); // remove <C-C> from input stream
got_int = false; // don't abandon the command line
found = 0;
} else if (char_avail()) {
// cancelled searching because a char was typed
s->incsearch_postponed = true;
}
ui_busy_stop();
}
if (found != 0) {
highlight_match = true; // highlight position
} else {
highlight_match = false; // remove highlight
}
// first restore the old curwin values, so the screen is
// positioned in the same way as the actual search command
restore_viewstate(curwin, &s->old_viewstate);
changed_cline_bef_curs(curwin);
update_topline(curwin);
if (found != 0) {
pos_T save_pos = curwin->w_cursor;
s->match_start = curwin->w_cursor;
set_search_match(&curwin->w_cursor);
validate_cursor(curwin);
end_pos = curwin->w_cursor;
s->match_end = end_pos;
curwin->w_cursor = save_pos;
} else {
end_pos = curwin->w_cursor; // shutup gcc 4
}
// Disable 'hlsearch' highlighting if the pattern matches
// everything. Avoids a flash when typing "foo\|".
if (!use_last_pat) {
next_char = ccline.cmdbuff[skiplen + patlen];
ccline.cmdbuff[skiplen + patlen] = NUL;
if (empty_pattern(ccline.cmdbuff + skiplen, (size_t)patlen, search_delim)
&& !no_hlsearch) {
redraw_all_later(UPD_SOME_VALID);
set_no_hlsearch(true);
}
ccline.cmdbuff[skiplen + patlen] = next_char;
}
validate_cursor(curwin);
// May redraw the status line to show the cursor position.
if (p_ru && (curwin->w_status_height > 0 || global_stl_height() > 0)) {
curwin->w_redr_status = true;
}
redraw_later(curwin, UPD_SOME_VALID);
update_screen();
highlight_match = false;
restore_last_search_pattern();
// Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
// end of the pattern, e.g. for ":s/pat/".
if (ccline.cmdbuff[skiplen + patlen] != NUL) {
curwin->w_cursor = s->search_start;
} else if (found != 0) {
curwin->w_cursor = end_pos;
}
msg_starthere();
redrawcmdline();
s->did_incsearch = true;
}
// When CTRL-L typed: add character from the match to the pattern.
// May set "*c" to the added character.
// Return OK when calling command_line_not_changed.
static int may_add_char_to_search(int firstc, int *c, incsearch_state_T *s)
FUNC_ATTR_NONNULL_ALL
{
int skiplen, patlen;
int search_delim;
// Parsing range may already set the last search pattern.
// NOTE: must call restore_last_search_pattern() before returning!
save_last_search_pattern();
// Add a character from under the cursor for 'incsearch'
if (!do_incsearch_highlighting(firstc, &search_delim, s, &skiplen,
&patlen)) {
restore_last_search_pattern();
return FAIL;
}
restore_last_search_pattern();
if (s->did_incsearch) {
curwin->w_cursor = s->match_end;
*c = gchar_cursor();
if (*c != NUL) {
// If 'ignorecase' and 'smartcase' are set and the
// command line has no uppercase characters, convert
// the character to lowercase
if (p_ic && p_scs
&& !pat_has_uppercase(ccline.cmdbuff + skiplen)) {
*c = mb_tolower(*c);
}
if (*c == search_delim
|| vim_strchr((magic_isset() ? "\\~^$.*[" : "\\^$"), *c) != NULL) {
// put a backslash before special characters
stuffcharReadbuff(*c);
*c = '\\';
}
return FAIL;
}
}
return OK;
}
static void finish_incsearch_highlighting(bool gotesc, incsearch_state_T *s,
bool call_update_screen)
{
if (!s->did_incsearch) {
return;
}
s->did_incsearch = false;
if (gotesc) {
curwin->w_cursor = s->save_cursor;
} else {
if (!equalpos(s->save_cursor, s->search_start)) {
// put the '" mark at the original position
curwin->w_cursor = s->save_cursor;
setpcmark();
}
curwin->w_cursor = s->search_start;
}
restore_viewstate(curwin, &s->old_viewstate);
highlight_match = false;
// by default search all lines
search_first_line = 0;
search_last_line = MAXLNUM;
magic_overruled = s->magic_overruled_save;
validate_cursor(curwin); // needed for TAB
status_redraw_all();
redraw_all_later(UPD_SOME_VALID);
if (call_update_screen) {
update_screen();
}
}
/// Initialize the current command-line info.
static void init_ccline(int firstc, int indent)
{
ccline.overstrike = false; // always start in insert mode
assert(indent >= 0);
// set some variables for redrawcmd()
ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
ccline.cmdindent = (firstc > 0 ? indent : 0);
// alloc initial ccline.cmdbuff
alloc_cmdbuff(indent + 50);
ccline.cmdlen = ccline.cmdpos = 0;
ccline.cmdbuff[0] = NUL;
ccline.last_colors = (ColoredCmdline){ .cmdbuff = NULL,
.colors = KV_INITIAL_VALUE };
sb_text_start_cmdline();
// autoindent for :insert and :append
if (firstc <= 0) {
memset(ccline.cmdbuff, ' ', (size_t)indent);
ccline.cmdbuff[indent] = NUL;
ccline.cmdpos = indent;
ccline.cmdspos = indent;
ccline.cmdlen = indent;
}
}
static void ui_ext_cmdline_hide(bool abort)
{
if (ui_has(kUICmdline)) {
cmdline_was_last_drawn = false;
ccline.redraw_state = kCmdRedrawNone;
ui_call_cmdline_hide(ccline.level, abort);
}
}
/// Internal entry point for cmdline mode.
///
/// @param count only used for incremental search
/// @param indent indent for inside conditionals
/// @param clear_ccline clear ccline first
static uint8_t *command_line_enter(int firstc, int count, int indent, bool clear_ccline)
{
// can be invoked recursively, identify each level
static int cmdline_level = 0;
cmdline_level++;
bool save_cmdpreview = cmdpreview;
cmdpreview = false;
CommandLineState state = {
.firstc = firstc,
.count = count,
.indent = indent,
.save_msg_scroll = msg_scroll,
.save_State = State,
.prev_cmdpos = -1,
.ignore_drag_release = true,
};
CommandLineState *s = &state;
s->save_p_icm = xstrdup(p_icm);
init_incsearch_state(&s->is_state);
CmdlineInfo save_ccline;
bool did_save_ccline = false;
if (ccline.cmdbuff != NULL) {
// Currently ccline can never be in use if clear_ccline is false.
// Some changes will be needed if this is no longer the case.
assert(clear_ccline);
// Being called recursively. Since ccline is global, we need to save
// the current buffer and restore it when returning.
save_cmdline(&save_ccline);
did_save_ccline = true;
} else if (clear_ccline) {
CLEAR_FIELD(ccline);
}
if (s->firstc == -1) {
s->firstc = NUL;
s->break_ctrl_c = true;
}
init_ccline(s->firstc, s->indent);
ccline.prompt_id = last_prompt_id++;
ccline.level = cmdline_level;
if (cmdline_level == 50) {
// Somehow got into a loop recursively calling getcmdline(), bail out.
emsg(_(e_command_too_recursive));
goto theend;
}
ExpandInit(&s->xpc);
ccline.xpc = &s->xpc;
cmdmsg_rl = (curwin->w_p_rl && *curwin->w_p_rlc == 's'
&& (s->firstc == '/' || s->firstc == '?'));
msg_grid_validate();
redir_off = true; // don't redirect the typed command
if (!cmd_silent) {
gotocmdline(true);
redrawcmdprompt(); // draw prompt or indent
ccline.cmdspos = cmd_startcol();
if (!msg_scroll) {
msg_ext_clear(false);
}
}
s->xpc.xp_context = EXPAND_NOTHING;
s->xpc.xp_backslash = XP_BS_NONE;
#ifndef BACKSLASH_IN_FILENAME
s->xpc.xp_shell = false;
#endif
if (ccline.input_fn) {
s->xpc.xp_context = ccline.xp_context;
s->xpc.xp_pattern = ccline.cmdbuff;
s->xpc.xp_arg = ccline.xp_arg;
}
// Avoid scrolling when called by a recursive do_cmdline(), e.g. when
// doing ":@0" when register 0 doesn't contain a CR.
msg_scroll = false;
State = MODE_CMDLINE;
if (s->firstc == '/' || s->firstc == '?' || s->firstc == '@') {
// Use ":lmap" mappings for search pattern and input().
if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) {
s->b_im_ptr = &curbuf->b_p_iminsert;
} else {
s->b_im_ptr = &curbuf->b_p_imsearch;
}
s->b_im_ptr_buf = curbuf;
if (*s->b_im_ptr == B_IMODE_LMAP) {
State |= MODE_LANGMAP;
}
}
setmouse();
setcursor();
Error err = ERROR_INIT;
char firstcbuf[2];
firstcbuf[0] = (char)(firstc > 0 ? firstc : '-');
firstcbuf[1] = 0;
if (has_event(EVENT_CMDLINEENTER)) {
save_v_event_T save_v_event;
dict_T *dict = get_v_event(&save_v_event);
// set v:event to a dictionary with information about the commandline
tv_dict_add_str(dict, S_LEN("cmdtype"), firstcbuf);
tv_dict_add_nr(dict, S_LEN("cmdlevel"), ccline.level);
tv_dict_set_keys_readonly(dict);
TRY_WRAP(&err, {
apply_autocmds(EVENT_CMDLINEENTER, firstcbuf, firstcbuf, false, curbuf);
restore_v_event(dict, &save_v_event);
});
if (ERROR_SET(&err)) {
msg_putchar('\n');
msg_scroll = true;
msg_puts_hl(err.msg, HLF_E, true);
api_clear_error(&err);
redrawcmd();
}
err = ERROR_INIT;
}
may_trigger_modechanged();
init_history();
s->hiscnt = get_hislen(); // set hiscnt to impossible history value
s->histype = hist_char2type(s->firstc);
do_digraph(-1); // init digraph typeahead
// If something above caused an error, reset the flags, we do want to type
// and execute commands. Display may be messed up a bit.
if (did_emsg) {
redrawcmd();
}
// Redraw the statusline in case it uses the current mode using the mode()
// function.
if (!cmd_silent && !exmode_active) {
bool found_one = false;
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
if (*p_stl != NUL || *wp->w_p_stl != NUL || *p_wbr != NUL || *wp->w_p_wbr != NUL) {
wp->w_redr_status = true;
found_one = true;
}
}
if (*p_tal != NUL) {
redraw_tabline = true;
found_one = true;
}
if (redraw_custom_title_later()) {
found_one = true;
}
if (found_one) {
redraw_statuslines();
}
}
did_emsg = false;
got_int = false;
s->state.check = command_line_check;
s->state.execute = command_line_execute;
state_enter(&s->state);
if (has_event(EVENT_CMDLINELEAVE)) {
save_v_event_T save_v_event;
dict_T *dict = get_v_event(&save_v_event);
tv_dict_add_str(dict, S_LEN("cmdtype"), firstcbuf);
tv_dict_add_nr(dict, S_LEN("cmdlevel"), ccline.level);
tv_dict_set_keys_readonly(dict);
// not readonly:
tv_dict_add_bool(dict, S_LEN("abort"),
s->gotesc ? kBoolVarTrue : kBoolVarFalse);
TRY_WRAP(&err, {
apply_autocmds(EVENT_CMDLINELEAVE, firstcbuf, firstcbuf, false, curbuf);
// error printed below, to avoid redraw issues
});
if (tv_dict_get_number(dict, "abort") != 0) {
s->gotesc = true;
}
restore_v_event(dict, &save_v_event);
}
cmdmsg_rl = false;
// We could have reached here without having a chance to clean up wild menu
// if certain special keys like <Esc> or <C-\> were used as wildchar. Make
// sure to still clean up to avoid memory corruption.
if (cmdline_pum_active()) {
cmdline_pum_remove();
}
wildmenu_cleanup(&ccline);
s->did_wild_list = false;
s->wim_index = 0;
ExpandCleanup(&s->xpc);
ccline.xpc = NULL;
finish_incsearch_highlighting(s->gotesc, &s->is_state, false);
if (ccline.cmdbuff != NULL) {
// Put line in history buffer (":" and "=" only when it was typed).
if (s->histype != HIST_INVALID
&& ccline.cmdlen
&& s->firstc != NUL
&& (s->some_key_typed || s->histype == HIST_SEARCH)) {
add_to_history(s->histype, ccline.cmdbuff, (size_t)ccline.cmdlen, true,
s->histype == HIST_SEARCH ? s->firstc : NUL);
if (s->firstc == ':') {
xfree(new_last_cmdline);
new_last_cmdline = xstrnsave(ccline.cmdbuff, (size_t)ccline.cmdlen);
}
}
if (s->gotesc) {
abandon_cmdline();
}
}
// If the screen was shifted up, redraw the whole screen (later).
// If the line is too long, clear it, so ruler and shown command do
// not get printed in the middle of it.
msg_check();
if (p_ch == 0 && !ui_has(kUIMessages)) {
set_must_redraw(UPD_VALID);
}
msg_scroll = s->save_msg_scroll;
redir_off = false;
if (ERROR_SET(&err)) {
msg_putchar('\n');
emsg(err.msg);
did_emsg = false;
api_clear_error(&err);
}
// When the command line was typed, no need for a wait-return prompt.
if (s->some_key_typed && !ERROR_SET(&err)) {
need_wait_return = false;
}
set_option_direct(kOptInccommand, CSTR_AS_OPTVAL(s->save_p_icm), 0, SID_NONE);
State = s->save_State;
if (cmdpreview != save_cmdpreview) {
cmdpreview = save_cmdpreview; // restore preview state
redraw_all_later(UPD_SOME_VALID);
}
may_trigger_modechanged();
setmouse();
sb_text_end_cmdline();
theend:
xfree(s->save_p_icm);
xfree(ccline.last_colors.cmdbuff);
kv_destroy(ccline.last_colors.colors);
char *p = ccline.cmdbuff;
if (ui_has(kUICmdline)) {
ui_ext_cmdline_hide(s->gotesc);
msg_ext_clear_later();
}
if (!cmd_silent) {
redraw_custom_title_later();
status_redraw_all(); // redraw to show mode change
}
cmdline_level--;
if (did_save_ccline) {
restore_cmdline(&save_ccline);
} else {
ccline.cmdbuff = NULL;
}
return (uint8_t *)p;
}
static int command_line_check(VimState *state)
{
CommandLineState *s = (CommandLineState *)state;
redir_off = true; // Don't redirect the typed command.
// Repeated, because a ":redir" inside
// completion may switch it on.
quit_more = false; // reset after CTRL-D which had a more-prompt
did_emsg = false; // There can't really be a reason why an error
// that occurs while typing a command should
// cause the command not to be executed.
// Trigger SafeState if nothing is pending.
may_trigger_safestate(s->xpc.xp_numfiles <= 0);
cursorcmd(); // set the cursor on the right spot
ui_cursor_shape();
return 1;
}
/// Handle CTRL-\ pressed in Command-line mode:
/// - CTRL-\ CTRL-N or CTRL-\ CTRL-G goes to Normal mode.
/// - CTRL-\ e prompts for an expression.
static int command_line_handle_ctrl_bsl(CommandLineState *s)
{
no_mapping++;
allow_keys++;
s->c = plain_vgetc();
no_mapping--;
allow_keys--;
// CTRL-\ e doesn't work when obtaining an expression, unless it
// is in a mapping.
if (s->c != Ctrl_N
&& s->c != Ctrl_G
&& (s->c != 'e'
|| (ccline.cmdfirstc == '=' && KeyTyped)
|| cmdline_star > 0)) {
vungetc(s->c);
return PROCESS_NEXT_KEY;
}
if (s->c == 'e') {
// Replace the command line with the result of an expression.
// This will call getcmdline() recursively in get_expr_register().
if (ccline.cmdpos == ccline.cmdlen) {
new_cmdpos = 99999; // keep it at the end
} else {
new_cmdpos = ccline.cmdpos;
}
s->c = get_expr_register();
if (s->c == '=') {
// Evaluate the expression. Set "textlock" to avoid nasty things
// like going to another buffer.
textlock++;
char *p = get_expr_line();
textlock--;
if (p != NULL) {
int len = (int)strlen(p);
realloc_cmdbuff(len + 1);
ccline.cmdlen = len;
STRCPY(ccline.cmdbuff, p);
xfree(p);
// Restore the cursor or use the position set with
// set_cmdline_pos().
ccline.cmdpos = MIN(ccline.cmdlen, new_cmdpos);
KeyTyped = false; // Don't do p_wc completion.
redrawcmd();
return CMDLINE_CHANGED;
}
}
beep_flush();
got_int = false; // don't abandon the command line
did_emsg = false;
emsg_on_display = false;
redrawcmd();
return CMDLINE_NOT_CHANGED;
}
s->gotesc = true; // will free ccline.cmdbuff after putting it in history
return GOTO_NORMAL_MODE;
}
/// Completion for 'wildchar' or 'wildcharm' key.
/// - hitting <ESC> twice means: abandon command line.
/// - wildcard expansion is only done when the 'wildchar' key is really
/// typed, not when it comes from a macro
/// @return CMDLINE_CHANGED if command line is changed or CMDLINE_NOT_CHANGED.
static int command_line_wildchar_complete(CommandLineState *s)
{
int res;
int options = WILD_NO_BEEP;
if (wim_flags[s->wim_index] & kOptWimFlagLastused) {
options |= WILD_BUFLASTUSED;
}
if (s->xpc.xp_numfiles > 0) { // typed p_wc at least twice
// if 'wildmode' contains "list" may still need to list
if (s->xpc.xp_numfiles > 1
&& !s->did_wild_list
&& ((wim_flags[s->wim_index] & kOptWimFlagList)
|| (p_wmnu && (wim_flags[s->wim_index] & kOptWimFlagFull) != 0))) {
showmatches(&s->xpc, p_wmnu && ((wim_flags[s->wim_index] & kOptWimFlagList) == 0));
redrawcmd();
s->did_wild_list = true;
}
if (wim_flags[s->wim_index] & kOptWimFlagLongest) {
res = nextwild(&s->xpc, WILD_LONGEST, options, s->firstc != '@');
} else if (wim_flags[s->wim_index] & kOptWimFlagFull) {
res = nextwild(&s->xpc, WILD_NEXT, options, s->firstc != '@');
} else {
res = OK; // don't insert 'wildchar' now
}
} else { // typed p_wc first time
s->wim_index = 0;
int j = ccline.cmdpos;
// if 'wildmode' first contains "longest", get longest
// common part
if (wim_flags[0] & kOptWimFlagLongest) {
res = nextwild(&s->xpc, WILD_LONGEST, options, s->firstc != '@');
} else {
res = nextwild(&s->xpc, WILD_EXPAND_KEEP, options, s->firstc != '@');
}
// if interrupted while completing, behave like it failed
if (got_int) {
vpeekc(); // remove <C-C> from input stream
got_int = false; // don't abandon the command line
ExpandOne(&s->xpc, NULL, NULL, 0, WILD_FREE);
s->xpc.xp_context = EXPAND_NOTHING;
return CMDLINE_CHANGED;
}
// when more than one match, and 'wildmode' first contains
// "list", or no change and 'wildmode' contains "longest,list",
// list all matches
if (res == OK && s->xpc.xp_numfiles > 1) {
// a "longest" that didn't do anything is skipped (but not
// "list:longest")
if (wim_flags[0] == kOptWimFlagLongest && ccline.cmdpos == j) {
s->wim_index = 1;
}
if ((wim_flags[s->wim_index] & kOptWimFlagList)
|| (p_wmnu && (wim_flags[s->wim_index] & kOptWimFlagFull) != 0)) {
if (!(wim_flags[0] & kOptWimFlagLongest)) {
int p_wmnu_save = p_wmnu;
p_wmnu = 0;
// remove match
nextwild(&s->xpc, WILD_PREV, 0, s->firstc != '@');
p_wmnu = p_wmnu_save;
}
showmatches(&s->xpc, p_wmnu && ((wim_flags[s->wim_index] & kOptWimFlagList) == 0));
redrawcmd();
s->did_wild_list = true;
if (wim_flags[s->wim_index] & kOptWimFlagLongest) {
nextwild(&s->xpc, WILD_LONGEST, options, s->firstc != '@');
} else if (wim_flags[s->wim_index] & kOptWimFlagFull) {
nextwild(&s->xpc, WILD_NEXT, options, s->firstc != '@');
}
} else {
vim_beep(kOptBoFlagWildmode);
}
} else if (s->xpc.xp_numfiles == -1) {
s->xpc.xp_context = EXPAND_NOTHING;
}
}
if (s->wim_index < 3) {
s->wim_index++;
}
if (s->c == ESC) {
s->gotesc = true;
}
return (res == OK) ? CMDLINE_CHANGED : CMDLINE_NOT_CHANGED;
}
static void command_line_end_wildmenu(CommandLineState *s)
{
if (cmdline_pum_active()) {
cmdline_pum_remove();
}
if (s->xpc.xp_numfiles != -1) {
ExpandOne(&s->xpc, NULL, NULL, 0, WILD_FREE);
}
s->did_wild_list = false;
if (!p_wmnu || (s->c != K_UP && s->c != K_DOWN)) {
s->xpc.xp_context = EXPAND_NOTHING;
}
s->wim_index = 0;
wildmenu_cleanup(&ccline);
}
static int command_line_execute(VimState *state, int key)
{
if (key == K_IGNORE || key == K_NOP) {
return -1; // get another key
}
disptick_T display_tick_saved = display_tick;
CommandLineState *s = (CommandLineState *)state;
s->c = key;
if (s->c == K_EVENT || s->c == K_COMMAND || s->c == K_LUA) {
if (s->c == K_EVENT) {
state_handle_k_event();
} else if (s->c == K_COMMAND) {
do_cmdline(NULL, getcmdkeycmd, NULL, DOCMD_NOWAIT);
} else {
map_execute_lua(false);
}
// Re-apply 'incsearch' highlighting in case it was cleared.
if (display_tick > display_tick_saved && s->is_state.did_incsearch) {
may_do_incsearch_highlighting(s->firstc, s->count, &s->is_state);
}
// nvim_select_popupmenu_item() can be called from the handling of
// K_EVENT, K_COMMAND, or K_LUA.
if (pum_want.active) {
if (cmdline_pum_active()) {
nextwild(&s->xpc, WILD_PUM_WANT, 0, s->firstc != '@');
if (pum_want.finish) {
nextwild(&s->xpc, WILD_APPLY, WILD_NO_BEEP, s->firstc != '@');
command_line_end_wildmenu(s);
}
}
pum_want.active = false;
}
if (!cmdline_was_last_drawn) {
redrawcmdline();
}
return 1;
}
if (KeyTyped) {
s->some_key_typed = true;
if (cmdmsg_rl && !KeyStuffed) {
// Invert horizontal movements and operations. Only when
// typed by the user directly, not when the result of a
// mapping.
switch (s->c) {
case K_RIGHT:
s->c = K_LEFT; break;
case K_S_RIGHT:
s->c = K_S_LEFT; break;
case K_C_RIGHT:
s->c = K_C_LEFT; break;
case K_LEFT:
s->c = K_RIGHT; break;
case K_S_LEFT:
s->c = K_S_RIGHT; break;
case K_C_LEFT:
s->c = K_C_RIGHT; break;
}
}
}
// Ignore got_int when CTRL-C was typed here.
// Don't ignore it in :global, we really need to break then, e.g., for
// ":g/pat/normal /pat" (without the <CR>).
// Don't ignore it for the input() function.
if ((s->c == Ctrl_C)
&& s->firstc != '@'
// do clear got_int in Ex mode to avoid infinite Ctrl-C loop
&& (!s->break_ctrl_c || exmode_active)
&& !global_busy) {
got_int = false;
}
// free old command line when finished moving around in the history
// list
if (s->lookfor != NULL
&& s->c != K_S_DOWN && s->c != K_S_UP
&& s->c != K_DOWN && s->c != K_UP
&& s->c != K_PAGEDOWN && s->c != K_PAGEUP
&& s->c != K_KPAGEDOWN && s->c != K_KPAGEUP
&& s->c != K_LEFT && s->c != K_RIGHT
&& (s->xpc.xp_numfiles > 0 || (s->c != Ctrl_P && s->c != Ctrl_N))) {
XFREE_CLEAR(s->lookfor);
s->lookforlen = 0;
}
// When there are matching completions to select <S-Tab> works like
// CTRL-P (unless 'wc' is <S-Tab>).
if (s->c != p_wc && s->c == K_S_TAB && s->xpc.xp_numfiles > 0) {
s->c = Ctrl_P;
}
if (p_wmnu) {
s->c = wildmenu_translate_key(&ccline, s->c, &s->xpc, s->did_wild_list);
}
int wild_type = 0;
const bool key_is_wc = (s->c == p_wc && KeyTyped) || s->c == p_wcm;
if ((cmdline_pum_active() || s->did_wild_list) && !key_is_wc) {
// Ctrl-Y: Accept the current selection and close the popup menu.
// Ctrl-E: cancel the cmdline popup menu and return the original text.
if (s->c == Ctrl_E || s->c == Ctrl_Y) {
wild_type = (s->c == Ctrl_E) ? WILD_CANCEL : WILD_APPLY;
nextwild(&s->xpc, wild_type, WILD_NO_BEEP, s->firstc != '@');
}
}
// The wildmenu is cleared if the pressed key is not used for
// navigating the wild menu (i.e. the key is not 'wildchar' or
// 'wildcharm' or Ctrl-N or Ctrl-P or Ctrl-A or Ctrl-L).
// If the popup menu is displayed, then PageDown and PageUp keys are
// also used to navigate the menu.
bool end_wildmenu = (!key_is_wc && s->c != Ctrl_Z
&& s->c != Ctrl_N && s->c != Ctrl_P && s->c != Ctrl_A
&& s->c != Ctrl_L);
end_wildmenu = end_wildmenu && (!cmdline_pum_active()
|| (s->c != K_PAGEDOWN && s->c != K_PAGEUP
&& s->c != K_KPAGEDOWN && s->c != K_KPAGEUP));
// free expanded names when finished walking through matches
if (end_wildmenu) {
command_line_end_wildmenu(s);
}
if (p_wmnu) {
s->c = wildmenu_process_key(&ccline, s->c, &s->xpc);
}
// CTRL-\ CTRL-N or CTRL-\ CTRL-G goes to Normal mode,
// CTRL-\ e prompts for an expression.
if (s->c == Ctrl_BSL) {
switch (command_line_handle_ctrl_bsl(s)) {
case CMDLINE_CHANGED:
return command_line_changed(s);
case CMDLINE_NOT_CHANGED:
return command_line_not_changed(s);
case GOTO_NORMAL_MODE:
return 0; // back to cmd mode
default:
s->c = Ctrl_BSL; // backslash key not processed by
// command_line_handle_ctrl_bsl()
}
}
if (s->c == cedit_key || s->c == K_CMDWIN) {
// TODO(vim): why is ex_normal_busy checked here?
if ((s->c == K_CMDWIN || ex_normal_busy == 0)
&& got_int == false) {
// Open a window to edit the command line (and history).
s->c = open_cmdwin();
s->some_key_typed = true;
}
} else {
s->c = do_digraph(s->c);
}
if (s->c == '\n'
|| s->c == '\r'
|| s->c == K_KENTER
|| (s->c == ESC
&& (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) {
// In Ex mode a backslash escapes a newline.
if (exmode_active
&& s->c != ESC
&& ccline.cmdpos == ccline.cmdlen
&& ccline.cmdpos > 0
&& ccline.cmdbuff[ccline.cmdpos - 1] == '\\') {
if (s->c == K_KENTER) {
s->c = '\n';
}
} else {
s->gotesc = false; // Might have typed ESC previously, don't
// truncate the cmdline now.
if (ccheck_abbr(s->c + ABBR_OFF)) {
return command_line_changed(s);
}
if (!cmd_silent) {
if (!ui_has(kUICmdline)) {
msg_cursor_goto(msg_row, 0);
}
ui_flush();
}
return 0;
}
}
// Completion for 'wildchar' or 'wildcharm' key.
if ((s->c == p_wc && !s->gotesc && KeyTyped) || s->c == p_wcm || s->c == Ctrl_Z) {
if (command_line_wildchar_complete(s) == CMDLINE_CHANGED) {
return command_line_changed(s);
}
}
s->gotesc = false;
// <S-Tab> goes to last match, in a clumsy way
if (s->c == K_S_TAB && KeyTyped) {
if (nextwild(&s->xpc, WILD_EXPAND_KEEP, 0, s->firstc != '@') == OK) {
if (s->xpc.xp_numfiles > 1
&& ((!s->did_wild_list && (wim_flags[s->wim_index] & kOptWimFlagList)) || p_wmnu)) {
// Trigger the popup menu when wildoptions=pum
showmatches(&s->xpc, p_wmnu && ((wim_flags[s->wim_index] & kOptWimFlagList) == 0));
}
nextwild(&s->xpc, WILD_PREV, 0, s->firstc != '@');
nextwild(&s->xpc, WILD_PREV, 0, s->firstc != '@');
return command_line_changed(s);
}
}
if (s->c == NUL || s->c == K_ZERO) {
// NUL is stored as NL
s->c = NL;
}
s->do_abbr = true; // default: check for abbreviation
// If already used to cancel/accept wildmenu, don't process the key further.
if (wild_type == WILD_CANCEL || wild_type == WILD_APPLY) {
return command_line_not_changed(s);
}
return command_line_handle_key(s);
}
// May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
// or previous match.
// Returns FAIL when calling command_line_not_changed.
static int may_do_command_line_next_incsearch(int firstc, int count, incsearch_state_T *s,
bool next_match)
FUNC_ATTR_NONNULL_ALL
{
int skiplen, patlen, search_delim;
// Parsing range may already set the last search pattern.
// NOTE: must call restore_last_search_pattern() before returning!
save_last_search_pattern();
if (!do_incsearch_highlighting(firstc, &search_delim, s, &skiplen,
&patlen)) {
restore_last_search_pattern();
return OK;
}
if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL) {
restore_last_search_pattern();
return FAIL;
}
ui_busy_start();
ui_flush();
pos_T t;
char *pat;
int search_flags = SEARCH_NOOF;
if (search_delim == ccline.cmdbuff[skiplen]) {
pat = last_search_pattern();
if (pat == NULL) {
restore_last_search_pattern();
return FAIL;
}
skiplen = 0;
patlen = (int)last_search_pattern_len();
} else {
pat = ccline.cmdbuff + skiplen;
}
if (next_match) {
t = s->match_end;
if (lt(s->match_start, s->match_end)) {
// start searching at the end of the match
// not at the beginning of the next column
decl(&t);
}
search_flags += SEARCH_COL;
} else {
t = s->match_start;
}
if (!p_hls) {
search_flags += SEARCH_KEEP;
}
emsg_off++;
char save = pat[patlen];
pat[patlen] = NUL;
int found = searchit(curwin, curbuf, &t, NULL,
next_match ? FORWARD : BACKWARD,
pat, (size_t)patlen, count, search_flags,
RE_SEARCH, NULL);
emsg_off--;
pat[patlen] = save;
ui_busy_stop();
if (found) {
s->search_start = s->match_start;
s->match_end = t;
s->match_start = t;
if (!next_match && firstc != '?') {
// move just before the current match, so that
// when nv_search finishes the cursor will be
// put back on the match
s->search_start = t;
decl(&s->search_start);
} else if (next_match && firstc == '?') {
// move just after the current match, so that
// when nv_search finishes the cursor will be
// put back on the match
s->search_start = t;
incl(&s->search_start);
}
if (lt(t, s->search_start) && next_match) {
// wrap around
s->search_start = t;
if (firstc == '?') {
incl(&s->search_start);
} else {
decl(&s->search_start);
}
}
set_search_match(&s->match_end);
curwin->w_cursor = s->match_start;
changed_cline_bef_curs(curwin);
update_topline(curwin);
validate_cursor(curwin);
highlight_match = true;
save_viewstate(curwin, &s->old_viewstate);
redraw_later(curwin, UPD_NOT_VALID);
update_screen();
highlight_match = false;
redrawcmdline();
curwin->w_cursor = s->match_end;
} else {
vim_beep(kOptBoFlagError);
}
restore_last_search_pattern();
return FAIL;
}
/// Handle backspace, delete and CTRL-W keys in the command-line mode.
static int command_line_erase_chars(CommandLineState *s)
{
if (s->c == K_KDEL) {
s->c = K_DEL;
}
// Delete current character is the same as backspace on next
// character, except at end of line
if (s->c == K_DEL && ccline.cmdpos != ccline.cmdlen) {
ccline.cmdpos++;
}
if (s->c == K_DEL) {
ccline.cmdpos += mb_off_next(ccline.cmdbuff, ccline.cmdbuff + ccline.cmdpos);
}
if (ccline.cmdpos > 0) {
int j = ccline.cmdpos;
char *p = mb_prevptr(ccline.cmdbuff, ccline.cmdbuff + j);
if (s->c == Ctrl_W) {
while (p > ccline.cmdbuff && ascii_isspace(*p)) {
p = mb_prevptr(ccline.cmdbuff, p);
}
int i = mb_get_class(p);
while (p > ccline.cmdbuff && mb_get_class(p) == i) {
p = mb_prevptr(ccline.cmdbuff, p);
}
if (mb_get_class(p) != i) {
p += utfc_ptr2len(p);
}
}
ccline.cmdpos = (int)(p - ccline.cmdbuff);
ccline.cmdlen -= j - ccline.cmdpos;
int i = ccline.cmdpos;
while (i < ccline.cmdlen) {
ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
}
// Truncate at the end, required for multi-byte chars.
ccline.cmdbuff[ccline.cmdlen] = NUL;
if (ccline.cmdlen == 0) {
s->is_state.search_start = s->is_state.save_cursor;
// save view settings, so that the screen won't be restored at the
// wrong position
s->is_state.old_viewstate = s->is_state.init_viewstate;
}
redrawcmd();
} else if (ccline.cmdlen == 0 && s->c != Ctrl_W
&& ccline.cmdprompt == NULL && s->indent == 0) {
// In ex and debug mode it doesn't make sense to return.
if (exmode_active || ccline.cmdfirstc == '>') {
return CMDLINE_NOT_CHANGED;
}
dealloc_cmdbuff(); // no commandline to return
if (!cmd_silent && !ui_has(kUICmdline)) {
msg_col = 0;
msg_putchar(' '); // delete ':'
}
s->is_state.search_start = s->is_state.save_cursor;
redraw_cmdline = true;
return GOTO_NORMAL_MODE;
}
return CMDLINE_CHANGED;
}
/// Handle the CTRL-^ key in the command-line mode and toggle the use of the
/// language :lmap mappings and/or Input Method.
static void command_line_toggle_langmap(CommandLineState *s)
{
OptInt *b_im_ptr = buf_valid(s->b_im_ptr_buf) ? s->b_im_ptr : NULL;
if (map_to_exists_mode("", MODE_LANGMAP, false)) {
// ":lmap" mappings exists, toggle use of mappings.
State ^= MODE_LANGMAP;
if (b_im_ptr != NULL) {
if (State & MODE_LANGMAP) {
*b_im_ptr = B_IMODE_LMAP;
} else {
*b_im_ptr = B_IMODE_NONE;
}
}
}
if (b_im_ptr != NULL) {
if (b_im_ptr == &curbuf->b_p_iminsert) {
set_iminsert_global(curbuf);
} else {
set_imsearch_global(curbuf);
}
}
ui_cursor_shape(); // may show different cursor shape
// Show/unshow value of 'keymap' in status lines later.
status_redraw_curbuf();
}
/// Handle the CTRL-R key in the command-line mode and insert the contents of a
/// numbered or named register.
static int command_line_insert_reg(CommandLineState *s)
{
const int save_new_cmdpos = new_cmdpos;
putcmdline('"', true);
no_mapping++;
allow_keys++;
int i = s->c = plain_vgetc(); // CTRL-R <char>
if (i == Ctrl_O) {
i = Ctrl_R; // CTRL-R CTRL-O == CTRL-R CTRL-R
}
if (i == Ctrl_R) {
s->c = plain_vgetc(); // CTRL-R CTRL-R <char>
}
no_mapping--;
allow_keys--;
// Insert the result of an expression.
new_cmdpos = -1;
if (s->c == '=') {
if (ccline.cmdfirstc == '=' // can't do this recursively
|| cmdline_star > 0) { // or when typing a password
beep_flush();
s->c = ESC;
} else {
s->c = get_expr_register();
}
}
bool literally = false;
if (s->c != ESC) { // use ESC to cancel inserting register
literally = i == Ctrl_R || is_literal_register(s->c);
cmdline_paste(s->c, literally, false);
// When there was a serious error abort getting the
// command line.
if (aborting()) {
s->gotesc = true; // will free ccline.cmdbuff after
// putting it in history
return GOTO_NORMAL_MODE;
}
KeyTyped = false; // Don't do p_wc completion.
if (new_cmdpos >= 0) {
// set_cmdline_pos() was used
ccline.cmdpos = MIN(ccline.cmdlen, new_cmdpos);
}
}
new_cmdpos = save_new_cmdpos;
// remove the double quote
ccline.special_char = NUL;
redrawcmd();
// With "literally": the command line has already changed.
// Else: the text has been stuffed, but the command line didn't change yet.
return literally ? CMDLINE_CHANGED : CMDLINE_NOT_CHANGED;
}
/// Handle the Left and Right mouse clicks in the command-line mode.
static void command_line_left_right_mouse(CommandLineState *s)
{
if (s->c == K_LEFTRELEASE || s->c == K_RIGHTRELEASE) {
s->ignore_drag_release = true;
} else {
s->ignore_drag_release = false;
}
ccline.cmdspos = cmd_startcol();
for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
ccline.cmdpos++) {
int cells = cmdline_charsize(ccline.cmdpos);
if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
&& mouse_col < ccline.cmdspos % Columns + cells) {
break;
}
// Count ">" for double-wide char that doesn't fit.
correct_screencol(ccline.cmdpos, cells, &ccline.cmdspos);
ccline.cmdpos += utfc_ptr2len(ccline.cmdbuff + ccline.cmdpos) - 1;
ccline.cmdspos += cells;
}
}
static void command_line_next_histidx(CommandLineState *s, bool next_match)
{
while (true) {
// one step backwards
if (!next_match) {
if (s->hiscnt == get_hislen()) {
// first time
s->hiscnt = *get_hisidx(s->histype);
} else if (s->hiscnt == 0 && *get_hisidx(s->histype) != get_hislen() - 1) {
s->hiscnt = get_hislen() - 1;
} else if (s->hiscnt != *get_hisidx(s->histype) + 1) {
s->hiscnt--;
} else {
// at top of list
s->hiscnt = s->save_hiscnt;
break;
}
} else { // one step forwards
// on last entry, clear the line
if (s->hiscnt == *get_hisidx(s->histype)) {
s->hiscnt = get_hislen();
break;
}
// not on a history line, nothing to do
if (s->hiscnt == get_hislen()) {
break;
}
if (s->hiscnt == get_hislen() - 1) {
// wrap around
s->hiscnt = 0;
} else {
s->hiscnt++;
}
}
if (s->hiscnt < 0 || get_histentry(s->histype)[s->hiscnt].hisstr == NULL) {
s->hiscnt = s->save_hiscnt;
break;
}
if ((s->c != K_UP && s->c != K_DOWN)
|| s->hiscnt == s->save_hiscnt
|| strncmp(get_histentry(s->histype)[s->hiscnt].hisstr,
s->lookfor, (size_t)s->lookforlen) == 0) {
break;
}
}
}
/// Handle the Up, Down, Page Up, Page down, CTRL-N and CTRL-P key in the
/// command-line mode.
static int command_line_browse_history(CommandLineState *s)
{
if (s->histype == HIST_INVALID || get_hislen() == 0 || s->firstc == NUL) {
// no history
return CMDLINE_NOT_CHANGED;
}
s->save_hiscnt = s->hiscnt;
// save current command string so it can be restored later
if (s->lookfor == NULL) {
s->lookfor = xstrnsave(ccline.cmdbuff, (size_t)ccline.cmdlen);
s->lookfor[ccline.cmdpos] = NUL;
s->lookforlen = ccline.cmdpos;
}
bool next_match = (s->c == K_DOWN || s->c == K_S_DOWN || s->c == Ctrl_N
|| s->c == K_PAGEDOWN || s->c == K_KPAGEDOWN);
command_line_next_histidx(s, next_match);
if (s->hiscnt != s->save_hiscnt) { // jumped to other entry
char *p;
int plen;
int old_firstc;
dealloc_cmdbuff();
s->xpc.xp_context = EXPAND_NOTHING;
if (s->hiscnt == get_hislen()) {
p = s->lookfor; // back to the old one
plen = s->lookforlen;
} else {
p = get_histentry(s->histype)[s->hiscnt].hisstr;
plen = (int)get_histentry(s->histype)[s->hiscnt].hisstrlen;
}
if (s->histype == HIST_SEARCH
&& p != s->lookfor
&& (old_firstc = (uint8_t)p[plen + 1]) != s->firstc) {
int len = 0;
// Correct for the separator character used when
// adding the history entry vs the one used now.
// First loop: count length.
// Second loop: copy the characters.
for (int i = 0; i <= 1; i++) {
len = 0;
for (int j = 0; p[j] != NUL; j++) {
// Replace old sep with new sep, unless it is
// escaped.
if (p[j] == old_firstc
&& (j == 0 || p[j - 1] != '\\')) {
if (i > 0) {
ccline.cmdbuff[len] = (char)s->firstc;
}
} else {
// Escape new sep, unless it is already
// escaped.
if (p[j] == s->firstc
&& (j == 0 || p[j - 1] != '\\')) {
if (i > 0) {
ccline.cmdbuff[len] = '\\';
}
len++;
}
if (i > 0) {
ccline.cmdbuff[len] = p[j];
}
}
len++;
}
if (i == 0) {
alloc_cmdbuff(len);
}
}
ccline.cmdbuff[len] = NUL;
ccline.cmdpos = ccline.cmdlen = len;
} else {
alloc_cmdbuff(plen);
STRCPY(ccline.cmdbuff, p);
ccline.cmdpos = ccline.cmdlen = plen;
}
redrawcmd();
return CMDLINE_CHANGED;
}
beep_flush();
return CMDLINE_NOT_CHANGED;
}
static int command_line_handle_key(CommandLineState *s)
{
// For one key prompt, avoid putting ESC and Ctrl_C onto cmdline.
// For all other keys, just put onto cmdline and exit.
if (ccline.one_key && s->c != ESC && s->c != Ctrl_C) {
goto end;
}
// Big switch for a typed command line character.
switch (s->c) {
case K_BS:
case Ctrl_H:
case K_DEL:
case K_KDEL:
case Ctrl_W:
switch (command_line_erase_chars(s)) {
case CMDLINE_NOT_CHANGED:
return command_line_not_changed(s);
case GOTO_NORMAL_MODE:
return 0; // back to cmd mode
default:
return command_line_changed(s);
}
case K_INS:
case K_KINS:
ccline.overstrike = !ccline.overstrike;
ui_cursor_shape(); // may show different cursor shape
may_trigger_modechanged();
status_redraw_curbuf();
redraw_statuslines();
return command_line_not_changed(s);
case Ctrl_HAT:
command_line_toggle_langmap(s);
return command_line_not_changed(s);
case Ctrl_U: {
// delete all characters left of the cursor
int j = ccline.cmdpos;
ccline.cmdlen -= j;
int i = ccline.cmdpos = 0;
while (i < ccline.cmdlen) {
ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
}
// Truncate at the end, required for multi-byte chars.
ccline.cmdbuff[ccline.cmdlen] = NUL;
if (ccline.cmdlen == 0) {
s->is_state.search_start = s->is_state.save_cursor;
}
redrawcmd();
return command_line_changed(s);
}
case ESC: // get here if p_wc != ESC or when ESC typed twice
case Ctrl_C:
// In exmode it doesn't make sense to return. Except when
// ":normal" runs out of characters. Also when highlight callback is active
// <C-c> should interrupt only it.
if ((exmode_active && (ex_normal_busy == 0 || typebuf.tb_len > 0))
|| (getln_interrupted_highlight && s->c == Ctrl_C)) {
getln_interrupted_highlight = false;
return command_line_not_changed(s);
}
s->gotesc = true; // will free ccline.cmdbuff after
// putting it in history
return 0; // back to cmd mode
case Ctrl_R: // insert register
switch (command_line_insert_reg(s)) {
case GOTO_NORMAL_MODE:
return 0; // back to cmd mode
case CMDLINE_CHANGED:
return command_line_changed(s);
default:
return command_line_not_changed(s);
}
case Ctrl_D:
if (showmatches(&s->xpc, false) == EXPAND_NOTHING) {
break; // Use ^D as normal char instead
}
wild_menu_showing = WM_LIST;
redrawcmd();
return 1; // don't do incremental search now
case K_RIGHT:
case K_S_RIGHT:
case K_C_RIGHT:
do {
if (ccline.cmdpos >= ccline.cmdlen) {
break;
}
int cells = cmdline_charsize(ccline.cmdpos);
if (KeyTyped && ccline.cmdspos + cells >= Columns * Rows) {
break;
}
ccline.cmdspos += cells;
ccline.cmdpos += utfc_ptr2len(ccline.cmdbuff + ccline.cmdpos);
} while ((s->c == K_S_RIGHT || s->c == K_C_RIGHT
|| (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
&& ccline.cmdbuff[ccline.cmdpos] != ' ');
ccline.cmdspos = cmd_screencol(ccline.cmdpos);
return command_line_not_changed(s);
case K_LEFT:
case K_S_LEFT:
case K_C_LEFT:
if (ccline.cmdpos == 0) {
return command_line_not_changed(s);
}
do {
ccline.cmdpos--;
// Move to first byte of possibly multibyte char.
ccline.cmdpos -= utf_head_off(ccline.cmdbuff,
ccline.cmdbuff + ccline.cmdpos);
ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
} while (ccline.cmdpos > 0
&& (s->c == K_S_LEFT || s->c == K_C_LEFT
|| (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
&& ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
ccline.cmdspos = cmd_screencol(ccline.cmdpos);
if (ccline.special_char != NUL) {
putcmdline(ccline.special_char, ccline.special_shift);
}
return command_line_not_changed(s);
case K_IGNORE:
// Ignore mouse event or open_cmdwin() result.
return command_line_not_changed(s);
case K_MIDDLEDRAG:
case K_MIDDLERELEASE:
return command_line_not_changed(s); // Ignore mouse
case K_MIDDLEMOUSE:
cmdline_paste(eval_has_provider("clipboard", false) ? '*' : 0, true, true);
redrawcmd();
return command_line_changed(s);
case K_LEFTDRAG:
case K_LEFTRELEASE:
case K_RIGHTDRAG:
case K_RIGHTRELEASE:
// Ignore drag and release events when the button-down wasn't
// seen before.
if (s->ignore_drag_release) {
return command_line_not_changed(s);
}
FALLTHROUGH;
case K_LEFTMOUSE:
// Return on left click above number prompt
if (ccline.mouse_used && mouse_row < cmdline_row) {
*ccline.mouse_used = true;
return 0;
}
FALLTHROUGH;
case K_RIGHTMOUSE:
command_line_left_right_mouse(s);
return command_line_not_changed(s);
// Mouse scroll wheel: ignored here
case K_MOUSEDOWN:
case K_MOUSEUP:
case K_MOUSELEFT:
case K_MOUSERIGHT:
// Alternate buttons ignored here
case K_X1MOUSE:
case K_X1DRAG:
case K_X1RELEASE:
case K_X2MOUSE:
case K_X2DRAG:
case K_X2RELEASE:
case K_MOUSEMOVE:
return command_line_not_changed(s);
case K_SELECT: // end of Select mode mapping - ignore
return command_line_not_changed(s);
case Ctrl_B: // begin of command line
case K_HOME:
case K_KHOME:
case K_S_HOME:
case K_C_HOME:
ccline.cmdpos = 0;
ccline.cmdspos = cmd_startcol();
return command_line_not_changed(s);
case Ctrl_E: // end of command line
case K_END:
case K_KEND:
case K_S_END:
case K_C_END:
ccline.cmdpos = ccline.cmdlen;
ccline.cmdspos = cmd_screencol(ccline.cmdpos);
return command_line_not_changed(s);
case Ctrl_A: // all matches
if (cmdline_pum_active()) {
// As Ctrl-A completes all the matches, close the popup
// menu (if present)
cmdline_pum_cleanup(&ccline);
}
if (nextwild(&s->xpc, WILD_ALL, 0, s->firstc != '@') == FAIL) {
break;
}
s->xpc.xp_context = EXPAND_NOTHING;
s->did_wild_list = false;
return command_line_changed(s);
case Ctrl_L:
if (may_add_char_to_search(s->firstc, &s->c, &s->is_state) == OK) {
return command_line_not_changed(s);
}
// completion: longest common part
if (nextwild(&s->xpc, WILD_LONGEST, 0, s->firstc != '@') == FAIL) {
break;
}
return command_line_changed(s);
case Ctrl_N: // next match
case Ctrl_P: // previous match
if (s->xpc.xp_numfiles > 0) {
const int wild_type = (s->c == Ctrl_P) ? WILD_PREV : WILD_NEXT;
if (nextwild(&s->xpc, wild_type, 0, s->firstc != '@') == FAIL) {
break;
}
return command_line_changed(s);
}
FALLTHROUGH;
case K_UP:
case K_DOWN:
case K_S_UP:
case K_S_DOWN:
case K_PAGEUP:
case K_KPAGEUP:
case K_PAGEDOWN:
case K_KPAGEDOWN:
if (cmdline_pum_active()
&& (s->c == K_PAGEUP || s->c == K_PAGEDOWN
|| s->c == K_KPAGEUP || s->c == K_KPAGEDOWN)) {
// If the popup menu is displayed, then PageUp and PageDown
// are used to scroll the menu.
const int wild_type =
(s->c == K_PAGEDOWN || s->c == K_KPAGEDOWN) ? WILD_PAGEDOWN : WILD_PAGEUP;
if (nextwild(&s->xpc, wild_type, 0, s->firstc != '@') == FAIL) {
break;
}
return command_line_changed(s);
} else {
switch (command_line_browse_history(s)) {
case CMDLINE_CHANGED:
return command_line_changed(s);
case GOTO_NORMAL_MODE:
return 0;
default:
return command_line_not_changed(s);
}
}
case Ctrl_G: // next match
case Ctrl_T: // previous match
if (may_do_command_line_next_incsearch(s->firstc, s->count, &s->is_state,
s->c == Ctrl_G) == FAIL) {
return command_line_not_changed(s);
}
break;
case Ctrl_V:
case Ctrl_Q:
s->ignore_drag_release = true;
putcmdline('^', true);
// Get next (two) characters.
// Do not include modifiers into the key for CTRL-SHIFT-V.
s->c = get_literal(mod_mask & MOD_MASK_SHIFT);
s->do_abbr = false; // don't do abbreviation now
ccline.special_char = NUL;
// may need to remove ^ when composing char was typed
if (utf_iscomposing_first(s->c) && !cmd_silent) {
if (ui_has(kUICmdline)) {
// TODO(bfredl): why not make unputcmdline also work with true?
unputcmdline();
} else {
draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
msg_putchar(' ');
cursorcmd();
}
}
break;
case Ctrl_K:
s->ignore_drag_release = true;
putcmdline('?', true);
s->c = get_digraph(true);
ccline.special_char = NUL;
if (s->c != NUL) {
break;
}
redrawcmd();
return command_line_not_changed(s);
case Ctrl__: // CTRL-_: switch language mode
if (!p_ari) {
break;
}
return command_line_not_changed(s);
case 'q':
// Number prompts use the mouse and return on 'q' press
if (ccline.mouse_used) {
*ccline.cmdbuff = NUL;
return 0;
}
FALLTHROUGH;
default:
// Normal character with no special meaning. Just set mod_mask
// to 0x0 so that typing Shift-Space in the GUI doesn't enter
// the string <S-Space>. This should only happen after ^V.
if (!IS_SPECIAL(s->c)) {
mod_mask = 0x0;
}
break;
}
// End of switch on command line character.
// We come here if we have a normal character.
if (s->do_abbr && (IS_SPECIAL(s->c) || !vim_iswordc(s->c))
// Add ABBR_OFF for characters above 0x100, this is
// what check_abbr() expects.
&& (ccheck_abbr((s->c >= 0x100) ? (s->c + ABBR_OFF) : s->c)
|| s->c == Ctrl_RSB)) {
return command_line_changed(s);
}
end:
// put the character in the command line
if (IS_SPECIAL(s->c) || mod_mask != 0) {
put_on_cmdline(get_special_key_name(s->c, mod_mask), -1, true);
} else {
int j = utf_char2bytes(s->c, IObuff);
IObuff[j] = NUL; // exclude composing chars
put_on_cmdline(IObuff, j, true);
}
return ccline.one_key ? 0 : command_line_changed(s);
}
/// Trigger CursorMovedC autocommands.
static void may_trigger_cursormovedc(CommandLineState *s)
{
if (ccline.cmdpos != s->prev_cmdpos) {
trigger_cmd_autocmd(get_cmdline_type(), EVENT_CURSORMOVEDC);
s->prev_cmdpos = ccline.cmdpos;
ccline.redraw_state = MAX(ccline.redraw_state, kCmdRedrawPos);
}
}
static int command_line_not_changed(CommandLineState *s)
{
may_trigger_cursormovedc(s);
// Incremental searches for "/" and "?":
// Enter command_line_not_changed() when a character has been read but the
// command line did not change. Then we only search and redraw if something
// changed in the past.
// Enter command_line_changed() when the command line did change.
if (!s->is_state.incsearch_postponed) {
return 1;
}
return command_line_changed(s);
}
/// Guess that the pattern matches everything. Only finds specific cases, such
/// as a trailing \|, which can happen while typing a pattern.
static bool empty_pattern(char *p, size_t len, int delim)
{
magic_T magic_val = MAGIC_ON;
if (len > 0) {
skip_regexp_ex(p, delim, magic_isset(), NULL, NULL, &magic_val);
} else {
return true;
}
return empty_pattern_magic(p, len, magic_val);
}
static bool empty_pattern_magic(char *p, size_t len, magic_T magic_val)
{
// remove trailing \v and the like
while (len >= 2 && p[len - 2] == '\\'
&& vim_strchr("mMvVcCZ", (uint8_t)p[len - 1]) != NULL) {
len -= 2;
}
// true, if the pattern is empty, or the pattern ends with \| and magic is
// set (or it ends with '|' and very magic is set)
return len == 0 || (len > 1 && p[len - 1] == '|'
&& ((p[len - 2] == '\\' && magic_val == MAGIC_ON)
|| (p[len - 2] != '\\' && magic_val == MAGIC_ALL)));
}
handle_T cmdpreview_get_bufnr(void)
{
return cmdpreview_bufnr;
}
int cmdpreview_get_ns(void)
{
return cmdpreview_ns;
}
/// Sets up command preview buffer.
///
/// @return Pointer to command preview buffer if succeeded, NULL if failed.
static buf_T *cmdpreview_open_buf(void)
{
buf_T *cmdpreview_buf = cmdpreview_bufnr ? buflist_findnr(cmdpreview_bufnr) : NULL;
// If preview buffer doesn't exist, open one.
if (cmdpreview_buf == NULL) {
Error err = ERROR_INIT;
handle_T bufnr = nvim_create_buf(false, true, &err);
if (ERROR_SET(&err)) {
return NULL;
}
cmdpreview_buf = buflist_findnr(bufnr);
}
// Preview buffer cannot preview itself!
if (cmdpreview_buf == curbuf) {
return NULL;
}
// Rename preview buffer.
aco_save_T aco;
aucmd_prepbuf(&aco, cmdpreview_buf);
int retv = rename_buffer("[Preview]");
aucmd_restbuf(&aco);
if (retv == FAIL) {
return NULL;
}
// Temporarily switch to preview buffer to set it up for previewing.
aucmd_prepbuf(&aco, cmdpreview_buf);
buf_clear();
curbuf->b_p_ma = true;
curbuf->b_p_ul = -1;
curbuf->b_p_tw = 0; // Reset 'textwidth' (was set by ftplugin)
aucmd_restbuf(&aco);
cmdpreview_bufnr = cmdpreview_buf->handle;
return cmdpreview_buf;
}
/// Open command preview window if it's not already open.
/// Returns to original window after opening command preview window.
///
/// @param cmdpreview_buf Pointer to command preview buffer
///
/// @return Pointer to command preview window if succeeded, NULL if failed.
static win_T *cmdpreview_open_win(buf_T *cmdpreview_buf)
FUNC_ATTR_NONNULL_ALL
{
win_T *save_curwin = curwin;
// Open preview window.
if (win_split((int)p_cwh, WSP_BOT) == FAIL) {
return NULL;
}
win_T *preview_win = curwin;
Error err = ERROR_INIT;
int result = OK;
// Switch to preview buffer
TRY_WRAP(&err, {
result = do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, cmdpreview_buf->handle, 0);
});
if (ERROR_SET(&err) || result == FAIL) {
api_clear_error(&err);
return NULL;
}
curwin->w_p_cul = false;
curwin->w_p_cuc = false;
curwin->w_p_spell = false;
curwin->w_p_fen = false;
win_enter(save_curwin, false);
return preview_win;
}
/// Closes any open command preview windows.
static void cmdpreview_close_win(void)
{
buf_T *buf = cmdpreview_bufnr ? buflist_findnr(cmdpreview_bufnr) : NULL;
if (buf != NULL) {
close_windows(buf, false);
}
}
/// Save the undo state of a buffer for command preview.
static void cmdpreview_save_undo(CpUndoInfo *cp_undoinfo, buf_T *buf)
FUNC_ATTR_NONNULL_ALL
{
cp_undoinfo->save_b_u_synced = buf->b_u_synced;
cp_undoinfo->save_b_u_oldhead = buf->b_u_oldhead;
cp_undoinfo->save_b_u_newhead = buf->b_u_newhead;
cp_undoinfo->save_b_u_curhead = buf->b_u_curhead;
cp_undoinfo->save_b_u_numhead = buf->b_u_numhead;
cp_undoinfo->save_b_u_seq_last = buf->b_u_seq_last;
cp_undoinfo->save_b_u_save_nr_last = buf->b_u_save_nr_last;
cp_undoinfo->save_b_u_seq_cur = buf->b_u_seq_cur;
cp_undoinfo->save_b_u_time_cur = buf->b_u_time_cur;
cp_undoinfo->save_b_u_save_nr_cur = buf->b_u_save_nr_cur;
cp_undoinfo->save_b_u_line_ptr = buf->b_u_line_ptr;
cp_undoinfo->save_b_u_line_lnum = buf->b_u_line_lnum;
cp_undoinfo->save_b_u_line_colnr = buf->b_u_line_colnr;
}
/// Restore the undo state of a buffer for command preview.
static void cmdpreview_restore_undo(const CpUndoInfo *cp_undoinfo, buf_T *buf)
{
buf->b_u_oldhead = cp_undoinfo->save_b_u_oldhead;
buf->b_u_newhead = cp_undoinfo->save_b_u_newhead;
buf->b_u_curhead = cp_undoinfo->save_b_u_curhead;
buf->b_u_numhead = cp_undoinfo->save_b_u_numhead;
buf->b_u_seq_last = cp_undoinfo->save_b_u_seq_last;
buf->b_u_save_nr_last = cp_undoinfo->save_b_u_save_nr_last;
buf->b_u_seq_cur = cp_undoinfo->save_b_u_seq_cur;
buf->b_u_time_cur = cp_undoinfo->save_b_u_time_cur;
buf->b_u_save_nr_cur = cp_undoinfo->save_b_u_save_nr_cur;
buf->b_u_line_ptr = cp_undoinfo->save_b_u_line_ptr;
buf->b_u_line_lnum = cp_undoinfo->save_b_u_line_lnum;
buf->b_u_line_colnr = cp_undoinfo->save_b_u_line_colnr;
if (buf->b_u_curhead == NULL) {
buf->b_u_synced = cp_undoinfo->save_b_u_synced;
}
}
/// Save current state and prepare windows and buffers for command preview.
static void cmdpreview_prepare(CpInfo *cpinfo)
FUNC_ATTR_NONNULL_ALL
{
Set(ptr_t) saved_bufs = SET_INIT;
kv_init(cpinfo->buf_info);
kv_init(cpinfo->win_info);
FOR_ALL_WINDOWS_IN_TAB(win, curtab) {
buf_T *buf = win->w_buffer;
// Don't save state of command preview buffer or preview window.
if (buf->handle == cmdpreview_bufnr) {
continue;
}
if (!set_has(ptr_t, &saved_bufs, buf)) {
CpBufInfo cp_bufinfo;
cp_bufinfo.buf = buf;
cp_bufinfo.save_b_p_ma = buf->b_p_ma;
cp_bufinfo.save_b_p_ul = buf->b_p_ul;
cp_bufinfo.save_b_changed = buf->b_changed;
cp_bufinfo.save_b_op_start = buf->b_op_start;
cp_bufinfo.save_b_op_end = buf->b_op_end;
cp_bufinfo.save_changedtick = buf_get_changedtick(buf);
cmdpreview_save_undo(&cp_bufinfo.undo_info, buf);
kv_push(cpinfo->buf_info, cp_bufinfo);
set_put(ptr_t, &saved_bufs, buf);
u_clearall(buf);
buf->b_p_ul = INT_MAX; // Make sure we can undo all changes
}
CpWinInfo cp_wininfo;
cp_wininfo.win = win;
// Save window cursor position and viewstate
cp_wininfo.save_w_cursor = win->w_cursor;
save_viewstate(win, &cp_wininfo.save_viewstate);
// Save 'cursorline' and 'cursorcolumn'
cp_wininfo.save_w_p_cul = win->w_p_cul;
cp_wininfo.save_w_p_cuc = win->w_p_cuc;
kv_push(cpinfo->win_info, cp_wininfo);
win->w_p_cul = false; // Disable 'cursorline' so it doesn't mess up the highlights
win->w_p_cuc = false; // Disable 'cursorcolumn' so it doesn't mess up the highlights
}
set_destroy(ptr_t, &saved_bufs);
cpinfo->save_hls = p_hls;
cpinfo->save_cmdmod = cmdmod;
win_size_save(&cpinfo->save_view);
save_search_patterns();
p_hls = false; // Don't show search highlighting during live substitution
cmdmod.cmod_split = 0; // Disable :leftabove/botright modifiers
cmdmod.cmod_tab = 0; // Disable :tab modifier
cmdmod.cmod_flags |= CMOD_NOSWAPFILE; // Disable swap for preview buffer
u_sync(true);
}
/// Restore the state of buffers and windows for command preview.
static void cmdpreview_restore_state(CpInfo *cpinfo)
FUNC_ATTR_NONNULL_ALL
{
for (size_t i = 0; i < cpinfo->buf_info.size; i++) {
CpBufInfo cp_bufinfo = cpinfo->buf_info.items[i];
buf_T *buf = cp_bufinfo.buf;
buf->b_changed = cp_bufinfo.save_b_changed;
// Clear preview highlights.
extmark_clear(buf, (uint32_t)cmdpreview_ns, 0, 0, MAXLNUM, MAXCOL);
if (buf->b_u_seq_cur != cp_bufinfo.undo_info.save_b_u_seq_cur) {
int count = 0;
// Calculate how many undo steps are necessary to restore earlier state.
for (u_header_T *uhp = buf->b_u_curhead ? buf->b_u_curhead : buf->b_u_newhead;
uhp != NULL;
uhp = uhp->uh_next.ptr, ++count) {}
aco_save_T aco;
aucmd_prepbuf(&aco, buf);
// Ensure all the entries will be undone
if (curbuf->b_u_synced == false) {
u_sync(true);
}
// Undo invisibly. This also moves the cursor!
if (!u_undo_and_forget(count, false)) {
abort();
}
aucmd_restbuf(&aco);
}
u_blockfree(buf);
cmdpreview_restore_undo(&cp_bufinfo.undo_info, buf);
buf->b_op_start = cp_bufinfo.save_b_op_start;
buf->b_op_end = cp_bufinfo.save_b_op_end;
if (cp_bufinfo.save_changedtick != buf_get_changedtick(buf)) {
buf_set_changedtick(buf, cp_bufinfo.save_changedtick);
}
buf->b_p_ul = cp_bufinfo.save_b_p_ul; // Restore 'undolevels'
buf->b_p_ma = cp_bufinfo.save_b_p_ma; // Restore 'modifiable'
}
for (size_t i = 0; i < cpinfo->win_info.size; i++) {
CpWinInfo cp_wininfo = cpinfo->win_info.items[i];
win_T *win = cp_wininfo.win;
// Restore window cursor position and viewstate
win->w_cursor = cp_wininfo.save_w_cursor;
restore_viewstate(win, &cp_wininfo.save_viewstate);
// Restore 'cursorline' and 'cursorcolumn'
win->w_p_cul = cp_wininfo.save_w_p_cul;
win->w_p_cuc = cp_wininfo.save_w_p_cuc;
update_topline(win);
}
cmdmod = cpinfo->save_cmdmod; // Restore cmdmod
p_hls = cpinfo->save_hls; // Restore 'hlsearch'
restore_search_patterns(); // Restore search patterns
win_size_restore(&cpinfo->save_view); // Restore window sizes
ga_clear(&cpinfo->save_view);
kv_destroy(cpinfo->win_info);
kv_destroy(cpinfo->buf_info);
}
/// Show 'inccommand' preview if command is previewable. It works like this:
/// 1. Store current undo information so we can revert to current state later.
/// 2. Execute the preview callback with the parsed command, preview buffer number and preview
/// namespace number as arguments. The preview callback sets the highlight and does the
/// changes required for the preview if needed.
/// 3. Preview callback returns 0, 1 or 2. 0 means no preview is shown. 1 means preview is shown
/// but preview window doesn't need to be opened. 2 means preview is shown and preview window
/// needs to be opened if inccommand=split.
/// 4. Use the return value of the preview callback to determine whether to
/// open the preview window or not and open preview window if needed.
/// 5. If the return value of the preview callback is not 0, update the screen while the effects
/// of the preview are still in place.
/// 6. Revert all changes made by the preview callback.
///
/// @return whether preview is shown or not.
static bool cmdpreview_may_show(CommandLineState *s)
{
// Parse the command line and return if it fails.
exarg_T ea;
CmdParseInfo cmdinfo;
// Copy the command line so we can modify it.
int cmdpreview_type = 0;
char *cmdline = xstrdup(ccline.cmdbuff);
const char *errormsg = NULL;
emsg_off++; // Block errors when parsing the command line, and don't update v:errmsg
if (!parse_cmdline(cmdline, &ea, &cmdinfo, &errormsg)) {
emsg_off--;
goto end;
}
emsg_off--;
// Check if command is previewable, if not, don't attempt to show preview
if (!(ea.argt & EX_PREVIEW)) {
undo_cmdmod(&cmdinfo.cmdmod);
goto end;
}
// Cursor may be at the end of the message grid rather than at cmdspos.
// Place it there in case preview callback flushes it. #30696
cursorcmd();
// Flush now: external cmdline may itself wish to update the screen which is
// currently disallowed during cmdpreview (no longer needed in case that changes).
cmdline_ui_flush();
// Swap invalid command range if needed
if ((ea.argt & EX_RANGE) && ea.line1 > ea.line2) {
linenr_T lnum = ea.line1;
ea.line1 = ea.line2;
ea.line2 = lnum;
}
CpInfo cpinfo;
bool icm_split = *p_icm == 's'; // inccommand=split
buf_T *cmdpreview_buf = NULL;
win_T *cmdpreview_win = NULL;
emsg_silent++; // Block error reporting as the command may be incomplete,
// but still update v:errmsg
msg_silent++; // Block messages, namely ones that prompt
block_autocmds(); // Block events
// Save current state and prepare for command preview.
cmdpreview_prepare(&cpinfo);
// Open preview buffer if inccommand=split.
if (icm_split && (cmdpreview_buf = cmdpreview_open_buf()) == NULL) {
// Failed to create preview buffer, so disable preview.
set_option_direct(kOptInccommand, STATIC_CSTR_AS_OPTVAL("nosplit"), 0, SID_NONE);
icm_split = false;
}
// Setup preview namespace if it's not already set.
if (!cmdpreview_ns) {
cmdpreview_ns = (int)nvim_create_namespace((String)STRING_INIT);
}
// Set cmdpreview state.
cmdpreview = true;
// Execute the preview callback and use its return value to determine whether to show preview or
// open the preview window. The preview callback also handles doing the changes and highlights for
// the preview.
Error err = ERROR_INIT;
TRY_WRAP(&err, {
cmdpreview_type = execute_cmd(&ea, &cmdinfo, true);
});
if (ERROR_SET(&err)) {
api_clear_error(&err);
cmdpreview_type = 0;
}
// If inccommand=split and preview callback returns 2, open preview window.
if (icm_split && cmdpreview_type == 2
&& (cmdpreview_win = cmdpreview_open_win(cmdpreview_buf)) == NULL) {
// If there's not enough room to open the preview window, just preview without the window.
cmdpreview_type = 1;
}
// If preview callback return value is nonzero, update screen now.
if (cmdpreview_type != 0) {
int save_rd = RedrawingDisabled;
RedrawingDisabled = 0;
update_screen();
RedrawingDisabled = save_rd;
}
// Close preview window if it's open.
if (icm_split && cmdpreview_type == 2 && cmdpreview_win != NULL) {
cmdpreview_close_win();
}
// Restore state.
cmdpreview_restore_state(&cpinfo);
unblock_autocmds(); // Unblock events
msg_silent--; // Unblock messages
emsg_silent--; // Unblock error reporting
redrawcmdline();
end:
xfree(cmdline);
return cmdpreview_type != 0;
}
/// Trigger CmdlineChanged autocommands.
static void do_autocmd_cmdlinechanged(int firstc)
{
if (has_event(EVENT_CMDLINECHANGED)) {
Error err = ERROR_INIT;
save_v_event_T save_v_event;
dict_T *dict = get_v_event(&save_v_event);
char firstcbuf[2];
firstcbuf[0] = (char)firstc;
firstcbuf[1] = 0;
// set v:event to a dictionary with information about the commandline
tv_dict_add_str(dict, S_LEN("cmdtype"), firstcbuf);
tv_dict_add_nr(dict, S_LEN("cmdlevel"), ccline.level);
tv_dict_set_keys_readonly(dict);
TRY_WRAP(&err, {
apply_autocmds(EVENT_CMDLINECHANGED, firstcbuf, firstcbuf, false, curbuf);
restore_v_event(dict, &save_v_event);
});
if (ERROR_SET(&err)) {
msg_putchar('\n');
msg_scroll = true;
msg_puts_hl(err.msg, HLF_E, true);
api_clear_error(&err);
redrawcmd();
}
}
}
static int command_line_changed(CommandLineState *s)
{
// Trigger CmdlineChanged autocommands.
do_autocmd_cmdlinechanged(s->firstc > 0 ? s->firstc : '-');
may_trigger_cursormovedc(s);
const bool prev_cmdpreview = cmdpreview;
if (s->firstc == ':'
&& current_sctx.sc_sid == 0 // only if interactive
&& *p_icm != NUL // 'inccommand' is set
&& !exmode_active // not in ex mode
&& cmdline_star == 0 // not typing a password
&& !vpeekc_any()
&& cmdpreview_may_show(s)) {
// 'inccommand' preview has been shown.
} else {
cmdpreview = false;
if (prev_cmdpreview) {
// TODO(bfredl): add an immediate redraw flag for cmdline mode which will trigger
// at next wait-for-input
update_screen(); // Clear 'inccommand' preview.
}
if (s->xpc.xp_context == EXPAND_NOTHING && (KeyTyped || vpeekc() == NUL)) {
may_do_incsearch_highlighting(s->firstc, s->count, &s->is_state);
}
}
if (p_arshape && !p_tbidi) {
// Always redraw the whole command line to fix shaping and
// right-left typing. Not efficient, but it works.
// Do it only when there are no characters left to read
// to avoid useless intermediate redraws.
// if cmdline is external the ui handles shaping, no redraw needed.
if (!ui_has(kUICmdline) && vpeekc() == NUL) {
redrawcmd();
}
}
return 1;
}
/// Abandon the command line.
static void abandon_cmdline(void)
{
dealloc_cmdbuff();
if (msg_scrolled == 0) {
compute_cmdrow();
}
// Avoid overwriting key prompt
if (!ccline.one_key) {
msg("", 0);
redraw_cmdline = true;
}
}
/// getcmdline() - accept a command line starting with firstc.
///
/// firstc == ':' get ":" command line.
/// firstc == '/' or '?' get search pattern
/// firstc == '=' get expression
/// firstc == '@' get text for input() function
/// firstc == '>' get text for debug mode
/// firstc == NUL get text for :insert command
/// firstc == -1 like NUL, and break on CTRL-C
///
/// The line is collected in ccline.cmdbuff, which is reallocated to fit the
/// command line.
///
/// Careful: getcmdline() can be called recursively!
///
/// Return pointer to allocated string if there is a commandline, NULL
/// otherwise.
///
/// @param count only used for incremental search
/// @param indent indent for inside conditionals
char *getcmdline(int firstc, int count, int indent, bool do_concat FUNC_ATTR_UNUSED)
{
return (char *)command_line_enter(firstc, count, indent, true);
}
/// Get a command line with a prompt
///
/// This is prepared to be called recursively from getcmdline() (e.g. by
/// f_input() when evaluating an expression from `<C-r>=`).
///
/// @param[in] firstc Prompt type: e.g. '@' for input(), '>' for debug.
/// @param[in] prompt Prompt string: what is displayed before the user text.
/// @param[in] hl_id Prompt highlight id.
/// @param[in] xp_context Type of expansion.
/// @param[in] xp_arg User-defined expansion argument.
/// @param[in] highlight_callback Callback used for highlighting user input.
/// @param[in] one_key Return after one key press for button prompt.
/// @param[in] mouse_used Set to true when returning after right mouse click.
///
/// @return [allocated] Command line or NULL.
char *getcmdline_prompt(const int firstc, const char *const prompt, const int hl_id,
const int xp_context, const char *const xp_arg,
const Callback highlight_callback, bool one_key, bool *mouse_used)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_MALLOC
{
const int msg_col_save = msg_col;
CmdlineInfo save_ccline;
bool did_save_ccline = false;
if (ccline.cmdbuff != NULL) {
// Save the values of the current cmdline and restore them below.
save_cmdline(&save_ccline);
did_save_ccline = true;
} else {
CLEAR_FIELD(ccline);
}
ccline.prompt_id = last_prompt_id++;
ccline.cmdprompt = (char *)prompt;
ccline.hl_id = hl_id;
ccline.xp_context = xp_context;
ccline.xp_arg = (char *)xp_arg;
ccline.input_fn = (firstc == '@');
ccline.highlight_callback = highlight_callback;
ccline.one_key = one_key;
ccline.mouse_used = mouse_used;
const bool cmd_silent_saved = cmd_silent;
int msg_silent_saved = msg_silent;
msg_silent = 0;
cmd_silent = false; // Want to see the prompt.
char *const ret = (char *)command_line_enter(firstc, 1, 0, false);
ccline.redraw_state = kCmdRedrawNone;
if (did_save_ccline) {
restore_cmdline(&save_ccline);
}
msg_silent = msg_silent_saved;
cmd_silent = cmd_silent_saved;
// Restore msg_col, the prompt from input() may have changed it.
// But only if called recursively and the commandline is therefore being
// restored to an old one; if not, the input() prompt stays on the screen,
// so we need its modified msg_col left intact.
if (ccline.cmdbuff != NULL) {
msg_col = msg_col_save;
}
return ret;
}
/// Read the 'wildmode' option, fill wim_flags[].
int check_opt_wim(void)
{
uint8_t new_wim_flags[4];
int i;
int idx = 0;
for (i = 0; i < 4; i++) {
new_wim_flags[i] = 0;
}
for (char *p = p_wim; *p; p++) {
// Note: Keep this in sync with opt_wim_values.
for (i = 0; ASCII_ISALPHA(p[i]); i++) {}
if (p[i] != NUL && p[i] != ',' && p[i] != ':') {
return FAIL;
}
if (i == 7 && strncmp(p, "longest", 7) == 0) {
new_wim_flags[idx] |= kOptWimFlagLongest;
} else if (i == 4 && strncmp(p, "full", 4) == 0) {
new_wim_flags[idx] |= kOptWimFlagFull;
} else if (i == 4 && strncmp(p, "list", 4) == 0) {
new_wim_flags[idx] |= kOptWimFlagList;
} else if (i == 8 && strncmp(p, "lastused", 8) == 0) {
new_wim_flags[idx] |= kOptWimFlagLastused;
} else {
return FAIL;
}
p += i;
if (*p == NUL) {
break;
}
if (*p == ',') {
if (idx == 3) {
return FAIL;
}
idx++;
}
}
// fill remaining entries with last flag
while (idx < 3) {
new_wim_flags[idx + 1] = new_wim_flags[idx];
idx++;
}
// only when there are no errors, wim_flags[] is changed
for (i = 0; i < 4; i++) {
wim_flags[i] = new_wim_flags[i];
}
return OK;
}
/// Return true when the text must not be changed and we can't switch to
/// another window or buffer. True when editing the command line etc.
bool text_locked(void)
{
if (cmdwin_type != 0) {
return true;
}
if (expr_map_locked()) {
return true;
}
return textlock != 0;
}
// Give an error message for a command that isn't allowed while the cmdline
// window is open or editing the cmdline in another way.
void text_locked_msg(void)
{
emsg(_(get_text_locked_msg()));
}
const char *get_text_locked_msg(void)
{
if (cmdwin_type != 0) {
return e_cmdwin;
} else {
return e_textlock;
}
}
/// Check for text, window or buffer locked.
/// Give an error message and return true if something is locked.
bool text_or_buf_locked(void)
{
if (text_locked()) {
text_locked_msg();
return true;
}
return curbuf_locked();
}
/// Check if "curbuf->b_ro_locked" or "allbuf_lock" is set and
/// return true when it is and give an error message.
bool curbuf_locked(void)
{
if (curbuf->b_ro_locked > 0) {
emsg(_(e_cannot_edit_other_buf));
return true;
}
return allbuf_locked();
}
// Check if "allbuf_lock" is set and return true when it is and give an error
// message.
bool allbuf_locked(void)
{
if (allbuf_lock > 0) {
emsg(_("E811: Not allowed to change buffer information now"));
return true;
}
return false;
}
static int cmdline_charsize(int idx)
{
if (cmdline_star > 0) { // showing '*', always 1 position
return 1;
}
return ptr2cells(ccline.cmdbuff + idx);
}
/// Compute the offset of the cursor on the command line for the prompt and
/// indent.
static int cmd_startcol(void)
{
return ccline.cmdindent + ((ccline.cmdfirstc != NUL) ? 1 : 0);
}
/// Compute the column position for a byte position on the command line.
int cmd_screencol(int bytepos)
{
int m; // maximum column
int col = cmd_startcol();
if (KeyTyped) {
m = Columns * Rows;
if (m < 0) { // overflow, Columns or Rows at weird value
m = MAXCOL;
}
} else {
m = MAXCOL;
}
for (int i = 0; i < ccline.cmdlen && i < bytepos;
i += utfc_ptr2len(ccline.cmdbuff + i)) {
int c = cmdline_charsize(i);
// Count ">" for double-wide multi-byte char that doesn't fit.
correct_screencol(i, c, &col);
// If the cmdline doesn't fit, show cursor on last visible char.
// Don't move the cursor itself, so we can still append.
if ((col += c) >= m) {
col -= c;
break;
}
}
return col;
}
/// Check if the character at "idx", which is "cells" wide, is a multi-byte
/// character that doesn't fit, so that a ">" must be displayed.
static void correct_screencol(int idx, int cells, int *col)
{
if (utfc_ptr2len(ccline.cmdbuff + idx) > 1
&& utf_ptr2cells(ccline.cmdbuff + idx) > 1
&& (*col) % Columns + cells > Columns) {
(*col)++;
}
}
/// Get an Ex command line for the ":" command.
///
/// @param c normally ':', NUL for ":append"
/// @param indent indent for inside conditionals
char *getexline(int c, void *cookie, int indent, bool do_concat)
{
// When executing a register, remove ':' that's in front of each line.
if (exec_from_reg && vpeekc() == ':') {
vgetc();
}
return getcmdline(c, 1, indent, do_concat);
}
bool cmdline_overstrike(void)
FUNC_ATTR_PURE
{
return ccline.overstrike;
}
/// Return true if the cursor is at the end of the cmdline.
bool cmdline_at_end(void)
FUNC_ATTR_PURE
{
return (ccline.cmdpos >= ccline.cmdlen);
}
/// Deallocate a command line buffer, updating the buffer size and length.
static void dealloc_cmdbuff(void)
{
XFREE_CLEAR(ccline.cmdbuff);
ccline.cmdlen = ccline.cmdbufflen = 0;
}
/// Allocate a new command line buffer.
/// Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
static void alloc_cmdbuff(int len)
{
// give some extra space to avoid having to allocate all the time
if (len < 80) {
len = 100;
} else {
len += 20;
}
ccline.cmdbuff = xmalloc((size_t)len);
ccline.cmdbufflen = len;
}
/// Re-allocate the command line to length len + something extra.
void realloc_cmdbuff(int len)
{
if (len < ccline.cmdbufflen) {
return; // no need to resize
}
char *p = ccline.cmdbuff;
alloc_cmdbuff(len); // will get some more
// There isn't always a NUL after the command, but it may need to be
// there, thus copy up to the NUL and add a NUL.
memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
ccline.cmdbuff[ccline.cmdlen] = NUL;
if (ccline.xpc != NULL
&& ccline.xpc->xp_pattern != NULL
&& ccline.xpc->xp_context != EXPAND_NOTHING
&& ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) {
int i = (int)(ccline.xpc->xp_pattern - p);
// If xp_pattern points inside the old cmdbuff it needs to be adjusted
// to point into the newly allocated memory.
if (i >= 0 && i <= ccline.cmdlen) {
ccline.xpc->xp_pattern = ccline.cmdbuff + i;
}
}
xfree(p);
}
enum { MAX_CB_ERRORS = 1, };
/// Color expression cmdline using built-in expressions parser
///
/// @param[in] colored_ccline Command-line to color.
/// @param[out] ret_ccline_colors What should be colored.
///
/// Always colors the whole cmdline.
static void color_expr_cmdline(const CmdlineInfo *const colored_ccline,
ColoredCmdline *const ret_ccline_colors)
FUNC_ATTR_NONNULL_ALL
{
ParserLine parser_lines[] = {
{
.data = colored_ccline->cmdbuff,
.size = strlen(colored_ccline->cmdbuff),
.allocated = false,
},
{ NULL, 0, false },
};
ParserLine *plines_p = parser_lines;
ParserHighlight colors;
kvi_init(colors);
ParserState pstate;
viml_parser_init(&pstate, parser_simple_get_line, &plines_p, &colors);
ExprAST east = viml_pexpr_parse(&pstate, kExprFlagsDisallowEOC);
viml_pexpr_free_ast(east);
viml_parser_destroy(&pstate);
kv_resize(ret_ccline_colors->colors, kv_size(colors));
size_t prev_end = 0;
for (size_t i = 0; i < kv_size(colors); i++) {
const ParserHighlightChunk chunk = kv_A(colors, i);
assert(chunk.start.col < INT_MAX);
assert(chunk.end_col < INT_MAX);
if (chunk.start.col != prev_end) {
kv_push(ret_ccline_colors->colors, ((CmdlineColorChunk) {
.start = (int)prev_end,
.end = (int)chunk.start.col,
.hl_id = 0,
}));
}
kv_push(ret_ccline_colors->colors, ((CmdlineColorChunk) {
.start = (int)chunk.start.col,
.end = (int)chunk.end_col,
.hl_id = syn_name2id(chunk.group),
}));
prev_end = chunk.end_col;
}
if (prev_end < (size_t)colored_ccline->cmdlen) {
kv_push(ret_ccline_colors->colors, ((CmdlineColorChunk) {
.start = (int)prev_end,
.end = colored_ccline->cmdlen,
.hl_id = 0,
}));
}
kvi_destroy(colors);
}
/// Color command-line
///
/// Should use built-in command parser or user-specified one. Currently only the
/// latter is supported.
///
/// @param[in,out] colored_ccline Command-line to color. Also holds a cache:
/// if ->prompt_id and ->cmdbuff values happen
/// to be equal to those from colored_cmdline it
/// will just do nothing, assuming that ->colors
/// already contains needed data.
///
/// Always colors the whole cmdline.
///
/// @return true if draw_cmdline may proceed, false if it does not need anything
/// to do.
static bool color_cmdline(CmdlineInfo *colored_ccline)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
bool printed_errmsg = false;
#define PRINT_ERRMSG(...) \
do { \
msg_scroll = true; \
msg_putchar('\n'); \
smsg(HLF_E, __VA_ARGS__); \
printed_errmsg = true; \
} while (0)
bool ret = true;
ColoredCmdline *ccline_colors = &colored_ccline->last_colors;
// Check whether result of the previous call is still valid.
if (ccline_colors->prompt_id == colored_ccline->prompt_id
&& ccline_colors->cmdbuff != NULL
&& strcmp(ccline_colors->cmdbuff, colored_ccline->cmdbuff) == 0) {
return ret;
}
kv_size(ccline_colors->colors) = 0;
if (colored_ccline->cmdbuff == NULL || *colored_ccline->cmdbuff == NUL) {
// Nothing to do, exiting.
XFREE_CLEAR(ccline_colors->cmdbuff);
return ret;
}
bool arg_allocated = false;
typval_T arg = {
.v_type = VAR_STRING,
.vval.v_string = colored_ccline->cmdbuff,
};
typval_T tv = { .v_type = VAR_UNKNOWN };
static unsigned prev_prompt_id = UINT_MAX;
static int prev_prompt_errors = 0;
Callback color_cb = CALLBACK_NONE;
bool can_free_cb = false;
Error err = ERROR_INIT;
const char *err_errmsg = e_intern2;
bool dgc_ret = true;
if (colored_ccline->prompt_id != prev_prompt_id) {
prev_prompt_errors = 0;
prev_prompt_id = colored_ccline->prompt_id;
} else if (prev_prompt_errors >= MAX_CB_ERRORS) {
goto color_cmdline_end;
}
if (colored_ccline->highlight_callback.type != kCallbackNone) {
// Currently this should only happen while processing input() prompts.
assert(colored_ccline->input_fn);
color_cb = colored_ccline->highlight_callback;
} else if (colored_ccline->cmdfirstc == ':') {
TRY_WRAP(&err, {
err_errmsg = N_("E5408: Unable to get g:Nvim_color_cmdline callback: %s");
dgc_ret = tv_dict_get_callback(&globvardict, S_LEN("Nvim_color_cmdline"),
&color_cb);
});
can_free_cb = true;
} else if (colored_ccline->cmdfirstc == '=') {
color_expr_cmdline(colored_ccline, ccline_colors);
}
if (ERROR_SET(&err) || !dgc_ret) {
goto color_cmdline_error;
}
if (color_cb.type == kCallbackNone) {
goto color_cmdline_end;
}
if (colored_ccline->cmdbuff[colored_ccline->cmdlen] != NUL) {
arg_allocated = true;
arg.vval.v_string = xmemdupz(colored_ccline->cmdbuff, (size_t)colored_ccline->cmdlen);
}
// msg_start() called by e.g. :echo may shift command-line to the first column
// even though msg_silent is here. Two ways to workaround this problem without
// altering message.c: use full_screen or save and restore msg_col.
//
// Saving and restoring full_screen does not work well with :redraw!. Saving
// and restoring msg_col is neither ideal, but while with full_screen it
// appears shifted one character to the right and cursor position is no longer
// correct, with msg_col it just misses leading `:`. Since `redraw!` in
// callback lags this is least of the user problems.
//
// Also using TRY_WRAP because error messages may overwrite typed
// command-line which is not expected.
getln_interrupted_highlight = false;
bool cbcall_ret = true;
TRY_WRAP(&err, {
err_errmsg = N_("E5407: Callback has thrown an exception: %s");
const int saved_msg_col = msg_col;
msg_silent++;
cbcall_ret = callback_call(&color_cb, 1, &arg, &tv);
msg_silent--;
msg_col = saved_msg_col;
if (got_int) {
getln_interrupted_highlight = true;
}
});
if (ERROR_SET(&err) || !cbcall_ret) {
goto color_cmdline_error;
}
if (tv.v_type != VAR_LIST) {
PRINT_ERRMSG("%s", _("E5400: Callback should return list"));
goto color_cmdline_error;
}
if (tv.vval.v_list == NULL) {
goto color_cmdline_end;
}
varnumber_T prev_end = 0;
int i = 0;
TV_LIST_ITER_CONST(tv.vval.v_list, li, {
if (TV_LIST_ITEM_TV(li)->v_type != VAR_LIST) {
PRINT_ERRMSG(_("E5401: List item %i is not a List"), i);
goto color_cmdline_error;
}
const list_T *const l = TV_LIST_ITEM_TV(li)->vval.v_list;
if (tv_list_len(l) != 3) {
PRINT_ERRMSG(_("E5402: List item %i has incorrect length: %d /= 3"),
i, tv_list_len(l));
goto color_cmdline_error;
}
bool error = false;
const varnumber_T start = (
tv_get_number_chk(TV_LIST_ITEM_TV(tv_list_first(l)), &error));
if (error) {
goto color_cmdline_error;
} else if (!(prev_end <= start && start < colored_ccline->cmdlen)) {
PRINT_ERRMSG(_("E5403: Chunk %i start %" PRIdVARNUMBER " not in range "
"[%" PRIdVARNUMBER ", %i)"),
i, start, prev_end, colored_ccline->cmdlen);
goto color_cmdline_error;
} else if (utf8len_tab_zero[(uint8_t)colored_ccline->cmdbuff[start]] == 0) {
PRINT_ERRMSG(_("E5405: Chunk %i start %" PRIdVARNUMBER " splits "
"multibyte character"), i, start);
goto color_cmdline_error;
}
if (start != prev_end) {
kv_push(ccline_colors->colors, ((CmdlineColorChunk) {
.start = (int)prev_end,
.end = (int)start,
.hl_id = 0,
}));
}
const varnumber_T end =
tv_get_number_chk(TV_LIST_ITEM_TV(TV_LIST_ITEM_NEXT(l, tv_list_first(l))), &error);
if (error) {
goto color_cmdline_error;
} else if (!(start < end && end <= colored_ccline->cmdlen)) {
PRINT_ERRMSG(_("E5404: Chunk %i end %" PRIdVARNUMBER " not in range "
"(%" PRIdVARNUMBER ", %i]"),
i, end, start, colored_ccline->cmdlen);
goto color_cmdline_error;
} else if (end < colored_ccline->cmdlen
&& (utf8len_tab_zero[(uint8_t)colored_ccline->cmdbuff[end]]
== 0)) {
PRINT_ERRMSG(_("E5406: Chunk %i end %" PRIdVARNUMBER " splits multibyte "
"character"), i, end);
goto color_cmdline_error;
}
prev_end = end;
const char *const group = tv_get_string_chk(TV_LIST_ITEM_TV(tv_list_last(l)));
if (group == NULL) {
goto color_cmdline_error;
}
kv_push(ccline_colors->colors, ((CmdlineColorChunk) {
.start = (int)start,
.end = (int)end,
.hl_id = syn_name2id(group),
}));
i++;
});
if (prev_end < colored_ccline->cmdlen) {
kv_push(ccline_colors->colors, ((CmdlineColorChunk) {
.start = (int)prev_end,
.end = colored_ccline->cmdlen,
.hl_id = 0,
}));
}
prev_prompt_errors = 0;
color_cmdline_end:
assert(!ERROR_SET(&err));
if (can_free_cb) {
callback_free(&color_cb);
}
xfree(ccline_colors->cmdbuff);
// Note: errors “output” is cached just as well as regular results.
ccline_colors->prompt_id = colored_ccline->prompt_id;
if (arg_allocated) {
ccline_colors->cmdbuff = arg.vval.v_string;
} else {
ccline_colors->cmdbuff = xmemdupz(colored_ccline->cmdbuff, (size_t)colored_ccline->cmdlen);
}
tv_clear(&tv);
return ret;
color_cmdline_error:
if (ERROR_SET(&err)) {
PRINT_ERRMSG(_(err_errmsg), err.msg);
api_clear_error(&err);
}
assert(printed_errmsg);
(void)printed_errmsg;
prev_prompt_errors++;
kv_size(ccline_colors->colors) = 0;
redrawcmdline();
ret = false;
goto color_cmdline_end;
#undef PRINT_ERRMSG
}
// Draw part of the cmdline at the current cursor position. But draw stars
// when cmdline_star is true.
static void draw_cmdline(int start, int len)
{
if (ccline.cmdbuff == NULL || !color_cmdline(&ccline)) {
return;
}
if (ui_has(kUICmdline)) {
ccline.special_char = NUL;
ccline.redraw_state = kCmdRedrawAll;
return;
}
if (cmdline_star > 0) {
for (int i = 0; i < len; i++) {
msg_putchar('*');
i += utfc_ptr2len(ccline.cmdbuff + start + i) - 1;
}
} else {
if (kv_size(ccline.last_colors.colors)) {
for (size_t i = 0; i < kv_size(ccline.last_colors.colors); i++) {
CmdlineColorChunk chunk = kv_A(ccline.last_colors.colors, i);
if (chunk.end <= start) {
continue;
}
const int chunk_start = MAX(chunk.start, start);
msg_outtrans_len(ccline.cmdbuff + chunk_start, chunk.end - chunk_start, chunk.hl_id, false);
}
} else {
msg_outtrans_len(ccline.cmdbuff + start, len, 0, false);
}
}
}
static void ui_ext_cmdline_show(CmdlineInfo *line)
{
Arena arena = ARENA_EMPTY;
Array content;
if (cmdline_star) {
content = arena_array(&arena, 1);
size_t len = 0;
for (char *p = ccline.cmdbuff; *p; MB_PTR_ADV(p)) {
len++;
}
char *buf = arena_alloc(&arena, len, false);
memset(buf, '*', len);
Array item = arena_array(&arena, 2);
ADD_C(item, INTEGER_OBJ(0));
ADD_C(item, STRING_OBJ(cbuf_as_string(buf, len)));
ADD_C(content, ARRAY_OBJ(item));
} else if (kv_size(line->last_colors.colors)) {
content = arena_array(&arena, kv_size(line->last_colors.colors));
for (size_t i = 0; i < kv_size(line->last_colors.colors); i++) {
CmdlineColorChunk chunk = kv_A(line->last_colors.colors, i);
Array item = arena_array(&arena, 2);
ADD_C(item, INTEGER_OBJ(chunk.hl_id == 0 ? 0 : syn_id2attr(chunk.hl_id)));
assert(chunk.end >= chunk.start);
ADD_C(item, STRING_OBJ(cbuf_as_string(line->cmdbuff + chunk.start,
(size_t)(chunk.end - chunk.start))));
ADD_C(content, ARRAY_OBJ(item));
}
} else {
Array item = arena_array(&arena, 2);
ADD_C(item, INTEGER_OBJ(0));
ADD_C(item, CSTR_AS_OBJ(line->cmdbuff));
content = arena_array(&arena, 1);
ADD_C(content, ARRAY_OBJ(item));
}
char charbuf[2] = { (char)line->cmdfirstc, 0 };
ui_call_cmdline_show(content, line->cmdpos,
cstr_as_string(charbuf),
cstr_as_string((line->cmdprompt)),
line->cmdindent, line->level, line->hl_id);
if (line->special_char) {
charbuf[0] = line->special_char;
ui_call_cmdline_special_char(cstr_as_string(charbuf),
line->special_shift,
line->level);
}
arena_mem_free(arena_finish(&arena));
}
void ui_ext_cmdline_block_append(size_t indent, const char *line)
{
char *buf = xmallocz(indent + strlen(line));
memset(buf, ' ', indent);
memcpy(buf + indent, line, strlen(line));
Array item = ARRAY_DICT_INIT;
ADD(item, INTEGER_OBJ(0));
ADD(item, CSTR_AS_OBJ(buf));
Array content = ARRAY_DICT_INIT;
ADD(content, ARRAY_OBJ(item));
ADD(cmdline_block, ARRAY_OBJ(content));
if (cmdline_block.size > 1) {
ui_call_cmdline_block_append(content);
} else {
ui_call_cmdline_block_show(cmdline_block);
}
}
void ui_ext_cmdline_block_leave(void)
{
api_free_array(cmdline_block);
cmdline_block = (Array)ARRAY_DICT_INIT;
ui_call_cmdline_block_hide();
}
/// Extra redrawing needed for redraw! and on ui_attach.
void cmdline_screen_cleared(void)
{
if (!ui_has(kUICmdline)) {
return;
}
if (cmdline_block.size) {
ui_call_cmdline_block_show(cmdline_block);
}
int prev_level = ccline.level - 1;
CmdlineInfo *line = ccline.prev_ccline;
while (prev_level > 0 && line) {
if (line->level == prev_level) {
// don't redraw a cmdline already shown in the cmdline window
if (prev_level != cmdwin_level) {
line->redraw_state = kCmdRedrawAll;
}
prev_level--;
}
line = line->prev_ccline;
}
redrawcmd();
}
/// called by ui_flush, do what redraws necessary to keep cmdline updated.
void cmdline_ui_flush(void)
{
if (!ui_has(kUICmdline)) {
return;
}
int level = ccline.level;
CmdlineInfo *line = &ccline;
while (level > 0 && line) {
if (line->level == level) {
CmdRedraw redraw_state = line->redraw_state;
line->redraw_state = kCmdRedrawNone;
if (redraw_state == kCmdRedrawAll) {
cmdline_was_last_drawn = true;
ui_ext_cmdline_show(line);
} else if (redraw_state == kCmdRedrawPos && cmdline_was_last_drawn) {
ui_call_cmdline_pos(line->cmdpos, line->level);
}
level--;
}
line = line->prev_ccline;
}
}
// Put a character on the command line. Shifts the following text to the
// right when "shift" is true. Used for CTRL-V, CTRL-K, etc.
// "c" must be printable (fit in one display cell)!
void putcmdline(char c, bool shift)
{
if (cmd_silent) {
return;
}
if (!ui_has(kUICmdline)) {
msg_no_more = true;
msg_putchar(c);
if (shift) {
draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
}
msg_no_more = false;
} else if (ccline.redraw_state != kCmdRedrawAll) {
char charbuf[2] = { c, 0 };
ui_call_cmdline_special_char(cstr_as_string(charbuf), shift,
ccline.level);
}
cursorcmd();
ccline.special_char = c;
ccline.special_shift = shift;
ui_cursor_shape();
}
/// Undo a putcmdline(c, false).
void unputcmdline(void)
{
if (cmd_silent) {
return;
}
msg_no_more = true;
if (ccline.cmdlen == ccline.cmdpos && !ui_has(kUICmdline)) {
msg_putchar(' ');
} else {
draw_cmdline(ccline.cmdpos, utfc_ptr2len(ccline.cmdbuff + ccline.cmdpos));
}
msg_no_more = false;
cursorcmd();
ccline.special_char = NUL;
ui_cursor_shape();
}
// Put the given string, of the given length, onto the command line.
// If len is -1, then strlen() is used to calculate the length.
// If 'redraw' is true then the new part of the command line, and the remaining
// part will be redrawn, otherwise it will not. If this function is called
// twice in a row, then 'redraw' should be false and redrawcmd() should be
// called afterwards.
void put_on_cmdline(const char *str, int len, bool redraw)
{
if (len < 0) {
len = (int)strlen(str);
}
realloc_cmdbuff(ccline.cmdlen + len + 1);
if (!ccline.overstrike) {
memmove(ccline.cmdbuff + ccline.cmdpos + len,
ccline.cmdbuff + ccline.cmdpos,
(size_t)(ccline.cmdlen - ccline.cmdpos));
ccline.cmdlen += len;
} else {
// Count nr of characters in the new string.
int m = 0;
int i;
for (i = 0; i < len; i += utfc_ptr2len(str + i)) {
m++;
}
// Count nr of bytes in cmdline that are overwritten by these
// characters.
for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
i += utfc_ptr2len(ccline.cmdbuff + i)) {
m--;
}
if (i < ccline.cmdlen) {
memmove(ccline.cmdbuff + ccline.cmdpos + len,
ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
ccline.cmdlen += ccline.cmdpos + len - i;
} else {
ccline.cmdlen = ccline.cmdpos + len;
}
}
memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
ccline.cmdbuff[ccline.cmdlen] = NUL;
// When the inserted text starts with a composing character,
// backup to the character before it.
if (ccline.cmdpos > 0 && (uint8_t)ccline.cmdbuff[ccline.cmdpos] >= 0x80) {
int i = utf_head_off(ccline.cmdbuff, ccline.cmdbuff + ccline.cmdpos);
if (i != 0) {
ccline.cmdpos -= i;
len += i;
ccline.cmdspos = cmd_screencol(ccline.cmdpos);
}
}
if (redraw && !cmd_silent) {
msg_no_more = true;
int i = cmdline_row;
cursorcmd();
draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
// Avoid clearing the rest of the line too often.
if (cmdline_row != i || ccline.overstrike) {
msg_clr_eos();
}
msg_no_more = false;
}
int m;
if (KeyTyped) {
m = Columns * Rows;
if (m < 0) { // overflow, Columns or Rows at weird value
m = MAXCOL;
}
} else {
m = MAXCOL;
}
for (int i = 0; i < len; i++) {
int c = cmdline_charsize(ccline.cmdpos);
// count ">" for a double-wide char that doesn't fit.
correct_screencol(ccline.cmdpos, c, &ccline.cmdspos);
// Stop cursor at the end of the screen, but do increment the
// insert position, so that entering a very long command
// works, even though you can't see it.
if (ccline.cmdspos + c < m) {
ccline.cmdspos += c;
}
c = utfc_ptr2len(ccline.cmdbuff + ccline.cmdpos) - 1;
c = MIN(c, len - i - 1);
ccline.cmdpos += c;
i += c;
ccline.cmdpos++;
}
if (redraw) {
msg_check();
}
}
/// Save ccline, because obtaining the "=" register may execute "normal :cmd"
/// and overwrite it.
static void save_cmdline(CmdlineInfo *ccp)
{
*ccp = ccline;
CLEAR_FIELD(ccline);
ccline.prev_ccline = ccp;
ccline.cmdbuff = NULL; // signal that ccline is not in use
}
/// Restore ccline after it has been saved with save_cmdline().
static void restore_cmdline(CmdlineInfo *ccp)
FUNC_ATTR_NONNULL_ALL
{
ccline = *ccp;
}
/// Paste a yank register into the command line.
/// Used by CTRL-R command in command-line mode.
/// insert_reg() can't be used here, because special characters from the
/// register contents will be interpreted as commands.
///
/// @param regname Register name.
/// @param literally Insert text literally instead of "as typed".
/// @param remcr When true, remove trailing CR.
///
/// @returns FAIL for failure, OK otherwise
static bool cmdline_paste(int regname, bool literally, bool remcr)
{
char *arg;
bool allocated;
// check for valid regname; also accept special characters for CTRL-R in
// the command line
if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
&& regname != Ctrl_A && regname != Ctrl_L
&& !valid_yank_reg(regname, false)) {
return FAIL;
}
// A register containing CTRL-R can cause an endless loop. Allow using
// CTRL-C to break the loop.
line_breakcheck();
if (got_int) {
return FAIL;
}
// Need to set "textlock" to avoid nasty things like going to another
// buffer when evaluating an expression.
textlock++;
const bool i = get_spec_reg(regname, &arg, &allocated, true);
textlock--;
if (i) {
// Got the value of a special register in "arg".
if (arg == NULL) {
return FAIL;
}
// When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
// part of the word.
char *p = arg;
if (p_is && regname == Ctrl_W) {
char *w;
int len;
// Locate start of last word in the cmd buffer.
for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff;) {
len = utf_head_off(ccline.cmdbuff, w - 1) + 1;
if (!vim_iswordc(utf_ptr2char(w - len))) {
break;
}
w -= len;
}
len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
if (p_ic ? STRNICMP(w, arg, len) == 0 : strncmp(w, arg, (size_t)len) == 0) {
p += len;
}
}
cmdline_paste_str(p, literally);
if (allocated) {
xfree(arg);
}
return OK;
}
return cmdline_paste_reg(regname, literally, remcr);
}
// Put a string on the command line.
// When "literally" is true, insert literally.
// When "literally" is false, insert as typed, but don't leave the command
// line.
void cmdline_paste_str(const char *s, bool literally)
{
if (literally) {
put_on_cmdline(s, -1, true);
} else {
while (*s != NUL) {
int cv = (uint8_t)(*s);
if (cv == Ctrl_V && s[1]) {
s++;
}
int c = mb_cptr2char_adv(&s);
if (cv == Ctrl_V || c == ESC || c == Ctrl_C
|| c == CAR || c == NL || c == Ctrl_L
|| (c == Ctrl_BSL && *s == Ctrl_N)) {
stuffcharReadbuff(Ctrl_V);
}
stuffcharReadbuff(c);
}
}
}
// This function is called when the screen size changes and with incremental
// search and in other situations where the command line may have been
// overwritten.
void redrawcmdline(void)
{
if (cmd_silent) {
return;
}
need_wait_return = false;
compute_cmdrow();
redrawcmd();
cursorcmd();
ui_cursor_shape();
}
static void redrawcmdprompt(void)
{
if (cmd_silent) {
return;
}
if (ui_has(kUICmdline)) {
ccline.redraw_state = kCmdRedrawAll;
return;
}
if (ccline.cmdfirstc != NUL) {
msg_putchar(ccline.cmdfirstc);
}
if (ccline.cmdprompt != NULL) {
msg_puts_hl(ccline.cmdprompt, ccline.hl_id, false);
ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
// do the reverse of cmd_startcol()
if (ccline.cmdfirstc != NUL) {
ccline.cmdindent--;
}
} else {
for (int i = ccline.cmdindent; i > 0; i--) {
msg_putchar(' ');
}
}
}
// Redraw what is currently on the command line.
void redrawcmd(void)
{
if (cmd_silent) {
return;
}
if (ui_has(kUICmdline)) {
draw_cmdline(0, ccline.cmdlen);
return;
}
// when 'incsearch' is set there may be no command line while redrawing
if (ccline.cmdbuff == NULL) {
msg_cursor_goto(cmdline_row, 0);
msg_clr_eos();
return;
}
redrawing_cmdline = true;
sb_text_restart_cmdline();
msg_start();
redrawcmdprompt();
// Don't use more prompt, truncate the cmdline if it doesn't fit.
msg_no_more = true;
draw_cmdline(0, ccline.cmdlen);
msg_clr_eos();
msg_no_more = false;
ccline.cmdspos = cmd_screencol(ccline.cmdpos);
if (ccline.special_char != NUL) {
putcmdline(ccline.special_char, ccline.special_shift);
}
// An emsg() before may have set msg_scroll. This is used in normal mode,
// in cmdline mode we can reset them now.
msg_scroll = false; // next message overwrites cmdline
// Typing ':' at the more prompt may set skip_redraw. We don't want this
// in cmdline mode.
skip_redraw = false;
redrawing_cmdline = false;
}
void compute_cmdrow(void)
{
if (exmode_active || msg_scrolled != 0) {
cmdline_row = Rows - 1;
} else {
win_T *wp = lastwin_nofloating();
cmdline_row = wp->w_winrow + wp->w_height
+ wp->w_hsep_height + wp->w_status_height + global_stl_height();
}
if (cmdline_row == Rows && p_ch > 0) {
cmdline_row--;
}
lines_left = cmdline_row;
}
void cursorcmd(void)
{
if (cmd_silent || ui_has(kUICmdline)) {
return;
}
msg_row = cmdline_row + (ccline.cmdspos / Columns);
msg_col = ccline.cmdspos % Columns;
msg_row = MIN(msg_row, Rows - 1);
msg_cursor_goto(msg_row, msg_col);
}
void gotocmdline(bool clr)
{
if (ui_has(kUICmdline)) {
return;
}
msg_start();
msg_col = 0; // always start in column 0
if (clr) { // clear the bottom line(s)
msg_clr_eos(); // will reset clear_cmdline
}
msg_cursor_goto(cmdline_row, 0);
}
// Check the word in front of the cursor for an abbreviation.
// Called when the non-id character "c" has been entered.
// When an abbreviation is recognized it is removed from the text with
// backspaces and the replacement string is inserted, followed by "c".
static int ccheck_abbr(int c)
{
int spos = 0;
if (p_paste || no_abbr) { // no abbreviations or in paste mode
return false;
}
// Do not consider '<,'> be part of the mapping, skip leading whitespace.
// Actually accepts any mark.
while (spos < ccline.cmdlen && ascii_iswhite(ccline.cmdbuff[spos])) {
spos++;
}
if (ccline.cmdlen - spos > 5
&& ccline.cmdbuff[spos] == '\''
&& ccline.cmdbuff[spos + 2] == ','
&& ccline.cmdbuff[spos + 3] == '\'') {
spos += 5;
} else {
// check abbreviation from the beginning of the commandline
spos = 0;
}
return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
}
/// Escape special characters in "fname", depending on "what":
///
/// @param[in] fname File name to escape.
/// @param[in] what What to escape for:
/// - VSE_NONE: for when used as a file name argument after a Vim command.
/// - VSE_SHELL: for a shell command.
/// - VSE_BUFFER: for the ":buffer" command.
///
/// @return [allocated] escaped file name.
char *vim_strsave_fnameescape(const char *const fname, const int what)
FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{
#ifdef BACKSLASH_IN_FILENAME
# define PATH_ESC_CHARS " \t\n*?[{`%#'\"|!<"
# define BUFFER_ESC_CHARS (" \t\n*?[`%#'\"|!<")
char buf[sizeof(PATH_ESC_CHARS)];
int j = 0;
// Don't escape '[', '{' and '!' if they are in 'isfname' and for the
// ":buffer" command.
for (const char *p = what == VSE_BUFFER ? BUFFER_ESC_CHARS : PATH_ESC_CHARS;
*p != NUL; p++) {
if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec((uint8_t)(*p))) {
buf[j++] = *p;
}
}
buf[j] = NUL;
char *p = vim_strsave_escaped(fname, buf);
#else
# define PATH_ESC_CHARS " \t\n*?[{`$\\%#'\"|!<"
# define SHELL_ESC_CHARS " \t\n*?[{`$\\%#'\"|!<>();&"
# define BUFFER_ESC_CHARS " \t\n*?[`$\\%#'\"|!<"
char *p = vim_strsave_escaped(fname,
what == VSE_SHELL ? SHELL_ESC_CHARS : what ==
VSE_BUFFER ? BUFFER_ESC_CHARS : PATH_ESC_CHARS);
if (what == VSE_SHELL && csh_like_shell()) {
// For csh and similar shells need to put two backslashes before '!'.
// One is taken by Vim, one by the shell.
char *s = vim_strsave_escaped(p, "!");
xfree(p);
p = s;
}
#endif
// '>' and '+' are special at the start of some commands, e.g. ":edit" and
// ":write". "cd -" has a special meaning.
if (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)) {
escape_fname(&p);
}
return p;
}
/// Put a backslash before the file name in "pp", which is in allocated memory.
void escape_fname(char **pp)
{
char *p = xmalloc(strlen(*pp) + 2);
p[0] = '\\';
STRCPY(p + 1, *pp);
xfree(*pp);
*pp = p;
}
/// For each file name in files[num_files]:
/// If 'orig_pat' starts with "~/", replace the home directory with "~".
void tilde_replace(char *orig_pat, int num_files, char **files)
{
if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) {
for (int i = 0; i < num_files; i++) {
char *p = home_replace_save(NULL, files[i]);
xfree(files[i]);
files[i] = p;
}
}
}
/// Get a pointer to the current command line info.
CmdlineInfo *get_cmdline_info(void)
{
return &ccline;
}
unsigned get_cmdline_last_prompt_id(void)
{
return last_prompt_id;
}
/// Get pointer to the command line info to use. save_cmdline() may clear
/// ccline and put the previous value in ccline.prev_ccline.
static CmdlineInfo *get_ccline_ptr(void)
{
if ((State & MODE_CMDLINE) == 0) {
return NULL;
} else if (ccline.cmdbuff != NULL) {
return &ccline;
} else if (ccline.prev_ccline && ccline.prev_ccline->cmdbuff != NULL) {
return ccline.prev_ccline;
} else {
return NULL;
}
}
/// Get the current command-line type.
/// Returns ':' or '/' or '?' or '@' or '>' or '-'
/// Only works when the command line is being edited.
/// Returns NUL when something is wrong.
static int get_cmdline_type(void)
{
CmdlineInfo *p = get_ccline_ptr();
if (p == NULL) {
return NUL;
}
if (p->cmdfirstc == NUL) {
return (p->input_fn) ? '@' : '-';
}
return p->cmdfirstc;
}
/// Get the current command line in allocated memory.
/// Only works when the command line is being edited.
///
/// @return NULL when something is wrong.
static char *get_cmdline_str(void)
{
if (cmdline_star > 0) {
return NULL;
}
CmdlineInfo *p = get_ccline_ptr();
if (p == NULL) {
return NULL;
}
return xstrnsave(p->cmdbuff, (size_t)p->cmdlen);
}
/// Get the current command-line completion pattern.
static char *get_cmdline_completion_pattern(void)
{
if (cmdline_star > 0) {
return NULL;
}
CmdlineInfo *p = get_ccline_ptr();
if (p == NULL || p->xpc == NULL) {
return NULL;
}
int xp_context = p->xpc->xp_context;
if (xp_context == EXPAND_NOTHING) {
set_expand_context(p->xpc);
xp_context = p->xpc->xp_context;
p->xpc->xp_context = EXPAND_NOTHING;
}
if (xp_context == EXPAND_UNSUCCESSFUL) {
return NULL;
}
char *compl_pat = p->xpc->xp_pattern;
if (compl_pat == NULL) {
return NULL;
}
return xstrdup(compl_pat);
}
/// Get the current command-line completion type.
static char *get_cmdline_completion(void)
{
if (cmdline_star > 0) {
return NULL;
}
CmdlineInfo *p = get_ccline_ptr();
if (p == NULL || p->xpc == NULL) {
return NULL;
}
int xp_context = p->xpc->xp_context;
if (xp_context == EXPAND_NOTHING) {
set_expand_context(p->xpc);
xp_context = p->xpc->xp_context;
p->xpc->xp_context = EXPAND_NOTHING;
}
if (xp_context == EXPAND_UNSUCCESSFUL) {
return NULL;
}
char *cmd_compl = get_user_cmd_complete(NULL, xp_context);
if (cmd_compl == NULL) {
return NULL;
}
if (xp_context == EXPAND_USER_LIST || xp_context == EXPAND_USER_DEFINED) {
size_t buflen = strlen(cmd_compl) + strlen(p->xpc->xp_arg) + 2;
char *buffer = xmalloc(buflen);
snprintf(buffer, buflen, "%s,%s", cmd_compl, p->xpc->xp_arg);
return buffer;
}
return xstrdup(cmd_compl);
}
/// "getcmdcomplpat()" function
void f_getcmdcomplpat(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
rettv->v_type = VAR_STRING;
rettv->vval.v_string = get_cmdline_completion_pattern();
}
/// "getcmdcompltype()" function
void f_getcmdcompltype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
rettv->v_type = VAR_STRING;
rettv->vval.v_string = get_cmdline_completion();
}
/// "getcmdline()" function
void f_getcmdline(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
rettv->v_type = VAR_STRING;
rettv->vval.v_string = get_cmdline_str();
}
/// "getcmdpos()" function
void f_getcmdpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
CmdlineInfo *p = get_ccline_ptr();
rettv->vval.v_number = p != NULL ? p->cmdpos + 1 : 0;
}
/// "getcmdprompt()" function
void f_getcmdprompt(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
CmdlineInfo *p = get_ccline_ptr();
rettv->v_type = VAR_STRING;
rettv->vval.v_string = p != NULL && p->cmdprompt != NULL
? xstrdup(p->cmdprompt) : NULL;
}
/// "getcmdscreenpos()" function
void f_getcmdscreenpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
CmdlineInfo *p = get_ccline_ptr();
rettv->vval.v_number = p != NULL ? p->cmdspos + 1 : 0;
}
/// "getcmdtype()" function
void f_getcmdtype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
rettv->v_type = VAR_STRING;
rettv->vval.v_string = xmallocz(1);
rettv->vval.v_string[0] = (char)get_cmdline_type();
}
/// Set the command line str to "str".
/// @return 1 when failed, 0 when OK.
static int set_cmdline_str(const char *str, int pos)
{
CmdlineInfo *p = get_ccline_ptr();
if (p == NULL) {
return 1;
}
int len = (int)strlen(str);
realloc_cmdbuff(len + 1);
p->cmdlen = len;
STRCPY(p->cmdbuff, str);
p->cmdpos = pos < 0 || pos > p->cmdlen ? p->cmdlen : pos;
new_cmdpos = p->cmdpos;
redrawcmd();
// Trigger CmdlineChanged autocommands.
do_autocmd_cmdlinechanged(get_cmdline_type());
return 0;
}
/// Set the command line byte position to "pos". Zero is the first position.
/// Only works when the command line is being edited.
/// @return 1 when failed, 0 when OK.
static int set_cmdline_pos(int pos)
{
CmdlineInfo *p = get_ccline_ptr();
if (p == NULL) {
return 1;
}
// The position is not set directly but after CTRL-\ e or CTRL-R = has
// changed the command line.
new_cmdpos = MAX(0, pos);
return 0;
}
/// "setcmdline()" function
void f_setcmdline(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
if (tv_check_for_string_arg(argvars, 0) == FAIL
|| tv_check_for_opt_number_arg(argvars, 1) == FAIL) {
return;
}
int pos = -1;
if (argvars[1].v_type != VAR_UNKNOWN) {
bool error = false;
pos = (int)tv_get_number_chk(&argvars[1], &error) - 1;
if (error) {
return;
}
if (pos < 0) {
emsg(_(e_positive));
return;
}
}
// Use tv_get_string() to handle a NULL string like an empty string.
rettv->vval.v_number = set_cmdline_str(tv_get_string(&argvars[0]), pos);
}
/// "setcmdpos()" function
void f_setcmdpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
const int pos = (int)tv_get_number(&argvars[0]) - 1;
if (pos >= 0) {
rettv->vval.v_number = set_cmdline_pos(pos);
}
}
/// Return the first character of the current command line.
int get_cmdline_firstc(void)
{
return ccline.cmdfirstc;
}
/// Get indices that specify a range within a list (not a range of text lines
/// in a buffer!) from a string. Used for ":history" and ":clist".
///
/// @param str string to parse range from
/// @param num1 from
/// @param num2 to
///
/// @return OK if parsed successfully, otherwise FAIL.
int get_list_range(char **str, int *num1, int *num2)
{
int len;
bool first = false;
varnumber_T num;
*str = skipwhite((*str));
if (**str == '-' || ascii_isdigit(**str)) { // parse "from" part of range
vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, false, NULL);
*str += len;
// overflow
if (num > INT_MAX) {
return FAIL;
}
*num1 = (int)num;
first = true;
}
*str = skipwhite((*str));
if (**str == ',') { // parse "to" part of range
*str = skipwhite((*str) + 1);
vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, false, NULL);
if (len > 0) {
*str = skipwhite((*str) + len);
// overflow
if (num > INT_MAX) {
return FAIL;
}
*num2 = (int)num;
} else if (!first) { // no number given at all
return FAIL;
}
} else if (first) { // only one number given
*num2 = *num1;
}
return OK;
}
void cmdline_init(void)
{
CLEAR_FIELD(ccline);
}
/// Check value of 'cedit' and set cedit_key.
/// Returns NULL if value is OK, error message otherwise.
const char *did_set_cedit(optset_T *args)
{
if (*p_cedit == NUL) {
cedit_key = -1;
} else {
int n = string_to_key(p_cedit);
if (n == 0 || vim_isprintc(n)) {
return e_invarg;
}
cedit_key = n;
}
return NULL;
}
/// Open a window on the current command line and history. Allow editing in
/// the window. Returns when the window is closed.
/// Returns:
/// CR if the command is to be executed
/// Ctrl_C if it is to be abandoned
/// K_IGNORE if editing continues
static int open_cmdwin(void)
{
bufref_T old_curbuf;
bufref_T bufref;
win_T *old_curwin = curwin;
int i;
garray_T winsizes;
int save_restart_edit = restart_edit;
int save_State = State;
bool save_exmode = exmode_active;
bool save_cmdmsg_rl = cmdmsg_rl;
// Can't do this when text or buffer is locked.
// Can't do this recursively. Can't do it when typing a password.
if (text_or_buf_locked() || cmdwin_type != 0 || cmdline_star > 0) {
beep_flush();
return K_IGNORE;
}
set_bufref(&old_curbuf, curbuf);
// Save current window sizes.
win_size_save(&winsizes);
// When using completion in Insert mode with <C-R>=<C-F> one can open the
// command line window, but we don't want the popup menu then.
pum_undisplay(true);
// don't use a new tab page
cmdmod.cmod_tab = 0;
cmdmod.cmod_flags |= CMOD_NOSWAPFILE;
// Create a window for the command-line buffer.
if (win_split((int)p_cwh, WSP_BOT) == FAIL) {
beep_flush();
ga_clear(&winsizes);
return K_IGNORE;
}
// win_split() autocommands may have messed with the old window or buffer.
// Treat it as abandoning this command-line.
if (!win_valid(old_curwin) || curwin == old_curwin || !bufref_valid(&old_curbuf)
|| old_curwin->w_buffer != old_curbuf.br_buf) {
beep_flush();
ga_clear(&winsizes);
return Ctrl_C;
}
// Don't let quitting the More prompt make this fail.
got_int = false;
// Set "cmdwin_..." variables before any autocommands may mess things up.
cmdwin_type = get_cmdline_type();
cmdwin_level = ccline.level;
cmdwin_win = curwin;
cmdwin_old_curwin = old_curwin;
// Create empty command-line buffer. Be especially cautious of BufLeave
// autocommands from do_ecmd(), as cmdwin restrictions do not apply to them!
const int newbuf_status = buf_open_scratch(0, NULL);
const bool cmdwin_valid = win_valid(cmdwin_win);
if (newbuf_status == FAIL || !cmdwin_valid || curwin != cmdwin_win || !win_valid(old_curwin)
|| !bufref_valid(&old_curbuf) || old_curwin->w_buffer != old_curbuf.br_buf) {
if (newbuf_status == OK) {
set_bufref(&bufref, curbuf);
}
if (cmdwin_valid && !last_window(cmdwin_win)) {
win_close(cmdwin_win, true, false);
}
// win_close() autocommands may have already deleted the buffer.
if (newbuf_status == OK && bufref_valid(&bufref) && bufref.br_buf != curbuf) {
close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, false, false);
}
cmdwin_type = 0;
cmdwin_level = 0;
cmdwin_win = NULL;
cmdwin_old_curwin = NULL;
beep_flush();
ga_clear(&winsizes);
return Ctrl_C;
}
cmdwin_buf = curbuf;
// Command-line buffer has bufhidden=wipe, unlike a true "scratch" buffer.
set_option_value_give_err(kOptBufhidden, STATIC_CSTR_AS_OPTVAL("wipe"), OPT_LOCAL);
curbuf->b_p_ma = true;
curwin->w_p_fen = false;
curwin->w_p_rl = cmdmsg_rl;
cmdmsg_rl = false;
// Don't allow switching to another buffer.
curbuf->b_ro_locked++;
// Showing the prompt may have set need_wait_return, reset it.
need_wait_return = false;
const int histtype = hist_char2type(cmdwin_type);
if (histtype == HIST_CMD || histtype == HIST_DEBUG) {
if (p_wc == TAB) {
add_map("<Tab>", "<C-X><C-V>", MODE_INSERT, true);
add_map("<Tab>", "a<C-X><C-V>", MODE_NORMAL, true);
}
set_option_value_give_err(kOptFiletype, STATIC_CSTR_AS_OPTVAL("vim"), OPT_LOCAL);
}
curbuf->b_ro_locked--;
// Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
// sets 'textwidth' to 78).
curbuf->b_p_tw = 0;
// Fill the buffer with the history.
init_history();
if (get_hislen() > 0 && histtype != HIST_INVALID) {
i = *get_hisidx(histtype);
if (i >= 0) {
linenr_T lnum = 0;
do {
if (++i == get_hislen()) {
i = 0;
}
if (get_histentry(histtype)[i].hisstr != NULL) {
ml_append(lnum++, get_histentry(histtype)[i].hisstr, 0, false);
}
} while (i != *get_hisidx(histtype));
}
}
// Replace the empty last line with the current command-line and put the
// cursor there.
ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, true);
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
curwin->w_cursor.col = ccline.cmdpos;
changed_line_abv_curs();
invalidate_botline(curwin);
ui_ext_cmdline_hide(false);
redraw_later(curwin, UPD_SOME_VALID);
// No Ex mode here!
exmode_active = false;
State = MODE_NORMAL;
setmouse();
// Reset here so it can be set by a CmdWinEnter autocommand.
cmdwin_result = 0;
// Trigger CmdwinEnter autocommands.
trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
if (restart_edit != 0) { // autocmd with ":startinsert"
stuffcharReadbuff(K_NOP);
}
i = RedrawingDisabled;
RedrawingDisabled = 0;
int save_count = save_batch_count();
// Call the main loop until <CR> or CTRL-C is typed.
normal_enter(true, false);
RedrawingDisabled = i;
restore_batch_count(save_count);
const bool save_KeyTyped = KeyTyped;
// Trigger CmdwinLeave autocommands.
trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
// Restore KeyTyped in case it is modified by autocommands
KeyTyped = save_KeyTyped;
cmdwin_type = 0;
cmdwin_level = 0;
cmdwin_buf = NULL;
cmdwin_win = NULL;
cmdwin_old_curwin = NULL;
exmode_active = save_exmode;
// Safety check: The old window or buffer was changed or deleted: It's a bug
// when this happens!
if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)
|| old_curwin->w_buffer != old_curbuf.br_buf) {
cmdwin_result = Ctrl_C;
emsg(_(e_active_window_or_buffer_changed_or_deleted));
} else {
win_T *wp;
// autocmds may abort script processing
if (aborting() && cmdwin_result != K_IGNORE) {
cmdwin_result = Ctrl_C;
}
// Set the new command line from the cmdline buffer.
dealloc_cmdbuff();
if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) { // :qa[!] typed
const char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
size_t plen = (cmdwin_result == K_XF2) ? 2 : 3;
if (histtype == HIST_CMD) {
// Execute the command directly.
ccline.cmdbuff = xmemdupz(p, plen);
ccline.cmdlen = (int)plen;
ccline.cmdbufflen = (int)plen + 1;
cmdwin_result = CAR;
} else {
// First need to cancel what we were doing.
stuffcharReadbuff(':');
stuffReadbuff(p);
stuffcharReadbuff(CAR);
}
} else if (cmdwin_result == Ctrl_C) {
// :q or :close, don't execute any command
// and don't modify the cmd window.
ccline.cmdbuff = NULL;
} else {
ccline.cmdlen = get_cursor_line_len();
ccline.cmdbufflen = ccline.cmdlen + 1;
ccline.cmdbuff = xstrnsave(get_cursor_line_ptr(), (size_t)ccline.cmdlen);
}
if (ccline.cmdbuff == NULL) {
ccline.cmdbuff = xmemdupz("", 0);
ccline.cmdlen = 0;
ccline.cmdbufflen = 1;
ccline.cmdpos = 0;
cmdwin_result = Ctrl_C;
} else {
ccline.cmdpos = curwin->w_cursor.col;
// If the cursor is on the last character, it probably should be after it.
if (ccline.cmdpos == ccline.cmdlen - 1 || ccline.cmdpos > ccline.cmdlen) {
ccline.cmdpos = ccline.cmdlen;
}
if (cmdwin_result == K_IGNORE) {
ccline.cmdspos = cmd_screencol(ccline.cmdpos);
redrawcmd();
}
}
// Avoid command-line window first character being concealed.
curwin->w_p_cole = 0;
// First go back to the original window.
wp = curwin;
set_bufref(&bufref, curbuf);
skip_win_fix_cursor = true;
win_goto(old_curwin);
// win_goto() may trigger an autocommand that already closes the
// cmdline window.
if (win_valid(wp) && wp != curwin) {
win_close(wp, true, false);
}
// win_close() may have already wiped the buffer when 'bh' is
// set to 'wipe', autocommands may have closed other windows
if (bufref_valid(&bufref) && bufref.br_buf != curbuf) {
close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, false, false);
}
// Restore window sizes.
win_size_restore(&winsizes);
skip_win_fix_cursor = false;
}
ga_clear(&winsizes);
restart_edit = save_restart_edit;
cmdmsg_rl = save_cmdmsg_rl;
State = save_State;
may_trigger_modechanged();
setmouse();
setcursor();
return cmdwin_result;
}
/// @return true if in the cmdwin, not editing the command line.
bool is_in_cmdwin(void)
FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
{
return cmdwin_type != 0 && get_cmdline_type() == NUL;
}
/// Get script string
///
/// Used for commands which accept either `:command script` or
///
/// :command << endmarker
/// script
/// endmarker
///
/// @param eap Command being run.
/// @param[out] lenp Location where length of resulting string is saved. Will
/// be set to zero when skipping.
///
/// @return [allocated] NULL or script. Does not show any error messages.
/// NULL is returned when skipping and on error.
char *script_get(exarg_T *const eap, size_t *const lenp)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_MALLOC
{
char *cmd = eap->arg;
if (cmd[0] != '<' || cmd[1] != '<' || eap->ea_getline == NULL) {
*lenp = strlen(eap->arg);
return eap->skip ? NULL : xmemdupz(eap->arg, *lenp);
}
cmd += 2;
garray_T ga = { .ga_data = NULL, .ga_len = 0 };
list_T *const l = heredoc_get(eap, cmd, true);
if (l == NULL) {
return NULL;
}
if (!eap->skip) {
ga_init(&ga, 1, 0x400);
}
TV_LIST_ITER_CONST(l, li, {
if (!eap->skip) {
ga_concat(&ga, tv_get_string(TV_LIST_ITEM_TV(li)));
ga_append(&ga, '\n');
}
});
*lenp = (size_t)ga.ga_len; // Set length without trailing NUL.
if (!eap->skip) {
ga_append(&ga, NUL);
}
tv_list_free(l);
return (char *)ga.ga_data;
}
/// This function is used by f_input() and f_inputdialog() functions. The third
/// argument to f_input() specifies the type of completion to use at the
/// prompt. The third argument to f_inputdialog() specifies the value to return
/// when the user cancels the prompt.
void get_user_input(const typval_T *const argvars, typval_T *const rettv, const bool inputdialog,
const bool secret)
FUNC_ATTR_NONNULL_ALL
{
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
const char *prompt;
const char *defstr = "";
typval_T *cancelreturn = NULL;
typval_T cancelreturn_strarg2 = TV_INITIAL_VALUE;
const char *xp_name = NULL;
Callback input_callback = { .type = kCallbackNone };
char prompt_buf[NUMBUFLEN];
char defstr_buf[NUMBUFLEN];
char cancelreturn_buf[NUMBUFLEN];
char xp_name_buf[NUMBUFLEN];
char def[1] = { 0 };
if (argvars[0].v_type == VAR_DICT) {
if (argvars[1].v_type != VAR_UNKNOWN) {
emsg(_("E5050: {opts} must be the only argument"));
return;
}
dict_T *const dict = argvars[0].vval.v_dict;
prompt = tv_dict_get_string_buf_chk(dict, S_LEN("prompt"), prompt_buf, "");
if (prompt == NULL) {
return;
}
defstr = tv_dict_get_string_buf_chk(dict, S_LEN("default"), defstr_buf, "");
if (defstr == NULL) {
return;
}
dictitem_T *cancelreturn_di = tv_dict_find(dict, S_LEN("cancelreturn"));
if (cancelreturn_di != NULL) {
cancelreturn = &cancelreturn_di->di_tv;
}
xp_name = tv_dict_get_string_buf_chk(dict, S_LEN("completion"),
xp_name_buf, def);
if (xp_name == NULL) { // error
return;
}
if (xp_name == def) { // default to NULL
xp_name = NULL;
}
if (!tv_dict_get_callback(dict, S_LEN("highlight"), &input_callback)) {
return;
}
} else {
prompt = tv_get_string_buf_chk(&argvars[0], prompt_buf);
if (prompt == NULL) {
return;
}
if (argvars[1].v_type != VAR_UNKNOWN) {
defstr = tv_get_string_buf_chk(&argvars[1], defstr_buf);
if (defstr == NULL) {
return;
}
if (argvars[2].v_type != VAR_UNKNOWN) {
const char *const strarg2 = tv_get_string_buf_chk(&argvars[2], cancelreturn_buf);
if (strarg2 == NULL) {
return;
}
if (inputdialog) {
cancelreturn_strarg2.v_type = VAR_STRING;
cancelreturn_strarg2.vval.v_string = (char *)strarg2;
cancelreturn = &cancelreturn_strarg2;
} else {
xp_name = strarg2;
}
}
}
}
int xp_type = EXPAND_NOTHING;
char *xp_arg = NULL;
if (xp_name != NULL) {
// input() with a third argument: completion
const int xp_namelen = (int)strlen(xp_name);
uint32_t argt = 0;
if (parse_compl_arg(xp_name, xp_namelen, &xp_type,
&argt, &xp_arg) == FAIL) {
return;
}
}
// Only the part of the message after the last NL is considered as
// prompt for the command line, unlsess cmdline is externalized
const char *p = prompt;
if (!ui_has(kUICmdline)) {
const char *lastnl = strrchr(prompt, '\n');
if (lastnl != NULL) {
p = lastnl + 1;
msg_start();
msg_clr_eos();
msg_puts_len(prompt, p - prompt, get_echo_hl_id(), false);
msg_didout = false;
msg_starthere();
}
}
cmdline_row = msg_row;
stuffReadbuffSpec(defstr);
const int save_ex_normal_busy = ex_normal_busy;
ex_normal_busy = 0;
rettv->vval.v_string = getcmdline_prompt(secret ? NUL : '@', p, get_echo_hl_id(),
xp_type, xp_arg, input_callback, false, NULL);
ex_normal_busy = save_ex_normal_busy;
callback_free(&input_callback);
if (rettv->vval.v_string == NULL && cancelreturn != NULL) {
tv_copy(cancelreturn, rettv);
}
xfree(xp_arg);
// Since the user typed this, no need to wait for return.
need_wait_return = false;
msg_didout = false;
}
|