-
Notifications
You must be signed in to change notification settings - Fork 0
/
evalvars.c
4524 lines (4107 loc) · 105 KB
/
evalvars.c
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
/* 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!)
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), VV_COMPAT+VV_RO},
{VV_NAME("count1", VAR_NUMBER), VV_RO},
{VV_NAME("prevcount", VAR_NUMBER), VV_RO},
{VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
{VV_NAME("warningmsg", VAR_STRING), 0},
{VV_NAME("statusmsg", VAR_STRING), 0},
{VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
{VV_NAME("this_session", VAR_STRING), VV_COMPAT},
{VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
{VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
{VV_NAME("termresponse", VAR_STRING), VV_RO},
{VV_NAME("fname", VAR_STRING), VV_RO},
{VV_NAME("lang", VAR_STRING), VV_RO},
{VV_NAME("lc_time", VAR_STRING), VV_RO},
{VV_NAME("ctype", VAR_STRING), VV_RO},
{VV_NAME("charconvert_from", VAR_STRING), VV_RO},
{VV_NAME("charconvert_to", VAR_STRING), VV_RO},
{VV_NAME("fname_in", VAR_STRING), VV_RO},
{VV_NAME("fname_out", VAR_STRING), VV_RO},
{VV_NAME("fname_new", VAR_STRING), VV_RO},
{VV_NAME("fname_diff", VAR_STRING), VV_RO},
{VV_NAME("cmdarg", VAR_STRING), VV_RO},
{VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
{VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
{VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
{VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
{VV_NAME("progname", VAR_STRING), VV_RO},
{VV_NAME("servername", VAR_STRING), VV_RO},
{VV_NAME("dying", VAR_NUMBER), VV_RO},
{VV_NAME("exception", VAR_STRING), VV_RO},
{VV_NAME("throwpoint", VAR_STRING), VV_RO},
{VV_NAME("register", VAR_STRING), VV_RO},
{VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
{VV_NAME("insertmode", VAR_STRING), VV_RO},
{VV_NAME("val", VAR_UNKNOWN), VV_RO},
{VV_NAME("key", VAR_UNKNOWN), VV_RO},
{VV_NAME("profiling", VAR_NUMBER), VV_RO},
{VV_NAME("fcs_reason", VAR_STRING), VV_RO},
{VV_NAME("fcs_choice", VAR_STRING), 0},
{VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
{VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
{VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
{VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
{VV_NAME("beval_col", VAR_NUMBER), VV_RO},
{VV_NAME("beval_text", VAR_STRING), VV_RO},
{VV_NAME("scrollstart", VAR_STRING), 0},
{VV_NAME("swapname", VAR_STRING), VV_RO},
{VV_NAME("swapchoice", VAR_STRING), 0},
{VV_NAME("swapcommand", VAR_STRING), VV_RO},
{VV_NAME("char", VAR_STRING), 0},
{VV_NAME("mouse_win", VAR_NUMBER), 0},
{VV_NAME("mouse_winid", VAR_NUMBER), 0},
{VV_NAME("mouse_lnum", VAR_NUMBER), 0},
{VV_NAME("mouse_col", VAR_NUMBER), 0},
{VV_NAME("operator", VAR_STRING), VV_RO},
{VV_NAME("searchforward", VAR_NUMBER), 0},
{VV_NAME("hlsearch", VAR_NUMBER), 0},
{VV_NAME("oldfiles", VAR_LIST), 0},
{VV_NAME("windowid", VAR_NUMBER), VV_RO},
{VV_NAME("progpath", VAR_STRING), VV_RO},
{VV_NAME("completed_item", VAR_DICT), VV_RO},
{VV_NAME("option_new", VAR_STRING), VV_RO},
{VV_NAME("option_old", VAR_STRING), VV_RO},
{VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
{VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
{VV_NAME("option_command", VAR_STRING), VV_RO},
{VV_NAME("option_type", VAR_STRING), VV_RO},
{VV_NAME("errors", VAR_LIST), 0},
{VV_NAME("false", VAR_BOOL), VV_RO},
{VV_NAME("true", VAR_BOOL), VV_RO},
{VV_NAME("none", VAR_SPECIAL), VV_RO},
{VV_NAME("null", VAR_SPECIAL), VV_RO},
{VV_NAME("numbermax", VAR_NUMBER), VV_RO},
{VV_NAME("numbermin", VAR_NUMBER), VV_RO},
{VV_NAME("numbersize", VAR_NUMBER), VV_RO},
{VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
{VV_NAME("testing", VAR_NUMBER), 0},
{VV_NAME("t_number", VAR_NUMBER), VV_RO},
{VV_NAME("t_string", VAR_NUMBER), VV_RO},
{VV_NAME("t_func", VAR_NUMBER), VV_RO},
{VV_NAME("t_list", VAR_NUMBER), VV_RO},
{VV_NAME("t_dict", VAR_NUMBER), VV_RO},
{VV_NAME("t_float", VAR_NUMBER), VV_RO},
{VV_NAME("t_bool", VAR_NUMBER), VV_RO},
{VV_NAME("t_none", VAR_NUMBER), VV_RO},
{VV_NAME("t_job", VAR_NUMBER), VV_RO},
{VV_NAME("t_channel", VAR_NUMBER), VV_RO},
{VV_NAME("t_blob", VAR_NUMBER), VV_RO},
{VV_NAME("termrfgresp", VAR_STRING), VV_RO},
{VV_NAME("termrbgresp", VAR_STRING), VV_RO},
{VV_NAME("termu7resp", VAR_STRING), VV_RO},
{VV_NAME("termstyleresp", VAR_STRING), VV_RO},
{VV_NAME("termblinkresp", VAR_STRING), VV_RO},
{VV_NAME("event", VAR_DICT), VV_RO},
{VV_NAME("versionlong", VAR_NUMBER), VV_RO},
{VV_NAME("echospace", VAR_NUMBER), VV_RO},
{VV_NAME("argv", VAR_LIST), VV_RO},
{VV_NAME("collate", VAR_STRING), VV_RO},
{VV_NAME("exiting", VAR_SPECIAL), VV_RO},
{VV_NAME("colornames", VAR_DICT), VV_RO},
{VV_NAME("sizeofint", VAR_NUMBER), VV_RO},
{VV_NAME("sizeoflong", VAR_NUMBER), VV_RO},
{VV_NAME("sizeofpointer", VAR_NUMBER), VV_RO},
};
// shorthand
#define vv_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_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_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;
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);
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);
if (err)
return FAIL;
return OK;
}
# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
int
eval_printexpr(char_u *fname, char_u *args)
{
int err = FALSE;
set_vim_var_string(VV_FNAME_IN, fname, -1);
set_vim_var_string(VV_CMDARG, args, -1);
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);
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)
{
int err = FALSE;
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);
(void)eval_to_bool(p_dex, &err, NULL, FALSE);
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);
}
void
eval_patch(
char_u *origfile,
char_u *difffile,
char_u *outfile)
{
int err;
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);
(void)eval_to_bool(p_pex, &err, NULL, FALSE);
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);
}
# 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);
// 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;
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);
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_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_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(_("E991: cannot use =<< 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_arg), p);
return NULL;
}
*p = NUL;
if (!script_get && vim_islower(*marker))
{
emsg(_("E221: 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(_("E172: 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(_("E990: Missing end marker '%s'"), 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_invarg));
else if (expr[0] == '.' && expr[1] == '=')
emsg(_("E985: .= is not supported with script version >= 2"));
else if (!ends_excmd2(eap->cmd, arg))
{
if (vim9script)
{
if (!ends_excmd2(eap->cmd, skipwhite(argend)))
semsg(_(e_trailing_arg), 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_listreq));
return FAIL;
}
i = list_len(l);
if (semicolon == 0 && var_count < i)
{
emsg(_("E687: Less targets than List items"));
return FAIL;
}
if (var_count - semicolon > i)
{
emsg(_("E688: 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;