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
|
/* 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.
*/
/*
* evalvars.c: functions for dealing with variables
*/
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
static dictitem_T globvars_var; // variable used for g:
static dict_T globvardict; // Dictionary with g: variables
#define globvarht globvardict.dv_hashtab
/*
* Old Vim variables such as "v:version" are also available without the "v:".
* Also in functions. We need a special hashtable for them.
*/
static hashtab_T compat_hashtab;
/*
* Array to hold the value of v: variables.
* The value is in a dictitem, so that it can also be used in the v: scope.
* The reason to use this table anyway is for very quick access to the
* variables with the VV_ defines.
*/
// values for vv_flags:
#define VV_COMPAT 1 // compatible, also used without "v:"
#define VV_RO 2 // read-only
#define VV_RO_SBX 4 // read-only in the sandbox
#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
typedef struct vimvar vimvar_T;
static struct vimvar
{
char *vv_name; // name of variable, without v:
dictitem16_T vv_di; // value and name for key (max 16 chars!)
type_T *vv_type; // type or NULL
char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
} vimvars[VV_LEN] =
{
// The order here must match the VV_ defines in vim.h!
// Initializing a union does not work, leave tv.vval empty to get zero's.
{VV_NAME("count", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
{VV_NAME("count1", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("prevcount", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("errmsg", VAR_STRING), NULL, VV_COMPAT},
{VV_NAME("warningmsg", VAR_STRING), NULL, 0},
{VV_NAME("statusmsg", VAR_STRING), NULL, 0},
{VV_NAME("shell_error", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
{VV_NAME("this_session", VAR_STRING), NULL, VV_COMPAT},
{VV_NAME("version", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
{VV_NAME("lnum", VAR_NUMBER), NULL, VV_RO_SBX},
{VV_NAME("termresponse", VAR_STRING), NULL, VV_RO},
{VV_NAME("fname", VAR_STRING), NULL, VV_RO},
{VV_NAME("lang", VAR_STRING), NULL, VV_RO},
{VV_NAME("lc_time", VAR_STRING), NULL, VV_RO},
{VV_NAME("ctype", VAR_STRING), NULL, VV_RO},
{VV_NAME("charconvert_from", VAR_STRING), NULL, VV_RO},
{VV_NAME("charconvert_to", VAR_STRING), NULL, VV_RO},
{VV_NAME("fname_in", VAR_STRING), NULL, VV_RO},
{VV_NAME("fname_out", VAR_STRING), NULL, VV_RO},
{VV_NAME("fname_new", VAR_STRING), NULL, VV_RO},
{VV_NAME("fname_diff", VAR_STRING), NULL, VV_RO},
{VV_NAME("cmdarg", VAR_STRING), NULL, VV_RO},
{VV_NAME("foldstart", VAR_NUMBER), NULL, VV_RO_SBX},
{VV_NAME("foldend", VAR_NUMBER), NULL, VV_RO_SBX},
{VV_NAME("folddashes", VAR_STRING), NULL, VV_RO_SBX},
{VV_NAME("foldlevel", VAR_NUMBER), NULL, VV_RO_SBX},
{VV_NAME("progname", VAR_STRING), NULL, VV_RO},
{VV_NAME("servername", VAR_STRING), NULL, VV_RO},
{VV_NAME("dying", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("exception", VAR_STRING), NULL, VV_RO},
{VV_NAME("throwpoint", VAR_STRING), NULL, VV_RO},
{VV_NAME("register", VAR_STRING), NULL, VV_RO},
{VV_NAME("cmdbang", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("insertmode", VAR_STRING), NULL, VV_RO},
{VV_NAME("val", VAR_UNKNOWN), NULL, VV_RO},
{VV_NAME("key", VAR_UNKNOWN), NULL, VV_RO},
{VV_NAME("profiling", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("fcs_reason", VAR_STRING), NULL, VV_RO},
{VV_NAME("fcs_choice", VAR_STRING), NULL, 0},
{VV_NAME("beval_bufnr", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("beval_winnr", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("beval_winid", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("beval_lnum", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("beval_col", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("beval_text", VAR_STRING), NULL, VV_RO},
{VV_NAME("scrollstart", VAR_STRING), NULL, 0},
{VV_NAME("swapname", VAR_STRING), NULL, VV_RO},
{VV_NAME("swapchoice", VAR_STRING), NULL, 0},
{VV_NAME("swapcommand", VAR_STRING), NULL, VV_RO},
{VV_NAME("char", VAR_STRING), NULL, 0},
{VV_NAME("mouse_win", VAR_NUMBER), NULL, 0},
{VV_NAME("mouse_winid", VAR_NUMBER), NULL, 0},
{VV_NAME("mouse_lnum", VAR_NUMBER), NULL, 0},
{VV_NAME("mouse_col", VAR_NUMBER), NULL, 0},
{VV_NAME("operator", VAR_STRING), NULL, VV_RO},
{VV_NAME("searchforward", VAR_NUMBER), NULL, 0},
{VV_NAME("hlsearch", VAR_NUMBER), NULL, 0},
{VV_NAME("oldfiles", VAR_LIST), &t_list_string, 0},
{VV_NAME("windowid", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("progpath", VAR_STRING), NULL, VV_RO},
{VV_NAME("completed_item", VAR_DICT), &t_dict_string, VV_RO},
{VV_NAME("option_new", VAR_STRING), NULL, VV_RO},
{VV_NAME("option_old", VAR_STRING), NULL, VV_RO},
{VV_NAME("option_oldlocal", VAR_STRING), NULL, VV_RO},
{VV_NAME("option_oldglobal", VAR_STRING), NULL, VV_RO},
{VV_NAME("option_command", VAR_STRING), NULL, VV_RO},
{VV_NAME("option_type", VAR_STRING), NULL, VV_RO},
{VV_NAME("errors", VAR_LIST), &t_list_string, 0},
{VV_NAME("false", VAR_BOOL), NULL, VV_RO},
{VV_NAME("true", VAR_BOOL), NULL, VV_RO},
{VV_NAME("none", VAR_SPECIAL), NULL, VV_RO},
{VV_NAME("null", VAR_SPECIAL), NULL, VV_RO},
{VV_NAME("numbermax", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("numbermin", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("numbersize", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("vim_did_enter", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("testing", VAR_NUMBER), NULL, 0},
{VV_NAME("t_number", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("t_string", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("t_func", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("t_list", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("t_dict", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("t_float", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("t_bool", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("t_none", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("t_job", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("t_channel", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("t_blob", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
{VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
{VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
{VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
{VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
{VV_NAME("event", VAR_DICT), NULL, VV_RO},
{VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
{VV_NAME("collate", VAR_STRING), NULL, VV_RO},
{VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
{VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
{VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
{VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
};
// shorthand
#define vv_tv_type vv_di.di_tv.v_type
#define vv_nr vv_di.di_tv.vval.v_number
#define vv_float vv_di.di_tv.vval.v_float
#define vv_str vv_di.di_tv.vval.v_string
#define vv_list vv_di.di_tv.vval.v_list
#define vv_dict vv_di.di_tv.vval.v_dict
#define vv_blob vv_di.di_tv.vval.v_blob
#define vv_tv vv_di.di_tv
static dictitem_T vimvars_var; // variable used for v:
static dict_T vimvardict; // Dictionary with v: variables
#define vimvarht vimvardict.dv_hashtab
// for VIM_VERSION_ defines
#include "version.h"
static void list_glob_vars(int *first);
static void list_buf_vars(int *first);
static void list_win_vars(int *first);
static void list_tab_vars(int *first);
static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op, int var_idx);
static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
static void list_one_var(dictitem_T *v, char *prefix, int *first);
static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
/*
* Initialize global and vim special variables
*/
void
evalvars_init(void)
{
int i;
struct vimvar *p;
init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
vimvardict.dv_lock = VAR_FIXED;
hash_init(&compat_hashtab);
for (i = 0; i < VV_LEN; ++i)
{
p = &vimvars[i];
if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
{
iemsg("INTERNAL: name too long, increase size of dictitem16_T");
getout(1);
}
STRCPY(p->vv_di.di_key, p->vv_name);
if (p->vv_flags & VV_RO)
p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
else if (p->vv_flags & VV_RO_SBX)
p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
else
p->vv_di.di_flags = DI_FLAGS_FIX;
// add to v: scope dict, unless the value is not always available
if (p->vv_tv_type != VAR_UNKNOWN)
hash_add(&vimvarht, p->vv_di.di_key);
if (p->vv_flags & VV_COMPAT)
// add to compat scope dict
hash_add(&compat_hashtab, p->vv_di.di_key);
}
set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
set_vim_var_nr(VV_SEARCHFORWARD, 1L);
set_vim_var_nr(VV_HLSEARCH, 1L);
set_vim_var_nr(VV_EXITING, VVAL_NULL);
set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
set_vim_var_list(VV_ERRORS, list_alloc());
set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
set_vim_var_nr(VV_FALSE, VVAL_FALSE);
set_vim_var_nr(VV_TRUE, VVAL_TRUE);
set_vim_var_nr(VV_NONE, VVAL_NONE);
set_vim_var_nr(VV_NULL, VVAL_NULL);
set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
set_vim_var_nr(VV_MAXCOL, MAXCOL);
set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
set_vim_var_dict(VV_COLORNAMES, dict_alloc());
// Default for v:register is not 0 but '"'. This is adjusted once the
// clipboard has been setup by calling reset_reg_var().
set_reg_var(0);
}
#if defined(EXITFREE) || defined(PROTO)
/*
* Free all vim variables information on exit
*/
void
evalvars_clear(void)
{
int i;
struct vimvar *p;
for (i = 0; i < VV_LEN; ++i)
{
p = &vimvars[i];
if (p->vv_di.di_tv.v_type == VAR_STRING)
VIM_CLEAR(p->vv_str);
else if (p->vv_di.di_tv.v_type == VAR_LIST)
{
list_unref(p->vv_list);
p->vv_list = NULL;
}
}
hash_clear(&vimvarht);
hash_init(&vimvarht); // garbage_collect() will access it
hash_clear(&compat_hashtab);
// global variables
vars_clear(&globvarht);
// Script-local variables. Clear all the variables here.
// The scriptvar_T is cleared later in free_scriptnames(), because a
// variable in one script might hold a reference to the whole scope of
// another script.
for (i = 1; i <= script_items.ga_len; ++i)
vars_clear(&SCRIPT_VARS(i));
}
#endif
int
garbage_collect_globvars(int copyID)
{
return set_ref_in_ht(&globvarht, copyID, NULL);
}
int
garbage_collect_vimvars(int copyID)
{
return set_ref_in_ht(&vimvarht, copyID, NULL);
}
int
garbage_collect_scriptvars(int copyID)
{
int i;
int idx;
int abort = FALSE;
scriptitem_T *si;
for (i = 1; i <= script_items.ga_len; ++i)
{
abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
si = SCRIPT_ITEM(i);
for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
{
svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
if (sv->sv_name != NULL)
abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
}
}
return abort;
}
/*
* Set an internal variable to a string value. Creates the variable if it does
* not already exist.
*/
void
set_internal_string_var(char_u *name, char_u *value)
{
char_u *val;
typval_T *tvp;
val = vim_strsave(value);
if (val != NULL)
{
tvp = alloc_string_tv(val);
if (tvp != NULL)
{
set_var(name, tvp, FALSE);
free_tv(tvp);
}
}
}
int
eval_charconvert(
char_u *enc_from,
char_u *enc_to,
char_u *fname_from,
char_u *fname_to)
{
int err = FALSE;
sctx_T saved_sctx = current_sctx;
sctx_T *ctx;
set_vim_var_string(VV_CC_FROM, enc_from, -1);
set_vim_var_string(VV_CC_TO, enc_to, -1);
set_vim_var_string(VV_FNAME_IN, fname_from, -1);
set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
ctx = get_option_sctx("charconvert");
if (ctx != NULL)
current_sctx = *ctx;
if (eval_to_bool(p_ccv, &err, NULL, FALSE))
err = TRUE;
set_vim_var_string(VV_CC_FROM, NULL, -1);
set_vim_var_string(VV_CC_TO, NULL, -1);
set_vim_var_string(VV_FNAME_IN, NULL, -1);
set_vim_var_string(VV_FNAME_OUT, NULL, -1);
current_sctx = saved_sctx;
if (err)
return FAIL;
return OK;
}
# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
int
eval_printexpr(char_u *fname, char_u *args)
{
int err = FALSE;
sctx_T saved_sctx = current_sctx;
sctx_T *ctx;
set_vim_var_string(VV_FNAME_IN, fname, -1);
set_vim_var_string(VV_CMDARG, args, -1);
ctx = get_option_sctx("printexpr");
if (ctx != NULL)
current_sctx = *ctx;
if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
err = TRUE;
set_vim_var_string(VV_FNAME_IN, NULL, -1);
set_vim_var_string(VV_CMDARG, NULL, -1);
current_sctx = saved_sctx;
if (err)
{
mch_remove(fname);
return FAIL;
}
return OK;
}
# endif
# if defined(FEAT_DIFF) || defined(PROTO)
void
eval_diff(
char_u *origfile,
char_u *newfile,
char_u *outfile)
{
sctx_T saved_sctx = current_sctx;
sctx_T *ctx;
typval_T *tv;
set_vim_var_string(VV_FNAME_IN, origfile, -1);
set_vim_var_string(VV_FNAME_NEW, newfile, -1);
set_vim_var_string(VV_FNAME_OUT, outfile, -1);
ctx = get_option_sctx("diffexpr");
if (ctx != NULL)
current_sctx = *ctx;
// errors are ignored
tv = eval_expr(p_dex, NULL);
free_tv(tv);
set_vim_var_string(VV_FNAME_IN, NULL, -1);
set_vim_var_string(VV_FNAME_NEW, NULL, -1);
set_vim_var_string(VV_FNAME_OUT, NULL, -1);
current_sctx = saved_sctx;
}
void
eval_patch(
char_u *origfile,
char_u *difffile,
char_u *outfile)
{
sctx_T saved_sctx = current_sctx;
sctx_T *ctx;
typval_T *tv;
set_vim_var_string(VV_FNAME_IN, origfile, -1);
set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
set_vim_var_string(VV_FNAME_OUT, outfile, -1);
ctx = get_option_sctx("patchexpr");
if (ctx != NULL)
current_sctx = *ctx;
// errors are ignored
tv = eval_expr(p_pex, NULL);
free_tv(tv);
set_vim_var_string(VV_FNAME_IN, NULL, -1);
set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
set_vim_var_string(VV_FNAME_OUT, NULL, -1);
current_sctx = saved_sctx;
}
# endif
#if defined(FEAT_SPELL) || defined(PROTO)
/*
* Evaluate an expression to a list with suggestions.
* For the "expr:" part of 'spellsuggest'.
* Returns NULL when there is an error.
*/
list_T *
eval_spell_expr(char_u *badword, char_u *expr)
{
typval_T save_val;
typval_T rettv;
list_T *list = NULL;
char_u *p = skipwhite(expr);
sctx_T saved_sctx = current_sctx;
sctx_T *ctx;
// Set "v:val" to the bad word.
prepare_vimvar(VV_VAL, &save_val);
set_vim_var_string(VV_VAL, badword, -1);
if (p_verbose == 0)
++emsg_off;
ctx = get_option_sctx("spellsuggest");
if (ctx != NULL)
current_sctx = *ctx;
if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
{
if (rettv.v_type != VAR_LIST)
clear_tv(&rettv);
else
list = rettv.vval.v_list;
}
if (p_verbose == 0)
--emsg_off;
clear_tv(get_vim_var_tv(VV_VAL));
restore_vimvar(VV_VAL, &save_val);
current_sctx = saved_sctx;
return list;
}
/*
* "list" is supposed to contain two items: a word and a number. Return the
* word in "pp" and the number as the return value.
* Return -1 if anything isn't right.
* Used to get the good word and score from the eval_spell_expr() result.
*/
int
get_spellword(list_T *list, char_u **pp)
{
listitem_T *li;
li = list->lv_first;
if (li == NULL)
return -1;
*pp = tv_get_string(&li->li_tv);
li = li->li_next;
if (li == NULL)
return -1;
return (int)tv_get_number(&li->li_tv);
}
#endif
/*
* Prepare v: variable "idx" to be used.
* Save the current typeval in "save_tv" and clear it.
* When not used yet add the variable to the v: hashtable.
*/
void
prepare_vimvar(int idx, typval_T *save_tv)
{
*save_tv = vimvars[idx].vv_tv;
vimvars[idx].vv_str = NULL; // don't free it now
if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
}
/*
* Restore v: variable "idx" to typeval "save_tv".
* Note that the v: variable must have been cleared already.
* When no longer defined, remove the variable from the v: hashtable.
*/
void
restore_vimvar(int idx, typval_T *save_tv)
{
hashitem_T *hi;
vimvars[idx].vv_tv = *save_tv;
if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
{
hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
if (HASHITEM_EMPTY(hi))
internal_error("restore_vimvar()");
else
hash_remove(&vimvarht, hi);
}
}
/*
* List Vim variables.
*/
static void
list_vim_vars(int *first)
{
list_hashtable_vars(&vimvarht, "v:", FALSE, first);
}
/*
* List script-local variables, if there is a script.
*/
static void
list_script_vars(int *first)
{
if (SCRIPT_ID_VALID(current_sctx.sc_sid))
list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
"s:", FALSE, first);
}
/*
* Get a list of lines from a HERE document. The here document is a list of
* lines surrounded by a marker.
* cmd << {marker}
* {line1}
* {line2}
* ....
* {marker}
*
* The {marker} is a string. If the optional 'trim' word is supplied before the
* marker, then the leading indentation before the lines (matching the
* indentation in the 'cmd' line) is stripped.
*
* When getting lines for an embedded script (e.g. python, lua, perl, ruby,
* tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
* missing, then '.' is accepted as a marker.
*
* Returns a List with {lines} or NULL.
*/
list_T *
heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
{
char_u *theline;
char_u *marker;
list_T *l;
char_u *p;
int marker_indent_len = 0;
int text_indent_len = 0;
char_u *text_indent = NULL;
char_u dot[] = ".";
int comment_char = in_vim9script() ? '#' : '"';
if (eap->getline == NULL)
{
emsg(_(e_cannot_use_heredoc_here));
return NULL;
}
// Check for the optional 'trim' word before the marker
cmd = skipwhite(cmd);
if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
{
cmd = skipwhite(cmd + 4);
// Trim the indentation from all the lines in the here document.
// The amount of indentation trimmed is the same as the indentation of
// the first line after the :let command line. To find the end marker
// the indent of the :let command line is trimmed.
p = *eap->cmdlinep;
while (VIM_ISWHITE(*p))
{
p++;
marker_indent_len++;
}
text_indent_len = -1;
}
// The marker is the next word.
if (*cmd != NUL && *cmd != comment_char)
{
marker = skipwhite(cmd);
p = skiptowhite(marker);
if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
{
semsg(_(e_trailing_characters_str), p);
return NULL;
}
*p = NUL;
if (!script_get && vim_islower(*marker))
{
emsg(_(e_marker_cannot_start_with_lower_case_letter));
return NULL;
}
}
else
{
// When getting lines for an embedded script, if the marker is missing,
// accept '.' as the marker.
if (script_get)
marker = dot;
else
{
emsg(_(e_missing_marker));
return NULL;
}
}
l = list_alloc();
if (l == NULL)
return NULL;
for (;;)
{
int mi = 0;
int ti = 0;
theline = eap->getline(NUL, eap->cookie, 0, FALSE);
if (theline == NULL)
{
semsg(_(e_missing_end_marker_str), marker);
break;
}
// with "trim": skip the indent matching the :let line to find the
// marker
if (marker_indent_len > 0
&& STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
mi = marker_indent_len;
if (STRCMP(marker, theline + mi) == 0)
{
vim_free(theline);
break;
}
if (text_indent_len == -1 && *theline != NUL)
{
// set the text indent from the first line.
p = theline;
text_indent_len = 0;
while (VIM_ISWHITE(*p))
{
p++;
text_indent_len++;
}
text_indent = vim_strnsave(theline, text_indent_len);
}
// with "trim": skip the indent matching the first line
if (text_indent != NULL)
for (ti = 0; ti < text_indent_len; ++ti)
if (theline[ti] != text_indent[ti])
break;
if (list_append_string(l, theline + ti, -1) == FAIL)
break;
vim_free(theline);
}
vim_free(text_indent);
return l;
}
/*
* Vim9 variable declaration:
* ":var name"
* ":var name: type"
* ":var name = expr"
* ":var name: type = expr"
* etc.
*/
void
ex_var(exarg_T *eap)
{
if (!in_vim9script())
{
semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
return;
}
ex_let(eap);
}
/*
* ":let" list all variable values
* ":let var1 var2" list variable values
* ":let var = expr" assignment command.
* ":let var += expr" assignment command.
* ":let var -= expr" assignment command.
* ":let var *= expr" assignment command.
* ":let var /= expr" assignment command.
* ":let var %= expr" assignment command.
* ":let var .= expr" assignment command.
* ":let var ..= expr" assignment command.
* ":let [var1, var2] = expr" unpack list.
* ":let var =<< ..." heredoc
* ":let var: string" Vim9 declaration
*
* ":final var = expr" assignment command.
* ":final [var1, var2] = expr" unpack list.
*
* ":const" list all variable values
* ":const var1 var2" list variable values
* ":const var = expr" assignment command.
* ":const [var1, var2] = expr" unpack list.
*/
void
ex_let(exarg_T *eap)
{
char_u *arg = eap->arg;
char_u *expr = NULL;
typval_T rettv;
int i;
int var_count = 0;
int semicolon = 0;
char_u op[4];
char_u *argend;
int first = TRUE;
int concat;
int has_assign;
int flags = 0;
int vim9script = in_vim9script();
if (eap->cmdidx == CMD_final && !vim9script)
{
// In legacy Vim script ":final" is short for ":finally".
ex_finally(eap);
return;
}
if (eap->cmdidx == CMD_let && vim9script)
{
emsg(_(e_cannot_use_let_in_vim9_script));
return;
}
if (eap->cmdidx == CMD_const)
flags |= ASSIGN_CONST;
else if (eap->cmdidx == CMD_final)
flags |= ASSIGN_FINAL;
// Vim9 assignment without ":let", ":const" or ":final"
if (eap->arg == eap->cmd)
flags |= ASSIGN_NO_DECL;
argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
if (argend == NULL)
return;
if (argend > arg && argend[-1] == '.') // for var.='str'
--argend;
expr = skipwhite(argend);
concat = expr[0] == '.'
&& ((expr[1] == '=' && in_old_script(2))
|| (expr[1] == '.' && expr[2] == '='));
has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
&& expr[1] == '=');
if (!has_assign && !concat)
{
// ":let" without "=": list variables
if (*arg == '[')
emsg(_(e_invalid_argument));
else if (expr[0] == '.' && expr[1] == '=')
emsg(_(e_dot_equal_not_supported_with_script_version_two));
else if (!ends_excmd2(eap->cmd, arg))
{
if (vim9script)
{
if (!ends_excmd2(eap->cmd, skipwhite(argend)))
semsg(_(e_trailing_characters_str), argend);
else
// Vim9 declaration ":var name: type"
arg = vim9_declare_scriptvar(eap, arg);
}
else
{
// ":let var1 var2" - list values
arg = list_arg_vars(eap, arg, &first);
}
}
else if (!eap->skip)
{
// ":let"
list_glob_vars(&first);
list_buf_vars(&first);
list_win_vars(&first);
list_tab_vars(&first);
list_script_vars(&first);
list_func_vars(&first);
list_vim_vars(&first);
}
set_nextcmd(eap, arg);
}
else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
{
list_T *l;
long cur_lnum = SOURCING_LNUM;
// HERE document
l = heredoc_get(eap, expr + 3, FALSE);
if (l != NULL)
{
rettv_list_set(&rettv, l);
if (!eap->skip)
{
// errors are for the assignment, not the end marker
SOURCING_LNUM = cur_lnum;
op[0] = '=';
op[1] = NUL;
(void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
flags, op);
}
clear_tv(&rettv);
}
}
else
{
evalarg_T evalarg;
int len = 1;
CLEAR_FIELD(rettv);
i = FAIL;
if (has_assign || concat)
{
int cur_lnum;
op[0] = '=';
op[1] = NUL;
if (*expr != '=')
{
if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
{
// +=, /=, etc. require an existing variable
semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
i = FAIL;
}
else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
{
op[0] = *expr; // +=, -=, *=, /=, %= or .=
++len;
if (expr[0] == '.' && expr[1] == '.') // ..=
{
++expr;
++len;
}
}
expr += 2;
}
else
++expr;
if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
|| !IS_WHITE_OR_NUL(*expr)))
{
vim_strncpy(op, expr - len, len);
semsg(_(e_white_space_required_before_and_after_str_at_str),
op, argend);
i = FAIL;
}
if (eap->skip)
++emsg_skip;
fill_evalarg_from_eap(&evalarg, eap, eap->skip);
expr = skipwhite_and_linebreak(expr, &evalarg);
cur_lnum = SOURCING_LNUM;
i = eval0(expr, &rettv, eap, &evalarg);
if (eap->skip)
--emsg_skip;
clear_evalarg(&evalarg, eap);
// Restore the line number so that any type error is given for the
// declaration, not the expression.
SOURCING_LNUM = cur_lnum;
}
if (eap->skip)
{
if (i != FAIL)
clear_tv(&rettv);
}
else if (i != FAIL)
{
(void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
flags, op);
clear_tv(&rettv);
}
}
}
/*
* Assign the typeval "tv" to the variable or variables at "arg_start".
* Handles both "var" with any type and "[var, var; var]" with a list type.
* When "op" is not NULL it points to a string with characters that
* must appear after the variable(s). Use "+", "-" or "." for add, subtract
* or concatenate.
* Returns OK or FAIL;
*/
int
ex_let_vars(
char_u *arg_start,
typval_T *tv,
int copy, // copy values from "tv", don't move
int semicolon, // from skip_var_list()
int var_count, // from skip_var_list()
int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
char_u *op)
{
char_u *arg = arg_start;
list_T *l;
int i;
int var_idx = 0;
listitem_T *item;
typval_T ltv;
if (*arg != '[')
{
// ":let var = expr" or ":for var in list"
if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
return FAIL;
return OK;
}
// ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
{
emsg(_(e_list_required));
return FAIL;
}
i = list_len(l);
if (semicolon == 0 && var_count < i)
{
emsg(_(e_less_targets_than_list_items));
return FAIL;
}
if (var_count - semicolon > i)
{
emsg(_(e_more_targets_than_list_items));
return FAIL;
}
CHECK_LIST_MATERIALIZE(l);
item = l->lv_first;
while (*arg != ']')
{
arg = skipwhite(arg + 1);
++var_idx;
arg = ex_let_one(arg, &item->li_tv, TRUE,
flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
item = item->li_next;
if (arg == NULL)
return FAIL;
arg = skipwhite(arg);
if (*arg == ';')
{
// Put the rest of the list (may be empty) in the var after ';'.
// Create a new list for this.
l = list_alloc();
if (l == NULL)
return FAIL;
while (item != NULL)
{
list_append_tv(l, &item->li_tv);
item = item->li_next;
}
ltv.v_type = VAR_LIST;
ltv.v_lock = 0;
ltv.vval.v_list = l;
l->lv_refcount = 1;
++var_idx;
arg = ex_let_one(skipwhite(arg + 1), <v, FALSE,
flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
clear_tv(<v);
if (arg == NULL)
return FAIL;
break;
}
else if (*arg != ',' && *arg != ']')
{
internal_error("ex_let_vars()");
return FAIL;
}
}
return OK;
}
/*
* Skip over assignable variable "var" or list of variables "[var, var]".
* Used for ":let varvar = expr" and ":for varvar in expr".
* For "[var, var]" increment "*var_count" for each variable.
* for "[var, var; var]" set "semicolon" to 1.
* If "silent" is TRUE do not give an "invalid argument" error message.
* Return NULL for an error.
*/
char_u *
skip_var_list(
char_u *arg,
int include_type,
int *var_count,
int *semicolon,
int silent)
{
char_u *p, *s;
if (*arg == '[')
{
// "[var, var]": find the matching ']'.
p = arg;
for (;;)
{
p = skipwhite(p + 1); // skip whites after '[', ';' or ','
s = skip_var_one(p, include_type);
if (s == p)
{
if (!silent)
semsg(_(e_invalid_argument_str), p);
return NULL;
}
++*var_count;
p = skipwhite(s);
if (*p == ']')
break;
else if (*p == ';')
{
if (*semicolon == 1)
{
emsg(_(e_double_semicolon_in_list_of_variables));
return NULL;
}
*semicolon = 1;
}
else if (*p != ',')
{
if (!silent)
semsg(_(e_invalid_argument_str), p);
return NULL;
}
}
return p + 1;
}
else
return skip_var_one(arg, include_type);
}
/*
* Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
* l[idx].
* In Vim9 script also skip over ": type" if "include_type" is TRUE.
*/
char_u *
skip_var_one(char_u *arg, int include_type)
{
char_u *end;
int vim9 = in_vim9script();
if (*arg == '@' && arg[1] != NUL)
return arg + 2;
end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
// "a: type" is declaring variable "a" with a type, not "a:".
// Same for "s: type".
if (vim9 && end == arg + 2 && end[-1] == ':')
--end;
if (include_type && vim9)
{
if (*end == ':')
end = skip_type(skipwhite(end + 1), FALSE);
}
return end;
}
/*
* List variables for hashtab "ht" with prefix "prefix".
* If "empty" is TRUE also list NULL strings as empty strings.
*/
void
list_hashtable_vars(
hashtab_T *ht,
char *prefix,
int empty,
int *first)
{
hashitem_T *hi;
dictitem_T *di;
int todo;
char_u buf[IOSIZE];
todo = (int)ht->ht_used;
for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
{
if (!HASHITEM_EMPTY(hi))
{
--todo;
di = HI2DI(hi);
// apply :filter /pat/ to variable name
vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
vim_strcat((char_u *)buf, di->di_key, IOSIZE);
if (message_filtered(buf))
continue;
if (empty || di->di_tv.v_type != VAR_STRING
|| di->di_tv.vval.v_string != NULL)
list_one_var(di, prefix, first);
}
}
}
/*
* List global variables.
*/
static void
list_glob_vars(int *first)
{
list_hashtable_vars(&globvarht, "", TRUE, first);
}
/*
* List buffer variables.
*/
static void
list_buf_vars(int *first)
{
list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
}
/*
* List window variables.
*/
static void
list_win_vars(int *first)
{
list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
}
/*
* List tab page variables.
*/
static void
list_tab_vars(int *first)
{
list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
}
/*
* List variables in "arg".
*/
static char_u *
list_arg_vars(exarg_T *eap, char_u *arg, int *first)
{
int error = FALSE;
int len;
char_u *name;
char_u *name_start;
char_u *arg_subsc;
char_u *tofree;
typval_T tv;
while (!ends_excmd2(eap->cmd, arg) && !got_int)
{
if (error || eap->skip)
{
arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
{
emsg_severe = TRUE;
if (!did_emsg)
semsg(_(e_trailing_characters_str), arg);
break;
}
}
else
{
// get_name_len() takes care of expanding curly braces
name_start = name = arg;
len = get_name_len(&arg, &tofree, TRUE, TRUE);
if (len <= 0)
{
// This is mainly to keep test 49 working: when expanding
// curly braces fails overrule the exception error message.
if (len < 0 && !aborting())
{
emsg_severe = TRUE;
semsg(_(e_invalid_argument_str), arg);
break;
}
error = TRUE;
}
else
{
arg = skipwhite(arg);
if (tofree != NULL)
name = tofree;
if (eval_variable(name, len, 0, &tv, NULL,
EVAL_VAR_VERBOSE) == FAIL)
error = TRUE;
else
{
// handle d.key, l[idx], f(expr)
arg_subsc = arg;
if (handle_subscript(&arg, name_start, &tv,
&EVALARG_EVALUATE, TRUE) == FAIL)
error = TRUE;
else
{
if (arg == arg_subsc && len == 2 && name[1] == ':')
{
switch (*name)
{
case 'g': list_glob_vars(first); break;
case 'b': list_buf_vars(first); break;
case 'w': list_win_vars(first); break;
case 't': list_tab_vars(first); break;
case 'v': list_vim_vars(first); break;
case 's': list_script_vars(first); break;
case 'l': list_func_vars(first); break;
default:
semsg(_(e_cant_list_variables_for_str), name);
}
}
else
{
char_u numbuf[NUMBUFLEN];
char_u *tf;
int c;
char_u *s;
s = echo_string(&tv, &tf, numbuf, 0);
c = *arg;
*arg = NUL;
list_one_var_a("",
arg == arg_subsc ? name : name_start,
tv.v_type,
s == NULL ? (char_u *)"" : s,
first);
*arg = c;
vim_free(tf);
}
clear_tv(&tv);
}
}
}
vim_free(tofree);
}
arg = skipwhite(arg);
}
return arg;
}
/*
* Set an environment variable, part of ex_let_one().
*/
static char_u *
ex_let_env(
char_u *arg,
typval_T *tv,
int flags,
char_u *endchars,
char_u *op)
{
char_u *arg_end = NULL;
char_u *name;
int len;
if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
&& (flags & ASSIGN_FOR_LOOP) == 0)
{
emsg(_(e_cannot_lock_environment_variable));
return NULL;
}
// Find the end of the name.
++arg;
name = arg;
len = get_env_len(&arg);
if (len == 0)
semsg(_(e_invalid_argument_str), name - 1);
else
{
if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
semsg(_(e_wrong_variable_type_for_str_equal), op);
else if (endchars != NULL
&& vim_strchr(endchars, *skipwhite(arg)) == NULL)
emsg(_(e_unexpected_characters_in_let));
else if (!check_secure())
{
char_u *tofree = NULL;
int c1 = name[len];
char_u *p;
name[len] = NUL;
p = tv_get_string_chk(tv);
if (p != NULL && op != NULL && *op == '.')
{
int mustfree = FALSE;
char_u *s = vim_getenv(name, &mustfree);
if (s != NULL)
{
p = tofree = concat_str(s, p);
if (mustfree)
vim_free(s);
}
}
if (p != NULL)
{
vim_setenv_ext(name, p);
arg_end = arg;
}
name[len] = c1;
vim_free(tofree);
}
}
return arg_end;
}
/*
* Set an option, part of ex_let_one().
*/
static char_u *
ex_let_option(
char_u *arg,
typval_T *tv,
int flags,
char_u *endchars,
char_u *op)
{
char_u *p;
int scope;
char_u *arg_end = NULL;
if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
&& (flags & ASSIGN_FOR_LOOP) == 0)
{
emsg(_(e_cannot_lock_option));
return NULL;
}
// Find the end of the name.
p = find_option_end(&arg, &scope);
if (p == NULL || (endchars != NULL
&& vim_strchr(endchars, *skipwhite(p)) == NULL))
emsg(_(e_unexpected_characters_in_let));
else
{
int c1;
long n = 0;
getoption_T opt_type;
long numval;
char_u *stringval = NULL;
char_u *s = NULL;
int failed = FALSE;
int opt_p_flags;
char_u *tofree = NULL;
char_u numbuf[NUMBUFLEN];
c1 = *p;
*p = NUL;
opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
scope);
if ((opt_type == gov_bool
|| opt_type == gov_number
|| opt_type == gov_hidden_bool
|| opt_type == gov_hidden_number)
&& (tv->v_type != VAR_STRING || !in_vim9script()))
{
if (opt_type == gov_bool || opt_type == gov_hidden_bool)
// bool, possibly hidden
n = (long)tv_get_bool(tv);
else
// number, possibly hidden
n = (long)tv_get_number(tv);
}
if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
|| tv->v_type == VAR_FUNC))
{
// If the option can be set to a function reference or a lambda
// and the passed value is a function reference, then convert it to
// the name (string) of the function reference.
s = tv2string(tv, &tofree, numbuf, 0);
}
// Avoid setting a string option to the text "v:false" or similar.
// In Vim9 script also don't convert a number to string.
else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
&& (!in_vim9script() || tv->v_type != VAR_NUMBER))
s = tv_get_string_chk(tv);
if (op != NULL && *op != '=')
{
if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
|| (opt_type == gov_string && *op != '.'))
{
semsg(_(e_wrong_variable_type_for_str_equal), op);
failed = TRUE; // don't set the value
}
else
{
// number, in legacy script also bool
if (opt_type == gov_number
|| (opt_type == gov_bool && !in_vim9script()))
{
switch (*op)
{
case '+': n = numval + n; break;
case '-': n = numval - n; break;
case '*': n = numval * n; break;
case '/': n = (long)num_divide(numval, n,
&failed); break;
case '%': n = (long)num_modulus(numval, n,
&failed); break;
}
s = NULL;
}
else if (opt_type == gov_string
&& stringval != NULL && s != NULL)
{
// string
s = concat_str(stringval, s);
vim_free(stringval);
stringval = s;
}
}
}
if (!failed)
{
if (opt_type != gov_string || s != NULL)
{
set_option_value(arg, n, s, scope);
arg_end = p;
}
else
emsg(_(e_string_required));
}
*p = c1;
vim_free(stringval);
vim_free(tofree);
}
return arg_end;
}
/*
* Set a register, part of ex_let_one().
*/
static char_u *
ex_let_register(
char_u *arg,
typval_T *tv,
int flags,
char_u *endchars,
char_u *op)
{
char_u *arg_end = NULL;
if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
&& (flags & ASSIGN_FOR_LOOP) == 0)
{
emsg(_(e_cannot_lock_register));
return NULL;
}
++arg;
if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
semsg(_(e_wrong_variable_type_for_str_equal), op);
else if (endchars != NULL
&& vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
emsg(_(e_unexpected_characters_in_let));
else
{
char_u *ptofree = NULL;
char_u *p;
p = tv_get_string_chk(tv);
if (p != NULL && op != NULL && *op == '.')
{
char_u *s = get_reg_contents(*arg == '@'
? '"' : *arg, GREG_EXPR_SRC);
if (s != NULL)
{
p = ptofree = concat_str(s, p);
vim_free(s);
}
}
if (p != NULL)
{
write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
arg_end = arg + 1;
}
vim_free(ptofree);
}
return arg_end;
}
/*
* Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
* Returns a pointer to the char just after the var name.
* Returns NULL if there is an error.
*/
static char_u *
ex_let_one(
char_u *arg, // points to variable name
typval_T *tv, // value to assign to variable
int copy, // copy value from "tv"
int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
char_u *endchars, // valid chars after variable name or NULL
char_u *op, // "+", "-", "." or NULL
int var_idx) // variable index for "let [a, b] = list"
{
char_u *arg_end = NULL;
if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
&& (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
&& vim_strchr((char_u *)"$@&", *arg) != NULL)
{
vim9_declare_error(arg);
return NULL;
}
if (*arg == '$')
{
// ":let $VAR = expr": Set environment variable.
return ex_let_env(arg, tv, flags, endchars, op);
}
else if (*arg == '&')
{
// ":let &option = expr": Set option value.
// ":let &l:option = expr": Set local option value.
// ":let &g:option = expr": Set global option value.
// ":for &ts in range(8)": Set option value for for loop
return ex_let_option(arg, tv, flags, endchars, op);
}
else if (*arg == '@')
{
// ":let @r = expr": Set register contents.
return ex_let_register(arg, tv, flags, endchars, op);
}
else if (eval_isnamec1(*arg) || *arg == '{')
{
lval_T lv;
char_u *p;
// ":let var = expr": Set internal variable.
// ":let var: type = expr": Set internal variable with type.
// ":let {expr} = expr": Idem, name made with curly braces
p = get_lval(arg, tv, &lv, FALSE, FALSE,
(flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
? GLV_NO_DECL : 0, FNE_CHECK_START);
if (p != NULL && lv.ll_name != NULL)
{
if (endchars != NULL && vim_strchr(endchars,
*skipwhite(lv.ll_name_end)) == NULL)
{
emsg(_(e_unexpected_characters_in_let));
}
else
{
set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
arg_end = lv.ll_name_end;
}
}
clear_lval(&lv);
}
else
semsg(_(e_invalid_argument_str), arg);
return arg_end;
}
/*
* ":unlet[!] var1 ... " command.
*/
void
ex_unlet(exarg_T *eap)
{
ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
}
/*
* ":lockvar" and ":unlockvar" commands
*/
void
ex_lockvar(exarg_T *eap)
{
char_u *arg = eap->arg;
int deep = 2;
if (eap->forceit)
deep = -1;
else if (vim_isdigit(*arg))
{
deep = getdigits(&arg);
arg = skipwhite(arg);
}
ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
}
/*
* ":unlet", ":lockvar" and ":unlockvar" are quite similar.
* Also used for Vim9 script. "callback" is invoked as:
* callback(&lv, name_end, eap, deep, cookie)
*/
void
ex_unletlock(
exarg_T *eap,
char_u *argstart,
int deep,
int glv_flags,
int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
void *cookie)
{
char_u *arg = argstart;
char_u *name_end;
int error = FALSE;
lval_T lv;
do
{
if (*arg == '$')
{
lv.ll_name = arg;
lv.ll_tv = NULL;
++arg;
if (get_env_len(&arg) == 0)
{
semsg(_(e_invalid_argument_str), arg - 1);
return;
}
if (!error && !eap->skip
&& callback(&lv, arg, eap, deep, cookie) == FAIL)
error = TRUE;
name_end = arg;
}
else
{
// Parse the name and find the end.
name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
glv_flags | GLV_NO_DECL, FNE_CHECK_START);
if (lv.ll_name == NULL)
error = TRUE; // error but continue parsing
if (name_end == NULL || (!VIM_ISWHITE(*name_end)
&& !ends_excmd(*name_end)))
{
if (name_end != NULL)
{
emsg_severe = TRUE;
semsg(_(e_trailing_characters_str), name_end);
}
if (!(eap->skip || error))
clear_lval(&lv);
break;
}
if (!error && !eap->skip
&& callback(&lv, name_end, eap, deep, cookie) == FAIL)
error = TRUE;
if (!eap->skip)
clear_lval(&lv);
}
arg = skipwhite(name_end);
} while (!ends_excmd2(name_end, arg));
set_nextcmd(eap, arg);
}
static int
do_unlet_var(
lval_T *lp,
char_u *name_end,
exarg_T *eap,
int deep UNUSED,
void *cookie UNUSED)
{
int forceit = eap->forceit;
int ret = OK;
int cc;
if (lp->ll_tv == NULL)
{
cc = *name_end;
*name_end = NUL;
// Environment variable, normal name or expanded name.
if (*lp->ll_name == '$')
vim_unsetenv(lp->ll_name + 1);
else if (do_unlet(lp->ll_name, forceit) == FAIL)
ret = FAIL;
*name_end = cc;
}
else if ((lp->ll_list != NULL
&& value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
|| (lp->ll_dict != NULL
&& value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
return FAIL;
else if (lp->ll_range)
{
if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
!lp->ll_empty2, lp->ll_n2) == FAIL)
return FAIL;
}
else
{
if (lp->ll_list != NULL)
// unlet a List item.
listitem_remove(lp->ll_list, lp->ll_li);
else
// unlet a Dictionary item.
dictitem_remove(lp->ll_dict, lp->ll_di);
}
return ret;
}
/*
* Unlet one item or a range of items from a list.
* Return OK or FAIL.
*/
int
list_unlet_range(
list_T *l,
listitem_T *li_first,
char_u *name,
long n1_arg,
int has_n2,
long n2)
{
listitem_T *li = li_first;
int n1 = n1_arg;
while (li != NULL && (!has_n2 || n2 >= n1))
{
if (value_check_lock(li->li_tv.v_lock, name, FALSE))
return FAIL;
li = li->li_next;
++n1;
}
// Delete a range of List items.
li = li_first;
n1 = n1_arg;
while (li != NULL && (!has_n2 || n2 >= n1))
{
listitem_T *next = li->li_next;
listitem_remove(l, li);
li = next;
++n1;
}
return OK;
}
/*
* "unlet" a variable. Return OK if it existed, FAIL if not.
* When "forceit" is TRUE don't complain if the variable doesn't exist.
*/
int
do_unlet(char_u *name, int forceit)
{
hashtab_T *ht;
hashitem_T *hi;
char_u *varname;
dict_T *d;
dictitem_T *di;
// can't :unlet a script variable in Vim9 script
if (in_vim9script() && check_vim9_unlet(name) == FAIL)
return FAIL;
ht = find_var_ht(name, &varname);
// can't :unlet a script variable in Vim9 script from a function
if (ht == get_script_local_ht()
&& SCRIPT_ID_VALID(current_sctx.sc_sid)
&& SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
== SCRIPT_VERSION_VIM9
&& check_vim9_unlet(name) == FAIL)
return FAIL;
if (ht != NULL && *varname != NUL)
{
d = get_current_funccal_dict(ht);
if (d == NULL)
{
if (ht == &globvarht)
d = &globvardict;
else if (ht == &compat_hashtab)
d = &vimvardict;
else
{
di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
d = di == NULL ? NULL : di->di_tv.vval.v_dict;
}
if (d == NULL)
{
internal_error("do_unlet()");
return FAIL;
}
}
hi = hash_find(ht, varname);
if (HASHITEM_EMPTY(hi))
hi = find_hi_in_scoped_ht(name, &ht);
if (hi != NULL && !HASHITEM_EMPTY(hi))
{
di = HI2DI(hi);
if (var_check_fixed(di->di_flags, name, FALSE)
|| var_check_ro(di->di_flags, name, FALSE)
|| value_check_lock(d->dv_lock, name, FALSE))
return FAIL;
delete_var(ht, hi);
return OK;
}
}
if (forceit)
return OK;
semsg(_(e_no_such_variable_str), name);
return FAIL;
}
/*
* Lock or unlock variable indicated by "lp".
* "deep" is the levels to go (-1 for unlimited);
* "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
*/
static int
do_lock_var(
lval_T *lp,
char_u *name_end,
exarg_T *eap,
int deep,
void *cookie UNUSED)
{
int lock = eap->cmdidx == CMD_lockvar;
int ret = OK;
int cc;
dictitem_T *di;
if (lp->ll_tv == NULL)
{
cc = *name_end;
*name_end = NUL;
if (*lp->ll_name == '$')
{
semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
ret = FAIL;
}
else
{
// Normal name or expanded name.
di = find_var(lp->ll_name, NULL, TRUE);
if (di == NULL)
{
if (in_vim9script())
semsg(_(e_cannot_find_variable_to_unlock_str),
lp->ll_name);
ret = FAIL;
}
else if ((di->di_flags & DI_FLAGS_FIX)
&& di->di_tv.v_type != VAR_DICT
&& di->di_tv.v_type != VAR_LIST)
{
// For historic reasons this error is not given for a list or
// dict. E.g., the b: dict could be locked/unlocked.
semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
ret = FAIL;
}
else
{
if (lock)
di->di_flags |= DI_FLAGS_LOCK;
else
di->di_flags &= ~DI_FLAGS_LOCK;
if (deep != 0)
item_lock(&di->di_tv, deep, lock, FALSE);
}
}
*name_end = cc;
}
else if (deep == 0)
{
// nothing to do
}
else if (lp->ll_range)
{
listitem_T *li = lp->ll_li;
// (un)lock a range of List items.
while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
{
item_lock(&li->li_tv, deep, lock, FALSE);
li = li->li_next;
++lp->ll_n1;
}
}
else if (lp->ll_list != NULL)
// (un)lock a List item.
item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
else
// (un)lock a Dictionary item.
item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
return ret;
}
/*
* Lock or unlock an item. "deep" is nr of levels to go.
* When "check_refcount" is TRUE do not lock a list or dict with a reference
* count larger than 1.
*/
void
item_lock(typval_T *tv, int deep, int lock, int check_refcount)
{
static int recurse = 0;
list_T *l;
listitem_T *li;
dict_T *d;
blob_T *b;
hashitem_T *hi;
int todo;
if (recurse >= DICT_MAXNEST)
{
emsg(_(e_variable_nested_too_deep_for_unlock));
return;
}
if (deep == 0)
return;
++recurse;
// lock/unlock the item itself
if (lock)
tv->v_lock |= VAR_LOCKED;
else
tv->v_lock &= ~VAR_LOCKED;
switch (tv->v_type)
{
case VAR_UNKNOWN:
case VAR_ANY:
case VAR_VOID:
case VAR_NUMBER:
case VAR_BOOL:
case VAR_STRING:
case VAR_FUNC:
case VAR_PARTIAL:
case VAR_FLOAT:
case VAR_SPECIAL:
case VAR_JOB:
case VAR_CHANNEL:
case VAR_INSTR:
break;
case VAR_BLOB:
if ((b = tv->vval.v_blob) != NULL
&& !(check_refcount && b->bv_refcount > 1))
{
if (lock)
b->bv_lock |= VAR_LOCKED;
else
b->bv_lock &= ~VAR_LOCKED;
}
break;
case VAR_LIST:
if ((l = tv->vval.v_list) != NULL
&& !(check_refcount && l->lv_refcount > 1))
{
if (lock)
l->lv_lock |= VAR_LOCKED;
else
l->lv_lock &= ~VAR_LOCKED;
if (deep < 0 || deep > 1)
{
if (l->lv_first == &range_list_item)
l->lv_lock |= VAR_ITEMS_LOCKED;
else
{
// recursive: lock/unlock the items the List contains
CHECK_LIST_MATERIALIZE(l);
FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
deep - 1, lock, check_refcount);
}
}
}
break;
case VAR_DICT:
if ((d = tv->vval.v_dict) != NULL
&& !(check_refcount && d->dv_refcount > 1))
{
if (lock)
d->dv_lock |= VAR_LOCKED;
else
d->dv_lock &= ~VAR_LOCKED;
if (deep < 0 || deep > 1)
{
// recursive: lock/unlock the items the List contains
todo = (int)d->dv_hashtab.ht_used;
for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
{
if (!HASHITEM_EMPTY(hi))
{
--todo;
item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
check_refcount);
}
}
}
}
}
--recurse;
}
#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
/*
* Delete all "menutrans_" variables.
*/
void
del_menutrans_vars(void)
{
hashitem_T *hi;
int todo;
hash_lock(&globvarht);
todo = (int)globvarht.ht_used;
for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
{
if (!HASHITEM_EMPTY(hi))
{
--todo;
if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
delete_var(&globvarht, hi);
}
}
hash_unlock(&globvarht);
}
#endif
/*
* Local string buffer for the next two functions to store a variable name
* with its prefix. Allocated in cat_prefix_varname(), freed later in
* get_user_var_name().
*/
static char_u *varnamebuf = NULL;
static int varnamebuflen = 0;
/*
* Function to concatenate a prefix and a variable name.
*/
char_u *
cat_prefix_varname(int prefix, char_u *name)
{
int len;
len = (int)STRLEN(name) + 3;
if (len > varnamebuflen)
{
vim_free(varnamebuf);
len += 10; // some additional space
varnamebuf = alloc(len);
if (varnamebuf == NULL)
{
varnamebuflen = 0;
return NULL;
}
varnamebuflen = len;
}
*varnamebuf = prefix;
varnamebuf[1] = ':';
STRCPY(varnamebuf + 2, name);
return varnamebuf;
}
/*
* Function given to ExpandGeneric() to obtain the list of user defined
* (global/buffer/window/built-in) variable names.
*/
char_u *
get_user_var_name(expand_T *xp, int idx)
{
static long_u gdone;
static long_u bdone;
static long_u wdone;
static long_u tdone;
static int vidx;
static hashitem_T *hi;
hashtab_T *ht;
if (idx == 0)
{
gdone = bdone = wdone = vidx = 0;
tdone = 0;
}
// Global variables
if (gdone < globvarht.ht_used)
{
if (gdone++ == 0)
hi = globvarht.ht_array;
else
++hi;
while (HASHITEM_EMPTY(hi))
++hi;
if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
return cat_prefix_varname('g', hi->hi_key);
return hi->hi_key;
}
// b: variables
ht =
#ifdef FEAT_CMDWIN
// In cmdwin, the alternative buffer should be used.
is_in_cmdwin() ? &prevwin->w_buffer->b_vars->dv_hashtab :
#endif
&curbuf->b_vars->dv_hashtab;
if (bdone < ht->ht_used)
{
if (bdone++ == 0)
hi = ht->ht_array;
else
++hi;
while (HASHITEM_EMPTY(hi))
++hi;
return cat_prefix_varname('b', hi->hi_key);
}
// w: variables
ht =
#ifdef FEAT_CMDWIN
// In cmdwin, the alternative window should be used.
is_in_cmdwin() ? &prevwin->w_vars->dv_hashtab :
#endif
&curwin->w_vars->dv_hashtab;
if (wdone < ht->ht_used)
{
if (wdone++ == 0)
hi = ht->ht_array;
else
++hi;
while (HASHITEM_EMPTY(hi))
++hi;
return cat_prefix_varname('w', hi->hi_key);
}
// t: variables
ht = &curtab->tp_vars->dv_hashtab;
if (tdone < ht->ht_used)
{
if (tdone++ == 0)
hi = ht->ht_array;
else
++hi;
while (HASHITEM_EMPTY(hi))
++hi;
return cat_prefix_varname('t', hi->hi_key);
}
// v: variables
if (vidx < VV_LEN)
return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
VIM_CLEAR(varnamebuf);
varnamebuflen = 0;
return NULL;
}
char *
get_var_special_name(int nr)
{
switch (nr)
{
case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
case VVAL_NONE: return "v:none";
}
internal_error("get_var_special_name()");
return "42";
}
/*
* Returns the global variable dictionary
*/
dict_T *
get_globvar_dict(void)
{
return &globvardict;
}
/*
* Returns the global variable hash table
*/
hashtab_T *
get_globvar_ht(void)
{
return &globvarht;
}
/*
* Returns the v: variable dictionary
*/
dict_T *
get_vimvar_dict(void)
{
return &vimvardict;
}
/*
* Returns the index of a v:variable. Negative if not found.
* Returns DI_ flags in "di_flags".
*/
int
find_vim_var(char_u *name, int *di_flags)
{
dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
struct vimvar *vv;
if (di == NULL)
return -1;
*di_flags = di->di_flags;
vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
return (int)(vv - vimvars);
}
/*
* Set type of v: variable to "type".
*/
void
set_vim_var_type(int idx, vartype_T type)
{
vimvars[idx].vv_tv_type = type;
}
/*
* Set number v: variable to "val".
* Note that this does not set the type, use set_vim_var_type() for that.
*/
void
set_vim_var_nr(int idx, varnumber_T val)
{
vimvars[idx].vv_nr = val;
}
char *
get_vim_var_name(int idx)
{
return vimvars[idx].vv_name;
}
/*
* Get typval_T v: variable value.
*/
typval_T *
get_vim_var_tv(int idx)
{
return &vimvars[idx].vv_tv;
}
type_T *
get_vim_var_type(int idx, garray_T *type_list)
{
if (vimvars[idx].vv_type != NULL)
return vimvars[idx].vv_type;
return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
}
/*
* Set v: variable to "tv". Only accepts the same type.
* Takes over the value of "tv".
*/
int
set_vim_var_tv(int idx, typval_T *tv)
{
if (vimvars[idx].vv_tv_type != tv->v_type)
{
emsg(_(e_type_mismatch_for_v_variable));
clear_tv(tv);
return FAIL;
}
// VV_RO is also checked when compiling, but let's check here as well.
if (vimvars[idx].vv_flags & VV_RO)
{
semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
return FAIL;
}
if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
{
semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
return FAIL;
}
clear_tv(&vimvars[idx].vv_di.di_tv);
vimvars[idx].vv_di.di_tv = *tv;
return OK;
}
/*
* Get number v: variable value.
*/
varnumber_T
get_vim_var_nr(int idx)
{
return vimvars[idx].vv_nr;
}
/*
* Get string v: variable value. Uses a static buffer, can only be used once.
* If the String variable has never been set, return an empty string.
* Never returns NULL;
*/
char_u *
get_vim_var_str(int idx)
{
return tv_get_string(&vimvars[idx].vv_tv);
}
/*
* Get List v: variable value. Caller must take care of reference count when
* needed.
*/
list_T *
get_vim_var_list(int idx)
{
return vimvars[idx].vv_list;
}
/*
* Get Dict v: variable value. Caller must take care of reference count when
* needed.
*/
dict_T *
get_vim_var_dict(int idx)
{
return vimvars[idx].vv_dict;
}
/*
* Set v:char to character "c".
*/
void
set_vim_var_char(int c)
{
char_u buf[MB_MAXBYTES + 1];
if (has_mbyte)
buf[(*mb_char2bytes)(c, buf)] = NUL;
else
{
buf[0] = c;
buf[1] = NUL;
}
set_vim_var_string(VV_CHAR, buf, -1);
}
/*
* Set v:count to "count" and v:count1 to "count1".
* When "set_prevcount" is TRUE first set v:prevcount from v:count.
*/
void
set_vcount(
long count,
long count1,
int set_prevcount)
{
if (set_prevcount)
vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
vimvars[VV_COUNT].vv_nr = count;
vimvars[VV_COUNT1].vv_nr = count1;
}
/*
* Save variables that might be changed as a side effect. Used when executing
* a timer callback.
*/
void
save_vimvars(vimvars_save_T *vvsave)
{
vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
}
/*
* Restore variables saved by save_vimvars().
*/
void
restore_vimvars(vimvars_save_T *vvsave)
{
vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
}
/*
* Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
* value.
*/
void
set_vim_var_string(
int idx,
char_u *val,
int len) // length of "val" to use or -1 (whole string)
{
clear_tv(&vimvars[idx].vv_di.di_tv);
vimvars[idx].vv_tv_type = VAR_STRING;
if (val == NULL)
vimvars[idx].vv_str = NULL;
else if (len == -1)
vimvars[idx].vv_str = vim_strsave(val);
else
vimvars[idx].vv_str = vim_strnsave(val, len);
}
/*
* Set List v: variable to "val".
*/
void
set_vim_var_list(int idx, list_T *val)
{
clear_tv(&vimvars[idx].vv_di.di_tv);
vimvars[idx].vv_tv_type = VAR_LIST;
vimvars[idx].vv_list = val;
if (val != NULL)
++val->lv_refcount;
}
/*
* Set Dictionary v: variable to "val".
*/
void
set_vim_var_dict(int idx, dict_T *val)
{
clear_tv(&vimvars[idx].vv_di.di_tv);
vimvars[idx].vv_tv_type = VAR_DICT;
vimvars[idx].vv_dict = val;
if (val != NULL)
{
++val->dv_refcount;
dict_set_items_ro(val);
}
}
/*
* Set the v:argv list.
*/
void
set_argv_var(char **argv, int argc)
{
list_T *l = list_alloc();
int i;
if (l == NULL)
getout(1);
l->lv_lock = VAR_FIXED;
for (i = 0; i < argc; ++i)
{
if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
getout(1);
l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
}
set_vim_var_list(VV_ARGV, l);
}
/*
* Reset v:register, taking the 'clipboard' setting into account.
*/
void
reset_reg_var(void)
{
int regname = 0;
// Adjust the register according to 'clipboard', so that when
// "unnamed" is present it becomes '*' or '+' instead of '"'.
#ifdef FEAT_CLIPBOARD
adjust_clip_reg(®name);
#endif
set_reg_var(regname);
}
/*
* Set v:register if needed.
*/
void
set_reg_var(int c)
{
char_u regname;
if (c == 0 || c == ' ')
regname = '"';
else
regname = c;
// Avoid free/alloc when the value is already right.
if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
set_vim_var_string(VV_REG, ®name, 1);
}
/*
* Get or set v:exception. If "oldval" == NULL, return the current value.
* Otherwise, restore the value to "oldval" and return NULL.
* Must always be called in pairs to save and restore v:exception! Does not
* take care of memory allocations.
*/
char_u *
v_exception(char_u *oldval)
{
if (oldval == NULL)
return vimvars[VV_EXCEPTION].vv_str;
vimvars[VV_EXCEPTION].vv_str = oldval;
return NULL;
}
/*
* Get or set v:throwpoint. If "oldval" == NULL, return the current value.
* Otherwise, restore the value to "oldval" and return NULL.
* Must always be called in pairs to save and restore v:throwpoint! Does not
* take care of memory allocations.
*/
char_u *
v_throwpoint(char_u *oldval)
{
if (oldval == NULL)
return vimvars[VV_THROWPOINT].vv_str;
vimvars[VV_THROWPOINT].vv_str = oldval;
return NULL;
}
/*
* Set v:cmdarg.
* If "eap" != NULL, use "eap" to generate the value and return the old value.
* If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
* Must always be called in pairs!
*/
char_u *
set_cmdarg(exarg_T *eap, char_u *oldarg)
{
char_u *oldval;
char_u *newval;
unsigned len;
oldval = vimvars[VV_CMDARG].vv_str;
if (eap == NULL)
{
vim_free(oldval);
vimvars[VV_CMDARG].vv_str = oldarg;
return NULL;
}
if (eap->force_bin == FORCE_BIN)
len = 6;
else if (eap->force_bin == FORCE_NOBIN)
len = 8;
else
len = 0;
if (eap->read_edit)
len += 7;
if (eap->force_ff != 0)
len += 10; // " ++ff=unix"
if (eap->force_enc != 0)
len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
if (eap->bad_char != 0)
len += 7 + 4; // " ++bad=" + "keep" or "drop"
newval = alloc(len + 1);
if (newval == NULL)
return NULL;
if (eap->force_bin == FORCE_BIN)
sprintf((char *)newval, " ++bin");
else if (eap->force_bin == FORCE_NOBIN)
sprintf((char *)newval, " ++nobin");
else
*newval = NUL;
if (eap->read_edit)
STRCAT(newval, " ++edit");
if (eap->force_ff != 0)
sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
eap->force_ff == 'u' ? "unix"
: eap->force_ff == 'd' ? "dos"
: "mac");
if (eap->force_enc != 0)
sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
eap->cmd + eap->force_enc);
if (eap->bad_char == BAD_KEEP)
STRCPY(newval + STRLEN(newval), " ++bad=keep");
else if (eap->bad_char == BAD_DROP)
STRCPY(newval + STRLEN(newval), " ++bad=drop");
else if (eap->bad_char != 0)
sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
vimvars[VV_CMDARG].vv_str = newval;
return oldval;
}
/*
* Get the value of internal variable "name".
* If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
* imported script ID.
* Return OK or FAIL. If OK is returned "rettv" must be cleared.
*/
int
eval_variable(
char_u *name,
int len, // length of "name"
scid_T sid, // script ID for imported item or zero
typval_T *rettv, // NULL when only checking existence
dictitem_T **dip, // non-NULL when typval's dict item is needed
int flags) // EVAL_VAR_ flags
{
int ret = OK;
typval_T *tv = NULL;
int found = FALSE;
hashtab_T *ht = NULL;
int cc;
type_T *type = NULL;
// truncate the name, so that we can use strcmp()
cc = name[len];
name[len] = NUL;
// Check for local variable when debugging.
if ((tv = lookup_debug_var(name)) == NULL)
{
// Check for user-defined variables.
dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
if (v != NULL)
{
tv = &v->di_tv;
if (dip != NULL)
*dip = v;
}
else
ht = NULL;
}
if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
{
imported_T *import = NULL;
char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
if (sid == 0)
import = find_imported(p, 0, TRUE, NULL);
// imported variable from another script
if (import != NULL || sid != 0)
{
if ((flags & EVAL_VAR_IMPORT) == 0)
{
if (SCRIPT_ID_VALID(sid))
{
ht = &SCRIPT_VARS(sid);
if (ht != NULL)
{
dictitem_T *v = find_var_in_ht(ht, 0, name,
flags & EVAL_VAR_NOAUTOLOAD);
if (v != NULL)
{
tv = &v->di_tv;
if (dip != NULL)
*dip = v;
}
else
ht = NULL;
}
}
else
{
if (flags & EVAL_VAR_VERBOSE)
semsg(_(e_expected_dot_after_name_str), name);
ret = FAIL;
}
}
else
{
if (rettv != NULL)
{
rettv->v_type = VAR_ANY;
rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
}
found = TRUE;
}
}
else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
{
int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
ufunc_T *ufunc = find_func(name, FALSE);
// In Vim9 script we can get a function reference by using the
// function name. For a global non-autoload function "g:" is
// required.
if (ufunc != NULL && (has_g_prefix
|| !func_requires_g_prefix(ufunc)))
{
found = TRUE;
if (rettv != NULL)
{
rettv->v_type = VAR_FUNC;
if (has_g_prefix)
// Keep the "g:", otherwise script-local may be
// assumed.
rettv->vval.v_string = vim_strsave(name);
else
rettv->vval.v_string = vim_strsave(ufunc->uf_name);
if (rettv->vval.v_string != NULL)
func_ref(ufunc->uf_name);
}
}
}
}
if (!found)
{
if (tv == NULL)
{
if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
semsg(_(e_undefined_variable_str), name);
ret = FAIL;
}
else if (rettv != NULL)
{
if (ht != NULL && ht == get_script_local_ht()
&& tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
{
svar_T *sv = find_typval_in_script(tv, 0);
if (sv != NULL)
type = sv->sv_type;
}
// If a list or dict variable wasn't initialized, do it now.
if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
{
tv->vval.v_dict = dict_alloc();
if (tv->vval.v_dict != NULL)
{
++tv->vval.v_dict->dv_refcount;
tv->vval.v_dict->dv_type = alloc_type(type);
}
}
else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
{
tv->vval.v_list = list_alloc();
if (tv->vval.v_list != NULL)
{
++tv->vval.v_list->lv_refcount;
tv->vval.v_list->lv_type = alloc_type(type);
}
}
else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
{
tv->vval.v_blob = blob_alloc();
if (tv->vval.v_blob != NULL)
++tv->vval.v_blob->bv_refcount;
}
copy_tv(tv, rettv);
}
}
name[len] = cc;
return ret;
}
/*
* Check if variable "name[len]" is a local variable or an argument.
* If so, "*eval_lavars_used" is set to TRUE.
*/
void
check_vars(char_u *name, int len)
{
int cc;
char_u *varname;
hashtab_T *ht;
if (eval_lavars_used == NULL)
return;
// truncate the name, so that we can use strcmp()
cc = name[len];
name[len] = NUL;
ht = find_var_ht(name, &varname);
if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
{
if (find_var(name, NULL, TRUE) != NULL)
*eval_lavars_used = TRUE;
}
name[len] = cc;
}
/*
* Find variable "name" in the list of variables.
* Return a pointer to it if found, NULL if not found.
* Careful: "a:0" variables don't have a name.
* When "htp" is not NULL set "htp" to the hashtab_T used.
*/
dictitem_T *
find_var(char_u *name, hashtab_T **htp, int no_autoload)
{
char_u *varname;
hashtab_T *ht;
dictitem_T *ret = NULL;
ht = find_var_ht(name, &varname);
if (htp != NULL)
*htp = ht;
if (ht == NULL)
return NULL;
ret = find_var_in_ht(ht, *name, varname, no_autoload);
if (ret != NULL)
return ret;
// Search in parent scope for lambda
ret = find_var_in_scoped_ht(name, no_autoload);
if (ret != NULL)
return ret;
// in Vim9 script items without a scope can be script-local
if (in_vim9script() && name[0] != NUL && name[1] != ':')
{
ht = get_script_local_ht();
if (ht != NULL)
{
ret = find_var_in_ht(ht, *name, varname, no_autoload);
if (ret != NULL)
{
if (htp != NULL)
*htp = ht;
return ret;
}
}
}
// When using "vim9script autoload" script-local items are prefixed but can
// be used with s:name.
if (SCRIPT_ID_VALID(current_sctx.sc_sid)
&& name[0] == 's' && name[1] == ':')
{
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
if (si->sn_autoload_prefix != NULL)
{
char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
if (auto_name != NULL)
{
ht = &globvarht;
ret = find_var_in_ht(ht, *name, auto_name, TRUE);
vim_free(auto_name);
if (ret != NULL)
{
if (htp != NULL)
*htp = ht;
return ret;
}
}
}
}
return NULL;
}
/*
* Like find_var() but if the name starts with <SNR>99_ then look in the
* referenced script (used for a funcref).
*/
dictitem_T *
find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
{
if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
{
char_u *p = name + 5;
int sid = getdigits(&p);
if (SCRIPT_ID_VALID(sid) && *p == '_')
{
hashtab_T *ht = &SCRIPT_VARS(sid);
if (ht != NULL)
{
dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
if (di != NULL)
{
if (htp != NULL)
*htp = ht;
return di;
}
}
}
}
return find_var(name, htp, no_autoload);
}
/*
* Find variable "varname" in hashtab "ht" with name "htname".
* When "varname" is empty returns curwin/curtab/etc vars dictionary.
* Returns NULL if not found.
*/
dictitem_T *
find_var_in_ht(
hashtab_T *ht,
int htname,
char_u *varname,
int no_autoload)
{
hashitem_T *hi;
if (*varname == NUL)
{
// Must be something like "s:", otherwise "ht" would be NULL.
switch (htname)
{
case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
case 'g': return &globvars_var;
case 'v': return &vimvars_var;
case 'b': return &curbuf->b_bufvar;
case 'w': return &curwin->w_winvar;
case 't': return &curtab->tp_winvar;
case 'l': return get_funccal_local_var();
case 'a': return get_funccal_args_var();
}
return NULL;
}
hi = hash_find(ht, varname);
if (HASHITEM_EMPTY(hi))
{
// For global variables we may try auto-loading the script. If it
// worked find the variable again. Don't auto-load a script if it was
// loaded already, otherwise it would be loaded every time when
// checking if a function name is a Funcref variable.
if (ht == &globvarht && !no_autoload)
{
// Note: script_autoload() may make "hi" invalid. It must either
// be obtained again or not used.
if (!script_autoload(varname, FALSE) || aborting())
return NULL;
hi = hash_find(ht, varname);
}
if (HASHITEM_EMPTY(hi))
return NULL;
}
return HI2DI(hi);
}
/*
* Get the script-local hashtab. NULL if not in a script context.
*/
hashtab_T *
get_script_local_ht(void)
{
scid_T sid = current_sctx.sc_sid;
if (SCRIPT_ID_VALID(sid))
return &SCRIPT_VARS(sid);
return NULL;
}
/*
* Look for "name[len]" in script-local variables and functions.
* When "cmd" is TRUE it must look like a command, a function must be followed
* by "(" or "->".
* Return OK when found, FAIL when not found.
*/
int
lookup_scriptitem(
char_u *name,
size_t len,
int cmd,
cctx_T *dummy UNUSED)
{
hashtab_T *ht = get_script_local_ht();
char_u buffer[30];
char_u *p;
int res;
hashitem_T *hi;
int is_global = FALSE;
char_u *fname = name;
if (ht == NULL)
return FAIL;
if (len < sizeof(buffer) - 1)
{
// avoid an alloc/free for short names
vim_strncpy(buffer, name, len);
p = buffer;
}
else
{
p = vim_strnsave(name, len);
if (p == NULL)
return FAIL;
}
hi = hash_find(ht, p);
res = HASHITEM_EMPTY(hi) ? FAIL : OK;
// if not script-local, then perhaps imported
if (res == FAIL && find_imported(p, 0, FALSE, NULL) != NULL)
res = OK;
if (p != buffer)
vim_free(p);
// Find a function, so that a following "->" works.
// When used as a command require "(" or "->" to follow, "Cmd" is a user
// command while "Cmd()" is a function call.
if (res != OK)
{
p = skipwhite(name + len);
if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
{
// Do not check for an internal function, since it might also be a
// valid command, such as ":split" versus "split()".
// Skip "g:" before a function name.
if (name[0] == 'g' && name[1] == ':')
{
is_global = TRUE;
fname = name + 2;
}
if (find_func(fname, is_global) != NULL)
res = OK;
}
}
return res;
}
/*
* Find the hashtab used for a variable name.
* Return NULL if the name is not valid.
* Set "varname" to the start of name without ':'.
*/
hashtab_T *
find_var_ht(char_u *name, char_u **varname)
{
hashitem_T *hi;
hashtab_T *ht;
if (name[0] == NUL)
return NULL;
if (name[1] != ':')
{
// The name must not start with a colon or #.
if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
return NULL;
*varname = name;
// "version" is "v:version" in all scopes if scriptversion < 3.
// Same for a few other variables marked with VV_COMPAT.
if (in_old_script(3))
{
hi = hash_find(&compat_hashtab, name);
if (!HASHITEM_EMPTY(hi))
return &compat_hashtab;
}
ht = get_funccal_local_ht();
if (ht != NULL)
return ht; // local variable
// In Vim9 script items at the script level are script-local, except
// for autoload names.
if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
{
ht = get_script_local_ht();
if (ht != NULL)
return ht;
}
return &globvarht; // global variable
}
*varname = name + 2;
if (*name == 'g') // global variable
return &globvarht;
// There must be no ':' or '#' in the rest of the name, unless g: is used
if (vim_strchr(name + 2, ':') != NULL
|| vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
return NULL;
if (*name == 'b') // buffer variable
return &curbuf->b_vars->dv_hashtab;
if (*name == 'w') // window variable
return &curwin->w_vars->dv_hashtab;
if (*name == 't') // tab page variable
return &curtab->tp_vars->dv_hashtab;
if (*name == 'v') // v: variable
return &vimvarht;
if (get_current_funccal() != NULL
&& get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
{
// a: and l: are only used in functions defined with ":function"
if (*name == 'a') // a: function argument
return get_funccal_args_ht();
if (*name == 'l') // l: local function variable
return get_funccal_local_ht();
}
if (*name == 's') // script variable
{
ht = get_script_local_ht();
if (ht != NULL)
return ht;
}
return NULL;
}
/*
* Get the string value of a (global/local) variable.
* Note: see tv_get_string() for how long the pointer remains valid.
* Returns NULL when it doesn't exist.
*/
char_u *
get_var_value(char_u *name)
{
dictitem_T *v;
v = find_var(name, NULL, FALSE);
if (v == NULL)
return NULL;
return tv_get_string(&v->di_tv);
}
/*
* Allocate a new hashtab for a sourced script. It will be used while
* sourcing this script and when executing functions defined in the script.
*/
void
new_script_vars(scid_T id)
{
scriptvar_T *sv;
sv = ALLOC_CLEAR_ONE(scriptvar_T);
if (sv == NULL)
return;
init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
SCRIPT_ITEM(id)->sn_vars = sv;
}
/*
* Initialize dictionary "dict" as a scope and set variable "dict_var" to
* point to it.
*/
void
init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
{
hash_init(&dict->dv_hashtab);
dict->dv_lock = 0;
dict->dv_scope = scope;
dict->dv_refcount = DO_NOT_FREE_CNT;
dict->dv_copyID = 0;
dict_var->di_tv.vval.v_dict = dict;
dict_var->di_tv.v_type = VAR_DICT;
dict_var->di_tv.v_lock = VAR_FIXED;
dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
dict_var->di_key[0] = NUL;
}
/*
* Unreference a dictionary initialized by init_var_dict().
*/
void
unref_var_dict(dict_T *dict)
{
// Now the dict needs to be freed if no one else is using it, go back to
// normal reference counting.
dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
dict_unref(dict);
}
/*
* Clean up a list of internal variables.
* Frees all allocated variables and the value they contain.
* Clears hashtab "ht", does not free it.
*/
void
vars_clear(hashtab_T *ht)
{
vars_clear_ext(ht, TRUE);
}
/*
* Like vars_clear(), but only free the value if "free_val" is TRUE.
*/
void
vars_clear_ext(hashtab_T *ht, int free_val)
{
int todo;
hashitem_T *hi;
dictitem_T *v;
hash_lock(ht);
todo = (int)ht->ht_used;
for (hi = ht->ht_array; todo > 0; ++hi)
{
if (!HASHITEM_EMPTY(hi))
{
--todo;
// Free the variable. Don't remove it from the hashtab,
// ht_array might change then. hash_clear() takes care of it
// later.
v = HI2DI(hi);
if (free_val)
clear_tv(&v->di_tv);
if (v->di_flags & DI_FLAGS_ALLOC)
vim_free(v);
}
}
hash_clear(ht);
hash_init(ht);
}
/*
* Delete a variable from hashtab "ht" at item "hi".
* Clear the variable value and free the dictitem.
*/
void
delete_var(hashtab_T *ht, hashitem_T *hi)
{
dictitem_T *di = HI2DI(hi);
hash_remove(ht, hi);
clear_tv(&di->di_tv);
vim_free(di);
}
/*
* List the value of one internal variable.
*/
static void
list_one_var(dictitem_T *v, char *prefix, int *first)
{
char_u *tofree;
char_u *s;
char_u numbuf[NUMBUFLEN];
s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
s == NULL ? (char_u *)"" : s, first);
vim_free(tofree);
}
static void
list_one_var_a(
char *prefix,
char_u *name,
int type,
char_u *string,
int *first) // when TRUE clear rest of screen and set to FALSE
{
// don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
msg_start();
msg_puts(prefix);
if (name != NULL) // "a:" vars don't have a name stored
msg_puts((char *)name);
msg_putchar(' ');
msg_advance(22);
if (type == VAR_NUMBER)
msg_putchar('#');
else if (type == VAR_FUNC || type == VAR_PARTIAL)
msg_putchar('*');
else if (type == VAR_LIST)
{
msg_putchar('[');
if (*string == '[')
++string;
}
else if (type == VAR_DICT)
{
msg_putchar('{');
if (*string == '{')
++string;
}
else
msg_putchar(' ');
msg_outtrans(string);
if (type == VAR_FUNC || type == VAR_PARTIAL)
msg_puts("()");
if (*first)
{
msg_clr_eos();
*first = FALSE;
}
}
/*
* Set variable "name" to value in "tv".
* If the variable already exists, the value is updated.
* Otherwise the variable is created.
*/
void
set_var(
char_u *name,
typval_T *tv,
int copy) // make copy of value in "tv"
{
set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
}
/*
* Set variable "name" to value in "tv_arg".
* When "sid" is non-zero "name" is in the script with this ID.
* If the variable already exists and "is_const" is FALSE the value is updated.
* Otherwise the variable is created.
*/
void
set_var_const(
char_u *name,
scid_T sid,
type_T *type_arg,
typval_T *tv_arg,
int copy, // make copy of value in "tv"
int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
int var_idx) // index for ":let [a, b] = list"
{
typval_T *tv = tv_arg;
type_T *type = type_arg;
typval_T bool_tv;
dictitem_T *di;
typval_T *dest_tv = NULL;
char_u *varname;
char_u *name_tofree = NULL;
hashtab_T *ht = NULL;
int is_script_local;
int vim9script = in_vim9script();
int var_in_vim9script;
int var_in_autoload = FALSE;
int flags = flags_arg;
int free_tv_arg = !copy; // free tv_arg if not used
if (sid != 0)
{
if (SCRIPT_ID_VALID(sid))
ht = &SCRIPT_VARS(sid);
varname = name;
}
else
{
scriptitem_T *si;
if (in_vim9script() && is_export
&& SCRIPT_ID_VALID(current_sctx.sc_sid)
&& (si = SCRIPT_ITEM(current_sctx.sc_sid))
->sn_autoload_prefix != NULL)
{
// In a vim9 autoload script an exported variable is put in the
// global namespace with the autoload prefix.
var_in_autoload = TRUE;
varname = concat_str(si->sn_autoload_prefix, name);
if (varname == NULL)
goto failed;
name_tofree = varname;
ht = &globvarht;
}
else
ht = find_var_ht(name, &varname);
}
if (ht == NULL || *varname == NUL)
{
semsg(_(e_illegal_variable_name_str), name);
goto failed;
}
is_script_local = ht == get_script_local_ht() || sid != 0 || var_in_autoload;
if (vim9script
&& !is_script_local
&& (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
&& (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
&& name[1] == ':')
{
vim9_declare_error(name);
goto failed;
}
if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
&& vim_strchr((char_u *)"gwbt", name[0]) != NULL)
// Do not make g:var, w:var, b:var or t:var final.
flags &= ~ASSIGN_FINAL;
var_in_vim9script = is_script_local && current_script_is_vim9();
if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
{
// For "[a, _] = list" the underscore is ignored.
if ((flags & ASSIGN_UNPACK) == 0)
emsg(_(e_cannot_use_underscore_here));
goto failed;
}
di = find_var_in_ht(ht, 0, varname, TRUE);
if (di == NULL && var_in_vim9script)
{
imported_T *import = find_imported(varname, 0, FALSE, NULL);
if (import != NULL)
{
// imported name space cannot be used
if ((flags & ASSIGN_NO_DECL) == 0)
{
semsg(_(e_redefining_imported_item_str), name);
goto failed;
}
semsg(_(e_cannot_use_str_itself_it_is_imported), name);
goto failed;
}
}
if (dest_tv == NULL)
{
// Search in parent scope which is possible to reference from lambda
if (di == NULL)
di = find_var_in_scoped_ht(name, TRUE);
if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
&& var_wrong_func_name(name, di == NULL))
goto failed;
if (need_convert_to_bool(type, tv))
{
// Destination is a bool and the value is not, but it can be
// converted.
CLEAR_FIELD(bool_tv);
bool_tv.v_type = VAR_BOOL;
bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
tv = &bool_tv;
}
if (di != NULL)
{
// Item already exists. Allowed to replace when reloading.
if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
{
if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
&& (flags & ASSIGN_FOR_LOOP) == 0)
{
emsg(_(e_cannot_modify_existing_variable));
goto failed;
}
if (is_script_local && vim9script
&& (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
{
semsg(_(e_redefining_script_item_str), name);
goto failed;
}
if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
{
where_T where = WHERE_INIT;
svar_T *sv = find_typval_in_script(&di->di_tv, sid);
if (sv != NULL)
{
// check the type and adjust to bool if needed
where.wt_index = var_idx;
where.wt_variable = TRUE;
if (check_script_var_type(sv, tv, name, where) == FAIL)
goto failed;
if (type == NULL)
type = sv->sv_type;
}
}
if ((flags & ASSIGN_FOR_LOOP) == 0
&& var_check_permission(di, name) == FAIL)
goto failed;
}
else
{
// can only redefine once
di->di_flags &= ~DI_FLAGS_RELOAD;
// A Vim9 script-local variable is also present in sn_all_vars
// and sn_var_vals. It may set "type" from "tv".
if (var_in_vim9script || var_in_autoload)
update_vim9_script_var(FALSE, di,
var_in_autoload ? name : di->di_key, flags,
tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
}
// existing variable, need to clear the value
// Handle setting internal di: variables separately where needed to
// prevent changing the type.
if (ht == &vimvarht)
{
if (di->di_tv.v_type == VAR_STRING)
{
VIM_CLEAR(di->di_tv.vval.v_string);
if (copy || tv->v_type != VAR_STRING)
{
char_u *val = tv_get_string(tv);
// Careful: when assigning to v:errmsg and
// tv_get_string() causes an error message the variable
// will already be set.
if (di->di_tv.vval.v_string == NULL)
di->di_tv.vval.v_string = vim_strsave(val);
}
else
{
// Take over the string to avoid an extra alloc/free.
di->di_tv.vval.v_string = tv->vval.v_string;
tv->vval.v_string = NULL;
}
goto failed;
}
else if (di->di_tv.v_type == VAR_NUMBER)
{
di->di_tv.vval.v_number = tv_get_number(tv);
if (STRCMP(varname, "searchforward") == 0)
set_search_direction(di->di_tv.vval.v_number
? '/' : '?');
#ifdef FEAT_SEARCH_EXTRA
else if (STRCMP(varname, "hlsearch") == 0)
{
no_hlsearch = !di->di_tv.vval.v_number;
redraw_all_later(SOME_VALID);
}
#endif
goto failed;
}
else if (di->di_tv.v_type != tv->v_type)
{
semsg(_(e_setting_str_to_value_with_wrong_type), name);
goto failed;
}
}
clear_tv(&di->di_tv);
}
else
{
// Item not found, check if a function already exists.
if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
&& lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
{
semsg(_(e_redefining_script_item_str), name);
goto failed;
}
// add a new variable
if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
{
semsg(_(e_unknown_variable_str), name);
goto failed;
}
// Can't add "v:" or "a:" variable.
if (ht == &vimvarht || ht == get_funccal_args_ht())
{
semsg(_(e_illegal_variable_name_str), name);
goto failed;
}
// Make sure the variable name is valid. In Vim9 script an
// autoload variable must be prefixed with "g:" unless in an
// autoload script.
if (!valid_varname(varname, -1, !vim9script
|| STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
goto failed;
di = alloc(sizeof(dictitem_T) + STRLEN(varname));
if (di == NULL)
goto failed;
STRCPY(di->di_key, varname);
if (hash_add(ht, DI2HIKEY(di)) == FAIL)
{
vim_free(di);
goto failed;
}
di->di_flags = DI_FLAGS_ALLOC;
if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
di->di_flags |= DI_FLAGS_LOCK;
// A Vim9 script-local variable is also added to sn_all_vars and
// sn_var_vals. It may set "type" from "tv".
if (var_in_vim9script || var_in_autoload)
update_vim9_script_var(TRUE, di,
var_in_autoload ? name : di->di_key, flags,
tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
}
dest_tv = &di->di_tv;
}
if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
copy_tv(tv, dest_tv);
else
{
*dest_tv = *tv;
dest_tv->v_lock = 0;
init_tv(tv);
}
free_tv_arg = FALSE;
if (vim9script && type != NULL)
{
if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
{
if (dest_tv->vval.v_dict->dv_type != type)
{
free_type(dest_tv->vval.v_dict->dv_type);
dest_tv->vval.v_dict->dv_type = alloc_type(type);
}
}
else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
{
if (dest_tv->vval.v_list->lv_type != type)
{
free_type(dest_tv->vval.v_list->lv_type);
dest_tv->vval.v_list->lv_type = alloc_type(type);
}
}
}
// ":const var = value" locks the value
// ":final var = value" locks "var"
if (flags & ASSIGN_CONST)
// Like :lockvar! name: lock the value and what it contains, but only
// if the reference count is up to one. That locks only literal
// values.
item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
failed:
vim_free(name_tofree);
if (free_tv_arg)
clear_tv(tv_arg);
}
/*
* Check in this order for backwards compatibility:
* - Whether the variable is read-only
* - Whether the variable value is locked
* - Whether the variable is locked
*/
int
var_check_permission(dictitem_T *di, char_u *name)
{
if (var_check_ro(di->di_flags, name, FALSE)
|| value_check_lock(di->di_tv.v_lock, name, FALSE)
|| var_check_lock(di->di_flags, name, FALSE))
return FAIL;
return OK;
}
/*
* Return TRUE if di_flags "flags" indicates variable "name" is read-only.
* Also give an error message.
*/
int
var_check_ro(int flags, char_u *name, int use_gettext)
{
if (flags & DI_FLAGS_RO)
{
if (name == NULL)
emsg(_(e_cannot_change_readonly_variable));
else
semsg(_(e_cannot_change_readonly_variable_str),
use_gettext ? (char_u *)_(name) : name);
return TRUE;
}
if ((flags & DI_FLAGS_RO_SBX) && sandbox)
{
if (name == NULL)
emsg(_(e_cannot_set_variable_in_sandbox));
else
semsg(_(e_cannot_set_variable_in_sandbox_str),
use_gettext ? (char_u *)_(name) : name);
return TRUE;
}
return FALSE;
}
/*
* Return TRUE if di_flags "flags" indicates variable "name" is locked.
* Also give an error message.
*/
int
var_check_lock(int flags, char_u *name, int use_gettext)
{
if (flags & DI_FLAGS_LOCK)
{
semsg(_(e_variable_is_locked_str),
use_gettext ? (char_u *)_(name) : name);
return TRUE;
}
return FALSE;
}
/*
* Return TRUE if di_flags "flags" indicates variable "name" is fixed.
* Also give an error message.
*/
int
var_check_fixed(int flags, char_u *name, int use_gettext)
{
if (flags & DI_FLAGS_FIX)
{
if (name == NULL)
emsg(_(e_cannot_delete_variable));
else
semsg(_(e_cannot_delete_variable_str),
use_gettext ? (char_u *)_(name) : name);
return TRUE;
}
return FALSE;
}
/*
* Check if a funcref is assigned to a valid variable name.
* Return TRUE and give an error if not.
*/
int
var_wrong_func_name(
char_u *name, // points to start of variable name
int new_var) // TRUE when creating the variable
{
// Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
// the name can be used without the s: prefix.
if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
|| (!in_vim9script() && name[0] == 's')) && name[1] == ':')
&& !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
? name[2] : name[0]))
{
semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
return TRUE;
}
// Don't allow hiding a function. When "v" is not NULL we might be
// assigning another function to the same var, the type is checked
// below.
if (new_var && function_exists(name, FALSE))
{
semsg(_(e_variable_name_conflicts_with_existing_function_str),
name);
return TRUE;
}
return FALSE;
}
/*
* Return TRUE if "flags" indicates variable "name" has a locked (immutable)
* value. Also give an error message, using "name" or _("name") when
* "use_gettext" is TRUE.
*/
int
value_check_lock(int lock, char_u *name, int use_gettext)
{
if (lock & VAR_LOCKED)
{
if (name == NULL)
emsg(_(e_value_is_locked));
else
semsg(_(e_value_is_locked_str),
use_gettext ? (char_u *)_(name) : name);
return TRUE;
}
if (lock & VAR_FIXED)
{
if (name == NULL)
emsg(_(e_cannot_change_value));
else
semsg(_(e_cannot_change_value_of_str),
use_gettext ? (char_u *)_(name) : name);
return TRUE;
}
return FALSE;
}
/*
* Check if a variable name is valid. When "autoload" is true "#" is allowed.
* If "len" is -1 use all of "varname", otherwise up to "varname[len]".
* Return FALSE and give an error if not.
*/
int
valid_varname(char_u *varname, int len, int autoload)
{
char_u *p;
for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
&& !(autoload && *p == AUTOLOAD_CHAR))
{
semsg(_(e_illegal_variable_name_str), varname);
return FALSE;
}
return TRUE;
}
/*
* getwinvar() and gettabwinvar()
*/
static void
getwinvar(
typval_T *argvars,
typval_T *rettv,
int off) // 1 for gettabwinvar()
{
win_T *win;
char_u *varname;
dictitem_T *v;
tabpage_T *tp = NULL;
int done = FALSE;
switchwin_T switchwin;
int need_switch_win;
if (off == 1)
tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
else
tp = curtab;
win = find_win_by_nr(&argvars[off], tp);
varname = tv_get_string_chk(&argvars[off + 1]);
++emsg_off;
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
if (win != NULL && varname != NULL)
{
// Set curwin to be our win, temporarily. Also set the tabpage,
// otherwise the window is not valid. Only do this when needed,
// autocommands get blocked.
need_switch_win = !(tp == curtab && win == curwin);
if (!need_switch_win
|| switch_win(&switchwin, win, tp, TRUE) == OK)
{
if (*varname == '&')
{
if (varname[1] == NUL)
{
// get all window-local options in a dict
dict_T *opts = get_winbuf_options(FALSE);
if (opts != NULL)
{
rettv_dict_set(rettv, opts);
done = TRUE;
}
}
else if (eval_option(&varname, rettv, 1) == OK)
// window-local-option
done = TRUE;
}
else
{
// Look up the variable.
// Let getwinvar({nr}, "") return the "w:" dictionary.
v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
varname, FALSE);
if (v != NULL)
{
copy_tv(&v->di_tv, rettv);
done = TRUE;
}
}
}
if (need_switch_win)
// restore previous notion of curwin
restore_win(&switchwin, TRUE);
}
if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
// use the default return value
copy_tv(&argvars[off + 2], rettv);
--emsg_off;
}
/*
* Set option "varname" to the value of "varp" for the current buffer/window.
*/
static void
set_option_from_tv(char_u *varname, typval_T *varp)
{
long numval = 0;
char_u *strval;
char_u nbuf[NUMBUFLEN];
int error = FALSE;
if (varp->v_type == VAR_BOOL)
{
numval = (long)varp->vval.v_number;
strval = (char_u *)"0"; // avoid using "false"
}
else
{
if (!in_vim9script() || varp->v_type != VAR_STRING)
numval = (long)tv_get_number_chk(varp, &error);
strval = tv_get_string_buf_chk(varp, nbuf);
}
if (!error && strval != NULL)
set_option_value(varname, numval, strval, OPT_LOCAL);
}
/*
* "setwinvar()" and "settabwinvar()" functions
*/
static void
setwinvar(typval_T *argvars, int off)
{
win_T *win;
switchwin_T switchwin;
int need_switch_win;
char_u *varname, *winvarname;
typval_T *varp;
tabpage_T *tp = NULL;
if (check_secure())
return;
if (off == 1)
tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
else
tp = curtab;
win = find_win_by_nr(&argvars[off], tp);
varname = tv_get_string_chk(&argvars[off + 1]);
varp = &argvars[off + 2];
if (win != NULL && varname != NULL && varp != NULL)
{
need_switch_win = !(tp == curtab && win == curwin);
if (!need_switch_win
|| switch_win(&switchwin, win, tp, TRUE) == OK)
{
if (*varname == '&')
set_option_from_tv(varname + 1, varp);
else
{
winvarname = alloc(STRLEN(varname) + 3);
if (winvarname != NULL)
{
STRCPY(winvarname, "w:");
STRCPY(winvarname + 2, varname);
set_var(winvarname, varp, TRUE);
vim_free(winvarname);
}
}
}
if (need_switch_win)
restore_win(&switchwin, TRUE);
}
}
/*
* reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
* v:option_type, and v:option_command.
*/
void
reset_v_option_vars(void)
{
set_vim_var_string(VV_OPTION_NEW, NULL, -1);
set_vim_var_string(VV_OPTION_OLD, NULL, -1);
set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
}
/*
* Add an assert error to v:errors.
*/
void
assert_error(garray_T *gap)
{
struct vimvar *vp = &vimvars[VV_ERRORS];
if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
// Make sure v:errors is a list.
set_vim_var_list(VV_ERRORS, list_alloc());
list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
}
int
var_exists(char_u *var)
{
char_u *arg = var;
char_u *name;
char_u *tofree;
typval_T tv;
int len = 0;
int n = FALSE;
// get_name_len() takes care of expanding curly braces
name = var;
len = get_name_len(&arg, &tofree, TRUE, FALSE);
if (len > 0)
{
if (tofree != NULL)
name = tofree;
n = (eval_variable(name, len, 0, &tv, NULL,
EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
if (n)
{
// handle d.key, l[idx], f(expr)
arg = skipwhite(arg);
n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
FALSE) == OK);
if (n)
clear_tv(&tv);
}
}
if (*arg != NUL)
n = FALSE;
vim_free(tofree);
return n;
}
static lval_T *redir_lval = NULL;
#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
static garray_T redir_ga; // only valid when redir_lval is not NULL
static char_u *redir_endp = NULL;
static char_u *redir_varname = NULL;
int
alloc_redir_lval(void)
{
redir_lval = ALLOC_CLEAR_ONE(lval_T);
if (redir_lval == NULL)
return FAIL;
return OK;
}
void
clear_redir_lval(void)
{
VIM_CLEAR(redir_lval);
}
void
init_redir_ga(void)
{
ga_init2(&redir_ga, sizeof(char), 500);
}
/*
* Start recording command output to a variable
* When "append" is TRUE append to an existing variable.
* Returns OK if successfully completed the setup. FAIL otherwise.
*/
int
var_redir_start(char_u *name, int append)
{
int called_emsg_before;
typval_T tv;
// Catch a bad name early.
if (!eval_isnamec1(*name))
{
emsg(_(e_invalid_argument));
return FAIL;
}
// Make a copy of the name, it is used in redir_lval until redir ends.
redir_varname = vim_strsave(name);
if (redir_varname == NULL)
return FAIL;
if (alloc_redir_lval() == FAIL)
{
var_redir_stop();
return FAIL;
}
// The output is stored in growarray "redir_ga" until redirection ends.
init_redir_ga();
// Parse the variable name (can be a dict or list entry).
redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
FNE_CHECK_START);
if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
{
clear_lval(redir_lval);
if (redir_endp != NULL && *redir_endp != NUL)
// Trailing characters are present after the variable name
semsg(_(e_trailing_characters_str), redir_endp);
else
semsg(_(e_invalid_argument_str), name);
redir_endp = NULL; // don't store a value, only cleanup
var_redir_stop();
return FAIL;
}
// check if we can write to the variable: set it to or append an empty
// string
called_emsg_before = called_emsg;
tv.v_type = VAR_STRING;
tv.vval.v_string = (char_u *)"";
if (append)
set_var_lval(redir_lval, redir_endp, &tv, TRUE,
ASSIGN_NO_DECL, (char_u *)".", 0);
else
set_var_lval(redir_lval, redir_endp, &tv, TRUE,
ASSIGN_NO_DECL, (char_u *)"=", 0);
clear_lval(redir_lval);
if (called_emsg > called_emsg_before)
{
redir_endp = NULL; // don't store a value, only cleanup
var_redir_stop();
return FAIL;
}
return OK;
}
/*
* Append "value[value_len]" to the variable set by var_redir_start().
* The actual appending is postponed until redirection ends, because the value
* appended may in fact be the string we write to, changing it may cause freed
* memory to be used:
* :redir => foo
* :let foo
* :redir END
*/
void
var_redir_str(char_u *value, int value_len)
{
int len;
if (redir_lval == NULL)
return;
if (value_len == -1)
len = (int)STRLEN(value); // Append the entire string
else
len = value_len; // Append only "value_len" characters
if (ga_grow(&redir_ga, len) == OK)
{
mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
redir_ga.ga_len += len;
}
else
var_redir_stop();
}
/*
* Stop redirecting command output to a variable.
* Frees the allocated memory.
*/
void
var_redir_stop(void)
{
typval_T tv;
if (EVALCMD_BUSY)
{
redir_lval = NULL;
return;
}
if (redir_lval != NULL)
{
// If there was no error: assign the text to the variable.
if (redir_endp != NULL)
{
ga_append(&redir_ga, NUL); // Append the trailing NUL.
tv.v_type = VAR_STRING;
tv.vval.v_string = redir_ga.ga_data;
// Call get_lval() again, if it's inside a Dict or List it may
// have changed.
redir_endp = get_lval(redir_varname, NULL, redir_lval,
FALSE, FALSE, 0, FNE_CHECK_START);
if (redir_endp != NULL && redir_lval->ll_name != NULL)
set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
(char_u *)".", 0);
clear_lval(redir_lval);
}
// free the collected output
VIM_CLEAR(redir_ga.ga_data);
VIM_CLEAR(redir_lval);
}
VIM_CLEAR(redir_varname);
}
/*
* Get the collected redirected text and clear redir_ga.
*/
char_u *
get_clear_redir_ga(void)
{
char_u *res;
ga_append(&redir_ga, NUL); // Append the trailing NUL.
res = redir_ga.ga_data;
redir_ga.ga_data = NULL;
return res;
}
/*
* "gettabvar()" function
*/
void
f_gettabvar(typval_T *argvars, typval_T *rettv)
{
switchwin_T switchwin;
tabpage_T *tp;
dictitem_T *v;
char_u *varname;
int done = FALSE;
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
if (in_vim9script()
&& (check_for_number_arg(argvars, 0) == FAIL
|| check_for_string_arg(argvars, 1) == FAIL))
return;
varname = tv_get_string_chk(&argvars[1]);
tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
if (tp != NULL && varname != NULL)
{
// Set tp to be our tabpage, temporarily. Also set the window to the
// first window in the tabpage, otherwise the window is not valid.
if (switch_win(&switchwin,
tp == curtab || tp->tp_firstwin == NULL ? firstwin
: tp->tp_firstwin, tp, TRUE) == OK)
{
// look up the variable
// Let gettabvar({nr}, "") return the "t:" dictionary.
v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
if (v != NULL)
{
copy_tv(&v->di_tv, rettv);
done = TRUE;
}
}
// restore previous notion of curwin
restore_win(&switchwin, TRUE);
}
if (!done && argvars[2].v_type != VAR_UNKNOWN)
copy_tv(&argvars[2], rettv);
}
/*
* "gettabwinvar()" function
*/
void
f_gettabwinvar(typval_T *argvars, typval_T *rettv)
{
if (in_vim9script()
&& (check_for_number_arg(argvars, 0) == FAIL
|| check_for_number_arg(argvars, 1) == FAIL
|| check_for_string_arg(argvars, 2) == FAIL))
return;
getwinvar(argvars, rettv, 1);
}
/*
* "getwinvar()" function
*/
void
f_getwinvar(typval_T *argvars, typval_T *rettv)
{
if (in_vim9script()
&& (check_for_number_arg(argvars, 0) == FAIL
|| check_for_string_arg(argvars, 1) == FAIL))
return;
getwinvar(argvars, rettv, 0);
}
/*
* "getbufvar()" function
*/
void
f_getbufvar(typval_T *argvars, typval_T *rettv)
{
buf_T *buf;
char_u *varname;
dictitem_T *v;
int done = FALSE;
if (in_vim9script()
&& (check_for_buffer_arg(argvars, 0) == FAIL
|| check_for_string_arg(argvars, 1) == FAIL))
return;
varname = tv_get_string_chk(&argvars[1]);
buf = tv_get_buf_from_arg(&argvars[0]);
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
if (buf != NULL && varname != NULL)
{
if (*varname == '&')
{
buf_T *save_curbuf = curbuf;
// set curbuf to be our buf, temporarily
curbuf = buf;
if (varname[1] == NUL)
{
// get all buffer-local options in a dict
dict_T *opts = get_winbuf_options(TRUE);
if (opts != NULL)
{
rettv_dict_set(rettv, opts);
done = TRUE;
}
}
else if (eval_option(&varname, rettv, TRUE) == OK)
// buffer-local-option
done = TRUE;
// restore previous notion of curbuf
curbuf = save_curbuf;
}
else
{
// Look up the variable.
if (*varname == NUL)
// Let getbufvar({nr}, "") return the "b:" dictionary.
v = &buf->b_bufvar;
else
v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
varname, FALSE);
if (v != NULL)
{
copy_tv(&v->di_tv, rettv);
done = TRUE;
}
}
}
if (!done && argvars[2].v_type != VAR_UNKNOWN)
// use the default value
copy_tv(&argvars[2], rettv);
}
/*
* "settabvar()" function
*/
void
f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
{
tabpage_T *save_curtab;
tabpage_T *tp;
char_u *varname, *tabvarname;
typval_T *varp;
if (check_secure())
return;
if (in_vim9script()
&& (check_for_number_arg(argvars, 0) == FAIL
|| check_for_string_arg(argvars, 1) == FAIL))
return;
tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
varname = tv_get_string_chk(&argvars[1]);
varp = &argvars[2];
if (varname != NULL && varp != NULL && tp != NULL)
{
save_curtab = curtab;
goto_tabpage_tp(tp, FALSE, FALSE);
tabvarname = alloc(STRLEN(varname) + 3);
if (tabvarname != NULL)
{
STRCPY(tabvarname, "t:");
STRCPY(tabvarname + 2, varname);
set_var(tabvarname, varp, TRUE);
vim_free(tabvarname);
}
// Restore current tabpage
if (valid_tabpage(save_curtab))
goto_tabpage_tp(save_curtab, FALSE, FALSE);
}
}
/*
* "settabwinvar()" function
*/
void
f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
{
if (in_vim9script()
&& (check_for_number_arg(argvars, 0) == FAIL
|| check_for_number_arg(argvars, 1) == FAIL
|| check_for_string_arg(argvars, 2) == FAIL))
return;
setwinvar(argvars, 1);
}
/*
* "setwinvar()" function
*/
void
f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
{
if (in_vim9script()
&& (check_for_number_arg(argvars, 0) == FAIL
|| check_for_string_arg(argvars, 1) == FAIL))
return;
setwinvar(argvars, 0);
}
/*
* "setbufvar()" function
*/
void
f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
{
buf_T *buf;
char_u *varname, *bufvarname;
typval_T *varp;
if (check_secure())
return;
if (in_vim9script()
&& (check_for_buffer_arg(argvars, 0) == FAIL
|| check_for_string_arg(argvars, 1) == FAIL))
return;
varname = tv_get_string_chk(&argvars[1]);
buf = tv_get_buf_from_arg(&argvars[0]);
varp = &argvars[2];
if (buf != NULL && varname != NULL && varp != NULL)
{
if (*varname == '&')
{
aco_save_T aco;
// set curbuf to be our buf, temporarily
aucmd_prepbuf(&aco, buf);
set_option_from_tv(varname + 1, varp);
// reset notion of buffer
aucmd_restbuf(&aco);
}
else
{
bufvarname = alloc(STRLEN(varname) + 3);
if (bufvarname != NULL)
{
buf_T *save_curbuf = curbuf;
curbuf = buf;
STRCPY(bufvarname, "b:");
STRCPY(bufvarname + 2, varname);
set_var(bufvarname, varp, TRUE);
vim_free(bufvarname);
curbuf = save_curbuf;
}
}
}
}
/*
* Get a callback from "arg". It can be a Funcref or a function name.
* When "arg" is zero return an empty string.
* "cb_name" is not allocated.
* "cb_name" is set to NULL for an invalid argument.
*/
callback_T
get_callback(typval_T *arg)
{
callback_T res;
int r = OK;
res.cb_free_name = FALSE;
if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
{
res.cb_partial = arg->vval.v_partial;
++res.cb_partial->pt_refcount;
res.cb_name = partial_name(res.cb_partial);
}
else
{
res.cb_partial = NULL;
if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
&& isdigit(*arg->vval.v_string))
r = FAIL;
else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
{
if (arg->v_type == VAR_STRING)
{
char_u *name;
name = get_scriptlocal_funcname(arg->vval.v_string);
if (name != NULL)
{
vim_free(arg->vval.v_string);
arg->vval.v_string = name;
}
}
res.cb_name = arg->vval.v_string;
func_ref(res.cb_name);
}
else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
res.cb_name = (char_u *)"";
else
r = FAIL;
if (r == FAIL)
{
emsg(_(e_invalid_callback_argument));
res.cb_name = NULL;
}
}
return res;
}
/*
* Copy a callback into a typval_T.
*/
void
put_callback(callback_T *cb, typval_T *tv)
{
if (cb->cb_partial != NULL)
{
tv->v_type = VAR_PARTIAL;
tv->vval.v_partial = cb->cb_partial;
++tv->vval.v_partial->pt_refcount;
}
else
{
tv->v_type = VAR_FUNC;
tv->vval.v_string = vim_strsave(cb->cb_name);
func_ref(cb->cb_name);
}
}
/*
* Make a copy of "src" into "dest", allocating the function name if needed,
* without incrementing the refcount.
*/
void
set_callback(callback_T *dest, callback_T *src)
{
if (src->cb_partial == NULL)
{
// just a function name, make a copy
dest->cb_name = vim_strsave(src->cb_name);
dest->cb_free_name = TRUE;
}
else
{
// cb_name is a pointer into cb_partial
dest->cb_name = src->cb_name;
dest->cb_free_name = FALSE;
}
dest->cb_partial = src->cb_partial;
}
/*
* Copy callback from "src" to "dest", incrementing the refcounts.
*/
void
copy_callback(callback_T *dest, callback_T *src)
{
dest->cb_partial = src->cb_partial;
if (dest->cb_partial != NULL)
{
dest->cb_name = src->cb_name;
dest->cb_free_name = FALSE;
++dest->cb_partial->pt_refcount;
}
else
{
dest->cb_name = vim_strsave(src->cb_name);
dest->cb_free_name = TRUE;
func_ref(src->cb_name);
}
}
/*
* When a callback refers to an autoload import, change the function name to
* the "path#name" form. Uses the current script context.
* Only works when the name is allocated.
*/
void
expand_autload_callback(callback_T *cb)
{
char_u *name;
char_u *p;
imported_T *import;
if (!in_vim9script() || cb->cb_name == NULL
|| (!cb->cb_free_name
&& (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
return;
if (cb->cb_partial != NULL)
name = cb->cb_partial->pt_name;
else
name = cb->cb_name;
p = vim_strchr(name, '.');
if (p == NULL)
return;
import = find_imported(name, p - name, FALSE, NULL);
if (import != NULL && SCRIPT_ID_VALID(import->imp_sid))
{
scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
if (si->sn_autoload_prefix != NULL)
{
char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
if (newname != NULL)
{
if (cb->cb_partial != NULL)
{
if (cb->cb_name == cb->cb_partial->pt_name)
cb->cb_name = newname;
vim_free(cb->cb_partial->pt_name);
cb->cb_partial->pt_name = newname;
}
else
{
vim_free(cb->cb_name);
cb->cb_name = newname;
}
}
}
}
}
/*
* Unref/free "callback" returned by get_callback() or set_callback().
*/
void
free_callback(callback_T *callback)
{
if (callback->cb_partial != NULL)
{
partial_unref(callback->cb_partial);
callback->cb_partial = NULL;
}
else if (callback->cb_name != NULL)
func_unref(callback->cb_name);
if (callback->cb_free_name)
{
vim_free(callback->cb_name);
callback->cb_free_name = FALSE;
}
callback->cb_name = NULL;
}
#endif // FEAT_EVAL
|