1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
|
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
* op_change, op_yank, do_put, do_join
*/
#include "vim.h"
/*
* Number of registers.
* 0 = unnamed register, for normal yanks and puts
* 1..9 = registers '1' to '9', for deletes
* 10..35 = registers 'a' to 'z'
* 36 = delete register '-'
* 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined
* 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined
*/
static yankreg_T y_regs[NUM_REGISTERS];
static yankreg_T *y_current; /* ptr to current yankreg */
static int y_append; /* TRUE when appending */
static yankreg_T *y_previous = NULL; /* ptr to last written yankreg */
/*
* structure used by block_prep, op_delete and op_yank for blockwise operators
* also op_change, op_shift, op_insert, op_replace - AKelly
*/
struct block_def
{
int startspaces; /* 'extra' cols before first char */
int endspaces; /* 'extra' cols after last char */
int textlen; /* chars in block */
char_u *textstart; /* pointer to 1st char (partially) in block */
colnr_T textcol; /* index of chars (partially) in block */
colnr_T start_vcol; /* start col of 1st char wholly inside block */
colnr_T end_vcol; /* start col of 1st char wholly after block */
int is_short; /* TRUE if line is too short to fit in block */
int is_MAX; /* TRUE if curswant==MAXCOL when starting */
int is_oneChar; /* TRUE if block within one character */
int pre_whitesp; /* screen cols of ws before block */
int pre_whitesp_c; /* chars of ws before block */
colnr_T end_char_vcols; /* number of vcols of post-block char */
colnr_T start_char_vcols; /* number of vcols of pre-block char */
};
static void shift_block(oparg_T *oap, int amount);
static int stuff_yank(int, char_u *);
static void put_reedit_in_typebuf(int silent);
static int put_in_typebuf(char_u *s, int esc, int colon,
int silent);
static void stuffescaped(char_u *arg, int literally);
static void mb_adjust_opend(oparg_T *oap);
static void free_yank_all(void);
static int yank_copy_line(struct block_def *bd, long y_idx);
#ifdef FEAT_CLIPBOARD
static void copy_yank_reg(yankreg_T *reg);
static void may_set_selection(void);
#endif
static void dis_msg(char_u *p, int skip_esc);
static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
static void str_to_reg(yankreg_T *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list);
#endif
static int ends_in_white(linenr_T lnum);
#ifdef FEAT_COMMENTS
static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
#else
static int fmt_check_par(linenr_T);
#endif
// Flags for third item in "opchars".
#define OPF_LINES 1 // operator always works on lines
#define OPF_CHANGE 2 // operator changes text
/*
* The names of operators.
* IMPORTANT: Index must correspond with defines in vim.h!!!
* The third field holds OPF_ flags.
*/
static char opchars[][3] =
{
{NUL, NUL, 0}, // OP_NOP
{'d', NUL, OPF_CHANGE}, // OP_DELETE
{'y', NUL, 0}, // OP_YANK
{'c', NUL, OPF_CHANGE}, // OP_CHANGE
{'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
{'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
{'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
{'g', '~', OPF_CHANGE}, // OP_TILDE
{'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
{'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
{':', NUL, OPF_LINES}, // OP_COLON
{'g', 'U', OPF_CHANGE}, // OP_UPPER
{'g', 'u', OPF_CHANGE}, // OP_LOWER
{'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
{'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
{'g', '?', OPF_CHANGE}, // OP_ROT13
{'r', NUL, OPF_CHANGE}, // OP_REPLACE
{'I', NUL, OPF_CHANGE}, // OP_INSERT
{'A', NUL, OPF_CHANGE}, // OP_APPEND
{'z', 'f', OPF_LINES}, // OP_FOLD
{'z', 'o', OPF_LINES}, // OP_FOLDOPEN
{'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
{'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
{'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
{'z', 'd', OPF_LINES}, // OP_FOLDDEL
{'z', 'D', OPF_LINES}, // OP_FOLDDELREC
{'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
{'g', '@', OPF_CHANGE}, // OP_FUNCTION
{Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
{Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
};
yankreg_T *
get_y_regs(void)
{
return y_regs;
}
yankreg_T *
get_y_current(void)
{
return y_current;
}
yankreg_T *
get_y_previous(void)
{
return y_previous;
}
void
set_y_previous(yankreg_T *yreg)
{
y_previous = yreg;
}
/*
* Translate a command name into an operator type.
* Must only be called with a valid operator name!
*/
int
get_op_type(int char1, int char2)
{
int i;
if (char1 == 'r') /* ignore second character */
return OP_REPLACE;
if (char1 == '~') /* when tilde is an operator */
return OP_TILDE;
if (char1 == 'g' && char2 == Ctrl_A) /* add */
return OP_NR_ADD;
if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
return OP_NR_SUB;
for (i = 0; ; ++i)
{
if (opchars[i][0] == char1 && opchars[i][1] == char2)
break;
if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
{
internal_error("get_op_type()");
break;
}
}
return i;
}
/*
* Return TRUE if operator "op" always works on whole lines.
*/
int
op_on_lines(int op)
{
return opchars[op][2] & OPF_LINES;
}
#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
/*
* Return TRUE if operator "op" changes text.
*/
int
op_is_change(int op)
{
return opchars[op][2] & OPF_CHANGE;
}
#endif
/*
* Get first operator command character.
* Returns 'g' or 'z' if there is another command character.
*/
int
get_op_char(int optype)
{
return opchars[optype][0];
}
/*
* Get second operator command character.
*/
int
get_extra_op_char(int optype)
{
return opchars[optype][1];
}
/*
* op_shift - handle a shift operation
*/
void
op_shift(oparg_T *oap, int curs_top, int amount)
{
long i;
int first_char;
int block_col = 0;
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return;
if (oap->block_mode)
block_col = curwin->w_cursor.col;
for (i = oap->line_count; --i >= 0; )
{
first_char = *ml_get_curline();
if (first_char == NUL) /* empty line */
curwin->w_cursor.col = 0;
else if (oap->block_mode)
shift_block(oap, amount);
else
/* Move the line right if it doesn't start with '#', 'smartindent'
* isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
if (first_char != '#' || !preprocs_left())
#endif
shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
++curwin->w_cursor.lnum;
}
changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
if (oap->block_mode)
{
curwin->w_cursor.lnum = oap->start.lnum;
curwin->w_cursor.col = block_col;
}
else if (curs_top) /* put cursor on first line, for ">>" */
{
curwin->w_cursor.lnum = oap->start.lnum;
beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
}
else
--curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
#ifdef FEAT_FOLDING
/* The cursor line is not in a closed fold */
foldOpenCursor();
#endif
if (oap->line_count > p_report)
{
char *op;
char *msg_line_single;
char *msg_line_plural;
if (oap->op_type == OP_RSHIFT)
op = ">";
else
op = "<";
msg_line_single = NGETTEXT("%ld line %sed %d time",
"%ld line %sed %d times", amount);
msg_line_plural = NGETTEXT("%ld lines %sed %d time",
"%ld lines %sed %d times", amount);
vim_snprintf((char *)IObuff, IOSIZE,
NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
oap->line_count, op, amount);
msg((char *)IObuff);
}
/*
* Set "'[" and "']" marks.
*/
curbuf->b_op_start = oap->start;
curbuf->b_op_end.lnum = oap->end.lnum;
curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
if (curbuf->b_op_end.col > 0)
--curbuf->b_op_end.col;
}
/*
* Shift the current line one shiftwidth left (if left != 0) or right
* leaves cursor on first blank in the line.
*/
void
shift_line(
int left,
int round,
int amount,
int call_changed_bytes) /* call changed_bytes() */
{
int count;
int i, j;
int p_sw = (int)get_sw_value_indent(curbuf);
count = get_indent(); /* get current indent */
if (round) /* round off indent */
{
i = count / p_sw; /* number of p_sw rounded down */
j = count % p_sw; /* extra spaces */
if (j && left) /* first remove extra spaces */
--amount;
if (left)
{
i -= amount;
if (i < 0)
i = 0;
}
else
i += amount;
count = i * p_sw;
}
else /* original vi indent */
{
if (left)
{
count -= p_sw * amount;
if (count < 0)
count = 0;
}
else
count += p_sw * amount;
}
/* Set new indent */
if (State & VREPLACE_FLAG)
change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
else
(void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
}
/*
* Shift one line of the current block one shiftwidth right or left.
* Leaves cursor on first character in block.
*/
static void
shift_block(oparg_T *oap, int amount)
{
int left = (oap->op_type == OP_LSHIFT);
int oldstate = State;
int total;
char_u *newp, *oldp;
int oldcol = curwin->w_cursor.col;
int p_sw = (int)get_sw_value_indent(curbuf);
#ifdef FEAT_VARTABS
int *p_vts = curbuf->b_p_vts_array;
#endif
int p_ts = (int)curbuf->b_p_ts;
struct block_def bd;
int incr;
colnr_T ws_vcol;
int i = 0, j = 0;
int len;
#ifdef FEAT_RIGHTLEFT
int old_p_ri = p_ri;
p_ri = 0; /* don't want revins in indent */
#endif
State = INSERT; /* don't want REPLACE for State */
block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
if (bd.is_short)
return;
/* total is number of screen columns to be inserted/removed */
total = (int)((unsigned)amount * (unsigned)p_sw);
if ((total / p_sw) != amount)
return; /* multiplication overflow */
oldp = ml_get_curline();
if (!left)
{
/*
* 1. Get start vcol
* 2. Total ws vcols
* 3. Divvy into TABs & spp
* 4. Construct new string
*/
total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
ws_vcol = bd.start_vcol - bd.pre_whitesp;
if (bd.startspaces)
{
if (has_mbyte)
{
if ((*mb_ptr2len)(bd.textstart) == 1)
++bd.textstart;
else
{
ws_vcol = 0;
bd.startspaces = 0;
}
}
else
++bd.textstart;
}
for ( ; VIM_ISWHITE(*bd.textstart); )
{
/* TODO: is passing bd.textstart for start of the line OK? */
incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
(colnr_T)(bd.start_vcol));
total += incr;
bd.start_vcol += incr;
}
/* OK, now total=all the VWS reqd, and textstart points at the 1st
* non-ws char in the block. */
#ifdef FEAT_VARTABS
if (!curbuf->b_p_et)
tabstop_fromto(ws_vcol, ws_vcol + total, p_ts, p_vts, &i, &j);
else
j = total;
#else
if (!curbuf->b_p_et)
i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
if (i)
j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
else
j = total;
#endif
/* if we're splitting a TAB, allow for it */
bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
len = (int)STRLEN(bd.textstart) + 1;
newp = alloc(bd.textcol + i + j + len);
if (newp == NULL)
return;
vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
mch_memmove(newp, oldp, (size_t)bd.textcol);
vim_memset(newp + bd.textcol, TAB, (size_t)i);
vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
/* the end */
mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
}
else /* left */
{
colnr_T destination_col; /* column to which text in block will
be shifted */
char_u *verbatim_copy_end; /* end of the part of the line which is
copied verbatim */
colnr_T verbatim_copy_width;/* the (displayed) width of this part
of line */
unsigned fill; /* nr of spaces that replace a TAB */
unsigned new_line_len; /* the length of the line after the
block shift */
size_t block_space_width;
size_t shift_amount;
char_u *non_white = bd.textstart;
colnr_T non_white_col;
/*
* Firstly, let's find the first non-whitespace character that is
* displayed after the block's start column and the character's column
* number. Also, let's calculate the width of all the whitespace
* characters that are displayed in the block and precede the searched
* non-whitespace character.
*/
/* If "bd.startspaces" is set, "bd.textstart" points to the character,
* the part of which is displayed at the block's beginning. Let's start
* searching from the next character. */
if (bd.startspaces)
MB_PTR_ADV(non_white);
/* The character's column is in "bd.start_vcol". */
non_white_col = bd.start_vcol;
while (VIM_ISWHITE(*non_white))
{
incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
non_white_col += incr;
}
block_space_width = non_white_col - oap->start_vcol;
/* We will shift by "total" or "block_space_width", whichever is less.
*/
shift_amount = (block_space_width < (size_t)total
? block_space_width : (size_t)total);
/* The column to which we will shift the text. */
destination_col = (colnr_T)(non_white_col - shift_amount);
/* Now let's find out how much of the beginning of the line we can
* reuse without modification. */
verbatim_copy_end = bd.textstart;
verbatim_copy_width = bd.start_vcol;
/* If "bd.startspaces" is set, "bd.textstart" points to the character
* preceding the block. We have to subtract its width to obtain its
* column number. */
if (bd.startspaces)
verbatim_copy_width -= bd.start_char_vcols;
while (verbatim_copy_width < destination_col)
{
char_u *line = verbatim_copy_end;
/* TODO: is passing verbatim_copy_end for start of the line OK? */
incr = lbr_chartabsize(line, verbatim_copy_end,
verbatim_copy_width);
if (verbatim_copy_width + incr > destination_col)
break;
verbatim_copy_width += incr;
MB_PTR_ADV(verbatim_copy_end);
}
/* If "destination_col" is different from the width of the initial
* part of the line that will be copied, it means we encountered a tab
* character, which we will have to partly replace with spaces. */
fill = destination_col - verbatim_copy_width;
/* The replacement line will consist of:
* - the beginning of the original line up to "verbatim_copy_end",
* - "fill" number of spaces,
* - the rest of the line, pointed to by non_white. */
new_line_len = (unsigned)(verbatim_copy_end - oldp)
+ fill
+ (unsigned)STRLEN(non_white) + 1;
newp = alloc(new_line_len);
if (newp == NULL)
return;
mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
}
/* replace the line */
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
State = oldstate;
curwin->w_cursor.col = oldcol;
#ifdef FEAT_RIGHTLEFT
p_ri = old_p_ri;
#endif
}
/*
* Insert string "s" (b_insert ? before : after) block :AKelly
* Caller must prepare for undo.
*/
static void
block_insert(
oparg_T *oap,
char_u *s,
int b_insert,
struct block_def *bdp)
{
int p_ts;
int count = 0; /* extra spaces to replace a cut TAB */
int spaces = 0; /* non-zero if cutting a TAB */
colnr_T offset; /* pointer along new line */
unsigned s_len; /* STRLEN(s) */
char_u *newp, *oldp; /* new, old lines */
linenr_T lnum; /* loop var */
int oldstate = State;
State = INSERT; /* don't want REPLACE for State */
s_len = (unsigned)STRLEN(s);
for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
{
block_prep(oap, bdp, lnum, TRUE);
if (bdp->is_short && b_insert)
continue; /* OP_INSERT, line ends before block start */
oldp = ml_get(lnum);
if (b_insert)
{
p_ts = bdp->start_char_vcols;
spaces = bdp->startspaces;
if (spaces != 0)
count = p_ts - 1; /* we're cutting a TAB */
offset = bdp->textcol;
}
else /* append */
{
p_ts = bdp->end_char_vcols;
if (!bdp->is_short) /* spaces = padding after block */
{
spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
if (spaces != 0)
count = p_ts - 1; /* we're cutting a TAB */
offset = bdp->textcol + bdp->textlen - (spaces != 0);
}
else /* spaces = padding to block edge */
{
/* if $ used, just append to EOL (ie spaces==0) */
if (!bdp->is_MAX)
spaces = (oap->end_vcol - bdp->end_vcol) + 1;
count = spaces;
offset = bdp->textcol + bdp->textlen;
}
}
if (has_mbyte && spaces > 0)
{
int off;
/* Avoid starting halfway a multi-byte character. */
if (b_insert)
{
off = (*mb_head_off)(oldp, oldp + offset + spaces);
}
else
{
off = (*mb_off_next)(oldp, oldp + offset);
offset += off;
}
spaces -= off;
count -= off;
}
newp = alloc(STRLEN(oldp) + s_len + count + 1);
if (newp == NULL)
continue;
/* copy up to shifted part */
mch_memmove(newp, oldp, (size_t)(offset));
oldp += offset;
/* insert pre-padding */
vim_memset(newp + offset, ' ', (size_t)spaces);
/* copy the new text */
mch_memmove(newp + offset + spaces, s, (size_t)s_len);
offset += s_len;
if (spaces && !bdp->is_short)
{
/* insert post-padding */
vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
/* We're splitting a TAB, don't copy it. */
oldp++;
/* We allowed for that TAB, remember this now */
count++;
}
if (spaces > 0)
offset += count;
STRMOVE(newp + offset, oldp);
ml_replace(lnum, newp, FALSE);
if (lnum == oap->end.lnum)
{
/* Set "']" mark to the end of the block instead of the end of
* the insert in the first line. */
curbuf->b_op_end.lnum = oap->end.lnum;
curbuf->b_op_end.col = offset;
}
} /* for all lnum */
changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
State = oldstate;
}
#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
/*
* op_reindent - handle reindenting a block of lines.
*/
void
op_reindent(oparg_T *oap, int (*how)(void))
{
long i;
char_u *l;
int amount;
linenr_T first_changed = 0;
linenr_T last_changed = 0;
linenr_T start_lnum = curwin->w_cursor.lnum;
/* Don't even try when 'modifiable' is off. */
if (!curbuf->b_p_ma)
{
emsg(_(e_modifiable));
return;
}
for (i = oap->line_count; --i >= 0 && !got_int; )
{
/* it's a slow thing to do, so give feedback so there's no worry that
* the computer's just hung. */
if (i > 1
&& (i % 50 == 0 || i == oap->line_count - 1)
&& oap->line_count > p_report)
smsg(_("%ld lines to indent... "), i);
/*
* Be vi-compatible: For lisp indenting the first line is not
* indented, unless there is only one line.
*/
#ifdef FEAT_LISP
if (i != oap->line_count - 1 || oap->line_count == 1
|| how != get_lisp_indent)
#endif
{
l = skipwhite(ml_get_curline());
if (*l == NUL) /* empty or blank line */
amount = 0;
else
amount = how(); /* get the indent for this line */
if (amount >= 0 && set_indent(amount, SIN_UNDO))
{
/* did change the indent, call changed_lines() later */
if (first_changed == 0)
first_changed = curwin->w_cursor.lnum;
last_changed = curwin->w_cursor.lnum;
}
}
++curwin->w_cursor.lnum;
curwin->w_cursor.col = 0; /* make sure it's valid */
}
/* put cursor on first non-blank of indented line */
curwin->w_cursor.lnum = start_lnum;
beginline(BL_SOL | BL_FIX);
/* Mark changed lines so that they will be redrawn. When Visual
* highlighting was present, need to continue until the last line. When
* there is no change still need to remove the Visual highlighting. */
if (last_changed != 0)
changed_lines(first_changed, 0,
oap->is_VIsual ? start_lnum + oap->line_count :
last_changed + 1, 0L);
else if (oap->is_VIsual)
redraw_curbuf_later(INVERTED);
if (oap->line_count > p_report)
{
i = oap->line_count - (i + 1);
smsg(NGETTEXT("%ld line indented ",
"%ld lines indented ", i), i);
}
/* set '[ and '] marks */
curbuf->b_op_start = oap->start;
curbuf->b_op_end = oap->end;
}
#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Keep the last expression line here, for repeating.
*/
static char_u *expr_line = NULL;
/*
* Get an expression for the "\"=expr1" or "CTRL-R =expr1"
* Returns '=' when OK, NUL otherwise.
*/
int
get_expr_register(void)
{
char_u *new_line;
new_line = getcmdline('=', 0L, 0, TRUE);
if (new_line == NULL)
return NUL;
if (*new_line == NUL) /* use previous line */
vim_free(new_line);
else
set_expr_line(new_line);
return '=';
}
/*
* Set the expression for the '=' register.
* Argument must be an allocated string.
*/
void
set_expr_line(char_u *new_line)
{
vim_free(expr_line);
expr_line = new_line;
}
/*
* Get the result of the '=' register expression.
* Returns a pointer to allocated memory, or NULL for failure.
*/
char_u *
get_expr_line(void)
{
char_u *expr_copy;
char_u *rv;
static int nested = 0;
if (expr_line == NULL)
return NULL;
/* Make a copy of the expression, because evaluating it may cause it to be
* changed. */
expr_copy = vim_strsave(expr_line);
if (expr_copy == NULL)
return NULL;
/* When we are invoked recursively limit the evaluation to 10 levels.
* Then return the string as-is. */
if (nested >= 10)
return expr_copy;
++nested;
rv = eval_to_string(expr_copy, NULL, TRUE);
--nested;
vim_free(expr_copy);
return rv;
}
/*
* Get the '=' register expression itself, without evaluating it.
*/
char_u *
get_expr_line_src(void)
{
if (expr_line == NULL)
return NULL;
return vim_strsave(expr_line);
}
#endif /* FEAT_EVAL */
/*
* Check if 'regname' is a valid name of a yank register.
* Note: There is no check for 0 (default register), caller should do this
*/
int
valid_yank_reg(
int regname,
int writing) /* if TRUE check for writable registers */
{
if ( (regname > 0 && ASCII_ISALNUM(regname))
|| (!writing && vim_strchr((char_u *)
#ifdef FEAT_EVAL
"/.%:="
#else
"/.%:"
#endif
, regname) != NULL)
|| regname == '#'
|| regname == '"'
|| regname == '-'
|| regname == '_'
#ifdef FEAT_CLIPBOARD
|| regname == '*'
|| regname == '+'
#endif
#ifdef FEAT_DND
|| (!writing && regname == '~')
#endif
)
return TRUE;
return FALSE;
}
/*
* Set y_current and y_append, according to the value of "regname".
* Cannot handle the '_' register.
* Must only be called with a valid register name!
*
* If regname is 0 and writing, use register 0
* If regname is 0 and reading, use previous register
*
* Return TRUE when the register should be inserted literally (selection or
* clipboard).
*/
int
get_yank_register(int regname, int writing)
{
int i;
int ret = FALSE;
y_append = FALSE;
if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
{
y_current = y_previous;
return ret;
}
i = regname;
if (VIM_ISDIGIT(i))
i -= '0';
else if (ASCII_ISLOWER(i))
i = CharOrdLow(i) + 10;
else if (ASCII_ISUPPER(i))
{
i = CharOrdUp(i) + 10;
y_append = TRUE;
}
else if (regname == '-')
i = DELETION_REGISTER;
#ifdef FEAT_CLIPBOARD
/* When selection is not available, use register 0 instead of '*' */
else if (clip_star.available && regname == '*')
{
i = STAR_REGISTER;
ret = TRUE;
}
/* When clipboard is not available, use register 0 instead of '+' */
else if (clip_plus.available && regname == '+')
{
i = PLUS_REGISTER;
ret = TRUE;
}
#endif
#ifdef FEAT_DND
else if (!writing && regname == '~')
i = TILDE_REGISTER;
#endif
else /* not 0-9, a-z, A-Z or '-': use register 0 */
i = 0;
y_current = &(y_regs[i]);
if (writing) /* remember the register we write into for do_put() */
y_previous = y_current;
return ret;
}
#if defined(FEAT_CLIPBOARD) || defined(PROTO)
/*
* When "regname" is a clipboard register, obtain the selection. If it's not
* available return zero, otherwise return "regname".
*/
int
may_get_selection(int regname)
{
if (regname == '*')
{
if (!clip_star.available)
regname = 0;
else
clip_get_selection(&clip_star);
}
else if (regname == '+')
{
if (!clip_plus.available)
regname = 0;
else
clip_get_selection(&clip_plus);
}
return regname;
}
#endif
/*
* Obtain the contents of a "normal" register. The register is made empty.
* The returned pointer has allocated memory, use put_register() later.
*/
void *
get_register(
int name,
int copy) /* make a copy, if FALSE make register empty. */
{
yankreg_T *reg;
int i;
#ifdef FEAT_CLIPBOARD
/* When Visual area changed, may have to update selection. Obtain the
* selection too. */
if (name == '*' && clip_star.available)
{
if (clip_isautosel_star())
clip_update_selection(&clip_star);
may_get_selection(name);
}
if (name == '+' && clip_plus.available)
{
if (clip_isautosel_plus())
clip_update_selection(&clip_plus);
may_get_selection(name);
}
#endif
get_yank_register(name, 0);
reg = ALLOC_ONE(yankreg_T);
if (reg != NULL)
{
*reg = *y_current;
if (copy)
{
/* If we run out of memory some or all of the lines are empty. */
if (reg->y_size == 0)
reg->y_array = NULL;
else
reg->y_array = ALLOC_MULT(char_u *, reg->y_size);
if (reg->y_array != NULL)
{
for (i = 0; i < reg->y_size; ++i)
reg->y_array[i] = vim_strsave(y_current->y_array[i]);
}
}
else
y_current->y_array = NULL;
}
return (void *)reg;
}
/*
* Put "reg" into register "name". Free any previous contents and "reg".
*/
void
put_register(int name, void *reg)
{
get_yank_register(name, 0);
free_yank_all();
*y_current = *(yankreg_T *)reg;
vim_free(reg);
#ifdef FEAT_CLIPBOARD
/* Send text written to clipboard register to the clipboard. */
may_set_selection();
#endif
}
#if (defined(FEAT_CLIPBOARD) && defined(FEAT_X11) && defined(USE_SYSTEM)) \
|| defined(PROTO)
void
free_register(void *reg)
{
yankreg_T tmp;
tmp = *y_current;
*y_current = *(yankreg_T *)reg;
free_yank_all();
vim_free(reg);
*y_current = tmp;
}
#endif
#if defined(FEAT_MOUSE) || defined(PROTO)
/*
* return TRUE if the current yank register has type MLINE
*/
int
yank_register_mline(int regname)
{
if (regname != 0 && !valid_yank_reg(regname, FALSE))
return FALSE;
if (regname == '_') /* black hole is always empty */
return FALSE;
get_yank_register(regname, FALSE);
return (y_current->y_type == MLINE);
}
#endif
/*
* Start or stop recording into a yank register.
*
* Return FAIL for failure, OK otherwise.
*/
int
do_record(int c)
{
char_u *p;
static int regname;
yankreg_T *old_y_previous, *old_y_current;
int retval;
if (reg_recording == 0) /* start recording */
{
/* registers 0-9, a-z and " are allowed */
if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
retval = FAIL;
else
{
reg_recording = c;
showmode();
regname = c;
retval = OK;
}
}
else /* stop recording */
{
/*
* Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
* needs to be removed again to put it in a register. exec_reg then
* adds the escaping back later.
*/
reg_recording = 0;
msg("");
p = get_recorded();
if (p == NULL)
retval = FAIL;
else
{
/* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
vim_unescape_csi(p);
/*
* We don't want to change the default register here, so save and
* restore the current register name.
*/
old_y_previous = y_previous;
old_y_current = y_current;
retval = stuff_yank(regname, p);
y_previous = old_y_previous;
y_current = old_y_current;
}
}
return retval;
}
/*
* Stuff string "p" into yank register "regname" as a single line (append if
* uppercase). "p" must have been alloced.
*
* return FAIL for failure, OK otherwise
*/
static int
stuff_yank(int regname, char_u *p)
{
char_u *lp;
char_u **pp;
/* check for read-only register */
if (regname != 0 && !valid_yank_reg(regname, TRUE))
{
vim_free(p);
return FAIL;
}
if (regname == '_') /* black hole: don't do anything */
{
vim_free(p);
return OK;
}
get_yank_register(regname, TRUE);
if (y_append && y_current->y_array != NULL)
{
pp = &(y_current->y_array[y_current->y_size - 1]);
lp = alloc(STRLEN(*pp) + STRLEN(p) + 1);
if (lp == NULL)
{
vim_free(p);
return FAIL;
}
STRCPY(lp, *pp);
STRCAT(lp, p);
vim_free(p);
vim_free(*pp);
*pp = lp;
}
else
{
free_yank_all();
if ((y_current->y_array = ALLOC_ONE(char_u *)) == NULL)
{
vim_free(p);
return FAIL;
}
y_current->y_array[0] = p;
y_current->y_size = 1;
y_current->y_type = MCHAR; /* used to be MLINE, why? */
#ifdef FEAT_VIMINFO
y_current->y_time_set = vim_time();
#endif
}
return OK;
}
static int execreg_lastc = NUL;
int
get_execreg_lastc(void)
{
return execreg_lastc;
}
void
set_execreg_lastc(int lastc)
{
execreg_lastc = lastc;
}
/*
* Execute a yank register: copy it into the stuff buffer.
*
* Return FAIL for failure, OK otherwise.
*/
int
do_execreg(
int regname,
int colon, /* insert ':' before each line */
int addcr, /* always add '\n' to end of line */
int silent) /* set "silent" flag in typeahead buffer */
{
long i;
char_u *p;
int retval = OK;
int remap;
// repeat previous one
if (regname == '@')
{
if (execreg_lastc == NUL)
{
emsg(_("E748: No previously used register"));
return FAIL;
}
regname = execreg_lastc;
}
// check for valid regname
if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
{
emsg_invreg(regname);
return FAIL;
}
execreg_lastc = regname;
#ifdef FEAT_CLIPBOARD
regname = may_get_selection(regname);
#endif
// black hole: don't stuff anything
if (regname == '_')
return OK;
// use last command line
if (regname == ':')
{
if (last_cmdline == NULL)
{
emsg(_(e_nolastcmd));
return FAIL;
}
// don't keep the cmdline containing @:
VIM_CLEAR(new_last_cmdline);
// Escape all control characters with a CTRL-V
p = vim_strsave_escaped_ext(last_cmdline,
(char_u *)"\001\002\003\004\005\006\007"
"\010\011\012\013\014\015\016\017"
"\020\021\022\023\024\025\026\027"
"\030\031\032\033\034\035\036\037",
Ctrl_V, FALSE);
if (p != NULL)
{
/* When in Visual mode "'<,'>" will be prepended to the command.
* Remove it when it's already there. */
if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
else
retval = put_in_typebuf(p, TRUE, TRUE, silent);
}
vim_free(p);
}
#ifdef FEAT_EVAL
else if (regname == '=')
{
p = get_expr_line();
if (p == NULL)
return FAIL;
retval = put_in_typebuf(p, TRUE, colon, silent);
vim_free(p);
}
#endif
else if (regname == '.') /* use last inserted text */
{
p = get_last_insert_save();
if (p == NULL)
{
emsg(_(e_noinstext));
return FAIL;
}
retval = put_in_typebuf(p, FALSE, colon, silent);
vim_free(p);
}
else
{
get_yank_register(regname, FALSE);
if (y_current->y_array == NULL)
return FAIL;
/* Disallow remaping for ":@r". */
remap = colon ? REMAP_NONE : REMAP_YES;
/*
* Insert lines into typeahead buffer, from last one to first one.
*/
put_reedit_in_typebuf(silent);
for (i = y_current->y_size; --i >= 0; )
{
char_u *escaped;
/* insert NL between lines and after last line if type is MLINE */
if (y_current->y_type == MLINE || i < y_current->y_size - 1
|| addcr)
{
if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
return FAIL;
}
escaped = vim_strsave_escape_csi(y_current->y_array[i]);
if (escaped == NULL)
return FAIL;
retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
vim_free(escaped);
if (retval == FAIL)
return FAIL;
if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
== FAIL)
return FAIL;
}
reg_executing = regname == 0 ? '"' : regname; // disable "q" command
}
return retval;
}
/*
* If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
* used only after other typeahead has been processed.
*/
static void
put_reedit_in_typebuf(int silent)
{
char_u buf[3];
if (restart_edit != NUL)
{
if (restart_edit == 'V')
{
buf[0] = 'g';
buf[1] = 'R';
buf[2] = NUL;
}
else
{
buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
buf[1] = NUL;
}
if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
restart_edit = NUL;
}
}
/*
* Insert register contents "s" into the typeahead buffer, so that it will be
* executed again.
* When "esc" is TRUE it is to be taken literally: Escape CSI characters and
* no remapping.
*/
static int
put_in_typebuf(
char_u *s,
int esc,
int colon, /* add ':' before the line */
int silent)
{
int retval = OK;
put_reedit_in_typebuf(silent);
if (colon)
retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
if (retval == OK)
{
char_u *p;
if (esc)
p = vim_strsave_escape_csi(s);
else
p = s;
if (p == NULL)
retval = FAIL;
else
retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
0, TRUE, silent);
if (esc)
vim_free(p);
}
if (colon && retval == OK)
retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
return retval;
}
/*
* Insert a yank register: copy it into the Read buffer.
* Used by CTRL-R command and middle mouse button in insert mode.
*
* return FAIL for failure, OK otherwise
*/
int
insert_reg(
int regname,
int literally_arg) /* insert literally, not as if typed */
{
long i;
int retval = OK;
char_u *arg;
int allocated;
int literally = literally_arg;
/*
* It is possible to get into an endless loop by having CTRL-R a in
* register a and then, in insert mode, doing CTRL-R a.
* If you hit CTRL-C, the loop will be broken here.
*/
ui_breakcheck();
if (got_int)
return FAIL;
/* check for valid regname */
if (regname != NUL && !valid_yank_reg(regname, FALSE))
return FAIL;
#ifdef FEAT_CLIPBOARD
regname = may_get_selection(regname);
#endif
if (regname == '.') /* insert last inserted text */
retval = stuff_inserted(NUL, 1L, TRUE);
else if (get_spec_reg(regname, &arg, &allocated, TRUE))
{
if (arg == NULL)
return FAIL;
stuffescaped(arg, literally);
if (allocated)
vim_free(arg);
}
else /* name or number register */
{
if (get_yank_register(regname, FALSE))
literally = TRUE;
if (y_current->y_array == NULL)
retval = FAIL;
else
{
for (i = 0; i < y_current->y_size; ++i)
{
stuffescaped(y_current->y_array[i], literally);
/*
* Insert a newline between lines and after last line if
* y_type is MLINE.
*/
if (y_current->y_type == MLINE || i < y_current->y_size - 1)
stuffcharReadbuff('\n');
}
}
}
return retval;
}
/*
* Stuff a string into the typeahead buffer, such that edit() will insert it
* literally ("literally" TRUE) or interpret is as typed characters.
*/
static void
stuffescaped(char_u *arg, int literally)
{
int c;
char_u *start;
while (*arg != NUL)
{
/* Stuff a sequence of normal ASCII characters, that's fast. Also
* stuff K_SPECIAL to get the effect of a special key when "literally"
* is TRUE. */
start = arg;
while ((*arg >= ' '
#ifndef EBCDIC
&& *arg < DEL /* EBCDIC: chars above space are normal */
#endif
)
|| (*arg == K_SPECIAL && !literally))
++arg;
if (arg > start)
stuffReadbuffLen(start, (long)(arg - start));
/* stuff a single special character */
if (*arg != NUL)
{
if (has_mbyte)
c = mb_cptr2char_adv(&arg);
else
c = *arg++;
if (literally && ((c < ' ' && c != TAB) || c == DEL))
stuffcharReadbuff(Ctrl_V);
stuffcharReadbuff(c);
}
}
}
/*
* If "regname" is a special register, return TRUE and store a pointer to its
* value in "argp".
*/
int
get_spec_reg(
int regname,
char_u **argp,
int *allocated, /* return: TRUE when value was allocated */
int errmsg) /* give error message when failing */
{
int cnt;
*argp = NULL;
*allocated = FALSE;
switch (regname)
{
case '%': /* file name */
if (errmsg)
check_fname(); /* will give emsg if not set */
*argp = curbuf->b_fname;
return TRUE;
case '#': /* alternate file name */
*argp = getaltfname(errmsg); /* may give emsg if not set */
return TRUE;
#ifdef FEAT_EVAL
case '=': /* result of expression */
*argp = get_expr_line();
*allocated = TRUE;
return TRUE;
#endif
case ':': /* last command line */
if (last_cmdline == NULL && errmsg)
emsg(_(e_nolastcmd));
*argp = last_cmdline;
return TRUE;
case '/': /* last search-pattern */
if (last_search_pat() == NULL && errmsg)
emsg(_(e_noprevre));
*argp = last_search_pat();
return TRUE;
case '.': /* last inserted text */
*argp = get_last_insert_save();
*allocated = TRUE;
if (*argp == NULL && errmsg)
emsg(_(e_noinstext));
return TRUE;
#ifdef FEAT_SEARCHPATH
case Ctrl_F: /* Filename under cursor */
case Ctrl_P: /* Path under cursor, expand via "path" */
if (!errmsg)
return FALSE;
*argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
| (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
*allocated = TRUE;
return TRUE;
#endif
case Ctrl_W: /* word under cursor */
case Ctrl_A: /* WORD (mnemonic All) under cursor */
if (!errmsg)
return FALSE;
cnt = find_ident_under_cursor(argp, regname == Ctrl_W
? (FIND_IDENT|FIND_STRING) : FIND_STRING);
*argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
*allocated = TRUE;
return TRUE;
case Ctrl_L: /* Line under cursor */
if (!errmsg)
return FALSE;
*argp = ml_get_buf(curwin->w_buffer,
curwin->w_cursor.lnum, FALSE);
return TRUE;
case '_': /* black hole: always empty */
*argp = (char_u *)"";
return TRUE;
}
return FALSE;
}
/*
* Paste a yank register into the command line.
* Only for non-special registers.
* 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.
*
* return FAIL for failure, OK otherwise
*/
int
cmdline_paste_reg(
int regname,
int literally_arg, /* Insert text literally instead of "as typed" */
int remcr) /* don't add CR characters */
{
long i;
int literally = literally_arg;
if (get_yank_register(regname, FALSE))
literally = TRUE;
if (y_current->y_array == NULL)
return FAIL;
for (i = 0; i < y_current->y_size; ++i)
{
cmdline_paste_str(y_current->y_array[i], literally);
/* Insert ^M between lines and after last line if type is MLINE.
* Don't do this when "remcr" is TRUE. */
if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
cmdline_paste_str((char_u *)"\r", literally);
/* Check for CTRL-C, in case someone tries to paste a few thousand
* lines and gets bored. */
ui_breakcheck();
if (got_int)
return FAIL;
}
return OK;
}
#if defined(FEAT_CLIPBOARD) || defined(PROTO)
/*
* Adjust the register name pointed to with "rp" for the clipboard being
* used always and the clipboard being available.
*/
void
adjust_clip_reg(int *rp)
{
/* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
* use '*' or '+' reg, respectively. "unnamedplus" prevails. */
if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
{
if (clip_unnamed != 0)
*rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
? '+' : '*';
else
*rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
? '+' : '*';
}
if (!clip_star.available && *rp == '*')
*rp = 0;
if (!clip_plus.available && *rp == '+')
*rp = 0;
}
#endif
/*
* Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
*/
void
shift_delete_registers()
{
int n;
y_current = &y_regs[9];
free_yank_all(); /* free register nine */
for (n = 9; n > 1; --n)
y_regs[n] = y_regs[n - 1];
y_current = &y_regs[1];
if (!y_append)
y_previous = y_current;
y_regs[1].y_array = NULL; /* set register one to empty */
}
#if defined(FEAT_EVAL)
static void
yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
{
static int recursive = FALSE;
dict_T *v_event;
list_T *list;
int n;
char_u buf[NUMBUFLEN + 2];
long reglen = 0;
if (recursive)
return;
v_event = get_vim_var_dict(VV_EVENT);
list = list_alloc();
if (list == NULL)
return;
for (n = 0; n < reg->y_size; n++)
list_append_string(list, reg->y_array[n], -1);
list->lv_lock = VAR_FIXED;
dict_add_list(v_event, "regcontents", list);
buf[0] = (char_u)oap->regname;
buf[1] = NUL;
dict_add_string(v_event, "regname", buf);
buf[0] = get_op_char(oap->op_type);
buf[1] = get_extra_op_char(oap->op_type);
buf[2] = NUL;
dict_add_string(v_event, "operator", buf);
buf[0] = NUL;
buf[1] = NUL;
switch (get_reg_type(oap->regname, ®len))
{
case MLINE: buf[0] = 'V'; break;
case MCHAR: buf[0] = 'v'; break;
case MBLOCK:
vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
reglen + 1);
break;
}
dict_add_string(v_event, "regtype", buf);
/* Lock the dictionary and its keys */
dict_set_items_ro(v_event);
recursive = TRUE;
textlock++;
apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
textlock--;
recursive = FALSE;
/* Empty the dictionary, v:event is still valid */
dict_free_contents(v_event);
hash_init(&v_event->dv_hashtab);
}
#endif
/*
* Handle a delete operation.
*
* Return FAIL if undo failed, OK otherwise.
*/
int
op_delete(oparg_T *oap)
{
int n;
linenr_T lnum;
char_u *ptr;
char_u *newp, *oldp;
struct block_def bd;
linenr_T old_lcount = curbuf->b_ml.ml_line_count;
int did_yank = FALSE;
if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
return OK;
/* Nothing to delete, return here. Do prepare undo, for op_change(). */
if (oap->empty)
return u_save_cursor();
if (!curbuf->b_p_ma)
{
emsg(_(e_modifiable));
return FAIL;
}
#ifdef FEAT_CLIPBOARD
adjust_clip_reg(&oap->regname);
#endif
if (has_mbyte)
mb_adjust_opend(oap);
/*
* Imitate the strange Vi behaviour: If the delete spans more than one
* line and motion_type == MCHAR and the result is a blank line, make the
* delete linewise. Don't do this for the change command or Visual mode.
*/
if ( oap->motion_type == MCHAR
&& !oap->is_VIsual
&& !oap->block_mode
&& oap->line_count > 1
&& oap->motion_force == NUL
&& oap->op_type == OP_DELETE)
{
ptr = ml_get(oap->end.lnum) + oap->end.col;
if (*ptr != NUL)
ptr += oap->inclusive;
ptr = skipwhite(ptr);
if (*ptr == NUL && inindent(0))
oap->motion_type = MLINE;
}
/*
* Check for trying to delete (e.g. "D") in an empty line.
* Note: For the change operator it is ok.
*/
if ( oap->motion_type == MCHAR
&& oap->line_count == 1
&& oap->op_type == OP_DELETE
&& *ml_get(oap->start.lnum) == NUL)
{
/*
* It's an error to operate on an empty region, when 'E' included in
* 'cpoptions' (Vi compatible).
*/
if (virtual_op)
/* Virtual editing: Nothing gets deleted, but we set the '[ and ']
* marks as if it happened. */
goto setmarks;
if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
beep_flush();
return OK;
}
/*
* Do a yank of whatever we're about to delete.
* If a yank register was specified, put the deleted text into that
* register. For the black hole register '_' don't yank anything.
*/
if (oap->regname != '_')
{
if (oap->regname != 0)
{
/* check for read-only register */
if (!valid_yank_reg(oap->regname, TRUE))
{
beep_flush();
return OK;
}
get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
did_yank = TRUE;
}
/*
* Put deleted text into register 1 and shift number registers if the
* delete contains a line break, or when using a specific operator (Vi
* compatible)
* Use the register name from before adjust_clip_reg() may have
* changed it.
*/
if (oap->motion_type == MLINE || oap->line_count > 1
|| oap->use_reg_one)
{
shift_delete_registers();
if (op_yank(oap, TRUE, FALSE) == OK)
did_yank = TRUE;
}
/* Yank into small delete register when no named register specified
* and the delete is within one line. */
if ((
#ifdef FEAT_CLIPBOARD
((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
#endif
oap->regname == 0) && oap->motion_type != MLINE
&& oap->line_count == 1)
{
oap->regname = '-';
get_yank_register(oap->regname, TRUE);
if (op_yank(oap, TRUE, FALSE) == OK)
did_yank = TRUE;
oap->regname = 0;
}
/*
* If there's too much stuff to fit in the yank register, then get a
* confirmation before doing the delete. This is crude, but simple.
* And it avoids doing a delete of something we can't put back if we
* want.
*/
if (!did_yank)
{
int msg_silent_save = msg_silent;
msg_silent = 0; /* must display the prompt */
n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
msg_silent = msg_silent_save;
if (n != 'y')
{
emsg(_(e_abort));
return FAIL;
}
}
#if defined(FEAT_EVAL)
if (did_yank && has_textyankpost())
yank_do_autocmd(oap, y_current);
#endif
}
/*
* block mode delete
*/
if (oap->block_mode)
{
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return FAIL;
for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
{
block_prep(oap, &bd, lnum, TRUE);
if (bd.textlen == 0) /* nothing to delete */
continue;
/* Adjust cursor position for tab replaced by spaces and 'lbr'. */
if (lnum == curwin->w_cursor.lnum)
{
curwin->w_cursor.col = bd.textcol + bd.startspaces;
curwin->w_cursor.coladd = 0;
}
// "n" == number of chars deleted
// If we delete a TAB, it may be replaced by several characters.
// Thus the number of characters may increase!
n = bd.textlen - bd.startspaces - bd.endspaces;
oldp = ml_get(lnum);
newp = alloc(STRLEN(oldp) + 1 - n);
if (newp == NULL)
continue;
/* copy up to deleted part */
mch_memmove(newp, oldp, (size_t)bd.textcol);
/* insert spaces */
vim_memset(newp + bd.textcol, ' ',
(size_t)(bd.startspaces + bd.endspaces));
/* copy the part after the deleted part */
oldp += bd.textcol + bd.textlen;
STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
/* replace the line */
ml_replace(lnum, newp, FALSE);
#ifdef FEAT_TEXT_PROP
if (curbuf->b_has_textprop && n != 0)
adjust_prop_columns(lnum, bd.textcol, -n, 0);
#endif
}
check_cursor_col();
changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
oap->end.lnum + 1, 0L);
oap->line_count = 0; /* no lines deleted */
}
else if (oap->motion_type == MLINE)
{
if (oap->op_type == OP_CHANGE)
{
/* Delete the lines except the first one. Temporarily move the
* cursor to the next line. Save the current line number, if the
* last line is deleted it may be changed.
*/
if (oap->line_count > 1)
{
lnum = curwin->w_cursor.lnum;
++curwin->w_cursor.lnum;
del_lines((long)(oap->line_count - 1), TRUE);
curwin->w_cursor.lnum = lnum;
}
if (u_save_cursor() == FAIL)
return FAIL;
if (curbuf->b_p_ai) /* don't delete indent */
{
beginline(BL_WHITE); /* cursor on first non-white */
did_ai = TRUE; /* delete the indent when ESC hit */
ai_col = curwin->w_cursor.col;
}
else
beginline(0); /* cursor in column 0 */
truncate_line(FALSE); /* delete the rest of the line */
/* leave cursor past last char in line */
if (oap->line_count > 1)
u_clearline(); /* "U" command not possible after "2cc" */
}
else
{
del_lines(oap->line_count, TRUE);
beginline(BL_WHITE | BL_FIX);
u_clearline(); /* "U" command not possible after "dd" */
}
}
else
{
if (virtual_op)
{
int endcol = 0;
/* For virtualedit: break the tabs that are partly included. */
if (gchar_pos(&oap->start) == '\t')
{
if (u_save_cursor() == FAIL) /* save first line for undo */
return FAIL;
if (oap->line_count == 1)
endcol = getviscol2(oap->end.col, oap->end.coladd);
coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
oap->start = curwin->w_cursor;
if (oap->line_count == 1)
{
coladvance(endcol);
oap->end.col = curwin->w_cursor.col;
oap->end.coladd = curwin->w_cursor.coladd;
curwin->w_cursor = oap->start;
}
}
/* Break a tab only when it's included in the area. */
if (gchar_pos(&oap->end) == '\t'
&& (int)oap->end.coladd < oap->inclusive)
{
/* save last line for undo */
if (u_save((linenr_T)(oap->end.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return FAIL;
curwin->w_cursor = oap->end;
coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
oap->end = curwin->w_cursor;
curwin->w_cursor = oap->start;
}
}
if (oap->line_count == 1) /* delete characters within one line */
{
if (u_save_cursor() == FAIL) /* save line for undo */
return FAIL;
/* if 'cpoptions' contains '$', display '$' at end of change */
if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
&& oap->op_type == OP_CHANGE
&& oap->end.lnum == curwin->w_cursor.lnum
&& !oap->is_VIsual)
display_dollar(oap->end.col - !oap->inclusive);
n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
if (virtual_op)
{
/* fix up things for virtualedit-delete:
* break the tabs which are going to get in our way
*/
char_u *curline = ml_get_curline();
int len = (int)STRLEN(curline);
if (oap->end.coladd != 0
&& (int)oap->end.col >= len - 1
&& !(oap->start.coladd && (int)oap->end.col >= len - 1))
n++;
/* Delete at least one char (e.g, when on a control char). */
if (n == 0 && oap->start.coladd != oap->end.coladd)
n = 1;
/* When deleted a char in the line, reset coladd. */
if (gchar_cursor() != NUL)
curwin->w_cursor.coladd = 0;
}
(void)del_bytes((long)n, !virtual_op,
oap->op_type == OP_DELETE && !oap->is_VIsual);
}
else /* delete characters between lines */
{
pos_T curpos;
/* save deleted and changed lines for undo */
if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
(linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
return FAIL;
truncate_line(TRUE); /* delete from cursor to end of line */
curpos = curwin->w_cursor; /* remember curwin->w_cursor */
++curwin->w_cursor.lnum;
del_lines((long)(oap->line_count - 2), FALSE);
/* delete from start of line until op_end */
n = (oap->end.col + 1 - !oap->inclusive);
curwin->w_cursor.col = 0;
(void)del_bytes((long)n, !virtual_op,
oap->op_type == OP_DELETE && !oap->is_VIsual);
curwin->w_cursor = curpos; /* restore curwin->w_cursor */
(void)do_join(2, FALSE, FALSE, FALSE, FALSE);
}
}
msgmore(curbuf->b_ml.ml_line_count - old_lcount);
setmarks:
if (oap->block_mode)
{
curbuf->b_op_end.lnum = oap->end.lnum;
curbuf->b_op_end.col = oap->start.col;
}
else
curbuf->b_op_end = oap->start;
curbuf->b_op_start = oap->start;
return OK;
}
/*
* Adjust end of operating area for ending on a multi-byte character.
* Used for deletion.
*/
static void
mb_adjust_opend(oparg_T *oap)
{
char_u *p;
if (oap->inclusive)
{
p = ml_get(oap->end.lnum);
oap->end.col += mb_tail_off(p, p + oap->end.col);
}
}
/*
* Replace the character under the cursor with "c".
* This takes care of multi-byte characters.
*/
static void
replace_character(int c)
{
int n = State;
State = REPLACE;
ins_char(c);
State = n;
/* Backup to the replaced character. */
dec_cursor();
}
/*
* Replace a whole area with one character.
*/
int
op_replace(oparg_T *oap, int c)
{
int n, numc;
int num_chars;
char_u *newp, *oldp;
size_t oldlen;
struct block_def bd;
char_u *after_p = NULL;
int had_ctrl_v_cr = FALSE;
if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
return OK; /* nothing to do */
if (c == REPLACE_CR_NCHAR)
{
had_ctrl_v_cr = TRUE;
c = CAR;
}
else if (c == REPLACE_NL_NCHAR)
{
had_ctrl_v_cr = TRUE;
c = NL;
}
if (has_mbyte)
mb_adjust_opend(oap);
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return FAIL;
/*
* block mode replace
*/
if (oap->block_mode)
{
bd.is_MAX = (curwin->w_curswant == MAXCOL);
for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
{
curwin->w_cursor.col = 0; /* make sure cursor position is valid */
block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
continue; /* nothing to replace */
/* n == number of extra chars required
* If we split a TAB, it may be replaced by several characters.
* Thus the number of characters may increase!
*/
/* If the range starts in virtual space, count the initial
* coladd offset as part of "startspaces" */
if (virtual_op && bd.is_short && *bd.textstart == NUL)
{
pos_T vpos;
vpos.lnum = curwin->w_cursor.lnum;
getvpos(&vpos, oap->start_vcol);
bd.startspaces += vpos.coladd;
n = bd.startspaces;
}
else
/* allow for pre spaces */
n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
/* allow for post spp */
n += (bd.endspaces
&& !bd.is_oneChar
&& bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
/* Figure out how many characters to replace. */
numc = oap->end_vcol - oap->start_vcol + 1;
if (bd.is_short && (!virtual_op || bd.is_MAX))
numc -= (oap->end_vcol - bd.end_vcol) + 1;
/* A double-wide character can be replaced only up to half the
* times. */
if ((*mb_char2cells)(c) > 1)
{
if ((numc & 1) && !bd.is_short)
{
++bd.endspaces;
++n;
}
numc = numc / 2;
}
/* Compute bytes needed, move character count to num_chars. */
num_chars = numc;
numc *= (*mb_char2len)(c);
/* oldlen includes textlen, so don't double count */
n += numc - bd.textlen;
oldp = ml_get_curline();
oldlen = STRLEN(oldp);
newp = alloc(oldlen + 1 + n);
if (newp == NULL)
continue;
vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
/* copy up to deleted part */
mch_memmove(newp, oldp, (size_t)bd.textcol);
oldp += bd.textcol + bd.textlen;
/* insert pre-spaces */
vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
/* insert replacement chars CHECK FOR ALLOCATED SPACE */
/* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
* literally. */
if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
{
if (has_mbyte)
{
n = (int)STRLEN(newp);
while (--num_chars >= 0)
n += (*mb_char2bytes)(c, newp + n);
}
else
vim_memset(newp + STRLEN(newp), c, (size_t)numc);
if (!bd.is_short)
{
/* insert post-spaces */
vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
/* copy the part after the changed part */
STRMOVE(newp + STRLEN(newp), oldp);
}
}
else
{
/* Replacing with \r or \n means splitting the line. */
after_p = alloc(oldlen + 1 + n - STRLEN(newp));
if (after_p != NULL)
STRMOVE(after_p, oldp);
}
/* replace the line */
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
if (after_p != NULL)
{
ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
appended_lines_mark(curwin->w_cursor.lnum, 1L);
oap->end.lnum++;
vim_free(after_p);
}
}
}
else
{
/*
* MCHAR and MLINE motion replace.
*/
if (oap->motion_type == MLINE)
{
oap->start.col = 0;
curwin->w_cursor.col = 0;
oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
if (oap->end.col)
--oap->end.col;
}
else if (!oap->inclusive)
dec(&(oap->end));
while (LTOREQ_POS(curwin->w_cursor, oap->end))
{
n = gchar_cursor();
if (n != NUL)
{
if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
{
/* This is slow, but it handles replacing a single-byte
* with a multi-byte and the other way around. */
if (curwin->w_cursor.lnum == oap->end.lnum)
oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
replace_character(c);
}
else
{
if (n == TAB)
{
int end_vcol = 0;
if (curwin->w_cursor.lnum == oap->end.lnum)
{
/* oap->end has to be recalculated when
* the tab breaks */
end_vcol = getviscol2(oap->end.col,
oap->end.coladd);
}
coladvance_force(getviscol());
if (curwin->w_cursor.lnum == oap->end.lnum)
getvpos(&oap->end, end_vcol);
}
PBYTE(curwin->w_cursor, c);
}
}
else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
{
int virtcols = oap->end.coladd;
if (curwin->w_cursor.lnum == oap->start.lnum
&& oap->start.col == oap->end.col && oap->start.coladd)
virtcols -= oap->start.coladd;
/* oap->end has been trimmed so it's effectively inclusive;
* as a result an extra +1 must be counted so we don't
* trample the NUL byte. */
coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
curwin->w_cursor.col -= (virtcols + 1);
for (; virtcols >= 0; virtcols--)
{
if ((*mb_char2len)(c) > 1)
replace_character(c);
else
PBYTE(curwin->w_cursor, c);
if (inc(&curwin->w_cursor) == -1)
break;
}
}
/* Advance to next character, stop at the end of the file. */
if (inc_cursor() == -1)
break;
}
}
curwin->w_cursor = oap->start;
check_cursor();
changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
/* Set "'[" and "']" marks. */
curbuf->b_op_start = oap->start;
curbuf->b_op_end = oap->end;
return OK;
}
static int swapchars(int op_type, pos_T *pos, int length);
/*
* Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
*/
void
op_tilde(oparg_T *oap)
{
pos_T pos;
struct block_def bd;
int did_change = FALSE;
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return;
pos = oap->start;
if (oap->block_mode) /* Visual block mode */
{
for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
{
int one_change;
block_prep(oap, &bd, pos.lnum, FALSE);
pos.col = bd.textcol;
one_change = swapchars(oap->op_type, &pos, bd.textlen);
did_change |= one_change;
#ifdef FEAT_NETBEANS_INTG
if (netbeans_active() && one_change)
{
char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
netbeans_removed(curbuf, pos.lnum, bd.textcol,
(long)bd.textlen);
netbeans_inserted(curbuf, pos.lnum, bd.textcol,
&ptr[bd.textcol], bd.textlen);
}
#endif
}
if (did_change)
changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
}
else /* not block mode */
{
if (oap->motion_type == MLINE)
{
oap->start.col = 0;
pos.col = 0;
oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
if (oap->end.col)
--oap->end.col;
}
else if (!oap->inclusive)
dec(&(oap->end));
if (pos.lnum == oap->end.lnum)
did_change = swapchars(oap->op_type, &pos,
oap->end.col - pos.col + 1);
else
for (;;)
{
did_change |= swapchars(oap->op_type, &pos,
pos.lnum == oap->end.lnum ? oap->end.col + 1:
(int)STRLEN(ml_get_pos(&pos)));
if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
break;
}
if (did_change)
{
changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
0L);
#ifdef FEAT_NETBEANS_INTG
if (netbeans_active() && did_change)
{
char_u *ptr;
int count;
pos = oap->start;
while (pos.lnum < oap->end.lnum)
{
ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
count = (int)STRLEN(ptr) - pos.col;
netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
netbeans_inserted(curbuf, pos.lnum, pos.col,
&ptr[pos.col], count);
pos.col = 0;
pos.lnum++;
}
ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
count = oap->end.col - pos.col + 1;
netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
netbeans_inserted(curbuf, pos.lnum, pos.col,
&ptr[pos.col], count);
}
#endif
}
}
if (!did_change && oap->is_VIsual)
/* No change: need to remove the Visual selection */
redraw_curbuf_later(INVERTED);
/*
* Set '[ and '] marks.
*/
curbuf->b_op_start = oap->start;
curbuf->b_op_end = oap->end;
if (oap->line_count > p_report)
smsg(NGETTEXT("%ld line changed", "%ld lines changed",
oap->line_count), oap->line_count);
}
/*
* Invoke swapchar() on "length" bytes at position "pos".
* "pos" is advanced to just after the changed characters.
* "length" is rounded up to include the whole last multi-byte character.
* Also works correctly when the number of bytes changes.
* Returns TRUE if some character was changed.
*/
static int
swapchars(int op_type, pos_T *pos, int length)
{
int todo;
int did_change = 0;
for (todo = length; todo > 0; --todo)
{
if (has_mbyte)
{
int len = (*mb_ptr2len)(ml_get_pos(pos));
/* we're counting bytes, not characters */
if (len > 0)
todo -= len - 1;
}
did_change |= swapchar(op_type, pos);
if (inc(pos) == -1) /* at end of file */
break;
}
return did_change;
}
/*
* If op_type == OP_UPPER: make uppercase,
* if op_type == OP_LOWER: make lowercase,
* if op_type == OP_ROT13: do rot13 encoding,
* else swap case of character at 'pos'
* returns TRUE when something actually changed.
*/
int
swapchar(int op_type, pos_T *pos)
{
int c;
int nc;
c = gchar_pos(pos);
/* Only do rot13 encoding for ASCII characters. */
if (c >= 0x80 && op_type == OP_ROT13)
return FALSE;
if (op_type == OP_UPPER && c == 0xdf
&& (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
{
pos_T sp = curwin->w_cursor;
/* Special handling of German sharp s: change to "SS". */
curwin->w_cursor = *pos;
del_char(FALSE);
ins_char('S');
ins_char('S');
curwin->w_cursor = sp;
inc(pos);
}
if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
return FALSE;
nc = c;
if (MB_ISLOWER(c))
{
if (op_type == OP_ROT13)
nc = ROT13(c, 'a');
else if (op_type != OP_LOWER)
nc = MB_TOUPPER(c);
}
else if (MB_ISUPPER(c))
{
if (op_type == OP_ROT13)
nc = ROT13(c, 'A');
else if (op_type != OP_UPPER)
nc = MB_TOLOWER(c);
}
if (nc != c)
{
if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
{
pos_T sp = curwin->w_cursor;
curwin->w_cursor = *pos;
/* don't use del_char(), it also removes composing chars */
del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
ins_char(nc);
curwin->w_cursor = sp;
}
else
PBYTE(*pos, nc);
return TRUE;
}
return FALSE;
}
/*
* op_insert - Insert and append operators for Visual mode.
*/
void
op_insert(oparg_T *oap, long count1)
{
long ins_len, pre_textlen = 0;
char_u *firstline, *ins_text;
colnr_T ind_pre = 0, ind_post;
struct block_def bd;
int i;
pos_T t1;
/* edit() changes this - record it for OP_APPEND */
bd.is_MAX = (curwin->w_curswant == MAXCOL);
/* vis block is still marked. Get rid of it now. */
curwin->w_cursor.lnum = oap->start.lnum;
update_screen(INVERTED);
if (oap->block_mode)
{
/* When 'virtualedit' is used, need to insert the extra spaces before
* doing block_prep(). When only "block" is used, virtual edit is
* already disabled, but still need it when calling
* coladvance_force(). */
if (curwin->w_cursor.coladd > 0)
{
int old_ve_flags = ve_flags;
ve_flags = VE_ALL;
if (u_save_cursor() == FAIL)
return;
coladvance_force(oap->op_type == OP_APPEND
? oap->end_vcol + 1 : getviscol());
if (oap->op_type == OP_APPEND)
--curwin->w_cursor.col;
ve_flags = old_ve_flags;
}
/* Get the info about the block before entering the text */
block_prep(oap, &bd, oap->start.lnum, TRUE);
/* Get indent information */
ind_pre = (colnr_T)getwhitecols_curline();
firstline = ml_get(oap->start.lnum) + bd.textcol;
if (oap->op_type == OP_APPEND)
firstline += bd.textlen;
pre_textlen = (long)STRLEN(firstline);
}
if (oap->op_type == OP_APPEND)
{
if (oap->block_mode && curwin->w_cursor.coladd == 0)
{
/* Move the cursor to the character right of the block. */
curwin->w_set_curswant = TRUE;
while (*ml_get_cursor() != NUL
&& (curwin->w_cursor.col < bd.textcol + bd.textlen))
++curwin->w_cursor.col;
if (bd.is_short && !bd.is_MAX)
{
/* First line was too short, make it longer and adjust the
* values in "bd". */
if (u_save_cursor() == FAIL)
return;
for (i = 0; i < bd.endspaces; ++i)
ins_char(' ');
bd.textlen += bd.endspaces;
}
}
else
{
curwin->w_cursor = oap->end;
check_cursor_col();
/* Works just like an 'i'nsert on the next character. */
if (!LINEEMPTY(curwin->w_cursor.lnum)
&& oap->start_vcol != oap->end_vcol)
inc_cursor();
}
}
t1 = oap->start;
(void)edit(NUL, FALSE, (linenr_T)count1);
/* When a tab was inserted, and the characters in front of the tab
* have been converted to a tab as well, the column of the cursor
* might have actually been reduced, so need to adjust here. */
if (t1.lnum == curbuf->b_op_start_orig.lnum
&& LT_POS(curbuf->b_op_start_orig, t1))
oap->start = curbuf->b_op_start_orig;
/* If user has moved off this line, we don't know what to do, so do
* nothing.
* Also don't repeat the insert when Insert mode ended with CTRL-C. */
if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
return;
if (oap->block_mode)
{
struct block_def bd2;
int did_indent = FALSE;
size_t len;
int add;
/* If indent kicked in, the firstline might have changed
* but only do that, if the indent actually increased. */
ind_post = (colnr_T)getwhitecols_curline();
if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
{
bd.textcol += ind_post - ind_pre;
bd.start_vcol += ind_post - ind_pre;
did_indent = TRUE;
}
/* The user may have moved the cursor before inserting something, try
* to adjust the block for that. But only do it, if the difference
* does not come from indent kicking in. */
if (oap->start.lnum == curbuf->b_op_start_orig.lnum
&& !bd.is_MAX && !did_indent)
{
if (oap->op_type == OP_INSERT
&& oap->start.col + oap->start.coladd
!= curbuf->b_op_start_orig.col
+ curbuf->b_op_start_orig.coladd)
{
int t = getviscol2(curbuf->b_op_start_orig.col,
curbuf->b_op_start_orig.coladd);
oap->start.col = curbuf->b_op_start_orig.col;
pre_textlen -= t - oap->start_vcol;
oap->start_vcol = t;
}
else if (oap->op_type == OP_APPEND
&& oap->end.col + oap->end.coladd
>= curbuf->b_op_start_orig.col
+ curbuf->b_op_start_orig.coladd)
{
int t = getviscol2(curbuf->b_op_start_orig.col,
curbuf->b_op_start_orig.coladd);
oap->start.col = curbuf->b_op_start_orig.col;
/* reset pre_textlen to the value of OP_INSERT */
pre_textlen += bd.textlen;
pre_textlen -= t - oap->start_vcol;
oap->start_vcol = t;
oap->op_type = OP_INSERT;
}
}
/*
* Spaces and tabs in the indent may have changed to other spaces and
* tabs. Get the starting column again and correct the length.
* Don't do this when "$" used, end-of-line will have changed.
*/
block_prep(oap, &bd2, oap->start.lnum, TRUE);
if (!bd.is_MAX || bd2.textlen < bd.textlen)
{
if (oap->op_type == OP_APPEND)
{
pre_textlen += bd2.textlen - bd.textlen;
if (bd2.endspaces)
--bd2.textlen;
}
bd.textcol = bd2.textcol;
bd.textlen = bd2.textlen;
}
/*
* Subsequent calls to ml_get() flush the firstline data - take a
* copy of the required string.
*/
firstline = ml_get(oap->start.lnum);
len = STRLEN(firstline);
add = bd.textcol;
if (oap->op_type == OP_APPEND)
add += bd.textlen;
if ((size_t)add > len)
firstline += len; // short line, point to the NUL
else
firstline += add;
if (pre_textlen >= 0
&& (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
{
ins_text = vim_strnsave(firstline, (int)ins_len);
if (ins_text != NULL)
{
/* block handled here */
if (u_save(oap->start.lnum,
(linenr_T)(oap->end.lnum + 1)) == OK)
block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
&bd);
curwin->w_cursor.col = oap->start.col;
check_cursor();
vim_free(ins_text);
}
}
}
}
/*
* op_change - handle a change operation
*
* return TRUE if edit() returns because of a CTRL-O command
*/
int
op_change(oparg_T *oap)
{
colnr_T l;
int retval;
long offset;
linenr_T linenr;
long ins_len;
long pre_textlen = 0;
long pre_indent = 0;
char_u *firstline;
char_u *ins_text, *newp, *oldp;
struct block_def bd;
l = oap->start.col;
if (oap->motion_type == MLINE)
{
l = 0;
#ifdef FEAT_SMARTINDENT
if (!p_paste && curbuf->b_p_si
# ifdef FEAT_CINDENT
&& !curbuf->b_p_cin
# endif
)
can_si = TRUE; /* It's like opening a new line, do si */
#endif
}
/* First delete the text in the region. In an empty buffer only need to
* save for undo */
if (curbuf->b_ml.ml_flags & ML_EMPTY)
{
if (u_save_cursor() == FAIL)
return FALSE;
}
else if (op_delete(oap) == FAIL)
return FALSE;
if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
&& !virtual_op)
inc_cursor();
/* check for still on same line (<CR> in inserted text meaningless) */
/* skip blank lines too */
if (oap->block_mode)
{
/* Add spaces before getting the current line length. */
if (virtual_op && (curwin->w_cursor.coladd > 0
|| gchar_cursor() == NUL))
coladvance_force(getviscol());
firstline = ml_get(oap->start.lnum);
pre_textlen = (long)STRLEN(firstline);
pre_indent = (long)getwhitecols(firstline);
bd.textcol = curwin->w_cursor.col;
}
#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
if (oap->motion_type == MLINE)
fix_indent();
#endif
retval = edit(NUL, FALSE, (linenr_T)1);
/*
* In Visual block mode, handle copying the new text to all lines of the
* block.
* Don't repeat the insert when Insert mode ended with CTRL-C.
*/
if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
{
/* Auto-indenting may have changed the indent. If the cursor was past
* the indent, exclude that indent change from the inserted text. */
firstline = ml_get(oap->start.lnum);
if (bd.textcol > (colnr_T)pre_indent)
{
long new_indent = (long)getwhitecols(firstline);
pre_textlen += new_indent - pre_indent;
bd.textcol += new_indent - pre_indent;
}
ins_len = (long)STRLEN(firstline) - pre_textlen;
if (ins_len > 0)
{
/* Subsequent calls to ml_get() flush the firstline data - take a
* copy of the inserted text. */
if ((ins_text = alloc(ins_len + 1)) != NULL)
{
vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
linenr++)
{
block_prep(oap, &bd, linenr, TRUE);
if (!bd.is_short || virtual_op)
{
pos_T vpos;
/* If the block starts in virtual space, count the
* initial coladd offset as part of "startspaces" */
if (bd.is_short)
{
vpos.lnum = linenr;
(void)getvpos(&vpos, oap->start_vcol);
}
else
vpos.coladd = 0;
oldp = ml_get(linenr);
newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
if (newp == NULL)
continue;
/* copy up to block start */
mch_memmove(newp, oldp, (size_t)bd.textcol);
offset = bd.textcol;
vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
offset += vpos.coladd;
mch_memmove(newp + offset, ins_text, (size_t)ins_len);
offset += ins_len;
oldp += bd.textcol;
STRMOVE(newp + offset, oldp);
ml_replace(linenr, newp, FALSE);
}
}
check_cursor();
changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
}
vim_free(ins_text);
}
}
return retval;
}
/*
* set all the yank registers to empty (called from main())
*/
void
init_yank(void)
{
int i;
for (i = 0; i < NUM_REGISTERS; ++i)
y_regs[i].y_array = NULL;
}
#if defined(EXITFREE) || defined(PROTO)
void
clear_registers(void)
{
int i;
for (i = 0; i < NUM_REGISTERS; ++i)
{
y_current = &y_regs[i];
if (y_current->y_array != NULL)
free_yank_all();
}
}
#endif
/*
* Free "n" lines from the current yank register.
* Called for normal freeing and in case of error.
*/
static void
free_yank(long n)
{
if (y_current->y_array != NULL)
{
long i;
for (i = n; --i >= 0; )
{
#ifdef AMIGA /* only for very slow machines */
if ((i & 1023) == 1023) /* this may take a while */
{
/*
* This message should never cause a hit-return message.
* Overwrite this message with any next message.
*/
++no_wait_return;
smsg(_("freeing %ld lines"), i + 1);
--no_wait_return;
msg_didout = FALSE;
msg_col = 0;
}
#endif
vim_free(y_current->y_array[i]);
}
VIM_CLEAR(y_current->y_array);
#ifdef AMIGA
if (n >= 1000)
msg("");
#endif
}
}
static void
free_yank_all(void)
{
free_yank(y_current->y_size);
}
/*
* Yank the text between "oap->start" and "oap->end" into a yank register.
* If we are to append (uppercase register), we first yank into a new yank
* register and then concatenate the old and the new one (so we keep the old
* one in case of out-of-memory).
*
* Return FAIL for failure, OK otherwise.
*/
int
op_yank(oparg_T *oap, int deleting, int mess)
{
long y_idx; /* index in y_array[] */
yankreg_T *curr; /* copy of y_current */
yankreg_T newreg; /* new yank register when appending */
char_u **new_ptr;
linenr_T lnum; /* current line number */
long j;
int yanktype = oap->motion_type;
long yanklines = oap->line_count;
linenr_T yankendlnum = oap->end.lnum;
char_u *p;
char_u *pnew;
struct block_def bd;
#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
int did_star = FALSE;
#endif
/* check for read-only register */
if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
{
beep_flush();
return FAIL;
}
if (oap->regname == '_') /* black hole: nothing to do */
return OK;
#ifdef FEAT_CLIPBOARD
if (!clip_star.available && oap->regname == '*')
oap->regname = 0;
else if (!clip_plus.available && oap->regname == '+')
oap->regname = 0;
#endif
if (!deleting) /* op_delete() already set y_current */
get_yank_register(oap->regname, TRUE);
curr = y_current;
/* append to existing contents */
if (y_append && y_current->y_array != NULL)
y_current = &newreg;
else
free_yank_all(); /* free previously yanked lines */
/*
* If the cursor was in column 1 before and after the movement, and the
* operator is not inclusive, the yank is always linewise.
*/
if ( oap->motion_type == MCHAR
&& oap->start.col == 0
&& !oap->inclusive
&& (!oap->is_VIsual || *p_sel == 'o')
&& !oap->block_mode
&& oap->end.col == 0
&& yanklines > 1)
{
yanktype = MLINE;
--yankendlnum;
--yanklines;
}
y_current->y_size = yanklines;
y_current->y_type = yanktype; /* set the yank register type */
y_current->y_width = 0;
y_current->y_array = lalloc_clear(sizeof(char_u *) * yanklines, TRUE);
if (y_current->y_array == NULL)
{
y_current = curr;
return FAIL;
}
#ifdef FEAT_VIMINFO
y_current->y_time_set = vim_time();
#endif
y_idx = 0;
lnum = oap->start.lnum;
if (oap->block_mode)
{
/* Visual block mode */
y_current->y_type = MBLOCK; /* set the yank register type */
y_current->y_width = oap->end_vcol - oap->start_vcol;
if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
y_current->y_width--;
}
for ( ; lnum <= yankendlnum; lnum++, y_idx++)
{
switch (y_current->y_type)
{
case MBLOCK:
block_prep(oap, &bd, lnum, FALSE);
if (yank_copy_line(&bd, y_idx) == FAIL)
goto fail;
break;
case MLINE:
if ((y_current->y_array[y_idx] =
vim_strsave(ml_get(lnum))) == NULL)
goto fail;
break;
case MCHAR:
{
colnr_T startcol = 0, endcol = MAXCOL;
int is_oneChar = FALSE;
colnr_T cs, ce;
p = ml_get(lnum);
bd.startspaces = 0;
bd.endspaces = 0;
if (lnum == oap->start.lnum)
{
startcol = oap->start.col;
if (virtual_op)
{
getvcol(curwin, &oap->start, &cs, NULL, &ce);
if (ce != cs && oap->start.coladd > 0)
{
/* Part of a tab selected -- but don't
* double-count it. */
bd.startspaces = (ce - cs + 1)
- oap->start.coladd;
startcol++;
}
}
}
if (lnum == oap->end.lnum)
{
endcol = oap->end.col;
if (virtual_op)
{
getvcol(curwin, &oap->end, &cs, NULL, &ce);
if (p[endcol] == NUL || (cs + oap->end.coladd < ce
/* Don't add space for double-wide
* char; endcol will be on last byte
* of multi-byte char. */
&& (*mb_head_off)(p, p + endcol) == 0))
{
if (oap->start.lnum == oap->end.lnum
&& oap->start.col == oap->end.col)
{
/* Special case: inside a single char */
is_oneChar = TRUE;
bd.startspaces = oap->end.coladd
- oap->start.coladd + oap->inclusive;
endcol = startcol;
}
else
{
bd.endspaces = oap->end.coladd
+ oap->inclusive;
endcol -= oap->inclusive;
}
}
}
}
if (endcol == MAXCOL)
endcol = (colnr_T)STRLEN(p);
if (startcol > endcol || is_oneChar)
bd.textlen = 0;
else
bd.textlen = endcol - startcol + oap->inclusive;
bd.textstart = p + startcol;
if (yank_copy_line(&bd, y_idx) == FAIL)
goto fail;
break;
}
/* NOTREACHED */
}
}
if (curr != y_current) /* append the new block to the old block */
{
new_ptr = ALLOC_MULT(char_u *, curr->y_size + y_current->y_size);
if (new_ptr == NULL)
goto fail;
for (j = 0; j < curr->y_size; ++j)
new_ptr[j] = curr->y_array[j];
vim_free(curr->y_array);
curr->y_array = new_ptr;
#ifdef FEAT_VIMINFO
curr->y_time_set = vim_time();
#endif
if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
curr->y_type = MLINE;
/* Concatenate the last line of the old block with the first line of
* the new block, unless being Vi compatible. */
if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
{
pnew = alloc(STRLEN(curr->y_array[curr->y_size - 1])
+ STRLEN(y_current->y_array[0]) + 1);
if (pnew == NULL)
{
y_idx = y_current->y_size - 1;
goto fail;
}
STRCPY(pnew, curr->y_array[--j]);
STRCAT(pnew, y_current->y_array[0]);
vim_free(curr->y_array[j]);
vim_free(y_current->y_array[0]);
curr->y_array[j++] = pnew;
y_idx = 1;
}
else
y_idx = 0;
while (y_idx < y_current->y_size)
curr->y_array[j++] = y_current->y_array[y_idx++];
curr->y_size = j;
vim_free(y_current->y_array);
y_current = curr;
}
if (curwin->w_p_rnu)
redraw_later(SOME_VALID); /* cursor moved to start */
if (mess) /* Display message about yank? */
{
if (yanktype == MCHAR
&& !oap->block_mode
&& yanklines == 1)
yanklines = 0;
/* Some versions of Vi use ">=" here, some don't... */
if (yanklines > p_report)
{
char namebuf[100];
if (oap->regname == NUL)
*namebuf = NUL;
else
vim_snprintf(namebuf, sizeof(namebuf),
_(" into \"%c"), oap->regname);
/* redisplay now, so message is not deleted */
update_topline_redraw();
if (oap->block_mode)
{
smsg(NGETTEXT("block of %ld line yanked%s",
"block of %ld lines yanked%s", yanklines),
yanklines, namebuf);
}
else
{
smsg(NGETTEXT("%ld line yanked%s",
"%ld lines yanked%s", yanklines),
yanklines, namebuf);
}
}
}
/*
* Set "'[" and "']" marks.
*/
curbuf->b_op_start = oap->start;
curbuf->b_op_end = oap->end;
if (yanktype == MLINE && !oap->block_mode)
{
curbuf->b_op_start.col = 0;
curbuf->b_op_end.col = MAXCOL;
}
#ifdef FEAT_CLIPBOARD
/*
* If we were yanking to the '*' register, send result to clipboard.
* If no register was specified, and "unnamed" in 'clipboard', make a copy
* to the '*' register.
*/
if (clip_star.available
&& (curr == &(y_regs[STAR_REGISTER])
|| (!deleting && oap->regname == 0
&& ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
{
if (curr != &(y_regs[STAR_REGISTER]))
/* Copy the text from register 0 to the clipboard register. */
copy_yank_reg(&(y_regs[STAR_REGISTER]));
clip_own_selection(&clip_star);
clip_gen_set_selection(&clip_star);
# ifdef FEAT_X11
did_star = TRUE;
# endif
}
# ifdef FEAT_X11
/*
* If we were yanking to the '+' register, send result to selection.
* Also copy to the '*' register, in case auto-select is off.
*/
if (clip_plus.available
&& (curr == &(y_regs[PLUS_REGISTER])
|| (!deleting && oap->regname == 0
&& ((clip_unnamed | clip_unnamed_saved) &
CLIP_UNNAMED_PLUS))))
{
if (curr != &(y_regs[PLUS_REGISTER]))
/* Copy the text from register 0 to the clipboard register. */
copy_yank_reg(&(y_regs[PLUS_REGISTER]));
clip_own_selection(&clip_plus);
clip_gen_set_selection(&clip_plus);
if (!clip_isautosel_star() && !clip_isautosel_plus()
&& !did_star && curr == &(y_regs[PLUS_REGISTER]))
{
copy_yank_reg(&(y_regs[STAR_REGISTER]));
clip_own_selection(&clip_star);
clip_gen_set_selection(&clip_star);
}
}
# endif
#endif
#if defined(FEAT_EVAL)
if (!deleting && has_textyankpost())
yank_do_autocmd(oap, y_current);
#endif
return OK;
fail: /* free the allocated lines */
free_yank(y_idx + 1);
y_current = curr;
return FAIL;
}
static int
yank_copy_line(struct block_def *bd, long y_idx)
{
char_u *pnew;
if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
== NULL)
return FAIL;
y_current->y_array[y_idx] = pnew;
vim_memset(pnew, ' ', (size_t)bd->startspaces);
pnew += bd->startspaces;
mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
pnew += bd->textlen;
vim_memset(pnew, ' ', (size_t)bd->endspaces);
pnew += bd->endspaces;
*pnew = NUL;
return OK;
}
#ifdef FEAT_CLIPBOARD
/*
* Make a copy of the y_current register to register "reg".
*/
static void
copy_yank_reg(yankreg_T *reg)
{
yankreg_T *curr = y_current;
long j;
y_current = reg;
free_yank_all();
*y_current = *curr;
y_current->y_array = lalloc_clear(
sizeof(char_u *) * y_current->y_size, TRUE);
if (y_current->y_array == NULL)
y_current->y_size = 0;
else
for (j = 0; j < y_current->y_size; ++j)
if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
{
free_yank(j);
y_current->y_size = 0;
break;
}
y_current = curr;
}
#endif
/*
* Put contents of register "regname" into the text.
* Caller must check "regname" to be valid!
* "flags": PUT_FIXINDENT make indent look nice
* PUT_CURSEND leave cursor after end of new text
* PUT_LINE force linewise put (":put")
*/
void
do_put(
int regname,
int dir, /* BACKWARD for 'P', FORWARD for 'p' */
long count,
int flags)
{
char_u *ptr;
char_u *newp, *oldp;
int yanklen;
int totlen = 0; /* init for gcc */
linenr_T lnum;
colnr_T col;
long i; /* index in y_array[] */
int y_type;
long y_size;
int oldlen;
long y_width = 0;
colnr_T vcol;
int delcount;
int incr = 0;
long j;
struct block_def bd;
char_u **y_array = NULL;
long nr_lines = 0;
pos_T new_cursor;
int indent;
int orig_indent = 0; /* init for gcc */
int indent_diff = 0; /* init for gcc */
int first_indent = TRUE;
int lendiff = 0;
pos_T old_pos;
char_u *insert_string = NULL;
int allocated = FALSE;
long cnt;
#ifdef FEAT_CLIPBOARD
/* Adjust register name for "unnamed" in 'clipboard'. */
adjust_clip_reg(®name);
(void)may_get_selection(regname);
#endif
if (flags & PUT_FIXINDENT)
orig_indent = get_indent();
curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
/*
* Using inserted text works differently, because the register includes
* special characters (newlines, etc.).
*/
if (regname == '.')
{
if (VIsual_active)
stuffcharReadbuff(VIsual_mode);
(void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
(count == -1 ? 'O' : 'i')), count, FALSE);
/* Putting the text is done later, so can't really move the cursor to
* the next character. Use "l" to simulate it. */
if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
stuffcharReadbuff('l');
return;
}
/*
* For special registers '%' (file name), '#' (alternate file name) and
* ':' (last command line), etc. we have to create a fake yank register.
*/
if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
{
if (insert_string == NULL)
return;
}
/* Autocommands may be executed when saving lines for undo. This might
* make "y_array" invalid, so we start undo now to avoid that. */
if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
goto end;
if (insert_string != NULL)
{
y_type = MCHAR;
#ifdef FEAT_EVAL
if (regname == '=')
{
/* For the = register we need to split the string at NL
* characters.
* Loop twice: count the number of lines and save them. */
for (;;)
{
y_size = 0;
ptr = insert_string;
while (ptr != NULL)
{
if (y_array != NULL)
y_array[y_size] = ptr;
++y_size;
ptr = vim_strchr(ptr, '\n');
if (ptr != NULL)
{
if (y_array != NULL)
*ptr = NUL;
++ptr;
/* A trailing '\n' makes the register linewise. */
if (*ptr == NUL)
{
y_type = MLINE;
break;
}
}
}
if (y_array != NULL)
break;
y_array = ALLOC_MULT(char_u *, y_size);
if (y_array == NULL)
goto end;
}
}
else
#endif
{
y_size = 1; /* use fake one-line yank register */
y_array = &insert_string;
}
}
else
{
get_yank_register(regname, FALSE);
y_type = y_current->y_type;
y_width = y_current->y_width;
y_size = y_current->y_size;
y_array = y_current->y_array;
}
if (y_type == MLINE)
{
if (flags & PUT_LINE_SPLIT)
{
char_u *p;
/* "p" or "P" in Visual mode: split the lines to put the text in
* between. */
if (u_save_cursor() == FAIL)
goto end;
p = ml_get_cursor();
if (dir == FORWARD && *p != NUL)
MB_PTR_ADV(p);
ptr = vim_strsave(p);
if (ptr == NULL)
goto end;
ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
vim_free(ptr);
oldp = ml_get_curline();
p = oldp + curwin->w_cursor.col;
if (dir == FORWARD && *p != NUL)
MB_PTR_ADV(p);
ptr = vim_strnsave(oldp, p - oldp);
if (ptr == NULL)
goto end;
ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
++nr_lines;
dir = FORWARD;
}
if (flags & PUT_LINE_FORWARD)
{
/* Must be "p" for a Visual block, put lines below the block. */
curwin->w_cursor = curbuf->b_visual.vi_end;
dir = FORWARD;
}
curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
}
if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
y_type = MLINE;
if (y_size == 0 || y_array == NULL)
{
semsg(_("E353: Nothing in register %s"),
regname == 0 ? (char_u *)"\"" : transchar(regname));
goto end;
}
if (y_type == MBLOCK)
{
lnum = curwin->w_cursor.lnum + y_size + 1;
if (lnum > curbuf->b_ml.ml_line_count)
lnum = curbuf->b_ml.ml_line_count + 1;
if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
goto end;
}
else if (y_type == MLINE)
{
lnum = curwin->w_cursor.lnum;
#ifdef FEAT_FOLDING
/* Correct line number for closed fold. Don't move the cursor yet,
* u_save() uses it. */
if (dir == BACKWARD)
(void)hasFolding(lnum, &lnum, NULL);
else
(void)hasFolding(lnum, NULL, &lnum);
#endif
if (dir == FORWARD)
++lnum;
/* In an empty buffer the empty line is going to be replaced, include
* it in the saved lines. */
if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
goto end;
#ifdef FEAT_FOLDING
if (dir == FORWARD)
curwin->w_cursor.lnum = lnum - 1;
else
curwin->w_cursor.lnum = lnum;
curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
#endif
}
else if (u_save_cursor() == FAIL)
goto end;
yanklen = (int)STRLEN(y_array[0]);
if (ve_flags == VE_ALL && y_type == MCHAR)
{
if (gchar_cursor() == TAB)
{
/* Don't need to insert spaces when "p" on the last position of a
* tab or "P" on the first position. */
#ifdef FEAT_VARTABS
int viscol = getviscol();
if (dir == FORWARD
? tabstop_padding(viscol, curbuf->b_p_ts,
curbuf->b_p_vts_array) != 1
: curwin->w_cursor.coladd > 0)
coladvance_force(viscol);
#else
if (dir == FORWARD
? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
: curwin->w_cursor.coladd > 0)
coladvance_force(getviscol());
#endif
else
curwin->w_cursor.coladd = 0;
}
else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
coladvance_force(getviscol() + (dir == FORWARD));
}
lnum = curwin->w_cursor.lnum;
col = curwin->w_cursor.col;
/*
* Block mode
*/
if (y_type == MBLOCK)
{
int c = gchar_cursor();
colnr_T endcol2 = 0;
if (dir == FORWARD && c != NUL)
{
if (ve_flags == VE_ALL)
getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
else
getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
if (has_mbyte)
/* move to start of next multi-byte character */
curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
else
if (c != TAB || ve_flags != VE_ALL)
++curwin->w_cursor.col;
++col;
}
else
getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
col += curwin->w_cursor.coladd;
if (ve_flags == VE_ALL
&& (curwin->w_cursor.coladd > 0
|| endcol2 == curwin->w_cursor.col))
{
if (dir == FORWARD && c == NUL)
++col;
if (dir != FORWARD && c != NUL)
++curwin->w_cursor.col;
if (c == TAB)
{
if (dir == BACKWARD && curwin->w_cursor.col)
curwin->w_cursor.col--;
if (dir == FORWARD && col - 1 == endcol2)
curwin->w_cursor.col++;
}
}
curwin->w_cursor.coladd = 0;
bd.textcol = 0;
for (i = 0; i < y_size; ++i)
{
int spaces;
char shortline;
bd.startspaces = 0;
bd.endspaces = 0;
vcol = 0;
delcount = 0;
/* add a new line */
if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
{
if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
(colnr_T)1, FALSE) == FAIL)
break;
++nr_lines;
}
/* get the old line and advance to the position to insert at */
oldp = ml_get_curline();
oldlen = (int)STRLEN(oldp);
for (ptr = oldp; vcol < col && *ptr; )
{
/* Count a tab for what it's worth (if list mode not on) */
incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
vcol += incr;
}
bd.textcol = (colnr_T)(ptr - oldp);
shortline = (vcol < col) || (vcol == col && !*ptr) ;
if (vcol < col) /* line too short, padd with spaces */
bd.startspaces = col - vcol;
else if (vcol > col)
{
bd.endspaces = vcol - col;
bd.startspaces = incr - bd.endspaces;
--bd.textcol;
delcount = 1;
if (has_mbyte)
bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
if (oldp[bd.textcol] != TAB)
{
/* Only a Tab can be split into spaces. Other
* characters will have to be moved to after the
* block, causing misalignment. */
delcount = 0;
bd.endspaces = 0;
}
}
yanklen = (int)STRLEN(y_array[i]);
/* calculate number of spaces required to fill right side of block*/
spaces = y_width + 1;
for (j = 0; j < yanklen; j++)
spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
if (spaces < 0)
spaces = 0;
/* insert the new text */
totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
newp = alloc(totlen + oldlen + 1);
if (newp == NULL)
break;
/* copy part up to cursor to new line */
ptr = newp;
mch_memmove(ptr, oldp, (size_t)bd.textcol);
ptr += bd.textcol;
/* may insert some spaces before the new text */
vim_memset(ptr, ' ', (size_t)bd.startspaces);
ptr += bd.startspaces;
/* insert the new text */
for (j = 0; j < count; ++j)
{
mch_memmove(ptr, y_array[i], (size_t)yanklen);
ptr += yanklen;
/* insert block's trailing spaces only if there's text behind */
if ((j < count - 1 || !shortline) && spaces)
{
vim_memset(ptr, ' ', (size_t)spaces);
ptr += spaces;
}
}
/* may insert some spaces after the new text */
vim_memset(ptr, ' ', (size_t)bd.endspaces);
ptr += bd.endspaces;
/* move the text after the cursor to the end of the line. */
mch_memmove(ptr, oldp + bd.textcol + delcount,
(size_t)(oldlen - bd.textcol - delcount + 1));
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
++curwin->w_cursor.lnum;
if (i == 0)
curwin->w_cursor.col += bd.startspaces;
}
changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
/* Set '[ mark. */
curbuf->b_op_start = curwin->w_cursor;
curbuf->b_op_start.lnum = lnum;
/* adjust '] mark */
curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
curbuf->b_op_end.col = bd.textcol + totlen - 1;
curbuf->b_op_end.coladd = 0;
if (flags & PUT_CURSEND)
{
colnr_T len;
curwin->w_cursor = curbuf->b_op_end;
curwin->w_cursor.col++;
/* in Insert mode we might be after the NUL, correct for that */
len = (colnr_T)STRLEN(ml_get_curline());
if (curwin->w_cursor.col > len)
curwin->w_cursor.col = len;
}
else
curwin->w_cursor.lnum = lnum;
}
else
{
/*
* Character or Line mode
*/
if (y_type == MCHAR)
{
/* if type is MCHAR, FORWARD is the same as BACKWARD on the next
* char */
if (dir == FORWARD && gchar_cursor() != NUL)
{
if (has_mbyte)
{
int bytelen = (*mb_ptr2len)(ml_get_cursor());
/* put it on the next of the multi-byte character. */
col += bytelen;
if (yanklen)
{
curwin->w_cursor.col += bytelen;
curbuf->b_op_end.col += bytelen;
}
}
else
{
++col;
if (yanklen)
{
++curwin->w_cursor.col;
++curbuf->b_op_end.col;
}
}
}
curbuf->b_op_start = curwin->w_cursor;
}
/*
* Line mode: BACKWARD is the same as FORWARD on the previous line
*/
else if (dir == BACKWARD)
--lnum;
new_cursor = curwin->w_cursor;
/*
* simple case: insert into current line
*/
if (y_type == MCHAR && y_size == 1)
{
linenr_T end_lnum = 0; /* init for gcc */
if (VIsual_active)
{
end_lnum = curbuf->b_visual.vi_end.lnum;
if (end_lnum < curbuf->b_visual.vi_start.lnum)
end_lnum = curbuf->b_visual.vi_start.lnum;
}
do {
totlen = count * yanklen;
if (totlen > 0)
{
oldp = ml_get(lnum);
if (VIsual_active && col > (int)STRLEN(oldp))
{
lnum++;
continue;
}
newp = alloc(STRLEN(oldp) + totlen + 1);
if (newp == NULL)
goto end; /* alloc() gave an error message */
mch_memmove(newp, oldp, (size_t)col);
ptr = newp + col;
for (i = 0; i < count; ++i)
{
mch_memmove(ptr, y_array[0], (size_t)yanklen);
ptr += yanklen;
}
STRMOVE(ptr, oldp + col);
ml_replace(lnum, newp, FALSE);
/* Place cursor on last putted char. */
if (lnum == curwin->w_cursor.lnum)
{
/* make sure curwin->w_virtcol is updated */
changed_cline_bef_curs();
curwin->w_cursor.col += (colnr_T)(totlen - 1);
}
}
if (VIsual_active)
lnum++;
} while (VIsual_active && lnum <= end_lnum);
if (VIsual_active) /* reset lnum to the last visual line */
lnum--;
curbuf->b_op_end = curwin->w_cursor;
/* For "CTRL-O p" in Insert mode, put cursor after last char */
if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
++curwin->w_cursor.col;
changed_bytes(lnum, col);
}
else
{
/*
* Insert at least one line. When y_type is MCHAR, break the first
* line in two.
*/
for (cnt = 1; cnt <= count; ++cnt)
{
i = 0;
if (y_type == MCHAR)
{
/*
* Split the current line in two at the insert position.
* First insert y_array[size - 1] in front of second line.
* Then append y_array[0] to first line.
*/
lnum = new_cursor.lnum;
ptr = ml_get(lnum) + col;
totlen = (int)STRLEN(y_array[y_size - 1]);
newp = alloc(STRLEN(ptr) + totlen + 1);
if (newp == NULL)
goto error;
STRCPY(newp, y_array[y_size - 1]);
STRCAT(newp, ptr);
/* insert second line */
ml_append(lnum, newp, (colnr_T)0, FALSE);
vim_free(newp);
oldp = ml_get(lnum);
newp = alloc(col + yanklen + 1);
if (newp == NULL)
goto error;
/* copy first part of line */
mch_memmove(newp, oldp, (size_t)col);
/* append to first line */
mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
ml_replace(lnum, newp, FALSE);
curwin->w_cursor.lnum = lnum;
i = 1;
}
for (; i < y_size; ++i)
{
if ((y_type != MCHAR || i < y_size - 1)
&& ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
== FAIL)
goto error;
lnum++;
++nr_lines;
if (flags & PUT_FIXINDENT)
{
old_pos = curwin->w_cursor;
curwin->w_cursor.lnum = lnum;
ptr = ml_get(lnum);
if (cnt == count && i == y_size - 1)
lendiff = (int)STRLEN(ptr);
#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
if (*ptr == '#' && preprocs_left())
indent = 0; /* Leave # lines at start */
else
#endif
if (*ptr == NUL)
indent = 0; /* Ignore empty lines */
else if (first_indent)
{
indent_diff = orig_indent - get_indent();
indent = orig_indent;
first_indent = FALSE;
}
else if ((indent = get_indent() + indent_diff) < 0)
indent = 0;
(void)set_indent(indent, 0);
curwin->w_cursor = old_pos;
/* remember how many chars were removed */
if (cnt == count && i == y_size - 1)
lendiff -= (int)STRLEN(ml_get(lnum));
}
}
}
error:
/* Adjust marks. */
if (y_type == MLINE)
{
curbuf->b_op_start.col = 0;
if (dir == FORWARD)
curbuf->b_op_start.lnum++;
}
/* Skip mark_adjust when adding lines after the last one, there
* can't be marks there. But still needed in diff mode. */
if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
< curbuf->b_ml.ml_line_count
#ifdef FEAT_DIFF
|| curwin->w_p_diff
#endif
)
mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
(linenr_T)MAXLNUM, nr_lines, 0L);
/* note changed text for displaying and folding */
if (y_type == MCHAR)
changed_lines(curwin->w_cursor.lnum, col,
curwin->w_cursor.lnum + 1, nr_lines);
else
changed_lines(curbuf->b_op_start.lnum, 0,
curbuf->b_op_start.lnum, nr_lines);
/* put '] mark at last inserted character */
curbuf->b_op_end.lnum = lnum;
/* correct length for change in indent */
col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
if (col > 1)
curbuf->b_op_end.col = col - 1;
else
curbuf->b_op_end.col = 0;
if (flags & PUT_CURSLINE)
{
/* ":put": put cursor on last inserted line */
curwin->w_cursor.lnum = lnum;
beginline(BL_WHITE | BL_FIX);
}
else if (flags & PUT_CURSEND)
{
/* put cursor after inserted text */
if (y_type == MLINE)
{
if (lnum >= curbuf->b_ml.ml_line_count)
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
else
curwin->w_cursor.lnum = lnum + 1;
curwin->w_cursor.col = 0;
}
else
{
curwin->w_cursor.lnum = lnum;
curwin->w_cursor.col = col;
}
}
else if (y_type == MLINE)
{
/* put cursor on first non-blank in first inserted line */
curwin->w_cursor.col = 0;
if (dir == FORWARD)
++curwin->w_cursor.lnum;
beginline(BL_WHITE | BL_FIX);
}
else /* put cursor on first inserted character */
curwin->w_cursor = new_cursor;
}
}
msgmore(nr_lines);
curwin->w_set_curswant = TRUE;
end:
if (allocated)
vim_free(insert_string);
if (regname == '=')
vim_free(y_array);
VIsual_active = FALSE;
/* If the cursor is past the end of the line put it at the end. */
adjust_cursor_eol();
}
/*
* When the cursor is on the NUL past the end of the line and it should not be
* there move it left.
*/
void
adjust_cursor_eol(void)
{
if (curwin->w_cursor.col > 0
&& gchar_cursor() == NUL
&& (ve_flags & VE_ONEMORE) == 0
&& !(restart_edit || (State & INSERT)))
{
/* Put the cursor on the last character in the line. */
dec_cursor();
if (ve_flags == VE_ALL)
{
colnr_T scol, ecol;
/* Coladd is set to the width of the last character. */
getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
curwin->w_cursor.coladd = ecol - scol + 1;
}
}
}
#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
/*
* Return TRUE if lines starting with '#' should be left aligned.
*/
int
preprocs_left(void)
{
return
# ifdef FEAT_SMARTINDENT
# ifdef FEAT_CINDENT
(curbuf->b_p_si && !curbuf->b_p_cin) ||
# else
curbuf->b_p_si
# endif
# endif
# ifdef FEAT_CINDENT
(curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
&& curbuf->b_ind_hash_comment == 0)
# endif
;
}
#endif
/*
* Return the character name of the register with the given number.
*/
int
get_register_name(int num)
{
if (num == -1)
return '"';
else if (num < 10)
return num + '0';
else if (num == DELETION_REGISTER)
return '-';
#ifdef FEAT_CLIPBOARD
else if (num == STAR_REGISTER)
return '*';
else if (num == PLUS_REGISTER)
return '+';
#endif
else
{
#ifdef EBCDIC
int i;
/* EBCDIC is really braindead ... */
i = 'a' + (num - 10);
if (i > 'i')
i += 7;
if (i > 'r')
i += 8;
return i;
#else
return num + 'a' - 10;
#endif
}
}
/*
* ":dis" and ":registers": Display the contents of the yank registers.
*/
void
ex_display(exarg_T *eap)
{
int i, n;
long j;
char_u *p;
yankreg_T *yb;
int name;
int attr;
char_u *arg = eap->arg;
int clen;
if (arg != NULL && *arg == NUL)
arg = NULL;
attr = HL_ATTR(HLF_8);
/* Highlight title */
msg_puts_title(_("\n--- Registers ---"));
for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
{
name = get_register_name(i);
if (arg != NULL && vim_strchr(arg, name) == NULL
#ifdef ONE_CLIPBOARD
/* Star register and plus register contain the same thing. */
&& (name != '*' || vim_strchr(arg, '+') == NULL)
#endif
)
continue; /* did not ask for this register */
#ifdef FEAT_CLIPBOARD
/* Adjust register name for "unnamed" in 'clipboard'.
* When it's a clipboard register, fill it with the current contents
* of the clipboard. */
adjust_clip_reg(&name);
(void)may_get_selection(name);
#endif
if (i == -1)
{
if (y_previous != NULL)
yb = y_previous;
else
yb = &(y_regs[0]);
}
else
yb = &(y_regs[i]);
#ifdef FEAT_EVAL
if (name == MB_TOLOWER(redir_reg)
|| (redir_reg == '"' && yb == y_previous))
continue; /* do not list register being written to, the
* pointer can be freed */
#endif
if (yb->y_array != NULL)
{
msg_putchar('\n');
msg_putchar('"');
msg_putchar(name);
msg_puts(" ");
n = (int)Columns - 6;
for (j = 0; j < yb->y_size && n > 1; ++j)
{
if (j)
{
msg_puts_attr("^J", attr);
n -= 2;
}
for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
{
clen = (*mb_ptr2len)(p);
msg_outtrans_len(p, clen);
p += clen - 1;
}
}
if (n > 1 && yb->y_type == MLINE)
msg_puts_attr("^J", attr);
out_flush(); /* show one line at a time */
}
ui_breakcheck();
}
/*
* display last inserted text
*/
if ((p = get_last_insert()) != NULL
&& (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
{
msg_puts("\n\". ");
dis_msg(p, TRUE);
}
/*
* display last command line
*/
if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
&& !got_int)
{
msg_puts("\n\": ");
dis_msg(last_cmdline, FALSE);
}
/*
* display current file name
*/
if (curbuf->b_fname != NULL
&& (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
{
msg_puts("\n\"% ");
dis_msg(curbuf->b_fname, FALSE);
}
/*
* display alternate file name
*/
if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
{
char_u *fname;
linenr_T dummy;
if (buflist_name_nr(0, &fname, &dummy) != FAIL)
{
msg_puts("\n\"# ");
dis_msg(fname, FALSE);
}
}
/*
* display last search pattern
*/
if (last_search_pat() != NULL
&& (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
{
msg_puts("\n\"/ ");
dis_msg(last_search_pat(), FALSE);
}
#ifdef FEAT_EVAL
/*
* display last used expression
*/
if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
&& !got_int)
{
msg_puts("\n\"= ");
dis_msg(expr_line, FALSE);
}
#endif
}
/*
* display a string for do_dis()
* truncate at end of screen line
*/
static void
dis_msg(
char_u *p,
int skip_esc) /* if TRUE, ignore trailing ESC */
{
int n;
int l;
n = (int)Columns - 6;
while (*p != NUL
&& !(*p == ESC && skip_esc && *(p + 1) == NUL)
&& (n -= ptr2cells(p)) >= 0)
{
if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
{
msg_outtrans_len(p, l);
p += l;
}
else
msg_outtrans_len(p++, 1);
}
ui_breakcheck();
}
#if defined(FEAT_COMMENTS) || defined(PROTO)
/*
* If "process" is TRUE and the line begins with a comment leader (possibly
* after some white space), return a pointer to the text after it. Put a boolean
* value indicating whether the line ends with an unclosed comment in
* "is_comment".
* line - line to be processed,
* process - if FALSE, will only check whether the line ends with an unclosed
* comment,
* include_space - whether to also skip space following the comment leader,
* is_comment - will indicate whether the current line ends with an unclosed
* comment.
*/
char_u *
skip_comment(
char_u *line,
int process,
int include_space,
int *is_comment)
{
char_u *comment_flags = NULL;
int lead_len;
int leader_offset = get_last_leader_offset(line, &comment_flags);
*is_comment = FALSE;
if (leader_offset != -1)
{
/* Let's check whether the line ends with an unclosed comment.
* If the last comment leader has COM_END in flags, there's no comment.
*/
while (*comment_flags)
{
if (*comment_flags == COM_END
|| *comment_flags == ':')
break;
++comment_flags;
}
if (*comment_flags != COM_END)
*is_comment = TRUE;
}
if (process == FALSE)
return line;
lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
if (lead_len == 0)
return line;
/* Find:
* - COM_END,
* - colon,
* whichever comes first.
*/
while (*comment_flags)
{
if (*comment_flags == COM_END
|| *comment_flags == ':')
break;
++comment_flags;
}
/* If we found a colon, it means that we are not processing a line
* starting with a closing part of a three-part comment. That's good,
* because we don't want to remove those as this would be annoying.
*/
if (*comment_flags == ':' || *comment_flags == NUL)
line += lead_len;
return line;
}
#endif
/*
* Join 'count' lines (minimal 2) at cursor position.
* When "save_undo" is TRUE save lines for undo first.
* Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
* leaders should not be removed.
* When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
* to set those marks.
*
* return FAIL for failure, OK otherwise
*/
int
do_join(
long count,
int insert_space,
int save_undo,
int use_formatoptions UNUSED,
int setmark)
{
char_u *curr = NULL;
char_u *curr_start = NULL;
char_u *cend;
char_u *newp;
char_u *spaces; /* number of spaces inserted before a line */
int endcurr1 = NUL;
int endcurr2 = NUL;
int currsize = 0; /* size of the current line */
int sumsize = 0; /* size of the long new line */
linenr_T t;
colnr_T col = 0;
int ret = OK;
#if defined(FEAT_COMMENTS) || defined(PROTO)
int *comments = NULL;
int remove_comments = (use_formatoptions == TRUE)
&& has_format_option(FO_REMOVE_COMS);
int prev_was_comment;
#endif
#ifdef FEAT_TEXT_PROP
textprop_T **prop_lines = NULL;
int *prop_lengths = NULL;
#endif
if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
(linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
return FAIL;
/* Allocate an array to store the number of spaces inserted before each
* line. We will use it to pre-compute the length of the new line and the
* proper placement of each original line in the new one. */
spaces = lalloc_clear(count, TRUE);
if (spaces == NULL)
return FAIL;
#if defined(FEAT_COMMENTS) || defined(PROTO)
if (remove_comments)
{
comments = lalloc_clear(count * sizeof(int), TRUE);
if (comments == NULL)
{
vim_free(spaces);
return FAIL;
}
}
#endif
/*
* Don't move anything yet, just compute the final line length
* and setup the array of space strings lengths
* This loops forward over the joined lines.
*/
for (t = 0; t < count; ++t)
{
curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
if (t == 0 && setmark)
{
/* Set the '[ mark. */
curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
}
#if defined(FEAT_COMMENTS) || defined(PROTO)
if (remove_comments)
{
/* We don't want to remove the comment leader if the
* previous line is not a comment. */
if (t > 0 && prev_was_comment)
{
char_u *new_curr = skip_comment(curr, TRUE, insert_space,
&prev_was_comment);
comments[t] = (int)(new_curr - curr);
curr = new_curr;
}
else
curr = skip_comment(curr, FALSE, insert_space,
&prev_was_comment);
}
#endif
if (insert_space && t > 0)
{
curr = skipwhite(curr);
if (*curr != ')' && currsize != 0 && endcurr1 != TAB
&& (!has_format_option(FO_MBYTE_JOIN)
|| (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
&& (!has_format_option(FO_MBYTE_JOIN2)
|| mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
)
{
/* don't add a space if the line is ending in a space */
if (endcurr1 == ' ')
endcurr1 = endcurr2;
else
++spaces[t];
/* extra space when 'joinspaces' set and line ends in '.' */
if ( p_js
&& (endcurr1 == '.'
|| (vim_strchr(p_cpo, CPO_JOINSP) == NULL
&& (endcurr1 == '?' || endcurr1 == '!'))))
++spaces[t];
}
}
currsize = (int)STRLEN(curr);
sumsize += currsize + spaces[t];
endcurr1 = endcurr2 = NUL;
if (insert_space && currsize > 0)
{
if (has_mbyte)
{
cend = curr + currsize;
MB_PTR_BACK(curr, cend);
endcurr1 = (*mb_ptr2char)(cend);
if (cend > curr)
{
MB_PTR_BACK(curr, cend);
endcurr2 = (*mb_ptr2char)(cend);
}
}
else
{
endcurr1 = *(curr + currsize - 1);
if (currsize > 1)
endcurr2 = *(curr + currsize - 2);
}
}
line_breakcheck();
if (got_int)
{
ret = FAIL;
goto theend;
}
}
/* store the column position before last line */
col = sumsize - currsize - spaces[count - 1];
/* allocate the space for the new line */
newp = alloc(sumsize + 1);
cend = newp + sumsize;
*cend = 0;
#ifdef FEAT_TEXT_PROP
// We need to move properties of the lines that are going to be deleted to
// the new long one.
if (curbuf->b_has_textprop && !text_prop_frozen)
{
// Allocate an array to copy the text properties of joined lines into.
// And another array to store the number of properties in each line.
prop_lines = ALLOC_CLEAR_MULT(textprop_T *, count - 1);
prop_lengths = ALLOC_CLEAR_MULT(int, count - 1);
if (prop_lengths == NULL)
VIM_CLEAR(prop_lines);
}
#endif
/*
* Move affected lines to the new long one.
* This loops backwards over the joined lines, including the original line.
*
* Move marks from each deleted line to the joined line, adjusting the
* column. This is not Vi compatible, but Vi deletes the marks, thus that
* should not really be a problem.
*/
for (t = count - 1; ; --t)
{
int spaces_removed;
cend -= currsize;
mch_memmove(cend, curr, (size_t)currsize);
if (spaces[t] > 0)
{
cend -= spaces[t];
vim_memset(cend, ' ', (size_t)(spaces[t]));
}
// If deleting more spaces than adding, the cursor moves no more than
// what is added if it is inside these spaces.
spaces_removed = (curr - curr_start) - spaces[t];
mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
(long)(cend - newp - spaces_removed), spaces_removed);
if (t == 0)
break;
#ifdef FEAT_TEXT_PROP
if (prop_lines != NULL)
adjust_props_for_join(curwin->w_cursor.lnum + t,
prop_lines + t - 1, prop_lengths + t - 1,
(long)(cend - newp - spaces_removed), spaces_removed);
#endif
curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
#if defined(FEAT_COMMENTS)
if (remove_comments)
curr += comments[t - 1];
#endif
if (insert_space && t > 1)
curr = skipwhite(curr);
currsize = (int)STRLEN(curr);
}
#ifdef FEAT_TEXT_PROP
if (prop_lines != NULL)
join_prop_lines(curwin->w_cursor.lnum, newp,
prop_lines, prop_lengths, count);
else
#endif
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
if (setmark)
{
/* Set the '] mark. */
curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
}
/* Only report the change in the first line here, del_lines() will report
* the deleted line. */
changed_lines(curwin->w_cursor.lnum, currsize,
curwin->w_cursor.lnum + 1, 0L);
/*
* Delete following lines. To do this we move the cursor there
* briefly, and then move it back. After del_lines() the cursor may
* have moved up (last line deleted), so the current lnum is kept in t.
*/
t = curwin->w_cursor.lnum;
++curwin->w_cursor.lnum;
del_lines(count - 1, FALSE);
curwin->w_cursor.lnum = t;
/*
* Set the cursor column:
* Vi compatible: use the column of the first join
* vim: use the column of the last join
*/
curwin->w_cursor.col =
(vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
check_cursor_col();
curwin->w_cursor.coladd = 0;
curwin->w_set_curswant = TRUE;
theend:
vim_free(spaces);
#if defined(FEAT_COMMENTS) || defined(PROTO)
if (remove_comments)
vim_free(comments);
#endif
return ret;
}
#ifdef FEAT_COMMENTS
/*
* Return TRUE if the two comment leaders given are the same. "lnum" is
* the first line. White-space is ignored. Note that the whole of
* 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
*/
static int
same_leader(
linenr_T lnum,
int leader1_len,
char_u *leader1_flags,
int leader2_len,
char_u *leader2_flags)
{
int idx1 = 0, idx2 = 0;
char_u *p;
char_u *line1;
char_u *line2;
if (leader1_len == 0)
return (leader2_len == 0);
/*
* If first leader has 'f' flag, the lines can be joined only if the
* second line does not have a leader.
* If first leader has 'e' flag, the lines can never be joined.
* If fist leader has 's' flag, the lines can only be joined if there is
* some text after it and the second line has the 'm' flag.
*/
if (leader1_flags != NULL)
{
for (p = leader1_flags; *p && *p != ':'; ++p)
{
if (*p == COM_FIRST)
return (leader2_len == 0);
if (*p == COM_END)
return FALSE;
if (*p == COM_START)
{
if (*(ml_get(lnum) + leader1_len) == NUL)
return FALSE;
if (leader2_flags == NULL || leader2_len == 0)
return FALSE;
for (p = leader2_flags; *p && *p != ':'; ++p)
if (*p == COM_MIDDLE)
return TRUE;
return FALSE;
}
}
}
/*
* Get current line and next line, compare the leaders.
* The first line has to be saved, only one line can be locked at a time.
*/
line1 = vim_strsave(ml_get(lnum));
if (line1 != NULL)
{
for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
;
line2 = ml_get(lnum + 1);
for (idx2 = 0; idx2 < leader2_len; ++idx2)
{
if (!VIM_ISWHITE(line2[idx2]))
{
if (line1[idx1++] != line2[idx2])
break;
}
else
while (VIM_ISWHITE(line1[idx1]))
++idx1;
}
vim_free(line1);
}
return (idx2 == leader2_len && idx1 == leader1_len);
}
#endif
/*
* Implementation of the format operator 'gq'.
*/
void
op_format(
oparg_T *oap,
int keep_cursor) /* keep cursor on same text char */
{
long old_line_count = curbuf->b_ml.ml_line_count;
/* Place the cursor where the "gq" or "gw" command was given, so that "u"
* can put it back there. */
curwin->w_cursor = oap->cursor_start;
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return;
curwin->w_cursor = oap->start;
if (oap->is_VIsual)
/* When there is no change: need to remove the Visual selection */
redraw_curbuf_later(INVERTED);
/* Set '[ mark at the start of the formatted area */
curbuf->b_op_start = oap->start;
/* For "gw" remember the cursor position and put it back below (adjusted
* for joined and split lines). */
if (keep_cursor)
saved_cursor = oap->cursor_start;
format_lines(oap->line_count, keep_cursor);
/*
* Leave the cursor at the first non-blank of the last formatted line.
* If the cursor was moved one line back (e.g. with "Q}") go to the next
* line, so "." will do the next lines.
*/
if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
++curwin->w_cursor.lnum;
beginline(BL_WHITE | BL_FIX);
old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
msgmore(old_line_count);
/* put '] mark on the end of the formatted area */
curbuf->b_op_end = curwin->w_cursor;
if (keep_cursor)
{
curwin->w_cursor = saved_cursor;
saved_cursor.lnum = 0;
}
if (oap->is_VIsual)
{
win_T *wp;
FOR_ALL_WINDOWS(wp)
{
if (wp->w_old_cursor_lnum != 0)
{
/* When lines have been inserted or deleted, adjust the end of
* the Visual area to be redrawn. */
if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
wp->w_old_cursor_lnum += old_line_count;
else
wp->w_old_visual_lnum += old_line_count;
}
}
}
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Implementation of the format operator 'gq' for when using 'formatexpr'.
*/
void
op_formatexpr(oparg_T *oap)
{
if (oap->is_VIsual)
/* When there is no change: need to remove the Visual selection */
redraw_curbuf_later(INVERTED);
if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
/* As documented: when 'formatexpr' returns non-zero fall back to
* internal formatting. */
op_format(oap, FALSE);
}
int
fex_format(
linenr_T lnum,
long count,
int c) /* character to be inserted */
{
int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
OPT_LOCAL);
int r;
char_u *fex;
/*
* Set v:lnum to the first line number and v:count to the number of lines.
* Set v:char to the character to be inserted (can be NUL).
*/
set_vim_var_nr(VV_LNUM, lnum);
set_vim_var_nr(VV_COUNT, count);
set_vim_var_char(c);
/* Make a copy, the option could be changed while calling it. */
fex = vim_strsave(curbuf->b_p_fex);
if (fex == NULL)
return 0;
/*
* Evaluate the function.
*/
if (use_sandbox)
++sandbox;
r = (int)eval_to_number(fex);
if (use_sandbox)
--sandbox;
set_vim_var_string(VV_CHAR, NULL, -1);
vim_free(fex);
return r;
}
#endif
/*
* Format "line_count" lines, starting at the cursor position.
* When "line_count" is negative, format until the end of the paragraph.
* Lines after the cursor line are saved for undo, caller must have saved the
* first line.
*/
void
format_lines(
linenr_T line_count,
int avoid_fex) /* don't use 'formatexpr' */
{
int max_len;
int is_not_par; /* current line not part of parag. */
int next_is_not_par; /* next line not part of paragraph */
int is_end_par; /* at end of paragraph */
int prev_is_end_par = FALSE;/* prev. line not part of parag. */
int next_is_start_par = FALSE;
#ifdef FEAT_COMMENTS
int leader_len = 0; /* leader len of current line */
int next_leader_len; /* leader len of next line */
char_u *leader_flags = NULL; /* flags for leader of current line */
char_u *next_leader_flags; /* flags for leader of next line */
int do_comments; /* format comments */
int do_comments_list = 0; /* format comments with 'n' or '2' */
#endif
int advance = TRUE;
int second_indent = -1; /* indent for second line (comment
* aware) */
int do_second_indent;
int do_number_indent;
int do_trail_white;
int first_par_line = TRUE;
int smd_save;
long count;
int need_set_indent = TRUE; /* set indent of next paragraph */
int force_format = FALSE;
int old_State = State;
/* length of a line to force formatting: 3 * 'tw' */
max_len = comp_textwidth(TRUE) * 3;
/* check for 'q', '2' and '1' in 'formatoptions' */
#ifdef FEAT_COMMENTS
do_comments = has_format_option(FO_Q_COMS);
#endif
do_second_indent = has_format_option(FO_Q_SECOND);
do_number_indent = has_format_option(FO_Q_NUMBER);
do_trail_white = has_format_option(FO_WHITE_PAR);
/*
* Get info about the previous and current line.
*/
if (curwin->w_cursor.lnum > 1)
is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
#ifdef FEAT_COMMENTS
, &leader_len, &leader_flags, do_comments
#endif
);
else
is_not_par = TRUE;
next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
#ifdef FEAT_COMMENTS
, &next_leader_len, &next_leader_flags, do_comments
#endif
);
is_end_par = (is_not_par || next_is_not_par);
if (!is_end_par && do_trail_white)
is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
curwin->w_cursor.lnum--;
for (count = line_count; count != 0 && !got_int; --count)
{
/*
* Advance to next paragraph.
*/
if (advance)
{
curwin->w_cursor.lnum++;
prev_is_end_par = is_end_par;
is_not_par = next_is_not_par;
#ifdef FEAT_COMMENTS
leader_len = next_leader_len;
leader_flags = next_leader_flags;
#endif
}
/*
* The last line to be formatted.
*/
if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
{
next_is_not_par = TRUE;
#ifdef FEAT_COMMENTS
next_leader_len = 0;
next_leader_flags = NULL;
#endif
}
else
{
next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
#ifdef FEAT_COMMENTS
, &next_leader_len, &next_leader_flags, do_comments
#endif
);
if (do_number_indent)
next_is_start_par =
(get_number_indent(curwin->w_cursor.lnum + 1) > 0);
}
advance = TRUE;
is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
if (!is_end_par && do_trail_white)
is_end_par = !ends_in_white(curwin->w_cursor.lnum);
/*
* Skip lines that are not in a paragraph.
*/
if (is_not_par)
{
if (line_count < 0)
break;
}
else
{
/*
* For the first line of a paragraph, check indent of second line.
* Don't do this for comments and empty lines.
*/
if (first_par_line
&& (do_second_indent || do_number_indent)
&& prev_is_end_par
&& curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
{
if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
{
#ifdef FEAT_COMMENTS
if (leader_len == 0 && next_leader_len == 0)
{
/* no comment found */
#endif
second_indent =
get_indent_lnum(curwin->w_cursor.lnum + 1);
#ifdef FEAT_COMMENTS
}
else
{
second_indent = next_leader_len;
do_comments_list = 1;
}
#endif
}
else if (do_number_indent)
{
#ifdef FEAT_COMMENTS
if (leader_len == 0 && next_leader_len == 0)
{
/* no comment found */
#endif
second_indent =
get_number_indent(curwin->w_cursor.lnum);
#ifdef FEAT_COMMENTS
}
else
{
/* get_number_indent() is now "comment aware"... */
second_indent =
get_number_indent(curwin->w_cursor.lnum);
do_comments_list = 1;
}
#endif
}
}
/*
* When the comment leader changes, it's the end of the paragraph.
*/
if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
#ifdef FEAT_COMMENTS
|| !same_leader(curwin->w_cursor.lnum,
leader_len, leader_flags,
next_leader_len, next_leader_flags)
#endif
)
is_end_par = TRUE;
/*
* If we have got to the end of a paragraph, or the line is
* getting long, format it.
*/
if (is_end_par || force_format)
{
if (need_set_indent)
/* replace indent in first line with minimal number of
* tabs and spaces, according to current options */
(void)set_indent(get_indent(), SIN_CHANGED);
/* put cursor on last non-space */
State = NORMAL; /* don't go past end-of-line */
coladvance((colnr_T)MAXCOL);
while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
dec_cursor();
/* do the formatting, without 'showmode' */
State = INSERT; /* for open_line() */
smd_save = p_smd;
p_smd = FALSE;
insertchar(NUL, INSCHAR_FORMAT
#ifdef FEAT_COMMENTS
+ (do_comments ? INSCHAR_DO_COM : 0)
+ (do_comments && do_comments_list
? INSCHAR_COM_LIST : 0)
#endif
+ (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
State = old_State;
p_smd = smd_save;
second_indent = -1;
/* at end of par.: need to set indent of next par. */
need_set_indent = is_end_par;
if (is_end_par)
{
/* When called with a negative line count, break at the
* end of the paragraph. */
if (line_count < 0)
break;
first_par_line = TRUE;
}
force_format = FALSE;
}
/*
* When still in same paragraph, join the lines together. But
* first delete the leader from the second line.
*/
if (!is_end_par)
{
advance = FALSE;
curwin->w_cursor.lnum++;
curwin->w_cursor.col = 0;
if (line_count < 0 && u_save_cursor() == FAIL)
break;
#ifdef FEAT_COMMENTS
if (next_leader_len > 0)
{
(void)del_bytes((long)next_leader_len, FALSE, FALSE);
mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
(long)-next_leader_len, 0);
} else
#endif
if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
{
int indent = getwhitecols_curline();
if (indent > 0)
{
(void)del_bytes(indent, FALSE, FALSE);
mark_col_adjust(curwin->w_cursor.lnum,
(colnr_T)0, 0L, (long)-indent, 0);
}
}
curwin->w_cursor.lnum--;
if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
{
beep_flush();
break;
}
first_par_line = FALSE;
/* If the line is getting long, format it next time */
if (STRLEN(ml_get_curline()) > (size_t)max_len)
force_format = TRUE;
else
force_format = FALSE;
}
}
line_breakcheck();
}
}
/*
* Return TRUE if line "lnum" ends in a white character.
*/
static int
ends_in_white(linenr_T lnum)
{
char_u *s = ml_get(lnum);
size_t l;
if (*s == NUL)
return FALSE;
/* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
* invocation may call function multiple times". */
l = STRLEN(s) - 1;
return VIM_ISWHITE(s[l]);
}
/*
* Blank lines, and lines containing only the comment leader, are left
* untouched by the formatting. The function returns TRUE in this
* case. It also returns TRUE when a line starts with the end of a comment
* ('e' in comment flags), so that this line is skipped, and not joined to the
* previous line. A new paragraph starts after a blank line, or when the
* comment leader changes -- webb.
*/
#ifdef FEAT_COMMENTS
static int
fmt_check_par(
linenr_T lnum,
int *leader_len,
char_u **leader_flags,
int do_comments)
{
char_u *flags = NULL; /* init for GCC */
char_u *ptr;
ptr = ml_get(lnum);
if (do_comments)
*leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
else
*leader_len = 0;
if (*leader_len > 0)
{
/*
* Search for 'e' flag in comment leader flags.
*/
flags = *leader_flags;
while (*flags && *flags != ':' && *flags != COM_END)
++flags;
}
return (*skipwhite(ptr + *leader_len) == NUL
|| (*leader_len > 0 && *flags == COM_END)
|| startPS(lnum, NUL, FALSE));
}
#else
static int
fmt_check_par(linenr_T lnum)
{
return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
}
#endif
/*
* Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
* previous line is in the same paragraph. Used for auto-formatting.
*/
int
paragraph_start(linenr_T lnum)
{
char_u *p;
#ifdef FEAT_COMMENTS
int leader_len = 0; /* leader len of current line */
char_u *leader_flags = NULL; /* flags for leader of current line */
int next_leader_len; /* leader len of next line */
char_u *next_leader_flags; /* flags for leader of next line */
int do_comments; /* format comments */
#endif
if (lnum <= 1)
return TRUE; /* start of the file */
p = ml_get(lnum - 1);
if (*p == NUL)
return TRUE; /* after empty line */
#ifdef FEAT_COMMENTS
do_comments = has_format_option(FO_Q_COMS);
#endif
if (fmt_check_par(lnum - 1
#ifdef FEAT_COMMENTS
, &leader_len, &leader_flags, do_comments
#endif
))
return TRUE; /* after non-paragraph line */
if (fmt_check_par(lnum
#ifdef FEAT_COMMENTS
, &next_leader_len, &next_leader_flags, do_comments
#endif
))
return TRUE; /* "lnum" is not a paragraph line */
if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
return TRUE; /* missing trailing space in previous line. */
if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
return TRUE; /* numbered item starts in "lnum". */
#ifdef FEAT_COMMENTS
if (!same_leader(lnum - 1, leader_len, leader_flags,
next_leader_len, next_leader_flags))
return TRUE; /* change of comment leader. */
#endif
return FALSE;
}
/*
* prepare a few things for block mode yank/delete/tilde
*
* for delete:
* - textlen includes the first/last char to be (partly) deleted
* - start/endspaces is the number of columns that are taken by the
* first/last deleted char minus the number of columns that have to be
* deleted.
* for yank and tilde:
* - textlen includes the first/last char to be wholly yanked
* - start/endspaces is the number of columns of the first/last yanked char
* that are to be yanked.
*/
static void
block_prep(
oparg_T *oap,
struct block_def *bdp,
linenr_T lnum,
int is_del)
{
int incr = 0;
char_u *pend;
char_u *pstart;
char_u *line;
char_u *prev_pstart;
char_u *prev_pend;
bdp->startspaces = 0;
bdp->endspaces = 0;
bdp->textlen = 0;
bdp->start_vcol = 0;
bdp->end_vcol = 0;
bdp->is_short = FALSE;
bdp->is_oneChar = FALSE;
bdp->pre_whitesp = 0;
bdp->pre_whitesp_c = 0;
bdp->end_char_vcols = 0;
bdp->start_char_vcols = 0;
line = ml_get(lnum);
pstart = line;
prev_pstart = line;
while (bdp->start_vcol < oap->start_vcol && *pstart)
{
/* Count a tab for what it's worth (if list mode not on) */
incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
bdp->start_vcol += incr;
if (VIM_ISWHITE(*pstart))
{
bdp->pre_whitesp += incr;
bdp->pre_whitesp_c++;
}
else
{
bdp->pre_whitesp = 0;
bdp->pre_whitesp_c = 0;
}
prev_pstart = pstart;
MB_PTR_ADV(pstart);
}
bdp->start_char_vcols = incr;
if (bdp->start_vcol < oap->start_vcol) /* line too short */
{
bdp->end_vcol = bdp->start_vcol;
bdp->is_short = TRUE;
if (!is_del || oap->op_type == OP_APPEND)
bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
}
else
{
/* notice: this converts partly selected Multibyte characters to
* spaces, too. */
bdp->startspaces = bdp->start_vcol - oap->start_vcol;
if (is_del && bdp->startspaces)
bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
pend = pstart;
bdp->end_vcol = bdp->start_vcol;
if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
{
bdp->is_oneChar = TRUE;
if (oap->op_type == OP_INSERT)
bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
else if (oap->op_type == OP_APPEND)
{
bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
}
else
{
bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
if (is_del && oap->op_type != OP_LSHIFT)
{
/* just putting the sum of those two into
* bdp->startspaces doesn't work for Visual replace,
* so we have to split the tab in two */
bdp->startspaces = bdp->start_char_vcols
- (bdp->start_vcol - oap->start_vcol);
bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
}
}
}
else
{
prev_pend = pend;
while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
{
/* Count a tab for what it's worth (if list mode not on) */
prev_pend = pend;
incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
bdp->end_vcol += incr;
}
if (bdp->end_vcol <= oap->end_vcol
&& (!is_del
|| oap->op_type == OP_APPEND
|| oap->op_type == OP_REPLACE)) /* line too short */
{
bdp->is_short = TRUE;
/* Alternative: include spaces to fill up the block.
* Disadvantage: can lead to trailing spaces when the line is
* short where the text is put */
/* if (!is_del || oap->op_type == OP_APPEND) */
if (oap->op_type == OP_APPEND || virtual_op)
bdp->endspaces = oap->end_vcol - bdp->end_vcol
+ oap->inclusive;
else
bdp->endspaces = 0; /* replace doesn't add characters */
}
else if (bdp->end_vcol > oap->end_vcol)
{
bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
if (!is_del && bdp->endspaces)
{
bdp->endspaces = incr - bdp->endspaces;
if (pend != pstart)
pend = prev_pend;
}
}
}
bdp->end_char_vcols = incr;
if (is_del && bdp->startspaces)
pstart = prev_pstart;
bdp->textlen = (int)(pend - pstart);
}
bdp->textcol = (colnr_T) (pstart - line);
bdp->textstart = pstart;
}
/*
* Handle the add/subtract operator.
*/
void
op_addsub(
oparg_T *oap,
linenr_T Prenum1, /* Amount of add/subtract */
int g_cmd) /* was g<c-a>/g<c-x> */
{
pos_T pos;
struct block_def bd;
int change_cnt = 0;
linenr_T amount = Prenum1;
// do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
// buffer is not completely updated yet. Postpone updating folds until before
// the call to changed_lines().
#ifdef FEAT_FOLDING
disable_fold_update++;
#endif
if (!VIsual_active)
{
pos = curwin->w_cursor;
if (u_save_cursor() == FAIL)
{
#ifdef FEAT_FOLDING
disable_fold_update--;
#endif
return;
}
change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
#ifdef FEAT_FOLDING
disable_fold_update--;
#endif
if (change_cnt)
changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
}
else
{
int one_change;
int length;
pos_T startpos;
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
{
#ifdef FEAT_FOLDING
disable_fold_update--;
#endif
return;
}
pos = oap->start;
for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
{
if (oap->block_mode) /* Visual block mode */
{
block_prep(oap, &bd, pos.lnum, FALSE);
pos.col = bd.textcol;
length = bd.textlen;
}
else if (oap->motion_type == MLINE)
{
curwin->w_cursor.col = 0;
pos.col = 0;
length = (colnr_T)STRLEN(ml_get(pos.lnum));
}
else /* oap->motion_type == MCHAR */
{
if (pos.lnum == oap->start.lnum && !oap->inclusive)
dec(&(oap->end));
length = (colnr_T)STRLEN(ml_get(pos.lnum));
pos.col = 0;
if (pos.lnum == oap->start.lnum)
{
pos.col += oap->start.col;
length -= oap->start.col;
}
if (pos.lnum == oap->end.lnum)
{
length = (int)STRLEN(ml_get(oap->end.lnum));
if (oap->end.col >= length)
oap->end.col = length - 1;
length = oap->end.col - pos.col + 1;
}
}
one_change = do_addsub(oap->op_type, &pos, length, amount);
if (one_change)
{
/* Remember the start position of the first change. */
if (change_cnt == 0)
startpos = curbuf->b_op_start;
++change_cnt;
}
#ifdef FEAT_NETBEANS_INTG
if (netbeans_active() && one_change)
{
char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
netbeans_inserted(curbuf, pos.lnum, pos.col,
&ptr[pos.col], length);
}
#endif
if (g_cmd && one_change)
amount += Prenum1;
}
#ifdef FEAT_FOLDING
disable_fold_update--;
#endif
if (change_cnt)
changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
if (!change_cnt && oap->is_VIsual)
/* No change: need to remove the Visual selection */
redraw_curbuf_later(INVERTED);
/* Set '[ mark if something changed. Keep the last end
* position from do_addsub(). */
if (change_cnt > 0)
curbuf->b_op_start = startpos;
if (change_cnt > p_report)
smsg(NGETTEXT("%ld line changed", "%ld lines changed",
change_cnt), change_cnt);
}
}
/*
* Add or subtract 'Prenum1' from a number in a line
* op_type is OP_NR_ADD or OP_NR_SUB
*
* Returns TRUE if some character was changed.
*/
static int
do_addsub(
int op_type,
pos_T *pos,
int length,
linenr_T Prenum1)
{
int col;
char_u *buf1;
char_u buf2[NUMBUFLEN];
int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
static int hexupper = FALSE; /* 0xABC */
uvarnumber_T n;
uvarnumber_T oldn;
char_u *ptr;
int c;
int todel;
int dohex;
int dooct;
int dobin;
int doalp;
int firstdigit;
int subtract;
int negative = FALSE;
int was_positive = TRUE;
int visual = VIsual_active;
int did_change = FALSE;
pos_T save_cursor = curwin->w_cursor;
int maxlen = 0;
pos_T startpos;
pos_T endpos;
dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
curwin->w_cursor = *pos;
ptr = ml_get(pos->lnum);
col = pos->col;
if (*ptr == NUL)
goto theend;
/*
* First check if we are on a hexadecimal number, after the "0x".
*/
if (!VIsual_active)
{
if (dobin)
while (col > 0 && vim_isbdigit(ptr[col]))
{
--col;
if (has_mbyte)
col -= (*mb_head_off)(ptr, ptr + col);
}
if (dohex)
while (col > 0 && vim_isxdigit(ptr[col]))
{
--col;
if (has_mbyte)
col -= (*mb_head_off)(ptr, ptr + col);
}
if ( dobin
&& dohex
&& ! ((col > 0
&& (ptr[col] == 'X'
|| ptr[col] == 'x')
&& ptr[col - 1] == '0'
&& (!has_mbyte ||
!(*mb_head_off)(ptr, ptr + col - 1))
&& vim_isxdigit(ptr[col + 1]))))
{
/* In case of binary/hexadecimal pattern overlap match, rescan */
col = pos->col;
while (col > 0 && vim_isdigit(ptr[col]))
{
col--;
if (has_mbyte)
col -= (*mb_head_off)(ptr, ptr + col);
}
}
if (( dohex
&& col > 0
&& (ptr[col] == 'X'
|| ptr[col] == 'x')
&& ptr[col - 1] == '0'
&& (!has_mbyte ||
!(*mb_head_off)(ptr, ptr + col - 1))
&& vim_isxdigit(ptr[col + 1])) ||
( dobin
&& col > 0
&& (ptr[col] == 'B'
|| ptr[col] == 'b')
&& ptr[col - 1] == '0'
&& (!has_mbyte ||
!(*mb_head_off)(ptr, ptr + col - 1))
&& vim_isbdigit(ptr[col + 1])))
{
/* Found hexadecimal or binary number, move to its start. */
--col;
if (has_mbyte)
col -= (*mb_head_off)(ptr, ptr + col);
}
else
{
/*
* Search forward and then backward to find the start of number.
*/
col = pos->col;
while (ptr[col] != NUL
&& !vim_isdigit(ptr[col])
&& !(doalp && ASCII_ISALPHA(ptr[col])))
col += MB_PTR2LEN(ptr + col);
while (col > 0
&& vim_isdigit(ptr[col - 1])
&& !(doalp && ASCII_ISALPHA(ptr[col])))
{
--col;
if (has_mbyte)
col -= (*mb_head_off)(ptr, ptr + col);
}
}
}
if (visual)
{
while (ptr[col] != NUL && length > 0
&& !vim_isdigit(ptr[col])
&& !(doalp && ASCII_ISALPHA(ptr[col])))
{
int mb_len = MB_PTR2LEN(ptr + col);
col += mb_len;
length -= mb_len;
}
if (length == 0)
goto theend;
if (col > pos->col && ptr[col - 1] == '-'
&& (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1)))
{
negative = TRUE;
was_positive = FALSE;
}
}
/*
* If a number was found, and saving for undo works, replace the number.
*/
firstdigit = ptr[col];
if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
{
beep_flush();
goto theend;
}
if (doalp && ASCII_ISALPHA(firstdigit))
{
/* decrement or increment alphabetic character */
if (op_type == OP_NR_SUB)
{
if (CharOrd(firstdigit) < Prenum1)
{
if (isupper(firstdigit))
firstdigit = 'A';
else
firstdigit = 'a';
}
else
#ifdef EBCDIC
firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
#else
firstdigit -= Prenum1;
#endif
}
else
{
if (26 - CharOrd(firstdigit) - 1 < Prenum1)
{
if (isupper(firstdigit))
firstdigit = 'Z';
else
firstdigit = 'z';
}
else
#ifdef EBCDIC
firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
#else
firstdigit += Prenum1;
#endif
}
curwin->w_cursor.col = col;
if (!did_change)
startpos = curwin->w_cursor;
did_change = TRUE;
(void)del_char(FALSE);
ins_char(firstdigit);
endpos = curwin->w_cursor;
curwin->w_cursor.col = col;
}
else
{
if (col > 0 && ptr[col - 1] == '-'
&& (!has_mbyte ||
!(*mb_head_off)(ptr, ptr + col - 1))
&& !visual)
{
/* negative number */
--col;
negative = TRUE;
}
/* get the number value (unsigned) */
if (visual && VIsual_mode != 'V')
maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
? (int)STRLEN(ptr) - col
: length);
vim_str2nr(ptr + col, &pre, &length,
0 + (dobin ? STR2NR_BIN : 0)
+ (dooct ? STR2NR_OCT : 0)
+ (dohex ? STR2NR_HEX : 0),
NULL, &n, maxlen, FALSE);
/* ignore leading '-' for hex and octal and bin numbers */
if (pre && negative)
{
++col;
--length;
negative = FALSE;
}
/* add or subtract */
subtract = FALSE;
if (op_type == OP_NR_SUB)
subtract ^= TRUE;
if (negative)
subtract ^= TRUE;
oldn = n;
if (subtract)
n -= (uvarnumber_T)Prenum1;
else
n += (uvarnumber_T)Prenum1;
/* handle wraparound for decimal numbers */
if (!pre)
{
if (subtract)
{
if (n > oldn)
{
n = 1 + (n ^ (uvarnumber_T)-1);
negative ^= TRUE;
}
}
else
{
/* add */
if (n < oldn)
{
n = (n ^ (uvarnumber_T)-1);
negative ^= TRUE;
}
}
if (n == 0)
negative = FALSE;
}
if (visual && !was_positive && !negative && col > 0)
{
/* need to remove the '-' */
col--;
length++;
}
/*
* Delete the old number.
*/
curwin->w_cursor.col = col;
if (!did_change)
startpos = curwin->w_cursor;
did_change = TRUE;
todel = length;
c = gchar_cursor();
/*
* Don't include the '-' in the length, only the length of the
* part after it is kept the same.
*/
if (c == '-')
--length;
while (todel-- > 0)
{
if (c < 0x100 && isalpha(c))
{
if (isupper(c))
hexupper = TRUE;
else
hexupper = FALSE;
}
/* del_char() will mark line needing displaying */
(void)del_char(FALSE);
c = gchar_cursor();
}
/*
* Prepare the leading characters in buf1[].
* When there are many leading zeros it could be very long.
* Allocate a bit too much.
*/
buf1 = alloc(length + NUMBUFLEN);
if (buf1 == NULL)
goto theend;
ptr = buf1;
if (negative && (!visual || was_positive))
*ptr++ = '-';
if (pre)
{
*ptr++ = '0';
--length;
}
if (pre == 'b' || pre == 'B' ||
pre == 'x' || pre == 'X')
{
*ptr++ = pre;
--length;
}
/*
* Put the number characters in buf2[].
*/
if (pre == 'b' || pre == 'B')
{
int i;
int bit = 0;
int bits = sizeof(uvarnumber_T) * 8;
/* leading zeros */
for (bit = bits; bit > 0; bit--)
if ((n >> (bit - 1)) & 0x1) break;
for (i = 0; bit > 0; bit--)
buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
buf2[i] = '\0';
}
else if (pre == 0)
vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
(long_long_u_T)n);
else if (pre == '0')
vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
(long_long_u_T)n);
else if (pre && hexupper)
vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
(long_long_u_T)n);
else
vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
(long_long_u_T)n);
length -= (int)STRLEN(buf2);
/*
* Adjust number of zeros to the new number of digits, so the
* total length of the number remains the same.
* Don't do this when
* the result may look like an octal number.
*/
if (firstdigit == '0' && !(dooct && pre == 0))
while (length-- > 0)
*ptr++ = '0';
*ptr = NUL;
STRCAT(buf1, buf2);
ins_str(buf1); /* insert the new number */
vim_free(buf1);
endpos = curwin->w_cursor;
if (did_change && curwin->w_cursor.col)
--curwin->w_cursor.col;
}
if (did_change)
{
/* set the '[ and '] marks */
curbuf->b_op_start = startpos;
curbuf->b_op_end = endpos;
if (curbuf->b_op_end.col > 0)
--curbuf->b_op_end.col;
}
theend:
if (visual)
curwin->w_cursor = save_cursor;
else if (did_change)
curwin->w_set_curswant = TRUE;
return did_change;
}
#if defined(FEAT_CLIPBOARD) || defined(PROTO)
/*
* SELECTION / PRIMARY ('*')
*
* Text selection stuff that uses the GUI selection register '*'. When using a
* GUI this may be text from another window, otherwise it is the last text we
* had highlighted with VIsual mode. With mouse support, clicking the middle
* button performs the paste, otherwise you will need to do <"*p>. "
* If not under X, it is synonymous with the clipboard register '+'.
*
* X CLIPBOARD ('+')
*
* Text selection stuff that uses the GUI clipboard register '+'.
* Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
* It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
* otherwise you will need to do <"+p>. "
* If not under X, it is synonymous with the selection register '*'.
*/
/*
* Routine to export any final X selection we had to the environment
* so that the text is still available after Vim has exited. X selections
* only exist while the owning application exists, so we write to the
* permanent (while X runs) store CUT_BUFFER0.
* Dump the CLIPBOARD selection if we own it (it's logically the more
* 'permanent' of the two), otherwise the PRIMARY one.
* For now, use a hard-coded sanity limit of 1Mb of data.
*/
#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
void
x11_export_final_selection(void)
{
Display *dpy;
char_u *str = NULL;
long_u len = 0;
int motion_type = -1;
# ifdef FEAT_GUI
if (gui.in_use)
dpy = X_DISPLAY;
else
# endif
# ifdef FEAT_XCLIPBOARD
dpy = xterm_dpy;
# else
return;
# endif
/* Get selection to export */
if (clip_plus.owned)
motion_type = clip_convert_selection(&str, &len, &clip_plus);
else if (clip_star.owned)
motion_type = clip_convert_selection(&str, &len, &clip_star);
/* Check it's OK */
if (dpy != NULL && str != NULL && motion_type >= 0
&& len < 1024*1024 && len > 0)
{
int ok = TRUE;
/* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
* 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
* encoding conversion usually doesn't work, so keep the text as-is.
*/
if (has_mbyte)
{
vimconv_T vc;
vc.vc_type = CONV_NONE;
if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
{
int intlen = len;
char_u *conv_str;
vc.vc_fail = TRUE;
conv_str = string_convert(&vc, str, &intlen);
len = intlen;
if (conv_str != NULL)
{
vim_free(str);
str = conv_str;
}
else
{
ok = FALSE;
}
convert_setup(&vc, NULL, NULL);
}
else
{
ok = FALSE;
}
}
/* Do not store the string if conversion failed. Better to use any
* other selection than garbled text. */
if (ok)
{
XStoreBuffer(dpy, (char *)str, (int)len, 0);
XFlush(dpy);
}
}
vim_free(str);
}
#endif
void
clip_free_selection(Clipboard_T *cbd)
{
yankreg_T *y_ptr = y_current;
if (cbd == &clip_plus)
y_current = &y_regs[PLUS_REGISTER];
else
y_current = &y_regs[STAR_REGISTER];
free_yank_all();
y_current->y_size = 0;
y_current = y_ptr;
}
/*
* Get the selected text and put it in register '*' or '+'.
*/
void
clip_get_selection(Clipboard_T *cbd)
{
yankreg_T *old_y_previous, *old_y_current;
pos_T old_cursor;
pos_T old_visual;
int old_visual_mode;
colnr_T old_curswant;
int old_set_curswant;
pos_T old_op_start, old_op_end;
oparg_T oa;
cmdarg_T ca;
if (cbd->owned)
{
if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
|| (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
return;
/* Get the text between clip_star.start & clip_star.end */
old_y_previous = y_previous;
old_y_current = y_current;
old_cursor = curwin->w_cursor;
old_curswant = curwin->w_curswant;
old_set_curswant = curwin->w_set_curswant;
old_op_start = curbuf->b_op_start;
old_op_end = curbuf->b_op_end;
old_visual = VIsual;
old_visual_mode = VIsual_mode;
clear_oparg(&oa);
oa.regname = (cbd == &clip_plus ? '+' : '*');
oa.op_type = OP_YANK;
vim_memset(&ca, 0, sizeof(ca));
ca.oap = &oa;
ca.cmdchar = 'y';
ca.count1 = 1;
ca.retval = CA_NO_ADJ_OP_END;
do_pending_operator(&ca, 0, TRUE);
y_previous = old_y_previous;
y_current = old_y_current;
curwin->w_cursor = old_cursor;
changed_cline_bef_curs(); /* need to update w_virtcol et al */
curwin->w_curswant = old_curswant;
curwin->w_set_curswant = old_set_curswant;
curbuf->b_op_start = old_op_start;
curbuf->b_op_end = old_op_end;
VIsual = old_visual;
VIsual_mode = old_visual_mode;
}
else if (!is_clipboard_needs_update())
{
clip_free_selection(cbd);
/* Try to get selected text from another window */
clip_gen_request_selection(cbd);
}
}
/*
* Convert from the GUI selection string into the '*'/'+' register.
*/
void
clip_yank_selection(
int type,
char_u *str,
long len,
Clipboard_T *cbd)
{
yankreg_T *y_ptr;
if (cbd == &clip_plus)
y_ptr = &y_regs[PLUS_REGISTER];
else
y_ptr = &y_regs[STAR_REGISTER];
clip_free_selection(cbd);
str_to_reg(y_ptr, type, str, len, 0L, FALSE);
}
/*
* Convert the '*'/'+' register into a GUI selection string returned in *str
* with length *len.
* Returns the motion type, or -1 for failure.
*/
int
clip_convert_selection(char_u **str, long_u *len, Clipboard_T *cbd)
{
char_u *p;
int lnum;
int i, j;
int_u eolsize;
yankreg_T *y_ptr;
if (cbd == &clip_plus)
y_ptr = &y_regs[PLUS_REGISTER];
else
y_ptr = &y_regs[STAR_REGISTER];
#ifdef USE_CRNL
eolsize = 2;
#else
eolsize = 1;
#endif
*str = NULL;
*len = 0;
if (y_ptr->y_array == NULL)
return -1;
for (i = 0; i < y_ptr->y_size; i++)
*len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
/*
* Don't want newline character at end of last line if we're in MCHAR mode.
*/
if (y_ptr->y_type == MCHAR && *len >= eolsize)
*len -= eolsize;
p = *str = alloc(*len + 1); // add one to avoid zero
if (p == NULL)
return -1;
lnum = 0;
for (i = 0, j = 0; i < (int)*len; i++, j++)
{
if (y_ptr->y_array[lnum][j] == '\n')
p[i] = NUL;
else if (y_ptr->y_array[lnum][j] == NUL)
{
#ifdef USE_CRNL
p[i++] = '\r';
#endif
p[i] = '\n';
lnum++;
j = -1;
}
else
p[i] = y_ptr->y_array[lnum][j];
}
return y_ptr->y_type;
}
/*
* If we have written to a clipboard register, send the text to the clipboard.
*/
static void
may_set_selection(void)
{
if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
{
clip_own_selection(&clip_star);
clip_gen_set_selection(&clip_star);
}
else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
{
clip_own_selection(&clip_plus);
clip_gen_set_selection(&clip_plus);
}
}
#endif /* FEAT_CLIPBOARD || PROTO */
#if defined(FEAT_DND) || defined(PROTO)
/*
* Replace the contents of the '~' register with str.
*/
void
dnd_yank_drag_data(char_u *str, long len)
{
yankreg_T *curr;
curr = y_current;
y_current = &y_regs[TILDE_REGISTER];
free_yank_all();
str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
y_current = curr;
}
#endif
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Return the type of a register.
* Used for getregtype()
* Returns MAUTO for error.
*/
char_u
get_reg_type(int regname, long *reglen)
{
switch (regname)
{
case '%': /* file name */
case '#': /* alternate file name */
case '=': /* expression */
case ':': /* last command line */
case '/': /* last search-pattern */
case '.': /* last inserted text */
#ifdef FEAT_SEARCHPATH
case Ctrl_F: /* Filename under cursor */
case Ctrl_P: /* Path under cursor, expand via "path" */
#endif
case Ctrl_W: /* word under cursor */
case Ctrl_A: /* WORD (mnemonic All) under cursor */
case '_': /* black hole: always empty */
return MCHAR;
}
#ifdef FEAT_CLIPBOARD
regname = may_get_selection(regname);
#endif
if (regname != NUL && !valid_yank_reg(regname, FALSE))
return MAUTO;
get_yank_register(regname, FALSE);
if (y_current->y_array != NULL)
{
if (reglen != NULL && y_current->y_type == MBLOCK)
*reglen = y_current->y_width;
return y_current->y_type;
}
return MAUTO;
}
/*
* When "flags" has GREG_LIST return a list with text "s".
* Otherwise just return "s".
*/
static char_u *
getreg_wrap_one_line(char_u *s, int flags)
{
if (flags & GREG_LIST)
{
list_T *list = list_alloc();
if (list != NULL)
{
if (list_append_string(list, NULL, -1) == FAIL)
{
list_free(list);
return NULL;
}
list->lv_first->li_tv.vval.v_string = s;
}
return (char_u *)list;
}
return s;
}
/*
* Return the contents of a register as a single allocated string.
* Used for "@r" in expressions and for getreg().
* Returns NULL for error.
* Flags:
* GREG_NO_EXPR Do not allow expression register
* GREG_EXPR_SRC For the expression register: return expression itself,
* not the result of its evaluation.
* GREG_LIST Return a list of lines in place of a single string.
*/
char_u *
get_reg_contents(int regname, int flags)
{
long i;
char_u *retval;
int allocated;
long len;
/* Don't allow using an expression register inside an expression */
if (regname == '=')
{
if (flags & GREG_NO_EXPR)
return NULL;
if (flags & GREG_EXPR_SRC)
return getreg_wrap_one_line(get_expr_line_src(), flags);
return getreg_wrap_one_line(get_expr_line(), flags);
}
if (regname == '@') /* "@@" is used for unnamed register */
regname = '"';
/* check for valid regname */
if (regname != NUL && !valid_yank_reg(regname, FALSE))
return NULL;
#ifdef FEAT_CLIPBOARD
regname = may_get_selection(regname);
#endif
if (get_spec_reg(regname, &retval, &allocated, FALSE))
{
if (retval == NULL)
return NULL;
if (allocated)
return getreg_wrap_one_line(retval, flags);
return getreg_wrap_one_line(vim_strsave(retval), flags);
}
get_yank_register(regname, FALSE);
if (y_current->y_array == NULL)
return NULL;
if (flags & GREG_LIST)
{
list_T *list = list_alloc();
int error = FALSE;
if (list == NULL)
return NULL;
for (i = 0; i < y_current->y_size; ++i)
if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
error = TRUE;
if (error)
{
list_free(list);
return NULL;
}
return (char_u *)list;
}
/*
* Compute length of resulting string.
*/
len = 0;
for (i = 0; i < y_current->y_size; ++i)
{
len += (long)STRLEN(y_current->y_array[i]);
/*
* Insert a newline between lines and after last line if
* y_type is MLINE.
*/
if (y_current->y_type == MLINE || i < y_current->y_size - 1)
++len;
}
retval = alloc(len + 1);
/*
* Copy the lines of the yank register into the string.
*/
if (retval != NULL)
{
len = 0;
for (i = 0; i < y_current->y_size; ++i)
{
STRCPY(retval + len, y_current->y_array[i]);
len += (long)STRLEN(retval + len);
/*
* Insert a NL between lines and after the last line if y_type is
* MLINE.
*/
if (y_current->y_type == MLINE || i < y_current->y_size - 1)
retval[len++] = '\n';
}
retval[len] = NUL;
}
return retval;
}
static int
init_write_reg(
int name,
yankreg_T **old_y_previous,
yankreg_T **old_y_current,
int must_append,
int *yank_type UNUSED)
{
if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
{
emsg_invreg(name);
return FAIL;
}
/* Don't want to change the current (unnamed) register */
*old_y_previous = y_previous;
*old_y_current = y_current;
get_yank_register(name, TRUE);
if (!y_append && !must_append)
free_yank_all();
return OK;
}
static void
finish_write_reg(
int name,
yankreg_T *old_y_previous,
yankreg_T *old_y_current)
{
# ifdef FEAT_CLIPBOARD
/* Send text of clipboard register to the clipboard. */
may_set_selection();
# endif
/* ':let @" = "val"' should change the meaning of the "" register */
if (name != '"')
y_previous = old_y_previous;
y_current = old_y_current;
}
/*
* Store string "str" in register "name".
* "maxlen" is the maximum number of bytes to use, -1 for all bytes.
* If "must_append" is TRUE, always append to the register. Otherwise append
* if "name" is an uppercase letter.
* Note: "maxlen" and "must_append" don't work for the "/" register.
* Careful: 'str' is modified, you may have to use a copy!
* If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
*/
void
write_reg_contents(
int name,
char_u *str,
int maxlen,
int must_append)
{
write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
}
void
write_reg_contents_lst(
int name,
char_u **strings,
int maxlen UNUSED,
int must_append,
int yank_type,
long block_len)
{
yankreg_T *old_y_previous, *old_y_current;
if (name == '/'
#ifdef FEAT_EVAL
|| name == '='
#endif
)
{
char_u *s;
if (strings[0] == NULL)
s = (char_u *)"";
else if (strings[1] != NULL)
{
emsg(_("E883: search pattern and expression register may not "
"contain two or more lines"));
return;
}
else
s = strings[0];
write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
return;
}
if (name == '_') /* black hole: nothing to do */
return;
if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
&yank_type) == FAIL)
return;
str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
finish_write_reg(name, old_y_previous, old_y_current);
}
void
write_reg_contents_ex(
int name,
char_u *str,
int maxlen,
int must_append,
int yank_type,
long block_len)
{
yankreg_T *old_y_previous, *old_y_current;
long len;
if (maxlen >= 0)
len = maxlen;
else
len = (long)STRLEN(str);
/* Special case: '/' search pattern */
if (name == '/')
{
set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
return;
}
if (name == '#')
{
buf_T *buf;
if (VIM_ISDIGIT(*str))
{
int num = atoi((char *)str);
buf = buflist_findnr(num);
if (buf == NULL)
semsg(_(e_nobufnr), (long)num);
}
else
buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
TRUE, FALSE, FALSE));
if (buf == NULL)
return;
curwin->w_alt_fnum = buf->b_fnum;
return;
}
#ifdef FEAT_EVAL
if (name == '=')
{
char_u *p, *s;
p = vim_strnsave(str, (int)len);
if (p == NULL)
return;
if (must_append)
{
s = concat_str(get_expr_line_src(), p);
vim_free(p);
p = s;
}
set_expr_line(p);
return;
}
#endif
if (name == '_') /* black hole: nothing to do */
return;
if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
&yank_type) == FAIL)
return;
str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
finish_write_reg(name, old_y_previous, old_y_current);
}
#endif /* FEAT_EVAL */
#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
/*
* Put a string into a register. When the register is not empty, the string
* is appended.
*/
static void
str_to_reg(
yankreg_T *y_ptr, /* pointer to yank register */
int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
char_u *str, /* string to put in register */
long len, /* length of string */
long blocklen, /* width of Visual block */
int str_list) /* TRUE if str is char_u ** */
{
int type; /* MCHAR, MLINE or MBLOCK */
int lnum;
long start;
long i;
int extra;
int newlines; /* number of lines added */
int extraline = 0; /* extra line at the end */
int append = FALSE; /* append to last line in register */
char_u *s;
char_u **ss;
char_u **pp;
long maxlen;
if (y_ptr->y_array == NULL) /* NULL means empty register */
y_ptr->y_size = 0;
if (yank_type == MAUTO)
type = ((str_list || (len > 0 && (str[len - 1] == NL
|| str[len - 1] == CAR)))
? MLINE : MCHAR);
else
type = yank_type;
/*
* Count the number of lines within the string
*/
newlines = 0;
if (str_list)
{
for (ss = (char_u **) str; *ss != NULL; ++ss)
++newlines;
}
else
{
for (i = 0; i < len; i++)
if (str[i] == '\n')
++newlines;
if (type == MCHAR || len == 0 || str[len - 1] != '\n')
{
extraline = 1;
++newlines; /* count extra newline at the end */
}
if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
{
append = TRUE;
--newlines; /* uncount newline when appending first line */
}
}
/* Without any lines make the register empty. */
if (y_ptr->y_size + newlines == 0)
{
VIM_CLEAR(y_ptr->y_array);
return;
}
/*
* Allocate an array to hold the pointers to the new register lines.
* If the register was not empty, move the existing lines to the new array.
*/
pp = lalloc_clear((y_ptr->y_size + newlines) * sizeof(char_u *), TRUE);
if (pp == NULL) /* out of memory */
return;
for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
pp[lnum] = y_ptr->y_array[lnum];
vim_free(y_ptr->y_array);
y_ptr->y_array = pp;
maxlen = 0;
/*
* Find the end of each line and save it into the array.
*/
if (str_list)
{
for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
{
i = (long)STRLEN(*ss);
pp[lnum] = vim_strnsave(*ss, i);
if (i > maxlen)
maxlen = i;
}
}
else
{
for (start = 0; start < len + extraline; start += i + 1)
{
for (i = start; i < len; ++i) /* find the end of the line */
if (str[i] == '\n')
break;
i -= start; /* i is now length of line */
if (i > maxlen)
maxlen = i;
if (append)
{
--lnum;
extra = (int)STRLEN(y_ptr->y_array[lnum]);
}
else
extra = 0;
s = alloc(i + extra + 1);
if (s == NULL)
break;
if (extra)
mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
if (append)
vim_free(y_ptr->y_array[lnum]);
if (i)
mch_memmove(s + extra, str + start, (size_t)i);
extra += i;
s[extra] = NUL;
y_ptr->y_array[lnum++] = s;
while (--extra >= 0)
{
if (*s == NUL)
*s = '\n'; /* replace NUL with newline */
++s;
}
append = FALSE; /* only first line is appended */
}
}
y_ptr->y_type = type;
y_ptr->y_size = lnum;
if (type == MBLOCK)
y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
else
y_ptr->y_width = 0;
#ifdef FEAT_VIMINFO
y_ptr->y_time_set = vim_time();
#endif
}
#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
void
clear_oparg(oparg_T *oap)
{
vim_memset(oap, 0, sizeof(oparg_T));
}
/*
* Count the number of bytes, characters and "words" in a line.
*
* "Words" are counted by looking for boundaries between non-space and
* space characters. (it seems to produce results that match 'wc'.)
*
* Return value is byte count; word count for the line is added to "*wc".
* Char count is added to "*cc".
*
* The function will only examine the first "limit" characters in the
* line, stopping if it encounters an end-of-line (NUL byte). In that
* case, eol_size will be added to the character count to account for
* the size of the EOL character.
*/
static varnumber_T
line_count_info(
char_u *line,
varnumber_T *wc,
varnumber_T *cc,
varnumber_T limit,
int eol_size)
{
varnumber_T i;
varnumber_T words = 0;
varnumber_T chars = 0;
int is_word = 0;
for (i = 0; i < limit && line[i] != NUL; )
{
if (is_word)
{
if (vim_isspace(line[i]))
{
words++;
is_word = 0;
}
}
else if (!vim_isspace(line[i]))
is_word = 1;
++chars;
i += (*mb_ptr2len)(line + i);
}
if (is_word)
words++;
*wc += words;
/* Add eol_size if the end of line was reached before hitting limit. */
if (i < limit && line[i] == NUL)
{
i += eol_size;
chars += eol_size;
}
*cc += chars;
return i;
}
/*
* Give some info about the position of the cursor (for "g CTRL-G").
* In Visual mode, give some info about the selected region. (In this case,
* the *_count_cursor variables store running totals for the selection.)
* When "dict" is not NULL store the info there instead of showing it.
*/
void
cursor_pos_info(dict_T *dict)
{
char_u *p;
char_u buf1[50];
char_u buf2[40];
linenr_T lnum;
varnumber_T byte_count = 0;
varnumber_T bom_count = 0;
varnumber_T byte_count_cursor = 0;
varnumber_T char_count = 0;
varnumber_T char_count_cursor = 0;
varnumber_T word_count = 0;
varnumber_T word_count_cursor = 0;
int eol_size;
varnumber_T last_check = 100000L;
long line_count_selected = 0;
pos_T min_pos, max_pos;
oparg_T oparg;
struct block_def bd;
/*
* Compute the length of the file in characters.
*/
if (curbuf->b_ml.ml_flags & ML_EMPTY)
{
if (dict == NULL)
{
msg(_(no_lines_msg));
return;
}
}
else
{
if (get_fileformat(curbuf) == EOL_DOS)
eol_size = 2;
else
eol_size = 1;
if (VIsual_active)
{
if (LT_POS(VIsual, curwin->w_cursor))
{
min_pos = VIsual;
max_pos = curwin->w_cursor;
}
else
{
min_pos = curwin->w_cursor;
max_pos = VIsual;
}
if (*p_sel == 'e' && max_pos.col > 0)
--max_pos.col;
if (VIsual_mode == Ctrl_V)
{
#ifdef FEAT_LINEBREAK
char_u * saved_sbr = p_sbr;
/* Make 'sbr' empty for a moment to get the correct size. */
p_sbr = empty_option;
#endif
oparg.is_VIsual = 1;
oparg.block_mode = TRUE;
oparg.op_type = OP_NOP;
getvcols(curwin, &min_pos, &max_pos,
&oparg.start_vcol, &oparg.end_vcol);
#ifdef FEAT_LINEBREAK
p_sbr = saved_sbr;
#endif
if (curwin->w_curswant == MAXCOL)
oparg.end_vcol = MAXCOL;
/* Swap the start, end vcol if needed */
if (oparg.end_vcol < oparg.start_vcol)
{
oparg.end_vcol += oparg.start_vcol;
oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
oparg.end_vcol -= oparg.start_vcol;
}
}
line_count_selected = max_pos.lnum - min_pos.lnum + 1;
}
for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
{
/* Check for a CTRL-C every 100000 characters. */
if (byte_count > last_check)
{
ui_breakcheck();
if (got_int)
return;
last_check = byte_count + 100000L;
}
/* Do extra processing for VIsual mode. */
if (VIsual_active
&& lnum >= min_pos.lnum && lnum <= max_pos.lnum)
{
char_u *s = NULL;
long len = 0L;
switch (VIsual_mode)
{
case Ctrl_V:
virtual_op = virtual_active();
block_prep(&oparg, &bd, lnum, 0);
virtual_op = MAYBE;
s = bd.textstart;
len = (long)bd.textlen;
break;
case 'V':
s = ml_get(lnum);
len = MAXCOL;
break;
case 'v':
{
colnr_T start_col = (lnum == min_pos.lnum)
? min_pos.col : 0;
colnr_T end_col = (lnum == max_pos.lnum)
? max_pos.col - start_col + 1 : MAXCOL;
s = ml_get(lnum) + start_col;
len = end_col;
}
break;
}
if (s != NULL)
{
byte_count_cursor += line_count_info(s, &word_count_cursor,
&char_count_cursor, len, eol_size);
if (lnum == curbuf->b_ml.ml_line_count
&& !curbuf->b_p_eol
&& (curbuf->b_p_bin || !curbuf->b_p_fixeol)
&& (long)STRLEN(s) < len)
byte_count_cursor -= eol_size;
}
}
else
{
/* In non-visual mode, check for the line the cursor is on */
if (lnum == curwin->w_cursor.lnum)
{
word_count_cursor += word_count;
char_count_cursor += char_count;
byte_count_cursor = byte_count +
line_count_info(ml_get(lnum),
&word_count_cursor, &char_count_cursor,
(varnumber_T)(curwin->w_cursor.col + 1),
eol_size);
}
}
/* Add to the running totals */
byte_count += line_count_info(ml_get(lnum), &word_count,
&char_count, (varnumber_T)MAXCOL,
eol_size);
}
/* Correction for when last line doesn't have an EOL. */
if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
byte_count -= eol_size;
if (dict == NULL)
{
if (VIsual_active)
{
if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
{
getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
&max_pos.col);
vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
(long)(oparg.end_vcol - oparg.start_vcol + 1));
}
else
buf1[0] = NUL;
if (char_count_cursor == byte_count_cursor
&& char_count == byte_count)
vim_snprintf((char *)IObuff, IOSIZE,
_("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
buf1, line_count_selected,
(long)curbuf->b_ml.ml_line_count,
(long_long_T)word_count_cursor,
(long_long_T)word_count,
(long_long_T)byte_count_cursor,
(long_long_T)byte_count);
else
vim_snprintf((char *)IObuff, IOSIZE,
_("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Chars; %lld of %lld Bytes"),
buf1, line_count_selected,
(long)curbuf->b_ml.ml_line_count,
(long_long_T)word_count_cursor,
(long_long_T)word_count,
(long_long_T)char_count_cursor,
(long_long_T)char_count,
(long_long_T)byte_count_cursor,
(long_long_T)byte_count);
}
else
{
p = ml_get_curline();
validate_virtcol();
col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
(int)curwin->w_virtcol + 1);
col_print(buf2, sizeof(buf2), (int)STRLEN(p),
linetabsize(p));
if (char_count_cursor == byte_count_cursor
&& char_count == byte_count)
vim_snprintf((char *)IObuff, IOSIZE,
_("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
(char *)buf1, (char *)buf2,
(long)curwin->w_cursor.lnum,
(long)curbuf->b_ml.ml_line_count,
(long_long_T)word_count_cursor, (long_long_T)word_count,
(long_long_T)byte_count_cursor, (long_long_T)byte_count);
else
vim_snprintf((char *)IObuff, IOSIZE,
_("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"),
(char *)buf1, (char *)buf2,
(long)curwin->w_cursor.lnum,
(long)curbuf->b_ml.ml_line_count,
(long_long_T)word_count_cursor, (long_long_T)word_count,
(long_long_T)char_count_cursor, (long_long_T)char_count,
(long_long_T)byte_count_cursor, (long_long_T)byte_count);
}
}
bom_count = bomb_size();
if (bom_count > 0)
vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
_("(+%lld for BOM)"), (long_long_T)bom_count);
if (dict == NULL)
{
/* Don't shorten this message, the user asked for it. */
p = p_shm;
p_shm = (char_u *)"";
msg((char *)IObuff);
p_shm = p;
}
}
#if defined(FEAT_EVAL)
if (dict != NULL)
{
dict_add_number(dict, "words", word_count);
dict_add_number(dict, "chars", char_count);
dict_add_number(dict, "bytes", byte_count + bom_count);
dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
byte_count_cursor);
dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
char_count_cursor);
dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
word_count_cursor);
}
#endif
}
|