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