Mercurial > vim
comparison src/eval.c @ 17885:5e2d8840da11 v8.1.1939
patch 8.1.1939: code for handling v: variables in generic eval file
Commit: https://github.com/vim/vim/commit/e5cdf153bcb348c68011b308c8988cea42d6ddeb
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Aug 29 22:09:46 2019 +0200
patch 8.1.1939: code for handling v: variables in generic eval file
Problem: Code for handling v: variables in generic eval file.
Solution: Move v: variables to evalvars.c. (Yegappan Lakshmanan,
closes #4872)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 29 Aug 2019 22:15:04 +0200 |
parents | d50a5faa75bd |
children | 39ffd167a307 |
comparison
equal
deleted
inserted
replaced
17884:ae47d7c8416c | 17885:5e2d8840da11 |
---|---|
27 #endif | 27 #endif |
28 static char *e_nowhitespace = N_("E274: No white space allowed before parenthesis"); | 28 static char *e_nowhitespace = N_("E274: No white space allowed before parenthesis"); |
29 | 29 |
30 #define NAMESPACE_CHAR (char_u *)"abglstvw" | 30 #define NAMESPACE_CHAR (char_u *)"abglstvw" |
31 | 31 |
32 static dictitem_T globvars_var; /* variable used for g: */ | |
33 | |
34 /* | |
35 * Old Vim variables such as "v:version" are also available without the "v:". | |
36 * Also in functions. We need a special hashtable for them. | |
37 */ | |
38 static hashtab_T compat_hashtab; | |
39 | |
40 /* | 32 /* |
41 * When recursively copying lists and dicts we need to remember which ones we | 33 * When recursively copying lists and dicts we need to remember which ones we |
42 * have done to avoid endless recursiveness. This unique ID is used for that. | 34 * have done to avoid endless recursiveness. This unique ID is used for that. |
43 * The last bit is used for previous_funccal, ignored when comparing. | 35 * The last bit is used for previous_funccal, ignored when comparing. |
44 */ | 36 */ |
45 static int current_copyID = 0; | 37 static int current_copyID = 0; |
46 | |
47 /* | |
48 * Array to hold the hashtab with variables local to each sourced script. | |
49 * Each item holds a variable (nameless) that points to the dict_T. | |
50 */ | |
51 typedef struct | |
52 { | |
53 dictitem_T sv_var; | |
54 dict_T sv_dict; | |
55 } scriptvar_T; | |
56 | |
57 static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL}; | |
58 #define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1]) | |
59 #define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab) | |
60 | 38 |
61 static int echo_attr = 0; /* attributes used for ":echo" */ | 39 static int echo_attr = 0; /* attributes used for ":echo" */ |
62 | 40 |
63 /* The names of packages that once were loaded are remembered. */ | 41 /* The names of packages that once were loaded are remembered. */ |
64 static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL}; | 42 static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL}; |
73 listwatch_T fi_lw; /* keep an eye on the item used. */ | 51 listwatch_T fi_lw; /* keep an eye on the item used. */ |
74 list_T *fi_list; /* list being used */ | 52 list_T *fi_list; /* list being used */ |
75 int fi_bi; /* index of blob */ | 53 int fi_bi; /* index of blob */ |
76 blob_T *fi_blob; /* blob being used */ | 54 blob_T *fi_blob; /* blob being used */ |
77 } forinfo_T; | 55 } forinfo_T; |
78 | |
79 | |
80 /* | |
81 * Array to hold the value of v: variables. | |
82 * The value is in a dictitem, so that it can also be used in the v: scope. | |
83 * The reason to use this table anyway is for very quick access to the | |
84 * variables with the VV_ defines. | |
85 */ | |
86 | |
87 /* values for vv_flags: */ | |
88 #define VV_COMPAT 1 /* compatible, also used without "v:" */ | |
89 #define VV_RO 2 /* read-only */ | |
90 #define VV_RO_SBX 4 /* read-only in the sandbox */ | |
91 | |
92 #define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}} | |
93 | |
94 static struct vimvar | |
95 { | |
96 char *vv_name; /* name of variable, without v: */ | |
97 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */ | |
98 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */ | |
99 } vimvars[VV_LEN] = | |
100 { | |
101 /* | |
102 * The order here must match the VV_ defines in vim.h! | |
103 * Initializing a union does not work, leave tv.vval empty to get zero's. | |
104 */ | |
105 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO}, | |
106 {VV_NAME("count1", VAR_NUMBER), VV_RO}, | |
107 {VV_NAME("prevcount", VAR_NUMBER), VV_RO}, | |
108 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT}, | |
109 {VV_NAME("warningmsg", VAR_STRING), 0}, | |
110 {VV_NAME("statusmsg", VAR_STRING), 0}, | |
111 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO}, | |
112 {VV_NAME("this_session", VAR_STRING), VV_COMPAT}, | |
113 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO}, | |
114 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX}, | |
115 {VV_NAME("termresponse", VAR_STRING), VV_RO}, | |
116 {VV_NAME("fname", VAR_STRING), VV_RO}, | |
117 {VV_NAME("lang", VAR_STRING), VV_RO}, | |
118 {VV_NAME("lc_time", VAR_STRING), VV_RO}, | |
119 {VV_NAME("ctype", VAR_STRING), VV_RO}, | |
120 {VV_NAME("charconvert_from", VAR_STRING), VV_RO}, | |
121 {VV_NAME("charconvert_to", VAR_STRING), VV_RO}, | |
122 {VV_NAME("fname_in", VAR_STRING), VV_RO}, | |
123 {VV_NAME("fname_out", VAR_STRING), VV_RO}, | |
124 {VV_NAME("fname_new", VAR_STRING), VV_RO}, | |
125 {VV_NAME("fname_diff", VAR_STRING), VV_RO}, | |
126 {VV_NAME("cmdarg", VAR_STRING), VV_RO}, | |
127 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX}, | |
128 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX}, | |
129 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX}, | |
130 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX}, | |
131 {VV_NAME("progname", VAR_STRING), VV_RO}, | |
132 {VV_NAME("servername", VAR_STRING), VV_RO}, | |
133 {VV_NAME("dying", VAR_NUMBER), VV_RO}, | |
134 {VV_NAME("exception", VAR_STRING), VV_RO}, | |
135 {VV_NAME("throwpoint", VAR_STRING), VV_RO}, | |
136 {VV_NAME("register", VAR_STRING), VV_RO}, | |
137 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO}, | |
138 {VV_NAME("insertmode", VAR_STRING), VV_RO}, | |
139 {VV_NAME("val", VAR_UNKNOWN), VV_RO}, | |
140 {VV_NAME("key", VAR_UNKNOWN), VV_RO}, | |
141 {VV_NAME("profiling", VAR_NUMBER), VV_RO}, | |
142 {VV_NAME("fcs_reason", VAR_STRING), VV_RO}, | |
143 {VV_NAME("fcs_choice", VAR_STRING), 0}, | |
144 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO}, | |
145 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO}, | |
146 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO}, | |
147 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO}, | |
148 {VV_NAME("beval_col", VAR_NUMBER), VV_RO}, | |
149 {VV_NAME("beval_text", VAR_STRING), VV_RO}, | |
150 {VV_NAME("scrollstart", VAR_STRING), 0}, | |
151 {VV_NAME("swapname", VAR_STRING), VV_RO}, | |
152 {VV_NAME("swapchoice", VAR_STRING), 0}, | |
153 {VV_NAME("swapcommand", VAR_STRING), VV_RO}, | |
154 {VV_NAME("char", VAR_STRING), 0}, | |
155 {VV_NAME("mouse_win", VAR_NUMBER), 0}, | |
156 {VV_NAME("mouse_winid", VAR_NUMBER), 0}, | |
157 {VV_NAME("mouse_lnum", VAR_NUMBER), 0}, | |
158 {VV_NAME("mouse_col", VAR_NUMBER), 0}, | |
159 {VV_NAME("operator", VAR_STRING), VV_RO}, | |
160 {VV_NAME("searchforward", VAR_NUMBER), 0}, | |
161 {VV_NAME("hlsearch", VAR_NUMBER), 0}, | |
162 {VV_NAME("oldfiles", VAR_LIST), 0}, | |
163 {VV_NAME("windowid", VAR_NUMBER), VV_RO}, | |
164 {VV_NAME("progpath", VAR_STRING), VV_RO}, | |
165 {VV_NAME("completed_item", VAR_DICT), VV_RO}, | |
166 {VV_NAME("option_new", VAR_STRING), VV_RO}, | |
167 {VV_NAME("option_old", VAR_STRING), VV_RO}, | |
168 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO}, | |
169 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO}, | |
170 {VV_NAME("option_command", VAR_STRING), VV_RO}, | |
171 {VV_NAME("option_type", VAR_STRING), VV_RO}, | |
172 {VV_NAME("errors", VAR_LIST), 0}, | |
173 {VV_NAME("false", VAR_SPECIAL), VV_RO}, | |
174 {VV_NAME("true", VAR_SPECIAL), VV_RO}, | |
175 {VV_NAME("null", VAR_SPECIAL), VV_RO}, | |
176 {VV_NAME("none", VAR_SPECIAL), VV_RO}, | |
177 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO}, | |
178 {VV_NAME("testing", VAR_NUMBER), 0}, | |
179 {VV_NAME("t_number", VAR_NUMBER), VV_RO}, | |
180 {VV_NAME("t_string", VAR_NUMBER), VV_RO}, | |
181 {VV_NAME("t_func", VAR_NUMBER), VV_RO}, | |
182 {VV_NAME("t_list", VAR_NUMBER), VV_RO}, | |
183 {VV_NAME("t_dict", VAR_NUMBER), VV_RO}, | |
184 {VV_NAME("t_float", VAR_NUMBER), VV_RO}, | |
185 {VV_NAME("t_bool", VAR_NUMBER), VV_RO}, | |
186 {VV_NAME("t_none", VAR_NUMBER), VV_RO}, | |
187 {VV_NAME("t_job", VAR_NUMBER), VV_RO}, | |
188 {VV_NAME("t_channel", VAR_NUMBER), VV_RO}, | |
189 {VV_NAME("t_blob", VAR_NUMBER), VV_RO}, | |
190 {VV_NAME("termrfgresp", VAR_STRING), VV_RO}, | |
191 {VV_NAME("termrbgresp", VAR_STRING), VV_RO}, | |
192 {VV_NAME("termu7resp", VAR_STRING), VV_RO}, | |
193 {VV_NAME("termstyleresp", VAR_STRING), VV_RO}, | |
194 {VV_NAME("termblinkresp", VAR_STRING), VV_RO}, | |
195 {VV_NAME("event", VAR_DICT), VV_RO}, | |
196 {VV_NAME("versionlong", VAR_NUMBER), VV_RO}, | |
197 {VV_NAME("echospace", VAR_NUMBER), VV_RO}, | |
198 }; | |
199 | |
200 /* shorthand */ | |
201 #define vv_type vv_di.di_tv.v_type | |
202 #define vv_nr vv_di.di_tv.vval.v_number | |
203 #define vv_float vv_di.di_tv.vval.v_float | |
204 #define vv_str vv_di.di_tv.vval.v_string | |
205 #define vv_list vv_di.di_tv.vval.v_list | |
206 #define vv_dict vv_di.di_tv.vval.v_dict | |
207 #define vv_blob vv_di.di_tv.vval.v_blob | |
208 #define vv_tv vv_di.di_tv | |
209 | |
210 static dictitem_T vimvars_var; /* variable used for v: */ | |
211 #define vimvarht vimvardict.dv_hashtab | |
212 | 56 |
213 static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op); | 57 static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op); |
214 static int eval2(char_u **arg, typval_T *rettv, int evaluate); | 58 static int eval2(char_u **arg, typval_T *rettv, int evaluate); |
215 static int eval3(char_u **arg, typval_T *rettv, int evaluate); | 59 static int eval3(char_u **arg, typval_T *rettv, int evaluate); |
216 static int eval4(char_u **arg, typval_T *rettv, int evaluate); | 60 static int eval4(char_u **arg, typval_T *rettv, int evaluate); |
222 static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate); | 66 static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate); |
223 static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate); | 67 static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate); |
224 static int free_unref_items(int copyID); | 68 static int free_unref_items(int copyID); |
225 static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate); | 69 static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate); |
226 static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end); | 70 static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end); |
227 static void check_vars(char_u *name, int len); | |
228 static typval_T *alloc_string_tv(char_u *string); | |
229 static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext); | 71 static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext); |
230 | |
231 /* for VIM_VERSION_ defines */ | |
232 #include "version.h" | |
233 | 72 |
234 /* | 73 /* |
235 * Return "n1" divided by "n2", taking care of dividing by zero. | 74 * Return "n1" divided by "n2", taking care of dividing by zero. |
236 */ | 75 */ |
237 varnumber_T | 76 varnumber_T |
262 { | 101 { |
263 // Give an error when n2 is 0? | 102 // Give an error when n2 is 0? |
264 return (n2 == 0) ? 0 : (n1 % n2); | 103 return (n2 == 0) ? 0 : (n1 % n2); |
265 } | 104 } |
266 | 105 |
267 | |
268 #if defined(EBCDIC) || defined(PROTO) | 106 #if defined(EBCDIC) || defined(PROTO) |
269 /* | 107 /* |
270 * Compare struct fst by function name. | 108 * Compare struct fst by function name. |
271 */ | 109 */ |
272 static int | 110 static int |
290 | 128 |
291 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name); | 129 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name); |
292 } | 130 } |
293 #endif | 131 #endif |
294 | 132 |
295 | |
296 /* | 133 /* |
297 * Initialize the global and v: variables. | 134 * Initialize the global and v: variables. |
298 */ | 135 */ |
299 void | 136 void |
300 eval_init(void) | 137 eval_init(void) |
301 { | 138 { |
302 int i; | 139 evalvars_init(); |
303 struct vimvar *p; | |
304 | |
305 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE); | |
306 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE); | |
307 vimvardict.dv_lock = VAR_FIXED; | |
308 hash_init(&compat_hashtab); | |
309 func_init(); | 140 func_init(); |
310 | |
311 for (i = 0; i < VV_LEN; ++i) | |
312 { | |
313 p = &vimvars[i]; | |
314 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN) | |
315 { | |
316 iemsg("INTERNAL: name too long, increase size of dictitem16_T"); | |
317 getout(1); | |
318 } | |
319 STRCPY(p->vv_di.di_key, p->vv_name); | |
320 if (p->vv_flags & VV_RO) | |
321 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; | |
322 else if (p->vv_flags & VV_RO_SBX) | |
323 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX; | |
324 else | |
325 p->vv_di.di_flags = DI_FLAGS_FIX; | |
326 | |
327 /* add to v: scope dict, unless the value is not always available */ | |
328 if (p->vv_type != VAR_UNKNOWN) | |
329 hash_add(&vimvarht, p->vv_di.di_key); | |
330 if (p->vv_flags & VV_COMPAT) | |
331 /* add to compat scope dict */ | |
332 hash_add(&compat_hashtab, p->vv_di.di_key); | |
333 } | |
334 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100; | |
335 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch(); | |
336 | |
337 set_vim_var_nr(VV_SEARCHFORWARD, 1L); | |
338 set_vim_var_nr(VV_HLSEARCH, 1L); | |
339 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED)); | |
340 set_vim_var_list(VV_ERRORS, list_alloc()); | |
341 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED)); | |
342 | |
343 set_vim_var_nr(VV_FALSE, VVAL_FALSE); | |
344 set_vim_var_nr(VV_TRUE, VVAL_TRUE); | |
345 set_vim_var_nr(VV_NONE, VVAL_NONE); | |
346 set_vim_var_nr(VV_NULL, VVAL_NULL); | |
347 | |
348 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER); | |
349 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING); | |
350 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC); | |
351 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST); | |
352 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT); | |
353 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT); | |
354 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL); | |
355 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE); | |
356 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB); | |
357 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL); | |
358 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB); | |
359 | |
360 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1); | |
361 | |
362 set_reg_var(0); /* default for v:register is not 0 but '"' */ | |
363 | 141 |
364 #ifdef EBCDIC | 142 #ifdef EBCDIC |
365 /* | 143 /* |
366 * Sort the function table, to enable binary search. | 144 * Sort the function table, to enable binary search. |
367 */ | 145 */ |
371 | 149 |
372 #if defined(EXITFREE) || defined(PROTO) | 150 #if defined(EXITFREE) || defined(PROTO) |
373 void | 151 void |
374 eval_clear(void) | 152 eval_clear(void) |
375 { | 153 { |
376 int i; | 154 evalvars_clear(); |
377 struct vimvar *p; | |
378 | |
379 for (i = 0; i < VV_LEN; ++i) | |
380 { | |
381 p = &vimvars[i]; | |
382 if (p->vv_di.di_tv.v_type == VAR_STRING) | |
383 VIM_CLEAR(p->vv_str); | |
384 else if (p->vv_di.di_tv.v_type == VAR_LIST) | |
385 { | |
386 list_unref(p->vv_list); | |
387 p->vv_list = NULL; | |
388 } | |
389 } | |
390 hash_clear(&vimvarht); | |
391 hash_init(&vimvarht); /* garbage_collect() will access it */ | |
392 hash_clear(&compat_hashtab); | |
393 | 155 |
394 free_scriptnames(); | 156 free_scriptnames(); |
395 free_locales(); | 157 free_locales(); |
396 | 158 |
397 /* global variables */ | |
398 vars_clear(&globvarht); | |
399 | |
400 /* autoloaded script names */ | 159 /* autoloaded script names */ |
401 ga_clear_strings(&ga_loaded); | 160 ga_clear_strings(&ga_loaded); |
402 | 161 |
403 /* Script-local variables. First clear all the variables and in a second | |
404 * loop free the scriptvar_T, because a variable in one script might hold | |
405 * a reference to the whole scope of another script. */ | |
406 for (i = 1; i <= ga_scripts.ga_len; ++i) | |
407 vars_clear(&SCRIPT_VARS(i)); | |
408 for (i = 1; i <= ga_scripts.ga_len; ++i) | |
409 vim_free(SCRIPT_SV(i)); | |
410 ga_clear(&ga_scripts); | |
411 | |
412 // unreferenced lists and dicts | 162 // unreferenced lists and dicts |
413 (void)garbage_collect(FALSE); | 163 (void)garbage_collect(FALSE); |
414 | 164 |
415 // functions not garbage collected | 165 // functions not garbage collected |
416 free_all_functions(); | 166 free_all_functions(); |
417 } | 167 } |
418 #endif | 168 #endif |
419 | |
420 | |
421 /* | |
422 * Set an internal variable to a string value. Creates the variable if it does | |
423 * not already exist. | |
424 */ | |
425 void | |
426 set_internal_string_var(char_u *name, char_u *value) | |
427 { | |
428 char_u *val; | |
429 typval_T *tvp; | |
430 | |
431 val = vim_strsave(value); | |
432 if (val != NULL) | |
433 { | |
434 tvp = alloc_string_tv(val); | |
435 if (tvp != NULL) | |
436 { | |
437 set_var(name, tvp, FALSE); | |
438 free_tv(tvp); | |
439 } | |
440 } | |
441 } | |
442 | 169 |
443 static lval_T *redir_lval = NULL; | 170 static lval_T *redir_lval = NULL; |
444 #define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval) | 171 #define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval) |
445 static garray_T redir_ga; /* only valid when redir_lval is not NULL */ | 172 static garray_T redir_ga; /* only valid when redir_lval is not NULL */ |
446 static char_u *redir_endp = NULL; | 173 static char_u *redir_endp = NULL; |
942 --emsg_off; | 669 --emsg_off; |
943 | 670 |
944 return retval; | 671 return retval; |
945 } | 672 } |
946 | 673 |
947 /* | |
948 * List Vim variables. | |
949 */ | |
950 void | |
951 list_vim_vars(int *first) | |
952 { | |
953 list_hashtable_vars(&vimvarht, "v:", FALSE, first); | |
954 } | |
955 | |
956 /* | |
957 * List script-local variables, if there is a script. | |
958 */ | |
959 void | |
960 list_script_vars(int *first) | |
961 { | |
962 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len) | |
963 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid), | |
964 "s:", FALSE, first); | |
965 } | |
966 | |
967 int | |
968 is_vimvarht(hashtab_T *ht) | |
969 { | |
970 return ht == &vimvarht; | |
971 } | |
972 | |
973 int | |
974 is_compatht(hashtab_T *ht) | |
975 { | |
976 return ht == &compat_hashtab; | |
977 } | |
978 | |
979 /* | |
980 * Prepare v: variable "idx" to be used. | |
981 * Save the current typeval in "save_tv". | |
982 * When not used yet add the variable to the v: hashtable. | |
983 */ | |
984 void | |
985 prepare_vimvar(int idx, typval_T *save_tv) | |
986 { | |
987 *save_tv = vimvars[idx].vv_tv; | |
988 if (vimvars[idx].vv_type == VAR_UNKNOWN) | |
989 hash_add(&vimvarht, vimvars[idx].vv_di.di_key); | |
990 } | |
991 | |
992 /* | |
993 * Restore v: variable "idx" to typeval "save_tv". | |
994 * When no longer defined, remove the variable from the v: hashtable. | |
995 */ | |
996 void | |
997 restore_vimvar(int idx, typval_T *save_tv) | |
998 { | |
999 hashitem_T *hi; | |
1000 | |
1001 vimvars[idx].vv_tv = *save_tv; | |
1002 if (vimvars[idx].vv_type == VAR_UNKNOWN) | |
1003 { | |
1004 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key); | |
1005 if (HASHITEM_EMPTY(hi)) | |
1006 internal_error("restore_vimvar()"); | |
1007 else | |
1008 hash_remove(&vimvarht, hi); | |
1009 } | |
1010 } | |
1011 | |
1012 #if defined(FEAT_SPELL) || defined(PROTO) | 674 #if defined(FEAT_SPELL) || defined(PROTO) |
1013 /* | 675 /* |
1014 * Evaluate an expression to a list with suggestions. | 676 * Evaluate an expression to a list with suggestions. |
1015 * For the "expr:" part of 'spellsuggest'. | 677 * For the "expr:" part of 'spellsuggest'. |
1016 * Returns NULL when there is an error. | 678 * Returns NULL when there is an error. |
1023 list_T *list = NULL; | 685 list_T *list = NULL; |
1024 char_u *p = skipwhite(expr); | 686 char_u *p = skipwhite(expr); |
1025 | 687 |
1026 /* Set "v:val" to the bad word. */ | 688 /* Set "v:val" to the bad word. */ |
1027 prepare_vimvar(VV_VAL, &save_val); | 689 prepare_vimvar(VV_VAL, &save_val); |
1028 vimvars[VV_VAL].vv_type = VAR_STRING; | 690 set_vim_var_string(VV_VAL, badword, -1); |
1029 vimvars[VV_VAL].vv_str = badword; | |
1030 if (p_verbose == 0) | 691 if (p_verbose == 0) |
1031 ++emsg_off; | 692 ++emsg_off; |
1032 | 693 |
1033 if (eval1(&p, &rettv, TRUE) == OK) | 694 if (eval1(&p, &rettv, TRUE) == OK) |
1034 { | 695 { |
1038 list = rettv.vval.v_list; | 699 list = rettv.vval.v_list; |
1039 } | 700 } |
1040 | 701 |
1041 if (p_verbose == 0) | 702 if (p_verbose == 0) |
1042 --emsg_off; | 703 --emsg_off; |
704 clear_tv(get_vim_var_tv(VV_VAL)); | |
1043 restore_vimvar(VV_VAL, &save_val); | 705 restore_vimvar(VV_VAL, &save_val); |
1044 | 706 |
1045 return list; | 707 return list; |
1046 } | 708 } |
1047 | 709 |
1082 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL) | 744 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL) |
1083 VIM_CLEAR(tv); | 745 VIM_CLEAR(tv); |
1084 | 746 |
1085 return tv; | 747 return tv; |
1086 } | 748 } |
1087 | |
1088 | 749 |
1089 /* | 750 /* |
1090 * Call some Vim script function and return the result in "*rettv". | 751 * Call some Vim script function and return the result in "*rettv". |
1091 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] | 752 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] |
1092 * should have type VAR_UNKNOWN. | 753 * should have type VAR_UNKNOWN. |
1183 return NULL; | 844 return NULL; |
1184 } | 845 } |
1185 | 846 |
1186 return rettv.vval.v_list; | 847 return rettv.vval.v_list; |
1187 } | 848 } |
1188 | |
1189 | 849 |
1190 #ifdef FEAT_FOLDING | 850 #ifdef FEAT_FOLDING |
1191 /* | 851 /* |
1192 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding | 852 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding |
1193 * it in "*cp". Doesn't give error messages. | 853 * it in "*cp". Doesn't give error messages. |
2283 } | 1943 } |
2284 } | 1944 } |
2285 hash_unlock(&globvarht); | 1945 hash_unlock(&globvarht); |
2286 } | 1946 } |
2287 #endif | 1947 #endif |
2288 | |
2289 /* | |
2290 * Local string buffer for the next two functions to store a variable name | |
2291 * with its prefix. Allocated in cat_prefix_varname(), freed later in | |
2292 * get_user_var_name(). | |
2293 */ | |
2294 | |
2295 static char_u *varnamebuf = NULL; | |
2296 static int varnamebuflen = 0; | |
2297 | |
2298 /* | |
2299 * Function to concatenate a prefix and a variable name. | |
2300 */ | |
2301 static char_u * | |
2302 cat_prefix_varname(int prefix, char_u *name) | |
2303 { | |
2304 int len; | |
2305 | |
2306 len = (int)STRLEN(name) + 3; | |
2307 if (len > varnamebuflen) | |
2308 { | |
2309 vim_free(varnamebuf); | |
2310 len += 10; /* some additional space */ | |
2311 varnamebuf = alloc(len); | |
2312 if (varnamebuf == NULL) | |
2313 { | |
2314 varnamebuflen = 0; | |
2315 return NULL; | |
2316 } | |
2317 varnamebuflen = len; | |
2318 } | |
2319 *varnamebuf = prefix; | |
2320 varnamebuf[1] = ':'; | |
2321 STRCPY(varnamebuf + 2, name); | |
2322 return varnamebuf; | |
2323 } | |
2324 | |
2325 /* | |
2326 * Function given to ExpandGeneric() to obtain the list of user defined | |
2327 * (global/buffer/window/built-in) variable names. | |
2328 */ | |
2329 char_u * | |
2330 get_user_var_name(expand_T *xp, int idx) | |
2331 { | |
2332 static long_u gdone; | |
2333 static long_u bdone; | |
2334 static long_u wdone; | |
2335 static long_u tdone; | |
2336 static int vidx; | |
2337 static hashitem_T *hi; | |
2338 hashtab_T *ht; | |
2339 | |
2340 if (idx == 0) | |
2341 { | |
2342 gdone = bdone = wdone = vidx = 0; | |
2343 tdone = 0; | |
2344 } | |
2345 | |
2346 /* Global variables */ | |
2347 if (gdone < globvarht.ht_used) | |
2348 { | |
2349 if (gdone++ == 0) | |
2350 hi = globvarht.ht_array; | |
2351 else | |
2352 ++hi; | |
2353 while (HASHITEM_EMPTY(hi)) | |
2354 ++hi; | |
2355 if (STRNCMP("g:", xp->xp_pattern, 2) == 0) | |
2356 return cat_prefix_varname('g', hi->hi_key); | |
2357 return hi->hi_key; | |
2358 } | |
2359 | |
2360 /* b: variables */ | |
2361 ht = &curbuf->b_vars->dv_hashtab; | |
2362 if (bdone < ht->ht_used) | |
2363 { | |
2364 if (bdone++ == 0) | |
2365 hi = ht->ht_array; | |
2366 else | |
2367 ++hi; | |
2368 while (HASHITEM_EMPTY(hi)) | |
2369 ++hi; | |
2370 return cat_prefix_varname('b', hi->hi_key); | |
2371 } | |
2372 | |
2373 /* w: variables */ | |
2374 ht = &curwin->w_vars->dv_hashtab; | |
2375 if (wdone < ht->ht_used) | |
2376 { | |
2377 if (wdone++ == 0) | |
2378 hi = ht->ht_array; | |
2379 else | |
2380 ++hi; | |
2381 while (HASHITEM_EMPTY(hi)) | |
2382 ++hi; | |
2383 return cat_prefix_varname('w', hi->hi_key); | |
2384 } | |
2385 | |
2386 /* t: variables */ | |
2387 ht = &curtab->tp_vars->dv_hashtab; | |
2388 if (tdone < ht->ht_used) | |
2389 { | |
2390 if (tdone++ == 0) | |
2391 hi = ht->ht_array; | |
2392 else | |
2393 ++hi; | |
2394 while (HASHITEM_EMPTY(hi)) | |
2395 ++hi; | |
2396 return cat_prefix_varname('t', hi->hi_key); | |
2397 } | |
2398 | |
2399 /* v: variables */ | |
2400 if (vidx < VV_LEN) | |
2401 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name); | |
2402 | |
2403 VIM_CLEAR(varnamebuf); | |
2404 varnamebuflen = 0; | |
2405 return NULL; | |
2406 } | |
2407 | 1948 |
2408 /* | 1949 /* |
2409 * Return TRUE if "pat" matches "text". | 1950 * Return TRUE if "pat" matches "text". |
2410 * Does not use 'cpo' and always uses 'magic'. | 1951 * Does not use 'cpo' and always uses 'magic'. |
2411 */ | 1952 */ |
4617 { | 4158 { |
4618 int copyID; | 4159 int copyID; |
4619 int abort = FALSE; | 4160 int abort = FALSE; |
4620 buf_T *buf; | 4161 buf_T *buf; |
4621 win_T *wp; | 4162 win_T *wp; |
4622 int i; | |
4623 int did_free = FALSE; | 4163 int did_free = FALSE; |
4624 tabpage_T *tp; | 4164 tabpage_T *tp; |
4625 | 4165 |
4626 if (!testing) | 4166 if (!testing) |
4627 { | 4167 { |
4644 * referenced through previous_funccal. This must be first, because if | 4184 * referenced through previous_funccal. This must be first, because if |
4645 * the item is referenced elsewhere the funccal must not be freed. */ | 4185 * the item is referenced elsewhere the funccal must not be freed. */ |
4646 abort = abort || set_ref_in_previous_funccal(copyID); | 4186 abort = abort || set_ref_in_previous_funccal(copyID); |
4647 | 4187 |
4648 /* script-local variables */ | 4188 /* script-local variables */ |
4649 for (i = 1; i <= ga_scripts.ga_len; ++i) | 4189 abort = abort || garbage_collect_scriptvars(copyID); |
4650 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL); | |
4651 | 4190 |
4652 /* buffer-local variables */ | 4191 /* buffer-local variables */ |
4653 FOR_ALL_BUFFERS(buf) | 4192 FOR_ALL_BUFFERS(buf) |
4654 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID, | 4193 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID, |
4655 NULL, NULL); | 4194 NULL, NULL); |
4686 | 4225 |
4687 /* function call arguments, if v:testing is set. */ | 4226 /* function call arguments, if v:testing is set. */ |
4688 abort = abort || set_ref_in_func_args(copyID); | 4227 abort = abort || set_ref_in_func_args(copyID); |
4689 | 4228 |
4690 /* v: vars */ | 4229 /* v: vars */ |
4691 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL); | 4230 abort = abort || garbage_collect_vimvars(copyID); |
4692 | 4231 |
4693 // callbacks in buffers | 4232 // callbacks in buffers |
4694 abort = abort || set_ref_in_buffers(copyID); | 4233 abort = abort || set_ref_in_buffers(copyID); |
4695 | 4234 |
4696 #ifdef FEAT_LUA | 4235 #ifdef FEAT_LUA |
5473 } | 5012 } |
5474 | 5013 |
5475 return OK; | 5014 return OK; |
5476 } | 5015 } |
5477 | 5016 |
5478 | |
5479 | |
5480 /* | 5017 /* |
5481 * Translate a String variable into a position. | 5018 * Translate a String variable into a position. |
5482 * Returns NULL when there is an error. | 5019 * Returns NULL when there is an error. |
5483 */ | 5020 */ |
5484 pos_T * | 5021 pos_T * |
5952 */ | 5489 */ |
5953 int | 5490 int |
5954 eval_isnamec1(int c) | 5491 eval_isnamec1(int c) |
5955 { | 5492 { |
5956 return (ASCII_ISALPHA(c) || c == '_'); | 5493 return (ASCII_ISALPHA(c) || c == '_'); |
5957 } | |
5958 | |
5959 /* | |
5960 * Set number v: variable to "val". | |
5961 */ | |
5962 void | |
5963 set_vim_var_nr(int idx, varnumber_T val) | |
5964 { | |
5965 vimvars[idx].vv_nr = val; | |
5966 } | |
5967 | |
5968 /* | |
5969 * Get typval_T v: variable value. | |
5970 */ | |
5971 typval_T * | |
5972 get_vim_var_tv(int idx) | |
5973 { | |
5974 return &vimvars[idx].vv_tv; | |
5975 } | |
5976 | |
5977 /* | |
5978 * Get number v: variable value. | |
5979 */ | |
5980 varnumber_T | |
5981 get_vim_var_nr(int idx) | |
5982 { | |
5983 return vimvars[idx].vv_nr; | |
5984 } | |
5985 | |
5986 /* | |
5987 * Get string v: variable value. Uses a static buffer, can only be used once. | |
5988 * If the String variable has never been set, return an empty string. | |
5989 * Never returns NULL; | |
5990 */ | |
5991 char_u * | |
5992 get_vim_var_str(int idx) | |
5993 { | |
5994 return tv_get_string(&vimvars[idx].vv_tv); | |
5995 } | |
5996 | |
5997 /* | |
5998 * Get List v: variable value. Caller must take care of reference count when | |
5999 * needed. | |
6000 */ | |
6001 list_T * | |
6002 get_vim_var_list(int idx) | |
6003 { | |
6004 return vimvars[idx].vv_list; | |
6005 } | |
6006 | |
6007 /* | |
6008 * Get Dict v: variable value. Caller must take care of reference count when | |
6009 * needed. | |
6010 */ | |
6011 dict_T * | |
6012 get_vim_var_dict(int idx) | |
6013 { | |
6014 return vimvars[idx].vv_dict; | |
6015 } | |
6016 | |
6017 /* | |
6018 * Set v:char to character "c". | |
6019 */ | |
6020 void | |
6021 set_vim_var_char(int c) | |
6022 { | |
6023 char_u buf[MB_MAXBYTES + 1]; | |
6024 | |
6025 if (has_mbyte) | |
6026 buf[(*mb_char2bytes)(c, buf)] = NUL; | |
6027 else | |
6028 { | |
6029 buf[0] = c; | |
6030 buf[1] = NUL; | |
6031 } | |
6032 set_vim_var_string(VV_CHAR, buf, -1); | |
6033 } | |
6034 | |
6035 /* | |
6036 * Set v:count to "count" and v:count1 to "count1". | |
6037 * When "set_prevcount" is TRUE first set v:prevcount from v:count. | |
6038 */ | |
6039 void | |
6040 set_vcount( | |
6041 long count, | |
6042 long count1, | |
6043 int set_prevcount) | |
6044 { | |
6045 if (set_prevcount) | |
6046 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr; | |
6047 vimvars[VV_COUNT].vv_nr = count; | |
6048 vimvars[VV_COUNT1].vv_nr = count1; | |
6049 } | |
6050 | |
6051 /* | |
6052 * Save variables that might be changed as a side effect. Used when executing | |
6053 * a timer callback. | |
6054 */ | |
6055 void | |
6056 save_vimvars(vimvars_save_T *vvsave) | |
6057 { | |
6058 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr; | |
6059 vvsave->vv_count = vimvars[VV_COUNT].vv_nr; | |
6060 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr; | |
6061 } | |
6062 | |
6063 /* | |
6064 * Restore variables saved by save_vimvars(). | |
6065 */ | |
6066 void | |
6067 restore_vimvars(vimvars_save_T *vvsave) | |
6068 { | |
6069 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount; | |
6070 vimvars[VV_COUNT].vv_nr = vvsave->vv_count; | |
6071 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1; | |
6072 } | |
6073 | |
6074 /* | |
6075 * Set string v: variable to a copy of "val". | |
6076 */ | |
6077 void | |
6078 set_vim_var_string( | |
6079 int idx, | |
6080 char_u *val, | |
6081 int len) /* length of "val" to use or -1 (whole string) */ | |
6082 { | |
6083 clear_tv(&vimvars[idx].vv_di.di_tv); | |
6084 vimvars[idx].vv_type = VAR_STRING; | |
6085 if (val == NULL) | |
6086 vimvars[idx].vv_str = NULL; | |
6087 else if (len == -1) | |
6088 vimvars[idx].vv_str = vim_strsave(val); | |
6089 else | |
6090 vimvars[idx].vv_str = vim_strnsave(val, len); | |
6091 } | |
6092 | |
6093 /* | |
6094 * Set List v: variable to "val". | |
6095 */ | |
6096 void | |
6097 set_vim_var_list(int idx, list_T *val) | |
6098 { | |
6099 clear_tv(&vimvars[idx].vv_di.di_tv); | |
6100 vimvars[idx].vv_type = VAR_LIST; | |
6101 vimvars[idx].vv_list = val; | |
6102 if (val != NULL) | |
6103 ++val->lv_refcount; | |
6104 } | |
6105 | |
6106 /* | |
6107 * Set Dictionary v: variable to "val". | |
6108 */ | |
6109 void | |
6110 set_vim_var_dict(int idx, dict_T *val) | |
6111 { | |
6112 clear_tv(&vimvars[idx].vv_di.di_tv); | |
6113 vimvars[idx].vv_type = VAR_DICT; | |
6114 vimvars[idx].vv_dict = val; | |
6115 if (val != NULL) | |
6116 { | |
6117 ++val->dv_refcount; | |
6118 dict_set_items_ro(val); | |
6119 } | |
6120 } | |
6121 | |
6122 /* | |
6123 * Set v:register if needed. | |
6124 */ | |
6125 void | |
6126 set_reg_var(int c) | |
6127 { | |
6128 char_u regname; | |
6129 | |
6130 if (c == 0 || c == ' ') | |
6131 regname = '"'; | |
6132 else | |
6133 regname = c; | |
6134 /* Avoid free/alloc when the value is already right. */ | |
6135 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c) | |
6136 set_vim_var_string(VV_REG, ®name, 1); | |
6137 } | |
6138 | |
6139 /* | |
6140 * Get or set v:exception. If "oldval" == NULL, return the current value. | |
6141 * Otherwise, restore the value to "oldval" and return NULL. | |
6142 * Must always be called in pairs to save and restore v:exception! Does not | |
6143 * take care of memory allocations. | |
6144 */ | |
6145 char_u * | |
6146 v_exception(char_u *oldval) | |
6147 { | |
6148 if (oldval == NULL) | |
6149 return vimvars[VV_EXCEPTION].vv_str; | |
6150 | |
6151 vimvars[VV_EXCEPTION].vv_str = oldval; | |
6152 return NULL; | |
6153 } | |
6154 | |
6155 /* | |
6156 * Get or set v:throwpoint. If "oldval" == NULL, return the current value. | |
6157 * Otherwise, restore the value to "oldval" and return NULL. | |
6158 * Must always be called in pairs to save and restore v:throwpoint! Does not | |
6159 * take care of memory allocations. | |
6160 */ | |
6161 char_u * | |
6162 v_throwpoint(char_u *oldval) | |
6163 { | |
6164 if (oldval == NULL) | |
6165 return vimvars[VV_THROWPOINT].vv_str; | |
6166 | |
6167 vimvars[VV_THROWPOINT].vv_str = oldval; | |
6168 return NULL; | |
6169 } | |
6170 | |
6171 /* | |
6172 * Set v:cmdarg. | |
6173 * If "eap" != NULL, use "eap" to generate the value and return the old value. | |
6174 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL. | |
6175 * Must always be called in pairs! | |
6176 */ | |
6177 char_u * | |
6178 set_cmdarg(exarg_T *eap, char_u *oldarg) | |
6179 { | |
6180 char_u *oldval; | |
6181 char_u *newval; | |
6182 unsigned len; | |
6183 | |
6184 oldval = vimvars[VV_CMDARG].vv_str; | |
6185 if (eap == NULL) | |
6186 { | |
6187 vim_free(oldval); | |
6188 vimvars[VV_CMDARG].vv_str = oldarg; | |
6189 return NULL; | |
6190 } | |
6191 | |
6192 if (eap->force_bin == FORCE_BIN) | |
6193 len = 6; | |
6194 else if (eap->force_bin == FORCE_NOBIN) | |
6195 len = 8; | |
6196 else | |
6197 len = 0; | |
6198 | |
6199 if (eap->read_edit) | |
6200 len += 7; | |
6201 | |
6202 if (eap->force_ff != 0) | |
6203 len += 10; /* " ++ff=unix" */ | |
6204 if (eap->force_enc != 0) | |
6205 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7; | |
6206 if (eap->bad_char != 0) | |
6207 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */ | |
6208 | |
6209 newval = alloc(len + 1); | |
6210 if (newval == NULL) | |
6211 return NULL; | |
6212 | |
6213 if (eap->force_bin == FORCE_BIN) | |
6214 sprintf((char *)newval, " ++bin"); | |
6215 else if (eap->force_bin == FORCE_NOBIN) | |
6216 sprintf((char *)newval, " ++nobin"); | |
6217 else | |
6218 *newval = NUL; | |
6219 | |
6220 if (eap->read_edit) | |
6221 STRCAT(newval, " ++edit"); | |
6222 | |
6223 if (eap->force_ff != 0) | |
6224 sprintf((char *)newval + STRLEN(newval), " ++ff=%s", | |
6225 eap->force_ff == 'u' ? "unix" | |
6226 : eap->force_ff == 'd' ? "dos" | |
6227 : "mac"); | |
6228 if (eap->force_enc != 0) | |
6229 sprintf((char *)newval + STRLEN(newval), " ++enc=%s", | |
6230 eap->cmd + eap->force_enc); | |
6231 if (eap->bad_char == BAD_KEEP) | |
6232 STRCPY(newval + STRLEN(newval), " ++bad=keep"); | |
6233 else if (eap->bad_char == BAD_DROP) | |
6234 STRCPY(newval + STRLEN(newval), " ++bad=drop"); | |
6235 else if (eap->bad_char != 0) | |
6236 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char); | |
6237 vimvars[VV_CMDARG].vv_str = newval; | |
6238 return oldval; | |
6239 } | |
6240 | |
6241 /* | |
6242 * Check if variable "name[len]" is a local variable or an argument. | |
6243 * If so, "*eval_lavars_used" is set to TRUE. | |
6244 */ | |
6245 static void | |
6246 check_vars(char_u *name, int len) | |
6247 { | |
6248 int cc; | |
6249 char_u *varname; | |
6250 hashtab_T *ht; | |
6251 | |
6252 if (eval_lavars_used == NULL) | |
6253 return; | |
6254 | |
6255 /* truncate the name, so that we can use strcmp() */ | |
6256 cc = name[len]; | |
6257 name[len] = NUL; | |
6258 | |
6259 ht = find_var_ht(name, &varname); | |
6260 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht()) | |
6261 { | |
6262 if (find_var(name, NULL, TRUE) != NULL) | |
6263 *eval_lavars_used = TRUE; | |
6264 } | |
6265 | |
6266 name[len] = cc; | |
6267 } | 5494 } |
6268 | 5495 |
6269 /* | 5496 /* |
6270 * Handle: | 5497 * Handle: |
6271 * - expr[expr], expr[expr:expr] subscript | 5498 * - expr[expr], expr[expr:expr] subscript |
6378 /* | 5605 /* |
6379 * Allocate memory for a variable type-value, and assign a string to it. | 5606 * Allocate memory for a variable type-value, and assign a string to it. |
6380 * The string "s" must have been allocated, it is consumed. | 5607 * The string "s" must have been allocated, it is consumed. |
6381 * Return NULL for out of memory, the variable otherwise. | 5608 * Return NULL for out of memory, the variable otherwise. |
6382 */ | 5609 */ |
6383 static typval_T * | 5610 typval_T * |
6384 alloc_string_tv(char_u *s) | 5611 alloc_string_tv(char_u *s) |
6385 { | 5612 { |
6386 typval_T *rettv; | 5613 typval_T *rettv; |
6387 | 5614 |
6388 rettv = alloc_tv(); | 5615 rettv = alloc_tv(); |
6775 } | 6002 } |
6776 return tv_get_string_buf(varp, buf); | 6003 return tv_get_string_buf(varp, buf); |
6777 } | 6004 } |
6778 | 6005 |
6779 /* | 6006 /* |
6780 * Find variable "name" in the list of variables. | |
6781 * Return a pointer to it if found, NULL if not found. | |
6782 * Careful: "a:0" variables don't have a name. | |
6783 * When "htp" is not NULL we are writing to the variable, set "htp" to the | |
6784 * hashtab_T used. | |
6785 */ | |
6786 dictitem_T * | |
6787 find_var(char_u *name, hashtab_T **htp, int no_autoload) | |
6788 { | |
6789 char_u *varname; | |
6790 hashtab_T *ht; | |
6791 dictitem_T *ret = NULL; | |
6792 | |
6793 ht = find_var_ht(name, &varname); | |
6794 if (htp != NULL) | |
6795 *htp = ht; | |
6796 if (ht == NULL) | |
6797 return NULL; | |
6798 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL); | |
6799 if (ret != NULL) | |
6800 return ret; | |
6801 | |
6802 /* Search in parent scope for lambda */ | |
6803 return find_var_in_scoped_ht(name, no_autoload || htp != NULL); | |
6804 } | |
6805 | |
6806 /* | |
6807 * Find variable "varname" in hashtab "ht" with name "htname". | |
6808 * Returns NULL if not found. | |
6809 */ | |
6810 dictitem_T * | |
6811 find_var_in_ht( | |
6812 hashtab_T *ht, | |
6813 int htname, | |
6814 char_u *varname, | |
6815 int no_autoload) | |
6816 { | |
6817 hashitem_T *hi; | |
6818 | |
6819 if (*varname == NUL) | |
6820 { | |
6821 /* Must be something like "s:", otherwise "ht" would be NULL. */ | |
6822 switch (htname) | |
6823 { | |
6824 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var; | |
6825 case 'g': return &globvars_var; | |
6826 case 'v': return &vimvars_var; | |
6827 case 'b': return &curbuf->b_bufvar; | |
6828 case 'w': return &curwin->w_winvar; | |
6829 case 't': return &curtab->tp_winvar; | |
6830 case 'l': return get_funccal_local_var(); | |
6831 case 'a': return get_funccal_args_var(); | |
6832 } | |
6833 return NULL; | |
6834 } | |
6835 | |
6836 hi = hash_find(ht, varname); | |
6837 if (HASHITEM_EMPTY(hi)) | |
6838 { | |
6839 /* For global variables we may try auto-loading the script. If it | |
6840 * worked find the variable again. Don't auto-load a script if it was | |
6841 * loaded already, otherwise it would be loaded every time when | |
6842 * checking if a function name is a Funcref variable. */ | |
6843 if (ht == &globvarht && !no_autoload) | |
6844 { | |
6845 /* Note: script_autoload() may make "hi" invalid. It must either | |
6846 * be obtained again or not used. */ | |
6847 if (!script_autoload(varname, FALSE) || aborting()) | |
6848 return NULL; | |
6849 hi = hash_find(ht, varname); | |
6850 } | |
6851 if (HASHITEM_EMPTY(hi)) | |
6852 return NULL; | |
6853 } | |
6854 return HI2DI(hi); | |
6855 } | |
6856 | |
6857 /* | |
6858 * Find the hashtab used for a variable name. | |
6859 * Return NULL if the name is not valid. | |
6860 * Set "varname" to the start of name without ':'. | |
6861 */ | |
6862 hashtab_T * | |
6863 find_var_ht(char_u *name, char_u **varname) | |
6864 { | |
6865 hashitem_T *hi; | |
6866 hashtab_T *ht; | |
6867 | |
6868 if (name[0] == NUL) | |
6869 return NULL; | |
6870 if (name[1] != ':') | |
6871 { | |
6872 /* The name must not start with a colon or #. */ | |
6873 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR) | |
6874 return NULL; | |
6875 *varname = name; | |
6876 | |
6877 // "version" is "v:version" in all scopes if scriptversion < 3. | |
6878 // Same for a few other variables marked with VV_COMPAT. | |
6879 if (current_sctx.sc_version < 3) | |
6880 { | |
6881 hi = hash_find(&compat_hashtab, name); | |
6882 if (!HASHITEM_EMPTY(hi)) | |
6883 return &compat_hashtab; | |
6884 } | |
6885 | |
6886 ht = get_funccal_local_ht(); | |
6887 if (ht == NULL) | |
6888 return &globvarht; /* global variable */ | |
6889 return ht; /* local variable */ | |
6890 } | |
6891 *varname = name + 2; | |
6892 if (*name == 'g') /* global variable */ | |
6893 return &globvarht; | |
6894 // There must be no ':' or '#' in the rest of the name, unless g: is used | |
6895 if (vim_strchr(name + 2, ':') != NULL | |
6896 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL) | |
6897 return NULL; | |
6898 if (*name == 'b') /* buffer variable */ | |
6899 return &curbuf->b_vars->dv_hashtab; | |
6900 if (*name == 'w') /* window variable */ | |
6901 return &curwin->w_vars->dv_hashtab; | |
6902 if (*name == 't') /* tab page variable */ | |
6903 return &curtab->tp_vars->dv_hashtab; | |
6904 if (*name == 'v') /* v: variable */ | |
6905 return &vimvarht; | |
6906 if (*name == 'a') /* a: function argument */ | |
6907 return get_funccal_args_ht(); | |
6908 if (*name == 'l') /* l: local function variable */ | |
6909 return get_funccal_local_ht(); | |
6910 if (*name == 's' /* script variable */ | |
6911 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len) | |
6912 return &SCRIPT_VARS(current_sctx.sc_sid); | |
6913 return NULL; | |
6914 } | |
6915 | |
6916 /* | |
6917 * Allocate a new hashtab for a sourced script. It will be used while | |
6918 * sourcing this script and when executing functions defined in the script. | |
6919 */ | |
6920 void | |
6921 new_script_vars(scid_T id) | |
6922 { | |
6923 int i; | |
6924 hashtab_T *ht; | |
6925 scriptvar_T *sv; | |
6926 | |
6927 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK) | |
6928 { | |
6929 /* Re-allocating ga_data means that an ht_array pointing to | |
6930 * ht_smallarray becomes invalid. We can recognize this: ht_mask is | |
6931 * at its init value. Also reset "v_dict", it's always the same. */ | |
6932 for (i = 1; i <= ga_scripts.ga_len; ++i) | |
6933 { | |
6934 ht = &SCRIPT_VARS(i); | |
6935 if (ht->ht_mask == HT_INIT_SIZE - 1) | |
6936 ht->ht_array = ht->ht_smallarray; | |
6937 sv = SCRIPT_SV(i); | |
6938 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict; | |
6939 } | |
6940 | |
6941 while (ga_scripts.ga_len < id) | |
6942 { | |
6943 sv = SCRIPT_SV(ga_scripts.ga_len + 1) = | |
6944 ALLOC_CLEAR_ONE(scriptvar_T); | |
6945 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE); | |
6946 ++ga_scripts.ga_len; | |
6947 } | |
6948 } | |
6949 } | |
6950 | |
6951 /* | |
6952 * Initialize dictionary "dict" as a scope and set variable "dict_var" to | |
6953 * point to it. | |
6954 */ | |
6955 void | |
6956 init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope) | |
6957 { | |
6958 hash_init(&dict->dv_hashtab); | |
6959 dict->dv_lock = 0; | |
6960 dict->dv_scope = scope; | |
6961 dict->dv_refcount = DO_NOT_FREE_CNT; | |
6962 dict->dv_copyID = 0; | |
6963 dict_var->di_tv.vval.v_dict = dict; | |
6964 dict_var->di_tv.v_type = VAR_DICT; | |
6965 dict_var->di_tv.v_lock = VAR_FIXED; | |
6966 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; | |
6967 dict_var->di_key[0] = NUL; | |
6968 } | |
6969 | |
6970 /* | |
6971 * Unreference a dictionary initialized by init_var_dict(). | |
6972 */ | |
6973 void | |
6974 unref_var_dict(dict_T *dict) | |
6975 { | |
6976 /* Now the dict needs to be freed if no one else is using it, go back to | |
6977 * normal reference counting. */ | |
6978 dict->dv_refcount -= DO_NOT_FREE_CNT - 1; | |
6979 dict_unref(dict); | |
6980 } | |
6981 | |
6982 /* | |
6983 * Return TRUE if typeval "tv" and its value are set to be locked (immutable). | 6007 * Return TRUE if typeval "tv" and its value are set to be locked (immutable). |
6984 * Also give an error message, using "name" or _("name") when use_gettext is | 6008 * Also give an error message, using "name" or _("name") when use_gettext is |
6985 * TRUE. | 6009 * TRUE. |
6986 */ | 6010 */ |
6987 static int | 6011 static int |
7728 } | 6752 } |
7729 } | 6753 } |
7730 } | 6754 } |
7731 | 6755 |
7732 /* | 6756 /* |
7733 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal, | |
7734 * v:option_type, and v:option_command. | |
7735 */ | |
7736 void | |
7737 reset_v_option_vars(void) | |
7738 { | |
7739 set_vim_var_string(VV_OPTION_NEW, NULL, -1); | |
7740 set_vim_var_string(VV_OPTION_OLD, NULL, -1); | |
7741 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1); | |
7742 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1); | |
7743 set_vim_var_string(VV_OPTION_TYPE, NULL, -1); | |
7744 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1); | |
7745 } | |
7746 | |
7747 /* | |
7748 * Add an assert error to v:errors. | |
7749 */ | |
7750 void | |
7751 assert_error(garray_T *gap) | |
7752 { | |
7753 struct vimvar *vp = &vimvars[VV_ERRORS]; | |
7754 | |
7755 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL) | |
7756 /* Make sure v:errors is a list. */ | |
7757 set_vim_var_list(VV_ERRORS, list_alloc()); | |
7758 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len); | |
7759 } | |
7760 /* | |
7761 * Compare "typ1" and "typ2". Put the result in "typ1". | 6757 * Compare "typ1" and "typ2". Put the result in "typ1". |
7762 */ | 6758 */ |
7763 int | 6759 int |
7764 typval_compare( | 6760 typval_compare( |
7765 typval_T *typ1, /* first operand */ | 6761 typval_T *typ1, /* first operand */ |
7997 ret = vim_strsave(ret); | 6993 ret = vim_strsave(ret); |
7998 return ret; | 6994 return ret; |
7999 } | 6995 } |
8000 | 6996 |
8001 #endif /* FEAT_EVAL */ | 6997 #endif /* FEAT_EVAL */ |
8002 | |
8003 | 6998 |
8004 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO) | 6999 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO) |
8005 | 7000 |
8006 #ifdef MSWIN | 7001 #ifdef MSWIN |
8007 /* | 7002 /* |
8752 { | 7747 { |
8753 typval_T rettv; | 7748 typval_T rettv; |
8754 typval_T argv[3]; | 7749 typval_T argv[3]; |
8755 int retval = FAIL; | 7750 int retval = FAIL; |
8756 | 7751 |
8757 copy_tv(tv, &vimvars[VV_VAL].vv_tv); | 7752 copy_tv(tv, get_vim_var_tv(VV_VAL)); |
8758 argv[0] = vimvars[VV_KEY].vv_tv; | 7753 argv[0] = *get_vim_var_tv(VV_KEY); |
8759 argv[1] = vimvars[VV_VAL].vv_tv; | 7754 argv[1] = *get_vim_var_tv(VV_VAL); |
8760 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL) | 7755 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL) |
8761 goto theend; | 7756 goto theend; |
8762 if (map) | 7757 if (map) |
8763 { | 7758 { |
8764 /* map(): replace the list item value */ | 7759 /* map(): replace the list item value */ |
8778 if (error) | 7773 if (error) |
8779 goto theend; | 7774 goto theend; |
8780 } | 7775 } |
8781 retval = OK; | 7776 retval = OK; |
8782 theend: | 7777 theend: |
8783 clear_tv(&vimvars[VV_VAL].vv_tv); | 7778 clear_tv(get_vim_var_tv(VV_VAL)); |
8784 return retval; | 7779 return retval; |
8785 } | 7780 } |
8786 | |
8787 | 7781 |
8788 /* | 7782 /* |
8789 * Implementation of map() and filter(). | 7783 * Implementation of map() and filter(). |
8790 */ | 7784 */ |
8791 void | 7785 void |
8846 did_emsg = FALSE; | 7840 did_emsg = FALSE; |
8847 | 7841 |
8848 prepare_vimvar(VV_KEY, &save_key); | 7842 prepare_vimvar(VV_KEY, &save_key); |
8849 if (argvars[0].v_type == VAR_DICT) | 7843 if (argvars[0].v_type == VAR_DICT) |
8850 { | 7844 { |
8851 vimvars[VV_KEY].vv_type = VAR_STRING; | |
8852 | |
8853 ht = &d->dv_hashtab; | 7845 ht = &d->dv_hashtab; |
8854 hash_lock(ht); | 7846 hash_lock(ht); |
8855 todo = (int)ht->ht_used; | 7847 todo = (int)ht->ht_used; |
8856 for (hi = ht->ht_array; todo > 0; ++hi) | 7848 for (hi = ht->ht_array; todo > 0; ++hi) |
8857 { | 7849 { |
8864 if (map && (var_check_lock(di->di_tv.v_lock, | 7856 if (map && (var_check_lock(di->di_tv.v_lock, |
8865 arg_errmsg, TRUE) | 7857 arg_errmsg, TRUE) |
8866 || var_check_ro(di->di_flags, | 7858 || var_check_ro(di->di_flags, |
8867 arg_errmsg, TRUE))) | 7859 arg_errmsg, TRUE))) |
8868 break; | 7860 break; |
8869 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key); | 7861 set_vim_var_string(VV_KEY, di->di_key, -1); |
8870 r = filter_map_one(&di->di_tv, expr, map, &rem); | 7862 r = filter_map_one(&di->di_tv, expr, map, &rem); |
8871 clear_tv(&vimvars[VV_KEY].vv_tv); | 7863 clear_tv(get_vim_var_tv(VV_KEY)); |
8872 if (r == FAIL || did_emsg) | 7864 if (r == FAIL || did_emsg) |
8873 break; | 7865 break; |
8874 if (!map && rem) | 7866 if (!map && rem) |
8875 { | 7867 { |
8876 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE) | 7868 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE) |
8885 else if (argvars[0].v_type == VAR_BLOB) | 7877 else if (argvars[0].v_type == VAR_BLOB) |
8886 { | 7878 { |
8887 int i; | 7879 int i; |
8888 typval_T tv; | 7880 typval_T tv; |
8889 | 7881 |
8890 vimvars[VV_KEY].vv_type = VAR_NUMBER; | |
8891 for (i = 0; i < b->bv_ga.ga_len; i++) | 7882 for (i = 0; i < b->bv_ga.ga_len; i++) |
8892 { | 7883 { |
8893 tv.v_type = VAR_NUMBER; | 7884 tv.v_type = VAR_NUMBER; |
8894 tv.vval.v_number = blob_get(b, i); | 7885 tv.vval.v_number = blob_get(b, i); |
8895 vimvars[VV_KEY].vv_nr = idx; | 7886 set_vim_var_nr(VV_KEY, idx); |
8896 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg) | 7887 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg) |
8897 break; | 7888 break; |
8898 if (tv.v_type != VAR_NUMBER) | 7889 if (tv.v_type != VAR_NUMBER) |
8899 { | 7890 { |
8900 emsg(_(e_invalblob)); | 7891 emsg(_(e_invalblob)); |
8914 } | 7905 } |
8915 } | 7906 } |
8916 else | 7907 else |
8917 { | 7908 { |
8918 // argvars[0].v_type == VAR_LIST | 7909 // argvars[0].v_type == VAR_LIST |
8919 vimvars[VV_KEY].vv_type = VAR_NUMBER; | |
8920 | |
8921 for (li = l->lv_first; li != NULL; li = nli) | 7910 for (li = l->lv_first; li != NULL; li = nli) |
8922 { | 7911 { |
8923 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE)) | 7912 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE)) |
8924 break; | 7913 break; |
8925 nli = li->li_next; | 7914 nli = li->li_next; |
8926 vimvars[VV_KEY].vv_nr = idx; | 7915 set_vim_var_nr(VV_KEY, idx); |
8927 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL | 7916 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL |
8928 || did_emsg) | 7917 || did_emsg) |
8929 break; | 7918 break; |
8930 if (!map && rem) | 7919 if (!map && rem) |
8931 listitem_remove(l, li); | 7920 listitem_remove(l, li); |