Bram Moolenaar | 0522ba0 | 2019-08-27 22:48:30 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * evalvars.c: functions for dealing with variables |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 17 | |
| 18 | static char *e_letunexp = N_("E18: Unexpected characters in :let"); |
| 19 | |
Bram Moolenaar | e5cdf15 | 2019-08-29 22:09:46 +0200 | [diff] [blame] | 20 | static dictitem_T globvars_var; // variable used for g: |
| 21 | |
| 22 | /* |
| 23 | * Old Vim variables such as "v:version" are also available without the "v:". |
| 24 | * Also in functions. We need a special hashtable for them. |
| 25 | */ |
| 26 | static hashtab_T compat_hashtab; |
| 27 | |
| 28 | /* |
| 29 | * Array to hold the value of v: variables. |
| 30 | * The value is in a dictitem, so that it can also be used in the v: scope. |
| 31 | * The reason to use this table anyway is for very quick access to the |
| 32 | * variables with the VV_ defines. |
| 33 | */ |
| 34 | |
| 35 | // values for vv_flags: |
| 36 | #define VV_COMPAT 1 // compatible, also used without "v:" |
| 37 | #define VV_RO 2 // read-only |
| 38 | #define VV_RO_SBX 4 // read-only in the sandbox |
| 39 | |
| 40 | #define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}} |
| 41 | |
| 42 | static struct vimvar |
| 43 | { |
| 44 | char *vv_name; // name of variable, without v: |
| 45 | dictitem16_T vv_di; // value and name for key (max 16 chars!) |
| 46 | char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX |
| 47 | } vimvars[VV_LEN] = |
| 48 | { |
| 49 | /* |
| 50 | * The order here must match the VV_ defines in vim.h! |
| 51 | * Initializing a union does not work, leave tv.vval empty to get zero's. |
| 52 | */ |
| 53 | {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO}, |
| 54 | {VV_NAME("count1", VAR_NUMBER), VV_RO}, |
| 55 | {VV_NAME("prevcount", VAR_NUMBER), VV_RO}, |
| 56 | {VV_NAME("errmsg", VAR_STRING), VV_COMPAT}, |
| 57 | {VV_NAME("warningmsg", VAR_STRING), 0}, |
| 58 | {VV_NAME("statusmsg", VAR_STRING), 0}, |
| 59 | {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO}, |
| 60 | {VV_NAME("this_session", VAR_STRING), VV_COMPAT}, |
| 61 | {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO}, |
| 62 | {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX}, |
| 63 | {VV_NAME("termresponse", VAR_STRING), VV_RO}, |
| 64 | {VV_NAME("fname", VAR_STRING), VV_RO}, |
| 65 | {VV_NAME("lang", VAR_STRING), VV_RO}, |
| 66 | {VV_NAME("lc_time", VAR_STRING), VV_RO}, |
| 67 | {VV_NAME("ctype", VAR_STRING), VV_RO}, |
| 68 | {VV_NAME("charconvert_from", VAR_STRING), VV_RO}, |
| 69 | {VV_NAME("charconvert_to", VAR_STRING), VV_RO}, |
| 70 | {VV_NAME("fname_in", VAR_STRING), VV_RO}, |
| 71 | {VV_NAME("fname_out", VAR_STRING), VV_RO}, |
| 72 | {VV_NAME("fname_new", VAR_STRING), VV_RO}, |
| 73 | {VV_NAME("fname_diff", VAR_STRING), VV_RO}, |
| 74 | {VV_NAME("cmdarg", VAR_STRING), VV_RO}, |
| 75 | {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX}, |
| 76 | {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX}, |
| 77 | {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX}, |
| 78 | {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX}, |
| 79 | {VV_NAME("progname", VAR_STRING), VV_RO}, |
| 80 | {VV_NAME("servername", VAR_STRING), VV_RO}, |
| 81 | {VV_NAME("dying", VAR_NUMBER), VV_RO}, |
| 82 | {VV_NAME("exception", VAR_STRING), VV_RO}, |
| 83 | {VV_NAME("throwpoint", VAR_STRING), VV_RO}, |
| 84 | {VV_NAME("register", VAR_STRING), VV_RO}, |
| 85 | {VV_NAME("cmdbang", VAR_NUMBER), VV_RO}, |
| 86 | {VV_NAME("insertmode", VAR_STRING), VV_RO}, |
| 87 | {VV_NAME("val", VAR_UNKNOWN), VV_RO}, |
| 88 | {VV_NAME("key", VAR_UNKNOWN), VV_RO}, |
| 89 | {VV_NAME("profiling", VAR_NUMBER), VV_RO}, |
| 90 | {VV_NAME("fcs_reason", VAR_STRING), VV_RO}, |
| 91 | {VV_NAME("fcs_choice", VAR_STRING), 0}, |
| 92 | {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO}, |
| 93 | {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO}, |
| 94 | {VV_NAME("beval_winid", VAR_NUMBER), VV_RO}, |
| 95 | {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO}, |
| 96 | {VV_NAME("beval_col", VAR_NUMBER), VV_RO}, |
| 97 | {VV_NAME("beval_text", VAR_STRING), VV_RO}, |
| 98 | {VV_NAME("scrollstart", VAR_STRING), 0}, |
| 99 | {VV_NAME("swapname", VAR_STRING), VV_RO}, |
| 100 | {VV_NAME("swapchoice", VAR_STRING), 0}, |
| 101 | {VV_NAME("swapcommand", VAR_STRING), VV_RO}, |
| 102 | {VV_NAME("char", VAR_STRING), 0}, |
| 103 | {VV_NAME("mouse_win", VAR_NUMBER), 0}, |
| 104 | {VV_NAME("mouse_winid", VAR_NUMBER), 0}, |
| 105 | {VV_NAME("mouse_lnum", VAR_NUMBER), 0}, |
| 106 | {VV_NAME("mouse_col", VAR_NUMBER), 0}, |
| 107 | {VV_NAME("operator", VAR_STRING), VV_RO}, |
| 108 | {VV_NAME("searchforward", VAR_NUMBER), 0}, |
| 109 | {VV_NAME("hlsearch", VAR_NUMBER), 0}, |
| 110 | {VV_NAME("oldfiles", VAR_LIST), 0}, |
| 111 | {VV_NAME("windowid", VAR_NUMBER), VV_RO}, |
| 112 | {VV_NAME("progpath", VAR_STRING), VV_RO}, |
| 113 | {VV_NAME("completed_item", VAR_DICT), VV_RO}, |
| 114 | {VV_NAME("option_new", VAR_STRING), VV_RO}, |
| 115 | {VV_NAME("option_old", VAR_STRING), VV_RO}, |
| 116 | {VV_NAME("option_oldlocal", VAR_STRING), VV_RO}, |
| 117 | {VV_NAME("option_oldglobal", VAR_STRING), VV_RO}, |
| 118 | {VV_NAME("option_command", VAR_STRING), VV_RO}, |
| 119 | {VV_NAME("option_type", VAR_STRING), VV_RO}, |
| 120 | {VV_NAME("errors", VAR_LIST), 0}, |
| 121 | {VV_NAME("false", VAR_SPECIAL), VV_RO}, |
| 122 | {VV_NAME("true", VAR_SPECIAL), VV_RO}, |
| 123 | {VV_NAME("null", VAR_SPECIAL), VV_RO}, |
| 124 | {VV_NAME("none", VAR_SPECIAL), VV_RO}, |
| 125 | {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO}, |
| 126 | {VV_NAME("testing", VAR_NUMBER), 0}, |
| 127 | {VV_NAME("t_number", VAR_NUMBER), VV_RO}, |
| 128 | {VV_NAME("t_string", VAR_NUMBER), VV_RO}, |
| 129 | {VV_NAME("t_func", VAR_NUMBER), VV_RO}, |
| 130 | {VV_NAME("t_list", VAR_NUMBER), VV_RO}, |
| 131 | {VV_NAME("t_dict", VAR_NUMBER), VV_RO}, |
| 132 | {VV_NAME("t_float", VAR_NUMBER), VV_RO}, |
| 133 | {VV_NAME("t_bool", VAR_NUMBER), VV_RO}, |
| 134 | {VV_NAME("t_none", VAR_NUMBER), VV_RO}, |
| 135 | {VV_NAME("t_job", VAR_NUMBER), VV_RO}, |
| 136 | {VV_NAME("t_channel", VAR_NUMBER), VV_RO}, |
| 137 | {VV_NAME("t_blob", VAR_NUMBER), VV_RO}, |
| 138 | {VV_NAME("termrfgresp", VAR_STRING), VV_RO}, |
| 139 | {VV_NAME("termrbgresp", VAR_STRING), VV_RO}, |
| 140 | {VV_NAME("termu7resp", VAR_STRING), VV_RO}, |
| 141 | {VV_NAME("termstyleresp", VAR_STRING), VV_RO}, |
| 142 | {VV_NAME("termblinkresp", VAR_STRING), VV_RO}, |
| 143 | {VV_NAME("event", VAR_DICT), VV_RO}, |
| 144 | {VV_NAME("versionlong", VAR_NUMBER), VV_RO}, |
| 145 | {VV_NAME("echospace", VAR_NUMBER), VV_RO}, |
| 146 | }; |
| 147 | |
| 148 | // shorthand |
| 149 | #define vv_type vv_di.di_tv.v_type |
| 150 | #define vv_nr vv_di.di_tv.vval.v_number |
| 151 | #define vv_float vv_di.di_tv.vval.v_float |
| 152 | #define vv_str vv_di.di_tv.vval.v_string |
| 153 | #define vv_list vv_di.di_tv.vval.v_list |
| 154 | #define vv_dict vv_di.di_tv.vval.v_dict |
| 155 | #define vv_blob vv_di.di_tv.vval.v_blob |
| 156 | #define vv_tv vv_di.di_tv |
| 157 | |
| 158 | static dictitem_T vimvars_var; // variable used for v: |
| 159 | #define vimvarht vimvardict.dv_hashtab |
| 160 | |
| 161 | // for VIM_VERSION_ defines |
| 162 | #include "version.h" |
| 163 | |
| 164 | /* |
| 165 | * Array to hold the hashtab with variables local to each sourced script. |
| 166 | * Each item holds a variable (nameless) that points to the dict_T. |
| 167 | */ |
| 168 | typedef struct |
| 169 | { |
| 170 | dictitem_T sv_var; |
| 171 | dict_T sv_dict; |
| 172 | } scriptvar_T; |
| 173 | |
| 174 | static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL}; |
| 175 | #define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1]) |
| 176 | #define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab) |
| 177 | |
Bram Moolenaar | 0522ba0 | 2019-08-27 22:48:30 +0200 | [diff] [blame] | 178 | static void ex_let_const(exarg_T *eap, int is_const); |
| 179 | static char_u *skip_var_one(char_u *arg); |
| 180 | static void list_glob_vars(int *first); |
| 181 | static void list_buf_vars(int *first); |
| 182 | static void list_win_vars(int *first); |
| 183 | static void list_tab_vars(int *first); |
| 184 | static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first); |
| 185 | static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int is_const, char_u *endchars, char_u *op); |
| 186 | static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep); |
| 187 | static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit); |
| 188 | static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock); |
| 189 | static void item_lock(typval_T *tv, int deep, int lock); |
| 190 | static void list_one_var(dictitem_T *v, char *prefix, int *first); |
| 191 | static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first); |
| 192 | |
| 193 | /* |
Bram Moolenaar | e5cdf15 | 2019-08-29 22:09:46 +0200 | [diff] [blame] | 194 | * Initialize global and vim special variables |
| 195 | */ |
| 196 | void |
| 197 | evalvars_init(void) |
| 198 | { |
| 199 | int i; |
| 200 | struct vimvar *p; |
| 201 | |
| 202 | init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE); |
| 203 | init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE); |
| 204 | vimvardict.dv_lock = VAR_FIXED; |
| 205 | hash_init(&compat_hashtab); |
| 206 | |
| 207 | for (i = 0; i < VV_LEN; ++i) |
| 208 | { |
| 209 | p = &vimvars[i]; |
| 210 | if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN) |
| 211 | { |
| 212 | iemsg("INTERNAL: name too long, increase size of dictitem16_T"); |
| 213 | getout(1); |
| 214 | } |
| 215 | STRCPY(p->vv_di.di_key, p->vv_name); |
| 216 | if (p->vv_flags & VV_RO) |
| 217 | p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; |
| 218 | else if (p->vv_flags & VV_RO_SBX) |
| 219 | p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX; |
| 220 | else |
| 221 | p->vv_di.di_flags = DI_FLAGS_FIX; |
| 222 | |
| 223 | // add to v: scope dict, unless the value is not always available |
| 224 | if (p->vv_type != VAR_UNKNOWN) |
| 225 | hash_add(&vimvarht, p->vv_di.di_key); |
| 226 | if (p->vv_flags & VV_COMPAT) |
| 227 | // add to compat scope dict |
| 228 | hash_add(&compat_hashtab, p->vv_di.di_key); |
| 229 | } |
| 230 | vimvars[VV_VERSION].vv_nr = VIM_VERSION_100; |
| 231 | vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch(); |
| 232 | |
| 233 | set_vim_var_nr(VV_SEARCHFORWARD, 1L); |
| 234 | set_vim_var_nr(VV_HLSEARCH, 1L); |
| 235 | set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED)); |
| 236 | set_vim_var_list(VV_ERRORS, list_alloc()); |
| 237 | set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED)); |
| 238 | |
| 239 | set_vim_var_nr(VV_FALSE, VVAL_FALSE); |
| 240 | set_vim_var_nr(VV_TRUE, VVAL_TRUE); |
| 241 | set_vim_var_nr(VV_NONE, VVAL_NONE); |
| 242 | set_vim_var_nr(VV_NULL, VVAL_NULL); |
| 243 | |
| 244 | set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER); |
| 245 | set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING); |
| 246 | set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC); |
| 247 | set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST); |
| 248 | set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT); |
| 249 | set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT); |
| 250 | set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL); |
| 251 | set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE); |
| 252 | set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB); |
| 253 | set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL); |
| 254 | set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB); |
| 255 | |
| 256 | set_vim_var_nr(VV_ECHOSPACE, sc_col - 1); |
| 257 | |
| 258 | set_reg_var(0); // default for v:register is not 0 but '"' |
| 259 | } |
| 260 | |
| 261 | #if defined(EXITFREE) || defined(PROTO) |
| 262 | /* |
| 263 | * Free all vim variables information on exit |
| 264 | */ |
| 265 | void |
| 266 | evalvars_clear(void) |
| 267 | { |
| 268 | int i; |
| 269 | struct vimvar *p; |
| 270 | |
| 271 | for (i = 0; i < VV_LEN; ++i) |
| 272 | { |
| 273 | p = &vimvars[i]; |
| 274 | if (p->vv_di.di_tv.v_type == VAR_STRING) |
| 275 | VIM_CLEAR(p->vv_str); |
| 276 | else if (p->vv_di.di_tv.v_type == VAR_LIST) |
| 277 | { |
| 278 | list_unref(p->vv_list); |
| 279 | p->vv_list = NULL; |
| 280 | } |
| 281 | } |
| 282 | hash_clear(&vimvarht); |
| 283 | hash_init(&vimvarht); // garbage_collect() will access it |
| 284 | hash_clear(&compat_hashtab); |
| 285 | |
| 286 | // global variables |
| 287 | vars_clear(&globvarht); |
| 288 | |
| 289 | // Script-local variables. First clear all the variables and in a second |
| 290 | // loop free the scriptvar_T, because a variable in one script might hold |
| 291 | // a reference to the whole scope of another script. |
| 292 | for (i = 1; i <= ga_scripts.ga_len; ++i) |
| 293 | vars_clear(&SCRIPT_VARS(i)); |
| 294 | for (i = 1; i <= ga_scripts.ga_len; ++i) |
| 295 | vim_free(SCRIPT_SV(i)); |
| 296 | ga_clear(&ga_scripts); |
| 297 | } |
| 298 | #endif |
| 299 | |
| 300 | int |
| 301 | garbage_collect_vimvars(int copyID) |
| 302 | { |
| 303 | return set_ref_in_ht(&vimvarht, copyID, NULL); |
| 304 | } |
| 305 | |
| 306 | int |
| 307 | garbage_collect_scriptvars(int copyID) |
| 308 | { |
| 309 | int i; |
| 310 | int abort = FALSE; |
| 311 | |
| 312 | for (i = 1; i <= ga_scripts.ga_len; ++i) |
| 313 | abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL); |
| 314 | |
| 315 | return abort; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Set an internal variable to a string value. Creates the variable if it does |
| 320 | * not already exist. |
| 321 | */ |
| 322 | void |
| 323 | set_internal_string_var(char_u *name, char_u *value) |
| 324 | { |
| 325 | char_u *val; |
| 326 | typval_T *tvp; |
| 327 | |
| 328 | val = vim_strsave(value); |
| 329 | if (val != NULL) |
| 330 | { |
| 331 | tvp = alloc_string_tv(val); |
| 332 | if (tvp != NULL) |
| 333 | { |
| 334 | set_var(name, tvp, FALSE); |
| 335 | free_tv(tvp); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Prepare v: variable "idx" to be used. |
| 342 | * Save the current typeval in "save_tv". |
| 343 | * When not used yet add the variable to the v: hashtable. |
| 344 | */ |
| 345 | void |
| 346 | prepare_vimvar(int idx, typval_T *save_tv) |
| 347 | { |
| 348 | *save_tv = vimvars[idx].vv_tv; |
| 349 | if (vimvars[idx].vv_type == VAR_UNKNOWN) |
| 350 | hash_add(&vimvarht, vimvars[idx].vv_di.di_key); |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Restore v: variable "idx" to typeval "save_tv". |
| 355 | * When no longer defined, remove the variable from the v: hashtable. |
| 356 | */ |
| 357 | void |
| 358 | restore_vimvar(int idx, typval_T *save_tv) |
| 359 | { |
| 360 | hashitem_T *hi; |
| 361 | |
| 362 | vimvars[idx].vv_tv = *save_tv; |
| 363 | if (vimvars[idx].vv_type == VAR_UNKNOWN) |
| 364 | { |
| 365 | hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key); |
| 366 | if (HASHITEM_EMPTY(hi)) |
| 367 | internal_error("restore_vimvar()"); |
| 368 | else |
| 369 | hash_remove(&vimvarht, hi); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * List Vim variables. |
| 375 | */ |
| 376 | static void |
| 377 | list_vim_vars(int *first) |
| 378 | { |
| 379 | list_hashtable_vars(&vimvarht, "v:", FALSE, first); |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * List script-local variables, if there is a script. |
| 384 | */ |
| 385 | static void |
| 386 | list_script_vars(int *first) |
| 387 | { |
| 388 | if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len) |
| 389 | list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid), |
| 390 | "s:", FALSE, first); |
| 391 | } |
| 392 | |
| 393 | /* |
Bram Moolenaar | 0522ba0 | 2019-08-27 22:48:30 +0200 | [diff] [blame] | 394 | * Get a list of lines from a HERE document. The here document is a list of |
| 395 | * lines surrounded by a marker. |
| 396 | * cmd << {marker} |
| 397 | * {line1} |
| 398 | * {line2} |
| 399 | * .... |
| 400 | * {marker} |
| 401 | * |
| 402 | * The {marker} is a string. If the optional 'trim' word is supplied before the |
| 403 | * marker, then the leading indentation before the lines (matching the |
| 404 | * indentation in the 'cmd' line) is stripped. |
| 405 | * Returns a List with {lines} or NULL. |
| 406 | */ |
| 407 | static list_T * |
| 408 | heredoc_get(exarg_T *eap, char_u *cmd) |
| 409 | { |
| 410 | char_u *theline; |
| 411 | char_u *marker; |
| 412 | list_T *l; |
| 413 | char_u *p; |
| 414 | int marker_indent_len = 0; |
| 415 | int text_indent_len = 0; |
| 416 | char_u *text_indent = NULL; |
| 417 | |
| 418 | if (eap->getline == NULL) |
| 419 | { |
| 420 | emsg(_("E991: cannot use =<< here")); |
| 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | // Check for the optional 'trim' word before the marker |
| 425 | cmd = skipwhite(cmd); |
| 426 | if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4]))) |
| 427 | { |
| 428 | cmd = skipwhite(cmd + 4); |
| 429 | |
| 430 | // Trim the indentation from all the lines in the here document. |
| 431 | // The amount of indentation trimmed is the same as the indentation of |
| 432 | // the first line after the :let command line. To find the end marker |
| 433 | // the indent of the :let command line is trimmed. |
| 434 | p = *eap->cmdlinep; |
| 435 | while (VIM_ISWHITE(*p)) |
| 436 | { |
| 437 | p++; |
| 438 | marker_indent_len++; |
| 439 | } |
| 440 | text_indent_len = -1; |
| 441 | } |
| 442 | |
| 443 | // The marker is the next word. |
| 444 | if (*cmd != NUL && *cmd != '"') |
| 445 | { |
| 446 | marker = skipwhite(cmd); |
| 447 | p = skiptowhite(marker); |
| 448 | if (*skipwhite(p) != NUL && *skipwhite(p) != '"') |
| 449 | { |
| 450 | emsg(_(e_trailing)); |
| 451 | return NULL; |
| 452 | } |
| 453 | *p = NUL; |
| 454 | if (vim_islower(*marker)) |
| 455 | { |
| 456 | emsg(_("E221: Marker cannot start with lower case letter")); |
| 457 | return NULL; |
| 458 | } |
| 459 | } |
| 460 | else |
| 461 | { |
| 462 | emsg(_("E172: Missing marker")); |
| 463 | return NULL; |
| 464 | } |
| 465 | |
| 466 | l = list_alloc(); |
| 467 | if (l == NULL) |
| 468 | return NULL; |
| 469 | |
| 470 | for (;;) |
| 471 | { |
| 472 | int mi = 0; |
| 473 | int ti = 0; |
| 474 | |
| 475 | theline = eap->getline(NUL, eap->cookie, 0, FALSE); |
| 476 | if (theline == NULL) |
| 477 | { |
| 478 | semsg(_("E990: Missing end marker '%s'"), marker); |
| 479 | break; |
| 480 | } |
| 481 | |
| 482 | // with "trim": skip the indent matching the :let line to find the |
| 483 | // marker |
| 484 | if (marker_indent_len > 0 |
| 485 | && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0) |
| 486 | mi = marker_indent_len; |
| 487 | if (STRCMP(marker, theline + mi) == 0) |
| 488 | { |
| 489 | vim_free(theline); |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | if (text_indent_len == -1 && *theline != NUL) |
| 494 | { |
| 495 | // set the text indent from the first line. |
| 496 | p = theline; |
| 497 | text_indent_len = 0; |
| 498 | while (VIM_ISWHITE(*p)) |
| 499 | { |
| 500 | p++; |
| 501 | text_indent_len++; |
| 502 | } |
| 503 | text_indent = vim_strnsave(theline, text_indent_len); |
| 504 | } |
| 505 | // with "trim": skip the indent matching the first line |
| 506 | if (text_indent != NULL) |
| 507 | for (ti = 0; ti < text_indent_len; ++ti) |
| 508 | if (theline[ti] != text_indent[ti]) |
| 509 | break; |
| 510 | |
| 511 | if (list_append_string(l, theline + ti, -1) == FAIL) |
| 512 | break; |
| 513 | vim_free(theline); |
| 514 | } |
| 515 | vim_free(text_indent); |
| 516 | |
| 517 | return l; |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * ":let" list all variable values |
| 522 | * ":let var1 var2" list variable values |
| 523 | * ":let var = expr" assignment command. |
| 524 | * ":let var += expr" assignment command. |
| 525 | * ":let var -= expr" assignment command. |
| 526 | * ":let var *= expr" assignment command. |
| 527 | * ":let var /= expr" assignment command. |
| 528 | * ":let var %= expr" assignment command. |
| 529 | * ":let var .= expr" assignment command. |
| 530 | * ":let var ..= expr" assignment command. |
| 531 | * ":let [var1, var2] = expr" unpack list. |
| 532 | */ |
| 533 | void |
| 534 | ex_let(exarg_T *eap) |
| 535 | { |
| 536 | ex_let_const(eap, FALSE); |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * ":const" list all variable values |
| 541 | * ":const var1 var2" list variable values |
| 542 | * ":const var = expr" assignment command. |
| 543 | * ":const [var1, var2] = expr" unpack list. |
| 544 | */ |
| 545 | void |
| 546 | ex_const(exarg_T *eap) |
| 547 | { |
| 548 | ex_let_const(eap, TRUE); |
| 549 | } |
| 550 | |
| 551 | static void |
| 552 | ex_let_const(exarg_T *eap, int is_const) |
| 553 | { |
| 554 | char_u *arg = eap->arg; |
| 555 | char_u *expr = NULL; |
| 556 | typval_T rettv; |
| 557 | int i; |
| 558 | int var_count = 0; |
| 559 | int semicolon = 0; |
| 560 | char_u op[2]; |
| 561 | char_u *argend; |
| 562 | int first = TRUE; |
| 563 | int concat; |
| 564 | |
| 565 | argend = skip_var_list(arg, &var_count, &semicolon); |
| 566 | if (argend == NULL) |
| 567 | return; |
| 568 | if (argend > arg && argend[-1] == '.') // for var.='str' |
| 569 | --argend; |
| 570 | expr = skipwhite(argend); |
| 571 | concat = expr[0] == '.' |
| 572 | && ((expr[1] == '=' && current_sctx.sc_version < 2) |
| 573 | || (expr[1] == '.' && expr[2] == '=')); |
| 574 | if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL |
| 575 | && expr[1] == '=') || concat)) |
| 576 | { |
| 577 | // ":let" without "=": list variables |
| 578 | if (*arg == '[') |
| 579 | emsg(_(e_invarg)); |
| 580 | else if (expr[0] == '.') |
| 581 | emsg(_("E985: .= is not supported with script version 2")); |
| 582 | else if (!ends_excmd(*arg)) |
| 583 | // ":let var1 var2" |
| 584 | arg = list_arg_vars(eap, arg, &first); |
| 585 | else if (!eap->skip) |
| 586 | { |
| 587 | // ":let" |
| 588 | list_glob_vars(&first); |
| 589 | list_buf_vars(&first); |
| 590 | list_win_vars(&first); |
| 591 | list_tab_vars(&first); |
| 592 | list_script_vars(&first); |
| 593 | list_func_vars(&first); |
| 594 | list_vim_vars(&first); |
| 595 | } |
| 596 | eap->nextcmd = check_nextcmd(arg); |
| 597 | } |
| 598 | else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<') |
| 599 | { |
| 600 | list_T *l; |
| 601 | |
| 602 | // HERE document |
| 603 | l = heredoc_get(eap, expr + 3); |
| 604 | if (l != NULL) |
| 605 | { |
| 606 | rettv_list_set(&rettv, l); |
| 607 | op[0] = '='; |
| 608 | op[1] = NUL; |
| 609 | (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count, |
| 610 | is_const, op); |
| 611 | clear_tv(&rettv); |
| 612 | } |
| 613 | } |
| 614 | else |
| 615 | { |
| 616 | op[0] = '='; |
| 617 | op[1] = NUL; |
| 618 | if (*expr != '=') |
| 619 | { |
| 620 | if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL) |
| 621 | { |
| 622 | op[0] = *expr; // +=, -=, *=, /=, %= or .= |
| 623 | if (expr[0] == '.' && expr[1] == '.') // ..= |
| 624 | ++expr; |
| 625 | } |
| 626 | expr = skipwhite(expr + 2); |
| 627 | } |
| 628 | else |
| 629 | expr = skipwhite(expr + 1); |
| 630 | |
| 631 | if (eap->skip) |
| 632 | ++emsg_skip; |
| 633 | i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip); |
| 634 | if (eap->skip) |
| 635 | { |
| 636 | if (i != FAIL) |
| 637 | clear_tv(&rettv); |
| 638 | --emsg_skip; |
| 639 | } |
| 640 | else if (i != FAIL) |
| 641 | { |
| 642 | (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count, |
| 643 | is_const, op); |
| 644 | clear_tv(&rettv); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Assign the typevalue "tv" to the variable or variables at "arg_start". |
| 651 | * Handles both "var" with any type and "[var, var; var]" with a list type. |
| 652 | * When "op" is not NULL it points to a string with characters that |
| 653 | * must appear after the variable(s). Use "+", "-" or "." for add, subtract |
| 654 | * or concatenate. |
| 655 | * Returns OK or FAIL; |
| 656 | */ |
| 657 | int |
| 658 | ex_let_vars( |
| 659 | char_u *arg_start, |
| 660 | typval_T *tv, |
| 661 | int copy, // copy values from "tv", don't move |
| 662 | int semicolon, // from skip_var_list() |
| 663 | int var_count, // from skip_var_list() |
| 664 | int is_const, // lock variables for const |
| 665 | char_u *op) |
| 666 | { |
| 667 | char_u *arg = arg_start; |
| 668 | list_T *l; |
| 669 | int i; |
| 670 | listitem_T *item; |
| 671 | typval_T ltv; |
| 672 | |
| 673 | if (*arg != '[') |
| 674 | { |
| 675 | // ":let var = expr" or ":for var in list" |
| 676 | if (ex_let_one(arg, tv, copy, is_const, op, op) == NULL) |
| 677 | return FAIL; |
| 678 | return OK; |
| 679 | } |
| 680 | |
| 681 | // ":let [v1, v2] = list" or ":for [v1, v2] in listlist" |
| 682 | if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL) |
| 683 | { |
| 684 | emsg(_(e_listreq)); |
| 685 | return FAIL; |
| 686 | } |
| 687 | |
| 688 | i = list_len(l); |
| 689 | if (semicolon == 0 && var_count < i) |
| 690 | { |
| 691 | emsg(_("E687: Less targets than List items")); |
| 692 | return FAIL; |
| 693 | } |
| 694 | if (var_count - semicolon > i) |
| 695 | { |
| 696 | emsg(_("E688: More targets than List items")); |
| 697 | return FAIL; |
| 698 | } |
| 699 | |
| 700 | item = l->lv_first; |
| 701 | while (*arg != ']') |
| 702 | { |
| 703 | arg = skipwhite(arg + 1); |
| 704 | arg = ex_let_one(arg, &item->li_tv, TRUE, is_const, |
| 705 | (char_u *)",;]", op); |
| 706 | item = item->li_next; |
| 707 | if (arg == NULL) |
| 708 | return FAIL; |
| 709 | |
| 710 | arg = skipwhite(arg); |
| 711 | if (*arg == ';') |
| 712 | { |
| 713 | // Put the rest of the list (may be empty) in the var after ';'. |
| 714 | // Create a new list for this. |
| 715 | l = list_alloc(); |
| 716 | if (l == NULL) |
| 717 | return FAIL; |
| 718 | while (item != NULL) |
| 719 | { |
| 720 | list_append_tv(l, &item->li_tv); |
| 721 | item = item->li_next; |
| 722 | } |
| 723 | |
| 724 | ltv.v_type = VAR_LIST; |
| 725 | ltv.v_lock = 0; |
| 726 | ltv.vval.v_list = l; |
| 727 | l->lv_refcount = 1; |
| 728 | |
| 729 | arg = ex_let_one(skipwhite(arg + 1), <v, FALSE, is_const, |
| 730 | (char_u *)"]", op); |
| 731 | clear_tv(<v); |
| 732 | if (arg == NULL) |
| 733 | return FAIL; |
| 734 | break; |
| 735 | } |
| 736 | else if (*arg != ',' && *arg != ']') |
| 737 | { |
| 738 | internal_error("ex_let_vars()"); |
| 739 | return FAIL; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | return OK; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Skip over assignable variable "var" or list of variables "[var, var]". |
| 748 | * Used for ":let varvar = expr" and ":for varvar in expr". |
| 749 | * For "[var, var]" increment "*var_count" for each variable. |
| 750 | * for "[var, var; var]" set "semicolon". |
| 751 | * Return NULL for an error. |
| 752 | */ |
| 753 | char_u * |
| 754 | skip_var_list( |
| 755 | char_u *arg, |
| 756 | int *var_count, |
| 757 | int *semicolon) |
| 758 | { |
| 759 | char_u *p, *s; |
| 760 | |
| 761 | if (*arg == '[') |
| 762 | { |
| 763 | // "[var, var]": find the matching ']'. |
| 764 | p = arg; |
| 765 | for (;;) |
| 766 | { |
| 767 | p = skipwhite(p + 1); // skip whites after '[', ';' or ',' |
| 768 | s = skip_var_one(p); |
| 769 | if (s == p) |
| 770 | { |
| 771 | semsg(_(e_invarg2), p); |
| 772 | return NULL; |
| 773 | } |
| 774 | ++*var_count; |
| 775 | |
| 776 | p = skipwhite(s); |
| 777 | if (*p == ']') |
| 778 | break; |
| 779 | else if (*p == ';') |
| 780 | { |
| 781 | if (*semicolon == 1) |
| 782 | { |
| 783 | emsg(_("Double ; in list of variables")); |
| 784 | return NULL; |
| 785 | } |
| 786 | *semicolon = 1; |
| 787 | } |
| 788 | else if (*p != ',') |
| 789 | { |
| 790 | semsg(_(e_invarg2), p); |
| 791 | return NULL; |
| 792 | } |
| 793 | } |
| 794 | return p + 1; |
| 795 | } |
| 796 | else |
| 797 | return skip_var_one(arg); |
| 798 | } |
| 799 | |
| 800 | /* |
| 801 | * Skip one (assignable) variable name, including @r, $VAR, &option, d.key, |
| 802 | * l[idx]. |
| 803 | */ |
| 804 | static char_u * |
| 805 | skip_var_one(char_u *arg) |
| 806 | { |
| 807 | if (*arg == '@' && arg[1] != NUL) |
| 808 | return arg + 2; |
| 809 | return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg, |
| 810 | NULL, NULL, FNE_INCL_BR | FNE_CHECK_START); |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * List variables for hashtab "ht" with prefix "prefix". |
| 815 | * If "empty" is TRUE also list NULL strings as empty strings. |
| 816 | */ |
| 817 | void |
| 818 | list_hashtable_vars( |
| 819 | hashtab_T *ht, |
| 820 | char *prefix, |
| 821 | int empty, |
| 822 | int *first) |
| 823 | { |
| 824 | hashitem_T *hi; |
| 825 | dictitem_T *di; |
| 826 | int todo; |
| 827 | char_u buf[IOSIZE]; |
| 828 | |
| 829 | todo = (int)ht->ht_used; |
| 830 | for (hi = ht->ht_array; todo > 0 && !got_int; ++hi) |
| 831 | { |
| 832 | if (!HASHITEM_EMPTY(hi)) |
| 833 | { |
| 834 | --todo; |
| 835 | di = HI2DI(hi); |
| 836 | |
| 837 | // apply :filter /pat/ to variable name |
| 838 | vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1); |
| 839 | vim_strcat((char_u *)buf, di->di_key, IOSIZE); |
| 840 | if (message_filtered(buf)) |
| 841 | continue; |
| 842 | |
| 843 | if (empty || di->di_tv.v_type != VAR_STRING |
| 844 | || di->di_tv.vval.v_string != NULL) |
| 845 | list_one_var(di, prefix, first); |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * List global variables. |
| 852 | */ |
| 853 | static void |
| 854 | list_glob_vars(int *first) |
| 855 | { |
| 856 | list_hashtable_vars(&globvarht, "", TRUE, first); |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * List buffer variables. |
| 861 | */ |
| 862 | static void |
| 863 | list_buf_vars(int *first) |
| 864 | { |
| 865 | list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first); |
| 866 | } |
| 867 | |
| 868 | /* |
| 869 | * List window variables. |
| 870 | */ |
| 871 | static void |
| 872 | list_win_vars(int *first) |
| 873 | { |
| 874 | list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first); |
| 875 | } |
| 876 | |
| 877 | /* |
| 878 | * List tab page variables. |
| 879 | */ |
| 880 | static void |
| 881 | list_tab_vars(int *first) |
| 882 | { |
| 883 | list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first); |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * List variables in "arg". |
| 888 | */ |
| 889 | static char_u * |
| 890 | list_arg_vars(exarg_T *eap, char_u *arg, int *first) |
| 891 | { |
| 892 | int error = FALSE; |
| 893 | int len; |
| 894 | char_u *name; |
| 895 | char_u *name_start; |
| 896 | char_u *arg_subsc; |
| 897 | char_u *tofree; |
| 898 | typval_T tv; |
| 899 | |
| 900 | while (!ends_excmd(*arg) && !got_int) |
| 901 | { |
| 902 | if (error || eap->skip) |
| 903 | { |
| 904 | arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START); |
| 905 | if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg)) |
| 906 | { |
| 907 | emsg_severe = TRUE; |
| 908 | emsg(_(e_trailing)); |
| 909 | break; |
| 910 | } |
| 911 | } |
| 912 | else |
| 913 | { |
| 914 | // get_name_len() takes care of expanding curly braces |
| 915 | name_start = name = arg; |
| 916 | len = get_name_len(&arg, &tofree, TRUE, TRUE); |
| 917 | if (len <= 0) |
| 918 | { |
| 919 | // This is mainly to keep test 49 working: when expanding |
| 920 | // curly braces fails overrule the exception error message. |
| 921 | if (len < 0 && !aborting()) |
| 922 | { |
| 923 | emsg_severe = TRUE; |
| 924 | semsg(_(e_invarg2), arg); |
| 925 | break; |
| 926 | } |
| 927 | error = TRUE; |
| 928 | } |
| 929 | else |
| 930 | { |
| 931 | if (tofree != NULL) |
| 932 | name = tofree; |
| 933 | if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL) |
| 934 | error = TRUE; |
| 935 | else |
| 936 | { |
| 937 | // handle d.key, l[idx], f(expr) |
| 938 | arg_subsc = arg; |
| 939 | if (handle_subscript(&arg, &tv, TRUE, TRUE, |
| 940 | name, &name) == FAIL) |
| 941 | error = TRUE; |
| 942 | else |
| 943 | { |
| 944 | if (arg == arg_subsc && len == 2 && name[1] == ':') |
| 945 | { |
| 946 | switch (*name) |
| 947 | { |
| 948 | case 'g': list_glob_vars(first); break; |
| 949 | case 'b': list_buf_vars(first); break; |
| 950 | case 'w': list_win_vars(first); break; |
| 951 | case 't': list_tab_vars(first); break; |
| 952 | case 'v': list_vim_vars(first); break; |
| 953 | case 's': list_script_vars(first); break; |
| 954 | case 'l': list_func_vars(first); break; |
| 955 | default: |
| 956 | semsg(_("E738: Can't list variables for %s"), name); |
| 957 | } |
| 958 | } |
| 959 | else |
| 960 | { |
| 961 | char_u numbuf[NUMBUFLEN]; |
| 962 | char_u *tf; |
| 963 | int c; |
| 964 | char_u *s; |
| 965 | |
| 966 | s = echo_string(&tv, &tf, numbuf, 0); |
| 967 | c = *arg; |
| 968 | *arg = NUL; |
| 969 | list_one_var_a("", |
| 970 | arg == arg_subsc ? name : name_start, |
| 971 | tv.v_type, |
| 972 | s == NULL ? (char_u *)"" : s, |
| 973 | first); |
| 974 | *arg = c; |
| 975 | vim_free(tf); |
| 976 | } |
| 977 | clear_tv(&tv); |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | vim_free(tofree); |
| 983 | } |
| 984 | |
| 985 | arg = skipwhite(arg); |
| 986 | } |
| 987 | |
| 988 | return arg; |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value. |
| 993 | * Returns a pointer to the char just after the var name. |
| 994 | * Returns NULL if there is an error. |
| 995 | */ |
| 996 | static char_u * |
| 997 | ex_let_one( |
| 998 | char_u *arg, // points to variable name |
| 999 | typval_T *tv, // value to assign to variable |
| 1000 | int copy, // copy value from "tv" |
| 1001 | int is_const, // lock variable for const |
| 1002 | char_u *endchars, // valid chars after variable name or NULL |
| 1003 | char_u *op) // "+", "-", "." or NULL |
| 1004 | { |
| 1005 | int c1; |
| 1006 | char_u *name; |
| 1007 | char_u *p; |
| 1008 | char_u *arg_end = NULL; |
| 1009 | int len; |
| 1010 | int opt_flags; |
| 1011 | char_u *tofree = NULL; |
| 1012 | |
| 1013 | // ":let $VAR = expr": Set environment variable. |
| 1014 | if (*arg == '$') |
| 1015 | { |
| 1016 | if (is_const) |
| 1017 | { |
| 1018 | emsg(_("E996: Cannot lock an environment variable")); |
| 1019 | return NULL; |
| 1020 | } |
| 1021 | // Find the end of the name. |
| 1022 | ++arg; |
| 1023 | name = arg; |
| 1024 | len = get_env_len(&arg); |
| 1025 | if (len == 0) |
| 1026 | semsg(_(e_invarg2), name - 1); |
| 1027 | else |
| 1028 | { |
| 1029 | if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL) |
| 1030 | semsg(_(e_letwrong), op); |
| 1031 | else if (endchars != NULL |
| 1032 | && vim_strchr(endchars, *skipwhite(arg)) == NULL) |
| 1033 | emsg(_(e_letunexp)); |
| 1034 | else if (!check_secure()) |
| 1035 | { |
| 1036 | c1 = name[len]; |
| 1037 | name[len] = NUL; |
| 1038 | p = tv_get_string_chk(tv); |
| 1039 | if (p != NULL && op != NULL && *op == '.') |
| 1040 | { |
| 1041 | int mustfree = FALSE; |
| 1042 | char_u *s = vim_getenv(name, &mustfree); |
| 1043 | |
| 1044 | if (s != NULL) |
| 1045 | { |
| 1046 | p = tofree = concat_str(s, p); |
| 1047 | if (mustfree) |
| 1048 | vim_free(s); |
| 1049 | } |
| 1050 | } |
| 1051 | if (p != NULL) |
| 1052 | { |
| 1053 | vim_setenv(name, p); |
| 1054 | if (STRICMP(name, "HOME") == 0) |
| 1055 | init_homedir(); |
| 1056 | else if (didset_vim && STRICMP(name, "VIM") == 0) |
| 1057 | didset_vim = FALSE; |
| 1058 | else if (didset_vimruntime |
| 1059 | && STRICMP(name, "VIMRUNTIME") == 0) |
| 1060 | didset_vimruntime = FALSE; |
| 1061 | arg_end = arg; |
| 1062 | } |
| 1063 | name[len] = c1; |
| 1064 | vim_free(tofree); |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | // ":let &option = expr": Set option value. |
| 1070 | // ":let &l:option = expr": Set local option value. |
| 1071 | // ":let &g:option = expr": Set global option value. |
| 1072 | else if (*arg == '&') |
| 1073 | { |
| 1074 | if (is_const) |
| 1075 | { |
| 1076 | emsg(_("E996: Cannot lock an option")); |
| 1077 | return NULL; |
| 1078 | } |
| 1079 | // Find the end of the name. |
| 1080 | p = find_option_end(&arg, &opt_flags); |
| 1081 | if (p == NULL || (endchars != NULL |
| 1082 | && vim_strchr(endchars, *skipwhite(p)) == NULL)) |
| 1083 | emsg(_(e_letunexp)); |
| 1084 | else |
| 1085 | { |
| 1086 | long n; |
| 1087 | int opt_type; |
| 1088 | long numval; |
| 1089 | char_u *stringval = NULL; |
| 1090 | char_u *s; |
| 1091 | |
| 1092 | c1 = *p; |
| 1093 | *p = NUL; |
| 1094 | |
| 1095 | n = (long)tv_get_number(tv); |
| 1096 | s = tv_get_string_chk(tv); // != NULL if number or string |
| 1097 | if (s != NULL && op != NULL && *op != '=') |
| 1098 | { |
| 1099 | opt_type = get_option_value(arg, &numval, |
| 1100 | &stringval, opt_flags); |
| 1101 | if ((opt_type == 1 && *op == '.') |
| 1102 | || (opt_type == 0 && *op != '.')) |
| 1103 | { |
| 1104 | semsg(_(e_letwrong), op); |
| 1105 | s = NULL; // don't set the value |
| 1106 | } |
| 1107 | else |
| 1108 | { |
| 1109 | if (opt_type == 1) // number |
| 1110 | { |
| 1111 | switch (*op) |
| 1112 | { |
| 1113 | case '+': n = numval + n; break; |
| 1114 | case '-': n = numval - n; break; |
| 1115 | case '*': n = numval * n; break; |
| 1116 | case '/': n = (long)num_divide(numval, n); break; |
| 1117 | case '%': n = (long)num_modulus(numval, n); break; |
| 1118 | } |
| 1119 | } |
| 1120 | else if (opt_type == 0 && stringval != NULL) // string |
| 1121 | { |
| 1122 | s = concat_str(stringval, s); |
| 1123 | vim_free(stringval); |
| 1124 | stringval = s; |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | if (s != NULL) |
| 1129 | { |
| 1130 | set_option_value(arg, n, s, opt_flags); |
| 1131 | arg_end = p; |
| 1132 | } |
| 1133 | *p = c1; |
| 1134 | vim_free(stringval); |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | // ":let @r = expr": Set register contents. |
| 1139 | else if (*arg == '@') |
| 1140 | { |
| 1141 | if (is_const) |
| 1142 | { |
| 1143 | emsg(_("E996: Cannot lock a register")); |
| 1144 | return NULL; |
| 1145 | } |
| 1146 | ++arg; |
| 1147 | if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL) |
| 1148 | semsg(_(e_letwrong), op); |
| 1149 | else if (endchars != NULL |
| 1150 | && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL) |
| 1151 | emsg(_(e_letunexp)); |
| 1152 | else |
| 1153 | { |
| 1154 | char_u *ptofree = NULL; |
| 1155 | char_u *s; |
| 1156 | |
| 1157 | p = tv_get_string_chk(tv); |
| 1158 | if (p != NULL && op != NULL && *op == '.') |
| 1159 | { |
| 1160 | s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC); |
| 1161 | if (s != NULL) |
| 1162 | { |
| 1163 | p = ptofree = concat_str(s, p); |
| 1164 | vim_free(s); |
| 1165 | } |
| 1166 | } |
| 1167 | if (p != NULL) |
| 1168 | { |
| 1169 | write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE); |
| 1170 | arg_end = arg + 1; |
| 1171 | } |
| 1172 | vim_free(ptofree); |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | // ":let var = expr": Set internal variable. |
| 1177 | // ":let {expr} = expr": Idem, name made with curly braces |
| 1178 | else if (eval_isnamec1(*arg) || *arg == '{') |
| 1179 | { |
| 1180 | lval_T lv; |
| 1181 | |
| 1182 | p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START); |
| 1183 | if (p != NULL && lv.ll_name != NULL) |
| 1184 | { |
| 1185 | if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL) |
| 1186 | emsg(_(e_letunexp)); |
| 1187 | else |
| 1188 | { |
| 1189 | set_var_lval(&lv, p, tv, copy, is_const, op); |
| 1190 | arg_end = p; |
| 1191 | } |
| 1192 | } |
| 1193 | clear_lval(&lv); |
| 1194 | } |
| 1195 | |
| 1196 | else |
| 1197 | semsg(_(e_invarg2), arg); |
| 1198 | |
| 1199 | return arg_end; |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | * ":unlet[!] var1 ... " command. |
| 1204 | */ |
| 1205 | void |
| 1206 | ex_unlet(exarg_T *eap) |
| 1207 | { |
| 1208 | ex_unletlock(eap, eap->arg, 0); |
| 1209 | } |
| 1210 | |
| 1211 | /* |
| 1212 | * ":lockvar" and ":unlockvar" commands |
| 1213 | */ |
| 1214 | void |
| 1215 | ex_lockvar(exarg_T *eap) |
| 1216 | { |
| 1217 | char_u *arg = eap->arg; |
| 1218 | int deep = 2; |
| 1219 | |
| 1220 | if (eap->forceit) |
| 1221 | deep = -1; |
| 1222 | else if (vim_isdigit(*arg)) |
| 1223 | { |
| 1224 | deep = getdigits(&arg); |
| 1225 | arg = skipwhite(arg); |
| 1226 | } |
| 1227 | |
| 1228 | ex_unletlock(eap, arg, deep); |
| 1229 | } |
| 1230 | |
| 1231 | /* |
| 1232 | * ":unlet", ":lockvar" and ":unlockvar" are quite similar. |
| 1233 | */ |
| 1234 | static void |
| 1235 | ex_unletlock( |
| 1236 | exarg_T *eap, |
| 1237 | char_u *argstart, |
| 1238 | int deep) |
| 1239 | { |
| 1240 | char_u *arg = argstart; |
| 1241 | char_u *name_end; |
| 1242 | int error = FALSE; |
| 1243 | lval_T lv; |
| 1244 | |
| 1245 | do |
| 1246 | { |
| 1247 | if (*arg == '$') |
| 1248 | { |
| 1249 | char_u *name = ++arg; |
| 1250 | |
| 1251 | if (get_env_len(&arg) == 0) |
| 1252 | { |
| 1253 | semsg(_(e_invarg2), name - 1); |
| 1254 | return; |
| 1255 | } |
| 1256 | vim_unsetenv(name); |
| 1257 | arg = skipwhite(arg); |
| 1258 | continue; |
| 1259 | } |
| 1260 | |
| 1261 | // Parse the name and find the end. |
| 1262 | name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0, |
| 1263 | FNE_CHECK_START); |
| 1264 | if (lv.ll_name == NULL) |
| 1265 | error = TRUE; // error but continue parsing |
| 1266 | if (name_end == NULL || (!VIM_ISWHITE(*name_end) |
| 1267 | && !ends_excmd(*name_end))) |
| 1268 | { |
| 1269 | if (name_end != NULL) |
| 1270 | { |
| 1271 | emsg_severe = TRUE; |
| 1272 | emsg(_(e_trailing)); |
| 1273 | } |
| 1274 | if (!(eap->skip || error)) |
| 1275 | clear_lval(&lv); |
| 1276 | break; |
| 1277 | } |
| 1278 | |
| 1279 | if (!error && !eap->skip) |
| 1280 | { |
| 1281 | if (eap->cmdidx == CMD_unlet) |
| 1282 | { |
| 1283 | if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL) |
| 1284 | error = TRUE; |
| 1285 | } |
| 1286 | else |
| 1287 | { |
| 1288 | if (do_lock_var(&lv, name_end, deep, |
| 1289 | eap->cmdidx == CMD_lockvar) == FAIL) |
| 1290 | error = TRUE; |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | if (!eap->skip) |
| 1295 | clear_lval(&lv); |
| 1296 | |
| 1297 | arg = skipwhite(name_end); |
| 1298 | } while (!ends_excmd(*arg)); |
| 1299 | |
| 1300 | eap->nextcmd = check_nextcmd(arg); |
| 1301 | } |
| 1302 | |
| 1303 | static int |
| 1304 | do_unlet_var( |
| 1305 | lval_T *lp, |
| 1306 | char_u *name_end, |
| 1307 | int forceit) |
| 1308 | { |
| 1309 | int ret = OK; |
| 1310 | int cc; |
| 1311 | |
| 1312 | if (lp->ll_tv == NULL) |
| 1313 | { |
| 1314 | cc = *name_end; |
| 1315 | *name_end = NUL; |
| 1316 | |
| 1317 | // Normal name or expanded name. |
| 1318 | if (do_unlet(lp->ll_name, forceit) == FAIL) |
| 1319 | ret = FAIL; |
| 1320 | *name_end = cc; |
| 1321 | } |
| 1322 | else if ((lp->ll_list != NULL |
| 1323 | && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE)) |
| 1324 | || (lp->ll_dict != NULL |
| 1325 | && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE))) |
| 1326 | return FAIL; |
| 1327 | else if (lp->ll_range) |
| 1328 | { |
| 1329 | listitem_T *li; |
| 1330 | listitem_T *ll_li = lp->ll_li; |
| 1331 | int ll_n1 = lp->ll_n1; |
| 1332 | |
| 1333 | while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1)) |
| 1334 | { |
| 1335 | li = ll_li->li_next; |
| 1336 | if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE)) |
| 1337 | return FAIL; |
| 1338 | ll_li = li; |
| 1339 | ++ll_n1; |
| 1340 | } |
| 1341 | |
| 1342 | // Delete a range of List items. |
| 1343 | while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1)) |
| 1344 | { |
| 1345 | li = lp->ll_li->li_next; |
| 1346 | listitem_remove(lp->ll_list, lp->ll_li); |
| 1347 | lp->ll_li = li; |
| 1348 | ++lp->ll_n1; |
| 1349 | } |
| 1350 | } |
| 1351 | else |
| 1352 | { |
| 1353 | if (lp->ll_list != NULL) |
| 1354 | // unlet a List item. |
| 1355 | listitem_remove(lp->ll_list, lp->ll_li); |
| 1356 | else |
| 1357 | // unlet a Dictionary item. |
| 1358 | dictitem_remove(lp->ll_dict, lp->ll_di); |
| 1359 | } |
| 1360 | |
| 1361 | return ret; |
| 1362 | } |
| 1363 | |
| 1364 | /* |
| 1365 | * "unlet" a variable. Return OK if it existed, FAIL if not. |
| 1366 | * When "forceit" is TRUE don't complain if the variable doesn't exist. |
| 1367 | */ |
| 1368 | int |
| 1369 | do_unlet(char_u *name, int forceit) |
| 1370 | { |
| 1371 | hashtab_T *ht; |
| 1372 | hashitem_T *hi; |
| 1373 | char_u *varname; |
| 1374 | dict_T *d; |
| 1375 | dictitem_T *di; |
| 1376 | |
| 1377 | ht = find_var_ht(name, &varname); |
| 1378 | if (ht != NULL && *varname != NUL) |
| 1379 | { |
| 1380 | d = get_current_funccal_dict(ht); |
| 1381 | if (d == NULL) |
| 1382 | { |
| 1383 | if (ht == &globvarht) |
| 1384 | d = &globvardict; |
Bram Moolenaar | e5cdf15 | 2019-08-29 22:09:46 +0200 | [diff] [blame] | 1385 | else if (ht == &compat_hashtab) |
Bram Moolenaar | 0522ba0 | 2019-08-27 22:48:30 +0200 | [diff] [blame] | 1386 | d = &vimvardict; |
| 1387 | else |
| 1388 | { |
| 1389 | di = find_var_in_ht(ht, *name, (char_u *)"", FALSE); |
| 1390 | d = di == NULL ? NULL : di->di_tv.vval.v_dict; |
| 1391 | } |
| 1392 | if (d == NULL) |
| 1393 | { |
| 1394 | internal_error("do_unlet()"); |
| 1395 | return FAIL; |
| 1396 | } |
| 1397 | } |
| 1398 | hi = hash_find(ht, varname); |
| 1399 | if (HASHITEM_EMPTY(hi)) |
| 1400 | hi = find_hi_in_scoped_ht(name, &ht); |
| 1401 | if (hi != NULL && !HASHITEM_EMPTY(hi)) |
| 1402 | { |
| 1403 | di = HI2DI(hi); |
| 1404 | if (var_check_fixed(di->di_flags, name, FALSE) |
| 1405 | || var_check_ro(di->di_flags, name, FALSE) |
| 1406 | || var_check_lock(d->dv_lock, name, FALSE)) |
| 1407 | return FAIL; |
| 1408 | |
| 1409 | delete_var(ht, hi); |
| 1410 | return OK; |
| 1411 | } |
| 1412 | } |
| 1413 | if (forceit) |
| 1414 | return OK; |
| 1415 | semsg(_("E108: No such variable: \"%s\""), name); |
| 1416 | return FAIL; |
| 1417 | } |
| 1418 | |
| 1419 | /* |
| 1420 | * Lock or unlock variable indicated by "lp". |
| 1421 | * "deep" is the levels to go (-1 for unlimited); |
| 1422 | * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar". |
| 1423 | */ |
| 1424 | static int |
| 1425 | do_lock_var( |
| 1426 | lval_T *lp, |
| 1427 | char_u *name_end, |
| 1428 | int deep, |
| 1429 | int lock) |
| 1430 | { |
| 1431 | int ret = OK; |
| 1432 | int cc; |
| 1433 | dictitem_T *di; |
| 1434 | |
| 1435 | if (deep == 0) // nothing to do |
| 1436 | return OK; |
| 1437 | |
| 1438 | if (lp->ll_tv == NULL) |
| 1439 | { |
| 1440 | cc = *name_end; |
| 1441 | *name_end = NUL; |
| 1442 | |
| 1443 | // Normal name or expanded name. |
| 1444 | di = find_var(lp->ll_name, NULL, TRUE); |
| 1445 | if (di == NULL) |
| 1446 | ret = FAIL; |
| 1447 | else if ((di->di_flags & DI_FLAGS_FIX) |
| 1448 | && di->di_tv.v_type != VAR_DICT |
| 1449 | && di->di_tv.v_type != VAR_LIST) |
| 1450 | // For historic reasons this error is not given for a list or dict. |
| 1451 | // E.g., the b: dict could be locked/unlocked. |
| 1452 | semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name); |
| 1453 | else |
| 1454 | { |
| 1455 | if (lock) |
| 1456 | di->di_flags |= DI_FLAGS_LOCK; |
| 1457 | else |
| 1458 | di->di_flags &= ~DI_FLAGS_LOCK; |
| 1459 | item_lock(&di->di_tv, deep, lock); |
| 1460 | } |
| 1461 | *name_end = cc; |
| 1462 | } |
| 1463 | else if (lp->ll_range) |
| 1464 | { |
| 1465 | listitem_T *li = lp->ll_li; |
| 1466 | |
| 1467 | // (un)lock a range of List items. |
| 1468 | while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1)) |
| 1469 | { |
| 1470 | item_lock(&li->li_tv, deep, lock); |
| 1471 | li = li->li_next; |
| 1472 | ++lp->ll_n1; |
| 1473 | } |
| 1474 | } |
| 1475 | else if (lp->ll_list != NULL) |
| 1476 | // (un)lock a List item. |
| 1477 | item_lock(&lp->ll_li->li_tv, deep, lock); |
| 1478 | else |
| 1479 | // (un)lock a Dictionary item. |
| 1480 | item_lock(&lp->ll_di->di_tv, deep, lock); |
| 1481 | |
| 1482 | return ret; |
| 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | * Lock or unlock an item. "deep" is nr of levels to go. |
| 1487 | */ |
| 1488 | static void |
| 1489 | item_lock(typval_T *tv, int deep, int lock) |
| 1490 | { |
| 1491 | static int recurse = 0; |
| 1492 | list_T *l; |
| 1493 | listitem_T *li; |
| 1494 | dict_T *d; |
| 1495 | blob_T *b; |
| 1496 | hashitem_T *hi; |
| 1497 | int todo; |
| 1498 | |
| 1499 | if (recurse >= DICT_MAXNEST) |
| 1500 | { |
| 1501 | emsg(_("E743: variable nested too deep for (un)lock")); |
| 1502 | return; |
| 1503 | } |
| 1504 | if (deep == 0) |
| 1505 | return; |
| 1506 | ++recurse; |
| 1507 | |
| 1508 | // lock/unlock the item itself |
| 1509 | if (lock) |
| 1510 | tv->v_lock |= VAR_LOCKED; |
| 1511 | else |
| 1512 | tv->v_lock &= ~VAR_LOCKED; |
| 1513 | |
| 1514 | switch (tv->v_type) |
| 1515 | { |
| 1516 | case VAR_UNKNOWN: |
| 1517 | case VAR_NUMBER: |
| 1518 | case VAR_STRING: |
| 1519 | case VAR_FUNC: |
| 1520 | case VAR_PARTIAL: |
| 1521 | case VAR_FLOAT: |
| 1522 | case VAR_SPECIAL: |
| 1523 | case VAR_JOB: |
| 1524 | case VAR_CHANNEL: |
| 1525 | break; |
| 1526 | |
| 1527 | case VAR_BLOB: |
| 1528 | if ((b = tv->vval.v_blob) != NULL) |
| 1529 | { |
| 1530 | if (lock) |
| 1531 | b->bv_lock |= VAR_LOCKED; |
| 1532 | else |
| 1533 | b->bv_lock &= ~VAR_LOCKED; |
| 1534 | } |
| 1535 | break; |
| 1536 | case VAR_LIST: |
| 1537 | if ((l = tv->vval.v_list) != NULL) |
| 1538 | { |
| 1539 | if (lock) |
| 1540 | l->lv_lock |= VAR_LOCKED; |
| 1541 | else |
| 1542 | l->lv_lock &= ~VAR_LOCKED; |
| 1543 | if (deep < 0 || deep > 1) |
| 1544 | // recursive: lock/unlock the items the List contains |
| 1545 | for (li = l->lv_first; li != NULL; li = li->li_next) |
| 1546 | item_lock(&li->li_tv, deep - 1, lock); |
| 1547 | } |
| 1548 | break; |
| 1549 | case VAR_DICT: |
| 1550 | if ((d = tv->vval.v_dict) != NULL) |
| 1551 | { |
| 1552 | if (lock) |
| 1553 | d->dv_lock |= VAR_LOCKED; |
| 1554 | else |
| 1555 | d->dv_lock &= ~VAR_LOCKED; |
| 1556 | if (deep < 0 || deep > 1) |
| 1557 | { |
| 1558 | // recursive: lock/unlock the items the List contains |
| 1559 | todo = (int)d->dv_hashtab.ht_used; |
| 1560 | for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) |
| 1561 | { |
| 1562 | if (!HASHITEM_EMPTY(hi)) |
| 1563 | { |
| 1564 | --todo; |
| 1565 | item_lock(&HI2DI(hi)->di_tv, deep - 1, lock); |
| 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | } |
| 1570 | } |
| 1571 | --recurse; |
| 1572 | } |
| 1573 | |
| 1574 | /* |
Bram Moolenaar | e5cdf15 | 2019-08-29 22:09:46 +0200 | [diff] [blame] | 1575 | * Local string buffer for the next two functions to store a variable name |
| 1576 | * with its prefix. Allocated in cat_prefix_varname(), freed later in |
| 1577 | * get_user_var_name(). |
| 1578 | */ |
| 1579 | |
| 1580 | static char_u *varnamebuf = NULL; |
| 1581 | static int varnamebuflen = 0; |
| 1582 | |
| 1583 | /* |
| 1584 | * Function to concatenate a prefix and a variable name. |
| 1585 | */ |
| 1586 | static char_u * |
| 1587 | cat_prefix_varname(int prefix, char_u *name) |
| 1588 | { |
| 1589 | int len; |
| 1590 | |
| 1591 | len = (int)STRLEN(name) + 3; |
| 1592 | if (len > varnamebuflen) |
| 1593 | { |
| 1594 | vim_free(varnamebuf); |
| 1595 | len += 10; /* some additional space */ |
| 1596 | varnamebuf = alloc(len); |
| 1597 | if (varnamebuf == NULL) |
| 1598 | { |
| 1599 | varnamebuflen = 0; |
| 1600 | return NULL; |
| 1601 | } |
| 1602 | varnamebuflen = len; |
| 1603 | } |
| 1604 | *varnamebuf = prefix; |
| 1605 | varnamebuf[1] = ':'; |
| 1606 | STRCPY(varnamebuf + 2, name); |
| 1607 | return varnamebuf; |
| 1608 | } |
| 1609 | |
| 1610 | /* |
| 1611 | * Function given to ExpandGeneric() to obtain the list of user defined |
| 1612 | * (global/buffer/window/built-in) variable names. |
| 1613 | */ |
| 1614 | char_u * |
| 1615 | get_user_var_name(expand_T *xp, int idx) |
| 1616 | { |
| 1617 | static long_u gdone; |
| 1618 | static long_u bdone; |
| 1619 | static long_u wdone; |
| 1620 | static long_u tdone; |
| 1621 | static int vidx; |
| 1622 | static hashitem_T *hi; |
| 1623 | hashtab_T *ht; |
| 1624 | |
| 1625 | if (idx == 0) |
| 1626 | { |
| 1627 | gdone = bdone = wdone = vidx = 0; |
| 1628 | tdone = 0; |
| 1629 | } |
| 1630 | |
| 1631 | // Global variables |
| 1632 | if (gdone < globvarht.ht_used) |
| 1633 | { |
| 1634 | if (gdone++ == 0) |
| 1635 | hi = globvarht.ht_array; |
| 1636 | else |
| 1637 | ++hi; |
| 1638 | while (HASHITEM_EMPTY(hi)) |
| 1639 | ++hi; |
| 1640 | if (STRNCMP("g:", xp->xp_pattern, 2) == 0) |
| 1641 | return cat_prefix_varname('g', hi->hi_key); |
| 1642 | return hi->hi_key; |
| 1643 | } |
| 1644 | |
| 1645 | // b: variables |
| 1646 | ht = &curbuf->b_vars->dv_hashtab; |
| 1647 | if (bdone < ht->ht_used) |
| 1648 | { |
| 1649 | if (bdone++ == 0) |
| 1650 | hi = ht->ht_array; |
| 1651 | else |
| 1652 | ++hi; |
| 1653 | while (HASHITEM_EMPTY(hi)) |
| 1654 | ++hi; |
| 1655 | return cat_prefix_varname('b', hi->hi_key); |
| 1656 | } |
| 1657 | |
| 1658 | // w: variables |
| 1659 | ht = &curwin->w_vars->dv_hashtab; |
| 1660 | if (wdone < ht->ht_used) |
| 1661 | { |
| 1662 | if (wdone++ == 0) |
| 1663 | hi = ht->ht_array; |
| 1664 | else |
| 1665 | ++hi; |
| 1666 | while (HASHITEM_EMPTY(hi)) |
| 1667 | ++hi; |
| 1668 | return cat_prefix_varname('w', hi->hi_key); |
| 1669 | } |
| 1670 | |
| 1671 | // t: variables |
| 1672 | ht = &curtab->tp_vars->dv_hashtab; |
| 1673 | if (tdone < ht->ht_used) |
| 1674 | { |
| 1675 | if (tdone++ == 0) |
| 1676 | hi = ht->ht_array; |
| 1677 | else |
| 1678 | ++hi; |
| 1679 | while (HASHITEM_EMPTY(hi)) |
| 1680 | ++hi; |
| 1681 | return cat_prefix_varname('t', hi->hi_key); |
| 1682 | } |
| 1683 | |
| 1684 | // v: variables |
| 1685 | if (vidx < VV_LEN) |
| 1686 | return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name); |
| 1687 | |
| 1688 | VIM_CLEAR(varnamebuf); |
| 1689 | varnamebuflen = 0; |
| 1690 | return NULL; |
| 1691 | } |
| 1692 | |
| 1693 | /* |
Bram Moolenaar | 34ed68d | 2019-08-29 22:48:24 +0200 | [diff] [blame] | 1694 | * Set type of v: variable to "type". |
| 1695 | */ |
| 1696 | void |
| 1697 | set_vim_var_type(int idx, vartype_T type) |
| 1698 | { |
| 1699 | vimvars[idx].vv_type = type; |
| 1700 | } |
| 1701 | |
| 1702 | /* |
Bram Moolenaar | e5cdf15 | 2019-08-29 22:09:46 +0200 | [diff] [blame] | 1703 | * Set number v: variable to "val". |
| 1704 | */ |
| 1705 | void |
| 1706 | set_vim_var_nr(int idx, varnumber_T val) |
| 1707 | { |
Bram Moolenaar | e5cdf15 | 2019-08-29 22:09:46 +0200 | [diff] [blame] | 1708 | vimvars[idx].vv_nr = val; |
| 1709 | } |
| 1710 | |
| 1711 | /* |
| 1712 | * Get typval_T v: variable value. |
| 1713 | */ |
| 1714 | typval_T * |
| 1715 | get_vim_var_tv(int idx) |
| 1716 | { |
| 1717 | return &vimvars[idx].vv_tv; |
| 1718 | } |
| 1719 | |
| 1720 | /* |
| 1721 | * Get number v: variable value. |
| 1722 | */ |
| 1723 | varnumber_T |
| 1724 | get_vim_var_nr(int idx) |
| 1725 | { |
| 1726 | return vimvars[idx].vv_nr; |
| 1727 | } |
| 1728 | |
| 1729 | /* |
| 1730 | * Get string v: variable value. Uses a static buffer, can only be used once. |
| 1731 | * If the String variable has never been set, return an empty string. |
| 1732 | * Never returns NULL; |
| 1733 | */ |
| 1734 | char_u * |
| 1735 | get_vim_var_str(int idx) |
| 1736 | { |
| 1737 | return tv_get_string(&vimvars[idx].vv_tv); |
| 1738 | } |
| 1739 | |
| 1740 | /* |
| 1741 | * Get List v: variable value. Caller must take care of reference count when |
| 1742 | * needed. |
| 1743 | */ |
| 1744 | list_T * |
| 1745 | get_vim_var_list(int idx) |
| 1746 | { |
| 1747 | return vimvars[idx].vv_list; |
| 1748 | } |
| 1749 | |
| 1750 | /* |
| 1751 | * Get Dict v: variable value. Caller must take care of reference count when |
| 1752 | * needed. |
| 1753 | */ |
| 1754 | dict_T * |
| 1755 | get_vim_var_dict(int idx) |
| 1756 | { |
| 1757 | return vimvars[idx].vv_dict; |
| 1758 | } |
| 1759 | |
| 1760 | /* |
| 1761 | * Set v:char to character "c". |
| 1762 | */ |
| 1763 | void |
| 1764 | set_vim_var_char(int c) |
| 1765 | { |
| 1766 | char_u buf[MB_MAXBYTES + 1]; |
| 1767 | |
| 1768 | if (has_mbyte) |
| 1769 | buf[(*mb_char2bytes)(c, buf)] = NUL; |
| 1770 | else |
| 1771 | { |
| 1772 | buf[0] = c; |
| 1773 | buf[1] = NUL; |
| 1774 | } |
| 1775 | set_vim_var_string(VV_CHAR, buf, -1); |
| 1776 | } |
| 1777 | |
| 1778 | /* |
| 1779 | * Set v:count to "count" and v:count1 to "count1". |
| 1780 | * When "set_prevcount" is TRUE first set v:prevcount from v:count. |
| 1781 | */ |
| 1782 | void |
| 1783 | set_vcount( |
| 1784 | long count, |
| 1785 | long count1, |
| 1786 | int set_prevcount) |
| 1787 | { |
| 1788 | if (set_prevcount) |
| 1789 | vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr; |
| 1790 | vimvars[VV_COUNT].vv_nr = count; |
| 1791 | vimvars[VV_COUNT1].vv_nr = count1; |
| 1792 | } |
| 1793 | |
| 1794 | /* |
| 1795 | * Save variables that might be changed as a side effect. Used when executing |
| 1796 | * a timer callback. |
| 1797 | */ |
| 1798 | void |
| 1799 | save_vimvars(vimvars_save_T *vvsave) |
| 1800 | { |
| 1801 | vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr; |
| 1802 | vvsave->vv_count = vimvars[VV_COUNT].vv_nr; |
| 1803 | vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr; |
| 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * Restore variables saved by save_vimvars(). |
| 1808 | */ |
| 1809 | void |
| 1810 | restore_vimvars(vimvars_save_T *vvsave) |
| 1811 | { |
| 1812 | vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount; |
| 1813 | vimvars[VV_COUNT].vv_nr = vvsave->vv_count; |
| 1814 | vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1; |
| 1815 | } |
| 1816 | |
| 1817 | /* |
| 1818 | * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the |
| 1819 | * value. |
| 1820 | */ |
| 1821 | void |
| 1822 | set_vim_var_string( |
| 1823 | int idx, |
| 1824 | char_u *val, |
| 1825 | int len) // length of "val" to use or -1 (whole string) |
| 1826 | { |
| 1827 | clear_tv(&vimvars[idx].vv_di.di_tv); |
| 1828 | vimvars[idx].vv_type = VAR_STRING; |
| 1829 | if (val == NULL) |
| 1830 | vimvars[idx].vv_str = NULL; |
| 1831 | else if (len == -1) |
| 1832 | vimvars[idx].vv_str = vim_strsave(val); |
| 1833 | else |
| 1834 | vimvars[idx].vv_str = vim_strnsave(val, len); |
| 1835 | } |
| 1836 | |
| 1837 | /* |
| 1838 | * Set List v: variable to "val". |
| 1839 | */ |
| 1840 | void |
| 1841 | set_vim_var_list(int idx, list_T *val) |
| 1842 | { |
| 1843 | clear_tv(&vimvars[idx].vv_di.di_tv); |
| 1844 | vimvars[idx].vv_type = VAR_LIST; |
| 1845 | vimvars[idx].vv_list = val; |
| 1846 | if (val != NULL) |
| 1847 | ++val->lv_refcount; |
| 1848 | } |
| 1849 | |
| 1850 | /* |
| 1851 | * Set Dictionary v: variable to "val". |
| 1852 | */ |
| 1853 | void |
| 1854 | set_vim_var_dict(int idx, dict_T *val) |
| 1855 | { |
| 1856 | clear_tv(&vimvars[idx].vv_di.di_tv); |
| 1857 | vimvars[idx].vv_type = VAR_DICT; |
| 1858 | vimvars[idx].vv_dict = val; |
| 1859 | if (val != NULL) |
| 1860 | { |
| 1861 | ++val->dv_refcount; |
| 1862 | dict_set_items_ro(val); |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | /* |
| 1867 | * Set v:register if needed. |
| 1868 | */ |
| 1869 | void |
| 1870 | set_reg_var(int c) |
| 1871 | { |
| 1872 | char_u regname; |
| 1873 | |
| 1874 | if (c == 0 || c == ' ') |
| 1875 | regname = '"'; |
| 1876 | else |
| 1877 | regname = c; |
| 1878 | // Avoid free/alloc when the value is already right. |
| 1879 | if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c) |
| 1880 | set_vim_var_string(VV_REG, ®name, 1); |
| 1881 | } |
| 1882 | |
| 1883 | /* |
| 1884 | * Get or set v:exception. If "oldval" == NULL, return the current value. |
| 1885 | * Otherwise, restore the value to "oldval" and return NULL. |
| 1886 | * Must always be called in pairs to save and restore v:exception! Does not |
| 1887 | * take care of memory allocations. |
| 1888 | */ |
| 1889 | char_u * |
| 1890 | v_exception(char_u *oldval) |
| 1891 | { |
| 1892 | if (oldval == NULL) |
| 1893 | return vimvars[VV_EXCEPTION].vv_str; |
| 1894 | |
| 1895 | vimvars[VV_EXCEPTION].vv_str = oldval; |
| 1896 | return NULL; |
| 1897 | } |
| 1898 | |
| 1899 | /* |
| 1900 | * Get or set v:throwpoint. If "oldval" == NULL, return the current value. |
| 1901 | * Otherwise, restore the value to "oldval" and return NULL. |
| 1902 | * Must always be called in pairs to save and restore v:throwpoint! Does not |
| 1903 | * take care of memory allocations. |
| 1904 | */ |
| 1905 | char_u * |
| 1906 | v_throwpoint(char_u *oldval) |
| 1907 | { |
| 1908 | if (oldval == NULL) |
| 1909 | return vimvars[VV_THROWPOINT].vv_str; |
| 1910 | |
| 1911 | vimvars[VV_THROWPOINT].vv_str = oldval; |
| 1912 | return NULL; |
| 1913 | } |
| 1914 | |
| 1915 | /* |
| 1916 | * Set v:cmdarg. |
| 1917 | * If "eap" != NULL, use "eap" to generate the value and return the old value. |
| 1918 | * If "oldarg" != NULL, restore the value to "oldarg" and return NULL. |
| 1919 | * Must always be called in pairs! |
| 1920 | */ |
| 1921 | char_u * |
| 1922 | set_cmdarg(exarg_T *eap, char_u *oldarg) |
| 1923 | { |
| 1924 | char_u *oldval; |
| 1925 | char_u *newval; |
| 1926 | unsigned len; |
| 1927 | |
| 1928 | oldval = vimvars[VV_CMDARG].vv_str; |
| 1929 | if (eap == NULL) |
| 1930 | { |
| 1931 | vim_free(oldval); |
| 1932 | vimvars[VV_CMDARG].vv_str = oldarg; |
| 1933 | return NULL; |
| 1934 | } |
| 1935 | |
| 1936 | if (eap->force_bin == FORCE_BIN) |
| 1937 | len = 6; |
| 1938 | else if (eap->force_bin == FORCE_NOBIN) |
| 1939 | len = 8; |
| 1940 | else |
| 1941 | len = 0; |
| 1942 | |
| 1943 | if (eap->read_edit) |
| 1944 | len += 7; |
| 1945 | |
| 1946 | if (eap->force_ff != 0) |
| 1947 | len += 10; // " ++ff=unix" |
| 1948 | if (eap->force_enc != 0) |
| 1949 | len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7; |
| 1950 | if (eap->bad_char != 0) |
| 1951 | len += 7 + 4; // " ++bad=" + "keep" or "drop" |
| 1952 | |
| 1953 | newval = alloc(len + 1); |
| 1954 | if (newval == NULL) |
| 1955 | return NULL; |
| 1956 | |
| 1957 | if (eap->force_bin == FORCE_BIN) |
| 1958 | sprintf((char *)newval, " ++bin"); |
| 1959 | else if (eap->force_bin == FORCE_NOBIN) |
| 1960 | sprintf((char *)newval, " ++nobin"); |
| 1961 | else |
| 1962 | *newval = NUL; |
| 1963 | |
| 1964 | if (eap->read_edit) |
| 1965 | STRCAT(newval, " ++edit"); |
| 1966 | |
| 1967 | if (eap->force_ff != 0) |
| 1968 | sprintf((char *)newval + STRLEN(newval), " ++ff=%s", |
| 1969 | eap->force_ff == 'u' ? "unix" |
| 1970 | : eap->force_ff == 'd' ? "dos" |
| 1971 | : "mac"); |
| 1972 | if (eap->force_enc != 0) |
| 1973 | sprintf((char *)newval + STRLEN(newval), " ++enc=%s", |
| 1974 | eap->cmd + eap->force_enc); |
| 1975 | if (eap->bad_char == BAD_KEEP) |
| 1976 | STRCPY(newval + STRLEN(newval), " ++bad=keep"); |
| 1977 | else if (eap->bad_char == BAD_DROP) |
| 1978 | STRCPY(newval + STRLEN(newval), " ++bad=drop"); |
| 1979 | else if (eap->bad_char != 0) |
| 1980 | sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char); |
| 1981 | vimvars[VV_CMDARG].vv_str = newval; |
| 1982 | return oldval; |
| 1983 | } |
| 1984 | |
| 1985 | /* |
Bram Moolenaar | 0522ba0 | 2019-08-27 22:48:30 +0200 | [diff] [blame] | 1986 | * Get the value of internal variable "name". |
| 1987 | * Return OK or FAIL. If OK is returned "rettv" must be cleared. |
| 1988 | */ |
| 1989 | int |
| 1990 | get_var_tv( |
| 1991 | char_u *name, |
| 1992 | int len, // length of "name" |
| 1993 | typval_T *rettv, // NULL when only checking existence |
| 1994 | dictitem_T **dip, // non-NULL when typval's dict item is needed |
| 1995 | int verbose, // may give error message |
| 1996 | int no_autoload) // do not use script autoloading |
| 1997 | { |
| 1998 | int ret = OK; |
| 1999 | typval_T *tv = NULL; |
| 2000 | dictitem_T *v; |
| 2001 | int cc; |
| 2002 | |
| 2003 | // truncate the name, so that we can use strcmp() |
| 2004 | cc = name[len]; |
| 2005 | name[len] = NUL; |
| 2006 | |
| 2007 | // Check for user-defined variables. |
| 2008 | v = find_var(name, NULL, no_autoload); |
| 2009 | if (v != NULL) |
| 2010 | { |
| 2011 | tv = &v->di_tv; |
| 2012 | if (dip != NULL) |
| 2013 | *dip = v; |
| 2014 | } |
| 2015 | |
| 2016 | if (tv == NULL) |
| 2017 | { |
| 2018 | if (rettv != NULL && verbose) |
| 2019 | semsg(_(e_undefvar), name); |
| 2020 | ret = FAIL; |
| 2021 | } |
| 2022 | else if (rettv != NULL) |
| 2023 | copy_tv(tv, rettv); |
| 2024 | |
| 2025 | name[len] = cc; |
| 2026 | |
| 2027 | return ret; |
| 2028 | } |
| 2029 | |
| 2030 | /* |
Bram Moolenaar | e5cdf15 | 2019-08-29 22:09:46 +0200 | [diff] [blame] | 2031 | * Check if variable "name[len]" is a local variable or an argument. |
| 2032 | * If so, "*eval_lavars_used" is set to TRUE. |
| 2033 | */ |
| 2034 | void |
| 2035 | check_vars(char_u *name, int len) |
| 2036 | { |
| 2037 | int cc; |
| 2038 | char_u *varname; |
| 2039 | hashtab_T *ht; |
| 2040 | |
| 2041 | if (eval_lavars_used == NULL) |
| 2042 | return; |
| 2043 | |
| 2044 | // truncate the name, so that we can use strcmp() |
| 2045 | cc = name[len]; |
| 2046 | name[len] = NUL; |
| 2047 | |
| 2048 | ht = find_var_ht(name, &varname); |
| 2049 | if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht()) |
| 2050 | { |
| 2051 | if (find_var(name, NULL, TRUE) != NULL) |
| 2052 | *eval_lavars_used = TRUE; |
| 2053 | } |
| 2054 | |
| 2055 | name[len] = cc; |
| 2056 | } |
| 2057 | |
| 2058 | /* |
| 2059 | * Find variable "name" in the list of variables. |
| 2060 | * Return a pointer to it if found, NULL if not found. |
| 2061 | * Careful: "a:0" variables don't have a name. |
| 2062 | * When "htp" is not NULL we are writing to the variable, set "htp" to the |
| 2063 | * hashtab_T used. |
| 2064 | */ |
| 2065 | dictitem_T * |
| 2066 | find_var(char_u *name, hashtab_T **htp, int no_autoload) |
| 2067 | { |
| 2068 | char_u *varname; |
| 2069 | hashtab_T *ht; |
| 2070 | dictitem_T *ret = NULL; |
| 2071 | |
| 2072 | ht = find_var_ht(name, &varname); |
| 2073 | if (htp != NULL) |
| 2074 | *htp = ht; |
| 2075 | if (ht == NULL) |
| 2076 | return NULL; |
| 2077 | ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL); |
| 2078 | if (ret != NULL) |
| 2079 | return ret; |
| 2080 | |
| 2081 | /* Search in parent scope for lambda */ |
| 2082 | return find_var_in_scoped_ht(name, no_autoload || htp != NULL); |
| 2083 | } |
| 2084 | |
| 2085 | /* |
| 2086 | * Find variable "varname" in hashtab "ht" with name "htname". |
| 2087 | * Returns NULL if not found. |
| 2088 | */ |
| 2089 | dictitem_T * |
| 2090 | find_var_in_ht( |
| 2091 | hashtab_T *ht, |
| 2092 | int htname, |
| 2093 | char_u *varname, |
| 2094 | int no_autoload) |
| 2095 | { |
| 2096 | hashitem_T *hi; |
| 2097 | |
| 2098 | if (*varname == NUL) |
| 2099 | { |
| 2100 | // Must be something like "s:", otherwise "ht" would be NULL. |
| 2101 | switch (htname) |
| 2102 | { |
| 2103 | case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var; |
| 2104 | case 'g': return &globvars_var; |
| 2105 | case 'v': return &vimvars_var; |
| 2106 | case 'b': return &curbuf->b_bufvar; |
| 2107 | case 'w': return &curwin->w_winvar; |
| 2108 | case 't': return &curtab->tp_winvar; |
| 2109 | case 'l': return get_funccal_local_var(); |
| 2110 | case 'a': return get_funccal_args_var(); |
| 2111 | } |
| 2112 | return NULL; |
| 2113 | } |
| 2114 | |
| 2115 | hi = hash_find(ht, varname); |
| 2116 | if (HASHITEM_EMPTY(hi)) |
| 2117 | { |
| 2118 | // For global variables we may try auto-loading the script. If it |
| 2119 | // worked find the variable again. Don't auto-load a script if it was |
| 2120 | // loaded already, otherwise it would be loaded every time when |
| 2121 | // checking if a function name is a Funcref variable. |
| 2122 | if (ht == &globvarht && !no_autoload) |
| 2123 | { |
| 2124 | // Note: script_autoload() may make "hi" invalid. It must either |
| 2125 | // be obtained again or not used. |
| 2126 | if (!script_autoload(varname, FALSE) || aborting()) |
| 2127 | return NULL; |
| 2128 | hi = hash_find(ht, varname); |
| 2129 | } |
| 2130 | if (HASHITEM_EMPTY(hi)) |
| 2131 | return NULL; |
| 2132 | } |
| 2133 | return HI2DI(hi); |
| 2134 | } |
| 2135 | |
| 2136 | /* |
| 2137 | * Find the hashtab used for a variable name. |
| 2138 | * Return NULL if the name is not valid. |
| 2139 | * Set "varname" to the start of name without ':'. |
| 2140 | */ |
| 2141 | hashtab_T * |
| 2142 | find_var_ht(char_u *name, char_u **varname) |
| 2143 | { |
| 2144 | hashitem_T *hi; |
| 2145 | hashtab_T *ht; |
| 2146 | |
| 2147 | if (name[0] == NUL) |
| 2148 | return NULL; |
| 2149 | if (name[1] != ':') |
| 2150 | { |
| 2151 | // The name must not start with a colon or #. |
| 2152 | if (name[0] == ':' || name[0] == AUTOLOAD_CHAR) |
| 2153 | return NULL; |
| 2154 | *varname = name; |
| 2155 | |
| 2156 | // "version" is "v:version" in all scopes if scriptversion < 3. |
| 2157 | // Same for a few other variables marked with VV_COMPAT. |
| 2158 | if (current_sctx.sc_version < 3) |
| 2159 | { |
| 2160 | hi = hash_find(&compat_hashtab, name); |
| 2161 | if (!HASHITEM_EMPTY(hi)) |
| 2162 | return &compat_hashtab; |
| 2163 | } |
| 2164 | |
| 2165 | ht = get_funccal_local_ht(); |
| 2166 | if (ht == NULL) |
| 2167 | return &globvarht; // global variable |
| 2168 | return ht; // local variable |
| 2169 | } |
| 2170 | *varname = name + 2; |
| 2171 | if (*name == 'g') // global variable |
| 2172 | return &globvarht; |
| 2173 | // There must be no ':' or '#' in the rest of the name, unless g: is used |
| 2174 | if (vim_strchr(name + 2, ':') != NULL |
| 2175 | || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL) |
| 2176 | return NULL; |
| 2177 | if (*name == 'b') // buffer variable |
| 2178 | return &curbuf->b_vars->dv_hashtab; |
| 2179 | if (*name == 'w') // window variable |
| 2180 | return &curwin->w_vars->dv_hashtab; |
| 2181 | if (*name == 't') // tab page variable |
| 2182 | return &curtab->tp_vars->dv_hashtab; |
| 2183 | if (*name == 'v') // v: variable |
| 2184 | return &vimvarht; |
| 2185 | if (*name == 'a') // a: function argument |
| 2186 | return get_funccal_args_ht(); |
| 2187 | if (*name == 'l') // l: local function variable |
| 2188 | return get_funccal_local_ht(); |
| 2189 | if (*name == 's' // script variable |
| 2190 | && current_sctx.sc_sid > 0 |
| 2191 | && current_sctx.sc_sid <= ga_scripts.ga_len) |
| 2192 | return &SCRIPT_VARS(current_sctx.sc_sid); |
| 2193 | return NULL; |
| 2194 | } |
| 2195 | |
| 2196 | /* |
Bram Moolenaar | 0522ba0 | 2019-08-27 22:48:30 +0200 | [diff] [blame] | 2197 | * Get the string value of a (global/local) variable. |
| 2198 | * Note: see tv_get_string() for how long the pointer remains valid. |
| 2199 | * Returns NULL when it doesn't exist. |
| 2200 | */ |
| 2201 | char_u * |
| 2202 | get_var_value(char_u *name) |
| 2203 | { |
| 2204 | dictitem_T *v; |
| 2205 | |
| 2206 | v = find_var(name, NULL, FALSE); |
| 2207 | if (v == NULL) |
| 2208 | return NULL; |
| 2209 | return tv_get_string(&v->di_tv); |
| 2210 | } |
| 2211 | |
| 2212 | /* |
Bram Moolenaar | e5cdf15 | 2019-08-29 22:09:46 +0200 | [diff] [blame] | 2213 | * Allocate a new hashtab for a sourced script. It will be used while |
| 2214 | * sourcing this script and when executing functions defined in the script. |
| 2215 | */ |
| 2216 | void |
| 2217 | new_script_vars(scid_T id) |
| 2218 | { |
| 2219 | int i; |
| 2220 | hashtab_T *ht; |
| 2221 | scriptvar_T *sv; |
| 2222 | |
| 2223 | if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK) |
| 2224 | { |
| 2225 | /* Re-allocating ga_data means that an ht_array pointing to |
| 2226 | * ht_smallarray becomes invalid. We can recognize this: ht_mask is |
| 2227 | * at its init value. Also reset "v_dict", it's always the same. */ |
| 2228 | for (i = 1; i <= ga_scripts.ga_len; ++i) |
| 2229 | { |
| 2230 | ht = &SCRIPT_VARS(i); |
| 2231 | if (ht->ht_mask == HT_INIT_SIZE - 1) |
| 2232 | ht->ht_array = ht->ht_smallarray; |
| 2233 | sv = SCRIPT_SV(i); |
| 2234 | sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict; |
| 2235 | } |
| 2236 | |
| 2237 | while (ga_scripts.ga_len < id) |
| 2238 | { |
| 2239 | sv = SCRIPT_SV(ga_scripts.ga_len + 1) = |
| 2240 | ALLOC_CLEAR_ONE(scriptvar_T); |
| 2241 | init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE); |
| 2242 | ++ga_scripts.ga_len; |
| 2243 | } |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | /* |
| 2248 | * Initialize dictionary "dict" as a scope and set variable "dict_var" to |
| 2249 | * point to it. |
| 2250 | */ |
| 2251 | void |
| 2252 | init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope) |
| 2253 | { |
| 2254 | hash_init(&dict->dv_hashtab); |
| 2255 | dict->dv_lock = 0; |
| 2256 | dict->dv_scope = scope; |
| 2257 | dict->dv_refcount = DO_NOT_FREE_CNT; |
| 2258 | dict->dv_copyID = 0; |
| 2259 | dict_var->di_tv.vval.v_dict = dict; |
| 2260 | dict_var->di_tv.v_type = VAR_DICT; |
| 2261 | dict_var->di_tv.v_lock = VAR_FIXED; |
| 2262 | dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX; |
| 2263 | dict_var->di_key[0] = NUL; |
| 2264 | } |
| 2265 | |
| 2266 | /* |
| 2267 | * Unreference a dictionary initialized by init_var_dict(). |
| 2268 | */ |
| 2269 | void |
| 2270 | unref_var_dict(dict_T *dict) |
| 2271 | { |
| 2272 | /* Now the dict needs to be freed if no one else is using it, go back to |
| 2273 | * normal reference counting. */ |
| 2274 | dict->dv_refcount -= DO_NOT_FREE_CNT - 1; |
| 2275 | dict_unref(dict); |
| 2276 | } |
| 2277 | |
| 2278 | /* |
Bram Moolenaar | 0522ba0 | 2019-08-27 22:48:30 +0200 | [diff] [blame] | 2279 | * Clean up a list of internal variables. |
| 2280 | * Frees all allocated variables and the value they contain. |
| 2281 | * Clears hashtab "ht", does not free it. |
| 2282 | */ |
| 2283 | void |
| 2284 | vars_clear(hashtab_T *ht) |
| 2285 | { |
| 2286 | vars_clear_ext(ht, TRUE); |
| 2287 | } |
| 2288 | |
| 2289 | /* |
| 2290 | * Like vars_clear(), but only free the value if "free_val" is TRUE. |
| 2291 | */ |
| 2292 | void |
| 2293 | vars_clear_ext(hashtab_T *ht, int free_val) |
| 2294 | { |
| 2295 | int todo; |
| 2296 | hashitem_T *hi; |
| 2297 | dictitem_T *v; |
| 2298 | |
| 2299 | hash_lock(ht); |
| 2300 | todo = (int)ht->ht_used; |
| 2301 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 2302 | { |
| 2303 | if (!HASHITEM_EMPTY(hi)) |
| 2304 | { |
| 2305 | --todo; |
| 2306 | |
| 2307 | // Free the variable. Don't remove it from the hashtab, |
| 2308 | // ht_array might change then. hash_clear() takes care of it |
| 2309 | // later. |
| 2310 | v = HI2DI(hi); |
| 2311 | if (free_val) |
| 2312 | clear_tv(&v->di_tv); |
| 2313 | if (v->di_flags & DI_FLAGS_ALLOC) |
| 2314 | vim_free(v); |
| 2315 | } |
| 2316 | } |
| 2317 | hash_clear(ht); |
| 2318 | ht->ht_used = 0; |
| 2319 | } |
| 2320 | |
| 2321 | /* |
| 2322 | * Delete a variable from hashtab "ht" at item "hi". |
| 2323 | * Clear the variable value and free the dictitem. |
| 2324 | */ |
| 2325 | void |
| 2326 | delete_var(hashtab_T *ht, hashitem_T *hi) |
| 2327 | { |
| 2328 | dictitem_T *di = HI2DI(hi); |
| 2329 | |
| 2330 | hash_remove(ht, hi); |
| 2331 | clear_tv(&di->di_tv); |
| 2332 | vim_free(di); |
| 2333 | } |
| 2334 | |
| 2335 | /* |
| 2336 | * List the value of one internal variable. |
| 2337 | */ |
| 2338 | static void |
| 2339 | list_one_var(dictitem_T *v, char *prefix, int *first) |
| 2340 | { |
| 2341 | char_u *tofree; |
| 2342 | char_u *s; |
| 2343 | char_u numbuf[NUMBUFLEN]; |
| 2344 | |
| 2345 | s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID()); |
| 2346 | list_one_var_a(prefix, v->di_key, v->di_tv.v_type, |
| 2347 | s == NULL ? (char_u *)"" : s, first); |
| 2348 | vim_free(tofree); |
| 2349 | } |
| 2350 | |
| 2351 | static void |
| 2352 | list_one_var_a( |
| 2353 | char *prefix, |
| 2354 | char_u *name, |
| 2355 | int type, |
| 2356 | char_u *string, |
| 2357 | int *first) // when TRUE clear rest of screen and set to FALSE |
| 2358 | { |
| 2359 | // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" |
| 2360 | msg_start(); |
| 2361 | msg_puts(prefix); |
| 2362 | if (name != NULL) // "a:" vars don't have a name stored |
| 2363 | msg_puts((char *)name); |
| 2364 | msg_putchar(' '); |
| 2365 | msg_advance(22); |
| 2366 | if (type == VAR_NUMBER) |
| 2367 | msg_putchar('#'); |
| 2368 | else if (type == VAR_FUNC || type == VAR_PARTIAL) |
| 2369 | msg_putchar('*'); |
| 2370 | else if (type == VAR_LIST) |
| 2371 | { |
| 2372 | msg_putchar('['); |
| 2373 | if (*string == '[') |
| 2374 | ++string; |
| 2375 | } |
| 2376 | else if (type == VAR_DICT) |
| 2377 | { |
| 2378 | msg_putchar('{'); |
| 2379 | if (*string == '{') |
| 2380 | ++string; |
| 2381 | } |
| 2382 | else |
| 2383 | msg_putchar(' '); |
| 2384 | |
| 2385 | msg_outtrans(string); |
| 2386 | |
| 2387 | if (type == VAR_FUNC || type == VAR_PARTIAL) |
| 2388 | msg_puts("()"); |
| 2389 | if (*first) |
| 2390 | { |
| 2391 | msg_clr_eos(); |
| 2392 | *first = FALSE; |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | /* |
| 2397 | * Set variable "name" to value in "tv". |
| 2398 | * If the variable already exists, the value is updated. |
| 2399 | * Otherwise the variable is created. |
| 2400 | */ |
| 2401 | void |
| 2402 | set_var( |
| 2403 | char_u *name, |
| 2404 | typval_T *tv, |
| 2405 | int copy) // make copy of value in "tv" |
| 2406 | { |
| 2407 | set_var_const(name, tv, copy, FALSE); |
| 2408 | } |
| 2409 | |
| 2410 | /* |
| 2411 | * Set variable "name" to value in "tv". |
| 2412 | * If the variable already exists and "is_const" is FALSE the value is updated. |
| 2413 | * Otherwise the variable is created. |
| 2414 | */ |
| 2415 | void |
| 2416 | set_var_const( |
| 2417 | char_u *name, |
| 2418 | typval_T *tv, |
| 2419 | int copy, // make copy of value in "tv" |
| 2420 | int is_const) // disallow to modify existing variable |
| 2421 | { |
| 2422 | dictitem_T *v; |
| 2423 | char_u *varname; |
| 2424 | hashtab_T *ht; |
| 2425 | |
| 2426 | ht = find_var_ht(name, &varname); |
| 2427 | if (ht == NULL || *varname == NUL) |
| 2428 | { |
| 2429 | semsg(_(e_illvar), name); |
| 2430 | return; |
| 2431 | } |
| 2432 | v = find_var_in_ht(ht, 0, varname, TRUE); |
| 2433 | |
| 2434 | // Search in parent scope which is possible to reference from lambda |
| 2435 | if (v == NULL) |
| 2436 | v = find_var_in_scoped_ht(name, TRUE); |
| 2437 | |
| 2438 | if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL) |
| 2439 | && var_check_func_name(name, v == NULL)) |
| 2440 | return; |
| 2441 | |
| 2442 | if (v != NULL) |
| 2443 | { |
| 2444 | if (is_const) |
| 2445 | { |
| 2446 | emsg(_(e_cannot_mod)); |
| 2447 | return; |
| 2448 | } |
| 2449 | |
| 2450 | // existing variable, need to clear the value |
| 2451 | if (var_check_ro(v->di_flags, name, FALSE) |
| 2452 | || var_check_lock(v->di_tv.v_lock, name, FALSE)) |
| 2453 | return; |
| 2454 | |
| 2455 | // Handle setting internal v: variables separately where needed to |
| 2456 | // prevent changing the type. |
Bram Moolenaar | e5cdf15 | 2019-08-29 22:09:46 +0200 | [diff] [blame] | 2457 | if (ht == &vimvarht) |
Bram Moolenaar | 0522ba0 | 2019-08-27 22:48:30 +0200 | [diff] [blame] | 2458 | { |
| 2459 | if (v->di_tv.v_type == VAR_STRING) |
| 2460 | { |
| 2461 | VIM_CLEAR(v->di_tv.vval.v_string); |
| 2462 | if (copy || tv->v_type != VAR_STRING) |
| 2463 | { |
| 2464 | char_u *val = tv_get_string(tv); |
| 2465 | |
| 2466 | // Careful: when assigning to v:errmsg and tv_get_string() |
| 2467 | // causes an error message the variable will alrady be set. |
| 2468 | if (v->di_tv.vval.v_string == NULL) |
| 2469 | v->di_tv.vval.v_string = vim_strsave(val); |
| 2470 | } |
| 2471 | else |
| 2472 | { |
| 2473 | // Take over the string to avoid an extra alloc/free. |
| 2474 | v->di_tv.vval.v_string = tv->vval.v_string; |
| 2475 | tv->vval.v_string = NULL; |
| 2476 | } |
| 2477 | return; |
| 2478 | } |
| 2479 | else if (v->di_tv.v_type == VAR_NUMBER) |
| 2480 | { |
| 2481 | v->di_tv.vval.v_number = tv_get_number(tv); |
| 2482 | if (STRCMP(varname, "searchforward") == 0) |
| 2483 | set_search_direction(v->di_tv.vval.v_number ? '/' : '?'); |
| 2484 | #ifdef FEAT_SEARCH_EXTRA |
| 2485 | else if (STRCMP(varname, "hlsearch") == 0) |
| 2486 | { |
| 2487 | no_hlsearch = !v->di_tv.vval.v_number; |
| 2488 | redraw_all_later(SOME_VALID); |
| 2489 | } |
| 2490 | #endif |
| 2491 | return; |
| 2492 | } |
| 2493 | else if (v->di_tv.v_type != tv->v_type) |
| 2494 | { |
| 2495 | semsg(_("E963: setting %s to value with wrong type"), name); |
| 2496 | return; |
| 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | clear_tv(&v->di_tv); |
| 2501 | } |
| 2502 | else // add a new variable |
| 2503 | { |
| 2504 | // Can't add "v:" or "a:" variable. |
Bram Moolenaar | e5cdf15 | 2019-08-29 22:09:46 +0200 | [diff] [blame] | 2505 | if (ht == &vimvarht || ht == get_funccal_args_ht()) |
Bram Moolenaar | 0522ba0 | 2019-08-27 22:48:30 +0200 | [diff] [blame] | 2506 | { |
| 2507 | semsg(_(e_illvar), name); |
| 2508 | return; |
| 2509 | } |
| 2510 | |
| 2511 | // Make sure the variable name is valid. |
| 2512 | if (!valid_varname(varname)) |
| 2513 | return; |
| 2514 | |
| 2515 | v = alloc(sizeof(dictitem_T) + STRLEN(varname)); |
| 2516 | if (v == NULL) |
| 2517 | return; |
| 2518 | STRCPY(v->di_key, varname); |
| 2519 | if (hash_add(ht, DI2HIKEY(v)) == FAIL) |
| 2520 | { |
| 2521 | vim_free(v); |
| 2522 | return; |
| 2523 | } |
| 2524 | v->di_flags = DI_FLAGS_ALLOC; |
| 2525 | if (is_const) |
| 2526 | v->di_flags |= DI_FLAGS_LOCK; |
| 2527 | } |
| 2528 | |
| 2529 | if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT) |
| 2530 | copy_tv(tv, &v->di_tv); |
| 2531 | else |
| 2532 | { |
| 2533 | v->di_tv = *tv; |
| 2534 | v->di_tv.v_lock = 0; |
| 2535 | init_tv(tv); |
| 2536 | } |
| 2537 | |
| 2538 | if (is_const) |
| 2539 | v->di_tv.v_lock |= VAR_LOCKED; |
| 2540 | } |
| 2541 | |
| 2542 | /* |
| 2543 | * Return TRUE if di_flags "flags" indicates variable "name" is read-only. |
| 2544 | * Also give an error message. |
| 2545 | */ |
| 2546 | int |
| 2547 | var_check_ro(int flags, char_u *name, int use_gettext) |
| 2548 | { |
| 2549 | if (flags & DI_FLAGS_RO) |
| 2550 | { |
| 2551 | semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name); |
| 2552 | return TRUE; |
| 2553 | } |
| 2554 | if ((flags & DI_FLAGS_RO_SBX) && sandbox) |
| 2555 | { |
| 2556 | semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name); |
| 2557 | return TRUE; |
| 2558 | } |
| 2559 | return FALSE; |
| 2560 | } |
| 2561 | |
| 2562 | /* |
| 2563 | * Return TRUE if di_flags "flags" indicates variable "name" is fixed. |
| 2564 | * Also give an error message. |
| 2565 | */ |
| 2566 | int |
| 2567 | var_check_fixed(int flags, char_u *name, int use_gettext) |
| 2568 | { |
| 2569 | if (flags & DI_FLAGS_FIX) |
| 2570 | { |
| 2571 | semsg(_("E795: Cannot delete variable %s"), |
| 2572 | use_gettext ? (char_u *)_(name) : name); |
| 2573 | return TRUE; |
| 2574 | } |
| 2575 | return FALSE; |
| 2576 | } |
| 2577 | |
| 2578 | /* |
| 2579 | * Check if a funcref is assigned to a valid variable name. |
| 2580 | * Return TRUE and give an error if not. |
| 2581 | */ |
| 2582 | int |
| 2583 | var_check_func_name( |
| 2584 | char_u *name, // points to start of variable name |
| 2585 | int new_var) // TRUE when creating the variable |
| 2586 | { |
| 2587 | // Allow for w: b: s: and t:. |
| 2588 | if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':') |
| 2589 | && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':') |
| 2590 | ? name[2] : name[0])) |
| 2591 | { |
| 2592 | semsg(_("E704: Funcref variable name must start with a capital: %s"), |
| 2593 | name); |
| 2594 | return TRUE; |
| 2595 | } |
| 2596 | // Don't allow hiding a function. When "v" is not NULL we might be |
| 2597 | // assigning another function to the same var, the type is checked |
| 2598 | // below. |
| 2599 | if (new_var && function_exists(name, FALSE)) |
| 2600 | { |
| 2601 | semsg(_("E705: Variable name conflicts with existing function: %s"), |
| 2602 | name); |
| 2603 | return TRUE; |
| 2604 | } |
| 2605 | return FALSE; |
| 2606 | } |
| 2607 | |
| 2608 | /* |
| 2609 | * Return TRUE if "flags" indicates variable "name" is locked (immutable). |
| 2610 | * Also give an error message, using "name" or _("name") when use_gettext is |
| 2611 | * TRUE. |
| 2612 | */ |
| 2613 | int |
| 2614 | var_check_lock(int lock, char_u *name, int use_gettext) |
| 2615 | { |
| 2616 | if (lock & VAR_LOCKED) |
| 2617 | { |
| 2618 | semsg(_("E741: Value is locked: %s"), |
| 2619 | name == NULL ? (char_u *)_("Unknown") |
| 2620 | : use_gettext ? (char_u *)_(name) |
| 2621 | : name); |
| 2622 | return TRUE; |
| 2623 | } |
| 2624 | if (lock & VAR_FIXED) |
| 2625 | { |
| 2626 | semsg(_("E742: Cannot change value of %s"), |
| 2627 | name == NULL ? (char_u *)_("Unknown") |
| 2628 | : use_gettext ? (char_u *)_(name) |
| 2629 | : name); |
| 2630 | return TRUE; |
| 2631 | } |
| 2632 | return FALSE; |
| 2633 | } |
| 2634 | |
| 2635 | /* |
| 2636 | * Check if a variable name is valid. |
| 2637 | * Return FALSE and give an error if not. |
| 2638 | */ |
| 2639 | int |
| 2640 | valid_varname(char_u *varname) |
| 2641 | { |
| 2642 | char_u *p; |
| 2643 | |
| 2644 | for (p = varname; *p != NUL; ++p) |
| 2645 | if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)) |
| 2646 | && *p != AUTOLOAD_CHAR) |
| 2647 | { |
| 2648 | semsg(_(e_illvar), varname); |
| 2649 | return FALSE; |
| 2650 | } |
| 2651 | return TRUE; |
| 2652 | } |
| 2653 | |
| 2654 | /* |
| 2655 | * getwinvar() and gettabwinvar() |
| 2656 | */ |
| 2657 | static void |
| 2658 | getwinvar( |
| 2659 | typval_T *argvars, |
| 2660 | typval_T *rettv, |
| 2661 | int off) // 1 for gettabwinvar() |
| 2662 | { |
| 2663 | win_T *win; |
| 2664 | char_u *varname; |
| 2665 | dictitem_T *v; |
| 2666 | tabpage_T *tp = NULL; |
| 2667 | int done = FALSE; |
| 2668 | win_T *oldcurwin; |
| 2669 | tabpage_T *oldtabpage; |
| 2670 | int need_switch_win; |
| 2671 | |
| 2672 | if (off == 1) |
| 2673 | tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL)); |
| 2674 | else |
| 2675 | tp = curtab; |
| 2676 | win = find_win_by_nr(&argvars[off], tp); |
| 2677 | varname = tv_get_string_chk(&argvars[off + 1]); |
| 2678 | ++emsg_off; |
| 2679 | |
| 2680 | rettv->v_type = VAR_STRING; |
| 2681 | rettv->vval.v_string = NULL; |
| 2682 | |
| 2683 | if (win != NULL && varname != NULL) |
| 2684 | { |
| 2685 | // Set curwin to be our win, temporarily. Also set the tabpage, |
| 2686 | // otherwise the window is not valid. Only do this when needed, |
| 2687 | // autocommands get blocked. |
| 2688 | need_switch_win = !(tp == curtab && win == curwin); |
| 2689 | if (!need_switch_win |
| 2690 | || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK) |
| 2691 | { |
| 2692 | if (*varname == '&') |
| 2693 | { |
| 2694 | if (varname[1] == NUL) |
| 2695 | { |
| 2696 | // get all window-local options in a dict |
| 2697 | dict_T *opts = get_winbuf_options(FALSE); |
| 2698 | |
| 2699 | if (opts != NULL) |
| 2700 | { |
| 2701 | rettv_dict_set(rettv, opts); |
| 2702 | done = TRUE; |
| 2703 | } |
| 2704 | } |
| 2705 | else if (get_option_tv(&varname, rettv, 1) == OK) |
| 2706 | // window-local-option |
| 2707 | done = TRUE; |
| 2708 | } |
| 2709 | else |
| 2710 | { |
| 2711 | // Look up the variable. |
| 2712 | // Let getwinvar({nr}, "") return the "w:" dictionary. |
| 2713 | v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', |
| 2714 | varname, FALSE); |
| 2715 | if (v != NULL) |
| 2716 | { |
| 2717 | copy_tv(&v->di_tv, rettv); |
| 2718 | done = TRUE; |
| 2719 | } |
| 2720 | } |
| 2721 | } |
| 2722 | |
| 2723 | if (need_switch_win) |
| 2724 | // restore previous notion of curwin |
| 2725 | restore_win(oldcurwin, oldtabpage, TRUE); |
| 2726 | } |
| 2727 | |
| 2728 | if (!done && argvars[off + 2].v_type != VAR_UNKNOWN) |
| 2729 | // use the default return value |
| 2730 | copy_tv(&argvars[off + 2], rettv); |
| 2731 | |
| 2732 | --emsg_off; |
| 2733 | } |
| 2734 | |
| 2735 | /* |
| 2736 | * "setwinvar()" and "settabwinvar()" functions |
| 2737 | */ |
| 2738 | static void |
| 2739 | setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off) |
| 2740 | { |
| 2741 | win_T *win; |
| 2742 | win_T *save_curwin; |
| 2743 | tabpage_T *save_curtab; |
| 2744 | int need_switch_win; |
| 2745 | char_u *varname, *winvarname; |
| 2746 | typval_T *varp; |
| 2747 | char_u nbuf[NUMBUFLEN]; |
| 2748 | tabpage_T *tp = NULL; |
| 2749 | |
| 2750 | if (check_secure()) |
| 2751 | return; |
| 2752 | |
| 2753 | if (off == 1) |
| 2754 | tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL)); |
| 2755 | else |
| 2756 | tp = curtab; |
| 2757 | win = find_win_by_nr(&argvars[off], tp); |
| 2758 | varname = tv_get_string_chk(&argvars[off + 1]); |
| 2759 | varp = &argvars[off + 2]; |
| 2760 | |
| 2761 | if (win != NULL && varname != NULL && varp != NULL) |
| 2762 | { |
| 2763 | need_switch_win = !(tp == curtab && win == curwin); |
| 2764 | if (!need_switch_win |
| 2765 | || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK) |
| 2766 | { |
| 2767 | if (*varname == '&') |
| 2768 | { |
| 2769 | long numval; |
| 2770 | char_u *strval; |
| 2771 | int error = FALSE; |
| 2772 | |
| 2773 | ++varname; |
| 2774 | numval = (long)tv_get_number_chk(varp, &error); |
| 2775 | strval = tv_get_string_buf_chk(varp, nbuf); |
| 2776 | if (!error && strval != NULL) |
| 2777 | set_option_value(varname, numval, strval, OPT_LOCAL); |
| 2778 | } |
| 2779 | else |
| 2780 | { |
| 2781 | winvarname = alloc(STRLEN(varname) + 3); |
| 2782 | if (winvarname != NULL) |
| 2783 | { |
| 2784 | STRCPY(winvarname, "w:"); |
| 2785 | STRCPY(winvarname + 2, varname); |
| 2786 | set_var(winvarname, varp, TRUE); |
| 2787 | vim_free(winvarname); |
| 2788 | } |
| 2789 | } |
| 2790 | } |
| 2791 | if (need_switch_win) |
| 2792 | restore_win(save_curwin, save_curtab, TRUE); |
| 2793 | } |
| 2794 | } |
| 2795 | |
Bram Moolenaar | e5cdf15 | 2019-08-29 22:09:46 +0200 | [diff] [blame] | 2796 | /* |
| 2797 | * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal, |
| 2798 | * v:option_type, and v:option_command. |
| 2799 | */ |
| 2800 | void |
| 2801 | reset_v_option_vars(void) |
| 2802 | { |
| 2803 | set_vim_var_string(VV_OPTION_NEW, NULL, -1); |
| 2804 | set_vim_var_string(VV_OPTION_OLD, NULL, -1); |
| 2805 | set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1); |
| 2806 | set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1); |
| 2807 | set_vim_var_string(VV_OPTION_TYPE, NULL, -1); |
| 2808 | set_vim_var_string(VV_OPTION_COMMAND, NULL, -1); |
| 2809 | } |
| 2810 | |
| 2811 | /* |
| 2812 | * Add an assert error to v:errors. |
| 2813 | */ |
| 2814 | void |
| 2815 | assert_error(garray_T *gap) |
| 2816 | { |
| 2817 | struct vimvar *vp = &vimvars[VV_ERRORS]; |
| 2818 | |
| 2819 | if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL) |
| 2820 | /* Make sure v:errors is a list. */ |
| 2821 | set_vim_var_list(VV_ERRORS, list_alloc()); |
| 2822 | list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len); |
| 2823 | } |
| 2824 | |
Bram Moolenaar | 0522ba0 | 2019-08-27 22:48:30 +0200 | [diff] [blame] | 2825 | int |
| 2826 | var_exists(char_u *var) |
| 2827 | { |
| 2828 | char_u *name; |
| 2829 | char_u *tofree; |
| 2830 | typval_T tv; |
| 2831 | int len = 0; |
| 2832 | int n = FALSE; |
| 2833 | |
| 2834 | // get_name_len() takes care of expanding curly braces |
| 2835 | name = var; |
| 2836 | len = get_name_len(&var, &tofree, TRUE, FALSE); |
| 2837 | if (len > 0) |
| 2838 | { |
| 2839 | if (tofree != NULL) |
| 2840 | name = tofree; |
| 2841 | n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK); |
| 2842 | if (n) |
| 2843 | { |
| 2844 | // handle d.key, l[idx], f(expr) |
| 2845 | n = (handle_subscript(&var, &tv, TRUE, FALSE, name, &name) == OK); |
| 2846 | if (n) |
| 2847 | clear_tv(&tv); |
| 2848 | } |
| 2849 | } |
| 2850 | if (*var != NUL) |
| 2851 | n = FALSE; |
| 2852 | |
| 2853 | vim_free(tofree); |
| 2854 | return n; |
| 2855 | } |
| 2856 | |
| 2857 | /* |
| 2858 | * "gettabvar()" function |
| 2859 | */ |
| 2860 | void |
| 2861 | f_gettabvar(typval_T *argvars, typval_T *rettv) |
| 2862 | { |
| 2863 | win_T *oldcurwin; |
| 2864 | tabpage_T *tp, *oldtabpage; |
| 2865 | dictitem_T *v; |
| 2866 | char_u *varname; |
| 2867 | int done = FALSE; |
| 2868 | |
| 2869 | rettv->v_type = VAR_STRING; |
| 2870 | rettv->vval.v_string = NULL; |
| 2871 | |
| 2872 | varname = tv_get_string_chk(&argvars[1]); |
| 2873 | tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL)); |
| 2874 | if (tp != NULL && varname != NULL) |
| 2875 | { |
| 2876 | // Set tp to be our tabpage, temporarily. Also set the window to the |
| 2877 | // first window in the tabpage, otherwise the window is not valid. |
| 2878 | if (switch_win(&oldcurwin, &oldtabpage, |
| 2879 | tp == curtab || tp->tp_firstwin == NULL ? firstwin |
| 2880 | : tp->tp_firstwin, tp, TRUE) == OK) |
| 2881 | { |
| 2882 | // look up the variable |
| 2883 | // Let gettabvar({nr}, "") return the "t:" dictionary. |
| 2884 | v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE); |
| 2885 | if (v != NULL) |
| 2886 | { |
| 2887 | copy_tv(&v->di_tv, rettv); |
| 2888 | done = TRUE; |
| 2889 | } |
| 2890 | } |
| 2891 | |
| 2892 | // restore previous notion of curwin |
| 2893 | restore_win(oldcurwin, oldtabpage, TRUE); |
| 2894 | } |
| 2895 | |
| 2896 | if (!done && argvars[2].v_type != VAR_UNKNOWN) |
| 2897 | copy_tv(&argvars[2], rettv); |
| 2898 | } |
| 2899 | |
| 2900 | /* |
| 2901 | * "gettabwinvar()" function |
| 2902 | */ |
| 2903 | void |
| 2904 | f_gettabwinvar(typval_T *argvars, typval_T *rettv) |
| 2905 | { |
| 2906 | getwinvar(argvars, rettv, 1); |
| 2907 | } |
| 2908 | |
| 2909 | /* |
| 2910 | * "getwinvar()" function |
| 2911 | */ |
| 2912 | void |
| 2913 | f_getwinvar(typval_T *argvars, typval_T *rettv) |
| 2914 | { |
| 2915 | getwinvar(argvars, rettv, 0); |
| 2916 | } |
| 2917 | |
| 2918 | /* |
| 2919 | * "settabvar()" function |
| 2920 | */ |
| 2921 | void |
| 2922 | f_settabvar(typval_T *argvars, typval_T *rettv) |
| 2923 | { |
| 2924 | tabpage_T *save_curtab; |
| 2925 | tabpage_T *tp; |
| 2926 | char_u *varname, *tabvarname; |
| 2927 | typval_T *varp; |
| 2928 | |
| 2929 | rettv->vval.v_number = 0; |
| 2930 | |
| 2931 | if (check_secure()) |
| 2932 | return; |
| 2933 | |
| 2934 | tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL)); |
| 2935 | varname = tv_get_string_chk(&argvars[1]); |
| 2936 | varp = &argvars[2]; |
| 2937 | |
| 2938 | if (varname != NULL && varp != NULL && tp != NULL) |
| 2939 | { |
| 2940 | save_curtab = curtab; |
| 2941 | goto_tabpage_tp(tp, FALSE, FALSE); |
| 2942 | |
| 2943 | tabvarname = alloc(STRLEN(varname) + 3); |
| 2944 | if (tabvarname != NULL) |
| 2945 | { |
| 2946 | STRCPY(tabvarname, "t:"); |
| 2947 | STRCPY(tabvarname + 2, varname); |
| 2948 | set_var(tabvarname, varp, TRUE); |
| 2949 | vim_free(tabvarname); |
| 2950 | } |
| 2951 | |
| 2952 | // Restore current tabpage |
| 2953 | if (valid_tabpage(save_curtab)) |
| 2954 | goto_tabpage_tp(save_curtab, FALSE, FALSE); |
| 2955 | } |
| 2956 | } |
| 2957 | |
| 2958 | /* |
| 2959 | * "settabwinvar()" function |
| 2960 | */ |
| 2961 | void |
| 2962 | f_settabwinvar(typval_T *argvars, typval_T *rettv) |
| 2963 | { |
| 2964 | setwinvar(argvars, rettv, 1); |
| 2965 | } |
| 2966 | |
| 2967 | /* |
| 2968 | * "setwinvar()" function |
| 2969 | */ |
| 2970 | void |
| 2971 | f_setwinvar(typval_T *argvars, typval_T *rettv) |
| 2972 | { |
| 2973 | setwinvar(argvars, rettv, 0); |
| 2974 | } |
| 2975 | |
| 2976 | #endif // FEAT_EVAL |