blob: bff5664ef270b5e52a2c34e3e1f936513f23b198 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar9937a052019-06-15 15:45:06 +020031static char *e_cannot_mod = N_("E995: Cannot modify existing variable");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020032#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020033static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020034#endif
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +020035static char *e_nowhitespace = N_("E274: No white space allowed before parenthesis");
Bram Moolenaar8c8de832008-06-24 22:58:06 +000036
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010037#define NAMESPACE_CHAR (char_u *)"abglstvw"
38
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020039static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
41/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000042 * Old Vim variables such as "v:version" are also available without the "v:".
43 * Also in functions. We need a special hashtable for them.
44 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000045static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000046
47/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000048 * When recursively copying lists and dicts we need to remember which ones we
49 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000050 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000051 */
52static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020053
Bram Moolenaard9fba312005-06-26 22:34:35 +000054/*
Bram Moolenaar33570922005-01-25 22:26:29 +000055 * Array to hold the hashtab with variables local to each sourced script.
56 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000057 */
Bram Moolenaar33570922005-01-25 22:26:29 +000058typedef struct
59{
60 dictitem_T sv_var;
61 dict_T sv_dict;
62} scriptvar_T;
63
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020064static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
65#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
66#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68static int echo_attr = 0; /* attributes used for ":echo" */
69
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000070/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000071static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
72
Bram Moolenaar071d4272004-06-13 20:20:40 +000073/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000074 * Info used by a ":for" loop.
75 */
Bram Moolenaar33570922005-01-25 22:26:29 +000076typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000077{
78 int fi_semicolon; /* TRUE if ending in '; var]' */
79 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000080 listwatch_T fi_lw; /* keep an eye on the item used. */
81 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010082 int fi_bi; /* index of blob */
83 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000084} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000085
Bram Moolenaara7043832005-01-21 11:56:39 +000086
87/*
Bram Moolenaar33570922005-01-25 22:26:29 +000088 * Array to hold the value of v: variables.
89 * The value is in a dictitem, so that it can also be used in the v: scope.
90 * The reason to use this table anyway is for very quick access to the
91 * variables with the VV_ defines.
92 */
Bram Moolenaar33570922005-01-25 22:26:29 +000093
94/* values for vv_flags: */
95#define VV_COMPAT 1 /* compatible, also used without "v:" */
96#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000097#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000098
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010099#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000100
101static struct vimvar
102{
103 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100104 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000105 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
106} vimvars[VV_LEN] =
107{
108 /*
109 * The order here must match the VV_ defines in vim.h!
110 * Initializing a union does not work, leave tv.vval empty to get zero's.
111 */
112 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
113 {VV_NAME("count1", VAR_NUMBER), VV_RO},
114 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
115 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
116 {VV_NAME("warningmsg", VAR_STRING), 0},
117 {VV_NAME("statusmsg", VAR_STRING), 0},
118 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
119 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
120 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
121 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
122 {VV_NAME("termresponse", VAR_STRING), VV_RO},
123 {VV_NAME("fname", VAR_STRING), VV_RO},
124 {VV_NAME("lang", VAR_STRING), VV_RO},
125 {VV_NAME("lc_time", VAR_STRING), VV_RO},
126 {VV_NAME("ctype", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
128 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
129 {VV_NAME("fname_in", VAR_STRING), VV_RO},
130 {VV_NAME("fname_out", VAR_STRING), VV_RO},
131 {VV_NAME("fname_new", VAR_STRING), VV_RO},
132 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
133 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
134 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
136 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
137 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
138 {VV_NAME("progname", VAR_STRING), VV_RO},
139 {VV_NAME("servername", VAR_STRING), VV_RO},
140 {VV_NAME("dying", VAR_NUMBER), VV_RO},
141 {VV_NAME("exception", VAR_STRING), VV_RO},
142 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
143 {VV_NAME("register", VAR_STRING), VV_RO},
144 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
145 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000146 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
147 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000148 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000149 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
150 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000151 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
152 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200153 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000154 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
156 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000157 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000158 {VV_NAME("swapname", VAR_STRING), VV_RO},
159 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000160 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200161 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000162 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200163 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000164 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
165 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000166 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000167 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100168 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000169 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200170 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200171 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200172 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200173 {VV_NAME("option_new", VAR_STRING), VV_RO},
174 {VV_NAME("option_old", VAR_STRING), VV_RO},
Bram Moolenaard7c96872019-06-15 17:12:48 +0200175 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
176 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
177 {VV_NAME("option_command", VAR_STRING), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200178 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100179 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100180 {VV_NAME("false", VAR_SPECIAL), VV_RO},
181 {VV_NAME("true", VAR_SPECIAL), VV_RO},
182 {VV_NAME("null", VAR_SPECIAL), VV_RO},
183 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100184 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200185 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200186 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
192 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
193 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
194 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
195 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100196 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200197 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
198 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200199 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200200 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
201 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
202 {VV_NAME("event", VAR_DICT), VV_RO},
203 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000204};
205
206/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000207#define vv_type vv_di.di_tv.v_type
208#define vv_nr vv_di.di_tv.vval.v_number
209#define vv_float vv_di.di_tv.vval.v_float
210#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000211#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200212#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100213#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000214#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000215
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200216static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000217#define vimvarht vimvardict.dv_hashtab
218
Bram Moolenaar9937a052019-06-15 15:45:06 +0200219static void ex_let_const(exarg_T *eap, int is_const);
220static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, int is_const, char_u *nextchars);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
222static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static void list_glob_vars(int *first);
224static void list_buf_vars(int *first);
225static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100226static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void list_vim_vars(int *first);
228static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100229static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200230static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int is_const, char_u *endchars, char_u *op);
231static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int is_const, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100233static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
234static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
235static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
236static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000237
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100238static int eval2(char_u **arg, typval_T *rettv, int evaluate);
239static int eval3(char_u **arg, typval_T *rettv, int evaluate);
240static int eval4(char_u **arg, typval_T *rettv, int evaluate);
241static int eval5(char_u **arg, typval_T *rettv, int evaluate);
242static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
243static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +0200244static int eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000245
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
247static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100248static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100249static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100250static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100251static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200252static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100253static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100254static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100255static void list_one_var(dictitem_T *v, char *prefix, int *first);
256static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar9937a052019-06-15 15:45:06 +0200257static void set_var_const(char_u *name, typval_T *tv, int copy, int is_const);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100258static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100259static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200260
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200261/* for VIM_VERSION_ defines */
262#include "version.h"
263
Bram Moolenaare21c1582019-03-02 11:57:09 +0100264/*
265 * Return "n1" divided by "n2", taking care of dividing by zero.
266 */
267 static varnumber_T
268num_divide(varnumber_T n1, varnumber_T n2)
269{
270 varnumber_T result;
271
272 if (n2 == 0) // give an error message?
273 {
274 if (n1 == 0)
275 result = VARNUM_MIN; // similar to NaN
276 else if (n1 < 0)
277 result = -VARNUM_MAX;
278 else
279 result = VARNUM_MAX;
280 }
281 else
282 result = n1 / n2;
283
284 return result;
285}
286
287/*
288 * Return "n1" modulus "n2", taking care of dividing by zero.
289 */
290 static varnumber_T
291num_modulus(varnumber_T n1, varnumber_T n2)
292{
293 // Give an error when n2 is 0?
294 return (n2 == 0) ? 0 : (n1 % n2);
295}
296
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100297
298#if defined(EBCDIC) || defined(PROTO)
299/*
300 * Compare struct fst by function name.
301 */
302 static int
303compare_func_name(const void *s1, const void *s2)
304{
305 struct fst *p1 = (struct fst *)s1;
306 struct fst *p2 = (struct fst *)s2;
307
308 return STRCMP(p1->f_name, p2->f_name);
309}
310
311/*
312 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100313 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100314 * On machines using EBCDIC we have to sort it.
315 */
316 static void
317sortFunctions(void)
318{
319 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
320
321 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
322}
323#endif
324
325
Bram Moolenaar33570922005-01-25 22:26:29 +0000326/*
327 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000328 */
329 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100330eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000331{
Bram Moolenaar33570922005-01-25 22:26:29 +0000332 int i;
333 struct vimvar *p;
334
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200335 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
336 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200337 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000338 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200339 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000340
341 for (i = 0; i < VV_LEN; ++i)
342 {
343 p = &vimvars[i];
Bram Moolenaard7c96872019-06-15 17:12:48 +0200344 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200345 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100346 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200347 getout(1);
348 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000349 STRCPY(p->vv_di.di_key, p->vv_name);
350 if (p->vv_flags & VV_RO)
351 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
352 else if (p->vv_flags & VV_RO_SBX)
353 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
354 else
355 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000356
357 /* add to v: scope dict, unless the value is not always available */
358 if (p->vv_type != VAR_UNKNOWN)
359 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000360 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000361 /* add to compat scope dict */
362 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000363 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100364 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
Bram Moolenaar37df9a42019-06-14 14:39:51 +0200365 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
Bram Moolenaara542c682016-01-31 16:28:04 +0100366
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000367 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100368 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100369 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100370 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100371 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100372
373 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
374 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
375 set_vim_var_nr(VV_NONE, VVAL_NONE);
376 set_vim_var_nr(VV_NULL, VVAL_NULL);
377
Bram Moolenaarf562e722016-07-19 17:25:25 +0200378 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
379 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
380 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
381 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
382 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
383 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
384 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
385 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
386 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
387 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100388 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200389
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200390 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200391
392#ifdef EBCDIC
393 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100394 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200395 */
396 sortFunctions();
397#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000398}
399
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000400#if defined(EXITFREE) || defined(PROTO)
401 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100402eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000403{
404 int i;
405 struct vimvar *p;
406
407 for (i = 0; i < VV_LEN; ++i)
408 {
409 p = &vimvars[i];
410 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100411 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000412 else if (p->vv_di.di_tv.v_type == VAR_LIST)
413 {
414 list_unref(p->vv_list);
415 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000416 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000417 }
418 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000419 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000420 hash_clear(&compat_hashtab);
421
Bram Moolenaard9fba312005-06-26 22:34:35 +0000422 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200423 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000424
425 /* global variables */
426 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000427
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000428 /* autoloaded script names */
429 ga_clear_strings(&ga_loaded);
430
Bram Moolenaarcca74132013-09-25 21:00:28 +0200431 /* Script-local variables. First clear all the variables and in a second
432 * loop free the scriptvar_T, because a variable in one script might hold
433 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200434 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200435 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200436 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200437 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200438 ga_clear(&ga_scripts);
439
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200440 // unreferenced lists and dicts
441 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200442
443 // functions not garbage collected
444 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000445}
446#endif
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448
449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 * Set an internal variable to a string value. Creates the variable if it does
451 * not already exist.
452 */
453 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100454set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000456 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000457 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458
459 val = vim_strsave(value);
460 if (val != NULL)
461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000462 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000463 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000465 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000466 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 }
468 }
469}
470
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000471static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200472#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000473static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000474static char_u *redir_endp = NULL;
475static char_u *redir_varname = NULL;
476
477/*
478 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100479 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000480 * Returns OK if successfully completed the setup. FAIL otherwise.
481 */
482 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100483var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000484{
485 int save_emsg;
486 int err;
487 typval_T tv;
488
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000489 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000490 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100492 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000493 return FAIL;
494 }
495
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000496 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000497 redir_varname = vim_strsave(name);
498 if (redir_varname == NULL)
499 return FAIL;
500
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200501 redir_lval = ALLOC_CLEAR_ONE(lval_T);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000502 if (redir_lval == NULL)
503 {
504 var_redir_stop();
505 return FAIL;
506 }
507
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000508 /* The output is stored in growarray "redir_ga" until redirection ends. */
509 ga_init2(&redir_ga, (int)sizeof(char), 500);
510
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000511 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100512 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000513 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000514 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
515 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200516 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000517 if (redir_endp != NULL && *redir_endp != NUL)
518 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100519 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000520 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100521 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000522 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000523 var_redir_stop();
524 return FAIL;
525 }
526
527 /* check if we can write to the variable: set it to or append an empty
528 * string */
529 save_emsg = did_emsg;
530 did_emsg = FALSE;
531 tv.v_type = VAR_STRING;
532 tv.vval.v_string = (char_u *)"";
533 if (append)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200534 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)".");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000535 else
Bram Moolenaar9937a052019-06-15 15:45:06 +0200536 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200537 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000538 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000539 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540 if (err)
541 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000542 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000543 var_redir_stop();
544 return FAIL;
545 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000546
547 return OK;
548}
549
550/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000551 * Append "value[value_len]" to the variable set by var_redir_start().
552 * The actual appending is postponed until redirection ends, because the value
553 * appended may in fact be the string we write to, changing it may cause freed
554 * memory to be used:
555 * :redir => foo
556 * :let foo
557 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000558 */
559 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100560var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000561{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000562 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000563
564 if (redir_lval == NULL)
565 return;
566
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000567 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000568 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000569 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000570 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000571
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000572 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000573 {
574 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000575 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000576 }
577 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000578 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000579}
580
581/*
582 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000583 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000584 */
585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100586var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000587{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000588 typval_T tv;
589
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200590 if (EVALCMD_BUSY)
591 {
592 redir_lval = NULL;
593 return;
594 }
595
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000596 if (redir_lval != NULL)
597 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000598 /* If there was no error: assign the text to the variable. */
599 if (redir_endp != NULL)
600 {
601 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
602 tv.v_type = VAR_STRING;
603 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200604 /* Call get_lval() again, if it's inside a Dict or List it may
605 * have changed. */
606 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100607 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200608 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200609 set_var_lval(redir_lval, redir_endp, &tv, FALSE, FALSE,
610 (char_u *)".");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200611 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000612 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000613
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000614 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100615 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000616
Bram Moolenaard23a8232018-02-10 18:45:26 +0100617 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000618 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100619 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000620}
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100623eval_charconvert(
624 char_u *enc_from,
625 char_u *enc_to,
626 char_u *fname_from,
627 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 int err = FALSE;
630
631 set_vim_var_string(VV_CC_FROM, enc_from, -1);
632 set_vim_var_string(VV_CC_TO, enc_to, -1);
633 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
634 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
635 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
636 err = TRUE;
637 set_vim_var_string(VV_CC_FROM, NULL, -1);
638 set_vim_var_string(VV_CC_TO, NULL, -1);
639 set_vim_var_string(VV_FNAME_IN, NULL, -1);
640 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
641
642 if (err)
643 return FAIL;
644 return OK;
645}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
647# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100649eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650{
651 int err = FALSE;
652
653 set_vim_var_string(VV_FNAME_IN, fname, -1);
654 set_vim_var_string(VV_CMDARG, args, -1);
655 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
656 err = TRUE;
657 set_vim_var_string(VV_FNAME_IN, NULL, -1);
658 set_vim_var_string(VV_CMDARG, NULL, -1);
659
660 if (err)
661 {
662 mch_remove(fname);
663 return FAIL;
664 }
665 return OK;
666}
667# endif
668
669# if defined(FEAT_DIFF) || defined(PROTO)
670 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671eval_diff(
672 char_u *origfile,
673 char_u *newfile,
674 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 int err = FALSE;
677
678 set_vim_var_string(VV_FNAME_IN, origfile, -1);
679 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
680 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
681 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
682 set_vim_var_string(VV_FNAME_IN, NULL, -1);
683 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
684 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
685}
686
687 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100688eval_patch(
689 char_u *origfile,
690 char_u *difffile,
691 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
693 int err;
694
695 set_vim_var_string(VV_FNAME_IN, origfile, -1);
696 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
697 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
698 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
699 set_vim_var_string(VV_FNAME_IN, NULL, -1);
700 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
701 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
702}
703# endif
704
705/*
706 * Top level evaluation function, returning a boolean.
707 * Sets "error" to TRUE if there was an error.
708 * Return TRUE or FALSE.
709 */
710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100711eval_to_bool(
712 char_u *arg,
713 int *error,
714 char_u **nextcmd,
715 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
Bram Moolenaar33570922005-01-25 22:26:29 +0000717 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200718 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
720 if (skip)
721 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000722 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 else
725 {
726 *error = FALSE;
727 if (!skip)
728 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100729 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000730 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 }
732 }
733 if (skip)
734 --emsg_skip;
735
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200736 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737}
738
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100739/*
740 * Call eval1() and give an error message if not done at a lower level.
741 */
742 static int
743eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
744{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100745 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100746 int ret;
747 int did_emsg_before = did_emsg;
748 int called_emsg_before = called_emsg;
749
750 ret = eval1(arg, rettv, evaluate);
751 if (ret == FAIL)
752 {
753 // Report the invalid expression unless the expression evaluation has
754 // been cancelled due to an aborting error, an interrupt, or an
755 // exception, or we already gave a more specific error.
756 // Also check called_emsg for when using assert_fails().
757 if (!aborting() && did_emsg == did_emsg_before
758 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100759 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100760 }
761 return ret;
762}
763
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200764 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100765eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
766{
767 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100768 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200769 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100770
771 if (expr->v_type == VAR_FUNC)
772 {
773 s = expr->vval.v_string;
774 if (s == NULL || *s == NUL)
775 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200776 vim_memset(&funcexe, 0, sizeof(funcexe));
777 funcexe.evaluate = TRUE;
778 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100779 return FAIL;
780 }
781 else if (expr->v_type == VAR_PARTIAL)
782 {
783 partial_T *partial = expr->vval.v_partial;
784
785 s = partial_name(partial);
786 if (s == NULL || *s == NUL)
787 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200788 vim_memset(&funcexe, 0, sizeof(funcexe));
789 funcexe.evaluate = TRUE;
790 funcexe.partial = partial;
791 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100792 return FAIL;
793 }
794 else
795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100796 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100797 if (s == NULL)
798 return FAIL;
799 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100800 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100801 return FAIL;
802 if (*s != NUL) /* check for trailing chars after expr */
803 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200804 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100805 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100806 return FAIL;
807 }
808 }
809 return OK;
810}
811
812/*
813 * Like eval_to_bool() but using a typval_T instead of a string.
814 * Works for string, funcref and partial.
815 */
816 int
817eval_expr_to_bool(typval_T *expr, int *error)
818{
819 typval_T rettv;
820 int res;
821
822 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
823 {
824 *error = TRUE;
825 return FALSE;
826 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100827 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100828 clear_tv(&rettv);
829 return res;
830}
831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832/*
833 * Top level evaluation function, returning a string. If "skip" is TRUE,
834 * only parsing to "nextcmd" is done, without reporting errors. Return
835 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
836 */
837 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100838eval_to_string_skip(
839 char_u *arg,
840 char_u **nextcmd,
841 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
Bram Moolenaar33570922005-01-25 22:26:29 +0000843 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 char_u *retval;
845
846 if (skip)
847 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000848 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 retval = NULL;
850 else
851 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100852 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000853 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855 if (skip)
856 --emsg_skip;
857
858 return retval;
859}
860
861/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000862 * Skip over an expression at "*pp".
863 * Return FAIL for an error, OK otherwise.
864 */
865 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100866skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000867{
Bram Moolenaar33570922005-01-25 22:26:29 +0000868 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000869
870 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000871 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000872}
873
874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000876 * When "convert" is TRUE convert a List into a sequence of lines and convert
877 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * Return pointer to allocated memory, or NULL for failure.
879 */
880 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100881eval_to_string(
882 char_u *arg,
883 char_u **nextcmd,
884 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885{
Bram Moolenaar33570922005-01-25 22:26:29 +0000886 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000888 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000889#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000890 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000893 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 retval = NULL;
895 else
896 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000897 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000898 {
899 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000900 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200901 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200902 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200903 if (tv.vval.v_list->lv_len > 0)
904 ga_append(&ga, NL);
905 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000906 ga_append(&ga, NUL);
907 retval = (char_u *)ga.ga_data;
908 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000909#ifdef FEAT_FLOAT
910 else if (convert && tv.v_type == VAR_FLOAT)
911 {
912 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
913 retval = vim_strsave(numbuf);
914 }
915#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000916 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100917 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000918 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920
921 return retval;
922}
923
924/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000925 * Call eval_to_string() without using current local variables and using
926 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 */
928 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100929eval_to_string_safe(
930 char_u *arg,
931 char_u **nextcmd,
932 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
934 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200935 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200937 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000938 if (use_sandbox)
939 ++sandbox;
940 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000941 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000942 if (use_sandbox)
943 --sandbox;
944 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200945 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 return retval;
947}
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949/*
950 * Top level evaluation function, returning a number.
951 * Evaluates "expr" silently.
952 * Returns -1 for an error.
953 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200954 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100955eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
Bram Moolenaar33570922005-01-25 22:26:29 +0000957 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200958 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000959 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 ++emsg_off;
962
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000963 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 retval = -1;
965 else
966 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100967 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000968 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
970 --emsg_off;
971
972 return retval;
973}
974
Bram Moolenaara40058a2005-07-11 22:42:07 +0000975/*
976 * Prepare v: variable "idx" to be used.
977 * Save the current typeval in "save_tv".
978 * When not used yet add the variable to the v: hashtable.
979 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100981prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000982{
983 *save_tv = vimvars[idx].vv_tv;
984 if (vimvars[idx].vv_type == VAR_UNKNOWN)
985 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
986}
987
988/*
989 * Restore v: variable "idx" to typeval "save_tv".
990 * When no longer defined, remove the variable from the v: hashtable.
991 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100993restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000994{
995 hashitem_T *hi;
996
Bram Moolenaara40058a2005-07-11 22:42:07 +0000997 vimvars[idx].vv_tv = *save_tv;
998 if (vimvars[idx].vv_type == VAR_UNKNOWN)
999 {
1000 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1001 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +01001002 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +00001003 else
1004 hash_remove(&vimvarht, hi);
1005 }
1006}
1007
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001008#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001009/*
1010 * Evaluate an expression to a list with suggestions.
1011 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001012 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001013 */
1014 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001015eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001016{
1017 typval_T save_val;
1018 typval_T rettv;
1019 list_T *list = NULL;
1020 char_u *p = skipwhite(expr);
1021
1022 /* Set "v:val" to the bad word. */
1023 prepare_vimvar(VV_VAL, &save_val);
1024 vimvars[VV_VAL].vv_type = VAR_STRING;
1025 vimvars[VV_VAL].vv_str = badword;
1026 if (p_verbose == 0)
1027 ++emsg_off;
1028
1029 if (eval1(&p, &rettv, TRUE) == OK)
1030 {
1031 if (rettv.v_type != VAR_LIST)
1032 clear_tv(&rettv);
1033 else
1034 list = rettv.vval.v_list;
1035 }
1036
1037 if (p_verbose == 0)
1038 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001039 restore_vimvar(VV_VAL, &save_val);
1040
1041 return list;
1042}
1043
1044/*
1045 * "list" is supposed to contain two items: a word and a number. Return the
1046 * word in "pp" and the number as the return value.
1047 * Return -1 if anything isn't right.
1048 * Used to get the good word and score from the eval_spell_expr() result.
1049 */
1050 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001051get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001052{
1053 listitem_T *li;
1054
1055 li = list->lv_first;
1056 if (li == NULL)
1057 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001058 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001059
1060 li = li->li_next;
1061 if (li == NULL)
1062 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001063 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001064}
1065#endif
1066
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001067/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001068 * Top level evaluation function.
1069 * Returns an allocated typval_T with the result.
1070 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001071 */
1072 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001073eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001074{
1075 typval_T *tv;
1076
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001077 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001078 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001079 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001080
1081 return tv;
1082}
1083
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001086 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001087 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1088 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001089 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001091 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001092call_vim_function(
1093 char_u *func,
1094 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001095 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001096 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001098 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001099 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001101 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001102 vim_memset(&funcexe, 0, sizeof(funcexe));
1103 funcexe.firstline = curwin->w_cursor.lnum;
1104 funcexe.lastline = curwin->w_cursor.lnum;
1105 funcexe.evaluate = TRUE;
1106 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001107 if (ret == FAIL)
1108 clear_tv(rettv);
1109
1110 return ret;
1111}
1112
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001113/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001114 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001115 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001116 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1117 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001118 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001119 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001120call_func_retnr(
1121 char_u *func,
1122 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001123 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001124{
1125 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001126 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001127
Bram Moolenaarded27a12018-08-01 19:06:03 +02001128 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001129 return -1;
1130
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001131 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001132 clear_tv(&rettv);
1133 return retval;
1134}
1135
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001136/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001137 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001138 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001139 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1140 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001141 */
1142 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001143call_func_retstr(
1144 char_u *func,
1145 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001146 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001147{
1148 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001149 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001150
Bram Moolenaarded27a12018-08-01 19:06:03 +02001151 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001152 return NULL;
1153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001154 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001155 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 return retval;
1157}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001158
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001159/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001160 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001161 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1162 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001163 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001164 */
1165 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001166call_func_retlist(
1167 char_u *func,
1168 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001169 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001170{
1171 typval_T rettv;
1172
Bram Moolenaarded27a12018-08-01 19:06:03 +02001173 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001174 return NULL;
1175
1176 if (rettv.v_type != VAR_LIST)
1177 {
1178 clear_tv(&rettv);
1179 return NULL;
1180 }
1181
1182 return rettv.vval.v_list;
1183}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
Bram Moolenaar05159a02005-02-26 23:04:13 +00001185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186#ifdef FEAT_FOLDING
1187/*
1188 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1189 * it in "*cp". Doesn't give error messages.
1190 */
1191 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001192eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193{
Bram Moolenaar33570922005-01-25 22:26:29 +00001194 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001195 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001197 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1198 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
1200 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001201 if (use_sandbox)
1202 ++sandbox;
1203 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001205 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 retval = 0;
1207 else
1208 {
1209 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001210 if (tv.v_type == VAR_NUMBER)
1211 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001212 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 retval = 0;
1214 else
1215 {
1216 /* If the result is a string, check if there is a non-digit before
1217 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001218 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (!VIM_ISDIGIT(*s) && *s != '-')
1220 *cp = *s++;
1221 retval = atol((char *)s);
1222 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001223 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001226 if (use_sandbox)
1227 --sandbox;
1228 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001230 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231}
1232#endif
1233
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234/*
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001235 * Get a list of lines from a HERE document. The here document is a list of
1236 * lines surrounded by a marker.
1237 * cmd << {marker}
1238 * {line1}
1239 * {line2}
1240 * ....
1241 * {marker}
1242 *
1243 * The {marker} is a string. If the optional 'trim' word is supplied before the
1244 * marker, then the leading indentation before the lines (matching the
1245 * indentation in the 'cmd' line) is stripped.
1246 * Returns a List with {lines} or NULL.
1247 */
1248 static list_T *
1249heredoc_get(exarg_T *eap, char_u *cmd)
1250{
1251 char_u *theline;
1252 char_u *marker;
1253 list_T *l;
1254 char_u *p;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001255 int marker_indent_len = 0;
1256 int text_indent_len = 0;
1257 char_u *text_indent = NULL;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001258
1259 if (eap->getline == NULL)
1260 {
1261 emsg(_("E991: cannot use =<< here"));
1262 return NULL;
1263 }
1264
1265 // Check for the optional 'trim' word before the marker
1266 cmd = skipwhite(cmd);
1267 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
1268 {
1269 cmd = skipwhite(cmd + 4);
1270
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001271 // Trim the indentation from all the lines in the here document.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001272 // The amount of indentation trimmed is the same as the indentation of
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001273 // the first line after the :let command line. To find the end marker
1274 // the indent of the :let command line is trimmed.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001275 p = *eap->cmdlinep;
1276 while (VIM_ISWHITE(*p))
1277 {
1278 p++;
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001279 marker_indent_len++;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001280 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001281 text_indent_len = -1;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001282 }
1283
Bram Moolenaar24582002019-07-21 14:14:26 +02001284 // The marker is the next word.
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001285 if (*cmd != NUL && *cmd != '"')
1286 {
1287 marker = skipwhite(cmd);
1288 p = skiptowhite(marker);
1289 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
1290 {
1291 emsg(_(e_trailing));
1292 return NULL;
1293 }
1294 *p = NUL;
Bram Moolenaar24582002019-07-21 14:14:26 +02001295 if (vim_islower(*marker))
1296 {
1297 emsg(_("E221: Marker cannot start with lower case letter"));
1298 return NULL;
1299 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001300 }
1301 else
Bram Moolenaar24582002019-07-21 14:14:26 +02001302 {
1303 emsg(_("E172: Missing marker"));
1304 return NULL;
1305 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001306
1307 l = list_alloc();
1308 if (l == NULL)
1309 return NULL;
1310
1311 for (;;)
1312 {
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001313 int mi = 0;
1314 int ti = 0;
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001315
Bram Moolenaare96a2492019-06-25 04:12:16 +02001316 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001317 if (theline == NULL)
1318 {
1319 semsg(_("E990: Missing end marker '%s'"), marker);
1320 break;
1321 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001322
1323 // with "trim": skip the indent matching the :let line to find the
1324 // marker
1325 if (marker_indent_len > 0
1326 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
1327 mi = marker_indent_len;
1328 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001329 {
1330 vim_free(theline);
1331 break;
1332 }
1333
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001334 if (text_indent_len == -1 && *theline != NUL)
1335 {
1336 // set the text indent from the first line.
1337 p = theline;
1338 text_indent_len = 0;
1339 while (VIM_ISWHITE(*p))
1340 {
1341 p++;
1342 text_indent_len++;
1343 }
1344 text_indent = vim_strnsave(theline, text_indent_len);
1345 }
1346 // with "trim": skip the indent matching the first line
1347 if (text_indent != NULL)
1348 for (ti = 0; ti < text_indent_len; ++ti)
1349 if (theline[ti] != text_indent[ti])
1350 break;
1351
1352 if (list_append_string(l, theline + ti, -1) == FAIL)
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001353 break;
1354 vim_free(theline);
1355 }
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001356 vim_free(text_indent);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001357
1358 return l;
1359}
1360
1361/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001362 * ":let" list all variable values
1363 * ":let var1 var2" list variable values
1364 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001365 * ":let var += expr" assignment command.
1366 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001367 * ":let var *= expr" assignment command.
1368 * ":let var /= expr" assignment command.
1369 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001370 * ":let var .= expr" assignment command.
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001371 * ":let var ..= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 */
1374 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001375ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
Bram Moolenaar9937a052019-06-15 15:45:06 +02001377 ex_let_const(eap, FALSE);
1378}
1379
1380/*
1381 * ":const" list all variable values
1382 * ":const var1 var2" list variable values
1383 * ":const var = expr" assignment command.
1384 * ":const [var1, var2] = expr" unpack list.
1385 */
1386 void
1387ex_const(exarg_T *eap)
1388{
1389 ex_let_const(eap, TRUE);
1390}
1391
1392 static void
1393ex_let_const(exarg_T *eap, int is_const)
1394{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001396 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 int var_count = 0;
1400 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001401 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001402 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001403 int first = TRUE;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001404 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
Bram Moolenaardb552d602006-03-23 22:59:57 +00001406 argend = skip_var_list(arg, &var_count, &semicolon);
1407 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001409 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001410 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001411 expr = skipwhite(argend);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001412 concat = expr[0] == '.'
1413 && ((expr[1] == '=' && current_sctx.sc_version < 2)
1414 || (expr[1] == '.' && expr[2] == '='));
1415 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
1416 && expr[1] == '=') || concat))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001418 /*
1419 * ":let" without "=": list variables
1420 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001421 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001422 emsg(_(e_invarg));
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02001423 else if (expr[0] == '.')
1424 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001425 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001427 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001429 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001430 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001431 list_glob_vars(&first);
1432 list_buf_vars(&first);
1433 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001434 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001435 list_script_vars(&first);
1436 list_func_vars(&first);
1437 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 eap->nextcmd = check_nextcmd(arg);
1440 }
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001441 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1442 {
1443 list_T *l;
1444
1445 // HERE document
1446 l = heredoc_get(eap, expr + 3);
1447 if (l != NULL)
1448 {
1449 rettv_list_set(&rettv, l);
1450 op[0] = '=';
1451 op[1] = NUL;
1452 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001453 is_const, op);
Bram Moolenaarf5842c52019-05-19 18:41:26 +02001454 clear_tv(&rettv);
1455 }
1456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 else
1458 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001459 op[0] = '=';
1460 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001461 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001462 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001463 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001464 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001465 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar0f248b02019-04-04 15:36:05 +02001466 if (expr[0] == '.' && expr[1] == '.') // ..=
1467 ++expr;
1468 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001469 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001470 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001471 else
1472 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001473
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 if (eap->skip)
1475 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001476 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 if (eap->skip)
1478 {
1479 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001480 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 --emsg_skip;
1482 }
1483 else if (i != FAIL)
1484 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001485 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001486 is_const, op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001487 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 }
1489 }
1490}
1491
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001492/*
1493 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1494 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar61343f02019-07-20 21:11:13 +02001495 * When "op" is not NULL it points to a string with characters that
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1497 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498 * Returns OK or FAIL;
1499 */
1500 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001501ex_let_vars(
1502 char_u *arg_start,
1503 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001504 int copy, // copy values from "tv", don't move
1505 int semicolon, // from skip_var_list()
1506 int var_count, // from skip_var_list()
1507 int is_const, // lock variables for const
Bram Moolenaar61343f02019-07-20 21:11:13 +02001508 char_u *op)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001509{
1510 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001511 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001512 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001513 listitem_T *item;
1514 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001515
1516 if (*arg != '[')
1517 {
1518 /*
1519 * ":let var = expr" or ":for var in list"
1520 */
Bram Moolenaar61343f02019-07-20 21:11:13 +02001521 if (ex_let_one(arg, tv, copy, is_const, op, op) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001522 return FAIL;
1523 return OK;
1524 }
1525
1526 /*
1527 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1528 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001529 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001531 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001532 return FAIL;
1533 }
1534
1535 i = list_len(l);
1536 if (semicolon == 0 && var_count < i)
1537 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001538 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001539 return FAIL;
1540 }
1541 if (var_count - semicolon > i)
1542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001543 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001544 return FAIL;
1545 }
1546
1547 item = l->lv_first;
1548 while (*arg != ']')
1549 {
1550 arg = skipwhite(arg + 1);
Bram Moolenaar9937a052019-06-15 15:45:06 +02001551 arg = ex_let_one(arg, &item->li_tv, TRUE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001552 (char_u *)",;]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001553 item = item->li_next;
1554 if (arg == NULL)
1555 return FAIL;
1556
1557 arg = skipwhite(arg);
1558 if (*arg == ';')
1559 {
1560 /* Put the rest of the list (may be empty) in the var after ';'.
1561 * Create a new list for this. */
1562 l = list_alloc();
1563 if (l == NULL)
1564 return FAIL;
1565 while (item != NULL)
1566 {
1567 list_append_tv(l, &item->li_tv);
1568 item = item->li_next;
1569 }
1570
1571 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001572 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001573 ltv.vval.v_list = l;
1574 l->lv_refcount = 1;
1575
Bram Moolenaar9937a052019-06-15 15:45:06 +02001576 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, is_const,
Bram Moolenaar61343f02019-07-20 21:11:13 +02001577 (char_u *)"]", op);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001578 clear_tv(&ltv);
1579 if (arg == NULL)
1580 return FAIL;
1581 break;
1582 }
1583 else if (*arg != ',' && *arg != ']')
1584 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001585 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001586 return FAIL;
1587 }
1588 }
1589
1590 return OK;
1591}
1592
1593/*
1594 * Skip over assignable variable "var" or list of variables "[var, var]".
1595 * Used for ":let varvar = expr" and ":for varvar in expr".
1596 * For "[var, var]" increment "*var_count" for each variable.
1597 * for "[var, var; var]" set "semicolon".
1598 * Return NULL for an error.
1599 */
1600 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601skip_var_list(
1602 char_u *arg,
1603 int *var_count,
1604 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001605{
1606 char_u *p, *s;
1607
1608 if (*arg == '[')
1609 {
1610 /* "[var, var]": find the matching ']'. */
1611 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001613 {
1614 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1615 s = skip_var_one(p);
1616 if (s == p)
1617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001618 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001619 return NULL;
1620 }
1621 ++*var_count;
1622
1623 p = skipwhite(s);
1624 if (*p == ']')
1625 break;
1626 else if (*p == ';')
1627 {
1628 if (*semicolon == 1)
1629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001630 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001631 return NULL;
1632 }
1633 *semicolon = 1;
1634 }
1635 else if (*p != ',')
1636 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001637 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001638 return NULL;
1639 }
1640 }
1641 return p + 1;
1642 }
1643 else
1644 return skip_var_one(arg);
1645}
1646
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001647/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001648 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001649 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001650 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001652skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001654 if (*arg == '@' && arg[1] != NUL)
1655 return arg + 2;
1656 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1657 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658}
1659
Bram Moolenaara7043832005-01-21 11:56:39 +00001660/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001661 * List variables for hashtab "ht" with prefix "prefix".
1662 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001663 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001664 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001665list_hashtable_vars(
1666 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001667 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001668 int empty,
1669 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001670{
Bram Moolenaar33570922005-01-25 22:26:29 +00001671 hashitem_T *hi;
1672 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001673 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001674 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001675
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001676 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001677 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1678 {
1679 if (!HASHITEM_EMPTY(hi))
1680 {
1681 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001682 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001683
1684 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001685 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1686 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001687 if (message_filtered(buf))
1688 continue;
1689
Bram Moolenaar33570922005-01-25 22:26:29 +00001690 if (empty || di->di_tv.v_type != VAR_STRING
1691 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001692 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001693 }
1694 }
1695}
1696
1697/*
1698 * List global variables.
1699 */
1700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001701list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001702{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001703 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001704}
1705
1706/*
1707 * List buffer variables.
1708 */
1709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001710list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001711{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001712 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001713}
1714
1715/*
1716 * List window variables.
1717 */
1718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001719list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001720{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001721 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001722}
1723
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001724/*
1725 * List tab page variables.
1726 */
1727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001728list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001729{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001730 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001731}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001732
Bram Moolenaara7043832005-01-21 11:56:39 +00001733/*
1734 * List Vim variables.
1735 */
1736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001737list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001738{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001739 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001740}
1741
1742/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001743 * List script-local variables, if there is a script.
1744 */
1745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001746list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001747{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001748 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1749 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001750 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001751}
1752
1753/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001754 * List variables in "arg".
1755 */
1756 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001757list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001758{
1759 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001760 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001761 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001762 char_u *name_start;
1763 char_u *arg_subsc;
1764 char_u *tofree;
1765 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001766
1767 while (!ends_excmd(*arg) && !got_int)
1768 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001769 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001770 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001771 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001772 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001773 {
1774 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001775 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001776 break;
1777 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001779 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001780 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001781 /* get_name_len() takes care of expanding curly braces */
1782 name_start = name = arg;
1783 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1784 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001785 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001786 /* This is mainly to keep test 49 working: when expanding
1787 * curly braces fails overrule the exception error message. */
1788 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001790 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001791 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001792 break;
1793 }
1794 error = TRUE;
1795 }
1796 else
1797 {
1798 if (tofree != NULL)
1799 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001800 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 else
1803 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001804 /* handle d.key, l[idx], f(expr) */
1805 arg_subsc = arg;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02001806 if (handle_subscript(&arg, &tv, TRUE, TRUE,
1807 name, &name) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001808 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001810 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001811 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001812 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001813 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001814 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001815 case 'g': list_glob_vars(first); break;
1816 case 'b': list_buf_vars(first); break;
1817 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001818 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001819 case 'v': list_vim_vars(first); break;
1820 case 's': list_script_vars(first); break;
1821 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001822 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001823 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001824 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001825 }
1826 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001827 {
1828 char_u numbuf[NUMBUFLEN];
1829 char_u *tf;
1830 int c;
1831 char_u *s;
1832
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001833 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001834 c = *arg;
1835 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001836 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001837 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001838 tv.v_type,
1839 s == NULL ? (char_u *)"" : s,
1840 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001841 *arg = c;
1842 vim_free(tf);
1843 }
1844 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 }
1847 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001848
1849 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001851
1852 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 }
1854
1855 return arg;
1856}
1857
1858/*
1859 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1860 * Returns a pointer to the char just after the var name.
1861 * Returns NULL if there is an error.
1862 */
1863 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001864ex_let_one(
Bram Moolenaar9937a052019-06-15 15:45:06 +02001865 char_u *arg, // points to variable name
1866 typval_T *tv, // value to assign to variable
1867 int copy, // copy value from "tv"
1868 int is_const, // lock variable for const
1869 char_u *endchars, // valid chars after variable name or NULL
1870 char_u *op) // "+", "-", "." or NULL
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871{
1872 int c1;
1873 char_u *name;
1874 char_u *p;
1875 char_u *arg_end = NULL;
1876 int len;
1877 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001878 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879
1880 /*
1881 * ":let $VAR = expr": Set environment variable.
1882 */
1883 if (*arg == '$')
1884 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001885 if (is_const)
1886 {
1887 emsg(_("E996: Cannot lock an environment variable"));
1888 return NULL;
1889 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 /* Find the end of the name. */
1891 ++arg;
1892 name = arg;
1893 len = get_env_len(&arg);
1894 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001895 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 else
1897 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001898 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001899 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001902 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001903 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 {
1905 c1 = name[len];
1906 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001907 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001908 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 {
1910 int mustfree = FALSE;
1911 char_u *s = vim_getenv(name, &mustfree);
1912
1913 if (s != NULL)
1914 {
1915 p = tofree = concat_str(s, p);
1916 if (mustfree)
1917 vim_free(s);
1918 }
1919 }
1920 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001921 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001923 if (STRICMP(name, "HOME") == 0)
1924 init_homedir();
1925 else if (didset_vim && STRICMP(name, "VIM") == 0)
1926 didset_vim = FALSE;
1927 else if (didset_vimruntime
1928 && STRICMP(name, "VIMRUNTIME") == 0)
1929 didset_vimruntime = FALSE;
1930 arg_end = arg;
1931 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 }
1935 }
1936 }
1937
1938 /*
1939 * ":let &option = expr": Set option value.
1940 * ":let &l:option = expr": Set local option value.
1941 * ":let &g:option = expr": Set global option value.
1942 */
1943 else if (*arg == '&')
1944 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02001945 if (is_const)
1946 {
1947 emsg(_("E996: Cannot lock an option"));
1948 return NULL;
1949 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 /* Find the end of the name. */
1951 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 if (p == NULL || (endchars != NULL
1953 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001954 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 else
1956 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 long n;
1958 int opt_type;
1959 long numval;
1960 char_u *stringval = NULL;
1961 char_u *s;
1962
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 c1 = *p;
1964 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001966 n = (long)tv_get_number(tv);
1967 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001968 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 {
1970 opt_type = get_option_value(arg, &numval,
1971 &stringval, opt_flags);
1972 if ((opt_type == 1 && *op == '.')
1973 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001974 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001975 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001976 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001977 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001978 else
1979 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001980 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001982 switch (*op)
1983 {
1984 case '+': n = numval + n; break;
1985 case '-': n = numval - n; break;
1986 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001987 case '/': n = (long)num_divide(numval, n); break;
1988 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001989 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001990 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001991 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001992 {
1993 s = concat_str(stringval, s);
1994 vim_free(stringval);
1995 stringval = s;
1996 }
1997 }
1998 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001999 if (s != NULL)
2000 {
2001 set_option_value(arg, n, s, opt_flags);
2002 arg_end = p;
2003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002004 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002005 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002006 }
2007 }
2008
2009 /*
2010 * ":let @r = expr": Set register contents.
2011 */
2012 else if (*arg == '@')
2013 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002014 if (is_const)
2015 {
2016 emsg(_("E996: Cannot lock a register"));
2017 return NULL;
2018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002020 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002021 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002024 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002025 else
2026 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002027 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028 char_u *s;
2029
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002030 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002031 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002032 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002033 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002034 if (s != NULL)
2035 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002036 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002037 vim_free(s);
2038 }
2039 }
2040 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002041 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002043 arg_end = arg + 1;
2044 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002045 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046 }
2047 }
2048
2049 /*
2050 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002051 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002053 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002054 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002055 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002057 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002058 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002059 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002060 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002061 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002062 else
2063 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02002064 set_var_lval(&lv, p, tv, copy, is_const, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 arg_end = p;
2066 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002067 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002068 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002069 }
2070
2071 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002072 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073
2074 return arg_end;
2075}
2076
2077/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002078 * Get an lval: variable, Dict item or List item that can be assigned a value
2079 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2080 * "name.key", "name.key[expr]" etc.
2081 * Indexing only works if "name" is an existing List or Dictionary.
2082 * "name" points to the start of the name.
2083 * If "rettv" is not NULL it points to the value to be assigned.
2084 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2085 * wrong; must end in space or cmd separator.
2086 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002087 * flags:
2088 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01002089 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002090 * GLV_NO_AUTOLOAD: do not use script autoloading
2091 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002092 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002094 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002096 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002097get_lval(
2098 char_u *name,
2099 typval_T *rettv,
2100 lval_T *lp,
2101 int unlet,
2102 int skip,
2103 int flags, /* GLV_ values */
2104 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002105{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002107 char_u *expr_start, *expr_end;
2108 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002109 dictitem_T *v;
2110 typval_T var1;
2111 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002113 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002114 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002115 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002116 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002117 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002118
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002119 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002121
2122 if (skip)
2123 {
2124 /* When skipping just find the end of the name. */
2125 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002126 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127 }
2128
2129 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002130 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002131 if (expr_start != NULL)
2132 {
2133 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002134 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002135 && *p != '[' && *p != '.')
2136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002137 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002138 return NULL;
2139 }
2140
2141 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2142 if (lp->ll_exp_name == NULL)
2143 {
2144 /* Report an invalid expression in braces, unless the
2145 * expression evaluation has been cancelled due to an
2146 * aborting error, an interrupt, or an exception. */
2147 if (!aborting() && !quiet)
2148 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002149 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002150 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 return NULL;
2152 }
2153 }
2154 lp->ll_name = lp->ll_exp_name;
2155 }
2156 else
2157 lp->ll_name = name;
2158
2159 /* Without [idx] or .key we are done. */
2160 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2161 return p;
2162
2163 cc = *p;
2164 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002165 /* Only pass &ht when we would write to the variable, it prevents autoload
2166 * as well. */
2167 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
2168 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002169 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002170 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002171 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 if (v == NULL)
2173 return NULL;
2174
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002175 /*
2176 * Loop until no more [idx] or .key is following.
2177 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002178 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002179 var1.v_type = VAR_UNKNOWN;
2180 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002181 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2184 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002185 && lp->ll_tv->vval.v_dict != NULL)
2186 && !(lp->ll_tv->v_type == VAR_BLOB
2187 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002189 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002190 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002193 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002195 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002196 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002199
Bram Moolenaar8c711452005-01-14 21:53:12 +00002200 len = -1;
2201 if (*p == '.')
2202 {
2203 key = p + 1;
2204 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2205 ;
2206 if (len == 0)
2207 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002209 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002210 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002211 }
2212 p = key + len;
2213 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002214 else
2215 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002216 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002217 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002218 if (*p == ':')
2219 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002220 else
2221 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002222 empty1 = FALSE;
2223 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002224 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002225 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002226 {
2227 /* not a number or string */
2228 clear_tv(&var1);
2229 return NULL;
2230 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002231 }
2232
2233 /* Optionally get the second index [ :expr]. */
2234 if (*p == ':')
2235 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002237 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002238 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002239 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002240 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002241 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002242 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002243 if (rettv != NULL
2244 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002245 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002246 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002247 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002248 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002249 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002250 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002251 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002252 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002253 }
2254 p = skipwhite(p + 1);
2255 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002257 else
2258 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002259 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002260 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2261 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002262 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002263 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002264 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002265 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002266 {
2267 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002268 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002269 clear_tv(&var2);
2270 return NULL;
2271 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002272 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002273 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002274 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002275 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002276 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002277
Bram Moolenaar8c711452005-01-14 21:53:12 +00002278 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002279 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002280 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002281 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002282 clear_tv(&var1);
2283 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002285 }
2286
2287 /* Skip to past ']'. */
2288 ++p;
2289 }
2290
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002292 {
2293 if (len == -1)
2294 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002296 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002297 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002298 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002299 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002301 }
2302 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002303 lp->ll_list = NULL;
2304 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002305 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002306
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002307 /* When assigning to a scope dictionary check that a function and
2308 * variable name is valid (only variable name unless it is l: or
2309 * g: dictionary). Disallow overwriting a builtin function. */
2310 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002311 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002312 int prevval;
2313 int wrong;
2314
2315 if (len != -1)
2316 {
2317 prevval = key[len];
2318 key[len] = NUL;
2319 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002320 else
2321 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002322 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2323 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002324 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002325 || !valid_varname(key);
2326 if (len != -1)
2327 key[len] = prevval;
2328 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002329 return NULL;
2330 }
2331
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002333 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002334 // Can't add "v:" or "a:" variable.
2335 if (lp->ll_dict == &vimvardict
2336 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002338 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002339 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002340 return NULL;
2341 }
2342
Bram Moolenaar31b81602019-02-10 22:14:27 +01002343 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002344 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002345 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002347 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002348 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002349 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 }
2351 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002353 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002355 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002357 p = NULL;
2358 break;
2359 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002360 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002361 else if ((flags & GLV_READ_ONLY) == 0
2362 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002363 {
2364 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002365 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002366 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002367
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002368 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002370 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002371 else if (lp->ll_tv->v_type == VAR_BLOB)
2372 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002373 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2374
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002375 /*
2376 * Get the number and item for the only or first index of the List.
2377 */
2378 if (empty1)
2379 lp->ll_n1 = 0;
2380 else
2381 // is number or string
2382 lp->ll_n1 = (long)tv_get_number(&var1);
2383 clear_tv(&var1);
2384
2385 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002386 || lp->ll_n1 > bloblen
2387 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002388 {
2389 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002390 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002391 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002392 return NULL;
2393 }
2394 if (lp->ll_range && !lp->ll_empty2)
2395 {
2396 lp->ll_n2 = (long)tv_get_number(&var2);
2397 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002398 if (lp->ll_n2 < 0
2399 || lp->ll_n2 >= bloblen
2400 || lp->ll_n2 < lp->ll_n1)
2401 {
2402 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002403 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002404 return NULL;
2405 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002406 }
2407 lp->ll_blob = lp->ll_tv->vval.v_blob;
2408 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002409 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002410 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002411 else
2412 {
2413 /*
2414 * Get the number and item for the only or first index of the List.
2415 */
2416 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002418 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002419 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002420 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002421 clear_tv(&var1);
2422
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 lp->ll_list = lp->ll_tv->vval.v_list;
2425 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2426 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002427 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002428 if (lp->ll_n1 < 0)
2429 {
2430 lp->ll_n1 = 0;
2431 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2432 }
2433 }
2434 if (lp->ll_li == NULL)
2435 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002436 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002437 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002438 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 }
2441
2442 /*
2443 * May need to find the item or absolute index for the second
2444 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002445 * When no index given: "lp->ll_empty2" is TRUE.
2446 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002447 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002450 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002451 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002457 {
2458 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002459 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002461 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 }
2464
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2466 if (lp->ll_n1 < 0)
2467 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2468 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002469 {
2470 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002471 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002473 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002474 }
2475
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002477 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 }
2479
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002480 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 return p;
2482}
2483
2484/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002485 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002487 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002488clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489{
2490 vim_free(lp->ll_exp_name);
2491 vim_free(lp->ll_newkey);
2492}
2493
2494/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002495 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002497 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2498 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 */
2500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002501set_var_lval(
2502 lval_T *lp,
2503 char_u *endp,
2504 typval_T *rettv,
2505 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02002506 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01002507 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508{
2509 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002510 listitem_T *ri;
2511 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512
2513 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002515 cc = *endp;
2516 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002517 if (lp->ll_blob != NULL)
2518 {
2519 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002520
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002521 if (op != NULL && *op != '=')
2522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002523 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002524 return;
2525 }
2526
2527 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2528 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002529 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002530
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002531 if (lp->ll_empty2)
2532 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2533
2534 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002536 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002537 return;
2538 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002539 if (lp->ll_empty2)
2540 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002541
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002542 ir = 0;
2543 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2544 blob_set(lp->ll_blob, il,
2545 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002546 }
2547 else
2548 {
2549 val = (int)tv_get_number_chk(rettv, &error);
2550 if (!error)
2551 {
2552 garray_T *gap = &lp->ll_blob->bv_ga;
2553
2554 // Allow for appending a byte. Setting a byte beyond
2555 // the end is an error otherwise.
2556 if (lp->ll_n1 < gap->ga_len
2557 || (lp->ll_n1 == gap->ga_len
2558 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2559 {
2560 blob_set(lp->ll_blob, lp->ll_n1, val);
2561 if (lp->ll_n1 == gap->ga_len)
2562 ++gap->ga_len;
2563 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002564 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002565 }
2566 }
2567 }
2568 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002570 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002571
Bram Moolenaar9937a052019-06-15 15:45:06 +02002572 if (is_const)
2573 {
2574 emsg(_(e_cannot_mod));
2575 *endp = cc;
2576 return;
2577 }
2578
Bram Moolenaarff697e62019-02-12 22:28:33 +01002579 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002580 di = NULL;
2581 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2582 &tv, &di, TRUE, FALSE) == OK)
2583 {
2584 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002585 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2586 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002587 && tv_op(&tv, rettv, op) == OK)
2588 set_var(lp->ll_name, &tv, FALSE);
2589 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002591 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002592 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02002593 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01002594 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002596 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002597 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002598 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002599 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 else if (lp->ll_range)
2601 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002602 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002603 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002604
Bram Moolenaar9937a052019-06-15 15:45:06 +02002605 if (is_const)
2606 {
2607 emsg(_("E996: Cannot lock a range"));
2608 return;
2609 }
2610
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002611 /*
2612 * Check whether any of the list items is locked
2613 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002614 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002615 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002616 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002617 return;
2618 ri = ri->li_next;
2619 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2620 break;
2621 ll_li = ll_li->li_next;
2622 ++ll_n1;
2623 }
2624
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 /*
2626 * Assign the List values to the list items.
2627 */
2628 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002630 if (op != NULL && *op != '=')
2631 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2632 else
2633 {
2634 clear_tv(&lp->ll_li->li_tv);
2635 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2636 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 ri = ri->li_next;
2638 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2639 break;
2640 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002643 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 ri = NULL;
2646 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002647 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002648 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 lp->ll_li = lp->ll_li->li_next;
2650 ++lp->ll_n1;
2651 }
2652 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002653 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002654 else if (lp->ll_empty2
2655 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002657 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 }
2659 else
2660 {
2661 /*
2662 * Assign to a List or Dictionary item.
2663 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02002664 if (is_const)
2665 {
2666 emsg(_("E996: Cannot lock a list or dict"));
2667 return;
2668 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (lp->ll_newkey != NULL)
2670 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002671 if (op != NULL && *op != '=')
2672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002673 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002674 return;
2675 }
2676
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002678 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (di == NULL)
2680 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002681 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2682 {
2683 vim_free(di);
2684 return;
2685 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002688 else if (op != NULL && *op != '=')
2689 {
2690 tv_op(lp->ll_tv, rettv, op);
2691 return;
2692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002693 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 /*
2697 * Assign the value to the variable or list item.
2698 */
2699 if (copy)
2700 copy_tv(rettv, lp->ll_tv);
2701 else
2702 {
2703 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002704 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002706 }
2707 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002708}
2709
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002710/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002711 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2712 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002713 * Returns OK or FAIL.
2714 */
2715 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002716tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002717{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002718 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002719 char_u numbuf[NUMBUFLEN];
2720 char_u *s;
2721
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002722 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2723 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2724 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 {
2726 switch (tv1->v_type)
2727 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002728 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002729 case VAR_DICT:
2730 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002731 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002732 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002733 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002734 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735 break;
2736
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002737 case VAR_BLOB:
2738 if (*op != '+' || tv2->v_type != VAR_BLOB)
2739 break;
2740 // BLOB += BLOB
2741 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2742 {
2743 blob_T *b1 = tv1->vval.v_blob;
2744 blob_T *b2 = tv2->vval.v_blob;
2745 int i, len = blob_len(b2);
2746 for (i = 0; i < len; i++)
2747 ga_append(&b1->bv_ga, blob_get(b2, i));
2748 }
2749 return OK;
2750
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002751 case VAR_LIST:
2752 if (*op != '+' || tv2->v_type != VAR_LIST)
2753 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002754 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002755 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2756 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2757 return OK;
2758
2759 case VAR_NUMBER:
2760 case VAR_STRING:
2761 if (tv2->v_type == VAR_LIST)
2762 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002763 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002764 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002765 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002766 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002767#ifdef FEAT_FLOAT
2768 if (tv2->v_type == VAR_FLOAT)
2769 {
2770 float_T f = n;
2771
Bram Moolenaarff697e62019-02-12 22:28:33 +01002772 if (*op == '%')
2773 break;
2774 switch (*op)
2775 {
2776 case '+': f += tv2->vval.v_float; break;
2777 case '-': f -= tv2->vval.v_float; break;
2778 case '*': f *= tv2->vval.v_float; break;
2779 case '/': f /= tv2->vval.v_float; break;
2780 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002781 clear_tv(tv1);
2782 tv1->v_type = VAR_FLOAT;
2783 tv1->vval.v_float = f;
2784 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002785 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002786#endif
2787 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002788 switch (*op)
2789 {
2790 case '+': n += tv_get_number(tv2); break;
2791 case '-': n -= tv_get_number(tv2); break;
2792 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002793 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2794 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002795 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002796 clear_tv(tv1);
2797 tv1->v_type = VAR_NUMBER;
2798 tv1->vval.v_number = n;
2799 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002800 }
2801 else
2802 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002803 if (tv2->v_type == VAR_FLOAT)
2804 break;
2805
Bram Moolenaarff697e62019-02-12 22:28:33 +01002806 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002807 s = tv_get_string(tv1);
2808 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002809 clear_tv(tv1);
2810 tv1->v_type = VAR_STRING;
2811 tv1->vval.v_string = s;
2812 }
2813 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002814
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002815 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002816#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002817 {
2818 float_T f;
2819
Bram Moolenaarff697e62019-02-12 22:28:33 +01002820 if (*op == '%' || *op == '.'
2821 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002822 && tv2->v_type != VAR_NUMBER
2823 && tv2->v_type != VAR_STRING))
2824 break;
2825 if (tv2->v_type == VAR_FLOAT)
2826 f = tv2->vval.v_float;
2827 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002828 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002829 switch (*op)
2830 {
2831 case '+': tv1->vval.v_float += f; break;
2832 case '-': tv1->vval.v_float -= f; break;
2833 case '*': tv1->vval.v_float *= f; break;
2834 case '/': tv1->vval.v_float /= f; break;
2835 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002836 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002837#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002838 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002839 }
2840 }
2841
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002842 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843 return FAIL;
2844}
2845
2846/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002847 * Evaluate the expression used in a ":for var in expr" command.
2848 * "arg" points to "var".
2849 * Set "*errp" to TRUE for an error, FALSE otherwise;
2850 * Return a pointer that holds the info. Null when there is an error.
2851 */
2852 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002853eval_for_line(
2854 char_u *arg,
2855 int *errp,
2856 char_u **nextcmdp,
2857 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002858{
Bram Moolenaar33570922005-01-25 22:26:29 +00002859 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002860 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 typval_T tv;
2862 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002863
2864 *errp = TRUE; /* default: there is an error */
2865
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002866 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002867 if (fi == NULL)
2868 return NULL;
2869
2870 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2871 if (expr == NULL)
2872 return fi;
2873
2874 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002875 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002876 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002877 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002878 return fi;
2879 }
2880
2881 if (skip)
2882 ++emsg_skip;
2883 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2884 {
2885 *errp = FALSE;
2886 if (!skip)
2887 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002888 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002889 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002890 l = tv.vval.v_list;
2891 if (l == NULL)
2892 {
2893 // a null list is like an empty list: do nothing
2894 clear_tv(&tv);
2895 }
2896 else
2897 {
2898 // No need to increment the refcount, it's already set for
2899 // the list being used in "tv".
2900 fi->fi_list = l;
2901 list_add_watch(l, &fi->fi_lw);
2902 fi->fi_lw.lw_item = l->lv_first;
2903 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002904 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002905 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002906 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002907 fi->fi_bi = 0;
2908 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002909 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002910 typval_T btv;
2911
2912 // Make a copy, so that the iteration still works when the
2913 // blob is changed.
2914 blob_copy(&tv, &btv);
2915 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002916 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002917 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002918 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002919 else
2920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002921 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002922 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002923 }
2924 }
2925 }
2926 if (skip)
2927 --emsg_skip;
2928
2929 return fi;
2930}
2931
2932/*
2933 * Use the first item in a ":for" list. Advance to the next.
2934 * Assign the values to the variable (list). "arg" points to the first one.
2935 * Return TRUE when a valid item was found, FALSE when at end of list or
2936 * something wrong.
2937 */
2938 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002939next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002940{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002941 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002942 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002943 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002944
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002945 if (fi->fi_blob != NULL)
2946 {
2947 typval_T tv;
2948
2949 if (fi->fi_bi >= blob_len(fi->fi_blob))
2950 return FALSE;
2951 tv.v_type = VAR_NUMBER;
2952 tv.v_lock = VAR_FIXED;
2953 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2954 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002955 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
2956 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002957 }
2958
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002959 item = fi->fi_lw.lw_item;
2960 if (item == NULL)
2961 result = FALSE;
2962 else
2963 {
2964 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002965 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
2966 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002967 }
2968 return result;
2969}
2970
2971/*
2972 * Free the structure used to store info used by ":for".
2973 */
2974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002975free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002976{
Bram Moolenaar33570922005-01-25 22:26:29 +00002977 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002978
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002979 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002980 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002982 list_unref(fi->fi_list);
2983 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002984 if (fi != NULL && fi->fi_blob != NULL)
2985 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002986 vim_free(fi);
2987}
2988
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002990set_context_for_expression(
2991 expand_T *xp,
2992 char_u *arg,
2993 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995 int got_eq = FALSE;
2996 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002997 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002999 if (cmdidx == CMD_let)
3000 {
3001 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003002 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003 {
3004 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003005 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003006 {
3007 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003008 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003009 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003010 break;
3011 }
3012 return;
3013 }
3014 }
3015 else
3016 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3017 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 while ((xp->xp_pattern = vim_strpbrk(arg,
3019 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3020 {
3021 c = *xp->xp_pattern;
3022 if (c == '&')
3023 {
3024 c = xp->xp_pattern[1];
3025 if (c == '&')
3026 {
3027 ++xp->xp_pattern;
3028 xp->xp_context = cmdidx != CMD_let || got_eq
3029 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3030 }
3031 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003032 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003034 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3035 xp->xp_pattern += 2;
3036
3037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 }
3039 else if (c == '$')
3040 {
3041 /* environment variable */
3042 xp->xp_context = EXPAND_ENV_VARS;
3043 }
3044 else if (c == '=')
3045 {
3046 got_eq = TRUE;
3047 xp->xp_context = EXPAND_EXPRESSION;
3048 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003049 else if (c == '#'
3050 && xp->xp_context == EXPAND_EXPRESSION)
3051 {
3052 /* Autoload function/variable contains '#'. */
3053 break;
3054 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003055 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 && xp->xp_context == EXPAND_FUNCTIONS
3057 && vim_strchr(xp->xp_pattern, '(') == NULL)
3058 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003059 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 break;
3061 }
3062 else if (cmdidx != CMD_let || got_eq)
3063 {
3064 if (c == '"') /* string */
3065 {
3066 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3067 if (c == '\\' && xp->xp_pattern[1] != NUL)
3068 ++xp->xp_pattern;
3069 xp->xp_context = EXPAND_NOTHING;
3070 }
3071 else if (c == '\'') /* literal string */
3072 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003073 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3075 /* skip */ ;
3076 xp->xp_context = EXPAND_NOTHING;
3077 }
3078 else if (c == '|')
3079 {
3080 if (xp->xp_pattern[1] == '|')
3081 {
3082 ++xp->xp_pattern;
3083 xp->xp_context = EXPAND_EXPRESSION;
3084 }
3085 else
3086 xp->xp_context = EXPAND_COMMANDS;
3087 }
3088 else
3089 xp->xp_context = EXPAND_EXPRESSION;
3090 }
3091 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003092 /* Doesn't look like something valid, expand as an expression
3093 * anyway. */
3094 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 arg = xp->xp_pattern;
3096 if (*arg != NUL)
3097 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3098 /* skip */ ;
3099 }
3100 xp->xp_pattern = arg;
3101}
3102
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 * ":unlet[!] var1 ... " command.
3105 */
3106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003107ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003109 ex_unletlock(eap, eap->arg, 0);
3110}
3111
3112/*
3113 * ":lockvar" and ":unlockvar" commands
3114 */
3115 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003116ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003117{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003119 int deep = 2;
3120
3121 if (eap->forceit)
3122 deep = -1;
3123 else if (vim_isdigit(*arg))
3124 {
3125 deep = getdigits(&arg);
3126 arg = skipwhite(arg);
3127 }
3128
3129 ex_unletlock(eap, arg, deep);
3130}
3131
3132/*
3133 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3134 */
3135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003136ex_unletlock(
3137 exarg_T *eap,
3138 char_u *argstart,
3139 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003140{
3141 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003144 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
3146 do
3147 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02003148 if (*arg == '$')
3149 {
3150 char_u *name = ++arg;
3151
3152 if (get_env_len(&arg) == 0)
3153 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003154 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02003155 return;
3156 }
3157 vim_unsetenv(name);
3158 arg = skipwhite(arg);
3159 continue;
3160 }
3161
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003162 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003163 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003164 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003165 if (lv.ll_name == NULL)
3166 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003167 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003168 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003170 if (name_end != NULL)
3171 {
3172 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003173 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003174 }
3175 if (!(eap->skip || error))
3176 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 break;
3178 }
3179
3180 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003181 {
3182 if (eap->cmdidx == CMD_unlet)
3183 {
3184 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3185 error = TRUE;
3186 }
3187 else
3188 {
3189 if (do_lock_var(&lv, name_end, deep,
3190 eap->cmdidx == CMD_lockvar) == FAIL)
3191 error = TRUE;
3192 }
3193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003195 if (!eap->skip)
3196 clear_lval(&lv);
3197
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 arg = skipwhite(name_end);
3199 } while (!ends_excmd(*arg));
3200
3201 eap->nextcmd = check_nextcmd(arg);
3202}
3203
Bram Moolenaar8c711452005-01-14 21:53:12 +00003204 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003205do_unlet_var(
3206 lval_T *lp,
3207 char_u *name_end,
3208 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003209{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003210 int ret = OK;
3211 int cc;
3212
3213 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003214 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003215 cc = *name_end;
3216 *name_end = NUL;
3217
3218 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003219 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003221 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003222 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003223 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003224 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003225 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003226 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003227 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003228 else if (lp->ll_range)
3229 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003230 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003231 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003232 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003233
3234 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3235 {
3236 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003237 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003238 return FAIL;
3239 ll_li = li;
3240 ++ll_n1;
3241 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003242
3243 /* Delete a range of List items. */
3244 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3245 {
3246 li = lp->ll_li->li_next;
3247 listitem_remove(lp->ll_list, lp->ll_li);
3248 lp->ll_li = li;
3249 ++lp->ll_n1;
3250 }
3251 }
3252 else
3253 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003254 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003255 /* unlet a List item. */
3256 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003257 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003258 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003259 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003260 }
3261
3262 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003263}
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265/*
3266 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003267 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 */
3269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003270do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 hashtab_T *ht;
3273 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003274 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003275 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003276 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 ht = find_var_ht(name, &varname);
3279 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003281 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003282 if (d == NULL)
3283 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003284 if (ht == &globvarht)
3285 d = &globvardict;
3286 else if (ht == &compat_hashtab)
3287 d = &vimvardict;
3288 else
3289 {
3290 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3291 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3292 }
3293 if (d == NULL)
3294 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003295 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003296 return FAIL;
3297 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003298 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003300 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003301 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003302 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003303 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003304 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003305 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003306 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003307 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003308 return FAIL;
3309
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003310 delete_var(ht, hi);
3311 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003314 if (forceit)
3315 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003316 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 return FAIL;
3318}
3319
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003320/*
3321 * Lock or unlock variable indicated by "lp".
3322 * "deep" is the levels to go (-1 for unlimited);
3323 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3324 */
3325 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003326do_lock_var(
3327 lval_T *lp,
3328 char_u *name_end,
3329 int deep,
3330 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003331{
3332 int ret = OK;
3333 int cc;
3334 dictitem_T *di;
3335
3336 if (deep == 0) /* nothing to do */
3337 return OK;
3338
3339 if (lp->ll_tv == NULL)
3340 {
3341 cc = *name_end;
3342 *name_end = NUL;
3343
3344 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003345 di = find_var(lp->ll_name, NULL, TRUE);
3346 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003347 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003348 else if ((di->di_flags & DI_FLAGS_FIX)
3349 && di->di_tv.v_type != VAR_DICT
3350 && di->di_tv.v_type != VAR_LIST)
3351 /* For historic reasons this error is not given for a list or dict.
3352 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003353 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003354 else
3355 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003356 if (lock)
3357 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003358 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003359 di->di_flags &= ~DI_FLAGS_LOCK;
3360 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003361 }
3362 *name_end = cc;
3363 }
3364 else if (lp->ll_range)
3365 {
3366 listitem_T *li = lp->ll_li;
3367
3368 /* (un)lock a range of List items. */
3369 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3370 {
3371 item_lock(&li->li_tv, deep, lock);
3372 li = li->li_next;
3373 ++lp->ll_n1;
3374 }
3375 }
3376 else if (lp->ll_list != NULL)
3377 /* (un)lock a List item. */
3378 item_lock(&lp->ll_li->li_tv, deep, lock);
3379 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003380 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003381 item_lock(&lp->ll_di->di_tv, deep, lock);
3382
3383 return ret;
3384}
3385
3386/*
3387 * Lock or unlock an item. "deep" is nr of levels to go.
3388 */
3389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003390item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003391{
3392 static int recurse = 0;
3393 list_T *l;
3394 listitem_T *li;
3395 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003396 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003397 hashitem_T *hi;
3398 int todo;
3399
3400 if (recurse >= DICT_MAXNEST)
3401 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003402 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003403 return;
3404 }
3405 if (deep == 0)
3406 return;
3407 ++recurse;
3408
3409 /* lock/unlock the item itself */
3410 if (lock)
3411 tv->v_lock |= VAR_LOCKED;
3412 else
3413 tv->v_lock &= ~VAR_LOCKED;
3414
3415 switch (tv->v_type)
3416 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003417 case VAR_UNKNOWN:
3418 case VAR_NUMBER:
3419 case VAR_STRING:
3420 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003421 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003422 case VAR_FLOAT:
3423 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003424 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003425 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003426 break;
3427
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003428 case VAR_BLOB:
3429 if ((b = tv->vval.v_blob) != NULL)
3430 {
3431 if (lock)
3432 b->bv_lock |= VAR_LOCKED;
3433 else
3434 b->bv_lock &= ~VAR_LOCKED;
3435 }
3436 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003437 case VAR_LIST:
3438 if ((l = tv->vval.v_list) != NULL)
3439 {
3440 if (lock)
3441 l->lv_lock |= VAR_LOCKED;
3442 else
3443 l->lv_lock &= ~VAR_LOCKED;
3444 if (deep < 0 || deep > 1)
3445 /* recursive: lock/unlock the items the List contains */
3446 for (li = l->lv_first; li != NULL; li = li->li_next)
3447 item_lock(&li->li_tv, deep - 1, lock);
3448 }
3449 break;
3450 case VAR_DICT:
3451 if ((d = tv->vval.v_dict) != NULL)
3452 {
3453 if (lock)
3454 d->dv_lock |= VAR_LOCKED;
3455 else
3456 d->dv_lock &= ~VAR_LOCKED;
3457 if (deep < 0 || deep > 1)
3458 {
3459 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003460 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003461 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3462 {
3463 if (!HASHITEM_EMPTY(hi))
3464 {
3465 --todo;
3466 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3467 }
3468 }
3469 }
3470 }
3471 }
3472 --recurse;
3473}
3474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3476/*
3477 * Delete all "menutrans_" variables.
3478 */
3479 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003480del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481{
Bram Moolenaar33570922005-01-25 22:26:29 +00003482 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003483 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
Bram Moolenaar33570922005-01-25 22:26:29 +00003485 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003486 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003487 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003488 {
3489 if (!HASHITEM_EMPTY(hi))
3490 {
3491 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003492 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3493 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003494 }
3495 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003496 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497}
3498#endif
3499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500/*
3501 * Local string buffer for the next two functions to store a variable name
3502 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3503 * get_user_var_name().
3504 */
3505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506static char_u *varnamebuf = NULL;
3507static int varnamebuflen = 0;
3508
3509/*
3510 * Function to concatenate a prefix and a variable name.
3511 */
3512 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003513cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
3515 int len;
3516
3517 len = (int)STRLEN(name) + 3;
3518 if (len > varnamebuflen)
3519 {
3520 vim_free(varnamebuf);
3521 len += 10; /* some additional space */
3522 varnamebuf = alloc(len);
3523 if (varnamebuf == NULL)
3524 {
3525 varnamebuflen = 0;
3526 return NULL;
3527 }
3528 varnamebuflen = len;
3529 }
3530 *varnamebuf = prefix;
3531 varnamebuf[1] = ':';
3532 STRCPY(varnamebuf + 2, name);
3533 return varnamebuf;
3534}
3535
3536/*
3537 * Function given to ExpandGeneric() to obtain the list of user defined
3538 * (global/buffer/window/built-in) variable names.
3539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003541get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003543 static long_u gdone;
3544 static long_u bdone;
3545 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003546 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003547 static int vidx;
3548 static hashitem_T *hi;
3549 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
3551 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003552 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003553 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003554 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003555 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003556
3557 /* Global variables */
3558 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003560 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003561 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003562 else
3563 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003564 while (HASHITEM_EMPTY(hi))
3565 ++hi;
3566 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3567 return cat_prefix_varname('g', hi->hi_key);
3568 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003570
3571 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003572 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003573 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003575 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003576 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003577 else
3578 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003579 while (HASHITEM_EMPTY(hi))
3580 ++hi;
3581 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003583
3584 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003585 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003586 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003588 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003589 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003590 else
3591 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003592 while (HASHITEM_EMPTY(hi))
3593 ++hi;
3594 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003596
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003597 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003598 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003599 if (tdone < ht->ht_used)
3600 {
3601 if (tdone++ == 0)
3602 hi = ht->ht_array;
3603 else
3604 ++hi;
3605 while (HASHITEM_EMPTY(hi))
3606 ++hi;
3607 return cat_prefix_varname('t', hi->hi_key);
3608 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003609
Bram Moolenaar33570922005-01-25 22:26:29 +00003610 /* v: variables */
3611 if (vidx < VV_LEN)
3612 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
Bram Moolenaard23a8232018-02-10 18:45:26 +01003614 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 varnamebuflen = 0;
3616 return NULL;
3617}
3618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003620 * Return TRUE if "pat" matches "text".
3621 * Does not use 'cpo' and always uses 'magic'.
3622 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02003623 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003624pattern_match(char_u *pat, char_u *text, int ic)
3625{
3626 int matches = FALSE;
3627 char_u *save_cpo;
3628 regmatch_T regmatch;
3629
3630 /* avoid 'l' flag in 'cpoptions' */
3631 save_cpo = p_cpo;
3632 p_cpo = (char_u *)"";
3633 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3634 if (regmatch.regprog != NULL)
3635 {
3636 regmatch.rm_ic = ic;
3637 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3638 vim_regfree(regmatch.regprog);
3639 }
3640 p_cpo = save_cpo;
3641 return matches;
3642}
3643
3644/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003645 * Handle a name followed by "(". Both for just "name(arg)" and for
3646 * "expr->name(arg)".
3647 * Returns OK or FAIL.
3648 */
3649 static int
3650eval_func(
3651 char_u **arg, // points to "(", will be advanced
3652 char_u *name,
3653 int name_len,
3654 typval_T *rettv,
3655 int evaluate,
3656 typval_T *basetv) // "expr" for "expr->name(arg)"
3657{
3658 char_u *s = name;
3659 int len = name_len;
3660 partial_T *partial;
3661 int ret = OK;
3662
3663 if (!evaluate)
3664 check_vars(s, len);
3665
3666 /* If "s" is the name of a variable of type VAR_FUNC
3667 * use its contents. */
3668 s = deref_func_name(s, &len, &partial, !evaluate);
3669
3670 /* Need to make a copy, in case evaluating the arguments makes
3671 * the name invalid. */
3672 s = vim_strsave(s);
3673 if (s == NULL)
3674 ret = FAIL;
3675 else
3676 {
3677 funcexe_T funcexe;
3678
3679 // Invoke the function.
3680 vim_memset(&funcexe, 0, sizeof(funcexe));
3681 funcexe.firstline = curwin->w_cursor.lnum;
3682 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003683 funcexe.evaluate = evaluate;
3684 funcexe.partial = partial;
3685 funcexe.basetv = basetv;
3686 ret = get_func_tv(s, len, rettv, arg, &funcexe);
3687 }
3688 vim_free(s);
3689
3690 /* If evaluate is FALSE rettv->v_type was not set in
3691 * get_func_tv, but it's needed in handle_subscript() to parse
3692 * what follows. So set it here. */
3693 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
3694 {
3695 rettv->vval.v_string = NULL;
3696 rettv->v_type = VAR_FUNC;
3697 }
3698
3699 /* Stop the expression evaluation when immediately
3700 * aborting on error, or when an interrupt occurred or
3701 * an exception was thrown but not caught. */
3702 if (evaluate && aborting())
3703 {
3704 if (ret == OK)
3705 clear_tv(rettv);
3706 ret = FAIL;
3707 }
3708 return ret;
3709}
3710
3711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003713 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3715 */
3716
3717/*
3718 * Handle zero level expression.
3719 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003720 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003721 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 * Return OK or FAIL.
3723 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003724 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003725eval0(
3726 char_u *arg,
3727 typval_T *rettv,
3728 char_u **nextcmd,
3729 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
3731 int ret;
3732 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003733 int did_emsg_before = did_emsg;
3734 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
3736 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003737 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 if (ret == FAIL || !ends_excmd(*p))
3739 {
3740 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003741 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 /*
3743 * Report the invalid expression unless the expression evaluation has
3744 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003745 * exception, or we already gave a more specific error.
3746 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003748 if (!aborting() && did_emsg == did_emsg_before
3749 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003750 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 ret = FAIL;
3752 }
3753 if (nextcmd != NULL)
3754 *nextcmd = check_nextcmd(p);
3755
3756 return ret;
3757}
3758
3759/*
3760 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003761 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 *
3763 * "arg" must point to the first non-white of the expression.
3764 * "arg" is advanced to the next non-white after the recognized expression.
3765 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003766 * Note: "rettv.v_lock" is not set.
3767 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 * Return OK or FAIL.
3769 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003771eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772{
3773 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003774 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
3776 /*
3777 * Get the first variable.
3778 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003779 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 return FAIL;
3781
3782 if ((*arg)[0] == '?')
3783 {
3784 result = FALSE;
3785 if (evaluate)
3786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003787 int error = FALSE;
3788
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003789 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003791 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003792 if (error)
3793 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795
3796 /*
3797 * Get the second variable.
3798 */
3799 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003800 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 return FAIL;
3802
3803 /*
3804 * Check for the ":".
3805 */
3806 if ((*arg)[0] != ':')
3807 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003808 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003810 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 return FAIL;
3812 }
3813
3814 /*
3815 * Get the third variable.
3816 */
3817 *arg = skipwhite(*arg + 1);
3818 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3819 {
3820 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003821 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 return FAIL;
3823 }
3824 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003825 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 }
3827
3828 return OK;
3829}
3830
3831/*
3832 * Handle first level expression:
3833 * expr2 || expr2 || expr2 logical OR
3834 *
3835 * "arg" must point to the first non-white of the expression.
3836 * "arg" is advanced to the next non-white after the recognized expression.
3837 *
3838 * Return OK or FAIL.
3839 */
3840 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003841eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842{
Bram Moolenaar33570922005-01-25 22:26:29 +00003843 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 long result;
3845 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003846 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
3848 /*
3849 * Get the first variable.
3850 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003851 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 return FAIL;
3853
3854 /*
3855 * Repeat until there is no following "||".
3856 */
3857 first = TRUE;
3858 result = FALSE;
3859 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3860 {
3861 if (evaluate && first)
3862 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003863 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003865 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003866 if (error)
3867 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 first = FALSE;
3869 }
3870
3871 /*
3872 * Get the second variable.
3873 */
3874 *arg = skipwhite(*arg + 2);
3875 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3876 return FAIL;
3877
3878 /*
3879 * Compute the result.
3880 */
3881 if (evaluate && !result)
3882 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003883 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003885 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003886 if (error)
3887 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 }
3889 if (evaluate)
3890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003891 rettv->v_type = VAR_NUMBER;
3892 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
3894 }
3895
3896 return OK;
3897}
3898
3899/*
3900 * Handle second level expression:
3901 * expr3 && expr3 && expr3 logical AND
3902 *
3903 * "arg" must point to the first non-white of the expression.
3904 * "arg" is advanced to the next non-white after the recognized expression.
3905 *
3906 * Return OK or FAIL.
3907 */
3908 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003909eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910{
Bram Moolenaar33570922005-01-25 22:26:29 +00003911 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 long result;
3913 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003914 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915
3916 /*
3917 * Get the first variable.
3918 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003919 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 return FAIL;
3921
3922 /*
3923 * Repeat until there is no following "&&".
3924 */
3925 first = TRUE;
3926 result = TRUE;
3927 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3928 {
3929 if (evaluate && first)
3930 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003931 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003933 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003934 if (error)
3935 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 first = FALSE;
3937 }
3938
3939 /*
3940 * Get the second variable.
3941 */
3942 *arg = skipwhite(*arg + 2);
3943 if (eval4(arg, &var2, evaluate && result) == FAIL)
3944 return FAIL;
3945
3946 /*
3947 * Compute the result.
3948 */
3949 if (evaluate && result)
3950 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003951 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003954 if (error)
3955 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 }
3957 if (evaluate)
3958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003959 rettv->v_type = VAR_NUMBER;
3960 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
3962 }
3963
3964 return OK;
3965}
3966
3967/*
3968 * Handle third level expression:
3969 * var1 == var2
3970 * var1 =~ var2
3971 * var1 != var2
3972 * var1 !~ var2
3973 * var1 > var2
3974 * var1 >= var2
3975 * var1 < var2
3976 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003977 * var1 is var2
3978 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 *
3980 * "arg" must point to the first non-white of the expression.
3981 * "arg" is advanced to the next non-white after the recognized expression.
3982 *
3983 * Return OK or FAIL.
3984 */
3985 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003986eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987{
Bram Moolenaar33570922005-01-25 22:26:29 +00003988 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 char_u *p;
3990 int i;
3991 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003992 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
3996 /*
3997 * Get the first variable.
3998 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003999 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 return FAIL;
4001
4002 p = *arg;
4003 switch (p[0])
4004 {
4005 case '=': if (p[1] == '=')
4006 type = TYPE_EQUAL;
4007 else if (p[1] == '~')
4008 type = TYPE_MATCH;
4009 break;
4010 case '!': if (p[1] == '=')
4011 type = TYPE_NEQUAL;
4012 else if (p[1] == '~')
4013 type = TYPE_NOMATCH;
4014 break;
4015 case '>': if (p[1] != '=')
4016 {
4017 type = TYPE_GREATER;
4018 len = 1;
4019 }
4020 else
4021 type = TYPE_GEQUAL;
4022 break;
4023 case '<': if (p[1] != '=')
4024 {
4025 type = TYPE_SMALLER;
4026 len = 1;
4027 }
4028 else
4029 type = TYPE_SEQUAL;
4030 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004031 case 'i': if (p[1] == 's')
4032 {
4033 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4034 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004035 i = p[len];
4036 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004037 {
4038 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4039 type_is = TRUE;
4040 }
4041 }
4042 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
4044
4045 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004046 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 */
4048 if (type != TYPE_UNKNOWN)
4049 {
4050 /* extra question mark appended: ignore case */
4051 if (p[len] == '?')
4052 {
4053 ic = TRUE;
4054 ++len;
4055 }
4056 /* extra '#' appended: match case */
4057 else if (p[len] == '#')
4058 {
4059 ic = FALSE;
4060 ++len;
4061 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004062 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 else
4064 ic = p_ic;
4065
4066 /*
4067 * Get the second variable.
4068 */
4069 *arg = skipwhite(p + len);
4070 if (eval5(arg, &var2, evaluate) == FAIL)
4071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004072 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 return FAIL;
4074 }
Bram Moolenaar31988702018-02-13 12:57:42 +01004075 if (evaluate)
4076 {
4077 int ret = typval_compare(rettv, &var2, type, type_is, ic);
4078
4079 clear_tv(&var2);
4080 return ret;
4081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
4083
4084 return OK;
4085}
4086
4087/*
4088 * Handle fourth level expression:
4089 * + number addition
4090 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004091 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004092 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 *
4094 * "arg" must point to the first non-white of the expression.
4095 * "arg" is advanced to the next non-white after the recognized expression.
4096 *
4097 * Return OK or FAIL.
4098 */
4099 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004100eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101{
Bram Moolenaar33570922005-01-25 22:26:29 +00004102 typval_T var2;
4103 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004105 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004106#ifdef FEAT_FLOAT
4107 float_T f1 = 0, f2 = 0;
4108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 char_u *s1, *s2;
4110 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4111 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004112 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
4114 /*
4115 * Get the first variable.
4116 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004117 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 return FAIL;
4119
4120 /*
4121 * Repeat computing, until no '+', '-' or '.' is following.
4122 */
4123 for (;;)
4124 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004125 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004127 concat = op == '.'
4128 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
4129 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 break;
4131
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004132 if ((op != '+' || (rettv->v_type != VAR_LIST
4133 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004134#ifdef FEAT_FLOAT
4135 && (op == '.' || rettv->v_type != VAR_FLOAT)
4136#endif
4137 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004138 {
4139 /* For "list + ...", an illegal use of the first operand as
4140 * a number cannot be determined before evaluating the 2nd
4141 * operand: if this is also a list, all is ok.
4142 * For "something . ...", "something - ..." or "non-list + ...",
4143 * we know that the first operand needs to be a string or number
4144 * without evaluating the 2nd operand. So check before to avoid
4145 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004146 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004147 {
4148 clear_tv(rettv);
4149 return FAIL;
4150 }
4151 }
4152
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 /*
4154 * Get the second variable.
4155 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02004156 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
4157 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004159 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004161 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 return FAIL;
4163 }
4164
4165 if (evaluate)
4166 {
4167 /*
4168 * Compute the result.
4169 */
4170 if (op == '.')
4171 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004172 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
4173 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004174 if (s2 == NULL) /* type error ? */
4175 {
4176 clear_tv(rettv);
4177 clear_tv(&var2);
4178 return FAIL;
4179 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004180 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
4182 rettv->v_type = VAR_STRING;
4183 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004185 else if (op == '+' && rettv->v_type == VAR_BLOB
4186 && var2.v_type == VAR_BLOB)
4187 {
4188 blob_T *b1 = rettv->vval.v_blob;
4189 blob_T *b2 = var2.vval.v_blob;
4190 blob_T *b = blob_alloc();
4191 int i;
4192
4193 if (b != NULL)
4194 {
4195 for (i = 0; i < blob_len(b1); i++)
4196 ga_append(&b->bv_ga, blob_get(b1, i));
4197 for (i = 0; i < blob_len(b2); i++)
4198 ga_append(&b->bv_ga, blob_get(b2, i));
4199
4200 clear_tv(rettv);
4201 rettv_blob_set(rettv, b);
4202 }
4203 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004204 else if (op == '+' && rettv->v_type == VAR_LIST
4205 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004206 {
4207 /* concatenate Lists */
4208 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4209 &var3) == FAIL)
4210 {
4211 clear_tv(rettv);
4212 clear_tv(&var2);
4213 return FAIL;
4214 }
4215 clear_tv(rettv);
4216 *rettv = var3;
4217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 else
4219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004220 int error = FALSE;
4221
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004222#ifdef FEAT_FLOAT
4223 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004224 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004225 f1 = rettv->vval.v_float;
4226 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004227 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004228 else
4229#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004230 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004231 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004232 if (error)
4233 {
4234 /* This can only happen for "list + non-list". For
4235 * "non-list + ..." or "something - ...", we returned
4236 * before evaluating the 2nd operand. */
4237 clear_tv(rettv);
4238 return FAIL;
4239 }
4240#ifdef FEAT_FLOAT
4241 if (var2.v_type == VAR_FLOAT)
4242 f1 = n1;
4243#endif
4244 }
4245#ifdef FEAT_FLOAT
4246 if (var2.v_type == VAR_FLOAT)
4247 {
4248 f2 = var2.vval.v_float;
4249 n2 = 0;
4250 }
4251 else
4252#endif
4253 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004254 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004255 if (error)
4256 {
4257 clear_tv(rettv);
4258 clear_tv(&var2);
4259 return FAIL;
4260 }
4261#ifdef FEAT_FLOAT
4262 if (rettv->v_type == VAR_FLOAT)
4263 f2 = n2;
4264#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004267
4268#ifdef FEAT_FLOAT
4269 /* If there is a float on either side the result is a float. */
4270 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4271 {
4272 if (op == '+')
4273 f1 = f1 + f2;
4274 else
4275 f1 = f1 - f2;
4276 rettv->v_type = VAR_FLOAT;
4277 rettv->vval.v_float = f1;
4278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004280#endif
4281 {
4282 if (op == '+')
4283 n1 = n1 + n2;
4284 else
4285 n1 = n1 - n2;
4286 rettv->v_type = VAR_NUMBER;
4287 rettv->vval.v_number = n1;
4288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 }
4293 return OK;
4294}
4295
4296/*
4297 * Handle fifth level expression:
4298 * * number multiplication
4299 * / number division
4300 * % number modulo
4301 *
4302 * "arg" must point to the first non-white of the expression.
4303 * "arg" is advanced to the next non-white after the recognized expression.
4304 *
4305 * Return OK or FAIL.
4306 */
4307 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004308eval6(
4309 char_u **arg,
4310 typval_T *rettv,
4311 int evaluate,
4312 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313{
Bram Moolenaar33570922005-01-25 22:26:29 +00004314 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004316 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004317#ifdef FEAT_FLOAT
4318 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004319 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004320#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 /*
4324 * Get the first variable.
4325 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004326 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FAIL;
4328
4329 /*
4330 * Repeat computing, until no '*', '/' or '%' is following.
4331 */
4332 for (;;)
4333 {
4334 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02004335 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 break;
4337
4338 if (evaluate)
4339 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004340#ifdef FEAT_FLOAT
4341 if (rettv->v_type == VAR_FLOAT)
4342 {
4343 f1 = rettv->vval.v_float;
4344 use_float = TRUE;
4345 n1 = 0;
4346 }
4347 else
4348#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004349 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 if (error)
4352 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 else
4355 n1 = 0;
4356
4357 /*
4358 * Get the second variable.
4359 */
4360 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004361 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 return FAIL;
4363
4364 if (evaluate)
4365 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004366#ifdef FEAT_FLOAT
4367 if (var2.v_type == VAR_FLOAT)
4368 {
4369 if (!use_float)
4370 {
4371 f1 = n1;
4372 use_float = TRUE;
4373 }
4374 f2 = var2.vval.v_float;
4375 n2 = 0;
4376 }
4377 else
4378#endif
4379 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004380 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004381 clear_tv(&var2);
4382 if (error)
4383 return FAIL;
4384#ifdef FEAT_FLOAT
4385 if (use_float)
4386 f2 = n2;
4387#endif
4388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390 /*
4391 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004392 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004394#ifdef FEAT_FLOAT
4395 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004397 if (op == '*')
4398 f1 = f1 * f2;
4399 else if (op == '/')
4400 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004401# ifdef VMS
4402 /* VMS crashes on divide by zero, work around it */
4403 if (f2 == 0.0)
4404 {
4405 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004406 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004407 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004408 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004409 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004410 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004411 }
4412 else
4413 f1 = f1 / f2;
4414# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004415 /* We rely on the floating point library to handle divide
4416 * by zero to result in "inf" and not a crash. */
4417 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004418# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004421 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004422 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004423 return FAIL;
4424 }
4425 rettv->v_type = VAR_FLOAT;
4426 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 }
4428 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004431 if (op == '*')
4432 n1 = n1 * n2;
4433 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004434 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004436 n1 = num_modulus(n1, n2);
4437
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004438 rettv->v_type = VAR_NUMBER;
4439 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
4442 }
4443
4444 return OK;
4445}
4446
4447/*
4448 * Handle sixth level expression:
4449 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004450 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004451 * "string" string constant
4452 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 * &option-name option value
4454 * @r register contents
4455 * identifier variable value
4456 * function() function call
4457 * $VAR environment variable
4458 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004459 * [expr, expr] List
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004460 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004461 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 *
4463 * Also handle:
4464 * ! in front logical NOT
4465 * - in front unary minus
4466 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004467 * trailing [] subscript in String or List
4468 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02004469 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 *
4471 * "arg" must point to the first non-white of the expression.
4472 * "arg" is advanced to the next non-white after the recognized expression.
4473 *
4474 * Return OK or FAIL.
4475 */
4476 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004477eval7(
4478 char_u **arg,
4479 typval_T *rettv,
4480 int evaluate,
4481 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004483 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 int len;
4485 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 char_u *start_leader, *end_leader;
4487 int ret = OK;
4488 char_u *alias;
4489
4490 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004491 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004492 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004494 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
4496 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004497 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 */
4499 start_leader = *arg;
4500 while (**arg == '!' || **arg == '-' || **arg == '+')
4501 *arg = skipwhite(*arg + 1);
4502 end_leader = *arg;
4503
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004504 if (**arg == '.' && (!isdigit(*(*arg + 1))
4505#ifdef FEAT_FLOAT
4506 || current_sctx.sc_version < 2
4507#endif
4508 ))
4509 {
4510 semsg(_(e_invexpr2), *arg);
4511 ++*arg;
4512 return FAIL;
4513 }
4514
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 switch (**arg)
4516 {
4517 /*
4518 * Number constant.
4519 */
4520 case '0':
4521 case '1':
4522 case '2':
4523 case '3':
4524 case '4':
4525 case '5':
4526 case '6':
4527 case '7':
4528 case '8':
4529 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004530 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004531 {
4532#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004533 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004534 int get_float = FALSE;
4535
4536 /* We accept a float when the format matches
4537 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004538 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004539 * With script version 2 and later the leading digit can be
4540 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004541 * Don't look for a float after the "." operator, so that
4542 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004543 if (**arg == '.')
4544 p = *arg;
4545 else
4546 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004547 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004549 get_float = TRUE;
4550 p = skipdigits(p + 2);
4551 if (*p == 'e' || *p == 'E')
4552 {
4553 ++p;
4554 if (*p == '-' || *p == '+')
4555 ++p;
4556 if (!vim_isdigit(*p))
4557 get_float = FALSE;
4558 else
4559 p = skipdigits(p + 1);
4560 }
4561 if (ASCII_ISALPHA(*p) || *p == '.')
4562 get_float = FALSE;
4563 }
4564 if (get_float)
4565 {
4566 float_T f;
4567
4568 *arg += string2float(*arg, &f);
4569 if (evaluate)
4570 {
4571 rettv->v_type = VAR_FLOAT;
4572 rettv->vval.v_float = f;
4573 }
4574 }
4575 else
4576#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004577 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004578 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004579 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004580 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004581
4582 // Blob constant: 0z0123456789abcdef
4583 if (evaluate)
4584 blob = blob_alloc();
4585 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4586 {
4587 if (!vim_isxdigit(bp[1]))
4588 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004589 if (blob != NULL)
4590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004591 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004592 ga_clear(&blob->bv_ga);
4593 VIM_CLEAR(blob);
4594 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004595 ret = FAIL;
4596 break;
4597 }
4598 if (blob != NULL)
4599 ga_append(&blob->bv_ga,
4600 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004601 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4602 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004603 }
4604 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004605 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004606 *arg = bp;
4607 }
4608 else
4609 {
4610 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004611 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
4612 if (len == 0)
4613 {
4614 semsg(_(e_invexpr2), *arg);
4615 ret = FAIL;
4616 break;
4617 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004618 *arg += len;
4619 if (evaluate)
4620 {
4621 rettv->v_type = VAR_NUMBER;
4622 rettv->vval.v_number = n;
4623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 }
4625 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
4628 /*
4629 * String constant: "string".
4630 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 break;
4633
4634 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004635 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004638 break;
4639
4640 /*
4641 * List: [expr, expr]
4642 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004643 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 break;
4645
4646 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004647 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004648 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004649 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004650 {
4651 ++*arg;
4652 ret = dict_get_tv(arg, rettv, evaluate, TRUE);
4653 }
4654 else
4655 ret = NOTDONE;
4656 break;
4657
4658 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004659 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004660 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004661 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004662 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4663 if (ret == NOTDONE)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004664 ret = dict_get_tv(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004665 break;
4666
4667 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004668 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004670 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 break;
4672
4673 /*
4674 * Environment variable: $VAR.
4675 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004676 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 break;
4678
4679 /*
4680 * Register contents: @r.
4681 */
4682 case '@': ++*arg;
4683 if (evaluate)
4684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004685 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004686 rettv->vval.v_string = get_reg_contents(**arg,
4687 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 }
4689 if (**arg != NUL)
4690 ++*arg;
4691 break;
4692
4693 /*
4694 * nested expression: (expression).
4695 */
4696 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004697 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 if (**arg == ')')
4699 ++*arg;
4700 else if (ret == OK)
4701 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004702 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004703 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 ret = FAIL;
4705 }
4706 break;
4707
Bram Moolenaar8c711452005-01-14 21:53:12 +00004708 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 break;
4710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004711
4712 if (ret == NOTDONE)
4713 {
4714 /*
4715 * Must be a variable or function name.
4716 * Can also be a curly-braces kind of name: {expr}.
4717 */
4718 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004719 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004720 if (alias != NULL)
4721 s = alias;
4722
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004723 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004724 ret = FAIL;
4725 else
4726 {
4727 if (**arg == '(') /* recursive! */
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004728 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004729 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004730 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004731 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004732 {
4733 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004734 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004735 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004736 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004737 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004738 }
4739
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 *arg = skipwhite(*arg);
4741
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004742 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004743 * expr(expr), expr->name(expr) */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004744 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004745 ret = handle_subscript(arg, rettv, evaluate, TRUE,
4746 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
4748 /*
4749 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4750 */
4751 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004752 ret = eval7_leader(rettv, start_leader, &end_leader);
4753 return ret;
4754}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004756/*
4757 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
4758 * Adjusts "end_leaderp" until it is at "start_leader".
4759 */
4760 static int
4761eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
4762{
4763 char_u *end_leader = *end_leaderp;
4764 int ret = OK;
4765 int error = FALSE;
4766 varnumber_T val = 0;
4767#ifdef FEAT_FLOAT
4768 float_T f = 0.0;
4769
4770 if (rettv->v_type == VAR_FLOAT)
4771 f = rettv->vval.v_float;
4772 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004773#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004774 val = tv_get_number_chk(rettv, &error);
4775 if (error)
4776 {
4777 clear_tv(rettv);
4778 ret = FAIL;
4779 }
4780 else
4781 {
4782 while (end_leader > start_leader)
4783 {
4784 --end_leader;
4785 if (*end_leader == '!')
4786 {
4787#ifdef FEAT_FLOAT
4788 if (rettv->v_type == VAR_FLOAT)
4789 f = !f;
4790 else
4791#endif
4792 val = !val;
4793 }
4794 else if (*end_leader == '-')
4795 {
4796#ifdef FEAT_FLOAT
4797 if (rettv->v_type == VAR_FLOAT)
4798 f = -f;
4799 else
4800#endif
4801 val = -val;
4802 }
4803 }
4804#ifdef FEAT_FLOAT
4805 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004808 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004811#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004813 clear_tv(rettv);
4814 rettv->v_type = VAR_NUMBER;
4815 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004818 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 return ret;
4820}
4821
4822/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004823 * Call the function referred to in "rettv".
4824 */
4825 static int
4826call_func_rettv(
4827 char_u **arg,
4828 typval_T *rettv,
4829 int evaluate,
4830 dict_T *selfdict,
4831 typval_T *basetv)
4832{
4833 partial_T *pt = NULL;
4834 funcexe_T funcexe;
4835 typval_T functv;
4836 char_u *s;
4837 int ret;
4838
4839 // need to copy the funcref so that we can clear rettv
4840 if (evaluate)
4841 {
4842 functv = *rettv;
4843 rettv->v_type = VAR_UNKNOWN;
4844
4845 /* Invoke the function. Recursive! */
4846 if (functv.v_type == VAR_PARTIAL)
4847 {
4848 pt = functv.vval.v_partial;
4849 s = partial_name(pt);
4850 }
4851 else
4852 s = functv.vval.v_string;
4853 }
4854 else
4855 s = (char_u *)"";
4856
4857 vim_memset(&funcexe, 0, sizeof(funcexe));
4858 funcexe.firstline = curwin->w_cursor.lnum;
4859 funcexe.lastline = curwin->w_cursor.lnum;
4860 funcexe.evaluate = evaluate;
4861 funcexe.partial = pt;
4862 funcexe.selfdict = selfdict;
4863 funcexe.basetv = basetv;
4864 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
4865
4866 /* Clear the funcref afterwards, so that deleting it while
4867 * evaluating the arguments is possible (see test55). */
4868 if (evaluate)
4869 clear_tv(&functv);
4870
4871 return ret;
4872}
4873
4874/*
4875 * Evaluate "->method()".
4876 * "*arg" points to the '-'.
4877 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4878 */
4879 static int
4880eval_lambda(
4881 char_u **arg,
4882 typval_T *rettv,
4883 int evaluate,
4884 int verbose) /* give error messages */
4885{
4886 typval_T base = *rettv;
4887 int ret;
4888
4889 // Skip over the ->.
4890 *arg += 2;
4891 rettv->v_type = VAR_UNKNOWN;
4892
4893 ret = get_lambda_tv(arg, rettv, evaluate);
4894 if (ret == NOTDONE)
4895 return FAIL;
4896 else if (**arg != '(')
4897 {
4898 if (verbose)
4899 {
4900 if (*skipwhite(*arg) == '(')
4901 semsg(_(e_nowhitespace));
4902 else
4903 semsg(_(e_missingparen), "lambda");
4904 }
4905 clear_tv(rettv);
4906 return FAIL;
4907 }
4908 return call_func_rettv(arg, rettv, evaluate, NULL, &base);
4909}
4910
4911/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004912 * Evaluate "->method()".
4913 * "*arg" points to the '-'.
4914 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4915 */
4916 static int
4917eval_method(
4918 char_u **arg,
4919 typval_T *rettv,
4920 int evaluate,
4921 int verbose) /* give error messages */
4922{
4923 char_u *name;
4924 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004925 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004926 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004927 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004928
4929 // Skip over the ->.
4930 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004931 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004932
Bram Moolenaarac92e252019-08-03 21:58:38 +02004933 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004934 len = get_name_len(arg, &alias, evaluate, TRUE);
4935 if (alias != NULL)
4936 name = alias;
4937
4938 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004939 {
4940 if (verbose)
4941 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004942 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004943 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004944 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004945 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004946 if (**arg != '(')
4947 {
4948 if (verbose)
4949 semsg(_(e_missingparen), name);
4950 ret = FAIL;
4951 }
Bram Moolenaar51841322019-08-08 21:10:01 +02004952 else if (VIM_ISWHITE((*arg)[-1]))
4953 {
4954 if (verbose)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004955 semsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02004956 ret = FAIL;
4957 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004958 else
4959 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004960 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004961
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004962 // Clear the funcref afterwards, so that deleting it while
4963 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004964 if (evaluate)
4965 clear_tv(&base);
4966
Bram Moolenaarac92e252019-08-03 21:58:38 +02004967 return ret;
4968}
4969
4970/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004971 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4972 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004973 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4974 */
4975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004976eval_index(
4977 char_u **arg,
4978 typval_T *rettv,
4979 int evaluate,
4980 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004981{
4982 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004983 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004984 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004986 long len = -1;
4987 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004988 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004990
Bram Moolenaara03f2332016-02-06 18:09:59 +01004991 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004992 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004993 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004994 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004995 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004996 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004997 return FAIL;
4998 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004999#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005000 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005001 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005002 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005003#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005004 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005005 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005006 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005007 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005008 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005009 return FAIL;
5010 case VAR_UNKNOWN:
5011 if (evaluate)
5012 return FAIL;
5013 /* FALLTHROUGH */
5014
5015 case VAR_STRING:
5016 case VAR_NUMBER:
5017 case VAR_LIST:
5018 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005019 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005020 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005021 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005022
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005023 init_tv(&var1);
5024 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005026 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 /*
5028 * dict.name
5029 */
5030 key = *arg + 1;
5031 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5032 ;
5033 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005034 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005036 }
5037 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005038 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 /*
5040 * something[idx]
5041 *
5042 * Get the (first) variable from inside the [].
5043 */
5044 *arg = skipwhite(*arg + 1);
5045 if (**arg == ':')
5046 empty1 = TRUE;
5047 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5048 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005049 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005050 {
5051 /* not a number or string */
5052 clear_tv(&var1);
5053 return FAIL;
5054 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055
5056 /*
5057 * Get the second variable from inside the [:].
5058 */
5059 if (**arg == ':')
5060 {
5061 range = TRUE;
5062 *arg = skipwhite(*arg + 1);
5063 if (**arg == ']')
5064 empty2 = TRUE;
5065 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005067 if (!empty1)
5068 clear_tv(&var1);
5069 return FAIL;
5070 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005071 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005072 {
5073 /* not a number or string */
5074 if (!empty1)
5075 clear_tv(&var1);
5076 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 return FAIL;
5078 }
5079 }
5080
5081 /* Check for the ']'. */
5082 if (**arg != ']')
5083 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005084 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005085 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 clear_tv(&var1);
5087 if (range)
5088 clear_tv(&var2);
5089 return FAIL;
5090 }
5091 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 }
5093
5094 if (evaluate)
5095 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 n1 = 0;
5097 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005098 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005099 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005101 }
5102 if (range)
5103 {
5104 if (empty2)
5105 n2 = -1;
5106 else
5107 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005108 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005109 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005110 }
5111 }
5112
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005113 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005115 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005116 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005117 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005118 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005119 case VAR_SPECIAL:
5120 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005121 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005122 break; /* not evaluating, skipping over subscript */
5123
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005124 case VAR_NUMBER:
5125 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005126 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005127 len = (long)STRLEN(s);
5128 if (range)
5129 {
5130 /* The resulting variable is a substring. If the indexes
5131 * are out of range the result is empty. */
5132 if (n1 < 0)
5133 {
5134 n1 = len + n1;
5135 if (n1 < 0)
5136 n1 = 0;
5137 }
5138 if (n2 < 0)
5139 n2 = len + n2;
5140 else if (n2 >= len)
5141 n2 = len;
5142 if (n1 >= len || n2 < 0 || n1 > n2)
5143 s = NULL;
5144 else
5145 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5146 }
5147 else
5148 {
5149 /* The resulting variable is a string of a single
5150 * character. If the index is too big or negative the
5151 * result is empty. */
5152 if (n1 >= len || n1 < 0)
5153 s = NULL;
5154 else
5155 s = vim_strnsave(s + n1, 1);
5156 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 clear_tv(rettv);
5158 rettv->v_type = VAR_STRING;
5159 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005160 break;
5161
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005162 case VAR_BLOB:
5163 len = blob_len(rettv->vval.v_blob);
5164 if (range)
5165 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005166 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005167 // are out of range the result is empty.
5168 if (n1 < 0)
5169 {
5170 n1 = len + n1;
5171 if (n1 < 0)
5172 n1 = 0;
5173 }
5174 if (n2 < 0)
5175 n2 = len + n2;
5176 else if (n2 >= len)
5177 n2 = len - 1;
5178 if (n1 >= len || n2 < 0 || n1 > n2)
5179 {
5180 clear_tv(rettv);
5181 rettv->v_type = VAR_BLOB;
5182 rettv->vval.v_blob = NULL;
5183 }
5184 else
5185 {
5186 blob_T *blob = blob_alloc();
5187
5188 if (blob != NULL)
5189 {
5190 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
5191 {
5192 blob_free(blob);
5193 return FAIL;
5194 }
5195 blob->bv_ga.ga_len = n2 - n1 + 1;
5196 for (i = n1; i <= n2; i++)
5197 blob_set(blob, i - n1,
5198 blob_get(rettv->vval.v_blob, i));
5199
5200 clear_tv(rettv);
5201 rettv_blob_set(rettv, blob);
5202 }
5203 }
5204 }
5205 else
5206 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005207 // The resulting variable is a byte value.
5208 // If the index is too big or negative that is an error.
5209 if (n1 < 0)
5210 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005211 if (n1 < len && n1 >= 0)
5212 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01005213 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005214
5215 clear_tv(rettv);
5216 rettv->v_type = VAR_NUMBER;
5217 rettv->vval.v_number = v;
5218 }
5219 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005220 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005221 }
5222 break;
5223
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005225 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005226 if (n1 < 0)
5227 n1 = len + n1;
5228 if (!empty1 && (n1 < 0 || n1 >= len))
5229 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005230 /* For a range we allow invalid values and return an empty
5231 * list. A list index out of range is an error. */
5232 if (!range)
5233 {
5234 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005235 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005236 return FAIL;
5237 }
5238 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 }
5240 if (range)
5241 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005242 list_T *l;
5243 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244
5245 if (n2 < 0)
5246 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005247 else if (n2 >= len)
5248 n2 = len - 1;
5249 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005250 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 l = list_alloc();
5252 if (l == NULL)
5253 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005254 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 n1 <= n2; ++n1)
5256 {
5257 if (list_append_tv(l, &item->li_tv) == FAIL)
5258 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005259 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 return FAIL;
5261 }
5262 item = item->li_next;
5263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005264 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005265 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 }
5267 else
5268 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005269 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005270 clear_tv(rettv);
5271 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 }
5273 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274
5275 case VAR_DICT:
5276 if (range)
5277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005278 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005279 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 if (len == -1)
5281 clear_tv(&var1);
5282 return FAIL;
5283 }
5284 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005285 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286
5287 if (len == -1)
5288 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005289 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005290 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 clear_tv(&var1);
5293 return FAIL;
5294 }
5295 }
5296
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005297 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005299 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005300 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 if (len == -1)
5302 clear_tv(&var1);
5303 if (item == NULL)
5304 return FAIL;
5305
5306 copy_tv(&item->di_tv, &var1);
5307 clear_tv(rettv);
5308 *rettv = var1;
5309 }
5310 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311 }
5312 }
5313
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 return OK;
5315}
5316
5317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 * Get an option value.
5319 * "arg" points to the '&' or '+' before the option name.
5320 * "arg" is advanced to character after the option name.
5321 * Return OK or FAIL.
5322 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005323 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005324get_option_tv(
5325 char_u **arg,
5326 typval_T *rettv, /* when NULL, only check if option exists */
5327 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328{
5329 char_u *option_end;
5330 long numval;
5331 char_u *stringval;
5332 int opt_type;
5333 int c;
5334 int working = (**arg == '+'); /* has("+option") */
5335 int ret = OK;
5336 int opt_flags;
5337
5338 /*
5339 * Isolate the option name and find its value.
5340 */
5341 option_end = find_option_end(arg, &opt_flags);
5342 if (option_end == NULL)
5343 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005344 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005345 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 return FAIL;
5347 }
5348
5349 if (!evaluate)
5350 {
5351 *arg = option_end;
5352 return OK;
5353 }
5354
5355 c = *option_end;
5356 *option_end = NUL;
5357 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005358 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359
5360 if (opt_type == -3) /* invalid name */
5361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005362 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005363 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 ret = FAIL;
5365 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005366 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 {
5368 if (opt_type == -2) /* hidden string option */
5369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 rettv->v_type = VAR_STRING;
5371 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
5373 else if (opt_type == -1) /* hidden number option */
5374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005375 rettv->v_type = VAR_NUMBER;
5376 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 }
5378 else if (opt_type == 1) /* number option */
5379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005380 rettv->v_type = VAR_NUMBER;
5381 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 }
5383 else /* string option */
5384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005385 rettv->v_type = VAR_STRING;
5386 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388 }
5389 else if (working && (opt_type == -2 || opt_type == -1))
5390 ret = FAIL;
5391
5392 *option_end = c; /* put back for error messages */
5393 *arg = option_end;
5394
5395 return ret;
5396}
5397
5398/*
5399 * Allocate a variable for a string constant.
5400 * Return OK or FAIL.
5401 */
5402 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005403get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
5405 char_u *p;
5406 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 int extra = 0;
5408
5409 /*
5410 * Find the end of the string, skipping backslashed characters.
5411 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005412 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 {
5414 if (*p == '\\' && p[1] != NUL)
5415 {
5416 ++p;
5417 /* A "\<x>" form occupies at least 4 characters, and produces up
5418 * to 6 characters: reserve space for 2 extra */
5419 if (*p == '<')
5420 extra += 2;
5421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 }
5423
5424 if (*p != '"')
5425 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005426 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 return FAIL;
5428 }
5429
5430 /* If only parsing, set *arg and return here */
5431 if (!evaluate)
5432 {
5433 *arg = p + 1;
5434 return OK;
5435 }
5436
5437 /*
5438 * Copy the string into allocated memory, handling backslashed
5439 * characters.
5440 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005441 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 if (name == NULL)
5443 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 rettv->v_type = VAR_STRING;
5445 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
5449 if (*p == '\\')
5450 {
5451 switch (*++p)
5452 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 case 'b': *name++ = BS; ++p; break;
5454 case 'e': *name++ = ESC; ++p; break;
5455 case 'f': *name++ = FF; ++p; break;
5456 case 'n': *name++ = NL; ++p; break;
5457 case 'r': *name++ = CAR; ++p; break;
5458 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459
5460 case 'X': /* hex: "\x1", "\x12" */
5461 case 'x':
5462 case 'u': /* Unicode: "\u0023" */
5463 case 'U':
5464 if (vim_isxdigit(p[1]))
5465 {
5466 int n, nr;
5467 int c = toupper(*p);
5468
5469 if (c == 'X')
5470 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005471 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005473 else
5474 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 nr = 0;
5476 while (--n >= 0 && vim_isxdigit(p[1]))
5477 {
5478 ++p;
5479 nr = (nr << 4) + hex2nr(*p);
5480 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 /* For "\u" store the number according to
5483 * 'encoding'. */
5484 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005485 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 break;
5490
5491 /* octal: "\1", "\12", "\123" */
5492 case '0':
5493 case '1':
5494 case '2':
5495 case '3':
5496 case '4':
5497 case '5':
5498 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005499 case '7': *name = *p++ - '0';
5500 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502 *name = (*name << 3) + *p++ - '0';
5503 if (*p >= '0' && *p <= '7')
5504 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005506 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 break;
5508
5509 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005510 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 if (extra != 0)
5512 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005513 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 break;
5515 }
5516 /* FALLTHROUGH */
5517
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 break;
5520 }
5521 }
5522 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005523 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005526 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005527 if (*p != NUL) /* just in case */
5528 ++p;
5529 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 return OK;
5532}
5533
5534/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005535 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 * Return OK or FAIL.
5537 */
5538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005539get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005542 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005543 int reduce = 0;
5544
5545 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005546 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005547 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005548 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005549 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005551 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005552 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005553 break;
5554 ++reduce;
5555 ++p;
5556 }
5557 }
5558
Bram Moolenaar8c711452005-01-14 21:53:12 +00005559 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005561 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005562 return FAIL;
5563 }
5564
Bram Moolenaar8c711452005-01-14 21:53:12 +00005565 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005566 if (!evaluate)
5567 {
5568 *arg = p + 1;
5569 return OK;
5570 }
5571
5572 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005574 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005575 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005576 if (str == NULL)
5577 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578 rettv->v_type = VAR_STRING;
5579 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005580
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005582 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005584 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005586 break;
5587 ++p;
5588 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005590 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005592 *arg = p + 1;
5593
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005594 return OK;
5595}
5596
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005597/*
5598 * Return the function name of the partial.
5599 */
5600 char_u *
5601partial_name(partial_T *pt)
5602{
5603 if (pt->pt_name != NULL)
5604 return pt->pt_name;
5605 return pt->pt_func->uf_name;
5606}
5607
Bram Moolenaarddecc252016-04-06 22:59:37 +02005608 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005609partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005610{
5611 int i;
5612
5613 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005614 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005615 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005616 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005617 if (pt->pt_name != NULL)
5618 {
5619 func_unref(pt->pt_name);
5620 vim_free(pt->pt_name);
5621 }
5622 else
5623 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005624 vim_free(pt);
5625}
5626
5627/*
5628 * Unreference a closure: decrement the reference count and free it when it
5629 * becomes zero.
5630 */
5631 void
5632partial_unref(partial_T *pt)
5633{
5634 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005635 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005636}
5637
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005638static int tv_equal_recurse_limit;
5639
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005640 static int
5641func_equal(
5642 typval_T *tv1,
5643 typval_T *tv2,
5644 int ic) /* ignore case */
5645{
5646 char_u *s1, *s2;
5647 dict_T *d1, *d2;
5648 int a1, a2;
5649 int i;
5650
5651 /* empty and NULL function name considered the same */
5652 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005653 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005654 if (s1 != NULL && *s1 == NUL)
5655 s1 = NULL;
5656 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005657 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005658 if (s2 != NULL && *s2 == NUL)
5659 s2 = NULL;
5660 if (s1 == NULL || s2 == NULL)
5661 {
5662 if (s1 != s2)
5663 return FALSE;
5664 }
5665 else if (STRCMP(s1, s2) != 0)
5666 return FALSE;
5667
5668 /* empty dict and NULL dict is different */
5669 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5670 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5671 if (d1 == NULL || d2 == NULL)
5672 {
5673 if (d1 != d2)
5674 return FALSE;
5675 }
5676 else if (!dict_equal(d1, d2, ic, TRUE))
5677 return FALSE;
5678
5679 /* empty list and no list considered the same */
5680 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5681 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5682 if (a1 != a2)
5683 return FALSE;
5684 for (i = 0; i < a1; ++i)
5685 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5686 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5687 return FALSE;
5688
5689 return TRUE;
5690}
5691
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005692/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005693 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005694 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005695 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005696 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005697 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005698tv_equal(
5699 typval_T *tv1,
5700 typval_T *tv2,
5701 int ic, /* ignore case */
5702 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005703{
5704 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005705 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005706 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005707 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005708
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005709 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005710 * recursiveness to a limit. We guess they are equal then.
5711 * A fixed limit has the problem of still taking an awful long time.
5712 * Reduce the limit every time running into it. That should work fine for
5713 * deeply linked structures that are not recursively linked and catch
5714 * recursiveness quickly. */
5715 if (!recursive)
5716 tv_equal_recurse_limit = 1000;
5717 if (recursive_cnt >= tv_equal_recurse_limit)
5718 {
5719 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005720 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005721 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005722
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005723 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5724 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005725 if ((tv1->v_type == VAR_FUNC
5726 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5727 && (tv2->v_type == VAR_FUNC
5728 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5729 {
5730 ++recursive_cnt;
5731 r = func_equal(tv1, tv2, ic);
5732 --recursive_cnt;
5733 return r;
5734 }
5735
5736 if (tv1->v_type != tv2->v_type)
5737 return FALSE;
5738
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005739 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005740 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005741 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005742 ++recursive_cnt;
5743 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5744 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005745 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005746
5747 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005748 ++recursive_cnt;
5749 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5750 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005751 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005752
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005753 case VAR_BLOB:
5754 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5755
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005756 case VAR_NUMBER:
5757 return tv1->vval.v_number == tv2->vval.v_number;
5758
5759 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005760 s1 = tv_get_string_buf(tv1, buf1);
5761 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005762 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005763
5764 case VAR_SPECIAL:
5765 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005766
5767 case VAR_FLOAT:
5768#ifdef FEAT_FLOAT
5769 return tv1->vval.v_float == tv2->vval.v_float;
5770#endif
5771 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005772#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005773 return tv1->vval.v_job == tv2->vval.v_job;
5774#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005775 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005776#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005777 return tv1->vval.v_channel == tv2->vval.v_channel;
5778#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005779 case VAR_FUNC:
5780 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005781 case VAR_UNKNOWN:
5782 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005783 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005784
Bram Moolenaara03f2332016-02-06 18:09:59 +01005785 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5786 * does not equal anything, not even itself. */
5787 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005788}
5789
5790/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005791 * Return the next (unique) copy ID.
5792 * Used for serializing nested structures.
5793 */
5794 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005795get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005796{
5797 current_copyID += COPYID_INC;
5798 return current_copyID;
5799}
5800
5801/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005802 * Garbage collection for lists and dictionaries.
5803 *
5804 * We use reference counts to be able to free most items right away when they
5805 * are no longer used. But for composite items it's possible that it becomes
5806 * unused while the reference count is > 0: When there is a recursive
5807 * reference. Example:
5808 * :let l = [1, 2, 3]
5809 * :let d = {9: l}
5810 * :let l[1] = d
5811 *
5812 * Since this is quite unusual we handle this with garbage collection: every
5813 * once in a while find out which lists and dicts are not referenced from any
5814 * variable.
5815 *
5816 * Here is a good reference text about garbage collection (refers to Python
5817 * but it applies to all reference-counting mechanisms):
5818 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005819 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005820
5821/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005822 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005823 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005824 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005825 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005826 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005827garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005828{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005829 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005830 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005831 buf_T *buf;
5832 win_T *wp;
5833 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005834 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005835 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005836
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005837 if (!testing)
5838 {
5839 /* Only do this once. */
5840 want_garbage_collect = FALSE;
5841 may_garbage_collect = FALSE;
5842 garbage_collect_at_exit = FALSE;
5843 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005844
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005845 /* We advance by two because we add one for items referenced through
5846 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005847 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005848
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005849 /*
5850 * 1. Go through all accessible variables and mark all lists and dicts
5851 * with copyID.
5852 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005853
5854 /* Don't free variables in the previous_funccal list unless they are only
5855 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005856 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005857 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005858
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005859 /* script-local variables */
5860 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005861 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005862
5863 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005864 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005865 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5866 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005867
5868 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005869 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005870 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5871 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005872 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005873 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5874 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005875#ifdef FEAT_TEXT_PROP
5876 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
5877 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5878 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005879 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02005880 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005881 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5882 NULL, NULL);
5883#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005884
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005885 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005886 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005887 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5888 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005889 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005890 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005891
5892 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005893 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005894
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005895 /* named functions (matters for closures) */
5896 abort = abort || set_ref_in_functions(copyID);
5897
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005898 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005899 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005900
Bram Moolenaard812df62008-11-09 12:46:09 +00005901 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005902 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005903
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005904 // callbacks in buffers
5905 abort = abort || set_ref_in_buffers(copyID);
5906
Bram Moolenaar1dced572012-04-05 16:54:08 +02005907#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005908 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005909#endif
5910
Bram Moolenaardb913952012-06-29 12:54:53 +02005911#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005912 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005913#endif
5914
5915#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005916 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005917#endif
5918
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005919#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005920 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005921 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005922#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005923#ifdef FEAT_NETBEANS_INTG
5924 abort = abort || set_ref_in_nb_channel(copyID);
5925#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005926
Bram Moolenaare3188e22016-05-31 21:13:04 +02005927#ifdef FEAT_TIMERS
5928 abort = abort || set_ref_in_timer(copyID);
5929#endif
5930
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005931#ifdef FEAT_QUICKFIX
5932 abort = abort || set_ref_in_quickfix(copyID);
5933#endif
5934
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005935#ifdef FEAT_TERMINAL
5936 abort = abort || set_ref_in_term(copyID);
5937#endif
5938
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005939#ifdef FEAT_TEXT_PROP
5940 abort = abort || set_ref_in_popups(copyID);
5941#endif
5942
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005943 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005944 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005945 /*
5946 * 2. Free lists and dictionaries that are not referenced.
5947 */
5948 did_free = free_unref_items(copyID);
5949
5950 /*
5951 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005952 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005953 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005954 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005955 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005956 else if (p_verbose > 0)
5957 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005958 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005959 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005960
5961 return did_free;
5962}
5963
5964/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005965 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005966 */
5967 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005968free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005969{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005970 int did_free = FALSE;
5971
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005972 /* Let all "free" functions know that we are here. This means no
5973 * dictionaries, lists, channels or jobs are to be freed, because we will
5974 * do that here. */
5975 in_free_unref_items = TRUE;
5976
5977 /*
5978 * PASS 1: free the contents of the items. We don't free the items
5979 * themselves yet, so that it is possible to decrement refcount counters
5980 */
5981
Bram Moolenaarcd524592016-07-17 14:57:05 +02005982 /* Go through the list of dicts and free items without the copyID. */
5983 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005984
Bram Moolenaarda861d62016-07-17 15:46:27 +02005985 /* Go through the list of lists and free items without the copyID. */
5986 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005987
5988#ifdef FEAT_JOB_CHANNEL
5989 /* Go through the list of jobs and free items without the copyID. This
5990 * must happen before doing channels, because jobs refer to channels, but
5991 * the reference from the channel to the job isn't tracked. */
5992 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5993
5994 /* Go through the list of channels and free items without the copyID. */
5995 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5996#endif
5997
5998 /*
5999 * PASS 2: free the items themselves.
6000 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006001 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02006002 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01006003
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006004#ifdef FEAT_JOB_CHANNEL
6005 /* Go through the list of jobs and free items without the copyID. This
6006 * must happen before doing channels, because jobs refer to channels, but
6007 * the reference from the channel to the job isn't tracked. */
6008 free_unused_jobs(copyID, COPYID_MASK);
6009
6010 /* Go through the list of channels and free items without the copyID. */
6011 free_unused_channels(copyID, COPYID_MASK);
6012#endif
6013
6014 in_free_unref_items = FALSE;
6015
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006016 return did_free;
6017}
6018
6019/*
6020 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006021 * "list_stack" is used to add lists to be marked. Can be NULL.
6022 *
6023 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006024 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006026set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006027{
6028 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006029 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006030 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006031 hashtab_T *cur_ht;
6032 ht_stack_T *ht_stack = NULL;
6033 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006034
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006035 cur_ht = ht;
6036 for (;;)
6037 {
6038 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006039 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006040 /* Mark each item in the hashtab. If the item contains a hashtab
6041 * it is added to ht_stack, if it contains a list it is added to
6042 * list_stack. */
6043 todo = (int)cur_ht->ht_used;
6044 for (hi = cur_ht->ht_array; todo > 0; ++hi)
6045 if (!HASHITEM_EMPTY(hi))
6046 {
6047 --todo;
6048 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6049 &ht_stack, list_stack);
6050 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006051 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006052
6053 if (ht_stack == NULL)
6054 break;
6055
6056 /* take an item from the stack */
6057 cur_ht = ht_stack->ht;
6058 tempitem = ht_stack;
6059 ht_stack = ht_stack->prev;
6060 free(tempitem);
6061 }
6062
6063 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006064}
6065
6066/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006067 * Mark a dict and its items with "copyID".
6068 * Returns TRUE if setting references failed somehow.
6069 */
6070 int
6071set_ref_in_dict(dict_T *d, int copyID)
6072{
6073 if (d != NULL && d->dv_copyID != copyID)
6074 {
6075 d->dv_copyID = copyID;
6076 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
6077 }
6078 return FALSE;
6079}
6080
6081/*
6082 * Mark a list and its items with "copyID".
6083 * Returns TRUE if setting references failed somehow.
6084 */
6085 int
6086set_ref_in_list(list_T *ll, int copyID)
6087{
6088 if (ll != NULL && ll->lv_copyID != copyID)
6089 {
6090 ll->lv_copyID = copyID;
6091 return set_ref_in_list_items(ll, copyID, NULL);
6092 }
6093 return FALSE;
6094}
6095
6096/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006097 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006098 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6099 *
6100 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006101 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006102 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006103set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006104{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006105 listitem_T *li;
6106 int abort = FALSE;
6107 list_T *cur_l;
6108 list_stack_T *list_stack = NULL;
6109 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006110
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006111 cur_l = l;
6112 for (;;)
6113 {
6114 if (!abort)
6115 /* Mark each item in the list. If the item contains a hashtab
6116 * it is added to ht_stack, if it contains a list it is added to
6117 * list_stack. */
6118 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
6119 abort = abort || set_ref_in_item(&li->li_tv, copyID,
6120 ht_stack, &list_stack);
6121 if (list_stack == NULL)
6122 break;
6123
6124 /* take an item from the stack */
6125 cur_l = list_stack->list;
6126 tempitem = list_stack;
6127 list_stack = list_stack->prev;
6128 free(tempitem);
6129 }
6130
6131 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006132}
6133
6134/*
6135 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006136 * "list_stack" is used to add lists to be marked. Can be NULL.
6137 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6138 *
6139 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006140 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006141 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006142set_ref_in_item(
6143 typval_T *tv,
6144 int copyID,
6145 ht_stack_T **ht_stack,
6146 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006147{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006148 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006149
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006150 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006151 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006152 dict_T *dd = tv->vval.v_dict;
6153
Bram Moolenaara03f2332016-02-06 18:09:59 +01006154 if (dd != NULL && dd->dv_copyID != copyID)
6155 {
6156 /* Didn't see this dict yet. */
6157 dd->dv_copyID = copyID;
6158 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006159 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006160 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
6161 }
6162 else
6163 {
6164 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
6165 if (newitem == NULL)
6166 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006167 else
6168 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01006169 newitem->ht = &dd->dv_hashtab;
6170 newitem->prev = *ht_stack;
6171 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006172 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006173 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006174 }
6175 }
6176 else if (tv->v_type == VAR_LIST)
6177 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006178 list_T *ll = tv->vval.v_list;
6179
Bram Moolenaara03f2332016-02-06 18:09:59 +01006180 if (ll != NULL && ll->lv_copyID != copyID)
6181 {
6182 /* Didn't see this list yet. */
6183 ll->lv_copyID = copyID;
6184 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006185 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02006186 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01006187 }
6188 else
6189 {
6190 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006191 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01006192 if (newitem == NULL)
6193 abort = TRUE;
6194 else
6195 {
6196 newitem->list = ll;
6197 newitem->prev = *list_stack;
6198 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006199 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006200 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01006201 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00006202 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006203 else if (tv->v_type == VAR_FUNC)
6204 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006205 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006206 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006207 else if (tv->v_type == VAR_PARTIAL)
6208 {
6209 partial_T *pt = tv->vval.v_partial;
6210 int i;
6211
6212 /* A partial does not have a copyID, because it cannot contain itself.
6213 */
6214 if (pt != NULL)
6215 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006216 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006217
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006218 if (pt->pt_dict != NULL)
6219 {
6220 typval_T dtv;
6221
6222 dtv.v_type = VAR_DICT;
6223 dtv.vval.v_dict = pt->pt_dict;
6224 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6225 }
6226
6227 for (i = 0; i < pt->pt_argc; ++i)
6228 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
6229 ht_stack, list_stack);
6230 }
6231 }
6232#ifdef FEAT_JOB_CHANNEL
6233 else if (tv->v_type == VAR_JOB)
6234 {
6235 job_T *job = tv->vval.v_job;
6236 typval_T dtv;
6237
6238 if (job != NULL && job->jv_copyID != copyID)
6239 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006240 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006241 if (job->jv_channel != NULL)
6242 {
6243 dtv.v_type = VAR_CHANNEL;
6244 dtv.vval.v_channel = job->jv_channel;
6245 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6246 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006247 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006248 {
6249 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006250 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006251 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6252 }
6253 }
6254 }
6255 else if (tv->v_type == VAR_CHANNEL)
6256 {
6257 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006258 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006259 typval_T dtv;
6260 jsonq_T *jq;
6261 cbq_T *cq;
6262
6263 if (ch != NULL && ch->ch_copyID != copyID)
6264 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02006265 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02006266 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006267 {
6268 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
6269 jq = jq->jq_next)
6270 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
6271 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
6272 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006273 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006274 {
6275 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006276 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006277 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6278 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006279 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006280 {
6281 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006282 dtv.vval.v_partial =
6283 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006284 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6285 }
6286 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006287 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006288 {
6289 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006290 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006291 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6292 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006293 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006294 {
6295 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006296 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006297 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
6298 }
6299 }
6300 }
6301#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006302 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006303}
6304
Bram Moolenaar17a13432016-01-24 14:22:10 +01006305 static char *
6306get_var_special_name(int nr)
6307{
6308 switch (nr)
6309 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01006310 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01006311 case VVAL_TRUE: return "v:true";
6312 case VVAL_NONE: return "v:none";
6313 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01006314 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01006315 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01006316 return "42";
6317}
6318
Bram Moolenaar8c711452005-01-14 21:53:12 +00006319/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320 * Return a string with the string representation of a variable.
6321 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006322 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006323 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02006324 * When both "echo_style" and "composite_val" are FALSE, put quotes around
6325 * stings as "string()", otherwise does not put quotes around strings, as
6326 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006327 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
6328 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006329 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006331 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006332echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01006333 typval_T *tv,
6334 char_u **tofree,
6335 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006336 int copyID,
6337 int echo_style,
6338 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02006339 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006341 static int recurse = 0;
6342 char_u *r = NULL;
6343
Bram Moolenaar33570922005-01-25 22:26:29 +00006344 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006345 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02006346 if (!did_echo_string_emsg)
6347 {
6348 /* Only give this message once for a recursive call to avoid
6349 * flooding the user with errors. And stop iterating over lists
6350 * and dicts. */
6351 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006352 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02006353 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006354 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02006355 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00006356 }
6357 ++recurse;
6358
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006359 switch (tv->v_type)
6360 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006361 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006362 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006363 {
6364 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02006365 r = tv->vval.v_string;
6366 if (r == NULL)
6367 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006368 }
6369 else
6370 {
6371 *tofree = string_quote(tv->vval.v_string, FALSE);
6372 r = *tofree;
6373 }
6374 break;
6375
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006377 if (echo_style)
6378 {
6379 *tofree = NULL;
6380 r = tv->vval.v_string;
6381 }
6382 else
6383 {
6384 *tofree = string_quote(tv->vval.v_string, TRUE);
6385 r = *tofree;
6386 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006387 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006388
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006389 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006390 {
6391 partial_T *pt = tv->vval.v_partial;
6392 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006393 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01006394 garray_T ga;
6395 int i;
6396 char_u *tf;
6397
6398 ga_init2(&ga, 1, 100);
6399 ga_concat(&ga, (char_u *)"function(");
6400 if (fname != NULL)
6401 {
6402 ga_concat(&ga, fname);
6403 vim_free(fname);
6404 }
6405 if (pt != NULL && pt->pt_argc > 0)
6406 {
6407 ga_concat(&ga, (char_u *)", [");
6408 for (i = 0; i < pt->pt_argc; ++i)
6409 {
6410 if (i > 0)
6411 ga_concat(&ga, (char_u *)", ");
6412 ga_concat(&ga,
6413 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
6414 vim_free(tf);
6415 }
6416 ga_concat(&ga, (char_u *)"]");
6417 }
6418 if (pt != NULL && pt->pt_dict != NULL)
6419 {
6420 typval_T dtv;
6421
6422 ga_concat(&ga, (char_u *)", ");
6423 dtv.v_type = VAR_DICT;
6424 dtv.vval.v_dict = pt->pt_dict;
6425 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
6426 vim_free(tf);
6427 }
6428 ga_concat(&ga, (char_u *)")");
6429
6430 *tofree = ga.ga_data;
6431 r = *tofree;
6432 break;
6433 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006434
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006435 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01006436 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006437 break;
6438
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006439 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006440 if (tv->vval.v_list == NULL)
6441 {
6442 *tofree = NULL;
6443 r = NULL;
6444 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006445 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
6446 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006447 {
6448 *tofree = NULL;
6449 r = (char_u *)"[...]";
6450 }
6451 else
6452 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006453 int old_copyID = tv->vval.v_list->lv_copyID;
6454
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006455 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006456 *tofree = list2string(tv, copyID, restore_copyID);
6457 if (restore_copyID)
6458 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006459 r = *tofree;
6460 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006461 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006462
Bram Moolenaar8c711452005-01-14 21:53:12 +00006463 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006464 if (tv->vval.v_dict == NULL)
6465 {
6466 *tofree = NULL;
6467 r = NULL;
6468 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006469 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
6470 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006471 {
6472 *tofree = NULL;
6473 r = (char_u *)"{...}";
6474 }
6475 else
6476 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006477 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006478 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006479 *tofree = dict2string(tv, copyID, restore_copyID);
6480 if (restore_copyID)
6481 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006482 r = *tofree;
6483 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006484 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006486 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01006487 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006488 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006489 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006490 break;
6491
Bram Moolenaar835dc632016-02-07 14:27:38 +01006492 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006493 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006495 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006496 if (composite_val)
6497 {
6498 *tofree = string_quote(r, FALSE);
6499 r = *tofree;
6500 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006501 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006502
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006503 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006504#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006505 *tofree = NULL;
6506 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6507 r = numbuf;
6508 break;
6509#endif
6510
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006511 case VAR_SPECIAL:
6512 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006513 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006514 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006515 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006516
Bram Moolenaar8502c702014-06-17 12:51:16 +02006517 if (--recurse == 0)
6518 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006519 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006520}
6521
6522/*
6523 * Return a string with the string representation of a variable.
6524 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6525 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006526 * Does not put quotes around strings, as ":echo" displays values.
6527 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6528 * May return NULL.
6529 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006530 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006531echo_string(
6532 typval_T *tv,
6533 char_u **tofree,
6534 char_u *numbuf,
6535 int copyID)
6536{
6537 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6538}
6539
6540/*
6541 * Return a string with the string representation of a variable.
6542 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6543 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006544 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006545 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006546 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006547 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006548tv2string(
6549 typval_T *tv,
6550 char_u **tofree,
6551 char_u *numbuf,
6552 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006553{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006554 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006555}
6556
6557/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006558 * Return string "str" in ' quotes, doubling ' characters.
6559 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006560 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006561 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006562 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006563string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006564{
Bram Moolenaar33570922005-01-25 22:26:29 +00006565 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006566 char_u *p, *r, *s;
6567
Bram Moolenaar33570922005-01-25 22:26:29 +00006568 len = (function ? 13 : 3);
6569 if (str != NULL)
6570 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006571 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006572 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 if (*p == '\'')
6574 ++len;
6575 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006576 s = r = alloc(len);
6577 if (r != NULL)
6578 {
6579 if (function)
6580 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006581 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006582 r += 10;
6583 }
6584 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006585 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006586 if (str != NULL)
6587 for (p = str; *p != NUL; )
6588 {
6589 if (*p == '\'')
6590 *r++ = '\'';
6591 MB_COPY_CHAR(p, r);
6592 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006593 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006594 if (function)
6595 *r++ = ')';
6596 *r++ = NUL;
6597 }
6598 return s;
6599}
6600
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006601#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006602/*
6603 * Convert the string "text" to a floating point number.
6604 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6605 * this always uses a decimal point.
6606 * Returns the length of the text that was consumed.
6607 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006609string2float(
6610 char_u *text,
6611 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006612{
6613 char *s = (char *)text;
6614 float_T f;
6615
Bram Moolenaar62473612017-01-08 19:25:40 +01006616 /* MS-Windows does not deal with "inf" and "nan" properly. */
6617 if (STRNICMP(text, "inf", 3) == 0)
6618 {
6619 *value = INFINITY;
6620 return 3;
6621 }
6622 if (STRNICMP(text, "-inf", 3) == 0)
6623 {
6624 *value = -INFINITY;
6625 return 4;
6626 }
6627 if (STRNICMP(text, "nan", 3) == 0)
6628 {
6629 *value = NAN;
6630 return 3;
6631 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006632 f = strtod(s, &s);
6633 *value = f;
6634 return (int)((char_u *)s - text);
6635}
6636#endif
6637
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 * Get the value of an environment variable.
6640 * "arg" is pointing to the '$'. It is advanced to after the name.
6641 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006642 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 */
6644 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006645get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 char_u *string = NULL;
6648 int len;
6649 int cc;
6650 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006651 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653 ++*arg;
6654 name = *arg;
6655 len = get_env_len(arg);
6656 if (evaluate)
6657 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006658 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006659 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006660
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006661 cc = name[len];
6662 name[len] = NUL;
6663 /* first try vim_getenv(), fast for normal environment vars */
6664 string = vim_getenv(name, &mustfree);
6665 if (string != NULL && *string != NUL)
6666 {
6667 if (!mustfree)
6668 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006670 else
6671 {
6672 if (mustfree)
6673 vim_free(string);
6674
6675 /* next try expanding things like $VIM and ${HOME} */
6676 string = expand_env_save(name - 1);
6677 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006678 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006679 }
6680 name[len] = cc;
6681
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006682 rettv->v_type = VAR_STRING;
6683 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 }
6685
6686 return OK;
6687}
6688
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006689
6690
6691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006693 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006696var2fpos(
6697 typval_T *varp,
6698 int dollar_lnum, /* TRUE when $ is last line */
6699 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006701 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006703 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704
Bram Moolenaara5525202006-03-02 22:52:09 +00006705 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006706 if (varp->v_type == VAR_LIST)
6707 {
6708 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006709 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006710 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006711 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006712
6713 l = varp->vval.v_list;
6714 if (l == NULL)
6715 return NULL;
6716
6717 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006718 pos.lnum = list_find_nr(l, 0L, &error);
6719 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006720 return NULL; /* invalid line number */
6721
6722 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006723 pos.col = list_find_nr(l, 1L, &error);
6724 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006725 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006726 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006727
6728 /* We accept "$" for the column number: last column. */
6729 li = list_find(l, 1L);
6730 if (li != NULL && li->li_tv.v_type == VAR_STRING
6731 && li->li_tv.vval.v_string != NULL
6732 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6733 pos.col = len + 1;
6734
Bram Moolenaara5525202006-03-02 22:52:09 +00006735 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006736 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006737 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006738 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006739
Bram Moolenaara5525202006-03-02 22:52:09 +00006740 /* Get the virtual offset. Defaults to zero. */
6741 pos.coladd = list_find_nr(l, 2L, &error);
6742 if (error)
6743 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006744
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006745 return &pos;
6746 }
6747
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006748 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006749 if (name == NULL)
6750 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006751 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006753 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6754 {
6755 if (VIsual_active)
6756 return &VIsual;
6757 return &curwin->w_cursor;
6758 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006759 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006761 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6763 return NULL;
6764 return pp;
6765 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006766
Bram Moolenaara5525202006-03-02 22:52:09 +00006767 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006768
Bram Moolenaar477933c2007-07-17 14:32:23 +00006769 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006770 {
6771 pos.col = 0;
6772 if (name[1] == '0') /* "w0": first visible line */
6773 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006774 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006775 /* In silent Ex mode topline is zero, but that's not a valid line
6776 * number; use one instead. */
6777 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006778 return &pos;
6779 }
6780 else if (name[1] == '$') /* "w$": last visible line */
6781 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006782 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006783 /* In silent Ex mode botline is zero, return zero then. */
6784 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006785 return &pos;
6786 }
6787 }
6788 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006790 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
6792 pos.lnum = curbuf->b_ml.ml_line_count;
6793 pos.col = 0;
6794 }
6795 else
6796 {
6797 pos.lnum = curwin->w_cursor.lnum;
6798 pos.col = (colnr_T)STRLEN(ml_get_curline());
6799 }
6800 return &pos;
6801 }
6802 return NULL;
6803}
6804
6805/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006806 * Convert list in "arg" into a position and optional file number.
6807 * When "fnump" is NULL there is no file number, only 3 items.
6808 * Note that the column is passed on as-is, the caller may want to decrement
6809 * it to use 1 for the first column.
6810 * Return FAIL when conversion is not possible, doesn't check the position for
6811 * validity.
6812 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006814list2fpos(
6815 typval_T *arg,
6816 pos_T *posp,
6817 int *fnump,
6818 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006819{
6820 list_T *l = arg->vval.v_list;
6821 long i = 0;
6822 long n;
6823
Bram Moolenaar493c1782014-05-28 14:34:46 +02006824 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6825 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006826 if (arg->v_type != VAR_LIST
6827 || l == NULL
6828 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006829 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006830 return FAIL;
6831
6832 if (fnump != NULL)
6833 {
6834 n = list_find_nr(l, i++, NULL); /* fnum */
6835 if (n < 0)
6836 return FAIL;
6837 if (n == 0)
6838 n = curbuf->b_fnum; /* current buffer */
6839 *fnump = n;
6840 }
6841
6842 n = list_find_nr(l, i++, NULL); /* lnum */
6843 if (n < 0)
6844 return FAIL;
6845 posp->lnum = n;
6846
6847 n = list_find_nr(l, i++, NULL); /* col */
6848 if (n < 0)
6849 return FAIL;
6850 posp->col = n;
6851
Bram Moolenaar493c1782014-05-28 14:34:46 +02006852 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006853 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006854 posp->coladd = 0;
6855 else
6856 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006857
Bram Moolenaar493c1782014-05-28 14:34:46 +02006858 if (curswantp != NULL)
6859 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6860
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006861 return OK;
6862}
6863
6864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 * Get the length of an environment variable name.
6866 * Advance "arg" to the first character after the name.
6867 * Return 0 for error.
6868 */
6869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006870get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871{
6872 char_u *p;
6873 int len;
6874
6875 for (p = *arg; vim_isIDc(*p); ++p)
6876 ;
6877 if (p == *arg) /* no name found */
6878 return 0;
6879
6880 len = (int)(p - *arg);
6881 *arg = p;
6882 return len;
6883}
6884
6885/*
6886 * Get the length of the name of a function or internal variable.
6887 * "arg" is advanced to the first non-white character after the name.
6888 * Return 0 if something is wrong.
6889 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006890 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006891get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
6893 char_u *p;
6894 int len;
6895
6896 /* Find the end of the name. */
6897 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006898 {
6899 if (*p == ':')
6900 {
6901 /* "s:" is start of "s:var", but "n:" is not and can be used in
6902 * slice "[n:]". Also "xx:" is not a namespace. */
6903 len = (int)(p - *arg);
6904 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6905 || len > 1)
6906 break;
6907 }
6908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 if (p == *arg) /* no name found */
6910 return 0;
6911
6912 len = (int)(p - *arg);
6913 *arg = skipwhite(p);
6914
6915 return len;
6916}
6917
6918/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006919 * Get the length of the name of a variable or function.
6920 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006922 * Return -1 if curly braces expansion failed.
6923 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 * If the name contains 'magic' {}'s, expand them and return the
6925 * expanded name in an allocated string via 'alias' - caller must free.
6926 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006927 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006928get_name_len(
6929 char_u **arg,
6930 char_u **alias,
6931 int evaluate,
6932 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933{
6934 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 char_u *p;
6936 char_u *expr_start;
6937 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
6939 *alias = NULL; /* default to no alias */
6940
6941 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6942 && (*arg)[2] == (int)KE_SNR)
6943 {
6944 /* hard coded <SNR>, already translated */
6945 *arg += 3;
6946 return get_id_len(arg) + 3;
6947 }
6948 len = eval_fname_script(*arg);
6949 if (len > 0)
6950 {
6951 /* literal "<SID>", "s:" or "<SNR>" */
6952 *arg += len;
6953 }
6954
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006956 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006958 p = find_name_end(*arg, &expr_start, &expr_end,
6959 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 if (expr_start != NULL)
6961 {
6962 char_u *temp_string;
6963
6964 if (!evaluate)
6965 {
6966 len += (int)(p - *arg);
6967 *arg = skipwhite(p);
6968 return len;
6969 }
6970
6971 /*
6972 * Include any <SID> etc in the expanded string:
6973 * Thus the -len here.
6974 */
6975 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6976 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006977 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 *alias = temp_string;
6979 *arg = skipwhite(p);
6980 return (int)STRLEN(temp_string);
6981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982
6983 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006984 // Only give an error when there is something, otherwise it will be
6985 // reported at a higher level.
6986 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006987 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988
6989 return len;
6990}
6991
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006992/*
6993 * Find the end of a variable or function name, taking care of magic braces.
6994 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6995 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006996 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006997 * Return a pointer to just after the name. Equal to "arg" if there is no
6998 * valid name.
6999 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007000 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007001find_name_end(
7002 char_u *arg,
7003 char_u **expr_start,
7004 char_u **expr_end,
7005 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007007 int mb_nest = 0;
7008 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007010 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007012 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007014 *expr_start = NULL;
7015 *expr_end = NULL;
7016 }
7017
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007018 /* Quick check for valid starting character. */
7019 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
7020 return arg;
7021
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007022 for (p = arg; *p != NUL
7023 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007024 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007025 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007026 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007027 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007028 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00007029 if (*p == '\'')
7030 {
7031 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007032 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007033 ;
7034 if (*p == NUL)
7035 break;
7036 }
7037 else if (*p == '"')
7038 {
7039 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007040 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00007041 if (*p == '\\' && p[1] != NUL)
7042 ++p;
7043 if (*p == NUL)
7044 break;
7045 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007046 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
7047 {
7048 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01007049 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007050 len = (int)(p - arg);
7051 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01007052 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01007053 break;
7054 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007055
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007056 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007058 if (*p == '[')
7059 ++br_nest;
7060 else if (*p == ']')
7061 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00007063
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007064 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007066 if (*p == '{')
7067 {
7068 mb_nest++;
7069 if (expr_start != NULL && *expr_start == NULL)
7070 *expr_start = p;
7071 }
7072 else if (*p == '}')
7073 {
7074 mb_nest--;
7075 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
7076 *expr_end = p;
7077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 }
7080
7081 return p;
7082}
7083
7084/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007085 * Expands out the 'magic' {}'s in a variable/function name.
7086 * Note that this can call itself recursively, to deal with
7087 * constructs like foo{bar}{baz}{bam}
7088 * The four pointer arguments point to "foo{expre}ss{ion}bar"
7089 * "in_start" ^
7090 * "expr_start" ^
7091 * "expr_end" ^
7092 * "in_end" ^
7093 *
7094 * Returns a new allocated string, which the caller must free.
7095 * Returns NULL for failure.
7096 */
7097 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007098make_expanded_name(
7099 char_u *in_start,
7100 char_u *expr_start,
7101 char_u *expr_end,
7102 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007103{
7104 char_u c1;
7105 char_u *retval = NULL;
7106 char_u *temp_result;
7107 char_u *nextcmd = NULL;
7108
7109 if (expr_end == NULL || in_end == NULL)
7110 return NULL;
7111 *expr_start = NUL;
7112 *expr_end = NUL;
7113 c1 = *in_end;
7114 *in_end = NUL;
7115
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007116 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007117 if (temp_result != NULL && nextcmd == NULL)
7118 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007119 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
7120 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007121 if (retval != NULL)
7122 {
7123 STRCPY(retval, in_start);
7124 STRCAT(retval, temp_result);
7125 STRCAT(retval, expr_end + 1);
7126 }
7127 }
7128 vim_free(temp_result);
7129
7130 *in_end = c1; /* put char back for error messages */
7131 *expr_start = '{';
7132 *expr_end = '}';
7133
7134 if (retval != NULL)
7135 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007136 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007137 if (expr_start != NULL)
7138 {
7139 /* Further expansion! */
7140 temp_result = make_expanded_name(retval, expr_start,
7141 expr_end, temp_result);
7142 vim_free(retval);
7143 retval = temp_result;
7144 }
7145 }
7146
7147 return retval;
7148}
7149
7150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007154 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007155eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007157 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
7158}
7159
7160/*
7161 * Return TRUE if character "c" can be used as the first character in a
7162 * variable or function name (excluding '{' and '}').
7163 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007164 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007165eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007166{
7167 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168}
7169
7170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 * Set number v: variable to "val".
7172 */
7173 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007174set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177}
7178
7179/*
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02007180 * Get typval_T v: variable value.
7181 */
7182 typval_T *
7183get_vim_var_tv(int idx)
7184{
7185 return &vimvars[idx].vv_tv;
7186}
7187
7188/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007189 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007191 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007192get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007194 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195}
7196
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007197/*
7198 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01007199 * If the String variable has never been set, return an empty string.
7200 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007201 */
7202 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007203get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007204{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007205 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007206}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007209 * Get List v: variable value. Caller must take care of reference count when
7210 * needed.
7211 */
7212 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007213get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00007214{
7215 return vimvars[idx].vv_list;
7216}
7217
7218/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007219 * Get Dict v: variable value. Caller must take care of reference count when
7220 * needed.
7221 */
7222 dict_T *
7223get_vim_var_dict(int idx)
7224{
7225 return vimvars[idx].vv_dict;
7226}
7227
7228/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007229 * Set v:char to character "c".
7230 */
7231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007232set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007233{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007234 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007235
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007236 if (has_mbyte)
7237 buf[(*mb_char2bytes)(c, buf)] = NUL;
7238 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00007239 {
7240 buf[0] = c;
7241 buf[1] = NUL;
7242 }
7243 set_vim_var_string(VV_CHAR, buf, -1);
7244}
7245
7246/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007247 * Set v:count to "count" and v:count1 to "count1".
7248 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 */
7250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007251set_vcount(
7252 long count,
7253 long count1,
7254 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00007256 if (set_prevcount)
7257 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007258 vimvars[VV_COUNT].vv_nr = count;
7259 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260}
7261
7262/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02007263 * Save variables that might be changed as a side effect. Used when executing
7264 * a timer callback.
7265 */
7266 void
7267save_vimvars(vimvars_save_T *vvsave)
7268{
7269 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
7270 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
7271 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
7272}
7273
7274/*
7275 * Restore variables saved by save_vimvars().
7276 */
7277 void
7278restore_vimvars(vimvars_save_T *vvsave)
7279{
7280 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
7281 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
7282 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
7283}
7284
7285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 * Set string v: variable to a copy of "val".
7287 */
7288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007289set_vim_var_string(
7290 int idx,
7291 char_u *val,
7292 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293{
Bram Moolenaara542c682016-01-31 16:28:04 +01007294 clear_tv(&vimvars[idx].vv_di.di_tv);
7295 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302}
7303
7304/*
Bram Moolenaard812df62008-11-09 12:46:09 +00007305 * Set List v: variable to "val".
7306 */
7307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007308set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00007309{
Bram Moolenaara542c682016-01-31 16:28:04 +01007310 clear_tv(&vimvars[idx].vv_di.di_tv);
7311 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00007312 vimvars[idx].vv_list = val;
7313 if (val != NULL)
7314 ++val->lv_refcount;
7315}
7316
7317/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02007318 * Set Dictionary v: variable to "val".
7319 */
7320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007321set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02007322{
Bram Moolenaara542c682016-01-31 16:28:04 +01007323 clear_tv(&vimvars[idx].vv_di.di_tv);
7324 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02007325 vimvars[idx].vv_dict = val;
7326 if (val != NULL)
7327 {
7328 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01007329 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02007330 }
7331}
7332
7333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 * Set v:register if needed.
7335 */
7336 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007337set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338{
7339 char_u regname;
7340
7341 if (c == 0 || c == ' ')
7342 regname = '"';
7343 else
7344 regname = c;
7345 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007346 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 set_vim_var_string(VV_REG, &regname, 1);
7348}
7349
7350/*
7351 * Get or set v:exception. If "oldval" == NULL, return the current value.
7352 * Otherwise, restore the value to "oldval" and return NULL.
7353 * Must always be called in pairs to save and restore v:exception! Does not
7354 * take care of memory allocations.
7355 */
7356 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007357v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358{
7359 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361
Bram Moolenaare9a41262005-01-15 22:18:47 +00007362 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 return NULL;
7364}
7365
7366/*
7367 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
7368 * Otherwise, restore the value to "oldval" and return NULL.
7369 * Must always be called in pairs to save and restore v:throwpoint! Does not
7370 * take care of memory allocations.
7371 */
7372 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007373v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374{
7375 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 return NULL;
7380}
7381
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382/*
7383 * Set v:cmdarg.
7384 * If "eap" != NULL, use "eap" to generate the value and return the old value.
7385 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
7386 * Must always be called in pairs!
7387 */
7388 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007389set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391 char_u *oldval;
7392 char_u *newval;
7393 unsigned len;
7394
Bram Moolenaare9a41262005-01-15 22:18:47 +00007395 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007396 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007398 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007400 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 }
7402
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007403 if (eap->force_bin == FORCE_BIN)
7404 len = 6;
7405 else if (eap->force_bin == FORCE_NOBIN)
7406 len = 8;
7407 else
7408 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007409
7410 if (eap->read_edit)
7411 len += 7;
7412
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007413 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007414 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007415 if (eap->force_enc != 0)
7416 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007417 if (eap->bad_char != 0)
7418 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007419
7420 newval = alloc(len + 1);
7421 if (newval == NULL)
7422 return NULL;
7423
7424 if (eap->force_bin == FORCE_BIN)
7425 sprintf((char *)newval, " ++bin");
7426 else if (eap->force_bin == FORCE_NOBIN)
7427 sprintf((char *)newval, " ++nobin");
7428 else
7429 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007430
7431 if (eap->read_edit)
7432 STRCAT(newval, " ++edit");
7433
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007434 if (eap->force_ff != 0)
7435 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02007436 eap->force_ff == 'u' ? "unix"
7437 : eap->force_ff == 'd' ? "dos"
7438 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007439 if (eap->force_enc != 0)
7440 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
7441 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02007442 if (eap->bad_char == BAD_KEEP)
7443 STRCPY(newval + STRLEN(newval), " ++bad=keep");
7444 else if (eap->bad_char == BAD_DROP)
7445 STRCPY(newval + STRLEN(newval), " ++bad=drop");
7446 else if (eap->bad_char != 0)
7447 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007448 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007449 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451
7452/*
7453 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01007454 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007457get_var_tv(
7458 char_u *name,
7459 int len, /* length of "name" */
7460 typval_T *rettv, /* NULL when only checking existence */
7461 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
7462 int verbose, /* may give error message */
7463 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464{
7465 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007466 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007467 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469
7470 /* truncate the name, so that we can use strcmp() */
7471 cc = name[len];
7472 name[len] = NUL;
7473
7474 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 * Check for user-defined variables.
7476 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01007477 v = find_var(name, NULL, no_autoload);
7478 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007480 tv = &v->di_tv;
7481 if (dip != NULL)
7482 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 }
7484
Bram Moolenaare9a41262005-01-15 22:18:47 +00007485 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007487 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007488 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 ret = FAIL;
7490 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007491 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007492 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493
7494 name[len] = cc;
7495
7496 return ret;
7497}
7498
7499/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007500 * Check if variable "name[len]" is a local variable or an argument.
7501 * If so, "*eval_lavars_used" is set to TRUE.
7502 */
7503 static void
7504check_vars(char_u *name, int len)
7505{
7506 int cc;
7507 char_u *varname;
7508 hashtab_T *ht;
7509
7510 if (eval_lavars_used == NULL)
7511 return;
7512
7513 /* truncate the name, so that we can use strcmp() */
7514 cc = name[len];
7515 name[len] = NUL;
7516
7517 ht = find_var_ht(name, &varname);
7518 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7519 {
7520 if (find_var(name, NULL, TRUE) != NULL)
7521 *eval_lavars_used = TRUE;
7522 }
7523
7524 name[len] = cc;
7525}
7526
7527/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02007528 * Handle:
7529 * - expr[expr], expr[expr:expr] subscript
7530 * - ".name" lookup
7531 * - function call with Funcref variable: func(expr)
7532 * - method call: var->method()
7533 *
7534 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007535 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007536 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007537handle_subscript(
7538 char_u **arg,
7539 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007540 int evaluate, // do more than finding the end
7541 int verbose, // give error messages
7542 char_u *start_leader, // start of '!' and '-' prefixes
7543 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007544{
7545 int ret = OK;
7546 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007547
Bram Moolenaar61343f02019-07-20 21:11:13 +02007548 // "." is ".name" lookup when we found a dict or when evaluating and
7549 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007550 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02007551 && (((**arg == '['
7552 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02007553 || (!evaluate
7554 && (*arg)[1] != '.'
7555 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02007556 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007557 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02007558 && !VIM_ISWHITE(*(*arg - 1)))
7559 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007560 {
7561 if (**arg == '(')
7562 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007563 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007564
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02007565 // Stop the expression evaluation when immediately aborting on
7566 // error, or when an interrupt occurred or an exception was thrown
7567 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007568 if (aborting())
7569 {
7570 if (ret == OK)
7571 clear_tv(rettv);
7572 ret = FAIL;
7573 }
7574 dict_unref(selfdict);
7575 selfdict = NULL;
7576 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02007577 else if (**arg == '-')
7578 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02007579 // Expression "-1.0->method()" applies the leader "-" before
7580 // applying ->.
7581 if (evaluate && *end_leaderp > start_leader)
7582 ret = eval7_leader(rettv, start_leader, end_leaderp);
7583 if (ret == OK)
7584 {
7585 if ((*arg)[2] == '{')
7586 // expr->{lambda}()
7587 ret = eval_lambda(arg, rettv, evaluate, verbose);
7588 else
7589 // expr->name()
7590 ret = eval_method(arg, rettv, evaluate, verbose);
7591 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02007592 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007593 else /* **arg == '[' || **arg == '.' */
7594 {
7595 dict_unref(selfdict);
7596 if (rettv->v_type == VAR_DICT)
7597 {
7598 selfdict = rettv->vval.v_dict;
7599 if (selfdict != NULL)
7600 ++selfdict->dv_refcount;
7601 }
7602 else
7603 selfdict = NULL;
7604 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7605 {
7606 clear_tv(rettv);
7607 ret = FAIL;
7608 }
7609 }
7610 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007611
Bram Moolenaar1d429612016-05-24 15:44:17 +02007612 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7613 * Don't do this when "Func" is already a partial that was bound
7614 * explicitly (pt_auto is FALSE). */
7615 if (selfdict != NULL
7616 && (rettv->v_type == VAR_FUNC
7617 || (rettv->v_type == VAR_PARTIAL
7618 && (rettv->vval.v_partial->pt_auto
7619 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007620 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007621
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007622 dict_unref(selfdict);
7623 return ret;
7624}
7625
7626/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007627 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007628 * value).
7629 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007630 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007631alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007632{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007633 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007634}
7635
7636/*
7637 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 * The string "s" must have been allocated, it is consumed.
7639 * Return NULL for out of memory, the variable otherwise.
7640 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007641 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007642alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
Bram Moolenaar33570922005-01-25 22:26:29 +00007644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007646 rettv = alloc_tv();
7647 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 rettv->v_type = VAR_STRING;
7650 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 }
7652 else
7653 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007654 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655}
7656
7657/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007658 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007661free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
7663 if (varp != NULL)
7664 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007665 switch (varp->v_type)
7666 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007667 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007668 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007669 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007670 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007671 vim_free(varp->vval.v_string);
7672 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007673 case VAR_PARTIAL:
7674 partial_unref(varp->vval.v_partial);
7675 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007676 case VAR_BLOB:
7677 blob_unref(varp->vval.v_blob);
7678 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007679 case VAR_LIST:
7680 list_unref(varp->vval.v_list);
7681 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007682 case VAR_DICT:
7683 dict_unref(varp->vval.v_dict);
7684 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007685 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007686#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007687 job_unref(varp->vval.v_job);
7688 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007689#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007690 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007691#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007692 channel_unref(varp->vval.v_channel);
7693 break;
7694#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007695 case VAR_NUMBER:
7696 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007697 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007698 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007699 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 vim_free(varp);
7702 }
7703}
7704
7705/*
7706 * Free the memory for a variable value and set the value to NULL or 0.
7707 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007709clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710{
7711 if (varp != NULL)
7712 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007713 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007715 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007716 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007717 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007718 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007719 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007720 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007721 case VAR_PARTIAL:
7722 partial_unref(varp->vval.v_partial);
7723 varp->vval.v_partial = NULL;
7724 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007725 case VAR_BLOB:
7726 blob_unref(varp->vval.v_blob);
7727 varp->vval.v_blob = NULL;
7728 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007729 case VAR_LIST:
7730 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007731 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007732 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733 case VAR_DICT:
7734 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007735 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007736 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007737 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007738 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007739 varp->vval.v_number = 0;
7740 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007741 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007742#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007743 varp->vval.v_float = 0.0;
7744 break;
7745#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007746 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007747#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007748 job_unref(varp->vval.v_job);
7749 varp->vval.v_job = NULL;
7750#endif
7751 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007752 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007753#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007754 channel_unref(varp->vval.v_channel);
7755 varp->vval.v_channel = NULL;
7756#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007757 case VAR_UNKNOWN:
7758 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007760 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 }
7762}
7763
7764/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765 * Set the value of a variable to NULL without freeing items.
7766 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007767 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007768init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007769{
7770 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007771 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007772}
7773
7774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 * Get the number value of a variable.
7776 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007777 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007778 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007779 * caller of incompatible types: it sets *denote to TRUE if "denote"
7780 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007782 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007783tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007785 int error = FALSE;
7786
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007787 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007788}
7789
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007790 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007791tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007792{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007793 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007795 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007797 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007798 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007799 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007800#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007801 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007802 break;
7803#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007804 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007805 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007806 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007807 break;
7808 case VAR_STRING:
7809 if (varp->vval.v_string != NULL)
7810 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007811 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007812 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007813 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007814 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007815 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007816 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007817 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007818 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007819 case VAR_SPECIAL:
7820 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7821 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007822 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007823#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007824 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007825 break;
7826#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007827 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007828#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007829 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007830 break;
7831#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007832 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007833 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007834 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007835 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007836 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007837 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007839 if (denote == NULL) /* useful for values that must be unsigned */
7840 n = -1;
7841 else
7842 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007843 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844}
7845
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007846#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007847 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007848tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007849{
7850 switch (varp->v_type)
7851 {
7852 case VAR_NUMBER:
7853 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007854 case VAR_FLOAT:
7855 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007856 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007857 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007858 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007859 break;
7860 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007861 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007862 break;
7863 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007864 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007865 break;
7866 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007867 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007868 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007869 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007870 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007871 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007872 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007873# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007874 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007875 break;
7876# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007877 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007878# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007879 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007880 break;
7881# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007882 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007883 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007884 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007885 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007886 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007887 break;
7888 }
7889 return 0;
7890}
7891#endif
7892
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 * Get the string value of a variable.
7895 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007896 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7897 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 * If the String variable has never been set, return an empty string.
7899 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007900 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007901 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007903 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007904tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 static char_u mybuf[NUMBUFLEN];
7907
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007908 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909}
7910
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007911 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007912tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007914 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007915
7916 return res != NULL ? res : (char_u *)"";
7917}
7918
Bram Moolenaar7d647822014-04-05 21:28:56 +02007919/*
7920 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7921 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007922 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007923tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007924{
7925 static char_u mybuf[NUMBUFLEN];
7926
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007927 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007928}
7929
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007930 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007931tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007932{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007933 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007935 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007936 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007937 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007938 return buf;
7939 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007940 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007941 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007942 break;
7943 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007944 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 break;
7946 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007947 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007948 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007949 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007950#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007951 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007952 break;
7953#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007954 case VAR_STRING:
7955 if (varp->vval.v_string != NULL)
7956 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007957 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007958 case VAR_SPECIAL:
7959 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7960 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007961 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007962 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007963 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007964 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007965#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007966 {
7967 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007968 char *status;
7969
7970 if (job == NULL)
7971 return (char_u *)"no process";
7972 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007973 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007974 : "run";
7975# ifdef UNIX
7976 vim_snprintf((char *)buf, NUMBUFLEN,
7977 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007978# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007979 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007980 "process %ld %s",
7981 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007982 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007983# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007984 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007985 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7986# endif
7987 return buf;
7988 }
7989#endif
7990 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007991 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007992#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007993 {
7994 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007995 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007996
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007997 if (channel == NULL)
7998 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7999 else
8000 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01008001 "channel %d %s", channel->ch_id, status);
8002 return buf;
8003 }
8004#endif
8005 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008006 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008007 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008008 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008010 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011}
8012
8013/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008014 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
8015 * string() on Dict, List, etc.
8016 */
8017 char_u *
8018tv_stringify(typval_T *varp, char_u *buf)
8019{
8020 if (varp->v_type == VAR_LIST
8021 || varp->v_type == VAR_DICT
8022 || varp->v_type == VAR_FUNC
8023 || varp->v_type == VAR_PARTIAL
8024 || varp->v_type == VAR_FLOAT)
8025 {
8026 typval_T tmp;
8027
8028 f_string(varp, &tmp);
8029 tv_get_string_buf(&tmp, buf);
8030 clear_tv(varp);
8031 *varp = tmp;
8032 return tmp.vval.v_string;
8033 }
8034 return tv_get_string_buf(varp, buf);
8035}
8036
8037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 * Find variable "name" in the list of variables.
8039 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008040 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00008041 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00008042 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008044 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008045find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008049 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050
Bram Moolenaara7043832005-01-21 11:56:39 +00008051 ht = find_var_ht(name, &varname);
8052 if (htp != NULL)
8053 *htp = ht;
8054 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008056 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
8057 if (ret != NULL)
8058 return ret;
8059
8060 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008061 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062}
8063
8064/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02008065 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00008066 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008069find_var_in_ht(
8070 hashtab_T *ht,
8071 int htname,
8072 char_u *varname,
8073 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00008074{
Bram Moolenaar33570922005-01-25 22:26:29 +00008075 hashitem_T *hi;
8076
8077 if (*varname == NUL)
8078 {
8079 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02008080 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00008081 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008082 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00008083 case 'g': return &globvars_var;
8084 case 'v': return &vimvars_var;
8085 case 'b': return &curbuf->b_bufvar;
8086 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008087 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008088 case 'l': return get_funccal_local_var();
8089 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00008090 }
8091 return NULL;
8092 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008093
8094 hi = hash_find(ht, varname);
8095 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008096 {
8097 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008098 * worked find the variable again. Don't auto-load a script if it was
8099 * loaded already, otherwise it would be loaded every time when
8100 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008101 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01008102 {
8103 /* Note: script_autoload() may make "hi" invalid. It must either
8104 * be obtained again or not used. */
8105 if (!script_autoload(varname, FALSE) || aborting())
8106 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008107 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01008108 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008109 if (HASHITEM_EMPTY(hi))
8110 return NULL;
8111 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008112 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008113}
8114
8115/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008116 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02008117 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00008118 * Set "varname" to the start of name without ':'.
8119 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008120 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008121find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00008123 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008124 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00008125
Bram Moolenaar73627d02015-08-11 15:46:09 +02008126 if (name[0] == NUL)
8127 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 if (name[1] != ':')
8129 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008130 /* The name must not start with a colon or #. */
8131 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 return NULL;
8133 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00008134
Bram Moolenaard2e716e2019-04-20 14:39:52 +02008135 // "version" is "v:version" in all scopes if scriptversion < 3.
8136 // Same for a few other variables marked with VV_COMPAT.
8137 if (current_sctx.sc_version < 3)
8138 {
8139 hi = hash_find(&compat_hashtab, name);
8140 if (!HASHITEM_EMPTY(hi))
8141 return &compat_hashtab;
8142 }
Bram Moolenaar532c7802005-01-27 14:44:31 +00008143
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008144 ht = get_funccal_local_ht();
8145 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008146 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008147 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 }
8149 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008150 if (*name == 'g') /* global variable */
8151 return &globvarht;
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02008152 // There must be no ':' or '#' in the rest of the name, unless g: is used
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008153 if (vim_strchr(name + 2, ':') != NULL
8154 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008155 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008157 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008159 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008160 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02008161 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00008162 if (*name == 'v') /* v: variable */
8163 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008164 if (*name == 'a') /* a: function argument */
8165 return get_funccal_args_ht();
8166 if (*name == 'l') /* l: local function variable */
8167 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008169 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
8170 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 return NULL;
8172}
8173
8174/*
8175 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008176 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 * Returns NULL when it doesn't exist.
8178 */
8179 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008180get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181{
Bram Moolenaar33570922005-01-25 22:26:29 +00008182 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008184 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 if (v == NULL)
8186 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008187 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188}
8189
8190/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008191 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 * sourcing this script and when executing functions defined in the script.
8193 */
8194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008195new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196{
Bram Moolenaara7043832005-01-21 11:56:39 +00008197 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008198 hashtab_T *ht;
8199 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00008200
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
8202 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008203 /* Re-allocating ga_data means that an ht_array pointing to
8204 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00008205 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00008206 for (i = 1; i <= ga_scripts.ga_len; ++i)
8207 {
8208 ht = &SCRIPT_VARS(i);
8209 if (ht->ht_mask == HT_INIT_SIZE - 1)
8210 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02008211 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00008212 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00008213 }
8214
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 while (ga_scripts.ga_len < id)
8216 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008217 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008218 ALLOC_CLEAR_ONE(scriptvar_T);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008219 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 }
8222 }
8223}
8224
8225/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008226 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
8227 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 */
8229 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008230init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231{
Bram Moolenaar33570922005-01-25 22:26:29 +00008232 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02008233 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02008234 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008235 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00008236 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008237 dict_var->di_tv.vval.v_dict = dict;
8238 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008239 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008240 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
8241 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242}
8243
8244/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02008245 * Unreference a dictionary initialized by init_var_dict().
8246 */
8247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008248unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02008249{
8250 /* Now the dict needs to be freed if no one else is using it, go back to
8251 * normal reference counting. */
8252 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
8253 dict_unref(dict);
8254}
8255
8256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00008258 * Frees all allocated variables and the value they contain.
8259 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 */
8261 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008262vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00008263{
8264 vars_clear_ext(ht, TRUE);
8265}
8266
8267/*
8268 * Like vars_clear(), but only free the value if "free_val" is TRUE.
8269 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008271vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272{
Bram Moolenaara7043832005-01-21 11:56:39 +00008273 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 hashitem_T *hi;
8275 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276
Bram Moolenaar33570922005-01-25 22:26:29 +00008277 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008278 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00008279 for (hi = ht->ht_array; todo > 0; ++hi)
8280 {
8281 if (!HASHITEM_EMPTY(hi))
8282 {
8283 --todo;
8284
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00008286 * ht_array might change then. hash_clear() takes care of it
8287 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008288 v = HI2DI(hi);
8289 if (free_val)
8290 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008291 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00008292 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00008293 }
8294 }
8295 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008296 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297}
8298
Bram Moolenaara7043832005-01-21 11:56:39 +00008299/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008300 * Delete a variable from hashtab "ht" at item "hi".
8301 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00008302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008304delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305{
Bram Moolenaar33570922005-01-25 22:26:29 +00008306 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00008307
8308 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00008309 clear_tv(&di->di_tv);
8310 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311}
8312
8313/*
8314 * List the value of one internal variable.
8315 */
8316 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01008317list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008319 char_u *tofree;
8320 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008321 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008322
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008323 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00008324 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008325 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008326 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327}
8328
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008330list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01008331 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01008332 char_u *name,
8333 int type,
8334 char_u *string,
8335 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336{
Bram Moolenaar31859182007-08-14 20:41:13 +00008337 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
8338 msg_start();
8339 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01008341 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 msg_putchar(' ');
8343 msg_advance(22);
8344 if (type == VAR_NUMBER)
8345 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008346 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008347 msg_putchar('*');
8348 else if (type == VAR_LIST)
8349 {
8350 msg_putchar('[');
8351 if (*string == '[')
8352 ++string;
8353 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008354 else if (type == VAR_DICT)
8355 {
8356 msg_putchar('{');
8357 if (*string == '{')
8358 ++string;
8359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 else
8361 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008362
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008364
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008365 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008366 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00008367 if (*first)
8368 {
8369 msg_clr_eos();
8370 *first = FALSE;
8371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372}
8373
8374/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008375 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 * If the variable already exists, the value is updated.
8377 * Otherwise the variable is created.
8378 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008379 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008380set_var(
8381 char_u *name,
8382 typval_T *tv,
Bram Moolenaar9937a052019-06-15 15:45:06 +02008383 int copy) // make copy of value in "tv"
8384{
8385 set_var_const(name, tv, copy, FALSE);
8386}
8387
8388/*
8389 * Set variable "name" to value in "tv".
8390 * If the variable already exists and "is_const" is FALSE the value is updated.
8391 * Otherwise the variable is created.
8392 */
8393 static void
8394set_var_const(
8395 char_u *name,
8396 typval_T *tv,
8397 int copy, // make copy of value in "tv"
8398 int is_const) // disallow to modify existing variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399{
Bram Moolenaar33570922005-01-25 22:26:29 +00008400 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008404 ht = find_var_ht(name, &varname);
8405 if (ht == NULL || *varname == NUL)
8406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008407 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008408 return;
8409 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02008410 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01008411
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008412 /* Search in parent scope which is possible to reference from lambda */
8413 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02008414 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02008415
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008416 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
8417 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008418 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008419
Bram Moolenaar33570922005-01-25 22:26:29 +00008420 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {
Bram Moolenaar9937a052019-06-15 15:45:06 +02008422 if (is_const)
8423 {
8424 emsg(_(e_cannot_mod));
8425 return;
8426 }
8427
Bram Moolenaar33570922005-01-25 22:26:29 +00008428 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02008429 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008430 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00008431 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008432
8433 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008434 * Handle setting internal v: variables separately where needed to
8435 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00008436 */
8437 if (ht == &vimvarht)
8438 {
8439 if (v->di_tv.v_type == VAR_STRING)
8440 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008441 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008442 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01008443 {
8444 char_u *val = tv_get_string(tv);
8445
8446 // Careful: when assigning to v:errmsg and tv_get_string()
8447 // causes an error message the variable will alrady be set.
8448 if (v->di_tv.vval.v_string == NULL)
8449 v->di_tv.vval.v_string = vim_strsave(val);
8450 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008451 else
8452 {
8453 /* Take over the string to avoid an extra alloc/free. */
8454 v->di_tv.vval.v_string = tv->vval.v_string;
8455 tv->vval.v_string = NULL;
8456 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008457 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008458 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008459 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008460 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008461 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008462 if (STRCMP(varname, "searchforward") == 0)
8463 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01008464#ifdef FEAT_SEARCH_EXTRA
8465 else if (STRCMP(varname, "hlsearch") == 0)
8466 {
8467 no_hlsearch = !v->di_tv.vval.v_number;
8468 redraw_all_later(SOME_VALID);
8469 }
8470#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008471 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008472 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02008473 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008474 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008475 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01008476 return;
8477 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008478 }
8479
8480 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 }
8482 else /* add a new variable */
8483 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01008484 // Can't add "v:" or "a:" variable.
8485 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008486 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008487 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00008488 return;
8489 }
8490
Bram Moolenaar31b81602019-02-10 22:14:27 +01008491 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008492 if (!valid_varname(varname))
8493 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00008494
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008495 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
Bram Moolenaara7043832005-01-21 11:56:39 +00008496 if (v == NULL)
8497 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00008498 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00008499 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {
Bram Moolenaara7043832005-01-21 11:56:39 +00008501 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 return;
8503 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02008504 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar9937a052019-06-15 15:45:06 +02008505 if (is_const)
8506 v->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 }
Bram Moolenaara7043832005-01-21 11:56:39 +00008508
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008509 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00008510 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008511 else
8512 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008513 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008514 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008515 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00008516 }
Bram Moolenaar9937a052019-06-15 15:45:06 +02008517
8518 if (is_const)
8519 v->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520}
8521
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008523 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008524 * Also give an error message.
8525 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008526 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008527var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008528{
8529 if (flags & DI_FLAGS_RO)
8530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008531 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008532 return TRUE;
8533 }
8534 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008536 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008537 return TRUE;
8538 }
8539 return FALSE;
8540}
8541
8542/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008543 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8544 * Also give an error message.
8545 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008546 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008547var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008548{
8549 if (flags & DI_FLAGS_FIX)
8550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008551 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008552 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008553 return TRUE;
8554 }
8555 return FALSE;
8556}
8557
8558/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008559 * Check if a funcref is assigned to a valid variable name.
8560 * Return TRUE and give an error if not.
8561 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008563var_check_func_name(
8564 char_u *name, /* points to start of variable name */
8565 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008566{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008567 /* Allow for w: b: s: and t:. */
8568 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008569 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8570 ? name[2] : name[0]))
8571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008572 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008573 name);
8574 return TRUE;
8575 }
8576 /* Don't allow hiding a function. When "v" is not NULL we might be
8577 * assigning another function to the same var, the type is checked
8578 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008579 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008581 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008582 name);
8583 return TRUE;
8584 }
8585 return FALSE;
8586}
8587
8588/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008589 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008590 * Also give an error message, using "name" or _("name") when use_gettext is
8591 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008592 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008593 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008594var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008595{
8596 if (lock & VAR_LOCKED)
8597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008598 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008599 name == NULL ? (char_u *)_("Unknown")
8600 : use_gettext ? (char_u *)_(name)
8601 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008602 return TRUE;
8603 }
8604 if (lock & VAR_FIXED)
8605 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008606 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008607 name == NULL ? (char_u *)_("Unknown")
8608 : use_gettext ? (char_u *)_(name)
8609 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008610 return TRUE;
8611 }
8612 return FALSE;
8613}
8614
8615/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008616 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8617 * Also give an error message, using "name" or _("name") when use_gettext is
8618 * TRUE.
8619 */
8620 static int
8621tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8622{
8623 int lock = 0;
8624
8625 switch (tv->v_type)
8626 {
8627 case VAR_BLOB:
8628 if (tv->vval.v_blob != NULL)
8629 lock = tv->vval.v_blob->bv_lock;
8630 break;
8631 case VAR_LIST:
8632 if (tv->vval.v_list != NULL)
8633 lock = tv->vval.v_list->lv_lock;
8634 break;
8635 case VAR_DICT:
8636 if (tv->vval.v_dict != NULL)
8637 lock = tv->vval.v_dict->dv_lock;
8638 break;
8639 default:
8640 break;
8641 }
8642 return var_check_lock(tv->v_lock, name, use_gettext)
8643 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8644}
8645
8646/*
8647 * Check if a variable name is valid.
8648 * Return FALSE and give an error if not.
8649 */
8650 int
8651valid_varname(char_u *varname)
8652{
8653 char_u *p;
8654
8655 for (p = varname; *p != NUL; ++p)
8656 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8657 && *p != AUTOLOAD_CHAR)
8658 {
8659 semsg(_(e_illvar), varname);
8660 return FALSE;
8661 }
8662 return TRUE;
8663}
8664
8665/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008666 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008667 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008668 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008669 * It is OK for "from" and "to" to point to the same item. This is used to
8670 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008671 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008672 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008673copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008675 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008676 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008677 switch (from->v_type)
8678 {
8679 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008680 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008681 to->vval.v_number = from->vval.v_number;
8682 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008683 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008684#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008685 to->vval.v_float = from->vval.v_float;
8686 break;
8687#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008688 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008689#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008690 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008691 if (to->vval.v_job != NULL)
8692 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008693 break;
8694#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008695 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008696#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008697 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008698 if (to->vval.v_channel != NULL)
8699 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008700 break;
8701#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008702 case VAR_STRING:
8703 case VAR_FUNC:
8704 if (from->vval.v_string == NULL)
8705 to->vval.v_string = NULL;
8706 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008707 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008708 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008709 if (from->v_type == VAR_FUNC)
8710 func_ref(to->vval.v_string);
8711 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008712 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008713 case VAR_PARTIAL:
8714 if (from->vval.v_partial == NULL)
8715 to->vval.v_partial = NULL;
8716 else
8717 {
8718 to->vval.v_partial = from->vval.v_partial;
8719 ++to->vval.v_partial->pt_refcount;
8720 }
8721 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008722 case VAR_BLOB:
8723 if (from->vval.v_blob == NULL)
8724 to->vval.v_blob = NULL;
8725 else
8726 {
8727 to->vval.v_blob = from->vval.v_blob;
8728 ++to->vval.v_blob->bv_refcount;
8729 }
8730 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008731 case VAR_LIST:
8732 if (from->vval.v_list == NULL)
8733 to->vval.v_list = NULL;
8734 else
8735 {
8736 to->vval.v_list = from->vval.v_list;
8737 ++to->vval.v_list->lv_refcount;
8738 }
8739 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008740 case VAR_DICT:
8741 if (from->vval.v_dict == NULL)
8742 to->vval.v_dict = NULL;
8743 else
8744 {
8745 to->vval.v_dict = from->vval.v_dict;
8746 ++to->vval.v_dict->dv_refcount;
8747 }
8748 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008749 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008750 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008751 break;
8752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753}
8754
8755/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008756 * Make a copy of an item.
8757 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008758 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8759 * reference to an already copied list/dict can be used.
8760 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008761 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008762 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008763item_copy(
8764 typval_T *from,
8765 typval_T *to,
8766 int deep,
8767 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008768{
8769 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008770 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008771
Bram Moolenaar33570922005-01-25 22:26:29 +00008772 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008773 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008774 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008775 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008776 }
8777 ++recurse;
8778
8779 switch (from->v_type)
8780 {
8781 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008782 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008783 case VAR_STRING:
8784 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008785 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008786 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008787 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008788 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008789 copy_tv(from, to);
8790 break;
8791 case VAR_LIST:
8792 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008793 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008794 if (from->vval.v_list == NULL)
8795 to->vval.v_list = NULL;
8796 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8797 {
8798 /* use the copy made earlier */
8799 to->vval.v_list = from->vval.v_list->lv_copylist;
8800 ++to->vval.v_list->lv_refcount;
8801 }
8802 else
8803 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8804 if (to->vval.v_list == NULL)
8805 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008806 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008807 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008808 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008809 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008810 case VAR_DICT:
8811 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008812 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008813 if (from->vval.v_dict == NULL)
8814 to->vval.v_dict = NULL;
8815 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8816 {
8817 /* use the copy made earlier */
8818 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8819 ++to->vval.v_dict->dv_refcount;
8820 }
8821 else
8822 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8823 if (to->vval.v_dict == NULL)
8824 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008825 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008826 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008827 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008828 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008829 }
8830 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008831 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008832}
8833
8834/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 * This function is used by f_input() and f_inputdialog() functions. The third
8836 * argument to f_input() specifies the type of completion to use at the
8837 * prompt. The third argument to f_inputdialog() specifies the value to return
8838 * when the user cancels the prompt.
8839 */
8840 void
8841get_user_input(
8842 typval_T *argvars,
8843 typval_T *rettv,
8844 int inputdialog,
8845 int secret)
8846{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008847 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008848 char_u *p = NULL;
8849 int c;
8850 char_u buf[NUMBUFLEN];
8851 int cmd_silent_save = cmd_silent;
8852 char_u *defstr = (char_u *)"";
8853 int xp_type = EXPAND_NOTHING;
8854 char_u *xp_arg = NULL;
8855
8856 rettv->v_type = VAR_STRING;
8857 rettv->vval.v_string = NULL;
8858
8859#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008860 /* While starting up, there is no place to enter text. When running tests
8861 * with --not-a-term we assume feedkeys() will be used. */
8862 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863 return;
8864#endif
8865
8866 cmd_silent = FALSE; /* Want to see the prompt. */
8867 if (prompt != NULL)
8868 {
8869 /* Only the part of the message after the last NL is considered as
8870 * prompt for the command line */
8871 p = vim_strrchr(prompt, '\n');
8872 if (p == NULL)
8873 p = prompt;
8874 else
8875 {
8876 ++p;
8877 c = *p;
8878 *p = NUL;
8879 msg_start();
8880 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008881 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008882 msg_didout = FALSE;
8883 msg_starthere();
8884 *p = c;
8885 }
8886 cmdline_row = msg_row;
8887
8888 if (argvars[1].v_type != VAR_UNKNOWN)
8889 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008890 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008891 if (defstr != NULL)
8892 stuffReadbuffSpec(defstr);
8893
8894 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8895 {
8896 char_u *xp_name;
8897 int xp_namelen;
8898 long argt;
8899
8900 /* input() with a third argument: completion */
8901 rettv->vval.v_string = NULL;
8902
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008903 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008904 if (xp_name == NULL)
8905 return;
8906
8907 xp_namelen = (int)STRLEN(xp_name);
8908
8909 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8910 &xp_arg) == FAIL)
8911 return;
8912 }
8913 }
8914
8915 if (defstr != NULL)
8916 {
8917 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008918
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008919 ex_normal_busy = 0;
8920 rettv->vval.v_string =
8921 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8922 xp_type, xp_arg);
8923 ex_normal_busy = save_ex_normal_busy;
8924 }
8925 if (inputdialog && rettv->vval.v_string == NULL
8926 && argvars[1].v_type != VAR_UNKNOWN
8927 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008928 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008929 &argvars[2], buf));
8930
8931 vim_free(xp_arg);
8932
8933 /* since the user typed this, no need to wait for return */
8934 need_wait_return = FALSE;
8935 msg_didout = FALSE;
8936 }
8937 cmd_silent = cmd_silent_save;
8938}
8939
8940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 * ":echo expr1 ..." print each argument separated with a space, add a
8942 * newline at the end.
8943 * ":echon expr1 ..." print each argument plain.
8944 */
8945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008946ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
8948 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008949 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008950 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 char_u *p;
8952 int needclr = TRUE;
8953 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008954 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008955 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008956 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957
8958 if (eap->skip)
8959 ++emsg_skip;
8960 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8961 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008962 /* If eval1() causes an error message the text from the command may
8963 * still need to be cleared. E.g., "echo 22,44". */
8964 need_clr_eos = needclr;
8965
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 {
8969 /*
8970 * Report the invalid expression unless the expression evaluation
8971 * has been cancelled due to an aborting error, an interrupt, or an
8972 * exception.
8973 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008974 if (!aborting() && did_emsg == did_emsg_before
8975 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008976 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008977 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 break;
8979 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008980 need_clr_eos = FALSE;
8981
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 if (!eap->skip)
8983 {
8984 if (atstart)
8985 {
8986 atstart = FALSE;
8987 /* Call msg_start() after eval1(), evaluating the expression
8988 * may cause a message to appear. */
8989 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008990 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008991 /* Mark the saved text as finishing the line, so that what
8992 * follows is displayed on a new line when scrolling back
8993 * at the more prompt. */
8994 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 }
8998 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008999 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01009000 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009001 if (p != NULL)
9002 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009004 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009006 if (*p != TAB && needclr)
9007 {
9008 /* remove any text still there from the command */
9009 msg_clr_eos();
9010 needclr = FALSE;
9011 }
9012 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 }
9014 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009015 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009016 if (has_mbyte)
9017 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009018 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009019
9020 (void)msg_outtrans_len_attr(p, i, echo_attr);
9021 p += i - 1;
9022 }
9023 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009024 (void)msg_outtrans_len_attr(p, 1, echo_attr);
9025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009027 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009029 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 arg = skipwhite(arg);
9031 }
9032 eap->nextcmd = check_nextcmd(arg);
9033
9034 if (eap->skip)
9035 --emsg_skip;
9036 else
9037 {
9038 /* remove text that may still be there from the command */
9039 if (needclr)
9040 msg_clr_eos();
9041 if (eap->cmdidx == CMD_echo)
9042 msg_end();
9043 }
9044}
9045
9046/*
9047 * ":echohl {name}".
9048 */
9049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009050ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02009052 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053}
9054
9055/*
9056 * ":execute expr1 ..." execute the result of an expression.
9057 * ":echomsg expr1 ..." Print a message
9058 * ":echoerr expr1 ..." Print an error
9059 * Each gets spaces around each argument and a newline at the end for
9060 * echo commands
9061 */
9062 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009063ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
9065 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00009066 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 int ret = OK;
9068 char_u *p;
9069 garray_T ga;
9070 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01009071 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072
9073 ga_init2(&ga, 1, 80);
9074
9075 if (eap->skip)
9076 ++emsg_skip;
9077 while (*arg != NUL && *arg != '|' && *arg != '\n')
9078 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01009079 ret = eval1_emsg(&arg, &rettv, !eap->skip);
9080 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082
9083 if (!eap->skip)
9084 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01009085 char_u buf[NUMBUFLEN];
9086
9087 if (eap->cmdidx == CMD_execute)
9088 p = tv_get_string_buf(&rettv, buf);
9089 else
9090 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 len = (int)STRLEN(p);
9092 if (ga_grow(&ga, len + 2) == FAIL)
9093 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 ret = FAIL;
9096 break;
9097 }
9098 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 ga.ga_len += len;
9102 }
9103
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009104 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 arg = skipwhite(arg);
9106 }
9107
9108 if (ret != FAIL && ga.ga_data != NULL)
9109 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01009110 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
9111 {
9112 /* Mark the already saved text as finishing the line, so that what
9113 * follows is displayed on a new line when scrolling back at the
9114 * more prompt. */
9115 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01009116 }
9117
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009119 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009120 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009121 out_flush();
9122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 else if (eap->cmdidx == CMD_echoerr)
9124 {
9125 /* We don't want to abort following commands, restore did_emsg. */
9126 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009127 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 if (!force_abort)
9129 did_emsg = save_did_emsg;
9130 }
9131 else if (eap->cmdidx == CMD_execute)
9132 do_cmdline((char_u *)ga.ga_data,
9133 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
9134 }
9135
9136 ga_clear(&ga);
9137
9138 if (eap->skip)
9139 --emsg_skip;
9140
9141 eap->nextcmd = check_nextcmd(arg);
9142}
9143
9144/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145 * Find window specified by "vp" in tabpage "tp".
9146 */
9147 win_T *
9148find_win_by_nr(
9149 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009150 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009151{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009152 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009153 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009155 if (nr < 0)
9156 return NULL;
9157 if (nr == 0)
9158 return curwin;
9159
Bram Moolenaar29323592016-07-24 22:04:11 +02009160 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
9161 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009162 if (nr >= LOWEST_WIN_ID)
9163 {
9164 if (wp->w_id == nr)
9165 return wp;
9166 }
9167 else if (--nr <= 0)
9168 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02009169 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009170 if (nr >= LOWEST_WIN_ID)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009171 {
9172#ifdef FEAT_TEXT_PROP
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02009173 // check tab-local popup windows
9174 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009175 if (wp->w_id == nr)
9176 return wp;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02009177 // check global popup windows
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009178 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9179 if (wp->w_id == nr)
9180 return wp;
9181#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009182 return NULL;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02009183 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009184 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009185}
9186
9187/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02009188 * Find a window: When using a Window ID in any tab page, when using a number
9189 * in the current tab page.
9190 */
9191 win_T *
9192find_win_by_nr_or_id(typval_T *vp)
9193{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009194 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02009195
9196 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01009197 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02009198 return find_win_by_nr(vp, NULL);
9199}
9200
9201/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009202 * Find window specified by "wvp" in tabpage "tvp".
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009203 * Returns the tab page in 'ptp'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009204 */
9205 win_T *
9206find_tabwin(
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009207 typval_T *wvp, // VAR_UNKNOWN for current window
9208 typval_T *tvp, // VAR_UNKNOWN for current tab page
9209 tabpage_T **ptp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009210{
9211 win_T *wp = NULL;
9212 tabpage_T *tp = NULL;
9213 long n;
9214
9215 if (wvp->v_type != VAR_UNKNOWN)
9216 {
9217 if (tvp->v_type != VAR_UNKNOWN)
9218 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009219 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009220 if (n >= 0)
9221 tp = find_tabpage(n);
9222 }
9223 else
9224 tp = curtab;
9225
9226 if (tp != NULL)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009227 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009228 wp = find_win_by_nr(wvp, tp);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009229 if (wp == NULL && wvp->v_type == VAR_NUMBER
9230 && wvp->vval.v_number != -1)
9231 // A window with the specified number is not found
9232 tp = NULL;
9233 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009234 }
9235 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009236 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009237 wp = curwin;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009238 tp = curtab;
9239 }
9240
9241 if (ptp != NULL)
9242 *ptp = tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009243
9244 return wp;
9245}
9246
9247/*
9248 * getwinvar() and gettabwinvar()
9249 */
9250 void
9251getwinvar(
9252 typval_T *argvars,
9253 typval_T *rettv,
9254 int off) /* 1 for gettabwinvar() */
9255{
9256 win_T *win;
9257 char_u *varname;
9258 dictitem_T *v;
9259 tabpage_T *tp = NULL;
9260 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009261 win_T *oldcurwin;
9262 tabpage_T *oldtabpage;
9263 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009264
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009265 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009266 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009267 else
9268 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009269 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009270 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009271 ++emsg_off;
9272
9273 rettv->v_type = VAR_STRING;
9274 rettv->vval.v_string = NULL;
9275
9276 if (win != NULL && varname != NULL)
9277 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278 /* Set curwin to be our win, temporarily. Also set the tabpage,
9279 * otherwise the window is not valid. Only do this when needed,
9280 * autocommands get blocked. */
9281 need_switch_win = !(tp == curtab && win == curwin);
9282 if (!need_switch_win
9283 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009284 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009285 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009286 {
Bram Moolenaar30567352016-08-27 21:25:44 +02009287 if (varname[1] == NUL)
9288 {
9289 /* get all window-local options in a dict */
9290 dict_T *opts = get_winbuf_options(FALSE);
9291
9292 if (opts != NULL)
9293 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009294 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02009295 done = TRUE;
9296 }
9297 }
9298 else if (get_option_tv(&varname, rettv, 1) == OK)
9299 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009300 done = TRUE;
9301 }
9302 else
9303 {
9304 /* Look up the variable. */
9305 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
9306 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
9307 varname, FALSE);
9308 if (v != NULL)
9309 {
9310 copy_tv(&v->di_tv, rettv);
9311 done = TRUE;
9312 }
9313 }
9314 }
9315
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009316 if (need_switch_win)
9317 /* restore previous notion of curwin */
9318 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009319 }
9320
9321 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
9322 /* use the default return value */
9323 copy_tv(&argvars[off + 2], rettv);
9324
9325 --emsg_off;
9326}
9327
9328/*
9329 * "setwinvar()" and "settabwinvar()" functions
9330 */
9331 void
9332setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
9333{
9334 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009335 win_T *save_curwin;
9336 tabpage_T *save_curtab;
9337 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009338 char_u *varname, *winvarname;
9339 typval_T *varp;
9340 char_u nbuf[NUMBUFLEN];
9341 tabpage_T *tp = NULL;
9342
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01009343 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009344 return;
9345
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009346 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009347 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348 else
9349 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009350 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009351 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009352 varp = &argvars[off + 2];
9353
9354 if (win != NULL && varname != NULL && varp != NULL)
9355 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009356 need_switch_win = !(tp == curtab && win == curwin);
9357 if (!need_switch_win
9358 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009359 {
9360 if (*varname == '&')
9361 {
9362 long numval;
9363 char_u *strval;
9364 int error = FALSE;
9365
9366 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009367 numval = (long)tv_get_number_chk(varp, &error);
9368 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369 if (!error && strval != NULL)
9370 set_option_value(varname, numval, strval, OPT_LOCAL);
9371 }
9372 else
9373 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02009374 winvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009375 if (winvarname != NULL)
9376 {
9377 STRCPY(winvarname, "w:");
9378 STRCPY(winvarname + 2, varname);
9379 set_var(winvarname, varp, TRUE);
9380 vim_free(winvarname);
9381 }
9382 }
9383 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009384 if (need_switch_win)
9385 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009386 }
9387}
9388
9389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
9391 * "arg" points to the "&" or '+' when called, to "option" when returning.
9392 * Returns NULL when no option name found. Otherwise pointer to the char
9393 * after the option name.
9394 */
9395 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009396find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
9398 char_u *p = *arg;
9399
9400 ++p;
9401 if (*p == 'g' && p[1] == ':')
9402 {
9403 *opt_flags = OPT_GLOBAL;
9404 p += 2;
9405 }
9406 else if (*p == 'l' && p[1] == ':')
9407 {
9408 *opt_flags = OPT_LOCAL;
9409 p += 2;
9410 }
9411 else
9412 *opt_flags = 0;
9413
9414 if (!ASCII_ISALPHA(*p))
9415 return NULL;
9416 *arg = p;
9417
9418 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
9419 p += 4; /* termcap option */
9420 else
9421 while (ASCII_ISALPHA(*p))
9422 ++p;
9423 return p;
9424}
9425
9426/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009427 * Return the autoload script name for a function or variable name.
9428 * Returns NULL when out of memory.
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009429 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02009431 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009432autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02009433{
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009434 char_u *p, *q = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009435 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02009436
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009437 // Get the script file name: replace '#' with '/', append ".vim".
Bram Moolenaar964b3742019-05-24 18:54:09 +02009438 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009439 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02009440 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009441 STRCPY(scriptname, "autoload/");
9442 STRCAT(scriptname, name);
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009443 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
9444 q = p, ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009445 *p = '/';
Bram Moolenaar28fc2472019-07-05 22:14:16 +02009446 STRCPY(q, ".vim");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009447 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009448}
9449
9450/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009451 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009452 * Return TRUE if a package was loaded.
9453 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02009454 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009455script_autoload(
9456 char_u *name,
9457 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009458{
9459 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009460 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009461 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009462 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009463
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009464 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00009465 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009466 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009467 return FALSE;
9468
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009469 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009470
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009471 /* Find the name in the list of previously loaded package names. Skip
9472 * "autoload/", it's always the same. */
9473 for (i = 0; i < ga_loaded.ga_len; ++i)
9474 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
9475 break;
9476 if (!reload && i < ga_loaded.ga_len)
9477 ret = FALSE; /* was loaded already */
9478 else
9479 {
9480 /* Remember the name if it wasn't loaded already. */
9481 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
9482 {
9483 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
9484 tofree = NULL;
9485 }
9486
9487 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009488 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009489 ret = TRUE;
9490 }
9491
9492 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009493 return ret;
9494}
9495
Bram Moolenaar661b1822005-07-28 22:36:45 +00009496/*
9497 * Display script name where an item was last set.
9498 * Should only be invoked when 'verbose' is non-zero.
9499 */
9500 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009501last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009502{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009503 char_u *p;
9504
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009505 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009506 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009507 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009508 if (p != NULL)
9509 {
9510 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009511 msg_puts(_("\n\tLast set from "));
9512 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009513 if (script_ctx.sc_lnum > 0)
9514 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009515 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009516 msg_outnum((long)script_ctx.sc_lnum);
9517 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009518 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009519 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009520 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009521 }
9522}
9523
Bram Moolenaar61be3762019-03-19 23:04:17 +01009524/*
Bram Moolenaard7c96872019-06-15 17:12:48 +02009525 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
9526 * v:option_type, and v:option_command.
Bram Moolenaar61be3762019-03-19 23:04:17 +01009527 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009528 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009529reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009530{
9531 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9532 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009533 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
9534 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009535 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02009536 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
Bram Moolenaar53744302015-07-17 17:38:22 +02009537}
9538
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009539/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009540 * Add an assert error to v:errors.
9541 */
9542 void
9543assert_error(garray_T *gap)
9544{
9545 struct vimvar *vp = &vimvars[VV_ERRORS];
9546
9547 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9548 /* Make sure v:errors is a list. */
9549 set_vim_var_list(VV_ERRORS, list_alloc());
9550 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9551}
Bram Moolenaar31988702018-02-13 12:57:42 +01009552/*
9553 * Compare "typ1" and "typ2". Put the result in "typ1".
9554 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009555 int
9556typval_compare(
9557 typval_T *typ1, /* first operand */
9558 typval_T *typ2, /* second operand */
9559 exptype_T type, /* operator */
9560 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009561 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009562{
9563 int i;
9564 varnumber_T n1, n2;
9565 char_u *s1, *s2;
9566 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9567
Bram Moolenaar31988702018-02-13 12:57:42 +01009568 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009569 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009570 /* For "is" a different type always means FALSE, for "notis"
9571 * it means TRUE. */
9572 n1 = (type == TYPE_NEQUAL);
9573 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009574 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9575 {
9576 if (type_is)
9577 {
9578 n1 = (typ1->v_type == typ2->v_type
9579 && typ1->vval.v_blob == typ2->vval.v_blob);
9580 if (type == TYPE_NEQUAL)
9581 n1 = !n1;
9582 }
9583 else if (typ1->v_type != typ2->v_type
9584 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9585 {
9586 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009587 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009588 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009589 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009590 clear_tv(typ1);
9591 return FAIL;
9592 }
9593 else
9594 {
9595 // Compare two Blobs for being equal or unequal.
9596 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9597 if (type == TYPE_NEQUAL)
9598 n1 = !n1;
9599 }
9600 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009601 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9602 {
9603 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009604 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009605 n1 = (typ1->v_type == typ2->v_type
9606 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009607 if (type == TYPE_NEQUAL)
9608 n1 = !n1;
9609 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009610 else if (typ1->v_type != typ2->v_type
9611 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009612 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009613 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009614 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009615 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009616 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009617 clear_tv(typ1);
9618 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009619 }
9620 else
9621 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009622 /* Compare two Lists for being equal or unequal. */
9623 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9624 ic, FALSE);
9625 if (type == TYPE_NEQUAL)
9626 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009627 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009628 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009629
9630 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9631 {
9632 if (type_is)
9633 {
9634 n1 = (typ1->v_type == typ2->v_type
9635 && typ1->vval.v_dict == typ2->vval.v_dict);
9636 if (type == TYPE_NEQUAL)
9637 n1 = !n1;
9638 }
9639 else if (typ1->v_type != typ2->v_type
9640 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9641 {
9642 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009643 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009644 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009645 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009646 clear_tv(typ1);
9647 return FAIL;
9648 }
9649 else
9650 {
9651 /* Compare two Dictionaries for being equal or unequal. */
9652 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9653 ic, FALSE);
9654 if (type == TYPE_NEQUAL)
9655 n1 = !n1;
9656 }
9657 }
9658
9659 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9660 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9661 {
9662 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009664 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009665 clear_tv(typ1);
9666 return FAIL;
9667 }
9668 if ((typ1->v_type == VAR_PARTIAL
9669 && typ1->vval.v_partial == NULL)
9670 || (typ2->v_type == VAR_PARTIAL
9671 && typ2->vval.v_partial == NULL))
9672 /* when a partial is NULL assume not equal */
9673 n1 = FALSE;
9674 else if (type_is)
9675 {
9676 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9677 /* strings are considered the same if their value is
9678 * the same */
9679 n1 = tv_equal(typ1, typ2, ic, FALSE);
9680 else if (typ1->v_type == VAR_PARTIAL
9681 && typ2->v_type == VAR_PARTIAL)
9682 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9683 else
9684 n1 = FALSE;
9685 }
9686 else
9687 n1 = tv_equal(typ1, typ2, ic, FALSE);
9688 if (type == TYPE_NEQUAL)
9689 n1 = !n1;
9690 }
9691
9692#ifdef FEAT_FLOAT
9693 /*
9694 * If one of the two variables is a float, compare as a float.
9695 * When using "=~" or "!~", always compare as string.
9696 */
9697 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9698 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9699 {
9700 float_T f1, f2;
9701
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009702 f1 = tv_get_float(typ1);
9703 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009704 n1 = FALSE;
9705 switch (type)
9706 {
9707 case TYPE_EQUAL: n1 = (f1 == f2); break;
9708 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9709 case TYPE_GREATER: n1 = (f1 > f2); break;
9710 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9711 case TYPE_SMALLER: n1 = (f1 < f2); break;
9712 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9713 case TYPE_UNKNOWN:
9714 case TYPE_MATCH:
9715 case TYPE_NOMATCH: break; /* avoid gcc warning */
9716 }
9717 }
9718#endif
9719
9720 /*
9721 * If one of the two variables is a number, compare as a number.
9722 * When using "=~" or "!~", always compare as string.
9723 */
9724 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9725 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9726 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009727 n1 = tv_get_number(typ1);
9728 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009729 switch (type)
9730 {
9731 case TYPE_EQUAL: n1 = (n1 == n2); break;
9732 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9733 case TYPE_GREATER: n1 = (n1 > n2); break;
9734 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9735 case TYPE_SMALLER: n1 = (n1 < n2); break;
9736 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9737 case TYPE_UNKNOWN:
9738 case TYPE_MATCH:
9739 case TYPE_NOMATCH: break; /* avoid gcc warning */
9740 }
9741 }
9742 else
9743 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009744 s1 = tv_get_string_buf(typ1, buf1);
9745 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009746 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9747 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9748 else
9749 i = 0;
9750 n1 = FALSE;
9751 switch (type)
9752 {
9753 case TYPE_EQUAL: n1 = (i == 0); break;
9754 case TYPE_NEQUAL: n1 = (i != 0); break;
9755 case TYPE_GREATER: n1 = (i > 0); break;
9756 case TYPE_GEQUAL: n1 = (i >= 0); break;
9757 case TYPE_SMALLER: n1 = (i < 0); break;
9758 case TYPE_SEQUAL: n1 = (i <= 0); break;
9759
9760 case TYPE_MATCH:
9761 case TYPE_NOMATCH:
9762 n1 = pattern_match(s2, s1, ic);
9763 if (type == TYPE_NOMATCH)
9764 n1 = !n1;
9765 break;
9766
9767 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9768 }
9769 }
9770 clear_tv(typ1);
9771 typ1->v_type = VAR_NUMBER;
9772 typ1->vval.v_number = n1;
9773
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009774 return OK;
9775}
9776
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009777 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009778typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009779{
9780 char_u *tofree;
9781 char_u numbuf[NUMBUFLEN];
9782 char_u *ret = NULL;
9783
9784 if (arg == NULL)
9785 return vim_strsave((char_u *)"(does not exist)");
9786 ret = tv2string(arg, &tofree, numbuf, 0);
9787 /* Make a copy if we have a value but it's not in allocated memory. */
9788 if (ret != NULL && tofree == NULL)
9789 ret = vim_strsave(ret);
9790 return ret;
9791}
9792
9793 int
9794var_exists(char_u *var)
9795{
9796 char_u *name;
9797 char_u *tofree;
9798 typval_T tv;
9799 int len = 0;
9800 int n = FALSE;
9801
9802 /* get_name_len() takes care of expanding curly braces */
9803 name = var;
9804 len = get_name_len(&var, &tofree, TRUE, FALSE);
9805 if (len > 0)
9806 {
9807 if (tofree != NULL)
9808 name = tofree;
9809 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9810 if (n)
9811 {
9812 /* handle d.key, l[idx], f(expr) */
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02009813 n = (handle_subscript(&var, &tv, TRUE, FALSE, name, &name) == OK);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009814 if (n)
9815 clear_tv(&tv);
9816 }
9817 }
9818 if (*var != NUL)
9819 n = FALSE;
9820
9821 vim_free(tofree);
9822 return n;
9823}
9824
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825#endif /* FEAT_EVAL */
9826
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009828#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829
Bram Moolenaar4f974752019-02-17 17:44:42 +01009830#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831/*
9832 * Functions for ":8" filename modifier: get 8.3 version of a filename.
9833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834
9835/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009836 * Get the short path (8.3) for the filename in "fnamep".
9837 * Only works for a valid file name.
9838 * When the path gets longer "fnamep" is changed and the allocated buffer
9839 * is put in "bufp".
9840 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
9841 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 */
9843 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009844get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009846 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 char_u *newbuf;
9848
9849 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009850 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 if (l > len - 1)
9852 {
9853 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009854 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 newbuf = vim_strnsave(*fnamep, l);
9856 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009857 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858
9859 vim_free(*bufp);
9860 *fnamep = *bufp = newbuf;
9861
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009862 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009863 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 }
9865
9866 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009867 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868}
9869
9870/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009871 * Get the short path (8.3) for the filename in "fname". The converted
9872 * path is returned in "bufp".
9873 *
9874 * Some of the directories specified in "fname" may not exist. This function
9875 * will shorten the existing directories at the beginning of the path and then
9876 * append the remaining non-existing path.
9877 *
9878 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02009879 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009880 * bufp - Pointer to an allocated buffer for the filename.
9881 * fnamelen - Length of the filename pointed to by fname
9882 *
9883 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 */
9885 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009886shortpath_for_invalid_fname(
9887 char_u **fname,
9888 char_u **bufp,
9889 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009891 char_u *short_fname, *save_fname, *pbuf_unused;
9892 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009894 int old_len, len;
9895 int new_len, sfx_len;
9896 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897
9898 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009899 old_len = *fnamelen;
9900 save_fname = vim_strnsave(*fname, old_len);
9901 pbuf_unused = NULL;
9902 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009904 endp = save_fname + old_len - 1; /* Find the end of the copy */
9905 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009907 /*
9908 * Try shortening the supplied path till it succeeds by removing one
9909 * directory at a time from the tail of the path.
9910 */
9911 len = 0;
9912 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009914 /* go back one path-separator */
9915 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
9916 --endp;
9917 if (endp <= save_fname)
9918 break; /* processed the complete path */
9919
9920 /*
9921 * Replace the path separator with a NUL and try to shorten the
9922 * resulting path.
9923 */
9924 ch = *endp;
9925 *endp = 0;
9926 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009927 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009928 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
9929 {
9930 retval = FAIL;
9931 goto theend;
9932 }
9933 *endp = ch; /* preserve the string */
9934
9935 if (len > 0)
9936 break; /* successfully shortened the path */
9937
9938 /* failed to shorten the path. Skip the path separator */
9939 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 }
9941
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009942 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009944 /*
9945 * Succeeded in shortening the path. Now concatenate the shortened
9946 * path with the remaining path at the tail.
9947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009949 /* Compute the length of the new path. */
9950 sfx_len = (int)(save_endp - endp) + 1;
9951 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009953 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009955 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009957 /* There is not enough space in the currently allocated string,
9958 * copy it to a buffer big enough. */
9959 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009961 {
9962 retval = FAIL;
9963 goto theend;
9964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 }
9966 else
9967 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009968 /* Transfer short_fname to the main buffer (it's big enough),
9969 * unless get_short_pathname() did its work in-place. */
9970 *fname = *bufp = save_fname;
9971 if (short_fname != save_fname)
9972 vim_strncpy(save_fname, short_fname, len);
9973 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009975
9976 /* concat the not-shortened part of the path */
9977 vim_strncpy(*fname + len, endp, sfx_len);
9978 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009980
9981theend:
9982 vim_free(pbuf_unused);
9983 vim_free(save_fname);
9984
9985 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986}
9987
9988/*
9989 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00009990 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 */
9992 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009993shortpath_for_partial(
9994 char_u **fnamep,
9995 char_u **bufp,
9996 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997{
9998 int sepcount, len, tflen;
9999 char_u *p;
10000 char_u *pbuf, *tfname;
10001 int hasTilde;
10002
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010003 /* Count up the path separators from the RHS.. so we know which part
10004 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010006 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 if (vim_ispathsep(*p))
10008 ++sepcount;
10009
10010 /* Need full path first (use expand_env() to remove a "~/") */
10011 hasTilde = (**fnamep == '~');
10012 if (hasTilde)
10013 pbuf = tfname = expand_env_save(*fnamep);
10014 else
10015 pbuf = tfname = FullName_save(*fnamep, FALSE);
10016
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010017 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010019 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10020 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021
10022 if (len == 0)
10023 {
10024 /* Don't have a valid filename, so shorten the rest of the
10025 * path if we can. This CAN give us invalid 8.3 filenames, but
10026 * there's not a lot of point in guessing what it might be.
10027 */
10028 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010029 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10030 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 }
10032
10033 /* Count the paths backward to find the beginning of the desired string. */
10034 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010035 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010036 if (has_mbyte)
10037 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 if (vim_ispathsep(*p))
10039 {
10040 if (sepcount == 0 || (hasTilde && sepcount == 1))
10041 break;
10042 else
10043 sepcount --;
10044 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 if (hasTilde)
10047 {
10048 --p;
10049 if (p >= tfname)
10050 *p = '~';
10051 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010052 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 }
10054 else
10055 ++p;
10056
10057 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10058 vim_free(*bufp);
10059 *fnamelen = (int)STRLEN(p);
10060 *bufp = pbuf;
10061 *fnamep = p;
10062
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010063 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010065#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066
10067/*
10068 * Adjust a filename, according to a string of modifiers.
10069 * *fnamep must be NUL terminated when called. When returning, the length is
10070 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010071 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 * When there is an error, *fnamep is set to NULL.
10073 */
10074 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010075modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010076 char_u *src, // string with modifiers
10077 int tilde_file, // "~" is a file name, not $HOME
10078 int *usedlen, // characters after src that are used
10079 char_u **fnamep, // file name so far
10080 char_u **bufp, // buffer for allocated file name or NULL
10081 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082{
10083 int valid = 0;
10084 char_u *tail;
10085 char_u *s, *p, *pbuf;
10086 char_u dirname[MAXPATHL];
10087 int c;
10088 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010089#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010090 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 int has_shortname = 0;
10092#endif
10093
10094repeat:
10095 /* ":p" - full path/file_name */
10096 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10097 {
10098 has_fullname = 1;
10099
10100 valid |= VALID_PATH;
10101 *usedlen += 2;
10102
10103 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10104 if ((*fnamep)[0] == '~'
10105#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10106 && ((*fnamep)[1] == '/'
10107# ifdef BACKSLASH_IN_FILENAME
10108 || (*fnamep)[1] == '\\'
10109# endif
10110 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010112 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 )
10114 {
10115 *fnamep = expand_env_save(*fnamep);
10116 vim_free(*bufp); /* free any allocated file name */
10117 *bufp = *fnamep;
10118 if (*fnamep == NULL)
10119 return -1;
10120 }
10121
10122 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010123 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 {
10125 if (vim_ispathsep(*p)
10126 && p[1] == '.'
10127 && (p[2] == NUL
10128 || vim_ispathsep(p[2])
10129 || (p[2] == '.'
10130 && (p[3] == NUL || vim_ispathsep(p[3])))))
10131 break;
10132 }
10133
10134 /* FullName_save() is slow, don't use it when not needed. */
10135 if (*p != NUL || !vim_isAbsName(*fnamep))
10136 {
10137 *fnamep = FullName_save(*fnamep, *p != NUL);
10138 vim_free(*bufp); /* free any allocated file name */
10139 *bufp = *fnamep;
10140 if (*fnamep == NULL)
10141 return -1;
10142 }
10143
Bram Moolenaar4f974752019-02-17 17:44:42 +010010144#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010145# if _WIN32_WINNT >= 0x0500
10146 if (vim_strchr(*fnamep, '~') != NULL)
10147 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010148 // Expand 8.3 filename to full path. Needed to make sure the same
10149 // file does not have two different names.
10150 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10151 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10152 WCHAR buf[_MAX_PATH];
10153
10154 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010155 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010156 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010157 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010158 char_u *p = utf16_to_enc(buf, NULL);
10159
10160 if (p != NULL)
10161 {
10162 vim_free(*bufp); // free any allocated file name
10163 *bufp = *fnamep = p;
10164 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010165 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010166 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010167 }
10168 }
10169# endif
10170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 /* Append a path separator to a directory. */
10172 if (mch_isdir(*fnamep))
10173 {
10174 /* Make room for one or two extra characters. */
10175 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10176 vim_free(*bufp); /* free any allocated file name */
10177 *bufp = *fnamep;
10178 if (*fnamep == NULL)
10179 return -1;
10180 add_pathsep(*fnamep);
10181 }
10182 }
10183
10184 /* ":." - path relative to the current directory */
10185 /* ":~" - path relative to the home directory */
10186 /* ":8" - shortname path - postponed till after */
10187 while (src[*usedlen] == ':'
10188 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10189 {
10190 *usedlen += 2;
10191 if (c == '8')
10192 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010193#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 has_shortname = 1; /* Postpone this. */
10195#endif
10196 continue;
10197 }
10198 pbuf = NULL;
10199 /* Need full path first (use expand_env() to remove a "~/") */
10200 if (!has_fullname)
10201 {
10202 if (c == '.' && **fnamep == '~')
10203 p = pbuf = expand_env_save(*fnamep);
10204 else
10205 p = pbuf = FullName_save(*fnamep, FALSE);
10206 }
10207 else
10208 p = *fnamep;
10209
10210 has_fullname = 0;
10211
10212 if (p != NULL)
10213 {
10214 if (c == '.')
10215 {
10216 mch_dirname(dirname, MAXPATHL);
10217 s = shorten_fname(p, dirname);
10218 if (s != NULL)
10219 {
10220 *fnamep = s;
10221 if (pbuf != NULL)
10222 {
10223 vim_free(*bufp); /* free any allocated file name */
10224 *bufp = pbuf;
10225 pbuf = NULL;
10226 }
10227 }
10228 }
10229 else
10230 {
10231 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10232 /* Only replace it when it starts with '~' */
10233 if (*dirname == '~')
10234 {
10235 s = vim_strsave(dirname);
10236 if (s != NULL)
10237 {
10238 *fnamep = s;
10239 vim_free(*bufp);
10240 *bufp = s;
10241 }
10242 }
10243 }
10244 vim_free(pbuf);
10245 }
10246 }
10247
10248 tail = gettail(*fnamep);
10249 *fnamelen = (int)STRLEN(*fnamep);
10250
10251 /* ":h" - head, remove "/file_name", can be repeated */
10252 /* Don't remove the first "/" or "c:\" */
10253 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10254 {
10255 valid |= VALID_HEAD;
10256 *usedlen += 2;
10257 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010258 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010259 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 *fnamelen = (int)(tail - *fnamep);
10261#ifdef VMS
10262 if (*fnamelen > 0)
10263 *fnamelen += 1; /* the path separator is part of the path */
10264#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010265 if (*fnamelen == 0)
10266 {
10267 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10268 p = vim_strsave((char_u *)".");
10269 if (p == NULL)
10270 return -1;
10271 vim_free(*bufp);
10272 *bufp = *fnamep = tail = p;
10273 *fnamelen = 1;
10274 }
10275 else
10276 {
10277 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010278 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 }
10281
10282 /* ":8" - shortname */
10283 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10284 {
10285 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010286#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 has_shortname = 1;
10288#endif
10289 }
10290
Bram Moolenaar4f974752019-02-17 17:44:42 +010010291#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010292 /*
10293 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 */
10295 if (has_shortname)
10296 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010297 /* Copy the string if it is shortened by :h and when it wasn't copied
10298 * yet, because we are going to change it in place. Avoids changing
10299 * the buffer name for "%:8". */
10300 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 {
10302 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010303 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 return -1;
10305 vim_free(*bufp);
10306 *bufp = *fnamep = p;
10307 }
10308
10309 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010310 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 if (!has_fullname && !vim_isAbsName(*fnamep))
10312 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010313 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 return -1;
10315 }
10316 else
10317 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010318 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319
Bram Moolenaardc935552011-08-17 15:23:23 +020010320 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010322 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 return -1;
10324
10325 if (l == 0)
10326 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010327 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010329 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 return -1;
10331 }
10332 *fnamelen = l;
10333 }
10334 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010335#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336
10337 /* ":t" - tail, just the basename */
10338 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10339 {
10340 *usedlen += 2;
10341 *fnamelen -= (int)(tail - *fnamep);
10342 *fnamep = tail;
10343 }
10344
10345 /* ":e" - extension, can be repeated */
10346 /* ":r" - root, without extension, can be repeated */
10347 while (src[*usedlen] == ':'
10348 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10349 {
10350 /* find a '.' in the tail:
10351 * - for second :e: before the current fname
10352 * - otherwise: The last '.'
10353 */
10354 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10355 s = *fnamep - 2;
10356 else
10357 s = *fnamep + *fnamelen - 1;
10358 for ( ; s > tail; --s)
10359 if (s[0] == '.')
10360 break;
10361 if (src[*usedlen + 1] == 'e') /* :e */
10362 {
10363 if (s > tail)
10364 {
10365 *fnamelen += (int)(*fnamep - (s + 1));
10366 *fnamep = s + 1;
10367#ifdef VMS
10368 /* cut version from the extension */
10369 s = *fnamep + *fnamelen - 1;
10370 for ( ; s > *fnamep; --s)
10371 if (s[0] == ';')
10372 break;
10373 if (s > *fnamep)
10374 *fnamelen = s - *fnamep;
10375#endif
10376 }
10377 else if (*fnamep <= tail)
10378 *fnamelen = 0;
10379 }
10380 else /* :r */
10381 {
10382 if (s > tail) /* remove one extension */
10383 *fnamelen = (int)(s - *fnamep);
10384 }
10385 *usedlen += 2;
10386 }
10387
10388 /* ":s?pat?foo?" - substitute */
10389 /* ":gs?pat?foo?" - global substitute */
10390 if (src[*usedlen] == ':'
10391 && (src[*usedlen + 1] == 's'
10392 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10393 {
10394 char_u *str;
10395 char_u *pat;
10396 char_u *sub;
10397 int sep;
10398 char_u *flags;
10399 int didit = FALSE;
10400
10401 flags = (char_u *)"";
10402 s = src + *usedlen + 2;
10403 if (src[*usedlen + 1] == 'g')
10404 {
10405 flags = (char_u *)"g";
10406 ++s;
10407 }
10408
10409 sep = *s++;
10410 if (sep)
10411 {
10412 /* find end of pattern */
10413 p = vim_strchr(s, sep);
10414 if (p != NULL)
10415 {
10416 pat = vim_strnsave(s, (int)(p - s));
10417 if (pat != NULL)
10418 {
10419 s = p + 1;
10420 /* find end of substitution */
10421 p = vim_strchr(s, sep);
10422 if (p != NULL)
10423 {
10424 sub = vim_strnsave(s, (int)(p - s));
10425 str = vim_strnsave(*fnamep, *fnamelen);
10426 if (sub != NULL && str != NULL)
10427 {
10428 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010429 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 if (s != NULL)
10431 {
10432 *fnamep = s;
10433 *fnamelen = (int)STRLEN(s);
10434 vim_free(*bufp);
10435 *bufp = s;
10436 didit = TRUE;
10437 }
10438 }
10439 vim_free(sub);
10440 vim_free(str);
10441 }
10442 vim_free(pat);
10443 }
10444 }
10445 /* after using ":s", repeat all the modifiers */
10446 if (didit)
10447 goto repeat;
10448 }
10449 }
10450
Bram Moolenaar26df0922014-02-23 23:39:13 +010010451 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10452 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010453 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010454 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010455 if (c != NUL)
10456 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010457 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010458 if (c != NUL)
10459 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010460 if (p == NULL)
10461 return -1;
10462 vim_free(*bufp);
10463 *bufp = *fnamep = p;
10464 *fnamelen = (int)STRLEN(p);
10465 *usedlen += 2;
10466 }
10467
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 return valid;
10469}
10470
10471/*
10472 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010473 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 * "flags" can be "g" to do a global substitute.
10475 * Returns an allocated string, NULL for error.
10476 */
10477 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010478do_string_sub(
10479 char_u *str,
10480 char_u *pat,
10481 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010482 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010483 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484{
10485 int sublen;
10486 regmatch_T regmatch;
10487 int i;
10488 int do_all;
10489 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010490 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 garray_T ga;
10492 char_u *ret;
10493 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010494 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495
10496 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10497 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010498 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499
10500 ga_init2(&ga, 1, 200);
10501
10502 do_all = (flags[0] == 'g');
10503
10504 regmatch.rm_ic = p_ic;
10505 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10506 if (regmatch.regprog != NULL)
10507 {
10508 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010509 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10511 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010512 /* Skip empty match except for first match. */
10513 if (regmatch.startp[0] == regmatch.endp[0])
10514 {
10515 if (zero_width == regmatch.startp[0])
10516 {
10517 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010518 i = MB_PTR2LEN(tail);
10519 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10520 (size_t)i);
10521 ga.ga_len += i;
10522 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010523 continue;
10524 }
10525 zero_width = regmatch.startp[0];
10526 }
10527
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 /*
10529 * Get some space for a temporary buffer to do the substitution
10530 * into. It will contain:
10531 * - The text up to where the match is.
10532 * - The substituted text.
10533 * - The text after the match.
10534 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010535 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010536 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10538 {
10539 ga_clear(&ga);
10540 break;
10541 }
10542
10543 /* copy the text up to where the match is */
10544 i = (int)(regmatch.startp[0] - tail);
10545 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10546 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010547 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 + ga.ga_len + i, TRUE, TRUE, FALSE);
10549 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010550 tail = regmatch.endp[0];
10551 if (*tail == NUL)
10552 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 if (!do_all)
10554 break;
10555 }
10556
10557 if (ga.ga_data != NULL)
10558 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10559
Bram Moolenaar473de612013-06-08 18:19:48 +020010560 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 }
10562
10563 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10564 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010565 if (p_cpo == empty_option)
10566 p_cpo = save_cpo;
10567 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010568 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010569 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570
10571 return ret;
10572}
10573
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010574 static int
10575filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10576{
10577 typval_T rettv;
10578 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010579 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010580
10581 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10582 argv[0] = vimvars[VV_KEY].vv_tv;
10583 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010584 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10585 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010586 if (map)
10587 {
10588 /* map(): replace the list item value */
10589 clear_tv(tv);
10590 rettv.v_lock = 0;
10591 *tv = rettv;
10592 }
10593 else
10594 {
10595 int error = FALSE;
10596
10597 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010598 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010599 clear_tv(&rettv);
10600 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010601 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010602 if (error)
10603 goto theend;
10604 }
10605 retval = OK;
10606theend:
10607 clear_tv(&vimvars[VV_VAL].vv_tv);
10608 return retval;
10609}
10610
10611
10612/*
10613 * Implementation of map() and filter().
10614 */
10615 void
10616filter_map(typval_T *argvars, typval_T *rettv, int map)
10617{
10618 typval_T *expr;
10619 listitem_T *li, *nli;
10620 list_T *l = NULL;
10621 dictitem_T *di;
10622 hashtab_T *ht;
10623 hashitem_T *hi;
10624 dict_T *d = NULL;
10625 typval_T save_val;
10626 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010627 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010628 int rem;
10629 int todo;
10630 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10631 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10632 : N_("filter() argument"));
10633 int save_did_emsg;
10634 int idx = 0;
10635
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010636 if (argvars[0].v_type == VAR_BLOB)
10637 {
10638 if ((b = argvars[0].vval.v_blob) == NULL)
10639 return;
10640 }
10641 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010642 {
10643 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010644 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010645 return;
10646 }
10647 else if (argvars[0].v_type == VAR_DICT)
10648 {
10649 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010650 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010651 return;
10652 }
10653 else
10654 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010655 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010656 return;
10657 }
10658
10659 expr = &argvars[1];
10660 /* On type errors, the preceding call has already displayed an error
10661 * message. Avoid a misleading error message for an empty string that
10662 * was not passed as argument. */
10663 if (expr->v_type != VAR_UNKNOWN)
10664 {
10665 prepare_vimvar(VV_VAL, &save_val);
10666
10667 /* We reset "did_emsg" to be able to detect whether an error
10668 * occurred during evaluation of the expression. */
10669 save_did_emsg = did_emsg;
10670 did_emsg = FALSE;
10671
10672 prepare_vimvar(VV_KEY, &save_key);
10673 if (argvars[0].v_type == VAR_DICT)
10674 {
10675 vimvars[VV_KEY].vv_type = VAR_STRING;
10676
10677 ht = &d->dv_hashtab;
10678 hash_lock(ht);
10679 todo = (int)ht->ht_used;
10680 for (hi = ht->ht_array; todo > 0; ++hi)
10681 {
10682 if (!HASHITEM_EMPTY(hi))
10683 {
10684 int r;
10685
10686 --todo;
10687 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010688 if (map && (var_check_lock(di->di_tv.v_lock,
10689 arg_errmsg, TRUE)
10690 || var_check_ro(di->di_flags,
10691 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010692 break;
10693 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10694 r = filter_map_one(&di->di_tv, expr, map, &rem);
10695 clear_tv(&vimvars[VV_KEY].vv_tv);
10696 if (r == FAIL || did_emsg)
10697 break;
10698 if (!map && rem)
10699 {
10700 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10701 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10702 break;
10703 dictitem_remove(d, di);
10704 }
10705 }
10706 }
10707 hash_unlock(ht);
10708 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010709 else if (argvars[0].v_type == VAR_BLOB)
10710 {
10711 int i;
10712 typval_T tv;
10713
10714 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10715 for (i = 0; i < b->bv_ga.ga_len; i++)
10716 {
10717 tv.v_type = VAR_NUMBER;
10718 tv.vval.v_number = blob_get(b, i);
10719 vimvars[VV_KEY].vv_nr = idx;
10720 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10721 break;
10722 if (tv.v_type != VAR_NUMBER)
10723 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010724 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010725 return;
10726 }
10727 tv.v_type = VAR_NUMBER;
10728 blob_set(b, i, tv.vval.v_number);
10729 if (!map && rem)
10730 {
10731 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10732
10733 mch_memmove(p + idx, p + i + 1,
10734 (size_t)b->bv_ga.ga_len - i - 1);
10735 --b->bv_ga.ga_len;
10736 --i;
10737 }
10738 }
10739 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010740 else
10741 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010742 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010743 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10744
10745 for (li = l->lv_first; li != NULL; li = nli)
10746 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010747 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010748 break;
10749 nli = li->li_next;
10750 vimvars[VV_KEY].vv_nr = idx;
10751 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10752 || did_emsg)
10753 break;
10754 if (!map && rem)
10755 listitem_remove(l, li);
10756 ++idx;
10757 }
10758 }
10759
10760 restore_vimvar(VV_KEY, &save_key);
10761 restore_vimvar(VV_VAL, &save_val);
10762
10763 did_emsg |= save_did_emsg;
10764 }
10765
10766 copy_tv(&argvars[0], rettv);
10767}
10768
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */