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