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