blob: 50b1b2aef932f9d1d5bd8e8b757a556a50cb4353 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_listreq = N_("E714: List required");
103static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200368 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200369 {VV_NAME("option_new", VAR_STRING), VV_RO},
370 {VV_NAME("option_old", VAR_STRING), VV_RO},
371 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100372 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 {VV_NAME("false", VAR_SPECIAL), VV_RO},
374 {VV_NAME("true", VAR_SPECIAL), VV_RO},
375 {VV_NAME("null", VAR_SPECIAL), VV_RO},
376 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
434static long list_len(list_T *l);
435static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
436static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
437static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
438static long list_find_nr(list_T *l, long idx, int *errorp);
439static long list_idx_of_item(list_T *l, listitem_T *item);
440static int list_append_number(list_T *l, varnumber_T n);
441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
454static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
455static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
456static char_u *string_quote(char_u *str, int function);
457static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
458static int find_internal_func(char_u *name);
459static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
460static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static void emsg_funcname(char *ermsg, char_u *name);
462static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_abs(typval_T *argvars, typval_T *rettv);
466static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_add(typval_T *argvars, typval_T *rettv);
469static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
470static void f_and(typval_T *argvars, typval_T *rettv);
471static void f_append(typval_T *argvars, typval_T *rettv);
472static void f_argc(typval_T *argvars, typval_T *rettv);
473static void f_argidx(typval_T *argvars, typval_T *rettv);
474static void f_arglistid(typval_T *argvars, typval_T *rettv);
475static void f_argv(typval_T *argvars, typval_T *rettv);
476static void f_assert_equal(typval_T *argvars, typval_T *rettv);
477static void f_assert_exception(typval_T *argvars, typval_T *rettv);
478static void f_assert_fails(typval_T *argvars, typval_T *rettv);
479static void f_assert_false(typval_T *argvars, typval_T *rettv);
480static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100482static void f_asin(typval_T *argvars, typval_T *rettv);
483static void f_atan(typval_T *argvars, typval_T *rettv);
484static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100486static void f_browse(typval_T *argvars, typval_T *rettv);
487static void f_browsedir(typval_T *argvars, typval_T *rettv);
488static void f_bufexists(typval_T *argvars, typval_T *rettv);
489static void f_buflisted(typval_T *argvars, typval_T *rettv);
490static void f_bufloaded(typval_T *argvars, typval_T *rettv);
491static void f_bufname(typval_T *argvars, typval_T *rettv);
492static void f_bufnr(typval_T *argvars, typval_T *rettv);
493static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
494static void f_byte2line(typval_T *argvars, typval_T *rettv);
495static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
496static void f_byteidx(typval_T *argvars, typval_T *rettv);
497static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
498static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100500static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100502#ifdef FEAT_CHANNEL
503static void f_ch_open(typval_T *argvars, typval_T *rettv);
504static void f_ch_close(typval_T *argvars, typval_T *rettv);
505static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
506static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
507#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100508static void f_changenr(typval_T *argvars, typval_T *rettv);
509static void f_char2nr(typval_T *argvars, typval_T *rettv);
510static void f_cindent(typval_T *argvars, typval_T *rettv);
511static void f_clearmatches(typval_T *argvars, typval_T *rettv);
512static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000513#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100514static void f_complete(typval_T *argvars, typval_T *rettv);
515static void f_complete_add(typval_T *argvars, typval_T *rettv);
516static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000517#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100518static void f_confirm(typval_T *argvars, typval_T *rettv);
519static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000520#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_cos(typval_T *argvars, typval_T *rettv);
522static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_count(typval_T *argvars, typval_T *rettv);
525static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
526static void f_cursor(typval_T *argsvars, typval_T *rettv);
527static void f_deepcopy(typval_T *argvars, typval_T *rettv);
528static void f_delete(typval_T *argvars, typval_T *rettv);
529static void f_did_filetype(typval_T *argvars, typval_T *rettv);
530static void f_diff_filler(typval_T *argvars, typval_T *rettv);
531static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
532static void f_empty(typval_T *argvars, typval_T *rettv);
533static void f_escape(typval_T *argvars, typval_T *rettv);
534static void f_eval(typval_T *argvars, typval_T *rettv);
535static void f_eventhandler(typval_T *argvars, typval_T *rettv);
536static void f_executable(typval_T *argvars, typval_T *rettv);
537static void f_exepath(typval_T *argvars, typval_T *rettv);
538static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200539#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200541#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_expand(typval_T *argvars, typval_T *rettv);
543static void f_extend(typval_T *argvars, typval_T *rettv);
544static void f_feedkeys(typval_T *argvars, typval_T *rettv);
545static void f_filereadable(typval_T *argvars, typval_T *rettv);
546static void f_filewritable(typval_T *argvars, typval_T *rettv);
547static void f_filter(typval_T *argvars, typval_T *rettv);
548static void f_finddir(typval_T *argvars, typval_T *rettv);
549static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000550#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_float2nr(typval_T *argvars, typval_T *rettv);
552static void f_floor(typval_T *argvars, typval_T *rettv);
553static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000554#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100555static void f_fnameescape(typval_T *argvars, typval_T *rettv);
556static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
557static void f_foldclosed(typval_T *argvars, typval_T *rettv);
558static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
559static void f_foldlevel(typval_T *argvars, typval_T *rettv);
560static void f_foldtext(typval_T *argvars, typval_T *rettv);
561static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
562static void f_foreground(typval_T *argvars, typval_T *rettv);
563static void f_function(typval_T *argvars, typval_T *rettv);
564static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
565static void f_get(typval_T *argvars, typval_T *rettv);
566static void f_getbufline(typval_T *argvars, typval_T *rettv);
567static void f_getbufvar(typval_T *argvars, typval_T *rettv);
568static void f_getchar(typval_T *argvars, typval_T *rettv);
569static void f_getcharmod(typval_T *argvars, typval_T *rettv);
570static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
571static void f_getcmdline(typval_T *argvars, typval_T *rettv);
572static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
573static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
574static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
575static void f_getcwd(typval_T *argvars, typval_T *rettv);
576static void f_getfontname(typval_T *argvars, typval_T *rettv);
577static void f_getfperm(typval_T *argvars, typval_T *rettv);
578static void f_getfsize(typval_T *argvars, typval_T *rettv);
579static void f_getftime(typval_T *argvars, typval_T *rettv);
580static void f_getftype(typval_T *argvars, typval_T *rettv);
581static void f_getline(typval_T *argvars, typval_T *rettv);
582static void f_getmatches(typval_T *argvars, typval_T *rettv);
583static void f_getpid(typval_T *argvars, typval_T *rettv);
584static void f_getcurpos(typval_T *argvars, typval_T *rettv);
585static void f_getpos(typval_T *argvars, typval_T *rettv);
586static void f_getqflist(typval_T *argvars, typval_T *rettv);
587static void f_getreg(typval_T *argvars, typval_T *rettv);
588static void f_getregtype(typval_T *argvars, typval_T *rettv);
589static void f_gettabvar(typval_T *argvars, typval_T *rettv);
590static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
591static void f_getwinposx(typval_T *argvars, typval_T *rettv);
592static void f_getwinposy(typval_T *argvars, typval_T *rettv);
593static void f_getwinvar(typval_T *argvars, typval_T *rettv);
594static void f_glob(typval_T *argvars, typval_T *rettv);
595static void f_globpath(typval_T *argvars, typval_T *rettv);
596static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
597static void f_has(typval_T *argvars, typval_T *rettv);
598static void f_has_key(typval_T *argvars, typval_T *rettv);
599static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
600static void f_hasmapto(typval_T *argvars, typval_T *rettv);
601static void f_histadd(typval_T *argvars, typval_T *rettv);
602static void f_histdel(typval_T *argvars, typval_T *rettv);
603static void f_histget(typval_T *argvars, typval_T *rettv);
604static void f_histnr(typval_T *argvars, typval_T *rettv);
605static void f_hlID(typval_T *argvars, typval_T *rettv);
606static void f_hlexists(typval_T *argvars, typval_T *rettv);
607static void f_hostname(typval_T *argvars, typval_T *rettv);
608static void f_iconv(typval_T *argvars, typval_T *rettv);
609static void f_indent(typval_T *argvars, typval_T *rettv);
610static void f_index(typval_T *argvars, typval_T *rettv);
611static void f_input(typval_T *argvars, typval_T *rettv);
612static void f_inputdialog(typval_T *argvars, typval_T *rettv);
613static void f_inputlist(typval_T *argvars, typval_T *rettv);
614static void f_inputrestore(typval_T *argvars, typval_T *rettv);
615static void f_inputsave(typval_T *argvars, typval_T *rettv);
616static void f_inputsecret(typval_T *argvars, typval_T *rettv);
617static void f_insert(typval_T *argvars, typval_T *rettv);
618static void f_invert(typval_T *argvars, typval_T *rettv);
619static void f_isdirectory(typval_T *argvars, typval_T *rettv);
620static void f_islocked(typval_T *argvars, typval_T *rettv);
621static void f_items(typval_T *argvars, typval_T *rettv);
622static void f_join(typval_T *argvars, typval_T *rettv);
623static void f_jsondecode(typval_T *argvars, typval_T *rettv);
624static void f_jsonencode(typval_T *argvars, typval_T *rettv);
625static void f_keys(typval_T *argvars, typval_T *rettv);
626static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
627static void f_len(typval_T *argvars, typval_T *rettv);
628static void f_libcall(typval_T *argvars, typval_T *rettv);
629static void f_libcallnr(typval_T *argvars, typval_T *rettv);
630static void f_line(typval_T *argvars, typval_T *rettv);
631static void f_line2byte(typval_T *argvars, typval_T *rettv);
632static void f_lispindent(typval_T *argvars, typval_T *rettv);
633static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000634#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100635static void f_log(typval_T *argvars, typval_T *rettv);
636static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000637#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200638#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100639static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200640#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100641static void f_map(typval_T *argvars, typval_T *rettv);
642static void f_maparg(typval_T *argvars, typval_T *rettv);
643static void f_mapcheck(typval_T *argvars, typval_T *rettv);
644static void f_match(typval_T *argvars, typval_T *rettv);
645static void f_matchadd(typval_T *argvars, typval_T *rettv);
646static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
647static void f_matcharg(typval_T *argvars, typval_T *rettv);
648static void f_matchdelete(typval_T *argvars, typval_T *rettv);
649static void f_matchend(typval_T *argvars, typval_T *rettv);
650static void f_matchlist(typval_T *argvars, typval_T *rettv);
651static void f_matchstr(typval_T *argvars, typval_T *rettv);
652static void f_max(typval_T *argvars, typval_T *rettv);
653static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000654#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000656#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100658#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100660#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
662static void f_nr2char(typval_T *argvars, typval_T *rettv);
663static void f_or(typval_T *argvars, typval_T *rettv);
664static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100665#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100666static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100667#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
672static void f_printf(typval_T *argvars, typval_T *rettv);
673static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200674#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200676#endif
677#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200679#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_range(typval_T *argvars, typval_T *rettv);
681static void f_readfile(typval_T *argvars, typval_T *rettv);
682static void f_reltime(typval_T *argvars, typval_T *rettv);
683static void f_reltimestr(typval_T *argvars, typval_T *rettv);
684static void f_remote_expr(typval_T *argvars, typval_T *rettv);
685static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
686static void f_remote_peek(typval_T *argvars, typval_T *rettv);
687static void f_remote_read(typval_T *argvars, typval_T *rettv);
688static void f_remote_send(typval_T *argvars, typval_T *rettv);
689static void f_remove(typval_T *argvars, typval_T *rettv);
690static void f_rename(typval_T *argvars, typval_T *rettv);
691static void f_repeat(typval_T *argvars, typval_T *rettv);
692static void f_resolve(typval_T *argvars, typval_T *rettv);
693static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_screenattr(typval_T *argvars, typval_T *rettv);
698static void f_screenchar(typval_T *argvars, typval_T *rettv);
699static void f_screencol(typval_T *argvars, typval_T *rettv);
700static void f_screenrow(typval_T *argvars, typval_T *rettv);
701static void f_search(typval_T *argvars, typval_T *rettv);
702static void f_searchdecl(typval_T *argvars, typval_T *rettv);
703static void f_searchpair(typval_T *argvars, typval_T *rettv);
704static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
705static void f_searchpos(typval_T *argvars, typval_T *rettv);
706static void f_server2client(typval_T *argvars, typval_T *rettv);
707static void f_serverlist(typval_T *argvars, typval_T *rettv);
708static void f_setbufvar(typval_T *argvars, typval_T *rettv);
709static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
710static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
711static void f_setline(typval_T *argvars, typval_T *rettv);
712static void f_setloclist(typval_T *argvars, typval_T *rettv);
713static void f_setmatches(typval_T *argvars, typval_T *rettv);
714static void f_setpos(typval_T *argvars, typval_T *rettv);
715static void f_setqflist(typval_T *argvars, typval_T *rettv);
716static void f_setreg(typval_T *argvars, typval_T *rettv);
717static void f_settabvar(typval_T *argvars, typval_T *rettv);
718static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
719static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100720#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100722#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100723static void f_shellescape(typval_T *argvars, typval_T *rettv);
724static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
725static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000726#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_sin(typval_T *argvars, typval_T *rettv);
728static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000729#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100730static void f_sort(typval_T *argvars, typval_T *rettv);
731static void f_soundfold(typval_T *argvars, typval_T *rettv);
732static void f_spellbadword(typval_T *argvars, typval_T *rettv);
733static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
734static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_sqrt(typval_T *argvars, typval_T *rettv);
737static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000738#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_str2nr(typval_T *argvars, typval_T *rettv);
740static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000741#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100742static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000743#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100744static void f_stridx(typval_T *argvars, typval_T *rettv);
745static void f_string(typval_T *argvars, typval_T *rettv);
746static void f_strlen(typval_T *argvars, typval_T *rettv);
747static void f_strpart(typval_T *argvars, typval_T *rettv);
748static void f_strridx(typval_T *argvars, typval_T *rettv);
749static void f_strtrans(typval_T *argvars, typval_T *rettv);
750static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
751static void f_strwidth(typval_T *argvars, typval_T *rettv);
752static void f_submatch(typval_T *argvars, typval_T *rettv);
753static void f_substitute(typval_T *argvars, typval_T *rettv);
754static void f_synID(typval_T *argvars, typval_T *rettv);
755static void f_synIDattr(typval_T *argvars, typval_T *rettv);
756static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
757static void f_synstack(typval_T *argvars, typval_T *rettv);
758static void f_synconcealed(typval_T *argvars, typval_T *rettv);
759static void f_system(typval_T *argvars, typval_T *rettv);
760static void f_systemlist(typval_T *argvars, typval_T *rettv);
761static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
762static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
763static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
764static void f_taglist(typval_T *argvars, typval_T *rettv);
765static void f_tagfiles(typval_T *argvars, typval_T *rettv);
766static void f_tempname(typval_T *argvars, typval_T *rettv);
767static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200768#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_tan(typval_T *argvars, typval_T *rettv);
770static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200771#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_tolower(typval_T *argvars, typval_T *rettv);
773static void f_toupper(typval_T *argvars, typval_T *rettv);
774static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000775#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000777#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100778static void f_type(typval_T *argvars, typval_T *rettv);
779static void f_undofile(typval_T *argvars, typval_T *rettv);
780static void f_undotree(typval_T *argvars, typval_T *rettv);
781static void f_uniq(typval_T *argvars, typval_T *rettv);
782static void f_values(typval_T *argvars, typval_T *rettv);
783static void f_virtcol(typval_T *argvars, typval_T *rettv);
784static void f_visualmode(typval_T *argvars, typval_T *rettv);
785static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
786static void f_winbufnr(typval_T *argvars, typval_T *rettv);
787static void f_wincol(typval_T *argvars, typval_T *rettv);
788static void f_winheight(typval_T *argvars, typval_T *rettv);
789static void f_winline(typval_T *argvars, typval_T *rettv);
790static void f_winnr(typval_T *argvars, typval_T *rettv);
791static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
792static void f_winrestview(typval_T *argvars, typval_T *rettv);
793static void f_winsaveview(typval_T *argvars, typval_T *rettv);
794static void f_winwidth(typval_T *argvars, typval_T *rettv);
795static void f_writefile(typval_T *argvars, typval_T *rettv);
796static void f_wordcount(typval_T *argvars, typval_T *rettv);
797static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000798
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100799static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
800static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
801static int get_env_len(char_u **arg);
802static int get_id_len(char_u **arg);
803static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
804static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000805#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
806#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
807 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100808static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
809static int eval_isnamec(int c);
810static int eval_isnamec1(int c);
811static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
812static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100813static typval_T *alloc_string_tv(char_u *string);
814static void init_tv(typval_T *varp);
815static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100816#ifdef FEAT_FLOAT
817static float_T get_tv_float(typval_T *varp);
818#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100819static linenr_T get_tv_lnum(typval_T *argvars);
820static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
821static char_u *get_tv_string(typval_T *varp);
822static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
823static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
824static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
825static hashtab_T *find_var_ht(char_u *name, char_u **varname);
826static funccall_T *get_funccal(void);
827static void vars_clear_ext(hashtab_T *ht, int free_val);
828static void delete_var(hashtab_T *ht, hashitem_T *hi);
829static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
830static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
831static void set_var(char_u *name, typval_T *varp, int copy);
832static int var_check_ro(int flags, char_u *name, int use_gettext);
833static int var_check_fixed(int flags, char_u *name, int use_gettext);
834static int var_check_func_name(char_u *name, int new_var);
835static int valid_varname(char_u *varname);
836static int tv_check_lock(int lock, char_u *name, int use_gettext);
837static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
838static char_u *find_option_end(char_u **arg, int *opt_flags);
839static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
840static int eval_fname_script(char_u *p);
841static int eval_fname_sid(char_u *p);
842static void list_func_head(ufunc_T *fp, int indent);
843static ufunc_T *find_func(char_u *name);
844static int function_exists(char_u *name);
845static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static void func_do_profile(ufunc_T *fp);
848static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
849static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000850static int
851# ifdef __BORLANDC__
852 _RTLENTRYF
853# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100854 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000855static int
856# ifdef __BORLANDC__
857 _RTLENTRYF
858# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000860#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100861static int script_autoload(char_u *name, int reload);
862static char_u *autoload_name(char_u *name);
863static void cat_func_name(char_u *buf, ufunc_T *fp);
864static void func_free(ufunc_T *fp);
865static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
866static int can_free_funccal(funccall_T *fc, int copyID) ;
867static void free_funccal(funccall_T *fc, int free_val);
868static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
869static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
870static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
871static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
872static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
873static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
874static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
875static int write_list(FILE *fd, list_T *list, int binary);
876static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200878
879#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880static int compare_func_name(const void *s1, const void *s2);
881static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200882#endif
883
Bram Moolenaar33570922005-01-25 22:26:29 +0000884/*
885 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000886 */
887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100888eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000889{
Bram Moolenaar33570922005-01-25 22:26:29 +0000890 int i;
891 struct vimvar *p;
892
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200893 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
894 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200895 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000896 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000897 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000898
899 for (i = 0; i < VV_LEN; ++i)
900 {
901 p = &vimvars[i];
902 STRCPY(p->vv_di.di_key, p->vv_name);
903 if (p->vv_flags & VV_RO)
904 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
905 else if (p->vv_flags & VV_RO_SBX)
906 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
907 else
908 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000909
910 /* add to v: scope dict, unless the value is not always available */
911 if (p->vv_type != VAR_UNKNOWN)
912 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000913 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000914 /* add to compat scope dict */
915 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000916 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100917 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
918
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000919 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100920 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200921 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100922 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100923
924 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
925 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
926 set_vim_var_nr(VV_NONE, VVAL_NONE);
927 set_vim_var_nr(VV_NULL, VVAL_NULL);
928
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200929 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200930
931#ifdef EBCDIC
932 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100933 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200934 */
935 sortFunctions();
936#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000937}
938
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000939#if defined(EXITFREE) || defined(PROTO)
940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100941eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000942{
943 int i;
944 struct vimvar *p;
945
946 for (i = 0; i < VV_LEN; ++i)
947 {
948 p = &vimvars[i];
949 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000950 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000951 vim_free(p->vv_str);
952 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000953 }
954 else if (p->vv_di.di_tv.v_type == VAR_LIST)
955 {
956 list_unref(p->vv_list);
957 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000958 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000959 }
960 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000961 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000962 hash_clear(&compat_hashtab);
963
Bram Moolenaard9fba312005-06-26 22:34:35 +0000964 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100965# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200966 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100967# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000968
969 /* global variables */
970 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000971
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000972 /* autoloaded script names */
973 ga_clear_strings(&ga_loaded);
974
Bram Moolenaarcca74132013-09-25 21:00:28 +0200975 /* Script-local variables. First clear all the variables and in a second
976 * loop free the scriptvar_T, because a variable in one script might hold
977 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200978 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200979 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200980 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200981 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200982 ga_clear(&ga_scripts);
983
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000984 /* unreferenced lists and dicts */
985 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000986
987 /* functions */
988 free_all_functions();
989 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000990}
991#endif
992
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 * Return the name of the executed function.
995 */
996 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100997func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000}
1001
1002/*
1003 * Return the address holding the next breakpoint line for a funccall cookie.
1004 */
1005 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001006func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007{
Bram Moolenaar33570922005-01-25 22:26:29 +00001008 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009}
1010
1011/*
1012 * Return the address holding the debug tick for a funccall cookie.
1013 */
1014 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001015func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016{
Bram Moolenaar33570922005-01-25 22:26:29 +00001017 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018}
1019
1020/*
1021 * Return the nesting level for a funccall cookie.
1022 */
1023 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001024func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaar33570922005-01-25 22:26:29 +00001026 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001030funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001032/* pointer to list of previously used funccal, still around because some
1033 * item in it is still being used. */
1034funccall_T *previous_funccal = NULL;
1035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036/*
1037 * Return TRUE when a function was ended by a ":return" command.
1038 */
1039 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001040current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 return current_funccal->returned;
1043}
1044
1045
1046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 * Set an internal variable to a string value. Creates the variable if it does
1048 * not already exist.
1049 */
1050 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001051set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001053 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001054 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
1056 val = vim_strsave(value);
1057 if (val != NULL)
1058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001059 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001060 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001062 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001063 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 }
1065 }
1066}
1067
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001069static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070static char_u *redir_endp = NULL;
1071static char_u *redir_varname = NULL;
1072
1073/*
1074 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001075 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 * Returns OK if successfully completed the setup. FAIL otherwise.
1077 */
1078 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001079var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080{
1081 int save_emsg;
1082 int err;
1083 typval_T tv;
1084
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001085 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001086 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 {
1088 EMSG(_(e_invarg));
1089 return FAIL;
1090 }
1091
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001092 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 redir_varname = vim_strsave(name);
1094 if (redir_varname == NULL)
1095 return FAIL;
1096
1097 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1098 if (redir_lval == NULL)
1099 {
1100 var_redir_stop();
1101 return FAIL;
1102 }
1103
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001104 /* The output is stored in growarray "redir_ga" until redirection ends. */
1105 ga_init2(&redir_ga, (int)sizeof(char), 500);
1106
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001108 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001109 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1111 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001112 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 if (redir_endp != NULL && *redir_endp != NUL)
1114 /* Trailing characters are present after the variable name */
1115 EMSG(_(e_trailing));
1116 else
1117 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001118 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 var_redir_stop();
1120 return FAIL;
1121 }
1122
1123 /* check if we can write to the variable: set it to or append an empty
1124 * string */
1125 save_emsg = did_emsg;
1126 did_emsg = FALSE;
1127 tv.v_type = VAR_STRING;
1128 tv.vval.v_string = (char_u *)"";
1129 if (append)
1130 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1131 else
1132 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001133 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001135 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 if (err)
1137 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001138 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 var_redir_stop();
1140 return FAIL;
1141 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142
1143 return OK;
1144}
1145
1146/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001147 * Append "value[value_len]" to the variable set by var_redir_start().
1148 * The actual appending is postponed until redirection ends, because the value
1149 * appended may in fact be the string we write to, changing it may cause freed
1150 * memory to be used:
1151 * :redir => foo
1152 * :let foo
1153 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 */
1155 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001156var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001158 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159
1160 if (redir_lval == NULL)
1161 return;
1162
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001163 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001164 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001166 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001168 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001169 {
1170 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001171 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001172 }
1173 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175}
1176
1177/*
1178 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001179 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180 */
1181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001182var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001184 typval_T tv;
1185
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 if (redir_lval != NULL)
1187 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001188 /* If there was no error: assign the text to the variable. */
1189 if (redir_endp != NULL)
1190 {
1191 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1192 tv.v_type = VAR_STRING;
1193 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001194 /* Call get_lval() again, if it's inside a Dict or List it may
1195 * have changed. */
1196 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001197 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001198 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1199 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1200 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001201 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001202
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001203 /* free the collected output */
1204 vim_free(redir_ga.ga_data);
1205 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001206
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 vim_free(redir_lval);
1208 redir_lval = NULL;
1209 }
1210 vim_free(redir_varname);
1211 redir_varname = NULL;
1212}
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214# if defined(FEAT_MBYTE) || defined(PROTO)
1215 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001216eval_charconvert(
1217 char_u *enc_from,
1218 char_u *enc_to,
1219 char_u *fname_from,
1220 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221{
1222 int err = FALSE;
1223
1224 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1225 set_vim_var_string(VV_CC_TO, enc_to, -1);
1226 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1227 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1228 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1229 err = TRUE;
1230 set_vim_var_string(VV_CC_FROM, NULL, -1);
1231 set_vim_var_string(VV_CC_TO, NULL, -1);
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1234
1235 if (err)
1236 return FAIL;
1237 return OK;
1238}
1239# endif
1240
1241# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001243eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244{
1245 int err = FALSE;
1246
1247 set_vim_var_string(VV_FNAME_IN, fname, -1);
1248 set_vim_var_string(VV_CMDARG, args, -1);
1249 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1250 err = TRUE;
1251 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1252 set_vim_var_string(VV_CMDARG, NULL, -1);
1253
1254 if (err)
1255 {
1256 mch_remove(fname);
1257 return FAIL;
1258 }
1259 return OK;
1260}
1261# endif
1262
1263# if defined(FEAT_DIFF) || defined(PROTO)
1264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001265eval_diff(
1266 char_u *origfile,
1267 char_u *newfile,
1268 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269{
1270 int err = FALSE;
1271
1272 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1273 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1274 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1275 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1276 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1277 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1278 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1279}
1280
1281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001282eval_patch(
1283 char_u *origfile,
1284 char_u *difffile,
1285 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 int err;
1288
1289 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1290 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1291 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1292 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1293 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1294 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1295 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1296}
1297# endif
1298
1299/*
1300 * Top level evaluation function, returning a boolean.
1301 * Sets "error" to TRUE if there was an error.
1302 * Return TRUE or FALSE.
1303 */
1304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001305eval_to_bool(
1306 char_u *arg,
1307 int *error,
1308 char_u **nextcmd,
1309 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
Bram Moolenaar33570922005-01-25 22:26:29 +00001311 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 int retval = FALSE;
1313
1314 if (skip)
1315 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001316 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 else
1319 {
1320 *error = FALSE;
1321 if (!skip)
1322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001323 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001324 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
1326 }
1327 if (skip)
1328 --emsg_skip;
1329
1330 return retval;
1331}
1332
1333/*
1334 * Top level evaluation function, returning a string. If "skip" is TRUE,
1335 * only parsing to "nextcmd" is done, without reporting errors. Return
1336 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1337 */
1338 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001339eval_to_string_skip(
1340 char_u *arg,
1341 char_u **nextcmd,
1342 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *retval;
1346
1347 if (skip)
1348 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 retval = NULL;
1351 else
1352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 retval = vim_strsave(get_tv_string(&tv));
1354 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 if (skip)
1357 --emsg_skip;
1358
1359 return retval;
1360}
1361
1362/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001363 * Skip over an expression at "*pp".
1364 * Return FAIL for an error, OK otherwise.
1365 */
1366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001367skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001368{
Bram Moolenaar33570922005-01-25 22:26:29 +00001369 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001370
1371 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001373}
1374
1375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377 * When "convert" is TRUE convert a List into a sequence of lines and convert
1378 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 * Return pointer to allocated memory, or NULL for failure.
1380 */
1381 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001382eval_to_string(
1383 char_u *arg,
1384 char_u **nextcmd,
1385 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
Bram Moolenaar33570922005-01-25 22:26:29 +00001387 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001390#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001391 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001394 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 retval = NULL;
1396 else
1397 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001398 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001399 {
1400 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001401 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001402 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001403 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001404 if (tv.vval.v_list->lv_len > 0)
1405 ga_append(&ga, NL);
1406 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001407 ga_append(&ga, NUL);
1408 retval = (char_u *)ga.ga_data;
1409 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001410#ifdef FEAT_FLOAT
1411 else if (convert && tv.v_type == VAR_FLOAT)
1412 {
1413 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1414 retval = vim_strsave(numbuf);
1415 }
1416#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001417 else
1418 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421
1422 return retval;
1423}
1424
1425/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001426 * Call eval_to_string() without using current local variables and using
1427 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 */
1429 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001430eval_to_string_safe(
1431 char_u *arg,
1432 char_u **nextcmd,
1433 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
1435 char_u *retval;
1436 void *save_funccalp;
1437
1438 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001439 if (use_sandbox)
1440 ++sandbox;
1441 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001442 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001443 if (use_sandbox)
1444 --sandbox;
1445 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 restore_funccal(save_funccalp);
1447 return retval;
1448}
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450/*
1451 * Top level evaluation function, returning a number.
1452 * Evaluates "expr" silently.
1453 * Returns -1 for an error.
1454 */
1455 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001456eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
Bram Moolenaar33570922005-01-25 22:26:29 +00001458 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001460 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
1462 ++emsg_off;
1463
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001464 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 retval = -1;
1466 else
1467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001468 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001469 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
1471 --emsg_off;
1472
1473 return retval;
1474}
1475
Bram Moolenaara40058a2005-07-11 22:42:07 +00001476/*
1477 * Prepare v: variable "idx" to be used.
1478 * Save the current typeval in "save_tv".
1479 * When not used yet add the variable to the v: hashtable.
1480 */
1481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001482prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001483{
1484 *save_tv = vimvars[idx].vv_tv;
1485 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1486 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1487}
1488
1489/*
1490 * Restore v: variable "idx" to typeval "save_tv".
1491 * When no longer defined, remove the variable from the v: hashtable.
1492 */
1493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001494restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001495{
1496 hashitem_T *hi;
1497
Bram Moolenaara40058a2005-07-11 22:42:07 +00001498 vimvars[idx].vv_tv = *save_tv;
1499 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1500 {
1501 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1502 if (HASHITEM_EMPTY(hi))
1503 EMSG2(_(e_intern2), "restore_vimvar()");
1504 else
1505 hash_remove(&vimvarht, hi);
1506 }
1507}
1508
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001509#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001510/*
1511 * Evaluate an expression to a list with suggestions.
1512 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001513 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514 */
1515 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001516eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001517{
1518 typval_T save_val;
1519 typval_T rettv;
1520 list_T *list = NULL;
1521 char_u *p = skipwhite(expr);
1522
1523 /* Set "v:val" to the bad word. */
1524 prepare_vimvar(VV_VAL, &save_val);
1525 vimvars[VV_VAL].vv_type = VAR_STRING;
1526 vimvars[VV_VAL].vv_str = badword;
1527 if (p_verbose == 0)
1528 ++emsg_off;
1529
1530 if (eval1(&p, &rettv, TRUE) == OK)
1531 {
1532 if (rettv.v_type != VAR_LIST)
1533 clear_tv(&rettv);
1534 else
1535 list = rettv.vval.v_list;
1536 }
1537
1538 if (p_verbose == 0)
1539 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001540 restore_vimvar(VV_VAL, &save_val);
1541
1542 return list;
1543}
1544
1545/*
1546 * "list" is supposed to contain two items: a word and a number. Return the
1547 * word in "pp" and the number as the return value.
1548 * Return -1 if anything isn't right.
1549 * Used to get the good word and score from the eval_spell_expr() result.
1550 */
1551 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001552get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001553{
1554 listitem_T *li;
1555
1556 li = list->lv_first;
1557 if (li == NULL)
1558 return -1;
1559 *pp = get_tv_string(&li->li_tv);
1560
1561 li = li->li_next;
1562 if (li == NULL)
1563 return -1;
1564 return get_tv_number(&li->li_tv);
1565}
1566#endif
1567
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001569 * Top level evaluation function.
1570 * Returns an allocated typval_T with the result.
1571 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001572 */
1573 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001574eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001575{
1576 typval_T *tv;
1577
1578 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001579 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001580 {
1581 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001582 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001583 }
1584
1585 return tv;
1586}
1587
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001590 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001591 * Uses argv[argc] for the function arguments. Only Number and String
1592 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001595 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001596call_vim_function(
1597 char_u *func,
1598 int argc,
1599 char_u **argv,
1600 int safe, /* use the sandbox */
1601 int str_arg_only, /* all arguments are strings */
1602 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
Bram Moolenaar33570922005-01-25 22:26:29 +00001604 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 long n;
1606 int len;
1607 int i;
1608 int doesrange;
1609 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001612 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 for (i = 0; i < argc; i++)
1617 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001618 /* Pass a NULL or empty argument as an empty string */
1619 if (argv[i] == NULL || *argv[i] == NUL)
1620 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001621 argvars[i].v_type = VAR_STRING;
1622 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001623 continue;
1624 }
1625
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001626 if (str_arg_only)
1627 len = 0;
1628 else
1629 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001630 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (len != 0 && len == (int)STRLEN(argv[i]))
1632 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001633 argvars[i].v_type = VAR_NUMBER;
1634 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 }
1636 else
1637 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001638 argvars[i].v_type = VAR_STRING;
1639 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 }
1641 }
1642
1643 if (safe)
1644 {
1645 save_funccalp = save_funccal();
1646 ++sandbox;
1647 }
1648
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1650 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 if (safe)
1654 {
1655 --sandbox;
1656 restore_funccal(save_funccalp);
1657 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658 vim_free(argvars);
1659
1660 if (ret == FAIL)
1661 clear_tv(rettv);
1662
1663 return ret;
1664}
1665
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001666/*
1667 * Call vimL function "func" and return the result as a number.
1668 * Returns -1 when calling the function fails.
1669 * Uses argv[argc] for the function arguments.
1670 */
1671 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001672call_func_retnr(
1673 char_u *func,
1674 int argc,
1675 char_u **argv,
1676 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001677{
1678 typval_T rettv;
1679 long retval;
1680
1681 /* All arguments are passed as strings, no conversion to number. */
1682 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1683 return -1;
1684
1685 retval = get_tv_number_chk(&rettv, NULL);
1686 clear_tv(&rettv);
1687 return retval;
1688}
1689
1690#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1691 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1692
Bram Moolenaar4f688582007-07-24 12:34:30 +00001693# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001694/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001695 * Call vimL function "func" and return the result as a string.
1696 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001697 * Uses argv[argc] for the function arguments.
1698 */
1699 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001700call_func_retstr(
1701 char_u *func,
1702 int argc,
1703 char_u **argv,
1704 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705{
1706 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001707 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001709 /* All arguments are passed as strings, no conversion to number. */
1710 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 return NULL;
1712
1713 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 return retval;
1716}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001717# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001719/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001720 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001722 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723 */
1724 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001725call_func_retlist(
1726 char_u *func,
1727 int argc,
1728 char_u **argv,
1729 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730{
1731 typval_T rettv;
1732
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001733 /* All arguments are passed as strings, no conversion to number. */
1734 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735 return NULL;
1736
1737 if (rettv.v_type != VAR_LIST)
1738 {
1739 clear_tv(&rettv);
1740 return NULL;
1741 }
1742
1743 return rettv.vval.v_list;
1744}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#endif
1746
1747/*
1748 * Save the current function call pointer, and set it to NULL.
1749 * Used when executing autocommands and for ":source".
1750 */
1751 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001752save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 current_funccal = NULL;
1757 return (void *)fc;
1758}
1759
1760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001761restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001763 funccall_T *fc = (funccall_T *)vfc;
1764
1765 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766}
1767
Bram Moolenaar05159a02005-02-26 23:04:13 +00001768#if defined(FEAT_PROFILE) || defined(PROTO)
1769/*
1770 * Prepare profiling for entering a child or something else that is not
1771 * counted for the script/function itself.
1772 * Should always be called in pair with prof_child_exit().
1773 */
1774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775prof_child_enter(
1776 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001777{
1778 funccall_T *fc = current_funccal;
1779
1780 if (fc != NULL && fc->func->uf_profiling)
1781 profile_start(&fc->prof_child);
1782 script_prof_save(tm);
1783}
1784
1785/*
1786 * Take care of time spent in a child.
1787 * Should always be called after prof_child_enter().
1788 */
1789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001790prof_child_exit(
1791 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001792{
1793 funccall_T *fc = current_funccal;
1794
1795 if (fc != NULL && fc->func->uf_profiling)
1796 {
1797 profile_end(&fc->prof_child);
1798 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1799 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1800 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1801 }
1802 script_prof_restore(tm);
1803}
1804#endif
1805
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#ifdef FEAT_FOLDING
1808/*
1809 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1810 * it in "*cp". Doesn't give error messages.
1811 */
1812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001813eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
Bram Moolenaar33570922005-01-25 22:26:29 +00001815 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 int retval;
1817 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001818 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1819 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001822 if (use_sandbox)
1823 ++sandbox;
1824 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 retval = 0;
1828 else
1829 {
1830 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 if (tv.v_type == VAR_NUMBER)
1832 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001833 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 retval = 0;
1835 else
1836 {
1837 /* If the result is a string, check if there is a non-digit before
1838 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (!VIM_ISDIGIT(*s) && *s != '-')
1841 *cp = *s++;
1842 retval = atol((char *)s);
1843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001847 if (use_sandbox)
1848 --sandbox;
1849 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 return retval;
1852}
1853#endif
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 * ":let" list all variable values
1857 * ":let var1 var2" list variable values
1858 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001859 * ":let var += expr" assignment command.
1860 * ":let var -= expr" assignment command.
1861 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 */
1864 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001865ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
1867 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001869 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 int var_count = 0;
1872 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001874 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001875 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
Bram Moolenaardb552d602006-03-23 22:59:57 +00001877 argend = skip_var_list(arg, &var_count, &semicolon);
1878 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001880 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1881 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001882 expr = skipwhite(argend);
1883 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1884 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001886 /*
1887 * ":let" without "=": list variables
1888 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 if (*arg == '[')
1890 EMSG(_(e_invarg));
1891 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001893 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001897 list_glob_vars(&first);
1898 list_buf_vars(&first);
1899 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001900#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001901 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001902#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001903 list_script_vars(&first);
1904 list_func_vars(&first);
1905 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 eap->nextcmd = check_nextcmd(arg);
1908 }
1909 else
1910 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001911 op[0] = '=';
1912 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001913 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001915 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1916 op[0] = *expr; /* +=, -= or .= */
1917 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001919 else
1920 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (eap->skip)
1923 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (eap->skip)
1926 {
1927 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 --emsg_skip;
1930 }
1931 else if (i != FAIL)
1932 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001933 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
1937 }
1938}
1939
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940/*
1941 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1942 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001943 * When "nextchars" is not NULL it points to a string with characters that
1944 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1945 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 * Returns OK or FAIL;
1947 */
1948 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001949ex_let_vars(
1950 char_u *arg_start,
1951 typval_T *tv,
1952 int copy, /* copy values from "tv", don't move */
1953 int semicolon, /* from skip_var_list() */
1954 int var_count, /* from skip_var_list() */
1955 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956{
1957 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001958 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001960 listitem_T *item;
1961 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962
1963 if (*arg != '[')
1964 {
1965 /*
1966 * ":let var = expr" or ":for var in list"
1967 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 return FAIL;
1970 return OK;
1971 }
1972
1973 /*
1974 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1975 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001976 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 {
1978 EMSG(_(e_listreq));
1979 return FAIL;
1980 }
1981
1982 i = list_len(l);
1983 if (semicolon == 0 && var_count < i)
1984 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001985 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 return FAIL;
1987 }
1988 if (var_count - semicolon > i)
1989 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001990 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return FAIL;
1992 }
1993
1994 item = l->lv_first;
1995 while (*arg != ']')
1996 {
1997 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 item = item->li_next;
2000 if (arg == NULL)
2001 return FAIL;
2002
2003 arg = skipwhite(arg);
2004 if (*arg == ';')
2005 {
2006 /* Put the rest of the list (may be empty) in the var after ';'.
2007 * Create a new list for this. */
2008 l = list_alloc();
2009 if (l == NULL)
2010 return FAIL;
2011 while (item != NULL)
2012 {
2013 list_append_tv(l, &item->li_tv);
2014 item = item->li_next;
2015 }
2016
2017 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002018 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 ltv.vval.v_list = l;
2020 l->lv_refcount = 1;
2021
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2023 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 clear_tv(&ltv);
2025 if (arg == NULL)
2026 return FAIL;
2027 break;
2028 }
2029 else if (*arg != ',' && *arg != ']')
2030 {
2031 EMSG2(_(e_intern2), "ex_let_vars()");
2032 return FAIL;
2033 }
2034 }
2035
2036 return OK;
2037}
2038
2039/*
2040 * Skip over assignable variable "var" or list of variables "[var, var]".
2041 * Used for ":let varvar = expr" and ":for varvar in expr".
2042 * For "[var, var]" increment "*var_count" for each variable.
2043 * for "[var, var; var]" set "semicolon".
2044 * Return NULL for an error.
2045 */
2046 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002047skip_var_list(
2048 char_u *arg,
2049 int *var_count,
2050 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051{
2052 char_u *p, *s;
2053
2054 if (*arg == '[')
2055 {
2056 /* "[var, var]": find the matching ']'. */
2057 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002058 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059 {
2060 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2061 s = skip_var_one(p);
2062 if (s == p)
2063 {
2064 EMSG2(_(e_invarg2), p);
2065 return NULL;
2066 }
2067 ++*var_count;
2068
2069 p = skipwhite(s);
2070 if (*p == ']')
2071 break;
2072 else if (*p == ';')
2073 {
2074 if (*semicolon == 1)
2075 {
2076 EMSG(_("Double ; in list of variables"));
2077 return NULL;
2078 }
2079 *semicolon = 1;
2080 }
2081 else if (*p != ',')
2082 {
2083 EMSG2(_(e_invarg2), p);
2084 return NULL;
2085 }
2086 }
2087 return p + 1;
2088 }
2089 else
2090 return skip_var_one(arg);
2091}
2092
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002093/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002094 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002095 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002096 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002097 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002098skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002099{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002100 if (*arg == '@' && arg[1] != NUL)
2101 return arg + 2;
2102 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2103 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002104}
2105
Bram Moolenaara7043832005-01-21 11:56:39 +00002106/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 * List variables for hashtab "ht" with prefix "prefix".
2108 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002111list_hashtable_vars(
2112 hashtab_T *ht,
2113 char_u *prefix,
2114 int empty,
2115 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002116{
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 hashitem_T *hi;
2118 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 int todo;
2120
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002121 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2123 {
2124 if (!HASHITEM_EMPTY(hi))
2125 {
2126 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002127 di = HI2DI(hi);
2128 if (empty || di->di_tv.v_type != VAR_STRING
2129 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002131 }
2132 }
2133}
2134
2135/*
2136 * List global variables.
2137 */
2138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002139list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002140{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002142}
2143
2144/*
2145 * List buffer variables.
2146 */
2147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002148list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002149{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002150 char_u numbuf[NUMBUFLEN];
2151
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002154
2155 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2157 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002158}
2159
2160/*
2161 * List window variables.
2162 */
2163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002164list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002165{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002166 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002168}
2169
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002170#ifdef FEAT_WINDOWS
2171/*
2172 * List tab page variables.
2173 */
2174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002176{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002177 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179}
2180#endif
2181
Bram Moolenaara7043832005-01-21 11:56:39 +00002182/*
2183 * List Vim variables.
2184 */
2185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002186list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189}
2190
2191/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192 * List script-local variables, if there is a script.
2193 */
2194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196{
2197 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2199 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002200}
2201
2202/*
2203 * List function variables, if there is a function.
2204 */
2205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002206list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002207{
2208 if (current_funccal != NULL)
2209 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002211}
2212
2213/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 * List variables in "arg".
2215 */
2216 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
2219 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 char_u *name_start;
2223 char_u *arg_subsc;
2224 char_u *tofree;
2225 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226
2227 while (!ends_excmd(*arg) && !got_int)
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002231 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2233 {
2234 emsg_severe = TRUE;
2235 EMSG(_(e_trailing));
2236 break;
2237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* get_name_len() takes care of expanding curly braces */
2242 name_start = name = arg;
2243 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2244 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* This is mainly to keep test 49 working: when expanding
2247 * curly braces fails overrule the exception error message. */
2248 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 emsg_severe = TRUE;
2251 EMSG2(_(e_invarg2), arg);
2252 break;
2253 }
2254 error = TRUE;
2255 }
2256 else
2257 {
2258 if (tofree != NULL)
2259 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002260 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 else
2263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* handle d.key, l[idx], f(expr) */
2265 arg_subsc = arg;
2266 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 case 'g': list_glob_vars(first); break;
2275 case 'b': list_buf_vars(first); break;
2276 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'v': list_vim_vars(first); break;
2281 case 's': list_script_vars(first); break;
2282 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 default:
2284 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
2287 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 {
2289 char_u numbuf[NUMBUFLEN];
2290 char_u *tf;
2291 int c;
2292 char_u *s;
2293
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002294 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 c = *arg;
2296 *arg = NUL;
2297 list_one_var_a((char_u *)"",
2298 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 tv.v_type,
2300 s == NULL ? (char_u *)"" : s,
2301 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 *arg = c;
2303 vim_free(tf);
2304 }
2305 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309
2310 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315
2316 return arg;
2317}
2318
2319/*
2320 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2321 * Returns a pointer to the char just after the var name.
2322 * Returns NULL if there is an error.
2323 */
2324 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002325ex_let_one(
2326 char_u *arg, /* points to variable name */
2327 typval_T *tv, /* value to assign to variable */
2328 int copy, /* copy value from "tv" */
2329 char_u *endchars, /* valid chars after variable name or NULL */
2330 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331{
2332 int c1;
2333 char_u *name;
2334 char_u *p;
2335 char_u *arg_end = NULL;
2336 int len;
2337 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339
2340 /*
2341 * ":let $VAR = expr": Set environment variable.
2342 */
2343 if (*arg == '$')
2344 {
2345 /* Find the end of the name. */
2346 ++arg;
2347 name = arg;
2348 len = get_env_len(&arg);
2349 if (len == 0)
2350 EMSG2(_(e_invarg2), name - 1);
2351 else
2352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 if (op != NULL && (*op == '+' || *op == '-'))
2354 EMSG2(_(e_letwrong), op);
2355 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002358 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 {
2360 c1 = name[len];
2361 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 p = get_tv_string_chk(tv);
2363 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 {
2365 int mustfree = FALSE;
2366 char_u *s = vim_getenv(name, &mustfree);
2367
2368 if (s != NULL)
2369 {
2370 p = tofree = concat_str(s, p);
2371 if (mustfree)
2372 vim_free(s);
2373 }
2374 }
2375 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (STRICMP(name, "HOME") == 0)
2379 init_homedir();
2380 else if (didset_vim && STRICMP(name, "VIM") == 0)
2381 didset_vim = FALSE;
2382 else if (didset_vimruntime
2383 && STRICMP(name, "VIMRUNTIME") == 0)
2384 didset_vimruntime = FALSE;
2385 arg_end = arg;
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390 }
2391 }
2392
2393 /*
2394 * ":let &option = expr": Set option value.
2395 * ":let &l:option = expr": Set local option value.
2396 * ":let &g:option = expr": Set global option value.
2397 */
2398 else if (*arg == '&')
2399 {
2400 /* Find the end of the name. */
2401 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002402 if (p == NULL || (endchars != NULL
2403 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 long n;
2408 int opt_type;
2409 long numval;
2410 char_u *stringval = NULL;
2411 char_u *s;
2412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 c1 = *p;
2414 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415
2416 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 s = get_tv_string_chk(tv); /* != NULL if number or string */
2418 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 {
2420 opt_type = get_option_value(arg, &numval,
2421 &stringval, opt_flags);
2422 if ((opt_type == 1 && *op == '.')
2423 || (opt_type == 0 && *op != '.'))
2424 EMSG2(_(e_letwrong), op);
2425 else
2426 {
2427 if (opt_type == 1) /* number */
2428 {
2429 if (*op == '+')
2430 n = numval + n;
2431 else
2432 n = numval - n;
2433 }
2434 else if (opt_type == 0 && stringval != NULL) /* string */
2435 {
2436 s = concat_str(stringval, s);
2437 vim_free(stringval);
2438 stringval = s;
2439 }
2440 }
2441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 if (s != NULL)
2443 {
2444 set_option_value(arg, n, s, opt_flags);
2445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let @r = expr": Set register contents.
2454 */
2455 else if (*arg == '@')
2456 {
2457 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (op != NULL && (*op == '+' || *op == '-'))
2459 EMSG2(_(e_letwrong), op);
2460 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 EMSG(_(e_letunexp));
2463 else
2464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 char_u *s;
2467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 p = get_tv_string_chk(tv);
2469 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002471 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (s != NULL)
2473 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(s);
2476 }
2477 }
2478 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 arg_end = arg + 1;
2482 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485 }
2486
2487 /*
2488 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002491 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002495 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2499 EMSG(_(e_letunexp));
2500 else
2501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 arg_end = p;
2504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 }
2508
2509 else
2510 EMSG2(_(e_invarg2), arg);
2511
2512 return arg_end;
2513}
2514
2515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2517 */
2518 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002519check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520{
2521 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2522 {
2523 EMSG2(_(e_readonlyvar), arg);
2524 return TRUE;
2525 }
2526 return FALSE;
2527}
2528
2529/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 * Get an lval: variable, Dict item or List item that can be assigned a value
2531 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2532 * "name.key", "name.key[expr]" etc.
2533 * Indexing only works if "name" is an existing List or Dictionary.
2534 * "name" points to the start of the name.
2535 * If "rettv" is not NULL it points to the value to be assigned.
2536 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2537 * wrong; must end in space or cmd separator.
2538 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539 * flags:
2540 * GLV_QUIET: do not give error messages
2541 * GLV_NO_AUTOLOAD: do not use script autoloading
2542 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002544 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 */
2547 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002548get_lval(
2549 char_u *name,
2550 typval_T *rettv,
2551 lval_T *lp,
2552 int unlet,
2553 int skip,
2554 int flags, /* GLV_ values */
2555 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 char_u *expr_start, *expr_end;
2559 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002560 dictitem_T *v;
2561 typval_T var1;
2562 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002568 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002571 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572
2573 if (skip)
2574 {
2575 /* When skipping just find the end of the name. */
2576 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002577 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 }
2579
2580 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002581 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 if (expr_start != NULL)
2583 {
2584 /* Don't expand the name when we already know there is an error. */
2585 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2586 && *p != '[' && *p != '.')
2587 {
2588 EMSG(_(e_trailing));
2589 return NULL;
2590 }
2591
2592 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2593 if (lp->ll_exp_name == NULL)
2594 {
2595 /* Report an invalid expression in braces, unless the
2596 * expression evaluation has been cancelled due to an
2597 * aborting error, an interrupt, or an exception. */
2598 if (!aborting() && !quiet)
2599 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002600 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 EMSG2(_(e_invarg2), name);
2602 return NULL;
2603 }
2604 }
2605 lp->ll_name = lp->ll_exp_name;
2606 }
2607 else
2608 lp->ll_name = name;
2609
2610 /* Without [idx] or .key we are done. */
2611 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2612 return p;
2613
2614 cc = *p;
2615 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002616 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 if (v == NULL && !quiet)
2618 EMSG2(_(e_undefvar), lp->ll_name);
2619 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 if (v == NULL)
2621 return NULL;
2622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 /*
2624 * Loop until no more [idx] or .key is following.
2625 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002626 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2630 && !(lp->ll_tv->v_type == VAR_DICT
2631 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!quiet)
2634 EMSG(_("E689: Can only index a List or Dictionary"));
2635 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002638 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (!quiet)
2640 EMSG(_("E708: [:] must come last"));
2641 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 len = -1;
2645 if (*p == '.')
2646 {
2647 key = p + 1;
2648 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2649 ;
2650 if (len == 0)
2651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
2653 EMSG(_(e_emptykey));
2654 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 }
2656 p = key + len;
2657 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 else
2659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 if (*p == ':')
2663 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002664 else
2665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 empty1 = FALSE;
2667 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002669 if (get_tv_string_chk(&var1) == NULL)
2670 {
2671 /* not a number or string */
2672 clear_tv(&var1);
2673 return NULL;
2674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
2676
2677 /* Optionally get the second index [ :expr]. */
2678 if (*p == ':')
2679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002683 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 if (!empty1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 if (rettv != NULL && (rettv->v_type != VAR_LIST
2689 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!quiet)
2692 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 if (!empty1)
2694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 }
2697 p = skipwhite(p + 1);
2698 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 else
2701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2704 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 if (!empty1)
2706 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002709 if (get_tv_string_chk(&var2) == NULL)
2710 {
2711 /* not a number or string */
2712 if (!empty1)
2713 clear_tv(&var1);
2714 clear_tv(&var2);
2715 return NULL;
2716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (!quiet)
2726 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (!empty1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733
2734 /* Skip to past ']'. */
2735 ++p;
2736 }
2737
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 {
2740 if (len == -1)
2741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 if (*key == NUL)
2745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (!quiet)
2747 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
2751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 lp->ll_list = NULL;
2753 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002754 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002755
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002756 /* When assigning to a scope dictionary check that a function and
2757 * variable name is valid (only variable name unless it is l: or
2758 * g: dictionary). Disallow overwriting a builtin function. */
2759 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002760 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002761 int prevval;
2762 int wrong;
2763
2764 if (len != -1)
2765 {
2766 prevval = key[len];
2767 key[len] = NUL;
2768 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002769 else
2770 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002771 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2772 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002774 || !valid_varname(key);
2775 if (len != -1)
2776 key[len] = prevval;
2777 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002778 return NULL;
2779 }
2780
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783 /* Can't add "v:" variable. */
2784 if (lp->ll_dict == &vimvardict)
2785 {
2786 EMSG2(_(e_illvar), name);
2787 return NULL;
2788 }
2789
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002790 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002794 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 if (len == -1)
2796 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 }
2799 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 if (len == -1)
2804 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 p = NULL;
2807 break;
2808 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002809 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002810 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002811 return NULL;
2812
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 if (len == -1)
2814 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 }
2817 else
2818 {
2819 /*
2820 * Get the number and item for the only or first index of the List.
2821 */
2822 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 else
2825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002826 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 clear_tv(&var1);
2828 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002829 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_list = lp->ll_tv->vval.v_list;
2831 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2832 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002834 if (lp->ll_n1 < 0)
2835 {
2836 lp->ll_n1 = 0;
2837 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2838 }
2839 }
2840 if (lp->ll_li == NULL)
2841 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002844 if (!quiet)
2845 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 }
2848
2849 /*
2850 * May need to find the item or absolute index for the second
2851 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 * When no index given: "lp->ll_empty2" is TRUE.
2853 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002857 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002863 {
2864 if (!quiet)
2865 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002867 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2872 if (lp->ll_n1 < 0)
2873 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2874 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002875 {
2876 if (!quiet)
2877 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002879 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002880 }
2881
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884 }
2885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return p;
2887}
2888
2889/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002890 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 */
2892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002893clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894{
2895 vim_free(lp->ll_exp_name);
2896 vim_free(lp->ll_newkey);
2897}
2898
2899/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002900 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002902 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 */
2904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002905set_var_lval(
2906 lval_T *lp,
2907 char_u *endp,
2908 typval_T *rettv,
2909 int copy,
2910 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911{
2912 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002913 listitem_T *ri;
2914 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915
2916 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 cc = *endp;
2921 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 if (op != NULL && *op != '=')
2923 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002927 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002928 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002929 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002931 if ((di == NULL
2932 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2933 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2934 FALSE)))
2935 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 set_var(lp->ll_name, &tv, FALSE);
2937 clear_tv(&tv);
2938 }
2939 }
2940 else
2941 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002945 else if (tv_check_lock(lp->ll_newkey == NULL
2946 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002947 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002948 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 else if (lp->ll_range)
2950 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002951 listitem_T *ll_li = lp->ll_li;
2952 int ll_n1 = lp->ll_n1;
2953
2954 /*
2955 * Check whether any of the list items is locked
2956 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002957 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002958 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002959 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002960 return;
2961 ri = ri->li_next;
2962 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2963 break;
2964 ll_li = ll_li->li_next;
2965 ++ll_n1;
2966 }
2967
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 /*
2969 * Assign the List values to the list items.
2970 */
2971 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 if (op != NULL && *op != '=')
2974 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2975 else
2976 {
2977 clear_tv(&lp->ll_li->li_tv);
2978 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2979 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 ri = ri->li_next;
2981 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2982 break;
2983 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002986 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 ri = NULL;
2989 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002990 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002991 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 lp->ll_li = lp->ll_li->li_next;
2993 ++lp->ll_n1;
2994 }
2995 if (ri != NULL)
2996 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 else if (lp->ll_empty2
2998 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 : lp->ll_n1 != lp->ll_n2)
3000 EMSG(_("E711: List value has not enough items"));
3001 }
3002 else
3003 {
3004 /*
3005 * Assign to a List or Dictionary item.
3006 */
3007 if (lp->ll_newkey != NULL)
3008 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 if (op != NULL && *op != '=')
3010 {
3011 EMSG2(_(e_letwrong), op);
3012 return;
3013 }
3014
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003016 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 if (di == NULL)
3018 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003019 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3020 {
3021 vim_free(di);
3022 return;
3023 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003024 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003025 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003026 else if (op != NULL && *op != '=')
3027 {
3028 tv_op(lp->ll_tv, rettv, op);
3029 return;
3030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003031 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003033
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 /*
3035 * Assign the value to the variable or list item.
3036 */
3037 if (copy)
3038 copy_tv(rettv, lp->ll_tv);
3039 else
3040 {
3041 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003042 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003044 }
3045 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003046}
3047
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003048/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3050 * Returns OK or FAIL.
3051 */
3052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003053tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054{
3055 long n;
3056 char_u numbuf[NUMBUFLEN];
3057 char_u *s;
3058
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003059 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3060 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3061 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003062 {
3063 switch (tv1->v_type)
3064 {
3065 case VAR_DICT:
3066 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003067 case VAR_SPECIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003068 break;
3069
3070 case VAR_LIST:
3071 if (*op != '+' || tv2->v_type != VAR_LIST)
3072 break;
3073 /* List += List */
3074 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3075 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3076 return OK;
3077
3078 case VAR_NUMBER:
3079 case VAR_STRING:
3080 if (tv2->v_type == VAR_LIST)
3081 break;
3082 if (*op == '+' || *op == '-')
3083 {
3084 /* nr += nr or nr -= nr*/
3085 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003086#ifdef FEAT_FLOAT
3087 if (tv2->v_type == VAR_FLOAT)
3088 {
3089 float_T f = n;
3090
3091 if (*op == '+')
3092 f += tv2->vval.v_float;
3093 else
3094 f -= tv2->vval.v_float;
3095 clear_tv(tv1);
3096 tv1->v_type = VAR_FLOAT;
3097 tv1->vval.v_float = f;
3098 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003100#endif
3101 {
3102 if (*op == '+')
3103 n += get_tv_number(tv2);
3104 else
3105 n -= get_tv_number(tv2);
3106 clear_tv(tv1);
3107 tv1->v_type = VAR_NUMBER;
3108 tv1->vval.v_number = n;
3109 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 }
3111 else
3112 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003113 if (tv2->v_type == VAR_FLOAT)
3114 break;
3115
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003116 /* str .= str */
3117 s = get_tv_string(tv1);
3118 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3119 clear_tv(tv1);
3120 tv1->v_type = VAR_STRING;
3121 tv1->vval.v_string = s;
3122 }
3123 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003124
3125#ifdef FEAT_FLOAT
3126 case VAR_FLOAT:
3127 {
3128 float_T f;
3129
3130 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3131 && tv2->v_type != VAR_NUMBER
3132 && tv2->v_type != VAR_STRING))
3133 break;
3134 if (tv2->v_type == VAR_FLOAT)
3135 f = tv2->vval.v_float;
3136 else
3137 f = get_tv_number(tv2);
3138 if (*op == '+')
3139 tv1->vval.v_float += f;
3140 else
3141 tv1->vval.v_float -= f;
3142 }
3143 return OK;
3144#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 }
3146 }
3147
3148 EMSG2(_(e_letwrong), op);
3149 return FAIL;
3150}
3151
3152/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153 * Add a watcher to a list.
3154 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003156list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003157{
3158 lw->lw_next = l->lv_watch;
3159 l->lv_watch = lw;
3160}
3161
3162/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003163 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 * No warning when it isn't found...
3165 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003167list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168{
Bram Moolenaar33570922005-01-25 22:26:29 +00003169 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170
3171 lwp = &l->lv_watch;
3172 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3173 {
3174 if (lw == lwrem)
3175 {
3176 *lwp = lw->lw_next;
3177 break;
3178 }
3179 lwp = &lw->lw_next;
3180 }
3181}
3182
3183/*
3184 * Just before removing an item from a list: advance watchers to the next
3185 * item.
3186 */
3187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003188list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189{
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191
3192 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3193 if (lw->lw_item == item)
3194 lw->lw_item = item->li_next;
3195}
3196
3197/*
3198 * Evaluate the expression used in a ":for var in expr" command.
3199 * "arg" points to "var".
3200 * Set "*errp" to TRUE for an error, FALSE otherwise;
3201 * Return a pointer that holds the info. Null when there is an error.
3202 */
3203 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003204eval_for_line(
3205 char_u *arg,
3206 int *errp,
3207 char_u **nextcmdp,
3208 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209{
Bram Moolenaar33570922005-01-25 22:26:29 +00003210 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003211 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 typval_T tv;
3213 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214
3215 *errp = TRUE; /* default: there is an error */
3216
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 if (fi == NULL)
3219 return NULL;
3220
3221 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3222 if (expr == NULL)
3223 return fi;
3224
3225 expr = skipwhite(expr);
3226 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3227 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003228 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 return fi;
3230 }
3231
3232 if (skip)
3233 ++emsg_skip;
3234 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3235 {
3236 *errp = FALSE;
3237 if (!skip)
3238 {
3239 l = tv.vval.v_list;
3240 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003241 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003243 clear_tv(&tv);
3244 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 else
3246 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003247 /* No need to increment the refcount, it's already set for the
3248 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 fi->fi_list = l;
3250 list_add_watch(l, &fi->fi_lw);
3251 fi->fi_lw.lw_item = l->lv_first;
3252 }
3253 }
3254 }
3255 if (skip)
3256 --emsg_skip;
3257
3258 return fi;
3259}
3260
3261/*
3262 * Use the first item in a ":for" list. Advance to the next.
3263 * Assign the values to the variable (list). "arg" points to the first one.
3264 * Return TRUE when a valid item was found, FALSE when at end of list or
3265 * something wrong.
3266 */
3267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003270 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273
3274 item = fi->fi_lw.lw_item;
3275 if (item == NULL)
3276 result = FALSE;
3277 else
3278 {
3279 fi->fi_lw.lw_item = item->li_next;
3280 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3281 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3282 }
3283 return result;
3284}
3285
3286/*
3287 * Free the structure used to store info used by ":for".
3288 */
3289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003290free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291{
Bram Moolenaar33570922005-01-25 22:26:29 +00003292 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003294 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003295 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003297 list_unref(fi->fi_list);
3298 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299 vim_free(fi);
3300}
3301
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3303
3304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003305set_context_for_expression(
3306 expand_T *xp,
3307 char_u *arg,
3308 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 int got_eq = FALSE;
3311 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003312 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 if (cmdidx == CMD_let)
3315 {
3316 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003317 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318 {
3319 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003320 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 {
3322 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003323 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 if (vim_iswhite(*p))
3325 break;
3326 }
3327 return;
3328 }
3329 }
3330 else
3331 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3332 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 while ((xp->xp_pattern = vim_strpbrk(arg,
3334 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3335 {
3336 c = *xp->xp_pattern;
3337 if (c == '&')
3338 {
3339 c = xp->xp_pattern[1];
3340 if (c == '&')
3341 {
3342 ++xp->xp_pattern;
3343 xp->xp_context = cmdidx != CMD_let || got_eq
3344 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3345 }
3346 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003349 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3350 xp->xp_pattern += 2;
3351
3352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 }
3354 else if (c == '$')
3355 {
3356 /* environment variable */
3357 xp->xp_context = EXPAND_ENV_VARS;
3358 }
3359 else if (c == '=')
3360 {
3361 got_eq = TRUE;
3362 xp->xp_context = EXPAND_EXPRESSION;
3363 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003364 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 && xp->xp_context == EXPAND_FUNCTIONS
3366 && vim_strchr(xp->xp_pattern, '(') == NULL)
3367 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003368 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 break;
3370 }
3371 else if (cmdidx != CMD_let || got_eq)
3372 {
3373 if (c == '"') /* string */
3374 {
3375 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3376 if (c == '\\' && xp->xp_pattern[1] != NUL)
3377 ++xp->xp_pattern;
3378 xp->xp_context = EXPAND_NOTHING;
3379 }
3380 else if (c == '\'') /* literal string */
3381 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003382 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3384 /* skip */ ;
3385 xp->xp_context = EXPAND_NOTHING;
3386 }
3387 else if (c == '|')
3388 {
3389 if (xp->xp_pattern[1] == '|')
3390 {
3391 ++xp->xp_pattern;
3392 xp->xp_context = EXPAND_EXPRESSION;
3393 }
3394 else
3395 xp->xp_context = EXPAND_COMMANDS;
3396 }
3397 else
3398 xp->xp_context = EXPAND_EXPRESSION;
3399 }
3400 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003401 /* Doesn't look like something valid, expand as an expression
3402 * anyway. */
3403 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 arg = xp->xp_pattern;
3405 if (*arg != NUL)
3406 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3407 /* skip */ ;
3408 }
3409 xp->xp_pattern = arg;
3410}
3411
3412#endif /* FEAT_CMDL_COMPL */
3413
3414/*
3415 * ":1,25call func(arg1, arg2)" function call.
3416 */
3417 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003418ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 char_u *arg = eap->arg;
3421 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003423 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003425 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 linenr_T lnum;
3427 int doesrange;
3428 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003429 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003431 if (eap->skip)
3432 {
3433 /* trans_function_name() doesn't work well when skipping, use eval0()
3434 * instead to skip to any following command, e.g. for:
3435 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003436 ++emsg_skip;
3437 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3438 clear_tv(&rettv);
3439 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003440 return;
3441 }
3442
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003443 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003444 if (fudi.fd_newkey != NULL)
3445 {
3446 /* Still need to give an error message for missing key. */
3447 EMSG2(_(e_dictkey), fudi.fd_newkey);
3448 vim_free(fudi.fd_newkey);
3449 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003450 if (tofree == NULL)
3451 return;
3452
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003453 /* Increase refcount on dictionary, it could get deleted when evaluating
3454 * the arguments. */
3455 if (fudi.fd_dict != NULL)
3456 ++fudi.fd_dict->dv_refcount;
3457
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003458 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003459 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003460 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaar532c7802005-01-27 14:44:31 +00003462 /* Skip white space to allow ":call func ()". Not good, but required for
3463 * backward compatibility. */
3464 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003465 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
3467 if (*startarg != '(')
3468 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003469 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 goto end;
3471 }
3472
3473 /*
3474 * When skipping, evaluate the function once, to find the end of the
3475 * arguments.
3476 * When the function takes a range, this is discovered after the first
3477 * call, and the loop is broken.
3478 */
3479 if (eap->skip)
3480 {
3481 ++emsg_skip;
3482 lnum = eap->line2; /* do it once, also with an invalid range */
3483 }
3484 else
3485 lnum = eap->line1;
3486 for ( ; lnum <= eap->line2; ++lnum)
3487 {
3488 if (!eap->skip && eap->addr_count > 0)
3489 {
3490 curwin->w_cursor.lnum = lnum;
3491 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003492#ifdef FEAT_VIRTUALEDIT
3493 curwin->w_cursor.coladd = 0;
3494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
3496 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003497 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003498 eap->line1, eap->line2, &doesrange,
3499 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
3501 failed = TRUE;
3502 break;
3503 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003504
3505 /* Handle a function returning a Funcref, Dictionary or List. */
3506 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3507 {
3508 failed = TRUE;
3509 break;
3510 }
3511
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003512 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 if (doesrange || eap->skip)
3514 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003517 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003518 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003519 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (aborting())
3521 break;
3522 }
3523 if (eap->skip)
3524 --emsg_skip;
3525
3526 if (!failed)
3527 {
3528 /* Check for trailing illegal characters and a following command. */
3529 if (!ends_excmd(*arg))
3530 {
3531 emsg_severe = TRUE;
3532 EMSG(_(e_trailing));
3533 }
3534 else
3535 eap->nextcmd = check_nextcmd(arg);
3536 }
3537
3538end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003539 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003540 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541}
3542
3543/*
3544 * ":unlet[!] var1 ... " command.
3545 */
3546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003547ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003549 ex_unletlock(eap, eap->arg, 0);
3550}
3551
3552/*
3553 * ":lockvar" and ":unlockvar" commands
3554 */
3555 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003556ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003557{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003559 int deep = 2;
3560
3561 if (eap->forceit)
3562 deep = -1;
3563 else if (vim_isdigit(*arg))
3564 {
3565 deep = getdigits(&arg);
3566 arg = skipwhite(arg);
3567 }
3568
3569 ex_unletlock(eap, arg, deep);
3570}
3571
3572/*
3573 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3574 */
3575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003576ex_unletlock(
3577 exarg_T *eap,
3578 char_u *argstart,
3579 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580{
3581 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
3586 do
3587 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003588 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003589 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003590 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591 if (lv.ll_name == NULL)
3592 error = TRUE; /* error but continue parsing */
3593 if (name_end == NULL || (!vim_iswhite(*name_end)
3594 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596 if (name_end != NULL)
3597 {
3598 emsg_severe = TRUE;
3599 EMSG(_(e_trailing));
3600 }
3601 if (!(eap->skip || error))
3602 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 break;
3604 }
3605
3606 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607 {
3608 if (eap->cmdidx == CMD_unlet)
3609 {
3610 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3611 error = TRUE;
3612 }
3613 else
3614 {
3615 if (do_lock_var(&lv, name_end, deep,
3616 eap->cmdidx == CMD_lockvar) == FAIL)
3617 error = TRUE;
3618 }
3619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 if (!eap->skip)
3622 clear_lval(&lv);
3623
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 arg = skipwhite(name_end);
3625 } while (!ends_excmd(*arg));
3626
3627 eap->nextcmd = check_nextcmd(arg);
3628}
3629
Bram Moolenaar8c711452005-01-14 21:53:12 +00003630 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003631do_unlet_var(
3632 lval_T *lp,
3633 char_u *name_end,
3634 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003635{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 int ret = OK;
3637 int cc;
3638
3639 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003640 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 cc = *name_end;
3642 *name_end = NUL;
3643
3644 /* Normal name or expanded name. */
3645 if (check_changedtick(lp->ll_name))
3646 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003651 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003652 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003653 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003654 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003655 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 else if (lp->ll_range)
3657 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003659 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003660 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003661
3662 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3663 {
3664 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003665 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003666 return FAIL;
3667 ll_li = li;
3668 ++ll_n1;
3669 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670
3671 /* Delete a range of List items. */
3672 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3673 {
3674 li = lp->ll_li->li_next;
3675 listitem_remove(lp->ll_list, lp->ll_li);
3676 lp->ll_li = li;
3677 ++lp->ll_n1;
3678 }
3679 }
3680 else
3681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 /* unlet a List item. */
3684 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003687 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003688 }
3689
3690 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003691}
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693/*
3694 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003695 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 */
3697 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003698do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
Bram Moolenaar33570922005-01-25 22:26:29 +00003700 hashtab_T *ht;
3701 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003702 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003703 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003704 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
Bram Moolenaar33570922005-01-25 22:26:29 +00003706 ht = find_var_ht(name, &varname);
3707 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003709 if (ht == &globvarht)
3710 d = &globvardict;
3711 else if (current_funccal != NULL
3712 && ht == &current_funccal->l_vars.dv_hashtab)
3713 d = &current_funccal->l_vars;
3714 else if (ht == &compat_hashtab)
3715 d = &vimvardict;
3716 else
3717 {
3718 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3719 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3720 }
3721 if (d == NULL)
3722 {
3723 EMSG2(_(e_intern2), "do_unlet()");
3724 return FAIL;
3725 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003726 hi = hash_find(ht, varname);
3727 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003728 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003729 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003730 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003731 || var_check_ro(di->di_flags, name, FALSE)
3732 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003733 return FAIL;
3734
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003735 delete_var(ht, hi);
3736 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003739 if (forceit)
3740 return OK;
3741 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 return FAIL;
3743}
3744
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003745/*
3746 * Lock or unlock variable indicated by "lp".
3747 * "deep" is the levels to go (-1 for unlimited);
3748 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3749 */
3750 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003751do_lock_var(
3752 lval_T *lp,
3753 char_u *name_end,
3754 int deep,
3755 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003756{
3757 int ret = OK;
3758 int cc;
3759 dictitem_T *di;
3760
3761 if (deep == 0) /* nothing to do */
3762 return OK;
3763
3764 if (lp->ll_tv == NULL)
3765 {
3766 cc = *name_end;
3767 *name_end = NUL;
3768
3769 /* Normal name or expanded name. */
3770 if (check_changedtick(lp->ll_name))
3771 ret = FAIL;
3772 else
3773 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003774 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775 if (di == NULL)
3776 ret = FAIL;
3777 else
3778 {
3779 if (lock)
3780 di->di_flags |= DI_FLAGS_LOCK;
3781 else
3782 di->di_flags &= ~DI_FLAGS_LOCK;
3783 item_lock(&di->di_tv, deep, lock);
3784 }
3785 }
3786 *name_end = cc;
3787 }
3788 else if (lp->ll_range)
3789 {
3790 listitem_T *li = lp->ll_li;
3791
3792 /* (un)lock a range of List items. */
3793 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3794 {
3795 item_lock(&li->li_tv, deep, lock);
3796 li = li->li_next;
3797 ++lp->ll_n1;
3798 }
3799 }
3800 else if (lp->ll_list != NULL)
3801 /* (un)lock a List item. */
3802 item_lock(&lp->ll_li->li_tv, deep, lock);
3803 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003804 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003805 item_lock(&lp->ll_di->di_tv, deep, lock);
3806
3807 return ret;
3808}
3809
3810/*
3811 * Lock or unlock an item. "deep" is nr of levels to go.
3812 */
3813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003814item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003815{
3816 static int recurse = 0;
3817 list_T *l;
3818 listitem_T *li;
3819 dict_T *d;
3820 hashitem_T *hi;
3821 int todo;
3822
3823 if (recurse >= DICT_MAXNEST)
3824 {
3825 EMSG(_("E743: variable nested too deep for (un)lock"));
3826 return;
3827 }
3828 if (deep == 0)
3829 return;
3830 ++recurse;
3831
3832 /* lock/unlock the item itself */
3833 if (lock)
3834 tv->v_lock |= VAR_LOCKED;
3835 else
3836 tv->v_lock &= ~VAR_LOCKED;
3837
3838 switch (tv->v_type)
3839 {
3840 case VAR_LIST:
3841 if ((l = tv->vval.v_list) != NULL)
3842 {
3843 if (lock)
3844 l->lv_lock |= VAR_LOCKED;
3845 else
3846 l->lv_lock &= ~VAR_LOCKED;
3847 if (deep < 0 || deep > 1)
3848 /* recursive: lock/unlock the items the List contains */
3849 for (li = l->lv_first; li != NULL; li = li->li_next)
3850 item_lock(&li->li_tv, deep - 1, lock);
3851 }
3852 break;
3853 case VAR_DICT:
3854 if ((d = tv->vval.v_dict) != NULL)
3855 {
3856 if (lock)
3857 d->dv_lock |= VAR_LOCKED;
3858 else
3859 d->dv_lock &= ~VAR_LOCKED;
3860 if (deep < 0 || deep > 1)
3861 {
3862 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003863 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003864 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3865 {
3866 if (!HASHITEM_EMPTY(hi))
3867 {
3868 --todo;
3869 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3870 }
3871 }
3872 }
3873 }
3874 }
3875 --recurse;
3876}
3877
Bram Moolenaara40058a2005-07-11 22:42:07 +00003878/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003879 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3880 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003881 */
3882 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003883tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003884{
3885 return (tv->v_lock & VAR_LOCKED)
3886 || (tv->v_type == VAR_LIST
3887 && tv->vval.v_list != NULL
3888 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3889 || (tv->v_type == VAR_DICT
3890 && tv->vval.v_dict != NULL
3891 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3892}
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3895/*
3896 * Delete all "menutrans_" variables.
3897 */
3898 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003899del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
Bram Moolenaar33570922005-01-25 22:26:29 +00003901 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003902 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
Bram Moolenaar33570922005-01-25 22:26:29 +00003904 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003905 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003906 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003907 {
3908 if (!HASHITEM_EMPTY(hi))
3909 {
3910 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003911 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3912 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003913 }
3914 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003915 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916}
3917#endif
3918
3919#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3920
3921/*
3922 * Local string buffer for the next two functions to store a variable name
3923 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3924 * get_user_var_name().
3925 */
3926
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003927static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929static char_u *varnamebuf = NULL;
3930static int varnamebuflen = 0;
3931
3932/*
3933 * Function to concatenate a prefix and a variable name.
3934 */
3935 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003936cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
3938 int len;
3939
3940 len = (int)STRLEN(name) + 3;
3941 if (len > varnamebuflen)
3942 {
3943 vim_free(varnamebuf);
3944 len += 10; /* some additional space */
3945 varnamebuf = alloc(len);
3946 if (varnamebuf == NULL)
3947 {
3948 varnamebuflen = 0;
3949 return NULL;
3950 }
3951 varnamebuflen = len;
3952 }
3953 *varnamebuf = prefix;
3954 varnamebuf[1] = ':';
3955 STRCPY(varnamebuf + 2, name);
3956 return varnamebuf;
3957}
3958
3959/*
3960 * Function given to ExpandGeneric() to obtain the list of user defined
3961 * (global/buffer/window/built-in) variable names.
3962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003964get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003966 static long_u gdone;
3967 static long_u bdone;
3968 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003969#ifdef FEAT_WINDOWS
3970 static long_u tdone;
3971#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003972 static int vidx;
3973 static hashitem_T *hi;
3974 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
3976 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003977 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003978 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003979#ifdef FEAT_WINDOWS
3980 tdone = 0;
3981#endif
3982 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003983
3984 /* Global variables */
3985 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003987 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003988 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003989 else
3990 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003991 while (HASHITEM_EMPTY(hi))
3992 ++hi;
3993 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3994 return cat_prefix_varname('g', hi->hi_key);
3995 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003997
3998 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003999 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004000 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004002 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004004 else
4005 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004006 while (HASHITEM_EMPTY(hi))
4007 ++hi;
4008 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004010 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return (char_u *)"b:changedtick";
4014 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004015
4016 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004017 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004018 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004020 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004021 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004022 else
4023 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 while (HASHITEM_EMPTY(hi))
4025 ++hi;
4026 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004028
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004029#ifdef FEAT_WINDOWS
4030 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004031 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004032 if (tdone < ht->ht_used)
4033 {
4034 if (tdone++ == 0)
4035 hi = ht->ht_array;
4036 else
4037 ++hi;
4038 while (HASHITEM_EMPTY(hi))
4039 ++hi;
4040 return cat_prefix_varname('t', hi->hi_key);
4041 }
4042#endif
4043
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 /* v: variables */
4045 if (vidx < VV_LEN)
4046 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048 vim_free(varnamebuf);
4049 varnamebuf = NULL;
4050 varnamebuflen = 0;
4051 return NULL;
4052}
4053
4054#endif /* FEAT_CMDL_COMPL */
4055
4056/*
4057 * types for expressions.
4058 */
4059typedef enum
4060{
4061 TYPE_UNKNOWN = 0
4062 , TYPE_EQUAL /* == */
4063 , TYPE_NEQUAL /* != */
4064 , TYPE_GREATER /* > */
4065 , TYPE_GEQUAL /* >= */
4066 , TYPE_SMALLER /* < */
4067 , TYPE_SEQUAL /* <= */
4068 , TYPE_MATCH /* =~ */
4069 , TYPE_NOMATCH /* !~ */
4070} exptype_T;
4071
4072/*
4073 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4076 */
4077
4078/*
4079 * Handle zero level expression.
4080 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004082 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 * Return OK or FAIL.
4084 */
4085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004086eval0(
4087 char_u *arg,
4088 typval_T *rettv,
4089 char_u **nextcmd,
4090 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
4092 int ret;
4093 char_u *p;
4094
4095 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 if (ret == FAIL || !ends_excmd(*p))
4098 {
4099 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 /*
4102 * Report the invalid expression unless the expression evaluation has
4103 * been cancelled due to an aborting error, an interrupt, or an
4104 * exception.
4105 */
4106 if (!aborting())
4107 EMSG2(_(e_invexpr2), arg);
4108 ret = FAIL;
4109 }
4110 if (nextcmd != NULL)
4111 *nextcmd = check_nextcmd(p);
4112
4113 return ret;
4114}
4115
4116/*
4117 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004118 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 *
4120 * "arg" must point to the first non-white of the expression.
4121 * "arg" is advanced to the next non-white after the recognized expression.
4122 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004123 * Note: "rettv.v_lock" is not set.
4124 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 * Return OK or FAIL.
4126 */
4127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004128eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
4130 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004131 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133 /*
4134 * Get the first variable.
4135 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 return FAIL;
4138
4139 if ((*arg)[0] == '?')
4140 {
4141 result = FALSE;
4142 if (evaluate)
4143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004144 int error = FALSE;
4145
4146 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004149 if (error)
4150 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152
4153 /*
4154 * Get the second variable.
4155 */
4156 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return FAIL;
4159
4160 /*
4161 * Check for the ":".
4162 */
4163 if ((*arg)[0] != ':')
4164 {
4165 EMSG(_("E109: Missing ':' after '?'"));
4166 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return FAIL;
4169 }
4170
4171 /*
4172 * Get the third variable.
4173 */
4174 *arg = skipwhite(*arg + 1);
4175 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4176 {
4177 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 return FAIL;
4180 }
4181 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184
4185 return OK;
4186}
4187
4188/*
4189 * Handle first level expression:
4190 * expr2 || expr2 || expr2 logical OR
4191 *
4192 * "arg" must point to the first non-white of the expression.
4193 * "arg" is advanced to the next non-white after the recognized expression.
4194 *
4195 * Return OK or FAIL.
4196 */
4197 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004198eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199{
Bram Moolenaar33570922005-01-25 22:26:29 +00004200 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 long result;
4202 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004203 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
4205 /*
4206 * Get the first variable.
4207 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return FAIL;
4210
4211 /*
4212 * Repeat until there is no following "||".
4213 */
4214 first = TRUE;
4215 result = FALSE;
4216 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4217 {
4218 if (evaluate && first)
4219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004220 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 if (error)
4224 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 first = FALSE;
4226 }
4227
4228 /*
4229 * Get the second variable.
4230 */
4231 *arg = skipwhite(*arg + 2);
4232 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4233 return FAIL;
4234
4235 /*
4236 * Compute the result.
4237 */
4238 if (evaluate && !result)
4239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004240 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (error)
4244 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246 if (evaluate)
4247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 rettv->v_type = VAR_NUMBER;
4249 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251 }
4252
4253 return OK;
4254}
4255
4256/*
4257 * Handle second level expression:
4258 * expr3 && expr3 && expr3 logical AND
4259 *
4260 * "arg" must point to the first non-white of the expression.
4261 * "arg" is advanced to the next non-white after the recognized expression.
4262 *
4263 * Return OK or FAIL.
4264 */
4265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004266eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267{
Bram Moolenaar33570922005-01-25 22:26:29 +00004268 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 long result;
4270 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
4273 /*
4274 * Get the first variable.
4275 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 return FAIL;
4278
4279 /*
4280 * Repeat until there is no following "&&".
4281 */
4282 first = TRUE;
4283 result = TRUE;
4284 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4285 {
4286 if (evaluate && first)
4287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004288 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004291 if (error)
4292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 first = FALSE;
4294 }
4295
4296 /*
4297 * Get the second variable.
4298 */
4299 *arg = skipwhite(*arg + 2);
4300 if (eval4(arg, &var2, evaluate && result) == FAIL)
4301 return FAIL;
4302
4303 /*
4304 * Compute the result.
4305 */
4306 if (evaluate && result)
4307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004308 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004310 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004311 if (error)
4312 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314 if (evaluate)
4315 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 rettv->v_type = VAR_NUMBER;
4317 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319 }
4320
4321 return OK;
4322}
4323
4324/*
4325 * Handle third level expression:
4326 * var1 == var2
4327 * var1 =~ var2
4328 * var1 != var2
4329 * var1 !~ var2
4330 * var1 > var2
4331 * var1 >= var2
4332 * var1 < var2
4333 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004334 * var1 is var2
4335 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 *
4337 * "arg" must point to the first non-white of the expression.
4338 * "arg" is advanced to the next non-white after the recognized expression.
4339 *
4340 * Return OK or FAIL.
4341 */
4342 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004343eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344{
Bram Moolenaar33570922005-01-25 22:26:29 +00004345 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 char_u *p;
4347 int i;
4348 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004349 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 int len = 2;
4351 long n1, n2;
4352 char_u *s1, *s2;
4353 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4354 regmatch_T regmatch;
4355 int ic;
4356 char_u *save_cpo;
4357
4358 /*
4359 * Get the first variable.
4360 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004361 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 return FAIL;
4363
4364 p = *arg;
4365 switch (p[0])
4366 {
4367 case '=': if (p[1] == '=')
4368 type = TYPE_EQUAL;
4369 else if (p[1] == '~')
4370 type = TYPE_MATCH;
4371 break;
4372 case '!': if (p[1] == '=')
4373 type = TYPE_NEQUAL;
4374 else if (p[1] == '~')
4375 type = TYPE_NOMATCH;
4376 break;
4377 case '>': if (p[1] != '=')
4378 {
4379 type = TYPE_GREATER;
4380 len = 1;
4381 }
4382 else
4383 type = TYPE_GEQUAL;
4384 break;
4385 case '<': if (p[1] != '=')
4386 {
4387 type = TYPE_SMALLER;
4388 len = 1;
4389 }
4390 else
4391 type = TYPE_SEQUAL;
4392 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004393 case 'i': if (p[1] == 's')
4394 {
4395 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4396 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004397 i = p[len];
4398 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004399 {
4400 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4401 type_is = TRUE;
4402 }
4403 }
4404 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406
4407 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004408 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 */
4410 if (type != TYPE_UNKNOWN)
4411 {
4412 /* extra question mark appended: ignore case */
4413 if (p[len] == '?')
4414 {
4415 ic = TRUE;
4416 ++len;
4417 }
4418 /* extra '#' appended: match case */
4419 else if (p[len] == '#')
4420 {
4421 ic = FALSE;
4422 ++len;
4423 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004424 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 else
4426 ic = p_ic;
4427
4428 /*
4429 * Get the second variable.
4430 */
4431 *arg = skipwhite(p + len);
4432 if (eval5(arg, &var2, evaluate) == FAIL)
4433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004434 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 return FAIL;
4436 }
4437
4438 if (evaluate)
4439 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 if (type_is && rettv->v_type != var2.v_type)
4441 {
4442 /* For "is" a different type always means FALSE, for "notis"
4443 * it means TRUE. */
4444 n1 = (type == TYPE_NEQUAL);
4445 }
4446 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4447 {
4448 if (type_is)
4449 {
4450 n1 = (rettv->v_type == var2.v_type
4451 && rettv->vval.v_list == var2.vval.v_list);
4452 if (type == TYPE_NEQUAL)
4453 n1 = !n1;
4454 }
4455 else if (rettv->v_type != var2.v_type
4456 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4457 {
4458 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004459 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004460 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004461 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004462 clear_tv(rettv);
4463 clear_tv(&var2);
4464 return FAIL;
4465 }
4466 else
4467 {
4468 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004469 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4470 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004471 if (type == TYPE_NEQUAL)
4472 n1 = !n1;
4473 }
4474 }
4475
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004476 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4477 {
4478 if (type_is)
4479 {
4480 n1 = (rettv->v_type == var2.v_type
4481 && rettv->vval.v_dict == var2.vval.v_dict);
4482 if (type == TYPE_NEQUAL)
4483 n1 = !n1;
4484 }
4485 else if (rettv->v_type != var2.v_type
4486 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4487 {
4488 if (rettv->v_type != var2.v_type)
4489 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4490 else
4491 EMSG(_("E736: Invalid operation for Dictionary"));
4492 clear_tv(rettv);
4493 clear_tv(&var2);
4494 return FAIL;
4495 }
4496 else
4497 {
4498 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004499 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4500 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004501 if (type == TYPE_NEQUAL)
4502 n1 = !n1;
4503 }
4504 }
4505
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4507 {
4508 if (rettv->v_type != var2.v_type
4509 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4510 {
4511 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004512 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004514 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004515 clear_tv(rettv);
4516 clear_tv(&var2);
4517 return FAIL;
4518 }
4519 else
4520 {
4521 /* Compare two Funcrefs for being equal or unequal. */
4522 if (rettv->vval.v_string == NULL
4523 || var2.vval.v_string == NULL)
4524 n1 = FALSE;
4525 else
4526 n1 = STRCMP(rettv->vval.v_string,
4527 var2.vval.v_string) == 0;
4528 if (type == TYPE_NEQUAL)
4529 n1 = !n1;
4530 }
4531 }
4532
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004533#ifdef FEAT_FLOAT
4534 /*
4535 * If one of the two variables is a float, compare as a float.
4536 * When using "=~" or "!~", always compare as string.
4537 */
4538 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4539 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4540 {
4541 float_T f1, f2;
4542
4543 if (rettv->v_type == VAR_FLOAT)
4544 f1 = rettv->vval.v_float;
4545 else
4546 f1 = get_tv_number(rettv);
4547 if (var2.v_type == VAR_FLOAT)
4548 f2 = var2.vval.v_float;
4549 else
4550 f2 = get_tv_number(&var2);
4551 n1 = FALSE;
4552 switch (type)
4553 {
4554 case TYPE_EQUAL: n1 = (f1 == f2); break;
4555 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4556 case TYPE_GREATER: n1 = (f1 > f2); break;
4557 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4558 case TYPE_SMALLER: n1 = (f1 < f2); break;
4559 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4560 case TYPE_UNKNOWN:
4561 case TYPE_MATCH:
4562 case TYPE_NOMATCH: break; /* avoid gcc warning */
4563 }
4564 }
4565#endif
4566
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 /*
4568 * If one of the two variables is a number, compare as a number.
4569 * When using "=~" or "!~", always compare as string.
4570 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004571 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004574 n1 = get_tv_number(rettv);
4575 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 switch (type)
4577 {
4578 case TYPE_EQUAL: n1 = (n1 == n2); break;
4579 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4580 case TYPE_GREATER: n1 = (n1 > n2); break;
4581 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4582 case TYPE_SMALLER: n1 = (n1 < n2); break;
4583 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4584 case TYPE_UNKNOWN:
4585 case TYPE_MATCH:
4586 case TYPE_NOMATCH: break; /* avoid gcc warning */
4587 }
4588 }
4589 else
4590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 s1 = get_tv_string_buf(rettv, buf1);
4592 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4594 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4595 else
4596 i = 0;
4597 n1 = FALSE;
4598 switch (type)
4599 {
4600 case TYPE_EQUAL: n1 = (i == 0); break;
4601 case TYPE_NEQUAL: n1 = (i != 0); break;
4602 case TYPE_GREATER: n1 = (i > 0); break;
4603 case TYPE_GEQUAL: n1 = (i >= 0); break;
4604 case TYPE_SMALLER: n1 = (i < 0); break;
4605 case TYPE_SEQUAL: n1 = (i <= 0); break;
4606
4607 case TYPE_MATCH:
4608 case TYPE_NOMATCH:
4609 /* avoid 'l' flag in 'cpoptions' */
4610 save_cpo = p_cpo;
4611 p_cpo = (char_u *)"";
4612 regmatch.regprog = vim_regcomp(s2,
4613 RE_MAGIC + RE_STRING);
4614 regmatch.rm_ic = ic;
4615 if (regmatch.regprog != NULL)
4616 {
4617 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004618 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 if (type == TYPE_NOMATCH)
4620 n1 = !n1;
4621 }
4622 p_cpo = save_cpo;
4623 break;
4624
4625 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4626 }
4627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 clear_tv(rettv);
4629 clear_tv(&var2);
4630 rettv->v_type = VAR_NUMBER;
4631 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
4633 }
4634
4635 return OK;
4636}
4637
4638/*
4639 * Handle fourth level expression:
4640 * + number addition
4641 * - number subtraction
4642 * . string concatenation
4643 *
4644 * "arg" must point to the first non-white of the expression.
4645 * "arg" is advanced to the next non-white after the recognized expression.
4646 *
4647 * Return OK or FAIL.
4648 */
4649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004650eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651{
Bram Moolenaar33570922005-01-25 22:26:29 +00004652 typval_T var2;
4653 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 int op;
4655 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004656#ifdef FEAT_FLOAT
4657 float_T f1 = 0, f2 = 0;
4658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 char_u *s1, *s2;
4660 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4661 char_u *p;
4662
4663 /*
4664 * Get the first variable.
4665 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004666 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 return FAIL;
4668
4669 /*
4670 * Repeat computing, until no '+', '-' or '.' is following.
4671 */
4672 for (;;)
4673 {
4674 op = **arg;
4675 if (op != '+' && op != '-' && op != '.')
4676 break;
4677
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004678 if ((op != '+' || rettv->v_type != VAR_LIST)
4679#ifdef FEAT_FLOAT
4680 && (op == '.' || rettv->v_type != VAR_FLOAT)
4681#endif
4682 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004683 {
4684 /* For "list + ...", an illegal use of the first operand as
4685 * a number cannot be determined before evaluating the 2nd
4686 * operand: if this is also a list, all is ok.
4687 * For "something . ...", "something - ..." or "non-list + ...",
4688 * we know that the first operand needs to be a string or number
4689 * without evaluating the 2nd operand. So check before to avoid
4690 * side effects after an error. */
4691 if (evaluate && get_tv_string_chk(rettv) == NULL)
4692 {
4693 clear_tv(rettv);
4694 return FAIL;
4695 }
4696 }
4697
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 /*
4699 * Get the second variable.
4700 */
4701 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004702 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004704 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return FAIL;
4706 }
4707
4708 if (evaluate)
4709 {
4710 /*
4711 * Compute the result.
4712 */
4713 if (op == '.')
4714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004715 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4716 s2 = get_tv_string_buf_chk(&var2, buf2);
4717 if (s2 == NULL) /* type error ? */
4718 {
4719 clear_tv(rettv);
4720 clear_tv(&var2);
4721 return FAIL;
4722 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004723 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004724 clear_tv(rettv);
4725 rettv->v_type = VAR_STRING;
4726 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004728 else if (op == '+' && rettv->v_type == VAR_LIST
4729 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004730 {
4731 /* concatenate Lists */
4732 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4733 &var3) == FAIL)
4734 {
4735 clear_tv(rettv);
4736 clear_tv(&var2);
4737 return FAIL;
4738 }
4739 clear_tv(rettv);
4740 *rettv = var3;
4741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 else
4743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004744 int error = FALSE;
4745
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746#ifdef FEAT_FLOAT
4747 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004749 f1 = rettv->vval.v_float;
4750 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004751 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004752 else
4753#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004755 n1 = get_tv_number_chk(rettv, &error);
4756 if (error)
4757 {
4758 /* This can only happen for "list + non-list". For
4759 * "non-list + ..." or "something - ...", we returned
4760 * before evaluating the 2nd operand. */
4761 clear_tv(rettv);
4762 return FAIL;
4763 }
4764#ifdef FEAT_FLOAT
4765 if (var2.v_type == VAR_FLOAT)
4766 f1 = n1;
4767#endif
4768 }
4769#ifdef FEAT_FLOAT
4770 if (var2.v_type == VAR_FLOAT)
4771 {
4772 f2 = var2.vval.v_float;
4773 n2 = 0;
4774 }
4775 else
4776#endif
4777 {
4778 n2 = get_tv_number_chk(&var2, &error);
4779 if (error)
4780 {
4781 clear_tv(rettv);
4782 clear_tv(&var2);
4783 return FAIL;
4784 }
4785#ifdef FEAT_FLOAT
4786 if (rettv->v_type == VAR_FLOAT)
4787 f2 = n2;
4788#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004790 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791
4792#ifdef FEAT_FLOAT
4793 /* If there is a float on either side the result is a float. */
4794 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4795 {
4796 if (op == '+')
4797 f1 = f1 + f2;
4798 else
4799 f1 = f1 - f2;
4800 rettv->v_type = VAR_FLOAT;
4801 rettv->vval.v_float = f1;
4802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004804#endif
4805 {
4806 if (op == '+')
4807 n1 = n1 + n2;
4808 else
4809 n1 = n1 - n2;
4810 rettv->v_type = VAR_NUMBER;
4811 rettv->vval.v_number = n1;
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816 }
4817 return OK;
4818}
4819
4820/*
4821 * Handle fifth level expression:
4822 * * number multiplication
4823 * / number division
4824 * % number modulo
4825 *
4826 * "arg" must point to the first non-white of the expression.
4827 * "arg" is advanced to the next non-white after the recognized expression.
4828 *
4829 * Return OK or FAIL.
4830 */
4831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004832eval6(
4833 char_u **arg,
4834 typval_T *rettv,
4835 int evaluate,
4836 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
Bram Moolenaar33570922005-01-25 22:26:29 +00004838 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 int op;
4840 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841#ifdef FEAT_FLOAT
4842 int use_float = FALSE;
4843 float_T f1 = 0, f2;
4844#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004845 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846
4847 /*
4848 * Get the first variable.
4849 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004850 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 return FAIL;
4852
4853 /*
4854 * Repeat computing, until no '*', '/' or '%' is following.
4855 */
4856 for (;;)
4857 {
4858 op = **arg;
4859 if (op != '*' && op != '/' && op != '%')
4860 break;
4861
4862 if (evaluate)
4863 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004864#ifdef FEAT_FLOAT
4865 if (rettv->v_type == VAR_FLOAT)
4866 {
4867 f1 = rettv->vval.v_float;
4868 use_float = TRUE;
4869 n1 = 0;
4870 }
4871 else
4872#endif
4873 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004875 if (error)
4876 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878 else
4879 n1 = 0;
4880
4881 /*
4882 * Get the second variable.
4883 */
4884 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004885 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 return FAIL;
4887
4888 if (evaluate)
4889 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890#ifdef FEAT_FLOAT
4891 if (var2.v_type == VAR_FLOAT)
4892 {
4893 if (!use_float)
4894 {
4895 f1 = n1;
4896 use_float = TRUE;
4897 }
4898 f2 = var2.vval.v_float;
4899 n2 = 0;
4900 }
4901 else
4902#endif
4903 {
4904 n2 = get_tv_number_chk(&var2, &error);
4905 clear_tv(&var2);
4906 if (error)
4907 return FAIL;
4908#ifdef FEAT_FLOAT
4909 if (use_float)
4910 f2 = n2;
4911#endif
4912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
4914 /*
4915 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004918#ifdef FEAT_FLOAT
4919 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 if (op == '*')
4922 f1 = f1 * f2;
4923 else if (op == '/')
4924 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004925# ifdef VMS
4926 /* VMS crashes on divide by zero, work around it */
4927 if (f2 == 0.0)
4928 {
4929 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004930 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004931 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004932 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004933 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004934 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004935 }
4936 else
4937 f1 = f1 / f2;
4938# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939 /* We rely on the floating point library to handle divide
4940 * by zero to result in "inf" and not a crash. */
4941 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004942# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004945 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004946 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004947 return FAIL;
4948 }
4949 rettv->v_type = VAR_FLOAT;
4950 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955 if (op == '*')
4956 n1 = n1 * n2;
4957 else if (op == '/')
4958 {
4959 if (n2 == 0) /* give an error message? */
4960 {
4961 if (n1 == 0)
4962 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4963 else if (n1 < 0)
4964 n1 = -0x7fffffffL;
4965 else
4966 n1 = 0x7fffffffL;
4967 }
4968 else
4969 n1 = n1 / n2;
4970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 {
4973 if (n2 == 0) /* give an error message? */
4974 n1 = 0;
4975 else
4976 n1 = n1 % n2;
4977 }
4978 rettv->v_type = VAR_NUMBER;
4979 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982 }
4983
4984 return OK;
4985}
4986
4987/*
4988 * Handle sixth level expression:
4989 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004990 * "string" string constant
4991 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 * &option-name option value
4993 * @r register contents
4994 * identifier variable value
4995 * function() function call
4996 * $VAR environment variable
4997 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004998 * [expr, expr] List
4999 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 *
5001 * Also handle:
5002 * ! in front logical NOT
5003 * - in front unary minus
5004 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 * trailing [] subscript in String or List
5006 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 *
5008 * "arg" must point to the first non-white of the expression.
5009 * "arg" is advanced to the next non-white after the recognized expression.
5010 *
5011 * Return OK or FAIL.
5012 */
5013 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005014eval7(
5015 char_u **arg,
5016 typval_T *rettv,
5017 int evaluate,
5018 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 long n;
5021 int len;
5022 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 char_u *start_leader, *end_leader;
5024 int ret = OK;
5025 char_u *alias;
5026
5027 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005028 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005029 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005031 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
5033 /*
5034 * Skip '!' and '-' characters. They are handled later.
5035 */
5036 start_leader = *arg;
5037 while (**arg == '!' || **arg == '-' || **arg == '+')
5038 *arg = skipwhite(*arg + 1);
5039 end_leader = *arg;
5040
5041 switch (**arg)
5042 {
5043 /*
5044 * Number constant.
5045 */
5046 case '0':
5047 case '1':
5048 case '2':
5049 case '3':
5050 case '4':
5051 case '5':
5052 case '6':
5053 case '7':
5054 case '8':
5055 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005056 {
5057#ifdef FEAT_FLOAT
5058 char_u *p = skipdigits(*arg + 1);
5059 int get_float = FALSE;
5060
5061 /* We accept a float when the format matches
5062 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005063 * strict to avoid backwards compatibility problems.
5064 * Don't look for a float after the "." operator, so that
5065 * ":let vers = 1.2.3" doesn't fail. */
5066 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005068 get_float = TRUE;
5069 p = skipdigits(p + 2);
5070 if (*p == 'e' || *p == 'E')
5071 {
5072 ++p;
5073 if (*p == '-' || *p == '+')
5074 ++p;
5075 if (!vim_isdigit(*p))
5076 get_float = FALSE;
5077 else
5078 p = skipdigits(p + 1);
5079 }
5080 if (ASCII_ISALPHA(*p) || *p == '.')
5081 get_float = FALSE;
5082 }
5083 if (get_float)
5084 {
5085 float_T f;
5086
5087 *arg += string2float(*arg, &f);
5088 if (evaluate)
5089 {
5090 rettv->v_type = VAR_FLOAT;
5091 rettv->vval.v_float = f;
5092 }
5093 }
5094 else
5095#endif
5096 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005097 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005098 *arg += len;
5099 if (evaluate)
5100 {
5101 rettv->v_type = VAR_NUMBER;
5102 rettv->vval.v_number = n;
5103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
5108 /*
5109 * String constant: "string".
5110 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005111 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 break;
5113
5114 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005115 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005118 break;
5119
5120 /*
5121 * List: [expr, expr]
5122 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 break;
5125
5126 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 * Dictionary: {key: val, key: val}
5128 */
5129 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5130 break;
5131
5132 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005133 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005135 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 break;
5137
5138 /*
5139 * Environment variable: $VAR.
5140 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005141 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 break;
5143
5144 /*
5145 * Register contents: @r.
5146 */
5147 case '@': ++*arg;
5148 if (evaluate)
5149 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005151 rettv->vval.v_string = get_reg_contents(**arg,
5152 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154 if (**arg != NUL)
5155 ++*arg;
5156 break;
5157
5158 /*
5159 * nested expression: (expression).
5160 */
5161 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 if (**arg == ')')
5164 ++*arg;
5165 else if (ret == OK)
5166 {
5167 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 ret = FAIL;
5170 }
5171 break;
5172
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 break;
5175 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176
5177 if (ret == NOTDONE)
5178 {
5179 /*
5180 * Must be a variable or function name.
5181 * Can also be a curly-braces kind of name: {expr}.
5182 */
5183 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005184 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 if (alias != NULL)
5186 s = alias;
5187
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005188 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 ret = FAIL;
5190 else
5191 {
5192 if (**arg == '(') /* recursive! */
5193 {
5194 /* If "s" is the name of a variable of type VAR_FUNC
5195 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005196 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197
5198 /* Invoke the function. */
5199 ret = get_func_tv(s, len, rettv, arg,
5200 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005201 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005202
5203 /* If evaluate is FALSE rettv->v_type was not set in
5204 * get_func_tv, but it's needed in handle_subscript() to parse
5205 * what follows. So set it here. */
5206 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5207 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005208 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005209 rettv->v_type = VAR_FUNC;
5210 }
5211
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 /* Stop the expression evaluation when immediately
5213 * aborting on error, or when an interrupt occurred or
5214 * an exception was thrown but not caught. */
5215 if (aborting())
5216 {
5217 if (ret == OK)
5218 clear_tv(rettv);
5219 ret = FAIL;
5220 }
5221 }
5222 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005223 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005224 else
5225 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005227 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 }
5229
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 *arg = skipwhite(*arg);
5231
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005232 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5233 * expr(expr). */
5234 if (ret == OK)
5235 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
5237 /*
5238 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5239 */
5240 if (ret == OK && evaluate && end_leader > start_leader)
5241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005242 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005243 int val = 0;
5244#ifdef FEAT_FLOAT
5245 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005246
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005247 if (rettv->v_type == VAR_FLOAT)
5248 f = rettv->vval.v_float;
5249 else
5250#endif
5251 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005252 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005254 clear_tv(rettv);
5255 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005257 else
5258 {
5259 while (end_leader > start_leader)
5260 {
5261 --end_leader;
5262 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005263 {
5264#ifdef FEAT_FLOAT
5265 if (rettv->v_type == VAR_FLOAT)
5266 f = !f;
5267 else
5268#endif
5269 val = !val;
5270 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005271 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005272 {
5273#ifdef FEAT_FLOAT
5274 if (rettv->v_type == VAR_FLOAT)
5275 f = -f;
5276 else
5277#endif
5278 val = -val;
5279 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005281#ifdef FEAT_FLOAT
5282 if (rettv->v_type == VAR_FLOAT)
5283 {
5284 clear_tv(rettv);
5285 rettv->vval.v_float = f;
5286 }
5287 else
5288#endif
5289 {
5290 clear_tv(rettv);
5291 rettv->v_type = VAR_NUMBER;
5292 rettv->vval.v_number = val;
5293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296
5297 return ret;
5298}
5299
5300/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005301 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5302 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5304 */
5305 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005306eval_index(
5307 char_u **arg,
5308 typval_T *rettv,
5309 int evaluate,
5310 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311{
5312 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005313 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 long len = -1;
5316 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005320 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005322 if (verbose)
5323 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 return FAIL;
5325 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005326#ifdef FEAT_FLOAT
5327 else if (rettv->v_type == VAR_FLOAT)
5328 {
5329 if (verbose)
5330 EMSG(_(e_float_as_string));
5331 return FAIL;
5332 }
5333#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005334 else if (rettv->v_type == VAR_SPECIAL)
5335 {
5336 return FAIL;
5337 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005339 init_tv(&var1);
5340 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 /*
5344 * dict.name
5345 */
5346 key = *arg + 1;
5347 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5348 ;
5349 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 }
5353 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 /*
5356 * something[idx]
5357 *
5358 * Get the (first) variable from inside the [].
5359 */
5360 *arg = skipwhite(*arg + 1);
5361 if (**arg == ':')
5362 empty1 = TRUE;
5363 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5364 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005365 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5366 {
5367 /* not a number or string */
5368 clear_tv(&var1);
5369 return FAIL;
5370 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371
5372 /*
5373 * Get the second variable from inside the [:].
5374 */
5375 if (**arg == ':')
5376 {
5377 range = TRUE;
5378 *arg = skipwhite(*arg + 1);
5379 if (**arg == ']')
5380 empty2 = TRUE;
5381 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005383 if (!empty1)
5384 clear_tv(&var1);
5385 return FAIL;
5386 }
5387 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5388 {
5389 /* not a number or string */
5390 if (!empty1)
5391 clear_tv(&var1);
5392 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 return FAIL;
5394 }
5395 }
5396
5397 /* Check for the ']'. */
5398 if (**arg != ']')
5399 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005400 if (verbose)
5401 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 clear_tv(&var1);
5403 if (range)
5404 clear_tv(&var2);
5405 return FAIL;
5406 }
5407 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 }
5409
5410 if (evaluate)
5411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005412 n1 = 0;
5413 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 n1 = get_tv_number(&var1);
5416 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 }
5418 if (range)
5419 {
5420 if (empty2)
5421 n2 = -1;
5422 else
5423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005424 n2 = get_tv_number(&var2);
5425 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 }
5427 }
5428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005429 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 {
5431 case VAR_NUMBER:
5432 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005433 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 len = (long)STRLEN(s);
5435 if (range)
5436 {
5437 /* The resulting variable is a substring. If the indexes
5438 * are out of range the result is empty. */
5439 if (n1 < 0)
5440 {
5441 n1 = len + n1;
5442 if (n1 < 0)
5443 n1 = 0;
5444 }
5445 if (n2 < 0)
5446 n2 = len + n2;
5447 else if (n2 >= len)
5448 n2 = len;
5449 if (n1 >= len || n2 < 0 || n1 > n2)
5450 s = NULL;
5451 else
5452 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5453 }
5454 else
5455 {
5456 /* The resulting variable is a string of a single
5457 * character. If the index is too big or negative the
5458 * result is empty. */
5459 if (n1 >= len || n1 < 0)
5460 s = NULL;
5461 else
5462 s = vim_strnsave(s + n1, 1);
5463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 clear_tv(rettv);
5465 rettv->v_type = VAR_STRING;
5466 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 break;
5468
5469 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 if (n1 < 0)
5472 n1 = len + n1;
5473 if (!empty1 && (n1 < 0 || n1 >= len))
5474 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005475 /* For a range we allow invalid values and return an empty
5476 * list. A list index out of range is an error. */
5477 if (!range)
5478 {
5479 if (verbose)
5480 EMSGN(_(e_listidx), n1);
5481 return FAIL;
5482 }
5483 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 }
5485 if (range)
5486 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005487 list_T *l;
5488 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489
5490 if (n2 < 0)
5491 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005492 else if (n2 >= len)
5493 n2 = len - 1;
5494 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005495 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 l = list_alloc();
5497 if (l == NULL)
5498 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005499 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 n1 <= n2; ++n1)
5501 {
5502 if (list_append_tv(l, &item->li_tv) == FAIL)
5503 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005504 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 return FAIL;
5506 }
5507 item = item->li_next;
5508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005509 clear_tv(rettv);
5510 rettv->v_type = VAR_LIST;
5511 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005512 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 }
5514 else
5515 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005516 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517 clear_tv(rettv);
5518 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 }
5520 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005521
5522 case VAR_DICT:
5523 if (range)
5524 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005525 if (verbose)
5526 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 if (len == -1)
5528 clear_tv(&var1);
5529 return FAIL;
5530 }
5531 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005532 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005533
5534 if (len == -1)
5535 {
5536 key = get_tv_string(&var1);
5537 if (*key == NUL)
5538 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005539 if (verbose)
5540 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005541 clear_tv(&var1);
5542 return FAIL;
5543 }
5544 }
5545
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005546 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005547
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005548 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005549 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550 if (len == -1)
5551 clear_tv(&var1);
5552 if (item == NULL)
5553 return FAIL;
5554
5555 copy_tv(&item->di_tv, &var1);
5556 clear_tv(rettv);
5557 *rettv = var1;
5558 }
5559 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 }
5561 }
5562
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 return OK;
5564}
5565
5566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 * Get an option value.
5568 * "arg" points to the '&' or '+' before the option name.
5569 * "arg" is advanced to character after the option name.
5570 * Return OK or FAIL.
5571 */
5572 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005573get_option_tv(
5574 char_u **arg,
5575 typval_T *rettv, /* when NULL, only check if option exists */
5576 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 char_u *option_end;
5579 long numval;
5580 char_u *stringval;
5581 int opt_type;
5582 int c;
5583 int working = (**arg == '+'); /* has("+option") */
5584 int ret = OK;
5585 int opt_flags;
5586
5587 /*
5588 * Isolate the option name and find its value.
5589 */
5590 option_end = find_option_end(arg, &opt_flags);
5591 if (option_end == NULL)
5592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 EMSG2(_("E112: Option name missing: %s"), *arg);
5595 return FAIL;
5596 }
5597
5598 if (!evaluate)
5599 {
5600 *arg = option_end;
5601 return OK;
5602 }
5603
5604 c = *option_end;
5605 *option_end = NUL;
5606 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005607 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
5609 if (opt_type == -3) /* invalid name */
5610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005611 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 EMSG2(_("E113: Unknown option: %s"), *arg);
5613 ret = FAIL;
5614 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005615 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 {
5617 if (opt_type == -2) /* hidden string option */
5618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005619 rettv->v_type = VAR_STRING;
5620 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 }
5622 else if (opt_type == -1) /* hidden number option */
5623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 rettv->v_type = VAR_NUMBER;
5625 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627 else if (opt_type == 1) /* number option */
5628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629 rettv->v_type = VAR_NUMBER;
5630 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 }
5632 else /* string option */
5633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634 rettv->v_type = VAR_STRING;
5635 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
5637 }
5638 else if (working && (opt_type == -2 || opt_type == -1))
5639 ret = FAIL;
5640
5641 *option_end = c; /* put back for error messages */
5642 *arg = option_end;
5643
5644 return ret;
5645}
5646
5647/*
5648 * Allocate a variable for a string constant.
5649 * Return OK or FAIL.
5650 */
5651 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005652get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 char_u *p;
5655 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 int extra = 0;
5657
5658 /*
5659 * Find the end of the string, skipping backslashed characters.
5660 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005661 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 {
5663 if (*p == '\\' && p[1] != NUL)
5664 {
5665 ++p;
5666 /* A "\<x>" form occupies at least 4 characters, and produces up
5667 * to 6 characters: reserve space for 2 extra */
5668 if (*p == '<')
5669 extra += 2;
5670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
5672
5673 if (*p != '"')
5674 {
5675 EMSG2(_("E114: Missing quote: %s"), *arg);
5676 return FAIL;
5677 }
5678
5679 /* If only parsing, set *arg and return here */
5680 if (!evaluate)
5681 {
5682 *arg = p + 1;
5683 return OK;
5684 }
5685
5686 /*
5687 * Copy the string into allocated memory, handling backslashed
5688 * characters.
5689 */
5690 name = alloc((unsigned)(p - *arg + extra));
5691 if (name == NULL)
5692 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 rettv->v_type = VAR_STRING;
5694 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
5698 if (*p == '\\')
5699 {
5700 switch (*++p)
5701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 case 'b': *name++ = BS; ++p; break;
5703 case 'e': *name++ = ESC; ++p; break;
5704 case 'f': *name++ = FF; ++p; break;
5705 case 'n': *name++ = NL; ++p; break;
5706 case 'r': *name++ = CAR; ++p; break;
5707 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708
5709 case 'X': /* hex: "\x1", "\x12" */
5710 case 'x':
5711 case 'u': /* Unicode: "\u0023" */
5712 case 'U':
5713 if (vim_isxdigit(p[1]))
5714 {
5715 int n, nr;
5716 int c = toupper(*p);
5717
5718 if (c == 'X')
5719 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005720 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005722 else
5723 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 nr = 0;
5725 while (--n >= 0 && vim_isxdigit(p[1]))
5726 {
5727 ++p;
5728 nr = (nr << 4) + hex2nr(*p);
5729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731#ifdef FEAT_MBYTE
5732 /* For "\u" store the number according to
5733 * 'encoding'. */
5734 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 else
5737#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 break;
5741
5742 /* octal: "\1", "\12", "\123" */
5743 case '0':
5744 case '1':
5745 case '2':
5746 case '3':
5747 case '4':
5748 case '5':
5749 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 case '7': *name = *p++ - '0';
5751 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 *name = (*name << 3) + *p++ - '0';
5754 if (*p >= '0' && *p <= '7')
5755 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 break;
5759
5760 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 if (extra != 0)
5763 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005764 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 break;
5766 }
5767 /* FALLTHROUGH */
5768
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 break;
5771 }
5772 }
5773 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 *arg = p + 1;
5779
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 return OK;
5781}
5782
5783/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 * Return OK or FAIL.
5786 */
5787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005788get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
5790 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005791 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 int reduce = 0;
5793
5794 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005796 */
5797 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5798 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005802 break;
5803 ++reduce;
5804 ++p;
5805 }
5806 }
5807
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005811 return FAIL;
5812 }
5813
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 if (!evaluate)
5816 {
5817 *arg = p + 1;
5818 return OK;
5819 }
5820
5821 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005823 */
5824 str = alloc((unsigned)((p - *arg) - reduce));
5825 if (str == NULL)
5826 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 rettv->v_type = VAR_STRING;
5828 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005829
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005831 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005835 break;
5836 ++p;
5837 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005839 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005841 *arg = p + 1;
5842
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 return OK;
5844}
5845
5846/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 * Allocate a variable for a List and fill it from "*arg".
5848 * Return OK or FAIL.
5849 */
5850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005851get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852{
Bram Moolenaar33570922005-01-25 22:26:29 +00005853 list_T *l = NULL;
5854 typval_T tv;
5855 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856
5857 if (evaluate)
5858 {
5859 l = list_alloc();
5860 if (l == NULL)
5861 return FAIL;
5862 }
5863
5864 *arg = skipwhite(*arg + 1);
5865 while (**arg != ']' && **arg != NUL)
5866 {
5867 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5868 goto failret;
5869 if (evaluate)
5870 {
5871 item = listitem_alloc();
5872 if (item != NULL)
5873 {
5874 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005875 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876 list_append(l, item);
5877 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005878 else
5879 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 }
5881
5882 if (**arg == ']')
5883 break;
5884 if (**arg != ',')
5885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887 goto failret;
5888 }
5889 *arg = skipwhite(*arg + 1);
5890 }
5891
5892 if (**arg != ']')
5893 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895failret:
5896 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005897 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898 return FAIL;
5899 }
5900
5901 *arg = skipwhite(*arg + 1);
5902 if (evaluate)
5903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005904 rettv->v_type = VAR_LIST;
5905 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 ++l->lv_refcount;
5907 }
5908
5909 return OK;
5910}
5911
5912/*
5913 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005914 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005916 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005917list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005919 list_T *l;
5920
5921 l = (list_T *)alloc_clear(sizeof(list_T));
5922 if (l != NULL)
5923 {
5924 /* Prepend the list to the list of lists for garbage collection. */
5925 if (first_list != NULL)
5926 first_list->lv_used_prev = l;
5927 l->lv_used_prev = NULL;
5928 l->lv_used_next = first_list;
5929 first_list = l;
5930 }
5931 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932}
5933
5934/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005935 * Allocate an empty list for a return value.
5936 * Returns OK or FAIL.
5937 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005938 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005939rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005940{
5941 list_T *l = list_alloc();
5942
5943 if (l == NULL)
5944 return FAIL;
5945
5946 rettv->vval.v_list = l;
5947 rettv->v_type = VAR_LIST;
5948 ++l->lv_refcount;
5949 return OK;
5950}
5951
5952/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953 * Unreference a list: decrement the reference count and free it when it
5954 * becomes zero.
5955 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005957list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005959 if (l != NULL && --l->lv_refcount <= 0)
5960 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961}
5962
5963/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01005964 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 * Ignores the reference count.
5966 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005968list_free(
5969 list_T *l,
5970 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971{
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005974 /* Remove the list from the list of lists for garbage collection. */
5975 if (l->lv_used_prev == NULL)
5976 first_list = l->lv_used_next;
5977 else
5978 l->lv_used_prev->lv_used_next = l->lv_used_next;
5979 if (l->lv_used_next != NULL)
5980 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5981
Bram Moolenaard9fba312005-06-26 22:34:35 +00005982 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005984 /* Remove the item before deleting it. */
5985 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005986 if (recurse || (item->li_tv.v_type != VAR_LIST
5987 && item->li_tv.v_type != VAR_DICT))
5988 clear_tv(&item->li_tv);
5989 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 }
5991 vim_free(l);
5992}
5993
5994/*
5995 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01005996 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005998 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005999listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000{
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002}
6003
6004/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006005 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006008listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006010 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 vim_free(item);
6012}
6013
6014/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006015 * Remove a list item from a List and free it. Also clears the value.
6016 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006018listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006019{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006020 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006021 listitem_free(item);
6022}
6023
6024/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 * Get the number of items in a list.
6026 */
6027 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 if (l == NULL)
6031 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006032 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033}
6034
6035/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006036 * Return TRUE when two lists have exactly the same values.
6037 */
6038 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006039list_equal(
6040 list_T *l1,
6041 list_T *l2,
6042 int ic, /* ignore case for strings */
6043 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006044{
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006046
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006047 if (l1 == NULL || l2 == NULL)
6048 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006049 if (l1 == l2)
6050 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006051 if (list_len(l1) != list_len(l2))
6052 return FALSE;
6053
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006054 for (item1 = l1->lv_first, item2 = l2->lv_first;
6055 item1 != NULL && item2 != NULL;
6056 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006057 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006058 return FALSE;
6059 return item1 == NULL && item2 == NULL;
6060}
6061
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006062/*
6063 * Return the dictitem that an entry in a hashtable points to.
6064 */
6065 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006066dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006067{
6068 return HI2DI(hi);
6069}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006070
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006071/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006072 * Return TRUE when two dictionaries have exactly the same key/values.
6073 */
6074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075dict_equal(
6076 dict_T *d1,
6077 dict_T *d2,
6078 int ic, /* ignore case for strings */
6079 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080{
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 hashitem_T *hi;
6082 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006083 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006084
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006085 if (d1 == NULL || d2 == NULL)
6086 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006087 if (d1 == d2)
6088 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006089 if (dict_len(d1) != dict_len(d2))
6090 return FALSE;
6091
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006092 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006094 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006095 if (!HASHITEM_EMPTY(hi))
6096 {
6097 item2 = dict_find(d2, hi->hi_key, -1);
6098 if (item2 == NULL)
6099 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006101 return FALSE;
6102 --todo;
6103 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006104 }
6105 return TRUE;
6106}
6107
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006108static int tv_equal_recurse_limit;
6109
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006110/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006112 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006113 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 */
6115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116tv_equal(
6117 typval_T *tv1,
6118 typval_T *tv2,
6119 int ic, /* ignore case */
6120 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121{
6122 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006123 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006125 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006126
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006127 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006128 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006130 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006131 * recursiveness to a limit. We guess they are equal then.
6132 * A fixed limit has the problem of still taking an awful long time.
6133 * Reduce the limit every time running into it. That should work fine for
6134 * deeply linked structures that are not recursively linked and catch
6135 * recursiveness quickly. */
6136 if (!recursive)
6137 tv_equal_recurse_limit = 1000;
6138 if (recursive_cnt >= tv_equal_recurse_limit)
6139 {
6140 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006141 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006142 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006143
6144 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006145 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006146 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006147 ++recursive_cnt;
6148 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6149 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006150 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006151
6152 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006153 ++recursive_cnt;
6154 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6155 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006156 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006157
6158 case VAR_FUNC:
6159 return (tv1->vval.v_string != NULL
6160 && tv2->vval.v_string != NULL
6161 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6162
6163 case VAR_NUMBER:
6164 return tv1->vval.v_number == tv2->vval.v_number;
6165
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006166#ifdef FEAT_FLOAT
6167 case VAR_FLOAT:
6168 return tv1->vval.v_float == tv2->vval.v_float;
6169#endif
6170
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006171 case VAR_STRING:
6172 s1 = get_tv_string_buf(tv1, buf1);
6173 s2 = get_tv_string_buf(tv2, buf2);
6174 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006175
6176 case VAR_SPECIAL:
6177 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006179
6180 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181 return TRUE;
6182}
6183
6184/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 * Locate item with index "n" in list "l" and return it.
6186 * A negative index is counted from the end; -1 is the last item.
6187 * Returns NULL when "n" is out of range.
6188 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006189 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006190list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191{
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 long idx;
6194
6195 if (l == NULL)
6196 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006197
6198 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006199 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006200 n = l->lv_len + n;
6201
6202 /* Check for index out of range. */
6203 if (n < 0 || n >= l->lv_len)
6204 return NULL;
6205
6206 /* When there is a cached index may start search from there. */
6207 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006209 if (n < l->lv_idx / 2)
6210 {
6211 /* closest to the start of the list */
6212 item = l->lv_first;
6213 idx = 0;
6214 }
6215 else if (n > (l->lv_idx + l->lv_len) / 2)
6216 {
6217 /* closest to the end of the list */
6218 item = l->lv_last;
6219 idx = l->lv_len - 1;
6220 }
6221 else
6222 {
6223 /* closest to the cached index */
6224 item = l->lv_idx_item;
6225 idx = l->lv_idx;
6226 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227 }
6228 else
6229 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006230 if (n < l->lv_len / 2)
6231 {
6232 /* closest to the start of the list */
6233 item = l->lv_first;
6234 idx = 0;
6235 }
6236 else
6237 {
6238 /* closest to the end of the list */
6239 item = l->lv_last;
6240 idx = l->lv_len - 1;
6241 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006243
6244 while (n > idx)
6245 {
6246 /* search forward */
6247 item = item->li_next;
6248 ++idx;
6249 }
6250 while (n < idx)
6251 {
6252 /* search backward */
6253 item = item->li_prev;
6254 --idx;
6255 }
6256
6257 /* cache the used index */
6258 l->lv_idx = idx;
6259 l->lv_idx_item = item;
6260
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 return item;
6262}
6263
6264/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006265 * Get list item "l[idx]" as a number.
6266 */
6267 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006268list_find_nr(
6269 list_T *l,
6270 long idx,
6271 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006272{
6273 listitem_T *li;
6274
6275 li = list_find(l, idx);
6276 if (li == NULL)
6277 {
6278 if (errorp != NULL)
6279 *errorp = TRUE;
6280 return -1L;
6281 }
6282 return get_tv_number_chk(&li->li_tv, errorp);
6283}
6284
6285/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006286 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6287 */
6288 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006289list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006290{
6291 listitem_T *li;
6292
6293 li = list_find(l, idx - 1);
6294 if (li == NULL)
6295 {
6296 EMSGN(_(e_listidx), idx);
6297 return NULL;
6298 }
6299 return get_tv_string(&li->li_tv);
6300}
6301
6302/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006303 * Locate "item" list "l" and return its index.
6304 * Returns -1 when "item" is not in the list.
6305 */
6306 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006307list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006308{
6309 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006311
6312 if (l == NULL)
6313 return -1;
6314 idx = 0;
6315 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6316 ++idx;
6317 if (li == NULL)
6318 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006319 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006320}
6321
6322/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 * Append item "item" to the end of list "l".
6324 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006326list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327{
6328 if (l->lv_last == NULL)
6329 {
6330 /* empty list */
6331 l->lv_first = item;
6332 l->lv_last = item;
6333 item->li_prev = NULL;
6334 }
6335 else
6336 {
6337 l->lv_last->li_next = item;
6338 item->li_prev = l->lv_last;
6339 l->lv_last = item;
6340 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006341 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342 item->li_next = NULL;
6343}
6344
6345/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006346 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006347 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006349 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006350list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006352 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353
Bram Moolenaar05159a02005-02-26 23:04:13 +00006354 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006355 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006356 copy_tv(tv, &li->li_tv);
6357 list_append(l, li);
6358 return OK;
6359}
6360
6361/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006362 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006363 * Return FAIL when out of memory.
6364 */
6365 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006366list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006367{
6368 listitem_T *li = listitem_alloc();
6369
6370 if (li == NULL)
6371 return FAIL;
6372 li->li_tv.v_type = VAR_DICT;
6373 li->li_tv.v_lock = 0;
6374 li->li_tv.vval.v_dict = dict;
6375 list_append(list, li);
6376 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377 return OK;
6378}
6379
6380/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006381 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006382 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006383 * Returns FAIL when out of memory.
6384 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006385 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006386list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006387{
6388 listitem_T *li = listitem_alloc();
6389
6390 if (li == NULL)
6391 return FAIL;
6392 list_append(l, li);
6393 li->li_tv.v_type = VAR_STRING;
6394 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006395 if (str == NULL)
6396 li->li_tv.vval.v_string = NULL;
6397 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006398 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006399 return FAIL;
6400 return OK;
6401}
6402
6403/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006404 * Append "n" to list "l".
6405 * Returns FAIL when out of memory.
6406 */
6407 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006408list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006409{
6410 listitem_T *li;
6411
6412 li = listitem_alloc();
6413 if (li == NULL)
6414 return FAIL;
6415 li->li_tv.v_type = VAR_NUMBER;
6416 li->li_tv.v_lock = 0;
6417 li->li_tv.vval.v_number = n;
6418 list_append(l, li);
6419 return OK;
6420}
6421
6422/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 * If "item" is NULL append at the end.
6425 * Return FAIL when out of memory.
6426 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006427 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006428list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429{
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431
6432 if (ni == NULL)
6433 return FAIL;
6434 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006435 list_insert(l, ni, item);
6436 return OK;
6437}
6438
6439 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006441{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006442 if (item == NULL)
6443 /* Append new item at end of list. */
6444 list_append(l, ni);
6445 else
6446 {
6447 /* Insert new item before existing item. */
6448 ni->li_prev = item->li_prev;
6449 ni->li_next = item;
6450 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006451 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006453 ++l->lv_idx;
6454 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006455 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006456 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006458 l->lv_idx_item = NULL;
6459 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006461 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463}
6464
6465/*
6466 * Extend "l1" with "l2".
6467 * If "bef" is NULL append at the end, otherwise insert before this item.
6468 * Returns FAIL when out of memory.
6469 */
6470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006471list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472{
Bram Moolenaar33570922005-01-25 22:26:29 +00006473 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006474 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006475
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006476 /* We also quit the loop when we have inserted the original item count of
6477 * the list, avoid a hang when we extend a list with itself. */
6478 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6480 return FAIL;
6481 return OK;
6482}
6483
6484/*
6485 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6486 * Return FAIL when out of memory.
6487 */
6488 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006489list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006490{
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006493 if (l1 == NULL || l2 == NULL)
6494 return FAIL;
6495
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006497 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498 if (l == NULL)
6499 return FAIL;
6500 tv->v_type = VAR_LIST;
6501 tv->vval.v_list = l;
6502
6503 /* append all items from the second list */
6504 return list_extend(l, l2, NULL);
6505}
6506
6507/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006508 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006509 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511 * Returns NULL when out of memory.
6512 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006513 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006514list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515{
Bram Moolenaar33570922005-01-25 22:26:29 +00006516 list_T *copy;
6517 listitem_T *item;
6518 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519
6520 if (orig == NULL)
6521 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522
6523 copy = list_alloc();
6524 if (copy != NULL)
6525 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526 if (copyID != 0)
6527 {
6528 /* Do this before adding the items, because one of the items may
6529 * refer back to this list. */
6530 orig->lv_copyID = copyID;
6531 orig->lv_copylist = copy;
6532 }
6533 for (item = orig->lv_first; item != NULL && !got_int;
6534 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006535 {
6536 ni = listitem_alloc();
6537 if (ni == NULL)
6538 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006539 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006540 {
6541 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6542 {
6543 vim_free(ni);
6544 break;
6545 }
6546 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006548 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549 list_append(copy, ni);
6550 }
6551 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006552 if (item != NULL)
6553 {
6554 list_unref(copy);
6555 copy = NULL;
6556 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 }
6558
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559 return copy;
6560}
6561
6562/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006564 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006565 * This used to be called list_remove, but that conflicts with a Sun header
6566 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006569vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006570{
Bram Moolenaar33570922005-01-25 22:26:29 +00006571 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573 /* notify watchers */
6574 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006576 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577 list_fix_watch(l, ip);
6578 if (ip == item2)
6579 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006581
6582 if (item2->li_next == NULL)
6583 l->lv_last = item->li_prev;
6584 else
6585 item2->li_next->li_prev = item->li_prev;
6586 if (item->li_prev == NULL)
6587 l->lv_first = item2->li_next;
6588 else
6589 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006590 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591}
6592
6593/*
6594 * Return an allocated string with the string representation of a list.
6595 * May return NULL.
6596 */
6597 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006598list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599{
6600 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601
6602 if (tv->vval.v_list == NULL)
6603 return NULL;
6604 ga_init2(&ga, (int)sizeof(char), 80);
6605 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006606 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 {
6608 vim_free(ga.ga_data);
6609 return NULL;
6610 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 ga_append(&ga, ']');
6612 ga_append(&ga, NUL);
6613 return (char_u *)ga.ga_data;
6614}
6615
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006616typedef struct join_S {
6617 char_u *s;
6618 char_u *tofree;
6619} join_T;
6620
6621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006622list_join_inner(
6623 garray_T *gap, /* to store the result in */
6624 list_T *l,
6625 char_u *sep,
6626 int echo_style,
6627 int copyID,
6628 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006629{
6630 int i;
6631 join_T *p;
6632 int len;
6633 int sumlen = 0;
6634 int first = TRUE;
6635 char_u *tofree;
6636 char_u numbuf[NUMBUFLEN];
6637 listitem_T *item;
6638 char_u *s;
6639
6640 /* Stringify each item in the list. */
6641 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6642 {
6643 if (echo_style)
6644 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6645 else
6646 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6647 if (s == NULL)
6648 return FAIL;
6649
6650 len = (int)STRLEN(s);
6651 sumlen += len;
6652
Bram Moolenaarcde88542015-08-11 19:14:00 +02006653 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006654 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6655 if (tofree != NULL || s != numbuf)
6656 {
6657 p->s = s;
6658 p->tofree = tofree;
6659 }
6660 else
6661 {
6662 p->s = vim_strnsave(s, len);
6663 p->tofree = p->s;
6664 }
6665
6666 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006667 if (did_echo_string_emsg) /* recursion error, bail out */
6668 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006669 }
6670
6671 /* Allocate result buffer with its total size, avoid re-allocation and
6672 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6673 if (join_gap->ga_len >= 2)
6674 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6675 if (ga_grow(gap, sumlen + 2) == FAIL)
6676 return FAIL;
6677
6678 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6679 {
6680 if (first)
6681 first = FALSE;
6682 else
6683 ga_concat(gap, sep);
6684 p = ((join_T *)join_gap->ga_data) + i;
6685
6686 if (p->s != NULL)
6687 ga_concat(gap, p->s);
6688 line_breakcheck();
6689 }
6690
6691 return OK;
6692}
6693
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006696 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006697 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006698 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006699 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006700list_join(
6701 garray_T *gap,
6702 list_T *l,
6703 char_u *sep,
6704 int echo_style,
6705 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006706{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006707 garray_T join_ga;
6708 int retval;
6709 join_T *p;
6710 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006711
Bram Moolenaard39a7512015-04-16 22:51:22 +02006712 if (l->lv_len < 1)
6713 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006714 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6715 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6716
6717 /* Dispose each item in join_ga. */
6718 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006720 p = (join_T *)join_ga.ga_data;
6721 for (i = 0; i < join_ga.ga_len; ++i)
6722 {
6723 vim_free(p->tofree);
6724 ++p;
6725 }
6726 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006727 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006728
6729 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006730}
6731
6732/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006733 * Return the next (unique) copy ID.
6734 * Used for serializing nested structures.
6735 */
6736 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006737get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006738{
6739 current_copyID += COPYID_INC;
6740 return current_copyID;
6741}
6742
6743/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006744 * Garbage collection for lists and dictionaries.
6745 *
6746 * We use reference counts to be able to free most items right away when they
6747 * are no longer used. But for composite items it's possible that it becomes
6748 * unused while the reference count is > 0: When there is a recursive
6749 * reference. Example:
6750 * :let l = [1, 2, 3]
6751 * :let d = {9: l}
6752 * :let l[1] = d
6753 *
6754 * Since this is quite unusual we handle this with garbage collection: every
6755 * once in a while find out which lists and dicts are not referenced from any
6756 * variable.
6757 *
6758 * Here is a good reference text about garbage collection (refers to Python
6759 * but it applies to all reference-counting mechanisms):
6760 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006761 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006762
6763/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764 * Do garbage collection for lists and dicts.
6765 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006766 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006768garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006769{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006770 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006771 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006772 buf_T *buf;
6773 win_T *wp;
6774 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006775 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006776 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006777 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006778#ifdef FEAT_WINDOWS
6779 tabpage_T *tp;
6780#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006781
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006782 /* Only do this once. */
6783 want_garbage_collect = FALSE;
6784 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006785 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006786
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006787 /* We advance by two because we add one for items referenced through
6788 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006789 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006790
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006791 /*
6792 * 1. Go through all accessible variables and mark all lists and dicts
6793 * with copyID.
6794 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006795
6796 /* Don't free variables in the previous_funccal list unless they are only
6797 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006798 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006799 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6800 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006801 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6802 NULL);
6803 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6804 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006805 }
6806
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006807 /* script-local variables */
6808 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006809 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006810
6811 /* buffer-local variables */
6812 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006813 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6814 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815
6816 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006817 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006818 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6819 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006820#ifdef FEAT_AUTOCMD
6821 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006822 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6823 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006824#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006826#ifdef FEAT_WINDOWS
6827 /* tabpage-local variables */
6828 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006829 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6830 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006831#endif
6832
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006834 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835
6836 /* function-local variables */
6837 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6838 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006839 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6840 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006841 }
6842
Bram Moolenaard812df62008-11-09 12:46:09 +00006843 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006844 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006845
Bram Moolenaar1dced572012-04-05 16:54:08 +02006846#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006847 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006848#endif
6849
Bram Moolenaardb913952012-06-29 12:54:53 +02006850#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006851 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006852#endif
6853
6854#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006855 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006856#endif
6857
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006858#ifdef FEAT_CHANNEL
6859 abort = abort || set_ref_in_channel(copyID);
6860#endif
6861
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006862 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006864 /*
6865 * 2. Free lists and dictionaries that are not referenced.
6866 */
6867 did_free = free_unref_items(copyID);
6868
6869 /*
6870 * 3. Check if any funccal can be freed now.
6871 */
6872 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006873 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006874 if (can_free_funccal(*pfc, copyID))
6875 {
6876 fc = *pfc;
6877 *pfc = fc->caller;
6878 free_funccal(fc, TRUE);
6879 did_free = TRUE;
6880 did_free_funccal = TRUE;
6881 }
6882 else
6883 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006884 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 if (did_free_funccal)
6886 /* When a funccal was freed some more items might be garbage
6887 * collected, so run again. */
6888 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 else if (p_verbose > 0)
6891 {
6892 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6893 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006894
6895 return did_free;
6896}
6897
6898/*
6899 * Free lists and dictionaries that are no longer referenced.
6900 */
6901 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006902free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006903{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006904 dict_T *dd, *dd_next;
6905 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006906 int did_free = FALSE;
6907
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006909 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910 */
6911 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006912 {
6913 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006914 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006915 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006916 /* Free the Dictionary and ordinary items it contains, but don't
6917 * recurse into Lists and Dictionaries, they will be in the list
6918 * of dicts or list of lists. */
6919 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006922 dd = dd_next;
6923 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924
6925 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006926 * Go through the list of lists and free items without the copyID.
6927 * But don't free a list that has a watcher (used in a for loop), these
6928 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 */
6930 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006931 {
6932 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006933 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6934 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006936 /* Free the List and ordinary items it contains, but don't recurse
6937 * into Lists and Dictionaries, they will be in the list of dicts
6938 * or list of lists. */
6939 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006942 ll = ll_next;
6943 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944 return did_free;
6945}
6946
6947/*
6948 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 * "list_stack" is used to add lists to be marked. Can be NULL.
6950 *
6951 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006952 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006954set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006955{
6956 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006957 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006959 hashtab_T *cur_ht;
6960 ht_stack_T *ht_stack = NULL;
6961 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006963 cur_ht = ht;
6964 for (;;)
6965 {
6966 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006968 /* Mark each item in the hashtab. If the item contains a hashtab
6969 * it is added to ht_stack, if it contains a list it is added to
6970 * list_stack. */
6971 todo = (int)cur_ht->ht_used;
6972 for (hi = cur_ht->ht_array; todo > 0; ++hi)
6973 if (!HASHITEM_EMPTY(hi))
6974 {
6975 --todo;
6976 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
6977 &ht_stack, list_stack);
6978 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006980
6981 if (ht_stack == NULL)
6982 break;
6983
6984 /* take an item from the stack */
6985 cur_ht = ht_stack->ht;
6986 tempitem = ht_stack;
6987 ht_stack = ht_stack->prev;
6988 free(tempitem);
6989 }
6990
6991 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992}
6993
6994/*
6995 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006996 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
6997 *
6998 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007001set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007003 listitem_T *li;
7004 int abort = FALSE;
7005 list_T *cur_l;
7006 list_stack_T *list_stack = NULL;
7007 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007009 cur_l = l;
7010 for (;;)
7011 {
7012 if (!abort)
7013 /* Mark each item in the list. If the item contains a hashtab
7014 * it is added to ht_stack, if it contains a list it is added to
7015 * list_stack. */
7016 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7017 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7018 ht_stack, &list_stack);
7019 if (list_stack == NULL)
7020 break;
7021
7022 /* take an item from the stack */
7023 cur_l = list_stack->list;
7024 tempitem = list_stack;
7025 list_stack = list_stack->prev;
7026 free(tempitem);
7027 }
7028
7029 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030}
7031
7032/*
7033 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 * "list_stack" is used to add lists to be marked. Can be NULL.
7035 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7036 *
7037 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007040set_ref_in_item(
7041 typval_T *tv,
7042 int copyID,
7043 ht_stack_T **ht_stack,
7044 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045{
7046 dict_T *dd;
7047 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007049
7050 switch (tv->v_type)
7051 {
7052 case VAR_DICT:
7053 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007054 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007055 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056 /* Didn't see this dict yet. */
7057 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 if (ht_stack == NULL)
7059 {
7060 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7061 }
7062 else
7063 {
7064 ht_stack_T *newitem = (ht_stack_T*)malloc(
7065 sizeof(ht_stack_T));
7066 if (newitem == NULL)
7067 abort = TRUE;
7068 else
7069 {
7070 newitem->ht = &dd->dv_hashtab;
7071 newitem->prev = *ht_stack;
7072 *ht_stack = newitem;
7073 }
7074 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007075 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007077
7078 case VAR_LIST:
7079 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007080 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007081 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007082 /* Didn't see this list yet. */
7083 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 if (list_stack == NULL)
7085 {
7086 abort = set_ref_in_list(ll, copyID, ht_stack);
7087 }
7088 else
7089 {
7090 list_stack_T *newitem = (list_stack_T*)malloc(
7091 sizeof(list_stack_T));
7092 if (newitem == NULL)
7093 abort = TRUE;
7094 else
7095 {
7096 newitem->list = ll;
7097 newitem->prev = *list_stack;
7098 *list_stack = newitem;
7099 }
7100 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007101 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007103 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007105}
7106
7107/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108 * Allocate an empty header for a dictionary.
7109 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007110 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007111dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007112{
Bram Moolenaar33570922005-01-25 22:26:29 +00007113 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007114
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007117 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007118 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007119 if (first_dict != NULL)
7120 first_dict->dv_used_prev = d;
7121 d->dv_used_next = first_dict;
7122 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007123 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007124
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007126 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007127 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007128 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007129 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007130 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132}
7133
7134/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007135 * Allocate an empty dict for a return value.
7136 * Returns OK or FAIL.
7137 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007138 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007139rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007140{
7141 dict_T *d = dict_alloc();
7142
7143 if (d == NULL)
7144 return FAIL;
7145
7146 rettv->vval.v_dict = d;
7147 rettv->v_type = VAR_DICT;
7148 ++d->dv_refcount;
7149 return OK;
7150}
7151
7152
7153/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154 * Unreference a Dictionary: decrement the reference count and free it when it
7155 * becomes zero.
7156 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007157 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007158dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007159{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007160 if (d != NULL && --d->dv_refcount <= 0)
7161 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007162}
7163
7164/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007165 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007166 * Ignores the reference count.
7167 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007169dict_free(
7170 dict_T *d,
7171 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007173 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007175 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007177 /* Remove the dict from the list of dicts for garbage collection. */
7178 if (d->dv_used_prev == NULL)
7179 first_dict = d->dv_used_next;
7180 else
7181 d->dv_used_prev->dv_used_next = d->dv_used_next;
7182 if (d->dv_used_next != NULL)
7183 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7184
7185 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007186 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007187 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007189 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190 if (!HASHITEM_EMPTY(hi))
7191 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007192 /* Remove the item before deleting it, just in case there is
7193 * something recursive causing trouble. */
7194 di = HI2DI(hi);
7195 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007196 if (recurse || (di->di_tv.v_type != VAR_LIST
7197 && di->di_tv.v_type != VAR_DICT))
7198 clear_tv(&di->di_tv);
7199 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 --todo;
7201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007204 vim_free(d);
7205}
7206
7207/*
7208 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007209 * The "key" is copied to the new item.
7210 * Note that the value of the item "di_tv" still needs to be initialized!
7211 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007213 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007214dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215{
Bram Moolenaar33570922005-01-25 22:26:29 +00007216 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007217
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007218 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007219 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007220 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007221 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007222 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007223 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007224 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225}
7226
7227/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007228 * Make a copy of a Dictionary item.
7229 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007230 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007231dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007232{
Bram Moolenaar33570922005-01-25 22:26:29 +00007233 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007234
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007235 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7236 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007237 if (di != NULL)
7238 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007239 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007240 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007241 copy_tv(&org->di_tv, &di->di_tv);
7242 }
7243 return di;
7244}
7245
7246/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247 * Remove item "item" from Dictionary "dict" and free it.
7248 */
7249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007250dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007251{
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007253
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255 if (HASHITEM_EMPTY(hi))
7256 EMSG2(_(e_intern2), "dictitem_remove()");
7257 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007259 dictitem_free(item);
7260}
7261
7262/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 * Free a dict item. Also clears the value.
7264 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007265 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007266dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007269 if (item->di_flags & DI_FLAGS_ALLOC)
7270 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271}
7272
7273/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007274 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7275 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007276 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007277 * Returns NULL when out of memory.
7278 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007280dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281{
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 dict_T *copy;
7283 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007286
7287 if (orig == NULL)
7288 return NULL;
7289
7290 copy = dict_alloc();
7291 if (copy != NULL)
7292 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007293 if (copyID != 0)
7294 {
7295 orig->dv_copyID = copyID;
7296 orig->dv_copydict = copy;
7297 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007298 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007299 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 --todo;
7304
7305 di = dictitem_alloc(hi->hi_key);
7306 if (di == NULL)
7307 break;
7308 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007309 {
7310 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7311 copyID) == FAIL)
7312 {
7313 vim_free(di);
7314 break;
7315 }
7316 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 else
7318 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7319 if (dict_add(copy, di) == FAIL)
7320 {
7321 dictitem_free(di);
7322 break;
7323 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007326
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007328 if (todo > 0)
7329 {
7330 dict_unref(copy);
7331 copy = NULL;
7332 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333 }
7334
7335 return copy;
7336}
7337
7338/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007340 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007341 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007342 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007343dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344{
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346}
7347
Bram Moolenaar8c711452005-01-14 21:53:12 +00007348/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007349 * Add a number or string entry to dictionary "d".
7350 * When "str" is NULL use number "nr", otherwise use "str".
7351 * Returns FAIL when out of memory and when key already exists.
7352 */
7353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007354dict_add_nr_str(
7355 dict_T *d,
7356 char *key,
7357 long nr,
7358 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007359{
7360 dictitem_T *item;
7361
7362 item = dictitem_alloc((char_u *)key);
7363 if (item == NULL)
7364 return FAIL;
7365 item->di_tv.v_lock = 0;
7366 if (str == NULL)
7367 {
7368 item->di_tv.v_type = VAR_NUMBER;
7369 item->di_tv.vval.v_number = nr;
7370 }
7371 else
7372 {
7373 item->di_tv.v_type = VAR_STRING;
7374 item->di_tv.vval.v_string = vim_strsave(str);
7375 }
7376 if (dict_add(d, item) == FAIL)
7377 {
7378 dictitem_free(item);
7379 return FAIL;
7380 }
7381 return OK;
7382}
7383
7384/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007385 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007386 * Returns FAIL when out of memory and when key already exists.
7387 */
7388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007389dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007390{
7391 dictitem_T *item;
7392
7393 item = dictitem_alloc((char_u *)key);
7394 if (item == NULL)
7395 return FAIL;
7396 item->di_tv.v_lock = 0;
7397 item->di_tv.v_type = VAR_LIST;
7398 item->di_tv.vval.v_list = list;
7399 if (dict_add(d, item) == FAIL)
7400 {
7401 dictitem_free(item);
7402 return FAIL;
7403 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007404 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007405 return OK;
7406}
7407
7408/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409 * Get the number of items in a Dictionary.
7410 */
7411 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007412dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414 if (d == NULL)
7415 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007416 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007417}
7418
7419/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 * Find item "key[len]" in Dictionary "d".
7421 * If "len" is negative use strlen(key).
7422 * Returns NULL when not found.
7423 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007424 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007425dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007427#define AKEYLEN 200
7428 char_u buf[AKEYLEN];
7429 char_u *akey;
7430 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007433 if (len < 0)
7434 akey = key;
7435 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007436 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007437 tofree = akey = vim_strnsave(key, len);
7438 if (akey == NULL)
7439 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007440 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007441 else
7442 {
7443 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007444 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007445 akey = buf;
7446 }
7447
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007449 vim_free(tofree);
7450 if (HASHITEM_EMPTY(hi))
7451 return NULL;
7452 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007453}
7454
7455/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007456 * Get a string item from a dictionary.
7457 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007458 * Returns NULL if the entry doesn't exist or out of memory.
7459 */
7460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007461get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007462{
7463 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007464 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007465
7466 di = dict_find(d, key, -1);
7467 if (di == NULL)
7468 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007469 s = get_tv_string(&di->di_tv);
7470 if (save && s != NULL)
7471 s = vim_strsave(s);
7472 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007473}
7474
7475/*
7476 * Get a number item from a dictionary.
7477 * Returns 0 if the entry doesn't exist or out of memory.
7478 */
7479 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007480get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007481{
7482 dictitem_T *di;
7483
7484 di = dict_find(d, key, -1);
7485 if (di == NULL)
7486 return 0;
7487 return get_tv_number(&di->di_tv);
7488}
7489
7490/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 * Return an allocated string with the string representation of a Dictionary.
7492 * May return NULL.
7493 */
7494 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007495dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496{
7497 garray_T ga;
7498 int first = TRUE;
7499 char_u *tofree;
7500 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007503 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007504 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 return NULL;
7508 ga_init2(&ga, (int)sizeof(char), 80);
7509 ga_append(&ga, '{');
7510
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007511 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007512 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007516 --todo;
7517
7518 if (first)
7519 first = FALSE;
7520 else
7521 ga_concat(&ga, (char_u *)", ");
7522
7523 tofree = string_quote(hi->hi_key, FALSE);
7524 if (tofree != NULL)
7525 {
7526 ga_concat(&ga, tofree);
7527 vim_free(tofree);
7528 }
7529 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007530 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 if (s != NULL)
7532 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007534 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007535 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007536 line_breakcheck();
7537
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007540 if (todo > 0)
7541 {
7542 vim_free(ga.ga_data);
7543 return NULL;
7544 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545
7546 ga_append(&ga, '}');
7547 ga_append(&ga, NUL);
7548 return (char_u *)ga.ga_data;
7549}
7550
7551/*
7552 * Allocate a variable for a Dictionary and fill it from "*arg".
7553 * Return OK or FAIL. Returns NOTDONE for {expr}.
7554 */
7555 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557{
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 dict_T *d = NULL;
7559 typval_T tvkey;
7560 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007561 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007562 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007564 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565
7566 /*
7567 * First check if it's not a curly-braces thing: {expr}.
7568 * Must do this without evaluating, otherwise a function may be called
7569 * twice. Unfortunately this means we need to call eval1() twice for the
7570 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007571 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007573 if (*start != '}')
7574 {
7575 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7576 return FAIL;
7577 if (*start == '}')
7578 return NOTDONE;
7579 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580
7581 if (evaluate)
7582 {
7583 d = dict_alloc();
7584 if (d == NULL)
7585 return FAIL;
7586 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007587 tvkey.v_type = VAR_UNKNOWN;
7588 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589
7590 *arg = skipwhite(*arg + 1);
7591 while (**arg != '}' && **arg != NUL)
7592 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007593 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594 goto failret;
7595 if (**arg != ':')
7596 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007597 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007598 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599 goto failret;
7600 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007601 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007603 key = get_tv_string_buf_chk(&tvkey, buf);
7604 if (key == NULL || *key == NUL)
7605 {
7606 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7607 if (key != NULL)
7608 EMSG(_(e_emptykey));
7609 clear_tv(&tvkey);
7610 goto failret;
7611 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613
7614 *arg = skipwhite(*arg + 1);
7615 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7616 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007617 if (evaluate)
7618 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 goto failret;
7620 }
7621 if (evaluate)
7622 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007623 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 if (item != NULL)
7625 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007626 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007627 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 clear_tv(&tv);
7629 goto failret;
7630 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007631 item = dictitem_alloc(key);
7632 clear_tv(&tvkey);
7633 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007636 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007637 if (dict_add(d, item) == FAIL)
7638 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 }
7640 }
7641
7642 if (**arg == '}')
7643 break;
7644 if (**arg != ',')
7645 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007646 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647 goto failret;
7648 }
7649 *arg = skipwhite(*arg + 1);
7650 }
7651
7652 if (**arg != '}')
7653 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007654 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655failret:
7656 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007657 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 return FAIL;
7659 }
7660
7661 *arg = skipwhite(*arg + 1);
7662 if (evaluate)
7663 {
7664 rettv->v_type = VAR_DICT;
7665 rettv->vval.v_dict = d;
7666 ++d->dv_refcount;
7667 }
7668
7669 return OK;
7670}
7671
Bram Moolenaar17a13432016-01-24 14:22:10 +01007672 static char *
7673get_var_special_name(int nr)
7674{
7675 switch (nr)
7676 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007677 case VVAL_FALSE: return "v:false";
7678 case VVAL_TRUE: return "v:true";
7679 case VVAL_NONE: return "v:none";
7680 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007681 }
7682 EMSG2(_(e_intern2), "get_var_special_name()");
7683 return "42";
7684}
7685
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007687 * Return a string with the string representation of a variable.
7688 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007689 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007691 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007692 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007693 */
7694 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007695echo_string(
7696 typval_T *tv,
7697 char_u **tofree,
7698 char_u *numbuf,
7699 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007700{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007701 static int recurse = 0;
7702 char_u *r = NULL;
7703
Bram Moolenaar33570922005-01-25 22:26:29 +00007704 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007705 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007706 if (!did_echo_string_emsg)
7707 {
7708 /* Only give this message once for a recursive call to avoid
7709 * flooding the user with errors. And stop iterating over lists
7710 * and dicts. */
7711 did_echo_string_emsg = TRUE;
7712 EMSG(_("E724: variable nested too deep for displaying"));
7713 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007714 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007715 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007716 }
7717 ++recurse;
7718
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007719 switch (tv->v_type)
7720 {
7721 case VAR_FUNC:
7722 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007723 r = tv->vval.v_string;
7724 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007725
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007726 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007727 if (tv->vval.v_list == NULL)
7728 {
7729 *tofree = NULL;
7730 r = NULL;
7731 }
7732 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7733 {
7734 *tofree = NULL;
7735 r = (char_u *)"[...]";
7736 }
7737 else
7738 {
7739 tv->vval.v_list->lv_copyID = copyID;
7740 *tofree = list2string(tv, copyID);
7741 r = *tofree;
7742 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007743 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007744
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007746 if (tv->vval.v_dict == NULL)
7747 {
7748 *tofree = NULL;
7749 r = NULL;
7750 }
7751 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7752 {
7753 *tofree = NULL;
7754 r = (char_u *)"{...}";
7755 }
7756 else
7757 {
7758 tv->vval.v_dict->dv_copyID = copyID;
7759 *tofree = dict2string(tv, copyID);
7760 r = *tofree;
7761 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007762 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007763
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007764 case VAR_STRING:
7765 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007766 *tofree = NULL;
7767 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007768 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007769
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007770#ifdef FEAT_FLOAT
7771 case VAR_FLOAT:
7772 *tofree = NULL;
7773 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7774 r = numbuf;
7775 break;
7776#endif
7777
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007778 case VAR_SPECIAL:
7779 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007780 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007781 break;
7782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007783 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007784 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007785 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007786 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007787
Bram Moolenaar8502c702014-06-17 12:51:16 +02007788 if (--recurse == 0)
7789 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007790 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007791}
7792
7793/*
7794 * Return a string with the string representation of a variable.
7795 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7796 * "numbuf" is used for a number.
7797 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007798 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007799 */
7800 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007801tv2string(
7802 typval_T *tv,
7803 char_u **tofree,
7804 char_u *numbuf,
7805 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007806{
7807 switch (tv->v_type)
7808 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007809 case VAR_FUNC:
7810 *tofree = string_quote(tv->vval.v_string, TRUE);
7811 return *tofree;
7812 case VAR_STRING:
7813 *tofree = string_quote(tv->vval.v_string, FALSE);
7814 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007815#ifdef FEAT_FLOAT
7816 case VAR_FLOAT:
7817 *tofree = NULL;
7818 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7819 return numbuf;
7820#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007821 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007822 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007823 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007824 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007826 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007827 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007828 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007829 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007830}
7831
7832/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007833 * Return string "str" in ' quotes, doubling ' characters.
7834 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007835 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007836 */
7837 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007838string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007839{
Bram Moolenaar33570922005-01-25 22:26:29 +00007840 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007841 char_u *p, *r, *s;
7842
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 len = (function ? 13 : 3);
7844 if (str != NULL)
7845 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007846 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 for (p = str; *p != NUL; mb_ptr_adv(p))
7848 if (*p == '\'')
7849 ++len;
7850 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007851 s = r = alloc(len);
7852 if (r != NULL)
7853 {
7854 if (function)
7855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007856 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007857 r += 10;
7858 }
7859 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007860 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007861 if (str != NULL)
7862 for (p = str; *p != NUL; )
7863 {
7864 if (*p == '\'')
7865 *r++ = '\'';
7866 MB_COPY_CHAR(p, r);
7867 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007868 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007869 if (function)
7870 *r++ = ')';
7871 *r++ = NUL;
7872 }
7873 return s;
7874}
7875
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007876#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007877/*
7878 * Convert the string "text" to a floating point number.
7879 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7880 * this always uses a decimal point.
7881 * Returns the length of the text that was consumed.
7882 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007883 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007884string2float(
7885 char_u *text,
7886 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007887{
7888 char *s = (char *)text;
7889 float_T f;
7890
7891 f = strtod(s, &s);
7892 *value = f;
7893 return (int)((char_u *)s - text);
7894}
7895#endif
7896
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 * Get the value of an environment variable.
7899 * "arg" is pointing to the '$'. It is advanced to after the name.
7900 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007901 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 */
7903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007904get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 char_u *string = NULL;
7907 int len;
7908 int cc;
7909 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007910 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911
7912 ++*arg;
7913 name = *arg;
7914 len = get_env_len(arg);
7915 if (evaluate)
7916 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007917 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007918 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007919
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007920 cc = name[len];
7921 name[len] = NUL;
7922 /* first try vim_getenv(), fast for normal environment vars */
7923 string = vim_getenv(name, &mustfree);
7924 if (string != NULL && *string != NUL)
7925 {
7926 if (!mustfree)
7927 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007929 else
7930 {
7931 if (mustfree)
7932 vim_free(string);
7933
7934 /* next try expanding things like $VIM and ${HOME} */
7935 string = expand_env_save(name - 1);
7936 if (string != NULL && *string == '$')
7937 {
7938 vim_free(string);
7939 string = NULL;
7940 }
7941 }
7942 name[len] = cc;
7943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007944 rettv->v_type = VAR_STRING;
7945 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 }
7947
7948 return OK;
7949}
7950
7951/*
7952 * Array with names and number of arguments of all internal functions
7953 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7954 */
7955static struct fst
7956{
7957 char *f_name; /* function name */
7958 char f_min_argc; /* minimal number of arguments */
7959 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01007960 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007961 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962} functions[] =
7963{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007964#ifdef FEAT_FLOAT
7965 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007966 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007967#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007968 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01007969 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007970 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"append", 2, 2, f_append},
7972 {"argc", 0, 0, f_argc},
7973 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007974 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007975 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01007976#ifdef FEAT_FLOAT
7977 {"asin", 1, 1, f_asin}, /* WJMc */
7978#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007979 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01007980 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01007981 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01007982 {"assert_false", 1, 2, f_assert_false},
7983 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007984#ifdef FEAT_FLOAT
7985 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007986 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007989 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"bufexists", 1, 1, f_bufexists},
7991 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7992 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7993 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7994 {"buflisted", 1, 1, f_buflisted},
7995 {"bufloaded", 1, 1, f_bufloaded},
7996 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007997 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 {"bufwinnr", 1, 1, f_bufwinnr},
7999 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008000 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008001 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008002 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008003#ifdef FEAT_FLOAT
8004 {"ceil", 1, 1, f_ceil},
8005#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008006#ifdef FEAT_CHANNEL
8007 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008008 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008009 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8010 {"ch_sendraw", 2, 3, f_ch_sendraw},
8011#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008012 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008013 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008015 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008017#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008018 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008019 {"complete_add", 1, 1, f_complete_add},
8020 {"complete_check", 0, 0, f_complete_check},
8021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008023 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008024#ifdef FEAT_FLOAT
8025 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008026 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008027#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008028 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008030 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008031 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008032 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008034 {"diff_filler", 1, 1, f_diff_filler},
8035 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008036 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008038 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {"eventhandler", 0, 0, f_eventhandler},
8040 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008041 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008043#ifdef FEAT_FLOAT
8044 {"exp", 1, 1, f_exp},
8045#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008046 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008047 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008048 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8050 {"filereadable", 1, 1, f_filereadable},
8051 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008052 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008053 {"finddir", 1, 3, f_finddir},
8054 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008055#ifdef FEAT_FLOAT
8056 {"float2nr", 1, 1, f_float2nr},
8057 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008058 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008059#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008060 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {"fnamemodify", 2, 2, f_fnamemodify},
8062 {"foldclosed", 1, 1, f_foldclosed},
8063 {"foldclosedend", 1, 1, f_foldclosedend},
8064 {"foldlevel", 1, 1, f_foldlevel},
8065 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008066 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008068 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008069 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008070 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008071 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008072 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"getchar", 0, 1, f_getchar},
8074 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008075 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"getcmdline", 0, 0, f_getcmdline},
8077 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008078 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008079 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008080 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008081 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008082 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008083 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 {"getfsize", 1, 1, f_getfsize},
8085 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008086 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008087 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008088 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008089 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008090 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008091 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008092 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008093 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008095 {"gettabvar", 2, 3, f_gettabvar},
8096 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"getwinposx", 0, 0, f_getwinposx},
8098 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008099 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008100 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008101 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008102 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008104 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008105 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008106 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8108 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8109 {"histadd", 2, 2, f_histadd},
8110 {"histdel", 1, 2, f_histdel},
8111 {"histget", 1, 2, f_histget},
8112 {"histnr", 1, 1, f_histnr},
8113 {"hlID", 1, 1, f_hlID},
8114 {"hlexists", 1, 1, f_hlexists},
8115 {"hostname", 0, 0, f_hostname},
8116 {"iconv", 3, 3, f_iconv},
8117 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008118 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008119 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008121 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"inputrestore", 0, 0, f_inputrestore},
8123 {"inputsave", 0, 0, f_inputsave},
8124 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008125 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008126 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008128 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008129 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008130 {"join", 1, 2, f_join},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008131 {"jsondecode", 1, 1, f_jsondecode},
8132 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008133 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008135 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"libcall", 3, 3, f_libcall},
8137 {"libcallnr", 3, 3, f_libcallnr},
8138 {"line", 1, 1, f_line},
8139 {"line2byte", 1, 1, f_line2byte},
8140 {"lispindent", 1, 1, f_lispindent},
8141 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008142#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008143 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008144 {"log10", 1, 1, f_log10},
8145#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008146#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008147 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008148#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008149 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008150 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008151 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008152 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008153 {"matchadd", 2, 5, f_matchadd},
8154 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008155 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008156 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008157 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008158 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008159 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008160 {"max", 1, 1, f_max},
8161 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008162#ifdef vim_mkdir
8163 {"mkdir", 1, 3, f_mkdir},
8164#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008166#ifdef FEAT_MZSCHEME
8167 {"mzeval", 1, 1, f_mzeval},
8168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008170 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008171 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008172 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008173#ifdef FEAT_PERL
8174 {"perleval", 1, 1, f_perleval},
8175#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008176#ifdef FEAT_FLOAT
8177 {"pow", 2, 2, f_pow},
8178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008180 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008181 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008182#ifdef FEAT_PYTHON3
8183 {"py3eval", 1, 1, f_py3eval},
8184#endif
8185#ifdef FEAT_PYTHON
8186 {"pyeval", 1, 1, f_pyeval},
8187#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008188 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008189 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008190 {"reltime", 0, 2, f_reltime},
8191 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"remote_expr", 2, 3, f_remote_expr},
8193 {"remote_foreground", 1, 1, f_remote_foreground},
8194 {"remote_peek", 1, 2, f_remote_peek},
8195 {"remote_read", 1, 1, f_remote_read},
8196 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008197 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008199 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008201 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008202#ifdef FEAT_FLOAT
8203 {"round", 1, 1, f_round},
8204#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008205 {"screenattr", 2, 2, f_screenattr},
8206 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008207 {"screencol", 0, 0, f_screencol},
8208 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008209 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008210 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008211 {"searchpair", 3, 7, f_searchpair},
8212 {"searchpairpos", 3, 7, f_searchpairpos},
8213 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {"server2client", 2, 2, f_server2client},
8215 {"serverlist", 0, 0, f_serverlist},
8216 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008217 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {"setcmdpos", 1, 1, f_setcmdpos},
8219 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008220 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008221 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008222 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008223 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008225 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008226 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008228#ifdef FEAT_CRYPT
8229 {"sha256", 1, 1, f_sha256},
8230#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008231 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008232 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008234#ifdef FEAT_FLOAT
8235 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008236 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008238 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008239 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008240 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008241 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008242 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008243#ifdef FEAT_FLOAT
8244 {"sqrt", 1, 1, f_sqrt},
8245 {"str2float", 1, 1, f_str2float},
8246#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008247 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008248 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008249 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250#ifdef HAVE_STRFTIME
8251 {"strftime", 1, 2, f_strftime},
8252#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008253 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008254 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {"strlen", 1, 1, f_strlen},
8256 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008257 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008259 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008260 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {"substitute", 4, 4, f_substitute},
8262 {"synID", 3, 3, f_synID},
8263 {"synIDattr", 2, 3, f_synIDattr},
8264 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008265 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008266 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008267 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008268 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008269 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008270 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008271 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008272 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008273 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008274#ifdef FEAT_FLOAT
8275 {"tan", 1, 1, f_tan},
8276 {"tanh", 1, 1, f_tanh},
8277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008279 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {"tolower", 1, 1, f_tolower},
8281 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008282 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008283#ifdef FEAT_FLOAT
8284 {"trunc", 1, 1, f_trunc},
8285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008287 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008288 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008289 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008290 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"virtcol", 1, 1, f_virtcol},
8292 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008293 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"winbufnr", 1, 1, f_winbufnr},
8295 {"wincol", 0, 0, f_wincol},
8296 {"winheight", 1, 1, f_winheight},
8297 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008298 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008300 {"winrestview", 1, 1, f_winrestview},
8301 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008303 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008304 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008305 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306};
8307
8308#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8309
8310/*
8311 * Function given to ExpandGeneric() to obtain the list of internal
8312 * or user defined function names.
8313 */
8314 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008315get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316{
8317 static int intidx = -1;
8318 char_u *name;
8319
8320 if (idx == 0)
8321 intidx = -1;
8322 if (intidx < 0)
8323 {
8324 name = get_user_func_name(xp, idx);
8325 if (name != NULL)
8326 return name;
8327 }
8328 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8329 {
8330 STRCPY(IObuff, functions[intidx].f_name);
8331 STRCAT(IObuff, "(");
8332 if (functions[intidx].f_max_argc == 0)
8333 STRCAT(IObuff, ")");
8334 return IObuff;
8335 }
8336
8337 return NULL;
8338}
8339
8340/*
8341 * Function given to ExpandGeneric() to obtain the list of internal or
8342 * user defined variable or function names.
8343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008345get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346{
8347 static int intidx = -1;
8348 char_u *name;
8349
8350 if (idx == 0)
8351 intidx = -1;
8352 if (intidx < 0)
8353 {
8354 name = get_function_name(xp, idx);
8355 if (name != NULL)
8356 return name;
8357 }
8358 return get_user_var_name(xp, ++intidx);
8359}
8360
8361#endif /* FEAT_CMDL_COMPL */
8362
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008363#if defined(EBCDIC) || defined(PROTO)
8364/*
8365 * Compare struct fst by function name.
8366 */
8367 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008368compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008369{
8370 struct fst *p1 = (struct fst *)s1;
8371 struct fst *p2 = (struct fst *)s2;
8372
8373 return STRCMP(p1->f_name, p2->f_name);
8374}
8375
8376/*
8377 * Sort the function table by function name.
8378 * The sorting of the table above is ASCII dependant.
8379 * On machines using EBCDIC we have to sort it.
8380 */
8381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008382sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008383{
8384 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8385
8386 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8387}
8388#endif
8389
8390
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391/*
8392 * Find internal function in table above.
8393 * Return index, or -1 if not found
8394 */
8395 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008396find_internal_func(
8397 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398{
8399 int first = 0;
8400 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8401 int cmp;
8402 int x;
8403
8404 /*
8405 * Find the function name in the table. Binary search.
8406 */
8407 while (first <= last)
8408 {
8409 x = first + ((unsigned)(last - first) >> 1);
8410 cmp = STRCMP(name, functions[x].f_name);
8411 if (cmp < 0)
8412 last = x - 1;
8413 else if (cmp > 0)
8414 first = x + 1;
8415 else
8416 return x;
8417 }
8418 return -1;
8419}
8420
8421/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008422 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8423 * name it contains, otherwise return "name".
8424 */
8425 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008426deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008427{
Bram Moolenaar33570922005-01-25 22:26:29 +00008428 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008429 int cc;
8430
8431 cc = name[*lenp];
8432 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008433 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008434 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008435 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008436 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008437 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008438 {
8439 *lenp = 0;
8440 return (char_u *)""; /* just in case */
8441 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008442 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008443 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008444 }
8445
8446 return name;
8447}
8448
8449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 * Allocate a variable for the result of a function.
8451 * Return OK or FAIL.
8452 */
8453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008454get_func_tv(
8455 char_u *name, /* name of the function */
8456 int len, /* length of "name" */
8457 typval_T *rettv,
8458 char_u **arg, /* argument, pointing to the '(' */
8459 linenr_T firstline, /* first line of range */
8460 linenr_T lastline, /* last line of range */
8461 int *doesrange, /* return: function handled range */
8462 int evaluate,
8463 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
8465 char_u *argp;
8466 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008467 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 int argcount = 0; /* number of arguments found */
8469
8470 /*
8471 * Get the arguments.
8472 */
8473 argp = *arg;
8474 while (argcount < MAX_FUNC_ARGS)
8475 {
8476 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8477 if (*argp == ')' || *argp == ',' || *argp == NUL)
8478 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8480 {
8481 ret = FAIL;
8482 break;
8483 }
8484 ++argcount;
8485 if (*argp != ',')
8486 break;
8487 }
8488 if (*argp == ')')
8489 ++argp;
8490 else
8491 ret = FAIL;
8492
8493 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008494 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008497 {
8498 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008499 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008500 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008501 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503
8504 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008505 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506
8507 *arg = skipwhite(argp);
8508 return ret;
8509}
8510
8511
8512/*
8513 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008514 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008515 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008518call_func(
8519 char_u *funcname, /* name of the function */
8520 int len, /* length of "name" */
8521 typval_T *rettv, /* return value goes here */
8522 int argcount, /* number of "argvars" */
8523 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008524 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008525 linenr_T firstline, /* first line of range */
8526 linenr_T lastline, /* last line of range */
8527 int *doesrange, /* return: function handled range */
8528 int evaluate,
8529 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532#define ERROR_UNKNOWN 0
8533#define ERROR_TOOMANY 1
8534#define ERROR_TOOFEW 2
8535#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008536#define ERROR_DICT 4
8537#define ERROR_NONE 5
8538#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 int error = ERROR_NONE;
8540 int i;
8541 int llen;
8542 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543#define FLEN_FIXED 40
8544 char_u fname_buf[FLEN_FIXED + 1];
8545 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008546 char_u *name;
8547
8548 /* Make a copy of the name, if it comes from a funcref variable it could
8549 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008550 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008551 if (name == NULL)
8552 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553
8554 /*
8555 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8556 * Change <SNR>123_name() to K_SNR 123_name().
8557 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 llen = eval_fname_script(name);
8560 if (llen > 0)
8561 {
8562 fname_buf[0] = K_SPECIAL;
8563 fname_buf[1] = KS_EXTRA;
8564 fname_buf[2] = (int)KE_SNR;
8565 i = 3;
8566 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8567 {
8568 if (current_SID <= 0)
8569 error = ERROR_SCRIPT;
8570 else
8571 {
8572 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8573 i = (int)STRLEN(fname_buf);
8574 }
8575 }
8576 if (i + STRLEN(name + llen) < FLEN_FIXED)
8577 {
8578 STRCPY(fname_buf + i, name + llen);
8579 fname = fname_buf;
8580 }
8581 else
8582 {
8583 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8584 if (fname == NULL)
8585 error = ERROR_OTHER;
8586 else
8587 {
8588 mch_memmove(fname, fname_buf, (size_t)i);
8589 STRCPY(fname + i, name + llen);
8590 }
8591 }
8592 }
8593 else
8594 fname = name;
8595
8596 *doesrange = FALSE;
8597
8598
8599 /* execute the function if no errors detected and executing */
8600 if (evaluate && error == ERROR_NONE)
8601 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008602 char_u *rfname = fname;
8603
8604 /* Ignore "g:" before a function name. */
8605 if (fname[0] == 'g' && fname[1] == ':')
8606 rfname = fname + 2;
8607
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008608 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8609 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 error = ERROR_UNKNOWN;
8611
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008612 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 {
8614 /*
8615 * User defined function.
8616 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008617 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008618
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008620 /* Trigger FuncUndefined event, may load the function. */
8621 if (fp == NULL
8622 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008623 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008624 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008626 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008627 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 }
8629#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008630 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008631 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008632 {
8633 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008634 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008635 }
8636
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 if (fp != NULL)
8638 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008639 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008641 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008643 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008645 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008646 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 else
8648 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008649 int did_save_redo = FALSE;
8650
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 /*
8652 * Call the user function.
8653 * Save and restore search patterns, script variables and
8654 * redo buffer.
8655 */
8656 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008657#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008658 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008659#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008660 {
8661 saveRedobuff();
8662 did_save_redo = TRUE;
8663 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008664 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008666 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008667 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8668 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8669 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008670 /* Function was unreferenced while being used, free it
8671 * now. */
8672 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008673 if (did_save_redo)
8674 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 restore_search_patterns();
8676 error = ERROR_NONE;
8677 }
8678 }
8679 }
8680 else
8681 {
8682 /*
8683 * Find the function name in the table, call its implementation.
8684 */
8685 i = find_internal_func(fname);
8686 if (i >= 0)
8687 {
8688 if (argcount < functions[i].f_min_argc)
8689 error = ERROR_TOOFEW;
8690 else if (argcount > functions[i].f_max_argc)
8691 error = ERROR_TOOMANY;
8692 else
8693 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008694 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 error = ERROR_NONE;
8697 }
8698 }
8699 }
8700 /*
8701 * The function call (or "FuncUndefined" autocommand sequence) might
8702 * have been aborted by an error, an interrupt, or an explicitly thrown
8703 * exception that has not been caught so far. This situation can be
8704 * tested for by calling aborting(). For an error in an internal
8705 * function or for the "E132" error in call_user_func(), however, the
8706 * throw point at which the "force_abort" flag (temporarily reset by
8707 * emsg()) is normally updated has not been reached yet. We need to
8708 * update that flag first to make aborting() reliable.
8709 */
8710 update_force_abort();
8711 }
8712 if (error == ERROR_NONE)
8713 ret = OK;
8714
8715 /*
8716 * Report an error unless the argument evaluation or function call has been
8717 * cancelled due to an aborting error, an interrupt, or an exception.
8718 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008719 if (!aborting())
8720 {
8721 switch (error)
8722 {
8723 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008724 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008725 break;
8726 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008727 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008728 break;
8729 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008730 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008731 name);
8732 break;
8733 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008734 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008735 name);
8736 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008737 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008738 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008739 name);
8740 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008741 }
8742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 if (fname != name && fname != fname_buf)
8745 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008746 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747
8748 return ret;
8749}
8750
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008751/*
8752 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008753 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008754 */
8755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008756emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008757{
8758 char_u *p;
8759
8760 if (*name == K_SPECIAL)
8761 p = concat_str((char_u *)"<SNR>", name + 3);
8762 else
8763 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008764 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008765 if (p != name)
8766 vim_free(p);
8767}
8768
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008769/*
8770 * Return TRUE for a non-zero Number and a non-empty String.
8771 */
8772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008773non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008774{
8775 return ((argvars[0].v_type == VAR_NUMBER
8776 && argvars[0].vval.v_number != 0)
8777 || (argvars[0].v_type == VAR_STRING
8778 && argvars[0].vval.v_string != NULL
8779 && *argvars[0].vval.v_string != NUL));
8780}
8781
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782/*********************************************
8783 * Implementation of the built-in functions
8784 */
8785
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008786#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008787static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008788
8789/*
8790 * Get the float value of "argvars[0]" into "f".
8791 * Returns FAIL when the argument is not a Number or Float.
8792 */
8793 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008794get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008795{
8796 if (argvars[0].v_type == VAR_FLOAT)
8797 {
8798 *f = argvars[0].vval.v_float;
8799 return OK;
8800 }
8801 if (argvars[0].v_type == VAR_NUMBER)
8802 {
8803 *f = (float_T)argvars[0].vval.v_number;
8804 return OK;
8805 }
8806 EMSG(_("E808: Number or Float required"));
8807 return FAIL;
8808}
8809
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008810/*
8811 * "abs(expr)" function
8812 */
8813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008814f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008815{
8816 if (argvars[0].v_type == VAR_FLOAT)
8817 {
8818 rettv->v_type = VAR_FLOAT;
8819 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8820 }
8821 else
8822 {
8823 varnumber_T n;
8824 int error = FALSE;
8825
8826 n = get_tv_number_chk(&argvars[0], &error);
8827 if (error)
8828 rettv->vval.v_number = -1;
8829 else if (n > 0)
8830 rettv->vval.v_number = n;
8831 else
8832 rettv->vval.v_number = -n;
8833 }
8834}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008835
8836/*
8837 * "acos()" function
8838 */
8839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008840f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008841{
8842 float_T f;
8843
8844 rettv->v_type = VAR_FLOAT;
8845 if (get_float_arg(argvars, &f) == OK)
8846 rettv->vval.v_float = acos(f);
8847 else
8848 rettv->vval.v_float = 0.0;
8849}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008850#endif
8851
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008853 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 */
8855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008856f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857{
Bram Moolenaar33570922005-01-25 22:26:29 +00008858 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008860 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008861 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008863 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008864 && !tv_check_lock(l->lv_lock,
8865 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008866 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008868 }
8869 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008870 EMSG(_(e_listreq));
8871}
8872
8873/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008874 * "alloc_fail(id, countdown, repeat)" function
8875 */
8876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008877f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008878{
8879 if (argvars[0].v_type != VAR_NUMBER
8880 || argvars[0].vval.v_number <= 0
8881 || argvars[1].v_type != VAR_NUMBER
8882 || argvars[1].vval.v_number < 0
8883 || argvars[2].v_type != VAR_NUMBER)
8884 EMSG(_(e_invarg));
8885 else
8886 {
8887 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008888 if (alloc_fail_id >= aid_last)
8889 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008890 alloc_fail_countdown = argvars[1].vval.v_number;
8891 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008892 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008893 }
8894}
8895
8896/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008897 * "and(expr, expr)" function
8898 */
8899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008900f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008901{
8902 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8903 & get_tv_number_chk(&argvars[1], NULL);
8904}
8905
8906/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008907 * "append(lnum, string/list)" function
8908 */
8909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008910f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008911{
8912 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008913 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 list_T *l = NULL;
8915 listitem_T *li = NULL;
8916 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008917 long added = 0;
8918
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008919 /* When coming here from Insert mode, sync undo, so that this can be
8920 * undone separately from what was previously inserted. */
8921 if (u_sync_once == 2)
8922 {
8923 u_sync_once = 1; /* notify that u_sync() was called */
8924 u_sync(TRUE);
8925 }
8926
Bram Moolenaar0d660222005-01-07 21:51:51 +00008927 lnum = get_tv_lnum(argvars);
8928 if (lnum >= 0
8929 && lnum <= curbuf->b_ml.ml_line_count
8930 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008931 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008932 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008933 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008934 l = argvars[1].vval.v_list;
8935 if (l == NULL)
8936 return;
8937 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008938 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008939 for (;;)
8940 {
8941 if (l == NULL)
8942 tv = &argvars[1]; /* append a string */
8943 else if (li == NULL)
8944 break; /* end of list */
8945 else
8946 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008947 line = get_tv_string_chk(tv);
8948 if (line == NULL) /* type error */
8949 {
8950 rettv->vval.v_number = 1; /* Failed */
8951 break;
8952 }
8953 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008954 ++added;
8955 if (l == NULL)
8956 break;
8957 li = li->li_next;
8958 }
8959
8960 appended_lines_mark(lnum, added);
8961 if (curwin->w_cursor.lnum > lnum)
8962 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008964 else
8965 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966}
8967
8968/*
8969 * "argc()" function
8970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008972f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975}
8976
8977/*
8978 * "argidx()" function
8979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008981f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984}
8985
8986/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008987 * "arglistid()" function
8988 */
8989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008990f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008991{
8992 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008993
8994 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01008995 wp = find_tabwin(&argvars[0], &argvars[1]);
8996 if (wp != NULL)
8997 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008998}
8999
9000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 * "argv(nr)" function
9002 */
9003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009004f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005{
9006 int idx;
9007
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009008 if (argvars[0].v_type != VAR_UNKNOWN)
9009 {
9010 idx = get_tv_number_chk(&argvars[0], NULL);
9011 if (idx >= 0 && idx < ARGCOUNT)
9012 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9013 else
9014 rettv->vval.v_string = NULL;
9015 rettv->v_type = VAR_STRING;
9016 }
9017 else if (rettv_list_alloc(rettv) == OK)
9018 for (idx = 0; idx < ARGCOUNT; ++idx)
9019 list_append_string(rettv->vval.v_list,
9020 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021}
9022
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009023static void prepare_assert_error(garray_T*gap);
9024static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9025static void assert_error(garray_T *gap);
9026static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009027
9028/*
9029 * Prepare "gap" for an assert error and add the sourcing position.
9030 */
9031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009032prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009033{
9034 char buf[NUMBUFLEN];
9035
9036 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009037 if (sourcing_name != NULL)
9038 {
9039 ga_concat(gap, sourcing_name);
9040 if (sourcing_lnum > 0)
9041 ga_concat(gap, (char_u *)" ");
9042 }
9043 if (sourcing_lnum > 0)
9044 {
9045 sprintf(buf, "line %ld", (long)sourcing_lnum);
9046 ga_concat(gap, (char_u *)buf);
9047 }
9048 if (sourcing_name != NULL || sourcing_lnum > 0)
9049 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009050}
9051
9052/*
9053 * Fill "gap" with information about an assert error.
9054 */
9055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009056fill_assert_error(
9057 garray_T *gap,
9058 typval_T *opt_msg_tv,
9059 char_u *exp_str,
9060 typval_T *exp_tv,
9061 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009062{
9063 char_u numbuf[NUMBUFLEN];
9064 char_u *tofree;
9065
9066 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9067 {
9068 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9069 vim_free(tofree);
9070 }
9071 else
9072 {
9073 ga_concat(gap, (char_u *)"Expected ");
9074 if (exp_str == NULL)
9075 {
9076 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9077 vim_free(tofree);
9078 }
9079 else
9080 ga_concat(gap, exp_str);
9081 ga_concat(gap, (char_u *)" but got ");
9082 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9083 vim_free(tofree);
9084 }
9085}
Bram Moolenaar43345542015-11-29 17:35:35 +01009086
9087/*
9088 * Add an assert error to v:errors.
9089 */
9090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009091assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009092{
9093 struct vimvar *vp = &vimvars[VV_ERRORS];
9094
9095 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9096 /* Make sure v:errors is a list. */
9097 set_vim_var_list(VV_ERRORS, list_alloc());
9098 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9099}
9100
Bram Moolenaar43345542015-11-29 17:35:35 +01009101/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009102 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009103 */
9104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009105f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009106{
9107 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009108
9109 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9110 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009111 prepare_assert_error(&ga);
9112 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9113 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009114 ga_clear(&ga);
9115 }
9116}
9117
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009118/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009119 * "assert_exception(string[, msg])" function
9120 */
9121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009122f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009123{
9124 garray_T ga;
9125 char *error;
9126
9127 error = (char *)get_tv_string_chk(&argvars[0]);
9128 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9129 {
9130 prepare_assert_error(&ga);
9131 ga_concat(&ga, (char_u *)"v:exception is not set");
9132 assert_error(&ga);
9133 ga_clear(&ga);
9134 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009135 else if (error != NULL
9136 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009137 {
9138 prepare_assert_error(&ga);
9139 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9140 &vimvars[VV_EXCEPTION].vv_tv);
9141 assert_error(&ga);
9142 ga_clear(&ga);
9143 }
9144}
9145
9146/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009147 * "assert_fails(cmd [, error])" function
9148 */
9149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009150f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009151{
9152 char_u *cmd = get_tv_string_chk(&argvars[0]);
9153 garray_T ga;
9154
9155 called_emsg = FALSE;
9156 suppress_errthrow = TRUE;
9157 emsg_silent = TRUE;
9158 do_cmdline_cmd(cmd);
9159 if (!called_emsg)
9160 {
9161 prepare_assert_error(&ga);
9162 ga_concat(&ga, (char_u *)"command did not fail: ");
9163 ga_concat(&ga, cmd);
9164 assert_error(&ga);
9165 ga_clear(&ga);
9166 }
9167 else if (argvars[1].v_type != VAR_UNKNOWN)
9168 {
9169 char_u buf[NUMBUFLEN];
9170 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9171
9172 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9173 {
9174 prepare_assert_error(&ga);
9175 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9176 &vimvars[VV_ERRMSG].vv_tv);
9177 assert_error(&ga);
9178 ga_clear(&ga);
9179 }
9180 }
9181
9182 called_emsg = FALSE;
9183 suppress_errthrow = FALSE;
9184 emsg_silent = FALSE;
9185 emsg_on_display = FALSE;
9186 set_vim_var_string(VV_ERRMSG, NULL, 0);
9187}
9188
9189/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009190 * Common for assert_true() and assert_false().
9191 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009193assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009194{
9195 int error = FALSE;
9196 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009197
9198 if (argvars[0].v_type != VAR_NUMBER
9199 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9200 || error)
9201 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009202 prepare_assert_error(&ga);
9203 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009204 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009205 NULL, &argvars[0]);
9206 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009207 ga_clear(&ga);
9208 }
9209}
9210
9211/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009212 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009213 */
9214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009215f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009216{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009217 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009218}
9219
9220/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009221 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009222 */
9223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009224f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009225{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009226 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009227}
9228
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009229#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009230/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009231 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009232 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009234f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009235{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009236 float_T f;
9237
9238 rettv->v_type = VAR_FLOAT;
9239 if (get_float_arg(argvars, &f) == OK)
9240 rettv->vval.v_float = asin(f);
9241 else
9242 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009243}
9244
9245/*
9246 * "atan()" function
9247 */
9248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009249f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009250{
9251 float_T f;
9252
9253 rettv->v_type = VAR_FLOAT;
9254 if (get_float_arg(argvars, &f) == OK)
9255 rettv->vval.v_float = atan(f);
9256 else
9257 rettv->vval.v_float = 0.0;
9258}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009259
9260/*
9261 * "atan2()" function
9262 */
9263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009264f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009265{
9266 float_T fx, fy;
9267
9268 rettv->v_type = VAR_FLOAT;
9269 if (get_float_arg(argvars, &fx) == OK
9270 && get_float_arg(&argvars[1], &fy) == OK)
9271 rettv->vval.v_float = atan2(fx, fy);
9272 else
9273 rettv->vval.v_float = 0.0;
9274}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009275#endif
9276
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277/*
9278 * "browse(save, title, initdir, default)" function
9279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009281f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282{
9283#ifdef FEAT_BROWSE
9284 int save;
9285 char_u *title;
9286 char_u *initdir;
9287 char_u *defname;
9288 char_u buf[NUMBUFLEN];
9289 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009290 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009292 save = get_tv_number_chk(&argvars[0], &error);
9293 title = get_tv_string_chk(&argvars[1]);
9294 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9295 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009297 if (error || title == NULL || initdir == NULL || defname == NULL)
9298 rettv->vval.v_string = NULL;
9299 else
9300 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009301 do_browse(save ? BROWSE_SAVE : 0,
9302 title, defname, NULL, initdir, NULL, curbuf);
9303#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009304 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009305#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009306 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009307}
9308
9309/*
9310 * "browsedir(title, initdir)" function
9311 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009313f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009314{
9315#ifdef FEAT_BROWSE
9316 char_u *title;
9317 char_u *initdir;
9318 char_u buf[NUMBUFLEN];
9319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009320 title = get_tv_string_chk(&argvars[0]);
9321 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009323 if (title == NULL || initdir == NULL)
9324 rettv->vval.v_string = NULL;
9325 else
9326 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009327 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009329 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009331 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332}
9333
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009334static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009335
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336/*
9337 * Find a buffer by number or exact name.
9338 */
9339 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009340find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341{
9342 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009344 if (avar->v_type == VAR_NUMBER)
9345 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009346 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009348 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009349 if (buf == NULL)
9350 {
9351 /* No full path name match, try a match with a URL or a "nofile"
9352 * buffer, these don't use the full path. */
9353 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9354 if (buf->b_fname != NULL
9355 && (path_with_url(buf->b_fname)
9356#ifdef FEAT_QUICKFIX
9357 || bt_nofile(buf)
9358#endif
9359 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009360 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009361 break;
9362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 }
9364 return buf;
9365}
9366
9367/*
9368 * "bufexists(expr)" function
9369 */
9370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009371f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009373 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374}
9375
9376/*
9377 * "buflisted(expr)" function
9378 */
9379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009380f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381{
9382 buf_T *buf;
9383
9384 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009385 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386}
9387
9388/*
9389 * "bufloaded(expr)" function
9390 */
9391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009392f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
9394 buf_T *buf;
9395
9396 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398}
9399
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009400static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009401
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402/*
9403 * Get buffer by number or pattern.
9404 */
9405 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009406get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 int save_magic;
9410 char_u *save_cpo;
9411 buf_T *buf;
9412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413 if (tv->v_type == VAR_NUMBER)
9414 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009415 if (tv->v_type != VAR_STRING)
9416 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 if (name == NULL || *name == NUL)
9418 return curbuf;
9419 if (name[0] == '$' && name[1] == NUL)
9420 return lastbuf;
9421
9422 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9423 save_magic = p_magic;
9424 p_magic = TRUE;
9425 save_cpo = p_cpo;
9426 p_cpo = (char_u *)"";
9427
9428 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009429 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430
9431 p_magic = save_magic;
9432 p_cpo = save_cpo;
9433
9434 /* If not found, try expanding the name, like done for bufexists(). */
9435 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437
9438 return buf;
9439}
9440
9441/*
9442 * "bufname(expr)" function
9443 */
9444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009445f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446{
9447 buf_T *buf;
9448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009449 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009451 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009454 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 --emsg_off;
9458}
9459
9460/*
9461 * "bufnr(expr)" function
9462 */
9463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009464f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465{
9466 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009467 int error = FALSE;
9468 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009472 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009473 --emsg_off;
9474
9475 /* If the buffer isn't found and the second argument is not zero create a
9476 * new buffer. */
9477 if (buf == NULL
9478 && argvars[1].v_type != VAR_UNKNOWN
9479 && get_tv_number_chk(&argvars[1], &error) != 0
9480 && !error
9481 && (name = get_tv_string_chk(&argvars[0])) != NULL
9482 && !error)
9483 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9484
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489}
9490
9491/*
9492 * "bufwinnr(nr)" function
9493 */
9494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009495f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
9497#ifdef FEAT_WINDOWS
9498 win_T *wp;
9499 int winnr = 0;
9500#endif
9501 buf_T *buf;
9502
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009503 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009505 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506#ifdef FEAT_WINDOWS
9507 for (wp = firstwin; wp; wp = wp->w_next)
9508 {
9509 ++winnr;
9510 if (wp->w_buffer == buf)
9511 break;
9512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516#endif
9517 --emsg_off;
9518}
9519
9520/*
9521 * "byte2line(byte)" function
9522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009524f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525{
9526#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528#else
9529 long boff = 0;
9530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009531 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 (linenr_T)0, &boff);
9537#endif
9538}
9539
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009541byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009542{
9543#ifdef FEAT_MBYTE
9544 char_u *t;
9545#endif
9546 char_u *str;
9547 long idx;
9548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 str = get_tv_string_chk(&argvars[0]);
9550 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009552 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009553 return;
9554
9555#ifdef FEAT_MBYTE
9556 t = str;
9557 for ( ; idx > 0; idx--)
9558 {
9559 if (*t == NUL) /* EOL reached */
9560 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009561 if (enc_utf8 && comp)
9562 t += utf_ptr2len(t);
9563 else
9564 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009565 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009566 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009567#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009568 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009570#endif
9571}
9572
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009573/*
9574 * "byteidx()" function
9575 */
9576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009577f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009578{
9579 byteidx(argvars, rettv, FALSE);
9580}
9581
9582/*
9583 * "byteidxcomp()" function
9584 */
9585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009586f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009587{
9588 byteidx(argvars, rettv, TRUE);
9589}
9590
Bram Moolenaardb913952012-06-29 12:54:53 +02009591 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009592func_call(
9593 char_u *name,
9594 typval_T *args,
9595 dict_T *selfdict,
9596 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009597{
9598 listitem_T *item;
9599 typval_T argv[MAX_FUNC_ARGS + 1];
9600 int argc = 0;
9601 int dummy;
9602 int r = 0;
9603
9604 for (item = args->vval.v_list->lv_first; item != NULL;
9605 item = item->li_next)
9606 {
9607 if (argc == MAX_FUNC_ARGS)
9608 {
9609 EMSG(_("E699: Too many arguments"));
9610 break;
9611 }
9612 /* Make a copy of each argument. This is needed to be able to set
9613 * v_lock to VAR_FIXED in the copy without changing the original list.
9614 */
9615 copy_tv(&item->li_tv, &argv[argc++]);
9616 }
9617
9618 if (item == NULL)
9619 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9620 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9621 &dummy, TRUE, selfdict);
9622
9623 /* Free the arguments. */
9624 while (argc > 0)
9625 clear_tv(&argv[--argc]);
9626
9627 return r;
9628}
9629
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009630/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009631 * "call(func, arglist)" function
9632 */
9633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009634f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009635{
9636 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009637 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009638
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009639 if (argvars[1].v_type != VAR_LIST)
9640 {
9641 EMSG(_(e_listreq));
9642 return;
9643 }
9644 if (argvars[1].vval.v_list == NULL)
9645 return;
9646
9647 if (argvars[0].v_type == VAR_FUNC)
9648 func = argvars[0].vval.v_string;
9649 else
9650 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009651 if (*func == NUL)
9652 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009653
Bram Moolenaare9a41262005-01-15 22:18:47 +00009654 if (argvars[2].v_type != VAR_UNKNOWN)
9655 {
9656 if (argvars[2].v_type != VAR_DICT)
9657 {
9658 EMSG(_(e_dictreq));
9659 return;
9660 }
9661 selfdict = argvars[2].vval.v_dict;
9662 }
9663
Bram Moolenaardb913952012-06-29 12:54:53 +02009664 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009665}
9666
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009667#ifdef FEAT_FLOAT
9668/*
9669 * "ceil({float})" function
9670 */
9671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009672f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009673{
9674 float_T f;
9675
9676 rettv->v_type = VAR_FLOAT;
9677 if (get_float_arg(argvars, &f) == OK)
9678 rettv->vval.v_float = ceil(f);
9679 else
9680 rettv->vval.v_float = 0.0;
9681}
9682#endif
9683
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009684#ifdef FEAT_CHANNEL
9685/*
9686 * Get the channel index from the handle argument.
9687 * Returns -1 if the handle is invalid or the channel is closed.
9688 */
9689 static int
9690get_channel_arg(typval_T *tv)
9691{
9692 int ch_idx;
9693
9694 if (tv->v_type != VAR_NUMBER)
9695 {
9696 EMSG2(_(e_invarg2), get_tv_string(tv));
9697 return -1;
9698 }
9699 ch_idx = tv->vval.v_number;
9700
9701 if (!channel_is_open(ch_idx))
9702 {
9703 EMSGN(_("E906: not an open channel"), ch_idx);
9704 return -1;
9705 }
9706 return ch_idx;
9707}
9708
9709/*
9710 * "ch_close()" function
9711 */
9712 static void
9713f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9714{
9715 int ch_idx = get_channel_arg(&argvars[0]);
9716
9717 if (ch_idx >= 0)
9718 channel_close(ch_idx);
9719}
9720
9721/*
9722 * Get a callback from "arg". It can be a Funcref or a function name.
9723 * When "arg" is zero return an empty string.
9724 * Return NULL for an invalid argument.
9725 */
9726 static char_u *
9727get_callback(typval_T *arg)
9728{
9729 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9730 return arg->vval.v_string;
9731 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9732 return (char_u *)"";
9733 EMSG(_("E999: Invalid callback argument"));
9734 return NULL;
9735}
9736
9737/*
9738 * "ch_open()" function
9739 */
9740 static void
9741f_ch_open(typval_T *argvars, typval_T *rettv)
9742{
9743 char_u *address;
9744 char_u *mode;
9745 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009746 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009747 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009748 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009749 int waittime = 0;
9750 int timeout = 2000;
9751 int json_mode = TRUE;
9752 int ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009753
9754 /* default: fail */
9755 rettv->vval.v_number = -1;
9756
9757 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009758 if (argvars[1].v_type != VAR_UNKNOWN
9759 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009760 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009761 EMSG(_(e_invarg));
9762 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009763 }
9764
9765 /* parse address */
9766 p = vim_strchr(address, ':');
9767 if (p == NULL)
9768 {
9769 EMSG2(_(e_invarg2), address);
9770 return;
9771 }
9772 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009773 port = strtol((char *)p, &rest, 10);
9774 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009775 {
9776 p[-1] = ':';
9777 EMSG2(_(e_invarg2), address);
9778 return;
9779 }
9780
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009781 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009782 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009783 /* parse argdict */
9784 dict_T *dict = argvars[1].vval.v_dict;
9785
9786 if (dict_find(dict, (char_u *)"mode", -1) != NULL)
9787 {
9788 mode = get_dict_string(dict, (char_u *)"mode", FALSE);
9789 if (STRCMP(mode, "raw") == 0)
9790 json_mode = FALSE;
9791 else if (STRCMP(mode, "json") != 0)
9792 {
9793 EMSG2(_(e_invarg2), mode);
9794 return;
9795 }
9796 }
9797 if (dict_find(dict, (char_u *)"waittime", -1) != NULL)
9798 waittime = get_dict_number(dict, (char_u *)"waittime");
9799 if (dict_find(dict, (char_u *)"timeout", -1) != NULL)
9800 timeout = get_dict_number(dict, (char_u *)"timeout");
9801 if (dict_find(dict, (char_u *)"callback", -1) != NULL)
9802 callback = get_dict_string(dict, (char_u *)"callback", FALSE);
9803 }
9804 if (waittime < 0 || timeout < 0)
9805 {
9806 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009807 return;
9808 }
9809
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009810 ch_idx = channel_open((char *)address, port, waittime, NULL);
9811 if (ch_idx >= 0)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009812 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009813 channel_set_json_mode(ch_idx, json_mode);
9814 channel_set_timeout(ch_idx, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009815 if (callback != NULL && *callback != NUL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009816 channel_set_callback(ch_idx, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009817 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009818 rettv->vval.v_number = ch_idx;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009819}
9820
9821/*
9822 * common for "sendexpr()" and "sendraw()"
9823 * Returns the channel index if the caller should read the response.
9824 * Otherwise returns -1.
9825 */
9826 static int
Bram Moolenaara07fec92016-02-05 21:04:08 +01009827send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009828{
9829 int ch_idx;
9830 char_u *callback = NULL;
9831
9832 ch_idx = get_channel_arg(&argvars[0]);
9833 if (ch_idx < 0)
9834 return -1;
9835
9836 if (argvars[2].v_type != VAR_UNKNOWN)
9837 {
9838 callback = get_callback(&argvars[2]);
9839 if (callback == NULL)
9840 return -1;
9841 }
Bram Moolenaara07fec92016-02-05 21:04:08 +01009842 /* Set the callback. An empty callback means no callback and not reading
9843 * the response. */
9844 if (callback != NULL && *callback != NUL)
9845 channel_set_req_callback(ch_idx, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009846
9847 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
9848 return ch_idx;
9849 return -1;
9850}
9851
9852/*
9853 * "ch_sendexpr()" function
9854 */
9855 static void
9856f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
9857{
9858 char_u *text;
9859 typval_T *listtv;
9860 int ch_idx;
9861 int id;
9862
9863 /* return an empty string by default */
9864 rettv->v_type = VAR_STRING;
9865 rettv->vval.v_string = NULL;
9866
9867 id = channel_get_id();
9868 text = json_encode_nr_expr(id, &argvars[1]);
9869 if (text == NULL)
9870 return;
9871
Bram Moolenaara07fec92016-02-05 21:04:08 +01009872 ch_idx = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01009873 vim_free(text);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009874 if (ch_idx >= 0)
9875 {
9876 if (channel_read_json_block(ch_idx, id, &listtv) == OK)
9877 {
9878 if (listtv->v_type == VAR_LIST)
9879 {
9880 list_T *list = listtv->vval.v_list;
9881
9882 if (list->lv_len == 2)
9883 {
9884 /* Move the item from the list and then change the type to
9885 * avoid the value being freed. */
9886 *rettv = list->lv_last->li_tv;
9887 list->lv_last->li_tv.v_type = VAR_NUMBER;
9888 }
9889 }
9890 clear_tv(listtv);
9891 }
9892 }
9893}
9894
9895/*
9896 * "ch_sendraw()" function
9897 */
9898 static void
9899f_ch_sendraw(typval_T *argvars, typval_T *rettv)
9900{
9901 char_u buf[NUMBUFLEN];
9902 char_u *text;
9903 int ch_idx;
9904
9905 /* return an empty string by default */
9906 rettv->v_type = VAR_STRING;
9907 rettv->vval.v_string = NULL;
9908
9909 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaara07fec92016-02-05 21:04:08 +01009910 ch_idx = send_common(argvars, text, 0, "sendraw");
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009911 if (ch_idx >= 0)
9912 rettv->vval.v_string = channel_read_block(ch_idx);
9913}
9914#endif
9915
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009916/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009917 * "changenr()" function
9918 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009920f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009921{
9922 rettv->vval.v_number = curbuf->b_u_seq_cur;
9923}
9924
9925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 * "char2nr(string)" function
9927 */
9928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009929f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930{
9931#ifdef FEAT_MBYTE
9932 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009933 {
9934 int utf8 = 0;
9935
9936 if (argvars[1].v_type != VAR_UNKNOWN)
9937 utf8 = get_tv_number_chk(&argvars[1], NULL);
9938
9939 if (utf8)
9940 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9941 else
9942 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 else
9945#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947}
9948
9949/*
9950 * "cindent(lnum)" function
9951 */
9952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009953f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954{
9955#ifdef FEAT_CINDENT
9956 pos_T pos;
9957 linenr_T lnum;
9958
9959 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009960 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9962 {
9963 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 curwin->w_cursor = pos;
9966 }
9967 else
9968#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009969 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970}
9971
9972/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009973 * "clearmatches()" function
9974 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009976f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009977{
9978#ifdef FEAT_SEARCH_EXTRA
9979 clear_matches(curwin);
9980#endif
9981}
9982
9983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 * "col(string)" function
9985 */
9986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009987f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988{
9989 colnr_T col = 0;
9990 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009991 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009993 fp = var2fpos(&argvars[0], FALSE, &fnum);
9994 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 {
9996 if (fp->col == MAXCOL)
9997 {
9998 /* '> can be MAXCOL, get the length of the line then */
9999 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010000 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 else
10002 col = MAXCOL;
10003 }
10004 else
10005 {
10006 col = fp->col + 1;
10007#ifdef FEAT_VIRTUALEDIT
10008 /* col(".") when the cursor is on the NUL at the end of the line
10009 * because of "coladd" can be seen as an extra column. */
10010 if (virtual_active() && fp == &curwin->w_cursor)
10011 {
10012 char_u *p = ml_get_cursor();
10013
10014 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10015 curwin->w_virtcol - curwin->w_cursor.coladd))
10016 {
10017# ifdef FEAT_MBYTE
10018 int l;
10019
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010020 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 col += l;
10022# else
10023 if (*p != NUL && p[1] == NUL)
10024 ++col;
10025# endif
10026 }
10027 }
10028#endif
10029 }
10030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032}
10033
Bram Moolenaar572cb562005-08-05 21:35:02 +000010034#if defined(FEAT_INS_EXPAND)
10035/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010036 * "complete()" function
10037 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010039f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010040{
10041 int startcol;
10042
10043 if ((State & INSERT) == 0)
10044 {
10045 EMSG(_("E785: complete() can only be used in Insert mode"));
10046 return;
10047 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010048
10049 /* Check for undo allowed here, because if something was already inserted
10050 * the line was already saved for undo and this check isn't done. */
10051 if (!undo_allowed())
10052 return;
10053
Bram Moolenaarade00832006-03-10 21:46:58 +000010054 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10055 {
10056 EMSG(_(e_invarg));
10057 return;
10058 }
10059
10060 startcol = get_tv_number_chk(&argvars[0], NULL);
10061 if (startcol <= 0)
10062 return;
10063
10064 set_completion(startcol - 1, argvars[1].vval.v_list);
10065}
10066
10067/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010068 * "complete_add()" function
10069 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010071f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010072{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010073 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010074}
10075
10076/*
10077 * "complete_check()" function
10078 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010080f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010081{
10082 int saved = RedrawingDisabled;
10083
10084 RedrawingDisabled = 0;
10085 ins_compl_check_keys(0);
10086 rettv->vval.v_number = compl_interrupted;
10087 RedrawingDisabled = saved;
10088}
10089#endif
10090
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091/*
10092 * "confirm(message, buttons[, default [, type]])" function
10093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010095f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096{
10097#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10098 char_u *message;
10099 char_u *buttons = NULL;
10100 char_u buf[NUMBUFLEN];
10101 char_u buf2[NUMBUFLEN];
10102 int def = 1;
10103 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010104 char_u *typestr;
10105 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010107 message = get_tv_string_chk(&argvars[0]);
10108 if (message == NULL)
10109 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010110 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010112 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10113 if (buttons == NULL)
10114 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010115 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010117 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010118 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010120 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10121 if (typestr == NULL)
10122 error = TRUE;
10123 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010125 switch (TOUPPER_ASC(*typestr))
10126 {
10127 case 'E': type = VIM_ERROR; break;
10128 case 'Q': type = VIM_QUESTION; break;
10129 case 'I': type = VIM_INFO; break;
10130 case 'W': type = VIM_WARNING; break;
10131 case 'G': type = VIM_GENERIC; break;
10132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 }
10134 }
10135 }
10136 }
10137
10138 if (buttons == NULL || *buttons == NUL)
10139 buttons = (char_u *)_("&Ok");
10140
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010141 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010142 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010143 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144#endif
10145}
10146
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010147/*
10148 * "copy()" function
10149 */
10150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010151f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010152{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010153 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010154}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010156#ifdef FEAT_FLOAT
10157/*
10158 * "cos()" function
10159 */
10160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010161f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010162{
10163 float_T f;
10164
10165 rettv->v_type = VAR_FLOAT;
10166 if (get_float_arg(argvars, &f) == OK)
10167 rettv->vval.v_float = cos(f);
10168 else
10169 rettv->vval.v_float = 0.0;
10170}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010171
10172/*
10173 * "cosh()" function
10174 */
10175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010176f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010177{
10178 float_T f;
10179
10180 rettv->v_type = VAR_FLOAT;
10181 if (get_float_arg(argvars, &f) == OK)
10182 rettv->vval.v_float = cosh(f);
10183 else
10184 rettv->vval.v_float = 0.0;
10185}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010186#endif
10187
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010189 * "count()" function
10190 */
10191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010192f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010193{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010194 long n = 0;
10195 int ic = FALSE;
10196
Bram Moolenaare9a41262005-01-15 22:18:47 +000010197 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010198 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010199 listitem_T *li;
10200 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010201 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010202
Bram Moolenaare9a41262005-01-15 22:18:47 +000010203 if ((l = argvars[0].vval.v_list) != NULL)
10204 {
10205 li = l->lv_first;
10206 if (argvars[2].v_type != VAR_UNKNOWN)
10207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 int error = FALSE;
10209
10210 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010211 if (argvars[3].v_type != VAR_UNKNOWN)
10212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 idx = get_tv_number_chk(&argvars[3], &error);
10214 if (!error)
10215 {
10216 li = list_find(l, idx);
10217 if (li == NULL)
10218 EMSGN(_(e_listidx), idx);
10219 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010220 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221 if (error)
10222 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010223 }
10224
10225 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010226 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010227 ++n;
10228 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010229 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010230 else if (argvars[0].v_type == VAR_DICT)
10231 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010232 int todo;
10233 dict_T *d;
10234 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010235
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010236 if ((d = argvars[0].vval.v_dict) != NULL)
10237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010238 int error = FALSE;
10239
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 if (argvars[2].v_type != VAR_UNKNOWN)
10241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010242 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 if (argvars[3].v_type != VAR_UNKNOWN)
10244 EMSG(_(e_invarg));
10245 }
10246
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010247 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010248 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010249 {
10250 if (!HASHITEM_EMPTY(hi))
10251 {
10252 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010253 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010254 ++n;
10255 }
10256 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010257 }
10258 }
10259 else
10260 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010261 rettv->vval.v_number = n;
10262}
10263
10264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10266 *
10267 * Checks the existence of a cscope connection.
10268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010270f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271{
10272#ifdef FEAT_CSCOPE
10273 int num = 0;
10274 char_u *dbpath = NULL;
10275 char_u *prepend = NULL;
10276 char_u buf[NUMBUFLEN];
10277
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010278 if (argvars[0].v_type != VAR_UNKNOWN
10279 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010281 num = (int)get_tv_number(&argvars[0]);
10282 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010283 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010284 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 }
10286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010287 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288#endif
10289}
10290
10291/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010292 * "cursor(lnum, col)" function, or
10293 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010295 * Moves the cursor to the specified line and column.
10296 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010299f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300{
10301 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010302#ifdef FEAT_VIRTUALEDIT
10303 long coladd = 0;
10304#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010305 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010307 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010308 if (argvars[1].v_type == VAR_UNKNOWN)
10309 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010310 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010311 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010312
Bram Moolenaar493c1782014-05-28 14:34:46 +020010313 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010314 {
10315 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010316 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010317 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010318 line = pos.lnum;
10319 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010320#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010321 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010322#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010323 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010324 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010325 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010326 set_curswant = FALSE;
10327 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010328 }
10329 else
10330 {
10331 line = get_tv_lnum(argvars);
10332 col = get_tv_number_chk(&argvars[1], NULL);
10333#ifdef FEAT_VIRTUALEDIT
10334 if (argvars[2].v_type != VAR_UNKNOWN)
10335 coladd = get_tv_number_chk(&argvars[2], NULL);
10336#endif
10337 }
10338 if (line < 0 || col < 0
10339#ifdef FEAT_VIRTUALEDIT
10340 || coladd < 0
10341#endif
10342 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010343 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 if (line > 0)
10345 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 if (col > 0)
10347 curwin->w_cursor.col = col - 1;
10348#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010349 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350#endif
10351
10352 /* Make sure the cursor is in a valid position. */
10353 check_cursor();
10354#ifdef FEAT_MBYTE
10355 /* Correct cursor for multi-byte character. */
10356 if (has_mbyte)
10357 mb_adjust_cursor();
10358#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010359
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010360 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010361 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362}
10363
10364/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010365 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 */
10367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010368f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010370 int noref = 0;
10371
10372 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010373 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010374 if (noref < 0 || noref > 1)
10375 EMSG(_(e_invarg));
10376 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010377 {
10378 current_copyID += COPYID_INC;
10379 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381}
10382
10383/*
10384 * "delete()" function
10385 */
10386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010387f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010389 char_u nbuf[NUMBUFLEN];
10390 char_u *name;
10391 char_u *flags;
10392
10393 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010395 return;
10396
10397 name = get_tv_string(&argvars[0]);
10398 if (name == NULL || *name == NUL)
10399 {
10400 EMSG(_(e_invarg));
10401 return;
10402 }
10403
10404 if (argvars[1].v_type != VAR_UNKNOWN)
10405 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010407 flags = (char_u *)"";
10408
10409 if (*flags == NUL)
10410 /* delete a file */
10411 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10412 else if (STRCMP(flags, "d") == 0)
10413 /* delete an empty directory */
10414 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10415 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010416 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010417 rettv->vval.v_number = delete_recursive(name);
10418 else
10419 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420}
10421
10422/*
10423 * "did_filetype()" function
10424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010426f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427{
10428#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430#endif
10431}
10432
10433/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010434 * "diff_filler()" function
10435 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010437f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010438{
10439#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010440 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010441#endif
10442}
10443
10444/*
10445 * "diff_hlID()" function
10446 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010448f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010449{
10450#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010451 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010452 static linenr_T prev_lnum = 0;
10453 static int changedtick = 0;
10454 static int fnum = 0;
10455 static int change_start = 0;
10456 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010457 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010458 int filler_lines;
10459 int col;
10460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010461 if (lnum < 0) /* ignore type error in {lnum} arg */
10462 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010463 if (lnum != prev_lnum
10464 || changedtick != curbuf->b_changedtick
10465 || fnum != curbuf->b_fnum)
10466 {
10467 /* New line, buffer, change: need to get the values. */
10468 filler_lines = diff_check(curwin, lnum);
10469 if (filler_lines < 0)
10470 {
10471 if (filler_lines == -1)
10472 {
10473 change_start = MAXCOL;
10474 change_end = -1;
10475 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10476 hlID = HLF_ADD; /* added line */
10477 else
10478 hlID = HLF_CHD; /* changed line */
10479 }
10480 else
10481 hlID = HLF_ADD; /* added line */
10482 }
10483 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010484 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010485 prev_lnum = lnum;
10486 changedtick = curbuf->b_changedtick;
10487 fnum = curbuf->b_fnum;
10488 }
10489
10490 if (hlID == HLF_CHD || hlID == HLF_TXD)
10491 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010492 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010493 if (col >= change_start && col <= change_end)
10494 hlID = HLF_TXD; /* changed text */
10495 else
10496 hlID = HLF_CHD; /* changed line */
10497 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010498 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010499#endif
10500}
10501
10502/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010503 * "empty({expr})" function
10504 */
10505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010506f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010507{
10508 int n;
10509
10510 switch (argvars[0].v_type)
10511 {
10512 case VAR_STRING:
10513 case VAR_FUNC:
10514 n = argvars[0].vval.v_string == NULL
10515 || *argvars[0].vval.v_string == NUL;
10516 break;
10517 case VAR_NUMBER:
10518 n = argvars[0].vval.v_number == 0;
10519 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010520#ifdef FEAT_FLOAT
10521 case VAR_FLOAT:
10522 n = argvars[0].vval.v_float == 0.0;
10523 break;
10524#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010525 case VAR_LIST:
10526 n = argvars[0].vval.v_list == NULL
10527 || argvars[0].vval.v_list->lv_first == NULL;
10528 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010529 case VAR_DICT:
10530 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010531 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010532 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010533 case VAR_SPECIAL:
10534 n = argvars[0].vval.v_number != VVAL_TRUE;
10535 break;
10536
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010537 default:
10538 EMSG2(_(e_intern2), "f_empty()");
10539 n = 0;
10540 }
10541
10542 rettv->vval.v_number = n;
10543}
10544
10545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 * "escape({string}, {chars})" function
10547 */
10548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010549f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550{
10551 char_u buf[NUMBUFLEN];
10552
Bram Moolenaar758711c2005-02-02 23:11:38 +000010553 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10554 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556}
10557
10558/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010559 * "eval()" function
10560 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010562f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010563{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010564 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010566 s = get_tv_string_chk(&argvars[0]);
10567 if (s != NULL)
10568 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010569
Bram Moolenaar615b9972015-01-14 17:15:05 +010010570 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10572 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010573 if (p != NULL && !aborting())
10574 EMSG2(_(e_invexpr2), p);
10575 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010577 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010578 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010579 else if (*s != NUL)
10580 EMSG(_(e_trailing));
10581}
10582
10583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 * "eventhandler()" function
10585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010587f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010589 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590}
10591
10592/*
10593 * "executable()" function
10594 */
10595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010596f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010598 char_u *name = get_tv_string(&argvars[0]);
10599
10600 /* Check in $PATH and also check directly if there is a directory name. */
10601 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10602 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010603}
10604
10605/*
10606 * "exepath()" function
10607 */
10608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010609f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010610{
10611 char_u *p = NULL;
10612
Bram Moolenaarb5971142015-03-21 17:32:19 +010010613 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010614 rettv->v_type = VAR_STRING;
10615 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616}
10617
10618/*
10619 * "exists()" function
10620 */
10621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010622f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623{
10624 char_u *p;
10625 char_u *name;
10626 int n = FALSE;
10627 int len = 0;
10628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 if (*p == '$') /* environment variable */
10631 {
10632 /* first try "normal" environment variables (fast) */
10633 if (mch_getenv(p + 1) != NULL)
10634 n = TRUE;
10635 else
10636 {
10637 /* try expanding things like $VIM and ${HOME} */
10638 p = expand_env_save(p);
10639 if (p != NULL && *p != '$')
10640 n = TRUE;
10641 vim_free(p);
10642 }
10643 }
10644 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010646 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010647 if (*skipwhite(p) != NUL)
10648 n = FALSE; /* trailing garbage */
10649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 else if (*p == '*') /* internal or user defined function */
10651 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010652 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 }
10654 else if (*p == ':')
10655 {
10656 n = cmd_exists(p + 1);
10657 }
10658 else if (*p == '#')
10659 {
10660#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010661 if (p[1] == '#')
10662 n = autocmd_supported(p + 2);
10663 else
10664 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665#endif
10666 }
10667 else /* internal variable */
10668 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010669 char_u *tofree;
10670 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010672 /* get_name_len() takes care of expanding curly braces */
10673 name = p;
10674 len = get_name_len(&p, &tofree, TRUE, FALSE);
10675 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010677 if (tofree != NULL)
10678 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010679 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010680 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010682 /* handle d.key, l[idx], f(expr) */
10683 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10684 if (n)
10685 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 }
10687 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010688 if (*p != NUL)
10689 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010691 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 }
10693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010694 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695}
10696
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010697#ifdef FEAT_FLOAT
10698/*
10699 * "exp()" function
10700 */
10701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010702f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010703{
10704 float_T f;
10705
10706 rettv->v_type = VAR_FLOAT;
10707 if (get_float_arg(argvars, &f) == OK)
10708 rettv->vval.v_float = exp(f);
10709 else
10710 rettv->vval.v_float = 0.0;
10711}
10712#endif
10713
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714/*
10715 * "expand()" function
10716 */
10717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010718f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719{
10720 char_u *s;
10721 int len;
10722 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010723 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010725 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010726 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010728 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010729 if (argvars[1].v_type != VAR_UNKNOWN
10730 && argvars[2].v_type != VAR_UNKNOWN
10731 && get_tv_number_chk(&argvars[2], &error)
10732 && !error)
10733 {
10734 rettv->v_type = VAR_LIST;
10735 rettv->vval.v_list = NULL;
10736 }
10737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010738 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 if (*s == '%' || *s == '#' || *s == '<')
10740 {
10741 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010742 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010744 if (rettv->v_type == VAR_LIST)
10745 {
10746 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10747 list_append_string(rettv->vval.v_list, result, -1);
10748 else
10749 vim_free(result);
10750 }
10751 else
10752 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 }
10754 else
10755 {
10756 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010757 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010758 if (argvars[1].v_type != VAR_UNKNOWN
10759 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010760 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010761 if (!error)
10762 {
10763 ExpandInit(&xpc);
10764 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010765 if (p_wic)
10766 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010767 if (rettv->v_type == VAR_STRING)
10768 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10769 options, WILD_ALL);
10770 else if (rettv_list_alloc(rettv) != FAIL)
10771 {
10772 int i;
10773
10774 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10775 for (i = 0; i < xpc.xp_numfiles; i++)
10776 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10777 ExpandCleanup(&xpc);
10778 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010779 }
10780 else
10781 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 }
10783}
10784
10785/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010786 * Go over all entries in "d2" and add them to "d1".
10787 * When "action" is "error" then a duplicate key is an error.
10788 * When "action" is "force" then a duplicate key is overwritten.
10789 * Otherwise duplicate keys are ignored ("action" is "keep").
10790 */
10791 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010792dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010793{
10794 dictitem_T *di1;
10795 hashitem_T *hi2;
10796 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010797 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010798
10799 todo = (int)d2->dv_hashtab.ht_used;
10800 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10801 {
10802 if (!HASHITEM_EMPTY(hi2))
10803 {
10804 --todo;
10805 di1 = dict_find(d1, hi2->hi_key, -1);
10806 if (d1->dv_scope != 0)
10807 {
10808 /* Disallow replacing a builtin function in l: and g:.
10809 * Check the key to be valid when adding to any
10810 * scope. */
10811 if (d1->dv_scope == VAR_DEF_SCOPE
10812 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10813 && var_check_func_name(hi2->hi_key,
10814 di1 == NULL))
10815 break;
10816 if (!valid_varname(hi2->hi_key))
10817 break;
10818 }
10819 if (di1 == NULL)
10820 {
10821 di1 = dictitem_copy(HI2DI(hi2));
10822 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10823 dictitem_free(di1);
10824 }
10825 else if (*action == 'e')
10826 {
10827 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10828 break;
10829 }
10830 else if (*action == 'f' && HI2DI(hi2) != di1)
10831 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010832 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10833 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010834 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010835 clear_tv(&di1->di_tv);
10836 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10837 }
10838 }
10839 }
10840}
10841
10842/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010843 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010844 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010845 */
10846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010847f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010848{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010849 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010850
Bram Moolenaare9a41262005-01-15 22:18:47 +000010851 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010852 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010853 list_T *l1, *l2;
10854 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010855 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010856 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010857
Bram Moolenaare9a41262005-01-15 22:18:47 +000010858 l1 = argvars[0].vval.v_list;
10859 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010860 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010861 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010862 {
10863 if (argvars[2].v_type != VAR_UNKNOWN)
10864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 before = get_tv_number_chk(&argvars[2], &error);
10866 if (error)
10867 return; /* type error; errmsg already given */
10868
Bram Moolenaar758711c2005-02-02 23:11:38 +000010869 if (before == l1->lv_len)
10870 item = NULL;
10871 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010872 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010873 item = list_find(l1, before);
10874 if (item == NULL)
10875 {
10876 EMSGN(_(e_listidx), before);
10877 return;
10878 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010879 }
10880 }
10881 else
10882 item = NULL;
10883 list_extend(l1, l2, item);
10884
Bram Moolenaare9a41262005-01-15 22:18:47 +000010885 copy_tv(&argvars[0], rettv);
10886 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010887 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010888 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10889 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010890 dict_T *d1, *d2;
10891 char_u *action;
10892 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010893
10894 d1 = argvars[0].vval.v_dict;
10895 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010896 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010897 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010898 {
10899 /* Check the third argument. */
10900 if (argvars[2].v_type != VAR_UNKNOWN)
10901 {
10902 static char *(av[]) = {"keep", "force", "error"};
10903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010904 action = get_tv_string_chk(&argvars[2]);
10905 if (action == NULL)
10906 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010907 for (i = 0; i < 3; ++i)
10908 if (STRCMP(action, av[i]) == 0)
10909 break;
10910 if (i == 3)
10911 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010912 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010913 return;
10914 }
10915 }
10916 else
10917 action = (char_u *)"force";
10918
Bram Moolenaara9922d62013-05-30 13:01:18 +020010919 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010920
Bram Moolenaare9a41262005-01-15 22:18:47 +000010921 copy_tv(&argvars[0], rettv);
10922 }
10923 }
10924 else
10925 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010926}
10927
10928/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010929 * "feedkeys()" function
10930 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010932f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010933{
10934 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010935 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010936 char_u *keys, *flags;
10937 char_u nbuf[NUMBUFLEN];
10938 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010939 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010940 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010941
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010942 /* This is not allowed in the sandbox. If the commands would still be
10943 * executed in the sandbox it would be OK, but it probably happens later,
10944 * when "sandbox" is no longer set. */
10945 if (check_secure())
10946 return;
10947
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010948 keys = get_tv_string(&argvars[0]);
10949 if (*keys != NUL)
10950 {
10951 if (argvars[1].v_type != VAR_UNKNOWN)
10952 {
10953 flags = get_tv_string_buf(&argvars[1], nbuf);
10954 for ( ; *flags != NUL; ++flags)
10955 {
10956 switch (*flags)
10957 {
10958 case 'n': remap = FALSE; break;
10959 case 'm': remap = TRUE; break;
10960 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010961 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010962 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010963 }
10964 }
10965 }
10966
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010967 /* Need to escape K_SPECIAL and CSI before putting the string in the
10968 * typeahead buffer. */
10969 keys_esc = vim_strsave_escape_csi(keys);
10970 if (keys_esc != NULL)
10971 {
10972 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010973 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010974 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010975 if (vgetc_busy)
10976 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010010977 if (execute)
10978 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010979 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010980 }
10981}
10982
10983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 * "filereadable()" function
10985 */
10986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010987f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010989 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 char_u *p;
10991 int n;
10992
Bram Moolenaarc236c162008-07-13 17:41:49 +000010993#ifndef O_NONBLOCK
10994# define O_NONBLOCK 0
10995#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010996 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010997 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10998 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 {
11000 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011001 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 }
11003 else
11004 n = FALSE;
11005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011006 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007}
11008
11009/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011010 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 * rights to write into.
11012 */
11013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011014f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011016 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017}
11018
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011019 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011020findfilendir(
11021 typval_T *argvars UNUSED,
11022 typval_T *rettv,
11023 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011024{
11025#ifdef FEAT_SEARCHPATH
11026 char_u *fname;
11027 char_u *fresult = NULL;
11028 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11029 char_u *p;
11030 char_u pathbuf[NUMBUFLEN];
11031 int count = 1;
11032 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011033 int error = FALSE;
11034#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011035
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011036 rettv->vval.v_string = NULL;
11037 rettv->v_type = VAR_STRING;
11038
11039#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011041
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011042 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011044 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11045 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011046 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 else
11048 {
11049 if (*p != NUL)
11050 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011052 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011053 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011054 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011055 }
11056
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011057 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11058 error = TRUE;
11059
11060 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011061 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011062 do
11063 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011064 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011065 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066 fresult = find_file_in_path_option(first ? fname : NULL,
11067 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011068 0, first, path,
11069 find_what,
11070 curbuf->b_ffname,
11071 find_what == FINDFILE_DIR
11072 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011073 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011074
11075 if (fresult != NULL && rettv->v_type == VAR_LIST)
11076 list_append_string(rettv->vval.v_list, fresult, -1);
11077
11078 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011079 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011080
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011081 if (rettv->v_type == VAR_STRING)
11082 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011083#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011084}
11085
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011086static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11087static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011088
11089/*
11090 * Implementation of map() and filter().
11091 */
11092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011093filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011094{
11095 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011096 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011097 listitem_T *li, *nli;
11098 list_T *l = NULL;
11099 dictitem_T *di;
11100 hashtab_T *ht;
11101 hashitem_T *hi;
11102 dict_T *d = NULL;
11103 typval_T save_val;
11104 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011105 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011106 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011107 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011108 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011109 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011110 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011111 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011112
Bram Moolenaare9a41262005-01-15 22:18:47 +000011113 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011114 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011115 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011116 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011117 return;
11118 }
11119 else if (argvars[0].v_type == VAR_DICT)
11120 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011121 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011122 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011123 return;
11124 }
11125 else
11126 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011127 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011128 return;
11129 }
11130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011131 expr = get_tv_string_buf_chk(&argvars[1], buf);
11132 /* On type errors, the preceding call has already displayed an error
11133 * message. Avoid a misleading error message for an empty string that
11134 * was not passed as argument. */
11135 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011137 prepare_vimvar(VV_VAL, &save_val);
11138 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011139
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011140 /* We reset "did_emsg" to be able to detect whether an error
11141 * occurred during evaluation of the expression. */
11142 save_did_emsg = did_emsg;
11143 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011144
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011145 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011146 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 vimvars[VV_KEY].vv_type = VAR_STRING;
11149
11150 ht = &d->dv_hashtab;
11151 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011152 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011153 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011155 if (!HASHITEM_EMPTY(hi))
11156 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011157 int r;
11158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011159 --todo;
11160 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011161 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011162 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11163 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011164 break;
11165 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011166 r = filter_map_one(&di->di_tv, expr, map, &rem);
11167 clear_tv(&vimvars[VV_KEY].vv_tv);
11168 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011169 break;
11170 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011171 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011172 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11173 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011174 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011176 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011177 }
11178 }
11179 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011180 }
11181 else
11182 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011183 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011185 for (li = l->lv_first; li != NULL; li = nli)
11186 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011187 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011188 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011189 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011190 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011191 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011192 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011193 break;
11194 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011195 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011196 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011197 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011198 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011199
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011200 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011201 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011202
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011203 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011204 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011205
11206 copy_tv(&argvars[0], rettv);
11207}
11208
11209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011210filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011211{
Bram Moolenaar33570922005-01-25 22:26:29 +000011212 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011213 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011214 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011215
Bram Moolenaar33570922005-01-25 22:26:29 +000011216 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011217 s = expr;
11218 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011219 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011220 if (*s != NUL) /* check for trailing chars after expr */
11221 {
11222 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011223 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011224 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011225 }
11226 if (map)
11227 {
11228 /* map(): replace the list item value */
11229 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011230 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011231 *tv = rettv;
11232 }
11233 else
11234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011235 int error = FALSE;
11236
Bram Moolenaare9a41262005-01-15 22:18:47 +000011237 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011238 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011239 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011240 /* On type error, nothing has been removed; return FAIL to stop the
11241 * loop. The error message was given by get_tv_number_chk(). */
11242 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011243 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011244 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011245 retval = OK;
11246theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011247 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011248 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011249}
11250
11251/*
11252 * "filter()" function
11253 */
11254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011255f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011256{
11257 filter_map(argvars, rettv, FALSE);
11258}
11259
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011260/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011261 * "finddir({fname}[, {path}[, {count}]])" function
11262 */
11263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011264f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011265{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011266 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011267}
11268
11269/*
11270 * "findfile({fname}[, {path}[, {count}]])" function
11271 */
11272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011273f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011274{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011275 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011276}
11277
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011278#ifdef FEAT_FLOAT
11279/*
11280 * "float2nr({float})" function
11281 */
11282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011283f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011284{
11285 float_T f;
11286
11287 if (get_float_arg(argvars, &f) == OK)
11288 {
11289 if (f < -0x7fffffff)
11290 rettv->vval.v_number = -0x7fffffff;
11291 else if (f > 0x7fffffff)
11292 rettv->vval.v_number = 0x7fffffff;
11293 else
11294 rettv->vval.v_number = (varnumber_T)f;
11295 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011296}
11297
11298/*
11299 * "floor({float})" function
11300 */
11301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011302f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011303{
11304 float_T f;
11305
11306 rettv->v_type = VAR_FLOAT;
11307 if (get_float_arg(argvars, &f) == OK)
11308 rettv->vval.v_float = floor(f);
11309 else
11310 rettv->vval.v_float = 0.0;
11311}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011312
11313/*
11314 * "fmod()" function
11315 */
11316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011317f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011318{
11319 float_T fx, fy;
11320
11321 rettv->v_type = VAR_FLOAT;
11322 if (get_float_arg(argvars, &fx) == OK
11323 && get_float_arg(&argvars[1], &fy) == OK)
11324 rettv->vval.v_float = fmod(fx, fy);
11325 else
11326 rettv->vval.v_float = 0.0;
11327}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011328#endif
11329
Bram Moolenaar0d660222005-01-07 21:51:51 +000011330/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011331 * "fnameescape({string})" function
11332 */
11333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011334f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011335{
11336 rettv->vval.v_string = vim_strsave_fnameescape(
11337 get_tv_string(&argvars[0]), FALSE);
11338 rettv->v_type = VAR_STRING;
11339}
11340
11341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 * "fnamemodify({fname}, {mods})" function
11343 */
11344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011345f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346{
11347 char_u *fname;
11348 char_u *mods;
11349 int usedlen = 0;
11350 int len;
11351 char_u *fbuf = NULL;
11352 char_u buf[NUMBUFLEN];
11353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011354 fname = get_tv_string_chk(&argvars[0]);
11355 mods = get_tv_string_buf_chk(&argvars[1], buf);
11356 if (fname == NULL || mods == NULL)
11357 fname = NULL;
11358 else
11359 {
11360 len = (int)STRLEN(fname);
11361 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011366 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011368 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 vim_free(fbuf);
11370}
11371
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011372static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373
11374/*
11375 * "foldclosed()" function
11376 */
11377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011378foldclosed_both(
11379 typval_T *argvars UNUSED,
11380 typval_T *rettv,
11381 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382{
11383#ifdef FEAT_FOLDING
11384 linenr_T lnum;
11385 linenr_T first, last;
11386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011387 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11389 {
11390 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11391 {
11392 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011393 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396 return;
11397 }
11398 }
11399#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401}
11402
11403/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011404 * "foldclosed()" function
11405 */
11406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011407f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011408{
11409 foldclosed_both(argvars, rettv, FALSE);
11410}
11411
11412/*
11413 * "foldclosedend()" function
11414 */
11415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011416f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011417{
11418 foldclosed_both(argvars, rettv, TRUE);
11419}
11420
11421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 * "foldlevel()" function
11423 */
11424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011425f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426{
11427#ifdef FEAT_FOLDING
11428 linenr_T lnum;
11429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011430 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011432 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434}
11435
11436/*
11437 * "foldtext()" function
11438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011440f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441{
11442#ifdef FEAT_FOLDING
11443 linenr_T lnum;
11444 char_u *s;
11445 char_u *r;
11446 int len;
11447 char *txt;
11448#endif
11449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011450 rettv->v_type = VAR_STRING;
11451 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011453 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11454 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11455 <= curbuf->b_ml.ml_line_count
11456 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 {
11458 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011459 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11460 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 {
11462 if (!linewhite(lnum))
11463 break;
11464 ++lnum;
11465 }
11466
11467 /* Find interesting text in this line. */
11468 s = skipwhite(ml_get(lnum));
11469 /* skip C comment-start */
11470 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011473 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011474 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011475 {
11476 s = skipwhite(ml_get(lnum + 1));
11477 if (*s == '*')
11478 s = skipwhite(s + 1);
11479 }
11480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481 txt = _("+-%s%3ld lines: ");
11482 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011483 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 + 20 /* for %3ld */
11485 + STRLEN(s))); /* concatenated */
11486 if (r != NULL)
11487 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011488 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11489 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11490 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491 len = (int)STRLEN(r);
11492 STRCAT(r, s);
11493 /* remove 'foldmarker' and 'commentstring' */
11494 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496 }
11497 }
11498#endif
11499}
11500
11501/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011502 * "foldtextresult(lnum)" function
11503 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011505f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011506{
11507#ifdef FEAT_FOLDING
11508 linenr_T lnum;
11509 char_u *text;
11510 char_u buf[51];
11511 foldinfo_T foldinfo;
11512 int fold_count;
11513#endif
11514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011515 rettv->v_type = VAR_STRING;
11516 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011517#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011519 /* treat illegal types and illegal string values for {lnum} the same */
11520 if (lnum < 0)
11521 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011522 fold_count = foldedCount(curwin, lnum, &foldinfo);
11523 if (fold_count > 0)
11524 {
11525 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11526 &foldinfo, buf);
11527 if (text == buf)
11528 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011530 }
11531#endif
11532}
11533
11534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535 * "foreground()" function
11536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011538f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540#ifdef FEAT_GUI
11541 if (gui.in_use)
11542 gui_mch_set_foreground();
11543#else
11544# ifdef WIN32
11545 win32_set_foreground();
11546# endif
11547#endif
11548}
11549
11550/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011551 * "function()" function
11552 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011554f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011555{
11556 char_u *s;
11557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011559 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011560 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011561 /* Don't check an autoload name for existence here. */
11562 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011563 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011564 else
11565 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011566 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011567 {
11568 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011569 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011570
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011571 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11572 * also be called from another script. Using trans_function_name()
11573 * would also work, but some plugins depend on the name being
11574 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011575 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011576 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011577 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011578 if (rettv->vval.v_string != NULL)
11579 {
11580 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011581 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011582 }
11583 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011584 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011585 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011586 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011587 }
11588}
11589
11590/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011591 * "garbagecollect()" function
11592 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011594f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011595{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011596 /* This is postponed until we are back at the toplevel, because we may be
11597 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11598 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011599
11600 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11601 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011602}
11603
11604/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011605 * "get()" function
11606 */
11607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011608f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011609{
Bram Moolenaar33570922005-01-25 22:26:29 +000011610 listitem_T *li;
11611 list_T *l;
11612 dictitem_T *di;
11613 dict_T *d;
11614 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011615
Bram Moolenaare9a41262005-01-15 22:18:47 +000011616 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011617 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011618 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011620 int error = FALSE;
11621
11622 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11623 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011624 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011625 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011626 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011627 else if (argvars[0].v_type == VAR_DICT)
11628 {
11629 if ((d = argvars[0].vval.v_dict) != NULL)
11630 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011631 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011632 if (di != NULL)
11633 tv = &di->di_tv;
11634 }
11635 }
11636 else
11637 EMSG2(_(e_listdictarg), "get()");
11638
11639 if (tv == NULL)
11640 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011641 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011642 copy_tv(&argvars[2], rettv);
11643 }
11644 else
11645 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011646}
11647
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011648static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011649
11650/*
11651 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011652 * Return a range (from start to end) of lines in rettv from the specified
11653 * buffer.
11654 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011655 */
11656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011657get_buffer_lines(
11658 buf_T *buf,
11659 linenr_T start,
11660 linenr_T end,
11661 int retlist,
11662 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011663{
11664 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011665
Bram Moolenaar959a1432013-12-14 12:17:38 +010011666 rettv->v_type = VAR_STRING;
11667 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011668 if (retlist && rettv_list_alloc(rettv) == FAIL)
11669 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011670
11671 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11672 return;
11673
11674 if (!retlist)
11675 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011676 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11677 p = ml_get_buf(buf, start, FALSE);
11678 else
11679 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011680 rettv->vval.v_string = vim_strsave(p);
11681 }
11682 else
11683 {
11684 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011685 return;
11686
11687 if (start < 1)
11688 start = 1;
11689 if (end > buf->b_ml.ml_line_count)
11690 end = buf->b_ml.ml_line_count;
11691 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011692 if (list_append_string(rettv->vval.v_list,
11693 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011694 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011695 }
11696}
11697
11698/*
11699 * "getbufline()" function
11700 */
11701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011702f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011703{
11704 linenr_T lnum;
11705 linenr_T end;
11706 buf_T *buf;
11707
11708 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11709 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011710 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011711 --emsg_off;
11712
Bram Moolenaar661b1822005-07-28 22:36:45 +000011713 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011714 if (argvars[2].v_type == VAR_UNKNOWN)
11715 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011716 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011717 end = get_tv_lnum_buf(&argvars[2], buf);
11718
Bram Moolenaar342337a2005-07-21 21:11:17 +000011719 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011720}
11721
Bram Moolenaar0d660222005-01-07 21:51:51 +000011722/*
11723 * "getbufvar()" function
11724 */
11725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011726f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011727{
11728 buf_T *buf;
11729 buf_T *save_curbuf;
11730 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011731 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011732 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011734 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11735 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011736 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011737 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011738
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011739 rettv->v_type = VAR_STRING;
11740 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011741
11742 if (buf != NULL && varname != NULL)
11743 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011744 /* set curbuf to be our buf, temporarily */
11745 save_curbuf = curbuf;
11746 curbuf = buf;
11747
Bram Moolenaar0d660222005-01-07 21:51:51 +000011748 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011749 {
11750 if (get_option_tv(&varname, rettv, TRUE) == OK)
11751 done = TRUE;
11752 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011753 else if (STRCMP(varname, "changedtick") == 0)
11754 {
11755 rettv->v_type = VAR_NUMBER;
11756 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011757 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011758 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011759 else
11760 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011761 /* Look up the variable. */
11762 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11763 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11764 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011765 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011766 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011767 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011768 done = TRUE;
11769 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011770 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011771
11772 /* restore previous notion of curbuf */
11773 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011774 }
11775
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011776 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11777 /* use the default value */
11778 copy_tv(&argvars[2], rettv);
11779
Bram Moolenaar0d660222005-01-07 21:51:51 +000011780 --emsg_off;
11781}
11782
11783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 * "getchar()" function
11785 */
11786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011787f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788{
11789 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011790 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011792 /* Position the cursor. Needed after a message that ends in a space. */
11793 windgoto(msg_row, msg_col);
11794
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 ++no_mapping;
11796 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011797 for (;;)
11798 {
11799 if (argvars[0].v_type == VAR_UNKNOWN)
11800 /* getchar(): blocking wait. */
11801 n = safe_vgetc();
11802 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11803 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011804 n = vpeekc_any();
11805 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011806 /* illegal argument or getchar(0) and no char avail: return zero */
11807 n = 0;
11808 else
11809 /* getchar(0) and char avail: return char */
11810 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011811
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011812 if (n == K_IGNORE)
11813 continue;
11814 break;
11815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816 --no_mapping;
11817 --allow_keys;
11818
Bram Moolenaar219b8702006-11-01 14:32:36 +000011819 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11820 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11821 vimvars[VV_MOUSE_COL].vv_nr = 0;
11822
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011823 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 if (IS_SPECIAL(n) || mod_mask != 0)
11825 {
11826 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11827 int i = 0;
11828
11829 /* Turn a special key into three bytes, plus modifier. */
11830 if (mod_mask != 0)
11831 {
11832 temp[i++] = K_SPECIAL;
11833 temp[i++] = KS_MODIFIER;
11834 temp[i++] = mod_mask;
11835 }
11836 if (IS_SPECIAL(n))
11837 {
11838 temp[i++] = K_SPECIAL;
11839 temp[i++] = K_SECOND(n);
11840 temp[i++] = K_THIRD(n);
11841 }
11842#ifdef FEAT_MBYTE
11843 else if (has_mbyte)
11844 i += (*mb_char2bytes)(n, temp + i);
11845#endif
11846 else
11847 temp[i++] = n;
11848 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011849 rettv->v_type = VAR_STRING;
11850 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011851
11852#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011853 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011854 {
11855 int row = mouse_row;
11856 int col = mouse_col;
11857 win_T *win;
11858 linenr_T lnum;
11859# ifdef FEAT_WINDOWS
11860 win_T *wp;
11861# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011862 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011863
11864 if (row >= 0 && col >= 0)
11865 {
11866 /* Find the window at the mouse coordinates and compute the
11867 * text position. */
11868 win = mouse_find_win(&row, &col);
11869 (void)mouse_comp_pos(win, &row, &col, &lnum);
11870# ifdef FEAT_WINDOWS
11871 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011872 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011873# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011874 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011875 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11876 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11877 }
11878 }
11879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880 }
11881}
11882
11883/*
11884 * "getcharmod()" function
11885 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011887f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011889 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890}
11891
11892/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011893 * "getcharsearch()" function
11894 */
11895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011896f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011897{
11898 if (rettv_dict_alloc(rettv) != FAIL)
11899 {
11900 dict_T *dict = rettv->vval.v_dict;
11901
11902 dict_add_nr_str(dict, "char", 0L, last_csearch());
11903 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11904 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11905 }
11906}
11907
11908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909 * "getcmdline()" function
11910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011912f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011914 rettv->v_type = VAR_STRING;
11915 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916}
11917
11918/*
11919 * "getcmdpos()" function
11920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011922f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011924 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925}
11926
11927/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011928 * "getcmdtype()" function
11929 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011931f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011932{
11933 rettv->v_type = VAR_STRING;
11934 rettv->vval.v_string = alloc(2);
11935 if (rettv->vval.v_string != NULL)
11936 {
11937 rettv->vval.v_string[0] = get_cmdline_type();
11938 rettv->vval.v_string[1] = NUL;
11939 }
11940}
11941
11942/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011943 * "getcmdwintype()" function
11944 */
11945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011946f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011947{
11948 rettv->v_type = VAR_STRING;
11949 rettv->vval.v_string = NULL;
11950#ifdef FEAT_CMDWIN
11951 rettv->vval.v_string = alloc(2);
11952 if (rettv->vval.v_string != NULL)
11953 {
11954 rettv->vval.v_string[0] = cmdwin_type;
11955 rettv->vval.v_string[1] = NUL;
11956 }
11957#endif
11958}
11959
11960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 * "getcwd()" function
11962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011964f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965{
Bram Moolenaarc9703302016-01-17 21:49:33 +010011966 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011967 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011969 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011970 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010011971
11972 wp = find_tabwin(&argvars[0], &argvars[1]);
11973 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011975 if (wp->w_localdir != NULL)
11976 rettv->vval.v_string = vim_strsave(wp->w_localdir);
11977 else if(globaldir != NULL)
11978 rettv->vval.v_string = vim_strsave(globaldir);
11979 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020011980 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010011981 cwd = alloc(MAXPATHL);
11982 if (cwd != NULL)
11983 {
11984 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11985 rettv->vval.v_string = vim_strsave(cwd);
11986 vim_free(cwd);
11987 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020011988 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010011989#ifdef BACKSLASH_IN_FILENAME
11990 if (rettv->vval.v_string != NULL)
11991 slash_adjust(rettv->vval.v_string);
11992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 }
11994}
11995
11996/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011997 * "getfontname()" function
11998 */
11999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012000f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012001{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012002 rettv->v_type = VAR_STRING;
12003 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012004#ifdef FEAT_GUI
12005 if (gui.in_use)
12006 {
12007 GuiFont font;
12008 char_u *name = NULL;
12009
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012010 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012011 {
12012 /* Get the "Normal" font. Either the name saved by
12013 * hl_set_font_name() or from the font ID. */
12014 font = gui.norm_font;
12015 name = hl_get_font_name();
12016 }
12017 else
12018 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012020 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12021 return;
12022 font = gui_mch_get_font(name, FALSE);
12023 if (font == NOFONT)
12024 return; /* Invalid font name, return empty string. */
12025 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012026 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012027 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012028 gui_mch_free_font(font);
12029 }
12030#endif
12031}
12032
12033/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012034 * "getfperm({fname})" function
12035 */
12036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012037f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012038{
12039 char_u *fname;
12040 struct stat st;
12041 char_u *perm = NULL;
12042 char_u flags[] = "rwx";
12043 int i;
12044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012045 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012047 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012048 if (mch_stat((char *)fname, &st) >= 0)
12049 {
12050 perm = vim_strsave((char_u *)"---------");
12051 if (perm != NULL)
12052 {
12053 for (i = 0; i < 9; i++)
12054 {
12055 if (st.st_mode & (1 << (8 - i)))
12056 perm[i] = flags[i % 3];
12057 }
12058 }
12059 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012060 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012061}
12062
12063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 * "getfsize({fname})" function
12065 */
12066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012067f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068{
12069 char_u *fname;
12070 struct stat st;
12071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012072 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012074 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075
12076 if (mch_stat((char *)fname, &st) >= 0)
12077 {
12078 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012079 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012083
12084 /* non-perfect check for overflow */
12085 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12086 rettv->vval.v_number = -2;
12087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088 }
12089 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091}
12092
12093/*
12094 * "getftime({fname})" function
12095 */
12096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012097f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098{
12099 char_u *fname;
12100 struct stat st;
12101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012102 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103
12104 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108}
12109
12110/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012111 * "getftype({fname})" function
12112 */
12113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012114f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012115{
12116 char_u *fname;
12117 struct stat st;
12118 char_u *type = NULL;
12119 char *t;
12120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012124 if (mch_lstat((char *)fname, &st) >= 0)
12125 {
12126#ifdef S_ISREG
12127 if (S_ISREG(st.st_mode))
12128 t = "file";
12129 else if (S_ISDIR(st.st_mode))
12130 t = "dir";
12131# ifdef S_ISLNK
12132 else if (S_ISLNK(st.st_mode))
12133 t = "link";
12134# endif
12135# ifdef S_ISBLK
12136 else if (S_ISBLK(st.st_mode))
12137 t = "bdev";
12138# endif
12139# ifdef S_ISCHR
12140 else if (S_ISCHR(st.st_mode))
12141 t = "cdev";
12142# endif
12143# ifdef S_ISFIFO
12144 else if (S_ISFIFO(st.st_mode))
12145 t = "fifo";
12146# endif
12147# ifdef S_ISSOCK
12148 else if (S_ISSOCK(st.st_mode))
12149 t = "fifo";
12150# endif
12151 else
12152 t = "other";
12153#else
12154# ifdef S_IFMT
12155 switch (st.st_mode & S_IFMT)
12156 {
12157 case S_IFREG: t = "file"; break;
12158 case S_IFDIR: t = "dir"; break;
12159# ifdef S_IFLNK
12160 case S_IFLNK: t = "link"; break;
12161# endif
12162# ifdef S_IFBLK
12163 case S_IFBLK: t = "bdev"; break;
12164# endif
12165# ifdef S_IFCHR
12166 case S_IFCHR: t = "cdev"; break;
12167# endif
12168# ifdef S_IFIFO
12169 case S_IFIFO: t = "fifo"; break;
12170# endif
12171# ifdef S_IFSOCK
12172 case S_IFSOCK: t = "socket"; break;
12173# endif
12174 default: t = "other";
12175 }
12176# else
12177 if (mch_isdir(fname))
12178 t = "dir";
12179 else
12180 t = "file";
12181# endif
12182#endif
12183 type = vim_strsave((char_u *)t);
12184 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012185 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012186}
12187
12188/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012189 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012190 */
12191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012192f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012193{
12194 linenr_T lnum;
12195 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012196 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012197
12198 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012199 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012200 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012201 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012202 retlist = FALSE;
12203 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012204 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012205 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012206 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012207 retlist = TRUE;
12208 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012209
Bram Moolenaar342337a2005-07-21 21:11:17 +000012210 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012211}
12212
12213/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012214 * "getmatches()" function
12215 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012217f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012218{
12219#ifdef FEAT_SEARCH_EXTRA
12220 dict_T *dict;
12221 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012222 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012223
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012224 if (rettv_list_alloc(rettv) == OK)
12225 {
12226 while (cur != NULL)
12227 {
12228 dict = dict_alloc();
12229 if (dict == NULL)
12230 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012231 if (cur->match.regprog == NULL)
12232 {
12233 /* match added with matchaddpos() */
12234 for (i = 0; i < MAXPOSMATCH; ++i)
12235 {
12236 llpos_T *llpos;
12237 char buf[6];
12238 list_T *l;
12239
12240 llpos = &cur->pos.pos[i];
12241 if (llpos->lnum == 0)
12242 break;
12243 l = list_alloc();
12244 if (l == NULL)
12245 break;
12246 list_append_number(l, (varnumber_T)llpos->lnum);
12247 if (llpos->col > 0)
12248 {
12249 list_append_number(l, (varnumber_T)llpos->col);
12250 list_append_number(l, (varnumber_T)llpos->len);
12251 }
12252 sprintf(buf, "pos%d", i + 1);
12253 dict_add_list(dict, buf, l);
12254 }
12255 }
12256 else
12257 {
12258 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12259 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012260 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012261 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12262 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012263# ifdef FEAT_CONCEAL
12264 if (cur->conceal_char)
12265 {
12266 char_u buf[MB_MAXBYTES + 1];
12267
12268 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12269 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12270 }
12271# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012272 list_append_dict(rettv->vval.v_list, dict);
12273 cur = cur->next;
12274 }
12275 }
12276#endif
12277}
12278
12279/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012280 * "getpid()" function
12281 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012283f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012284{
12285 rettv->vval.v_number = mch_get_pid();
12286}
12287
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012288static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012289
12290/*
12291 * "getcurpos()" function
12292 */
12293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012294f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012295{
12296 getpos_both(argvars, rettv, TRUE);
12297}
12298
Bram Moolenaar18081e32008-02-20 19:11:07 +000012299/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012300 * "getpos(string)" function
12301 */
12302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012303f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012304{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012305 getpos_both(argvars, rettv, FALSE);
12306}
12307
12308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012309getpos_both(
12310 typval_T *argvars,
12311 typval_T *rettv,
12312 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012313{
Bram Moolenaara5525202006-03-02 22:52:09 +000012314 pos_T *fp;
12315 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012316 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012317
12318 if (rettv_list_alloc(rettv) == OK)
12319 {
12320 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012321 if (getcurpos)
12322 fp = &curwin->w_cursor;
12323 else
12324 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012325 if (fnum != -1)
12326 list_append_number(l, (varnumber_T)fnum);
12327 else
12328 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012329 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12330 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012331 list_append_number(l, (fp != NULL)
12332 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012333 : (varnumber_T)0);
12334 list_append_number(l,
12335#ifdef FEAT_VIRTUALEDIT
12336 (fp != NULL) ? (varnumber_T)fp->coladd :
12337#endif
12338 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012339 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012340 list_append_number(l, curwin->w_curswant == MAXCOL ?
12341 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012342 }
12343 else
12344 rettv->vval.v_number = FALSE;
12345}
12346
12347/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012348 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012349 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012351f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012352{
12353#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012354 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012355#endif
12356
Bram Moolenaar2641f772005-03-25 21:58:17 +000012357#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012358 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012359 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012360 wp = NULL;
12361 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12362 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012363 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012364 if (wp == NULL)
12365 return;
12366 }
12367
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012368 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012369 }
12370#endif
12371}
12372
12373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 * "getreg()" function
12375 */
12376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012377f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378{
12379 char_u *strregname;
12380 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012381 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012382 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012383 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012385 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012386 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012387 strregname = get_tv_string_chk(&argvars[0]);
12388 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012389 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012391 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012392 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12393 return_list = get_tv_number_chk(&argvars[2], &error);
12394 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012397 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012398
12399 if (error)
12400 return;
12401
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402 regname = (strregname == NULL ? '"' : *strregname);
12403 if (regname == 0)
12404 regname = '"';
12405
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012406 if (return_list)
12407 {
12408 rettv->v_type = VAR_LIST;
12409 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12410 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012411 if (rettv->vval.v_list != NULL)
12412 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012413 }
12414 else
12415 {
12416 rettv->v_type = VAR_STRING;
12417 rettv->vval.v_string = get_reg_contents(regname,
12418 arg2 ? GREG_EXPR_SRC : 0);
12419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420}
12421
12422/*
12423 * "getregtype()" function
12424 */
12425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012426f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427{
12428 char_u *strregname;
12429 int regname;
12430 char_u buf[NUMBUFLEN + 2];
12431 long reglen = 0;
12432
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012433 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012434 {
12435 strregname = get_tv_string_chk(&argvars[0]);
12436 if (strregname == NULL) /* type error; errmsg already given */
12437 {
12438 rettv->v_type = VAR_STRING;
12439 rettv->vval.v_string = NULL;
12440 return;
12441 }
12442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443 else
12444 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012445 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446
12447 regname = (strregname == NULL ? '"' : *strregname);
12448 if (regname == 0)
12449 regname = '"';
12450
12451 buf[0] = NUL;
12452 buf[1] = NUL;
12453 switch (get_reg_type(regname, &reglen))
12454 {
12455 case MLINE: buf[0] = 'V'; break;
12456 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457 case MBLOCK:
12458 buf[0] = Ctrl_V;
12459 sprintf((char *)buf + 1, "%ld", reglen + 1);
12460 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012462 rettv->v_type = VAR_STRING;
12463 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464}
12465
12466/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012467 * "gettabvar()" function
12468 */
12469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012470f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012471{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012472 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012473 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012474 dictitem_T *v;
12475 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012476 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012477
12478 rettv->v_type = VAR_STRING;
12479 rettv->vval.v_string = NULL;
12480
12481 varname = get_tv_string_chk(&argvars[1]);
12482 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12483 if (tp != NULL && varname != NULL)
12484 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012485 /* Set tp to be our tabpage, temporarily. Also set the window to the
12486 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012487 if (switch_win(&oldcurwin, &oldtabpage,
12488 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012489 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012490 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012491 /* look up the variable */
12492 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12493 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12494 if (v != NULL)
12495 {
12496 copy_tv(&v->di_tv, rettv);
12497 done = TRUE;
12498 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012499 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012500
12501 /* restore previous notion of curwin */
12502 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012503 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012504
12505 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012506 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012507}
12508
12509/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012510 * "gettabwinvar()" function
12511 */
12512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012513f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012514{
12515 getwinvar(argvars, rettv, 1);
12516}
12517
12518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 * "getwinposx()" function
12520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012522f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012524 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012525#ifdef FEAT_GUI
12526 if (gui.in_use)
12527 {
12528 int x, y;
12529
12530 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 }
12533#endif
12534}
12535
12536/*
12537 * "getwinposy()" function
12538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012540f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012542 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543#ifdef FEAT_GUI
12544 if (gui.in_use)
12545 {
12546 int x, y;
12547
12548 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012549 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550 }
12551#endif
12552}
12553
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012554/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012555 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012556 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012557 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012558find_win_by_nr(
12559 typval_T *vp,
12560 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012561{
12562#ifdef FEAT_WINDOWS
12563 win_T *wp;
12564#endif
12565 int nr;
12566
12567 nr = get_tv_number_chk(vp, NULL);
12568
12569#ifdef FEAT_WINDOWS
12570 if (nr < 0)
12571 return NULL;
12572 if (nr == 0)
12573 return curwin;
12574
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012575 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12576 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012577 if (--nr <= 0)
12578 break;
12579 return wp;
12580#else
12581 if (nr == 0 || nr == 1)
12582 return curwin;
12583 return NULL;
12584#endif
12585}
12586
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012588 * Find window specified by "wvp" in tabpage "tvp".
12589 */
12590 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012591find_tabwin(
12592 typval_T *wvp, /* VAR_UNKNOWN for current window */
12593 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012594{
12595 win_T *wp = NULL;
12596 tabpage_T *tp = NULL;
12597 long n;
12598
12599 if (wvp->v_type != VAR_UNKNOWN)
12600 {
12601 if (tvp->v_type != VAR_UNKNOWN)
12602 {
12603 n = get_tv_number(tvp);
12604 if (n >= 0)
12605 tp = find_tabpage(n);
12606 }
12607 else
12608 tp = curtab;
12609
12610 if (tp != NULL)
12611 wp = find_win_by_nr(wvp, tp);
12612 }
12613 else
12614 wp = curwin;
12615
12616 return wp;
12617}
12618
12619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620 * "getwinvar()" function
12621 */
12622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012623f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012625 getwinvar(argvars, rettv, 0);
12626}
12627
12628/*
12629 * getwinvar() and gettabwinvar()
12630 */
12631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012632getwinvar(
12633 typval_T *argvars,
12634 typval_T *rettv,
12635 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012636{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012637 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012640 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012641 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012642#ifdef FEAT_WINDOWS
12643 win_T *oldcurwin;
12644 tabpage_T *oldtabpage;
12645 int need_switch_win;
12646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012648#ifdef FEAT_WINDOWS
12649 if (off == 1)
12650 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12651 else
12652 tp = curtab;
12653#endif
12654 win = find_win_by_nr(&argvars[off], tp);
12655 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012656 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012658 rettv->v_type = VAR_STRING;
12659 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660
12661 if (win != NULL && varname != NULL)
12662 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012663#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012664 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012665 * otherwise the window is not valid. Only do this when needed,
12666 * autocommands get blocked. */
12667 need_switch_win = !(tp == curtab && win == curwin);
12668 if (!need_switch_win
12669 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12670#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012671 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012672 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012673 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012674 if (get_option_tv(&varname, rettv, 1) == OK)
12675 done = TRUE;
12676 }
12677 else
12678 {
12679 /* Look up the variable. */
12680 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12681 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12682 varname, FALSE);
12683 if (v != NULL)
12684 {
12685 copy_tv(&v->di_tv, rettv);
12686 done = TRUE;
12687 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012690
Bram Moolenaarba117c22015-09-29 16:53:22 +020012691#ifdef FEAT_WINDOWS
12692 if (need_switch_win)
12693 /* restore previous notion of curwin */
12694 restore_win(oldcurwin, oldtabpage, TRUE);
12695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 }
12697
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012698 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12699 /* use the default return value */
12700 copy_tv(&argvars[off + 2], rettv);
12701
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 --emsg_off;
12703}
12704
12705/*
12706 * "glob()" function
12707 */
12708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012709f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012711 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012713 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012715 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012716 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012718 if (argvars[1].v_type != VAR_UNKNOWN)
12719 {
12720 if (get_tv_number_chk(&argvars[1], &error))
12721 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012722 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012723 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012724 if (get_tv_number_chk(&argvars[2], &error))
12725 {
12726 rettv->v_type = VAR_LIST;
12727 rettv->vval.v_list = NULL;
12728 }
12729 if (argvars[3].v_type != VAR_UNKNOWN
12730 && get_tv_number_chk(&argvars[3], &error))
12731 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012732 }
12733 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012734 if (!error)
12735 {
12736 ExpandInit(&xpc);
12737 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012738 if (p_wic)
12739 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012740 if (rettv->v_type == VAR_STRING)
12741 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012742 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012743 else if (rettv_list_alloc(rettv) != FAIL)
12744 {
12745 int i;
12746
12747 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12748 NULL, options, WILD_ALL_KEEP);
12749 for (i = 0; i < xpc.xp_numfiles; i++)
12750 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12751
12752 ExpandCleanup(&xpc);
12753 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012754 }
12755 else
12756 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757}
12758
12759/*
12760 * "globpath()" function
12761 */
12762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012763f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012765 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012767 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012768 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012769 garray_T ga;
12770 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012772 /* When the optional second argument is non-zero, don't remove matches
12773 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012775 if (argvars[2].v_type != VAR_UNKNOWN)
12776 {
12777 if (get_tv_number_chk(&argvars[2], &error))
12778 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012779 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012780 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012781 if (get_tv_number_chk(&argvars[3], &error))
12782 {
12783 rettv->v_type = VAR_LIST;
12784 rettv->vval.v_list = NULL;
12785 }
12786 if (argvars[4].v_type != VAR_UNKNOWN
12787 && get_tv_number_chk(&argvars[4], &error))
12788 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012789 }
12790 }
12791 if (file != NULL && !error)
12792 {
12793 ga_init2(&ga, (int)sizeof(char_u *), 10);
12794 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12795 if (rettv->v_type == VAR_STRING)
12796 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12797 else if (rettv_list_alloc(rettv) != FAIL)
12798 for (i = 0; i < ga.ga_len; ++i)
12799 list_append_string(rettv->vval.v_list,
12800 ((char_u **)(ga.ga_data))[i], -1);
12801 ga_clear_strings(&ga);
12802 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012803 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012804 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805}
12806
12807/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012808 * "glob2regpat()" function
12809 */
12810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012811f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012812{
12813 char_u *pat = get_tv_string_chk(&argvars[0]);
12814
12815 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012816 rettv->vval.v_string = (pat == NULL)
12817 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012818}
12819
12820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821 * "has()" function
12822 */
12823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012824f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825{
12826 int i;
12827 char_u *name;
12828 int n = FALSE;
12829 static char *(has_list[]) =
12830 {
12831#ifdef AMIGA
12832 "amiga",
12833# ifdef FEAT_ARP
12834 "arp",
12835# endif
12836#endif
12837#ifdef __BEOS__
12838 "beos",
12839#endif
12840#ifdef MSDOS
12841# ifdef DJGPP
12842 "dos32",
12843# else
12844 "dos16",
12845# endif
12846#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012847#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 "mac",
12849#endif
12850#if defined(MACOS_X_UNIX)
12851 "macunix",
12852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853#ifdef __QNX__
12854 "qnx",
12855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856#ifdef UNIX
12857 "unix",
12858#endif
12859#ifdef VMS
12860 "vms",
12861#endif
12862#ifdef WIN16
12863 "win16",
12864#endif
12865#ifdef WIN32
12866 "win32",
12867#endif
12868#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12869 "win32unix",
12870#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012871#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 "win64",
12873#endif
12874#ifdef EBCDIC
12875 "ebcdic",
12876#endif
12877#ifndef CASE_INSENSITIVE_FILENAME
12878 "fname_case",
12879#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012880#ifdef HAVE_ACL
12881 "acl",
12882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883#ifdef FEAT_ARABIC
12884 "arabic",
12885#endif
12886#ifdef FEAT_AUTOCMD
12887 "autocmd",
12888#endif
12889#ifdef FEAT_BEVAL
12890 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012891# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12892 "balloon_multiline",
12893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894#endif
12895#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12896 "builtin_terms",
12897# ifdef ALL_BUILTIN_TCAPS
12898 "all_builtin_terms",
12899# endif
12900#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012901#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12902 || defined(FEAT_GUI_W32) \
12903 || defined(FEAT_GUI_MOTIF))
12904 "browsefilter",
12905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906#ifdef FEAT_BYTEOFF
12907 "byte_offset",
12908#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010012909#ifdef FEAT_CHANNEL
12910 "channel",
12911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912#ifdef FEAT_CINDENT
12913 "cindent",
12914#endif
12915#ifdef FEAT_CLIENTSERVER
12916 "clientserver",
12917#endif
12918#ifdef FEAT_CLIPBOARD
12919 "clipboard",
12920#endif
12921#ifdef FEAT_CMDL_COMPL
12922 "cmdline_compl",
12923#endif
12924#ifdef FEAT_CMDHIST
12925 "cmdline_hist",
12926#endif
12927#ifdef FEAT_COMMENTS
12928 "comments",
12929#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012930#ifdef FEAT_CONCEAL
12931 "conceal",
12932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933#ifdef FEAT_CRYPT
12934 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010012935 "crypt-blowfish",
12936 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937#endif
12938#ifdef FEAT_CSCOPE
12939 "cscope",
12940#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012941#ifdef FEAT_CURSORBIND
12942 "cursorbind",
12943#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012944#ifdef CURSOR_SHAPE
12945 "cursorshape",
12946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947#ifdef DEBUG
12948 "debug",
12949#endif
12950#ifdef FEAT_CON_DIALOG
12951 "dialog_con",
12952#endif
12953#ifdef FEAT_GUI_DIALOG
12954 "dialog_gui",
12955#endif
12956#ifdef FEAT_DIFF
12957 "diff",
12958#endif
12959#ifdef FEAT_DIGRAPHS
12960 "digraphs",
12961#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012962#ifdef FEAT_DIRECTX
12963 "directx",
12964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965#ifdef FEAT_DND
12966 "dnd",
12967#endif
12968#ifdef FEAT_EMACS_TAGS
12969 "emacs_tags",
12970#endif
12971 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010012972 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973#ifdef FEAT_SEARCH_EXTRA
12974 "extra_search",
12975#endif
12976#ifdef FEAT_FKMAP
12977 "farsi",
12978#endif
12979#ifdef FEAT_SEARCHPATH
12980 "file_in_path",
12981#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012982#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012983 "filterpipe",
12984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985#ifdef FEAT_FIND_ID
12986 "find_in_path",
12987#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012988#ifdef FEAT_FLOAT
12989 "float",
12990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991#ifdef FEAT_FOLDING
12992 "folding",
12993#endif
12994#ifdef FEAT_FOOTER
12995 "footer",
12996#endif
12997#if !defined(USE_SYSTEM) && defined(UNIX)
12998 "fork",
12999#endif
13000#ifdef FEAT_GETTEXT
13001 "gettext",
13002#endif
13003#ifdef FEAT_GUI
13004 "gui",
13005#endif
13006#ifdef FEAT_GUI_ATHENA
13007# ifdef FEAT_GUI_NEXTAW
13008 "gui_neXtaw",
13009# else
13010 "gui_athena",
13011# endif
13012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013#ifdef FEAT_GUI_GTK
13014 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013017#ifdef FEAT_GUI_GNOME
13018 "gui_gnome",
13019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020#ifdef FEAT_GUI_MAC
13021 "gui_mac",
13022#endif
13023#ifdef FEAT_GUI_MOTIF
13024 "gui_motif",
13025#endif
13026#ifdef FEAT_GUI_PHOTON
13027 "gui_photon",
13028#endif
13029#ifdef FEAT_GUI_W16
13030 "gui_win16",
13031#endif
13032#ifdef FEAT_GUI_W32
13033 "gui_win32",
13034#endif
13035#ifdef FEAT_HANGULIN
13036 "hangul_input",
13037#endif
13038#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13039 "iconv",
13040#endif
13041#ifdef FEAT_INS_EXPAND
13042 "insert_expand",
13043#endif
13044#ifdef FEAT_JUMPLIST
13045 "jumplist",
13046#endif
13047#ifdef FEAT_KEYMAP
13048 "keymap",
13049#endif
13050#ifdef FEAT_LANGMAP
13051 "langmap",
13052#endif
13053#ifdef FEAT_LIBCALL
13054 "libcall",
13055#endif
13056#ifdef FEAT_LINEBREAK
13057 "linebreak",
13058#endif
13059#ifdef FEAT_LISP
13060 "lispindent",
13061#endif
13062#ifdef FEAT_LISTCMDS
13063 "listcmds",
13064#endif
13065#ifdef FEAT_LOCALMAP
13066 "localmap",
13067#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013068#ifdef FEAT_LUA
13069# ifndef DYNAMIC_LUA
13070 "lua",
13071# endif
13072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073#ifdef FEAT_MENU
13074 "menu",
13075#endif
13076#ifdef FEAT_SESSION
13077 "mksession",
13078#endif
13079#ifdef FEAT_MODIFY_FNAME
13080 "modify_fname",
13081#endif
13082#ifdef FEAT_MOUSE
13083 "mouse",
13084#endif
13085#ifdef FEAT_MOUSESHAPE
13086 "mouseshape",
13087#endif
13088#if defined(UNIX) || defined(VMS)
13089# ifdef FEAT_MOUSE_DEC
13090 "mouse_dec",
13091# endif
13092# ifdef FEAT_MOUSE_GPM
13093 "mouse_gpm",
13094# endif
13095# ifdef FEAT_MOUSE_JSB
13096 "mouse_jsbterm",
13097# endif
13098# ifdef FEAT_MOUSE_NET
13099 "mouse_netterm",
13100# endif
13101# ifdef FEAT_MOUSE_PTERM
13102 "mouse_pterm",
13103# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013104# ifdef FEAT_MOUSE_SGR
13105 "mouse_sgr",
13106# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013107# ifdef FEAT_SYSMOUSE
13108 "mouse_sysmouse",
13109# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013110# ifdef FEAT_MOUSE_URXVT
13111 "mouse_urxvt",
13112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113# ifdef FEAT_MOUSE_XTERM
13114 "mouse_xterm",
13115# endif
13116#endif
13117#ifdef FEAT_MBYTE
13118 "multi_byte",
13119#endif
13120#ifdef FEAT_MBYTE_IME
13121 "multi_byte_ime",
13122#endif
13123#ifdef FEAT_MULTI_LANG
13124 "multi_lang",
13125#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013126#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013127#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013128 "mzscheme",
13129#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131#ifdef FEAT_OLE
13132 "ole",
13133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134#ifdef FEAT_PATH_EXTRA
13135 "path_extra",
13136#endif
13137#ifdef FEAT_PERL
13138#ifndef DYNAMIC_PERL
13139 "perl",
13140#endif
13141#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013142#ifdef FEAT_PERSISTENT_UNDO
13143 "persistent_undo",
13144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145#ifdef FEAT_PYTHON
13146#ifndef DYNAMIC_PYTHON
13147 "python",
13148#endif
13149#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013150#ifdef FEAT_PYTHON3
13151#ifndef DYNAMIC_PYTHON3
13152 "python3",
13153#endif
13154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155#ifdef FEAT_POSTSCRIPT
13156 "postscript",
13157#endif
13158#ifdef FEAT_PRINTER
13159 "printer",
13160#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013161#ifdef FEAT_PROFILE
13162 "profile",
13163#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013164#ifdef FEAT_RELTIME
13165 "reltime",
13166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167#ifdef FEAT_QUICKFIX
13168 "quickfix",
13169#endif
13170#ifdef FEAT_RIGHTLEFT
13171 "rightleft",
13172#endif
13173#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13174 "ruby",
13175#endif
13176#ifdef FEAT_SCROLLBIND
13177 "scrollbind",
13178#endif
13179#ifdef FEAT_CMDL_INFO
13180 "showcmd",
13181 "cmdline_info",
13182#endif
13183#ifdef FEAT_SIGNS
13184 "signs",
13185#endif
13186#ifdef FEAT_SMARTINDENT
13187 "smartindent",
13188#endif
13189#ifdef FEAT_SNIFF
13190 "sniff",
13191#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013192#ifdef STARTUPTIME
13193 "startuptime",
13194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195#ifdef FEAT_STL_OPT
13196 "statusline",
13197#endif
13198#ifdef FEAT_SUN_WORKSHOP
13199 "sun_workshop",
13200#endif
13201#ifdef FEAT_NETBEANS_INTG
13202 "netbeans_intg",
13203#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013204#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013205 "spell",
13206#endif
13207#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 "syntax",
13209#endif
13210#if defined(USE_SYSTEM) || !defined(UNIX)
13211 "system",
13212#endif
13213#ifdef FEAT_TAG_BINS
13214 "tag_binary",
13215#endif
13216#ifdef FEAT_TAG_OLDSTATIC
13217 "tag_old_static",
13218#endif
13219#ifdef FEAT_TAG_ANYWHITE
13220 "tag_any_white",
13221#endif
13222#ifdef FEAT_TCL
13223# ifndef DYNAMIC_TCL
13224 "tcl",
13225# endif
13226#endif
13227#ifdef TERMINFO
13228 "terminfo",
13229#endif
13230#ifdef FEAT_TERMRESPONSE
13231 "termresponse",
13232#endif
13233#ifdef FEAT_TEXTOBJ
13234 "textobjects",
13235#endif
13236#ifdef HAVE_TGETENT
13237 "tgetent",
13238#endif
13239#ifdef FEAT_TITLE
13240 "title",
13241#endif
13242#ifdef FEAT_TOOLBAR
13243 "toolbar",
13244#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013245#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13246 "unnamedplus",
13247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248#ifdef FEAT_USR_CMDS
13249 "user-commands", /* was accidentally included in 5.4 */
13250 "user_commands",
13251#endif
13252#ifdef FEAT_VIMINFO
13253 "viminfo",
13254#endif
13255#ifdef FEAT_VERTSPLIT
13256 "vertsplit",
13257#endif
13258#ifdef FEAT_VIRTUALEDIT
13259 "virtualedit",
13260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262#ifdef FEAT_VISUALEXTRA
13263 "visualextra",
13264#endif
13265#ifdef FEAT_VREPLACE
13266 "vreplace",
13267#endif
13268#ifdef FEAT_WILDIGN
13269 "wildignore",
13270#endif
13271#ifdef FEAT_WILDMENU
13272 "wildmenu",
13273#endif
13274#ifdef FEAT_WINDOWS
13275 "windows",
13276#endif
13277#ifdef FEAT_WAK
13278 "winaltkeys",
13279#endif
13280#ifdef FEAT_WRITEBACKUP
13281 "writebackup",
13282#endif
13283#ifdef FEAT_XIM
13284 "xim",
13285#endif
13286#ifdef FEAT_XFONTSET
13287 "xfontset",
13288#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013289#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013290 "xpm",
13291 "xpm_w32", /* for backward compatibility */
13292#else
13293# if defined(HAVE_XPM)
13294 "xpm",
13295# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297#ifdef USE_XSMP
13298 "xsmp",
13299#endif
13300#ifdef USE_XSMP_INTERACT
13301 "xsmp_interact",
13302#endif
13303#ifdef FEAT_XCLIPBOARD
13304 "xterm_clipboard",
13305#endif
13306#ifdef FEAT_XTERM_SAVE
13307 "xterm_save",
13308#endif
13309#if defined(UNIX) && defined(FEAT_X11)
13310 "X11",
13311#endif
13312 NULL
13313 };
13314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013315 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 for (i = 0; has_list[i] != NULL; ++i)
13317 if (STRICMP(name, has_list[i]) == 0)
13318 {
13319 n = TRUE;
13320 break;
13321 }
13322
13323 if (n == FALSE)
13324 {
13325 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013326 {
13327 if (name[5] == '-'
13328 && STRLEN(name) > 11
13329 && vim_isdigit(name[6])
13330 && vim_isdigit(name[8])
13331 && vim_isdigit(name[10]))
13332 {
13333 int major = atoi((char *)name + 6);
13334 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013335
13336 /* Expect "patch-9.9.01234". */
13337 n = (major < VIM_VERSION_MAJOR
13338 || (major == VIM_VERSION_MAJOR
13339 && (minor < VIM_VERSION_MINOR
13340 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013341 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013342 }
13343 else
13344 n = has_patch(atoi((char *)name + 5));
13345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346 else if (STRICMP(name, "vim_starting") == 0)
13347 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013348#ifdef FEAT_MBYTE
13349 else if (STRICMP(name, "multi_byte_encoding") == 0)
13350 n = has_mbyte;
13351#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013352#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13353 else if (STRICMP(name, "balloon_multiline") == 0)
13354 n = multiline_balloon_available();
13355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356#ifdef DYNAMIC_TCL
13357 else if (STRICMP(name, "tcl") == 0)
13358 n = tcl_enabled(FALSE);
13359#endif
13360#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13361 else if (STRICMP(name, "iconv") == 0)
13362 n = iconv_enabled(FALSE);
13363#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013364#ifdef DYNAMIC_LUA
13365 else if (STRICMP(name, "lua") == 0)
13366 n = lua_enabled(FALSE);
13367#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013368#ifdef DYNAMIC_MZSCHEME
13369 else if (STRICMP(name, "mzscheme") == 0)
13370 n = mzscheme_enabled(FALSE);
13371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372#ifdef DYNAMIC_RUBY
13373 else if (STRICMP(name, "ruby") == 0)
13374 n = ruby_enabled(FALSE);
13375#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013376#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377#ifdef DYNAMIC_PYTHON
13378 else if (STRICMP(name, "python") == 0)
13379 n = python_enabled(FALSE);
13380#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013381#endif
13382#ifdef FEAT_PYTHON3
13383#ifdef DYNAMIC_PYTHON3
13384 else if (STRICMP(name, "python3") == 0)
13385 n = python3_enabled(FALSE);
13386#endif
13387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388#ifdef DYNAMIC_PERL
13389 else if (STRICMP(name, "perl") == 0)
13390 n = perl_enabled(FALSE);
13391#endif
13392#ifdef FEAT_GUI
13393 else if (STRICMP(name, "gui_running") == 0)
13394 n = (gui.in_use || gui.starting);
13395# ifdef FEAT_GUI_W32
13396 else if (STRICMP(name, "gui_win32s") == 0)
13397 n = gui_is_win32s();
13398# endif
13399# ifdef FEAT_BROWSE
13400 else if (STRICMP(name, "browse") == 0)
13401 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13402# endif
13403#endif
13404#ifdef FEAT_SYN_HL
13405 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013406 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407#endif
13408#if defined(WIN3264)
13409 else if (STRICMP(name, "win95") == 0)
13410 n = mch_windows95();
13411#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013412#ifdef FEAT_NETBEANS_INTG
13413 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013414 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 }
13417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419}
13420
13421/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013422 * "has_key()" function
13423 */
13424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013425f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013426{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013427 if (argvars[0].v_type != VAR_DICT)
13428 {
13429 EMSG(_(e_dictreq));
13430 return;
13431 }
13432 if (argvars[0].vval.v_dict == NULL)
13433 return;
13434
13435 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013436 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013437}
13438
13439/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013440 * "haslocaldir()" function
13441 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013443f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013444{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013445 win_T *wp = NULL;
13446
13447 wp = find_tabwin(&argvars[0], &argvars[1]);
13448 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013449}
13450
13451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 * "hasmapto()" function
13453 */
13454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013455f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456{
13457 char_u *name;
13458 char_u *mode;
13459 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013460 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013462 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013463 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 mode = (char_u *)"nvo";
13465 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013467 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013468 if (argvars[2].v_type != VAR_UNKNOWN)
13469 abbr = get_tv_number(&argvars[2]);
13470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471
Bram Moolenaar2c932302006-03-18 21:42:09 +000013472 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013473 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013475 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476}
13477
13478/*
13479 * "histadd()" function
13480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013482f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483{
13484#ifdef FEAT_CMDHIST
13485 int histype;
13486 char_u *str;
13487 char_u buf[NUMBUFLEN];
13488#endif
13489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013490 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 if (check_restricted() || check_secure())
13492 return;
13493#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013494 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13495 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 if (histype >= 0)
13497 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013498 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499 if (*str != NUL)
13500 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013501 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013503 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 return;
13505 }
13506 }
13507#endif
13508}
13509
13510/*
13511 * "histdel()" function
13512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013514f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515{
13516#ifdef FEAT_CMDHIST
13517 int n;
13518 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013519 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013521 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13522 if (str == NULL)
13523 n = 0;
13524 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013526 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013527 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013529 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013530 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 else
13532 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013533 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013534 get_tv_string_buf(&argvars[1], buf));
13535 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536#endif
13537}
13538
13539/*
13540 * "histget()" function
13541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013543f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544{
13545#ifdef FEAT_CMDHIST
13546 int type;
13547 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013548 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013550 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13551 if (str == NULL)
13552 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013554 {
13555 type = get_histtype(str);
13556 if (argvars[1].v_type == VAR_UNKNOWN)
13557 idx = get_history_idx(type);
13558 else
13559 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13560 /* -1 on type error */
13561 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013564 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013566 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567}
13568
13569/*
13570 * "histnr()" function
13571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013573f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574{
13575 int i;
13576
13577#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013578 char_u *history = get_tv_string_chk(&argvars[0]);
13579
13580 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 if (i >= HIST_CMD && i < HIST_COUNT)
13582 i = get_history_idx(i);
13583 else
13584#endif
13585 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013586 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587}
13588
13589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 * "highlightID(name)" function
13591 */
13592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013593f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596}
13597
13598/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013599 * "highlight_exists()" function
13600 */
13601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013602f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013603{
13604 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13605}
13606
13607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 * "hostname()" function
13609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013611f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612{
13613 char_u hostname[256];
13614
13615 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013616 rettv->v_type = VAR_STRING;
13617 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618}
13619
13620/*
13621 * iconv() function
13622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013624f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625{
13626#ifdef FEAT_MBYTE
13627 char_u buf1[NUMBUFLEN];
13628 char_u buf2[NUMBUFLEN];
13629 char_u *from, *to, *str;
13630 vimconv_T vimconv;
13631#endif
13632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013633 rettv->v_type = VAR_STRING;
13634 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635
13636#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 str = get_tv_string(&argvars[0]);
13638 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13639 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 vimconv.vc_type = CONV_NONE;
13641 convert_setup(&vimconv, from, to);
13642
13643 /* If the encodings are equal, no conversion needed. */
13644 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013647 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648
13649 convert_setup(&vimconv, NULL, NULL);
13650 vim_free(from);
13651 vim_free(to);
13652#endif
13653}
13654
13655/*
13656 * "indent()" function
13657 */
13658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013659f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660{
13661 linenr_T lnum;
13662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013667 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668}
13669
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013670/*
13671 * "index()" function
13672 */
13673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013674f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013675{
Bram Moolenaar33570922005-01-25 22:26:29 +000013676 list_T *l;
13677 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013678 long idx = 0;
13679 int ic = FALSE;
13680
13681 rettv->vval.v_number = -1;
13682 if (argvars[0].v_type != VAR_LIST)
13683 {
13684 EMSG(_(e_listreq));
13685 return;
13686 }
13687 l = argvars[0].vval.v_list;
13688 if (l != NULL)
13689 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013690 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013691 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013693 int error = FALSE;
13694
Bram Moolenaar758711c2005-02-02 23:11:38 +000013695 /* Start at specified item. Use the cached index that list_find()
13696 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013697 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013698 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013699 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013700 ic = get_tv_number_chk(&argvars[3], &error);
13701 if (error)
13702 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013703 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013704
Bram Moolenaar758711c2005-02-02 23:11:38 +000013705 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013706 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013707 {
13708 rettv->vval.v_number = idx;
13709 break;
13710 }
13711 }
13712}
13713
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714static int inputsecret_flag = 0;
13715
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013716static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013717
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013719 * This function is used by f_input() and f_inputdialog() functions. The third
13720 * argument to f_input() specifies the type of completion to use at the
13721 * prompt. The third argument to f_inputdialog() specifies the value to return
13722 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 */
13724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013725get_user_input(
13726 typval_T *argvars,
13727 typval_T *rettv,
13728 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013730 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731 char_u *p = NULL;
13732 int c;
13733 char_u buf[NUMBUFLEN];
13734 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013735 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013736 int xp_type = EXPAND_NOTHING;
13737 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013740 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741
13742#ifdef NO_CONSOLE_INPUT
13743 /* While starting up, there is no place to enter text. */
13744 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746#endif
13747
13748 cmd_silent = FALSE; /* Want to see the prompt. */
13749 if (prompt != NULL)
13750 {
13751 /* Only the part of the message after the last NL is considered as
13752 * prompt for the command line */
13753 p = vim_strrchr(prompt, '\n');
13754 if (p == NULL)
13755 p = prompt;
13756 else
13757 {
13758 ++p;
13759 c = *p;
13760 *p = NUL;
13761 msg_start();
13762 msg_clr_eos();
13763 msg_puts_attr(prompt, echo_attr);
13764 msg_didout = FALSE;
13765 msg_starthere();
13766 *p = c;
13767 }
13768 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013770 if (argvars[1].v_type != VAR_UNKNOWN)
13771 {
13772 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13773 if (defstr != NULL)
13774 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013776 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013777 {
13778 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013779 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013780 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013781
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013782 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013783 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013784
Bram Moolenaar4463f292005-09-25 22:20:24 +000013785 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13786 if (xp_name == NULL)
13787 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013788
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013789 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013790
Bram Moolenaar4463f292005-09-25 22:20:24 +000013791 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13792 &xp_arg) == FAIL)
13793 return;
13794 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013795 }
13796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013797 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013798 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013799 int save_ex_normal_busy = ex_normal_busy;
13800 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013801 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013802 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13803 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013804 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013805 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013806 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013807 && argvars[1].v_type != VAR_UNKNOWN
13808 && argvars[2].v_type != VAR_UNKNOWN)
13809 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13810 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013811
13812 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013814 /* since the user typed this, no need to wait for return */
13815 need_wait_return = FALSE;
13816 msg_didout = FALSE;
13817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 cmd_silent = cmd_silent_save;
13819}
13820
13821/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013822 * "input()" function
13823 * Also handles inputsecret() when inputsecret is set.
13824 */
13825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013826f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013827{
13828 get_user_input(argvars, rettv, FALSE);
13829}
13830
13831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 * "inputdialog()" function
13833 */
13834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013835f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836{
13837#if defined(FEAT_GUI_TEXTDIALOG)
13838 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13839 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13840 {
13841 char_u *message;
13842 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013843 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013845 message = get_tv_string_chk(&argvars[0]);
13846 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013847 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013848 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 else
13850 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013851 if (message != NULL && defstr != NULL
13852 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013853 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 else
13856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013857 if (message != NULL && defstr != NULL
13858 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013859 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013860 rettv->vval.v_string = vim_strsave(
13861 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013863 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013865 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866 }
13867 else
13868#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013869 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870}
13871
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013872/*
13873 * "inputlist()" function
13874 */
13875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013876f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013877{
13878 listitem_T *li;
13879 int selected;
13880 int mouse_used;
13881
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013882#ifdef NO_CONSOLE_INPUT
13883 /* While starting up, there is no place to enter text. */
13884 if (no_console_input())
13885 return;
13886#endif
13887 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13888 {
13889 EMSG2(_(e_listarg), "inputlist()");
13890 return;
13891 }
13892
13893 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013894 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013895 lines_left = Rows; /* avoid more prompt */
13896 msg_scroll = TRUE;
13897 msg_clr_eos();
13898
13899 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13900 {
13901 msg_puts(get_tv_string(&li->li_tv));
13902 msg_putchar('\n');
13903 }
13904
13905 /* Ask for choice. */
13906 selected = prompt_for_number(&mouse_used);
13907 if (mouse_used)
13908 selected -= lines_left;
13909
13910 rettv->vval.v_number = selected;
13911}
13912
13913
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13915
13916/*
13917 * "inputrestore()" function
13918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013920f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921{
13922 if (ga_userinput.ga_len > 0)
13923 {
13924 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13926 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013927 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928 }
13929 else if (p_verbose > 1)
13930 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013931 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013932 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 }
13934}
13935
13936/*
13937 * "inputsave()" function
13938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013940f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013942 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 if (ga_grow(&ga_userinput, 1) == OK)
13944 {
13945 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13946 + ga_userinput.ga_len);
13947 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013948 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 }
13950 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013951 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952}
13953
13954/*
13955 * "inputsecret()" function
13956 */
13957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013958f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959{
13960 ++cmdline_star;
13961 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013962 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963 --cmdline_star;
13964 --inputsecret_flag;
13965}
13966
13967/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013968 * "insert()" function
13969 */
13970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013971f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013972{
13973 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013974 listitem_T *item;
13975 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013976 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013977
13978 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013979 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013980 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013981 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013982 {
13983 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013984 before = get_tv_number_chk(&argvars[2], &error);
13985 if (error)
13986 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013987
Bram Moolenaar758711c2005-02-02 23:11:38 +000013988 if (before == l->lv_len)
13989 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013990 else
13991 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013992 item = list_find(l, before);
13993 if (item == NULL)
13994 {
13995 EMSGN(_(e_listidx), before);
13996 l = NULL;
13997 }
13998 }
13999 if (l != NULL)
14000 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014001 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014002 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014003 }
14004 }
14005}
14006
14007/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014008 * "invert(expr)" function
14009 */
14010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014011f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014012{
14013 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14014}
14015
14016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017 * "isdirectory()" function
14018 */
14019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014020f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014022 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023}
14024
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014025/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014026 * "islocked()" function
14027 */
14028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014029f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014030{
14031 lval_T lv;
14032 char_u *end;
14033 dictitem_T *di;
14034
14035 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014036 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14037 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014038 if (end != NULL && lv.ll_name != NULL)
14039 {
14040 if (*end != NUL)
14041 EMSG(_(e_trailing));
14042 else
14043 {
14044 if (lv.ll_tv == NULL)
14045 {
14046 if (check_changedtick(lv.ll_name))
14047 rettv->vval.v_number = 1; /* always locked */
14048 else
14049 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014050 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014051 if (di != NULL)
14052 {
14053 /* Consider a variable locked when:
14054 * 1. the variable itself is locked
14055 * 2. the value of the variable is locked.
14056 * 3. the List or Dict value is locked.
14057 */
14058 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14059 || tv_islocked(&di->di_tv));
14060 }
14061 }
14062 }
14063 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014064 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014065 else if (lv.ll_newkey != NULL)
14066 EMSG2(_(e_dictkey), lv.ll_newkey);
14067 else if (lv.ll_list != NULL)
14068 /* List item. */
14069 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14070 else
14071 /* Dictionary item. */
14072 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14073 }
14074 }
14075
14076 clear_lval(&lv);
14077}
14078
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014079static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014080
14081/*
14082 * Turn a dict into a list:
14083 * "what" == 0: list of keys
14084 * "what" == 1: list of values
14085 * "what" == 2: list of items
14086 */
14087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014088dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014089{
Bram Moolenaar33570922005-01-25 22:26:29 +000014090 list_T *l2;
14091 dictitem_T *di;
14092 hashitem_T *hi;
14093 listitem_T *li;
14094 listitem_T *li2;
14095 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014096 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014097
Bram Moolenaar8c711452005-01-14 21:53:12 +000014098 if (argvars[0].v_type != VAR_DICT)
14099 {
14100 EMSG(_(e_dictreq));
14101 return;
14102 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014103 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014104 return;
14105
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014106 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014107 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014108
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014109 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014110 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014111 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014112 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014113 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014114 --todo;
14115 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014116
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014117 li = listitem_alloc();
14118 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014119 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014120 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014121
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014122 if (what == 0)
14123 {
14124 /* keys() */
14125 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014126 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014127 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14128 }
14129 else if (what == 1)
14130 {
14131 /* values() */
14132 copy_tv(&di->di_tv, &li->li_tv);
14133 }
14134 else
14135 {
14136 /* items() */
14137 l2 = list_alloc();
14138 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014139 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014140 li->li_tv.vval.v_list = l2;
14141 if (l2 == NULL)
14142 break;
14143 ++l2->lv_refcount;
14144
14145 li2 = listitem_alloc();
14146 if (li2 == NULL)
14147 break;
14148 list_append(l2, li2);
14149 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014150 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014151 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14152
14153 li2 = listitem_alloc();
14154 if (li2 == NULL)
14155 break;
14156 list_append(l2, li2);
14157 copy_tv(&di->di_tv, &li2->li_tv);
14158 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014159 }
14160 }
14161}
14162
14163/*
14164 * "items(dict)" function
14165 */
14166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014167f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014168{
14169 dict_list(argvars, rettv, 2);
14170}
14171
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014173 * "join()" function
14174 */
14175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014176f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014177{
14178 garray_T ga;
14179 char_u *sep;
14180
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014181 if (argvars[0].v_type != VAR_LIST)
14182 {
14183 EMSG(_(e_listreq));
14184 return;
14185 }
14186 if (argvars[0].vval.v_list == NULL)
14187 return;
14188 if (argvars[1].v_type == VAR_UNKNOWN)
14189 sep = (char_u *)" ";
14190 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014192
14193 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014194
14195 if (sep != NULL)
14196 {
14197 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014198 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199 ga_append(&ga, NUL);
14200 rettv->vval.v_string = (char_u *)ga.ga_data;
14201 }
14202 else
14203 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014204}
14205
14206/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014207 * "jsondecode()" function
14208 */
14209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014210f_jsondecode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014211{
14212 js_read_T reader;
14213
14214 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014215 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014216 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +010014217 if (json_decode_all(&reader, rettv) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014218 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014219}
14220
14221/*
14222 * "jsonencode()" function
14223 */
14224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014225f_jsonencode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014226{
14227 rettv->v_type = VAR_STRING;
14228 rettv->vval.v_string = json_encode(&argvars[0]);
14229}
14230
14231/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014232 * "keys()" function
14233 */
14234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014235f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014236{
14237 dict_list(argvars, rettv, 0);
14238}
14239
14240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 * "last_buffer_nr()" function.
14242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014244f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245{
14246 int n = 0;
14247 buf_T *buf;
14248
14249 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14250 if (n < buf->b_fnum)
14251 n = buf->b_fnum;
14252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014253 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014254}
14255
14256/*
14257 * "len()" function
14258 */
14259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014260f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014261{
14262 switch (argvars[0].v_type)
14263 {
14264 case VAR_STRING:
14265 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014266 rettv->vval.v_number = (varnumber_T)STRLEN(
14267 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014268 break;
14269 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014270 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014271 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014272 case VAR_DICT:
14273 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14274 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014275 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014276 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014277 break;
14278 }
14279}
14280
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014281static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014282
14283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014284libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014285{
14286#ifdef FEAT_LIBCALL
14287 char_u *string_in;
14288 char_u **string_result;
14289 int nr_result;
14290#endif
14291
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014292 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014293 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014294 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014295
14296 if (check_restricted() || check_secure())
14297 return;
14298
14299#ifdef FEAT_LIBCALL
14300 /* The first two args must be strings, otherwise its meaningless */
14301 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14302 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014303 string_in = NULL;
14304 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014305 string_in = argvars[2].vval.v_string;
14306 if (type == VAR_NUMBER)
14307 string_result = NULL;
14308 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014310 if (mch_libcall(argvars[0].vval.v_string,
14311 argvars[1].vval.v_string,
14312 string_in,
14313 argvars[2].vval.v_number,
14314 string_result,
14315 &nr_result) == OK
14316 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014318 }
14319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320}
14321
14322/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014323 * "libcall()" function
14324 */
14325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014326f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014327{
14328 libcall_common(argvars, rettv, VAR_STRING);
14329}
14330
14331/*
14332 * "libcallnr()" function
14333 */
14334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014335f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014336{
14337 libcall_common(argvars, rettv, VAR_NUMBER);
14338}
14339
14340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341 * "line(string)" function
14342 */
14343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014344f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345{
14346 linenr_T lnum = 0;
14347 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014348 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014350 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 if (fp != NULL)
14352 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014353 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354}
14355
14356/*
14357 * "line2byte(lnum)" function
14358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014360f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361{
14362#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364#else
14365 linenr_T lnum;
14366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014367 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014369 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014371 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14372 if (rettv->vval.v_number >= 0)
14373 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374#endif
14375}
14376
14377/*
14378 * "lispindent(lnum)" function
14379 */
14380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014381f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382{
14383#ifdef FEAT_LISP
14384 pos_T pos;
14385 linenr_T lnum;
14386
14387 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014388 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14390 {
14391 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014392 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393 curwin->w_cursor = pos;
14394 }
14395 else
14396#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398}
14399
14400/*
14401 * "localtime()" function
14402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014404f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014406 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407}
14408
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014409static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410
14411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014412get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413{
14414 char_u *keys;
14415 char_u *which;
14416 char_u buf[NUMBUFLEN];
14417 char_u *keys_buf = NULL;
14418 char_u *rhs;
14419 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014420 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014421 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014422 mapblock_T *mp;
14423 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424
14425 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014426 rettv->v_type = VAR_STRING;
14427 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 if (*keys == NUL)
14431 return;
14432
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014433 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014435 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014436 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014437 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014438 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014439 if (argvars[3].v_type != VAR_UNKNOWN)
14440 get_dict = get_tv_number(&argvars[3]);
14441 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 else
14444 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014445 if (which == NULL)
14446 return;
14447
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448 mode = get_map_mode(&which, 0);
14449
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014450 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014451 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014453
14454 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014456 /* Return a string. */
14457 if (rhs != NULL)
14458 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459
Bram Moolenaarbd743252010-10-20 21:23:33 +020014460 }
14461 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14462 {
14463 /* Return a dictionary. */
14464 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14465 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14466 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467
Bram Moolenaarbd743252010-10-20 21:23:33 +020014468 dict_add_nr_str(dict, "lhs", 0L, lhs);
14469 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14470 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14471 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14472 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14473 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14474 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014475 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014476 dict_add_nr_str(dict, "mode", 0L, mapmode);
14477
14478 vim_free(lhs);
14479 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 }
14481}
14482
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014483#ifdef FEAT_FLOAT
14484/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014485 * "log()" function
14486 */
14487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014488f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014489{
14490 float_T f;
14491
14492 rettv->v_type = VAR_FLOAT;
14493 if (get_float_arg(argvars, &f) == OK)
14494 rettv->vval.v_float = log(f);
14495 else
14496 rettv->vval.v_float = 0.0;
14497}
14498
14499/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014500 * "log10()" function
14501 */
14502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014503f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014504{
14505 float_T f;
14506
14507 rettv->v_type = VAR_FLOAT;
14508 if (get_float_arg(argvars, &f) == OK)
14509 rettv->vval.v_float = log10(f);
14510 else
14511 rettv->vval.v_float = 0.0;
14512}
14513#endif
14514
Bram Moolenaar1dced572012-04-05 16:54:08 +020014515#ifdef FEAT_LUA
14516/*
14517 * "luaeval()" function
14518 */
14519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014520f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014521{
14522 char_u *str;
14523 char_u buf[NUMBUFLEN];
14524
14525 str = get_tv_string_buf(&argvars[0], buf);
14526 do_luaeval(str, argvars + 1, rettv);
14527}
14528#endif
14529
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014531 * "map()" function
14532 */
14533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014534f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014535{
14536 filter_map(argvars, rettv, TRUE);
14537}
14538
14539/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014540 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 */
14542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014543f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014545 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546}
14547
14548/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014549 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 */
14551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014552f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014554 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555}
14556
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014557static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558
14559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014560find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014562 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014563 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014564 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 char_u *pat;
14566 regmatch_T regmatch;
14567 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014568 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 char_u *save_cpo;
14570 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014571 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014572 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014573 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014574 list_T *l = NULL;
14575 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014576 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014577 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578
14579 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14580 save_cpo = p_cpo;
14581 p_cpo = (char_u *)"";
14582
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014583 rettv->vval.v_number = -1;
14584 if (type == 3)
14585 {
14586 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014587 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014588 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014589 }
14590 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014592 rettv->v_type = VAR_STRING;
14593 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014596 if (argvars[0].v_type == VAR_LIST)
14597 {
14598 if ((l = argvars[0].vval.v_list) == NULL)
14599 goto theend;
14600 li = l->lv_first;
14601 }
14602 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014603 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014604 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014605 len = (long)STRLEN(str);
14606 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014608 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14609 if (pat == NULL)
14610 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014611
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014612 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014614 int error = FALSE;
14615
14616 start = get_tv_number_chk(&argvars[2], &error);
14617 if (error)
14618 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014619 if (l != NULL)
14620 {
14621 li = list_find(l, start);
14622 if (li == NULL)
14623 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014624 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014625 }
14626 else
14627 {
14628 if (start < 0)
14629 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014630 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014631 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014632 /* When "count" argument is there ignore matches before "start",
14633 * otherwise skip part of the string. Differs when pattern is "^"
14634 * or "\<". */
14635 if (argvars[3].v_type != VAR_UNKNOWN)
14636 startcol = start;
14637 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014638 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014639 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014640 len -= start;
14641 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014642 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014643
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014644 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014645 nth = get_tv_number_chk(&argvars[3], &error);
14646 if (error)
14647 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648 }
14649
14650 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14651 if (regmatch.regprog != NULL)
14652 {
14653 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014654
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014655 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014656 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014657 if (l != NULL)
14658 {
14659 if (li == NULL)
14660 {
14661 match = FALSE;
14662 break;
14663 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014664 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014665 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014666 if (str == NULL)
14667 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014668 }
14669
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014670 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014671
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014672 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014673 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014674 if (l == NULL && !match)
14675 break;
14676
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014677 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014678 if (l != NULL)
14679 {
14680 li = li->li_next;
14681 ++idx;
14682 }
14683 else
14684 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014685#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014686 startcol = (colnr_T)(regmatch.startp[0]
14687 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014688#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014689 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014690#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014691 if (startcol > (colnr_T)len
14692 || str + startcol <= regmatch.startp[0])
14693 {
14694 match = FALSE;
14695 break;
14696 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014697 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014698 }
14699
14700 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014702 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014703 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014704 int i;
14705
14706 /* return list with matched string and submatches */
14707 for (i = 0; i < NSUBEXP; ++i)
14708 {
14709 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014710 {
14711 if (list_append_string(rettv->vval.v_list,
14712 (char_u *)"", 0) == FAIL)
14713 break;
14714 }
14715 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014716 regmatch.startp[i],
14717 (int)(regmatch.endp[i] - regmatch.startp[i]))
14718 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014719 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014720 }
14721 }
14722 else if (type == 2)
14723 {
14724 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014725 if (l != NULL)
14726 copy_tv(&li->li_tv, rettv);
14727 else
14728 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014730 }
14731 else if (l != NULL)
14732 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 else
14734 {
14735 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014736 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737 (varnumber_T)(regmatch.startp[0] - str);
14738 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014739 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014741 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 }
14743 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014744 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 }
14746
14747theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014748 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 p_cpo = save_cpo;
14750}
14751
14752/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014753 * "match()" function
14754 */
14755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014756f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014757{
14758 find_some_match(argvars, rettv, 1);
14759}
14760
14761/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014762 * "matchadd()" function
14763 */
14764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014765f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014766{
14767#ifdef FEAT_SEARCH_EXTRA
14768 char_u buf[NUMBUFLEN];
14769 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14770 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14771 int prio = 10; /* default priority */
14772 int id = -1;
14773 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014774 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014775
14776 rettv->vval.v_number = -1;
14777
14778 if (grp == NULL || pat == NULL)
14779 return;
14780 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014781 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014782 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014783 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014784 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014785 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014786 if (argvars[4].v_type != VAR_UNKNOWN)
14787 {
14788 if (argvars[4].v_type != VAR_DICT)
14789 {
14790 EMSG(_(e_dictreq));
14791 return;
14792 }
14793 if (dict_find(argvars[4].vval.v_dict,
14794 (char_u *)"conceal", -1) != NULL)
14795 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14796 (char_u *)"conceal", FALSE);
14797 }
14798 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014799 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014800 if (error == TRUE)
14801 return;
14802 if (id >= 1 && id <= 3)
14803 {
14804 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14805 return;
14806 }
14807
Bram Moolenaar6561d522015-07-21 15:48:27 +020014808 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14809 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014810#endif
14811}
14812
14813/*
14814 * "matchaddpos()" function
14815 */
14816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014817f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020014818{
14819#ifdef FEAT_SEARCH_EXTRA
14820 char_u buf[NUMBUFLEN];
14821 char_u *group;
14822 int prio = 10;
14823 int id = -1;
14824 int error = FALSE;
14825 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014826 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014827
14828 rettv->vval.v_number = -1;
14829
14830 group = get_tv_string_buf_chk(&argvars[0], buf);
14831 if (group == NULL)
14832 return;
14833
14834 if (argvars[1].v_type != VAR_LIST)
14835 {
14836 EMSG2(_(e_listarg), "matchaddpos()");
14837 return;
14838 }
14839 l = argvars[1].vval.v_list;
14840 if (l == NULL)
14841 return;
14842
14843 if (argvars[2].v_type != VAR_UNKNOWN)
14844 {
14845 prio = get_tv_number_chk(&argvars[2], &error);
14846 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014847 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014848 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014849 if (argvars[4].v_type != VAR_UNKNOWN)
14850 {
14851 if (argvars[4].v_type != VAR_DICT)
14852 {
14853 EMSG(_(e_dictreq));
14854 return;
14855 }
14856 if (dict_find(argvars[4].vval.v_dict,
14857 (char_u *)"conceal", -1) != NULL)
14858 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14859 (char_u *)"conceal", FALSE);
14860 }
14861 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014862 }
14863 if (error == TRUE)
14864 return;
14865
14866 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14867 if (id == 1 || id == 2)
14868 {
14869 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14870 return;
14871 }
14872
Bram Moolenaar6561d522015-07-21 15:48:27 +020014873 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14874 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014875#endif
14876}
14877
14878/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014879 * "matcharg()" function
14880 */
14881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014882f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014883{
14884 if (rettv_list_alloc(rettv) == OK)
14885 {
14886#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014887 int id = get_tv_number(&argvars[0]);
14888 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014889
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014890 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014891 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014892 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14893 {
14894 list_append_string(rettv->vval.v_list,
14895 syn_id2name(m->hlg_id), -1);
14896 list_append_string(rettv->vval.v_list, m->pattern, -1);
14897 }
14898 else
14899 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014900 list_append_string(rettv->vval.v_list, NULL, -1);
14901 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014902 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014903 }
14904#endif
14905 }
14906}
14907
14908/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014909 * "matchdelete()" function
14910 */
14911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014912f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014913{
14914#ifdef FEAT_SEARCH_EXTRA
14915 rettv->vval.v_number = match_delete(curwin,
14916 (int)get_tv_number(&argvars[0]), TRUE);
14917#endif
14918}
14919
14920/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014921 * "matchend()" function
14922 */
14923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014924f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014925{
14926 find_some_match(argvars, rettv, 0);
14927}
14928
14929/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014930 * "matchlist()" function
14931 */
14932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014933f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014934{
14935 find_some_match(argvars, rettv, 3);
14936}
14937
14938/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014939 * "matchstr()" function
14940 */
14941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014942f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014943{
14944 find_some_match(argvars, rettv, 2);
14945}
14946
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014947static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014948
14949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014950max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014951{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014952 long n = 0;
14953 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014954 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014955
14956 if (argvars[0].v_type == VAR_LIST)
14957 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014958 list_T *l;
14959 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014960
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014961 l = argvars[0].vval.v_list;
14962 if (l != NULL)
14963 {
14964 li = l->lv_first;
14965 if (li != NULL)
14966 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014967 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014968 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014969 {
14970 li = li->li_next;
14971 if (li == NULL)
14972 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014973 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014974 if (domax ? i > n : i < n)
14975 n = i;
14976 }
14977 }
14978 }
14979 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014980 else if (argvars[0].v_type == VAR_DICT)
14981 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014982 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014983 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014984 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014985 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014986
14987 d = argvars[0].vval.v_dict;
14988 if (d != NULL)
14989 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014990 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014991 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014992 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014993 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014994 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014995 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014996 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014997 if (first)
14998 {
14999 n = i;
15000 first = FALSE;
15001 }
15002 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015003 n = i;
15004 }
15005 }
15006 }
15007 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015008 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015009 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015010 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015011}
15012
15013/*
15014 * "max()" function
15015 */
15016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015017f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015018{
15019 max_min(argvars, rettv, TRUE);
15020}
15021
15022/*
15023 * "min()" function
15024 */
15025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015026f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015027{
15028 max_min(argvars, rettv, FALSE);
15029}
15030
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015031static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015032
15033/*
15034 * Create the directory in which "dir" is located, and higher levels when
15035 * needed.
15036 */
15037 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015038mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015039{
15040 char_u *p;
15041 char_u *updir;
15042 int r = FAIL;
15043
15044 /* Get end of directory name in "dir".
15045 * We're done when it's "/" or "c:/". */
15046 p = gettail_sep(dir);
15047 if (p <= get_past_head(dir))
15048 return OK;
15049
15050 /* If the directory exists we're done. Otherwise: create it.*/
15051 updir = vim_strnsave(dir, (int)(p - dir));
15052 if (updir == NULL)
15053 return FAIL;
15054 if (mch_isdir(updir))
15055 r = OK;
15056 else if (mkdir_recurse(updir, prot) == OK)
15057 r = vim_mkdir_emsg(updir, prot);
15058 vim_free(updir);
15059 return r;
15060}
15061
15062#ifdef vim_mkdir
15063/*
15064 * "mkdir()" function
15065 */
15066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015067f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015068{
15069 char_u *dir;
15070 char_u buf[NUMBUFLEN];
15071 int prot = 0755;
15072
15073 rettv->vval.v_number = FAIL;
15074 if (check_restricted() || check_secure())
15075 return;
15076
15077 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015078 if (*dir == NUL)
15079 rettv->vval.v_number = FAIL;
15080 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015081 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015082 if (*gettail(dir) == NUL)
15083 /* remove trailing slashes */
15084 *gettail_sep(dir) = NUL;
15085
15086 if (argvars[1].v_type != VAR_UNKNOWN)
15087 {
15088 if (argvars[2].v_type != VAR_UNKNOWN)
15089 prot = get_tv_number_chk(&argvars[2], NULL);
15090 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15091 mkdir_recurse(dir, prot);
15092 }
15093 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015094 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015095}
15096#endif
15097
Bram Moolenaar0d660222005-01-07 21:51:51 +000015098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015099 * "mode()" function
15100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015102f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015104 char_u buf[3];
15105
15106 buf[1] = NUL;
15107 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 if (VIsual_active)
15110 {
15111 if (VIsual_select)
15112 buf[0] = VIsual_mode + 's' - 'v';
15113 else
15114 buf[0] = VIsual_mode;
15115 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015116 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015117 || State == CONFIRM)
15118 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015120 if (State == ASKMORE)
15121 buf[1] = 'm';
15122 else if (State == CONFIRM)
15123 buf[1] = '?';
15124 }
15125 else if (State == EXTERNCMD)
15126 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127 else if (State & INSERT)
15128 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015129#ifdef FEAT_VREPLACE
15130 if (State & VREPLACE_FLAG)
15131 {
15132 buf[0] = 'R';
15133 buf[1] = 'v';
15134 }
15135 else
15136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137 if (State & REPLACE_FLAG)
15138 buf[0] = 'R';
15139 else
15140 buf[0] = 'i';
15141 }
15142 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015143 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015145 if (exmode_active)
15146 buf[1] = 'v';
15147 }
15148 else if (exmode_active)
15149 {
15150 buf[0] = 'c';
15151 buf[1] = 'e';
15152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015156 if (finish_op)
15157 buf[1] = 'o';
15158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015160 /* Clear out the minor mode when the argument is not a non-zero number or
15161 * non-empty string. */
15162 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015163 buf[1] = NUL;
15164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015165 rettv->vval.v_string = vim_strsave(buf);
15166 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167}
15168
Bram Moolenaar429fa852013-04-15 12:27:36 +020015169#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015170/*
15171 * "mzeval()" function
15172 */
15173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015174f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015175{
15176 char_u *str;
15177 char_u buf[NUMBUFLEN];
15178
15179 str = get_tv_string_buf(&argvars[0], buf);
15180 do_mzeval(str, rettv);
15181}
Bram Moolenaar75676462013-01-30 14:55:42 +010015182
15183 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015184mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015185{
15186 typval_T argvars[3];
15187
15188 argvars[0].v_type = VAR_STRING;
15189 argvars[0].vval.v_string = name;
15190 copy_tv(args, &argvars[1]);
15191 argvars[2].v_type = VAR_UNKNOWN;
15192 f_call(argvars, rettv);
15193 clear_tv(&argvars[1]);
15194}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015195#endif
15196
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015198 * "nextnonblank()" function
15199 */
15200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015201f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015202{
15203 linenr_T lnum;
15204
15205 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015207 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015208 {
15209 lnum = 0;
15210 break;
15211 }
15212 if (*skipwhite(ml_get(lnum)) != NUL)
15213 break;
15214 }
15215 rettv->vval.v_number = lnum;
15216}
15217
15218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219 * "nr2char()" function
15220 */
15221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015222f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223{
15224 char_u buf[NUMBUFLEN];
15225
15226#ifdef FEAT_MBYTE
15227 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015228 {
15229 int utf8 = 0;
15230
15231 if (argvars[1].v_type != VAR_UNKNOWN)
15232 utf8 = get_tv_number_chk(&argvars[1], NULL);
15233 if (utf8)
15234 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15235 else
15236 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238 else
15239#endif
15240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015241 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242 buf[1] = NUL;
15243 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015244 rettv->v_type = VAR_STRING;
15245 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015246}
15247
15248/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015249 * "or(expr, expr)" function
15250 */
15251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015252f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015253{
15254 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15255 | get_tv_number_chk(&argvars[1], NULL);
15256}
15257
15258/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015259 * "pathshorten()" function
15260 */
15261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015262f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015263{
15264 char_u *p;
15265
15266 rettv->v_type = VAR_STRING;
15267 p = get_tv_string_chk(&argvars[0]);
15268 if (p == NULL)
15269 rettv->vval.v_string = NULL;
15270 else
15271 {
15272 p = vim_strsave(p);
15273 rettv->vval.v_string = p;
15274 if (p != NULL)
15275 shorten_dir(p);
15276 }
15277}
15278
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015279#ifdef FEAT_PERL
15280/*
15281 * "perleval()" function
15282 */
15283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015284f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015285{
15286 char_u *str;
15287 char_u buf[NUMBUFLEN];
15288
15289 str = get_tv_string_buf(&argvars[0], buf);
15290 do_perleval(str, rettv);
15291}
15292#endif
15293
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015294#ifdef FEAT_FLOAT
15295/*
15296 * "pow()" function
15297 */
15298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015299f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015300{
15301 float_T fx, fy;
15302
15303 rettv->v_type = VAR_FLOAT;
15304 if (get_float_arg(argvars, &fx) == OK
15305 && get_float_arg(&argvars[1], &fy) == OK)
15306 rettv->vval.v_float = pow(fx, fy);
15307 else
15308 rettv->vval.v_float = 0.0;
15309}
15310#endif
15311
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015312/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015313 * "prevnonblank()" function
15314 */
15315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015316f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015317{
15318 linenr_T lnum;
15319
15320 lnum = get_tv_lnum(argvars);
15321 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15322 lnum = 0;
15323 else
15324 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15325 --lnum;
15326 rettv->vval.v_number = lnum;
15327}
15328
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015329/* This dummy va_list is here because:
15330 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15331 * - locally in the function results in a "used before set" warning
15332 * - using va_start() to initialize it gives "function with fixed args" error */
15333static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015334
Bram Moolenaar8c711452005-01-14 21:53:12 +000015335/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015336 * "printf()" function
15337 */
15338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015339f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015340{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015341 char_u buf[NUMBUFLEN];
15342 int len;
15343 char_u *s;
15344 int saved_did_emsg = did_emsg;
15345 char *fmt;
15346
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015347 rettv->v_type = VAR_STRING;
15348 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015349
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015350 /* Get the required length, allocate the buffer and do it for real. */
15351 did_emsg = FALSE;
15352 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15353 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15354 if (!did_emsg)
15355 {
15356 s = alloc(len + 1);
15357 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015358 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015359 rettv->vval.v_string = s;
15360 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015361 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015362 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015363 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015364}
15365
15366/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015367 * "pumvisible()" function
15368 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015370f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015371{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015372#ifdef FEAT_INS_EXPAND
15373 if (pum_visible())
15374 rettv->vval.v_number = 1;
15375#endif
15376}
15377
Bram Moolenaardb913952012-06-29 12:54:53 +020015378#ifdef FEAT_PYTHON3
15379/*
15380 * "py3eval()" function
15381 */
15382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015383f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015384{
15385 char_u *str;
15386 char_u buf[NUMBUFLEN];
15387
15388 str = get_tv_string_buf(&argvars[0], buf);
15389 do_py3eval(str, rettv);
15390}
15391#endif
15392
15393#ifdef FEAT_PYTHON
15394/*
15395 * "pyeval()" function
15396 */
15397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015398f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015399{
15400 char_u *str;
15401 char_u buf[NUMBUFLEN];
15402
15403 str = get_tv_string_buf(&argvars[0], buf);
15404 do_pyeval(str, rettv);
15405}
15406#endif
15407
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015408/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015409 * "range()" function
15410 */
15411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015412f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015413{
15414 long start;
15415 long end;
15416 long stride = 1;
15417 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015418 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015420 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015421 if (argvars[1].v_type == VAR_UNKNOWN)
15422 {
15423 end = start - 1;
15424 start = 0;
15425 }
15426 else
15427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015428 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015429 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015430 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015431 }
15432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015433 if (error)
15434 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015435 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015436 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015437 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015438 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015439 else
15440 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015441 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015442 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015443 if (list_append_number(rettv->vval.v_list,
15444 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015445 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015446 }
15447}
15448
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015449/*
15450 * "readfile()" function
15451 */
15452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015453f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015454{
15455 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015456 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015457 char_u *fname;
15458 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015459 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15460 int io_size = sizeof(buf);
15461 int readlen; /* size of last fread() */
15462 char_u *prev = NULL; /* previously read bytes, if any */
15463 long prevlen = 0; /* length of data in prev */
15464 long prevsize = 0; /* size of prev buffer */
15465 long maxline = MAXLNUM;
15466 long cnt = 0;
15467 char_u *p; /* position in buf */
15468 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015469
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015470 if (argvars[1].v_type != VAR_UNKNOWN)
15471 {
15472 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15473 binary = TRUE;
15474 if (argvars[2].v_type != VAR_UNKNOWN)
15475 maxline = get_tv_number(&argvars[2]);
15476 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015477
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015478 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015479 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015480
15481 /* Always open the file in binary mode, library functions have a mind of
15482 * their own about CR-LF conversion. */
15483 fname = get_tv_string(&argvars[0]);
15484 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15485 {
15486 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15487 return;
15488 }
15489
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015490 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015491 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015492 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015493
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015494 /* This for loop processes what was read, but is also entered at end
15495 * of file so that either:
15496 * - an incomplete line gets written
15497 * - a "binary" file gets an empty line at the end if it ends in a
15498 * newline. */
15499 for (p = buf, start = buf;
15500 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15501 ++p)
15502 {
15503 if (*p == '\n' || readlen <= 0)
15504 {
15505 listitem_T *li;
15506 char_u *s = NULL;
15507 long_u len = p - start;
15508
15509 /* Finished a line. Remove CRs before NL. */
15510 if (readlen > 0 && !binary)
15511 {
15512 while (len > 0 && start[len - 1] == '\r')
15513 --len;
15514 /* removal may cross back to the "prev" string */
15515 if (len == 0)
15516 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15517 --prevlen;
15518 }
15519 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015520 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015521 else
15522 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015523 /* Change "prev" buffer to be the right size. This way
15524 * the bytes are only copied once, and very long lines are
15525 * allocated only once. */
15526 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015527 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015528 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015529 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015530 prev = NULL; /* the list will own the string */
15531 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015532 }
15533 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015534 if (s == NULL)
15535 {
15536 do_outofmem_msg((long_u) prevlen + len + 1);
15537 failed = TRUE;
15538 break;
15539 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015540
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015541 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015542 {
15543 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015544 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015545 break;
15546 }
15547 li->li_tv.v_type = VAR_STRING;
15548 li->li_tv.v_lock = 0;
15549 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015550 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015551
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015552 start = p + 1; /* step over newline */
15553 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015554 break;
15555 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015556 else if (*p == NUL)
15557 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015558#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015559 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15560 * when finding the BF and check the previous two bytes. */
15561 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015562 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015563 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15564 * + 1, these may be in the "prev" string. */
15565 char_u back1 = p >= buf + 1 ? p[-1]
15566 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15567 char_u back2 = p >= buf + 2 ? p[-2]
15568 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15569 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015570
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015571 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015572 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015573 char_u *dest = p - 2;
15574
15575 /* Usually a BOM is at the beginning of a file, and so at
15576 * the beginning of a line; then we can just step over it.
15577 */
15578 if (start == dest)
15579 start = p + 1;
15580 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015581 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015582 /* have to shuffle buf to close gap */
15583 int adjust_prevlen = 0;
15584
15585 if (dest < buf)
15586 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015587 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015588 dest = buf;
15589 }
15590 if (readlen > p - buf + 1)
15591 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15592 readlen -= 3 - adjust_prevlen;
15593 prevlen -= adjust_prevlen;
15594 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015595 }
15596 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015597 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015598#endif
15599 } /* for */
15600
15601 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15602 break;
15603 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015604 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015605 /* There's part of a line in buf, store it in "prev". */
15606 if (p - start + prevlen >= prevsize)
15607 {
15608 /* need bigger "prev" buffer */
15609 char_u *newprev;
15610
15611 /* A common use case is ordinary text files and "prev" gets a
15612 * fragment of a line, so the first allocation is made
15613 * small, to avoid repeatedly 'allocing' large and
15614 * 'reallocing' small. */
15615 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015616 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015617 else
15618 {
15619 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015620 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015621 prevsize = grow50pc > growmin ? grow50pc : growmin;
15622 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015623 newprev = prev == NULL ? alloc(prevsize)
15624 : vim_realloc(prev, prevsize);
15625 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015626 {
15627 do_outofmem_msg((long_u)prevsize);
15628 failed = TRUE;
15629 break;
15630 }
15631 prev = newprev;
15632 }
15633 /* Add the line part to end of "prev". */
15634 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015635 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015636 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015637 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015638
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015639 /*
15640 * For a negative line count use only the lines at the end of the file,
15641 * free the rest.
15642 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015643 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015644 while (cnt > -maxline)
15645 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015646 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015647 --cnt;
15648 }
15649
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015650 if (failed)
15651 {
15652 list_free(rettv->vval.v_list, TRUE);
15653 /* readfile doc says an empty list is returned on error */
15654 rettv->vval.v_list = list_alloc();
15655 }
15656
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015657 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015658 fclose(fd);
15659}
15660
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015661#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015662static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015663
15664/*
15665 * Convert a List to proftime_T.
15666 * Return FAIL when there is something wrong.
15667 */
15668 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015669list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015670{
15671 long n1, n2;
15672 int error = FALSE;
15673
15674 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15675 || arg->vval.v_list->lv_len != 2)
15676 return FAIL;
15677 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15678 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15679# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015680 tm->HighPart = n1;
15681 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015682# else
15683 tm->tv_sec = n1;
15684 tm->tv_usec = n2;
15685# endif
15686 return error ? FAIL : OK;
15687}
15688#endif /* FEAT_RELTIME */
15689
15690/*
15691 * "reltime()" function
15692 */
15693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015694f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015695{
15696#ifdef FEAT_RELTIME
15697 proftime_T res;
15698 proftime_T start;
15699
15700 if (argvars[0].v_type == VAR_UNKNOWN)
15701 {
15702 /* No arguments: get current time. */
15703 profile_start(&res);
15704 }
15705 else if (argvars[1].v_type == VAR_UNKNOWN)
15706 {
15707 if (list2proftime(&argvars[0], &res) == FAIL)
15708 return;
15709 profile_end(&res);
15710 }
15711 else
15712 {
15713 /* Two arguments: compute the difference. */
15714 if (list2proftime(&argvars[0], &start) == FAIL
15715 || list2proftime(&argvars[1], &res) == FAIL)
15716 return;
15717 profile_sub(&res, &start);
15718 }
15719
15720 if (rettv_list_alloc(rettv) == OK)
15721 {
15722 long n1, n2;
15723
15724# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015725 n1 = res.HighPart;
15726 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015727# else
15728 n1 = res.tv_sec;
15729 n2 = res.tv_usec;
15730# endif
15731 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15732 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15733 }
15734#endif
15735}
15736
15737/*
15738 * "reltimestr()" function
15739 */
15740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015741f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015742{
15743#ifdef FEAT_RELTIME
15744 proftime_T tm;
15745#endif
15746
15747 rettv->v_type = VAR_STRING;
15748 rettv->vval.v_string = NULL;
15749#ifdef FEAT_RELTIME
15750 if (list2proftime(&argvars[0], &tm) == OK)
15751 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15752#endif
15753}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015754
Bram Moolenaar0d660222005-01-07 21:51:51 +000015755#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015756static void make_connection(void);
15757static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015758
15759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015760make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015761{
15762 if (X_DISPLAY == NULL
15763# ifdef FEAT_GUI
15764 && !gui.in_use
15765# endif
15766 )
15767 {
15768 x_force_connect = TRUE;
15769 setup_term_clip();
15770 x_force_connect = FALSE;
15771 }
15772}
15773
15774 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015775check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015776{
15777 make_connection();
15778 if (X_DISPLAY == NULL)
15779 {
15780 EMSG(_("E240: No connection to Vim server"));
15781 return FAIL;
15782 }
15783 return OK;
15784}
15785#endif
15786
15787#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015788static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015789
15790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015791remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015792{
15793 char_u *server_name;
15794 char_u *keys;
15795 char_u *r = NULL;
15796 char_u buf[NUMBUFLEN];
15797# ifdef WIN32
15798 HWND w;
15799# else
15800 Window w;
15801# endif
15802
15803 if (check_restricted() || check_secure())
15804 return;
15805
15806# ifdef FEAT_X11
15807 if (check_connection() == FAIL)
15808 return;
15809# endif
15810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015811 server_name = get_tv_string_chk(&argvars[0]);
15812 if (server_name == NULL)
15813 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015814 keys = get_tv_string_buf(&argvars[1], buf);
15815# ifdef WIN32
15816 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15817# else
15818 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15819 < 0)
15820# endif
15821 {
15822 if (r != NULL)
15823 EMSG(r); /* sending worked but evaluation failed */
15824 else
15825 EMSG2(_("E241: Unable to send to %s"), server_name);
15826 return;
15827 }
15828
15829 rettv->vval.v_string = r;
15830
15831 if (argvars[2].v_type != VAR_UNKNOWN)
15832 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015833 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015834 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015835 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015836
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015837 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015838 v.di_tv.v_type = VAR_STRING;
15839 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015840 idvar = get_tv_string_chk(&argvars[2]);
15841 if (idvar != NULL)
15842 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015843 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015844 }
15845}
15846#endif
15847
15848/*
15849 * "remote_expr()" function
15850 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015852f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015853{
15854 rettv->v_type = VAR_STRING;
15855 rettv->vval.v_string = NULL;
15856#ifdef FEAT_CLIENTSERVER
15857 remote_common(argvars, rettv, TRUE);
15858#endif
15859}
15860
15861/*
15862 * "remote_foreground()" function
15863 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015865f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015866{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015867#ifdef FEAT_CLIENTSERVER
15868# ifdef WIN32
15869 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015870 {
15871 char_u *server_name = get_tv_string_chk(&argvars[0]);
15872
15873 if (server_name != NULL)
15874 serverForeground(server_name);
15875 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015876# else
15877 /* Send a foreground() expression to the server. */
15878 argvars[1].v_type = VAR_STRING;
15879 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15880 argvars[2].v_type = VAR_UNKNOWN;
15881 remote_common(argvars, rettv, TRUE);
15882 vim_free(argvars[1].vval.v_string);
15883# endif
15884#endif
15885}
15886
Bram Moolenaar0d660222005-01-07 21:51:51 +000015887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015888f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015889{
15890#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015891 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015892 char_u *s = NULL;
15893# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015894 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015895# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015896 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015897
15898 if (check_restricted() || check_secure())
15899 {
15900 rettv->vval.v_number = -1;
15901 return;
15902 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015903 serverid = get_tv_string_chk(&argvars[0]);
15904 if (serverid == NULL)
15905 {
15906 rettv->vval.v_number = -1;
15907 return; /* type error; errmsg already given */
15908 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015909# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015910 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015911 if (n == 0)
15912 rettv->vval.v_number = -1;
15913 else
15914 {
15915 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15916 rettv->vval.v_number = (s != NULL);
15917 }
15918# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015919 if (check_connection() == FAIL)
15920 return;
15921
15922 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015923 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015924# endif
15925
15926 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015928 char_u *retvar;
15929
Bram Moolenaar33570922005-01-25 22:26:29 +000015930 v.di_tv.v_type = VAR_STRING;
15931 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015932 retvar = get_tv_string_chk(&argvars[1]);
15933 if (retvar != NULL)
15934 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015935 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015936 }
15937#else
15938 rettv->vval.v_number = -1;
15939#endif
15940}
15941
Bram Moolenaar0d660222005-01-07 21:51:51 +000015942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015943f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015944{
15945 char_u *r = NULL;
15946
15947#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015948 char_u *serverid = get_tv_string_chk(&argvars[0]);
15949
15950 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015951 {
15952# ifdef WIN32
15953 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015954 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015955
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015956 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015957 if (n != 0)
15958 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15959 if (r == NULL)
15960# else
15961 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015962 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015963# endif
15964 EMSG(_("E277: Unable to read a server reply"));
15965 }
15966#endif
15967 rettv->v_type = VAR_STRING;
15968 rettv->vval.v_string = r;
15969}
15970
15971/*
15972 * "remote_send()" function
15973 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015975f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015976{
15977 rettv->v_type = VAR_STRING;
15978 rettv->vval.v_string = NULL;
15979#ifdef FEAT_CLIENTSERVER
15980 remote_common(argvars, rettv, FALSE);
15981#endif
15982}
15983
15984/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015985 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015986 */
15987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015988f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015989{
Bram Moolenaar33570922005-01-25 22:26:29 +000015990 list_T *l;
15991 listitem_T *item, *item2;
15992 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015993 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015994 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015995 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015996 dict_T *d;
15997 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015998 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015999
Bram Moolenaar8c711452005-01-14 21:53:12 +000016000 if (argvars[0].v_type == VAR_DICT)
16001 {
16002 if (argvars[2].v_type != VAR_UNKNOWN)
16003 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016004 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016005 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016006 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016007 key = get_tv_string_chk(&argvars[1]);
16008 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016010 di = dict_find(d, key, -1);
16011 if (di == NULL)
16012 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016013 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16014 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016015 {
16016 *rettv = di->di_tv;
16017 init_tv(&di->di_tv);
16018 dictitem_remove(d, di);
16019 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016020 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016021 }
16022 }
16023 else if (argvars[0].v_type != VAR_LIST)
16024 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016025 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016026 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016027 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016028 int error = FALSE;
16029
16030 idx = get_tv_number_chk(&argvars[1], &error);
16031 if (error)
16032 ; /* type error: do nothing, errmsg already given */
16033 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016034 EMSGN(_(e_listidx), idx);
16035 else
16036 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016037 if (argvars[2].v_type == VAR_UNKNOWN)
16038 {
16039 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016040 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016041 *rettv = item->li_tv;
16042 vim_free(item);
16043 }
16044 else
16045 {
16046 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016047 end = get_tv_number_chk(&argvars[2], &error);
16048 if (error)
16049 ; /* type error: do nothing */
16050 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016051 EMSGN(_(e_listidx), end);
16052 else
16053 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016054 int cnt = 0;
16055
16056 for (li = item; li != NULL; li = li->li_next)
16057 {
16058 ++cnt;
16059 if (li == item2)
16060 break;
16061 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016062 if (li == NULL) /* didn't find "item2" after "item" */
16063 EMSG(_(e_invrange));
16064 else
16065 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016066 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016067 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016068 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016069 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016070 l->lv_first = item;
16071 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016072 item->li_prev = NULL;
16073 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016074 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016075 }
16076 }
16077 }
16078 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016079 }
16080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081}
16082
16083/*
16084 * "rename({from}, {to})" function
16085 */
16086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016087f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088{
16089 char_u buf[NUMBUFLEN];
16090
16091 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016092 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016094 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16095 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096}
16097
16098/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016099 * "repeat()" function
16100 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016102f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016103{
16104 char_u *p;
16105 int n;
16106 int slen;
16107 int len;
16108 char_u *r;
16109 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016110
16111 n = get_tv_number(&argvars[1]);
16112 if (argvars[0].v_type == VAR_LIST)
16113 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016114 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016115 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016116 if (list_extend(rettv->vval.v_list,
16117 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016118 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016119 }
16120 else
16121 {
16122 p = get_tv_string(&argvars[0]);
16123 rettv->v_type = VAR_STRING;
16124 rettv->vval.v_string = NULL;
16125
16126 slen = (int)STRLEN(p);
16127 len = slen * n;
16128 if (len <= 0)
16129 return;
16130
16131 r = alloc(len + 1);
16132 if (r != NULL)
16133 {
16134 for (i = 0; i < n; i++)
16135 mch_memmove(r + i * slen, p, (size_t)slen);
16136 r[len] = NUL;
16137 }
16138
16139 rettv->vval.v_string = r;
16140 }
16141}
16142
16143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144 * "resolve()" function
16145 */
16146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016147f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148{
16149 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016150#ifdef HAVE_READLINK
16151 char_u *buf = NULL;
16152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016154 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155#ifdef FEAT_SHORTCUT
16156 {
16157 char_u *v = NULL;
16158
16159 v = mch_resolve_shortcut(p);
16160 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016161 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016163 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 }
16165#else
16166# ifdef HAVE_READLINK
16167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 char_u *cpy;
16169 int len;
16170 char_u *remain = NULL;
16171 char_u *q;
16172 int is_relative_to_current = FALSE;
16173 int has_trailing_pathsep = FALSE;
16174 int limit = 100;
16175
16176 p = vim_strsave(p);
16177
16178 if (p[0] == '.' && (vim_ispathsep(p[1])
16179 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16180 is_relative_to_current = TRUE;
16181
16182 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016183 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016184 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016186 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188
16189 q = getnextcomp(p);
16190 if (*q != NUL)
16191 {
16192 /* Separate the first path component in "p", and keep the
16193 * remainder (beginning with the path separator). */
16194 remain = vim_strsave(q - 1);
16195 q[-1] = NUL;
16196 }
16197
Bram Moolenaard9462e32011-04-11 21:35:11 +020016198 buf = alloc(MAXPATHL + 1);
16199 if (buf == NULL)
16200 goto fail;
16201
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202 for (;;)
16203 {
16204 for (;;)
16205 {
16206 len = readlink((char *)p, (char *)buf, MAXPATHL);
16207 if (len <= 0)
16208 break;
16209 buf[len] = NUL;
16210
16211 if (limit-- == 0)
16212 {
16213 vim_free(p);
16214 vim_free(remain);
16215 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016216 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 goto fail;
16218 }
16219
16220 /* Ensure that the result will have a trailing path separator
16221 * if the argument has one. */
16222 if (remain == NULL && has_trailing_pathsep)
16223 add_pathsep(buf);
16224
16225 /* Separate the first path component in the link value and
16226 * concatenate the remainders. */
16227 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16228 if (*q != NUL)
16229 {
16230 if (remain == NULL)
16231 remain = vim_strsave(q - 1);
16232 else
16233 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016234 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235 if (cpy != NULL)
16236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 vim_free(remain);
16238 remain = cpy;
16239 }
16240 }
16241 q[-1] = NUL;
16242 }
16243
16244 q = gettail(p);
16245 if (q > p && *q == NUL)
16246 {
16247 /* Ignore trailing path separator. */
16248 q[-1] = NUL;
16249 q = gettail(p);
16250 }
16251 if (q > p && !mch_isFullName(buf))
16252 {
16253 /* symlink is relative to directory of argument */
16254 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16255 if (cpy != NULL)
16256 {
16257 STRCPY(cpy, p);
16258 STRCPY(gettail(cpy), buf);
16259 vim_free(p);
16260 p = cpy;
16261 }
16262 }
16263 else
16264 {
16265 vim_free(p);
16266 p = vim_strsave(buf);
16267 }
16268 }
16269
16270 if (remain == NULL)
16271 break;
16272
16273 /* Append the first path component of "remain" to "p". */
16274 q = getnextcomp(remain + 1);
16275 len = q - remain - (*q != NUL);
16276 cpy = vim_strnsave(p, STRLEN(p) + len);
16277 if (cpy != NULL)
16278 {
16279 STRNCAT(cpy, remain, len);
16280 vim_free(p);
16281 p = cpy;
16282 }
16283 /* Shorten "remain". */
16284 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016285 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286 else
16287 {
16288 vim_free(remain);
16289 remain = NULL;
16290 }
16291 }
16292
16293 /* If the result is a relative path name, make it explicitly relative to
16294 * the current directory if and only if the argument had this form. */
16295 if (!vim_ispathsep(*p))
16296 {
16297 if (is_relative_to_current
16298 && *p != NUL
16299 && !(p[0] == '.'
16300 && (p[1] == NUL
16301 || vim_ispathsep(p[1])
16302 || (p[1] == '.'
16303 && (p[2] == NUL
16304 || vim_ispathsep(p[2]))))))
16305 {
16306 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016307 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 if (cpy != NULL)
16309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 vim_free(p);
16311 p = cpy;
16312 }
16313 }
16314 else if (!is_relative_to_current)
16315 {
16316 /* Strip leading "./". */
16317 q = p;
16318 while (q[0] == '.' && vim_ispathsep(q[1]))
16319 q += 2;
16320 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016321 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322 }
16323 }
16324
16325 /* Ensure that the result will have no trailing path separator
16326 * if the argument had none. But keep "/" or "//". */
16327 if (!has_trailing_pathsep)
16328 {
16329 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016330 if (after_pathsep(p, q))
16331 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332 }
16333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016334 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335 }
16336# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016337 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338# endif
16339#endif
16340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016341 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342
16343#ifdef HAVE_READLINK
16344fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016345 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016347 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348}
16349
16350/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016351 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 */
16353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016354f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355{
Bram Moolenaar33570922005-01-25 22:26:29 +000016356 list_T *l;
16357 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358
Bram Moolenaar0d660222005-01-07 21:51:51 +000016359 if (argvars[0].v_type != VAR_LIST)
16360 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016361 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016362 && !tv_check_lock(l->lv_lock,
16363 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016364 {
16365 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016366 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016367 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016368 while (li != NULL)
16369 {
16370 ni = li->li_prev;
16371 list_append(l, li);
16372 li = ni;
16373 }
16374 rettv->vval.v_list = l;
16375 rettv->v_type = VAR_LIST;
16376 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016377 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379}
16380
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016381#define SP_NOMOVE 0x01 /* don't move cursor */
16382#define SP_REPEAT 0x02 /* repeat to find outer pair */
16383#define SP_RETCOUNT 0x04 /* return matchcount */
16384#define SP_SETPCMARK 0x08 /* set previous context mark */
16385#define SP_START 0x10 /* accept match at start position */
16386#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16387#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016388#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016389
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016390static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016391
16392/*
16393 * Get flags for a search function.
16394 * Possibly sets "p_ws".
16395 * Returns BACKWARD, FORWARD or zero (for an error).
16396 */
16397 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016398get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399{
16400 int dir = FORWARD;
16401 char_u *flags;
16402 char_u nbuf[NUMBUFLEN];
16403 int mask;
16404
16405 if (varp->v_type != VAR_UNKNOWN)
16406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016407 flags = get_tv_string_buf_chk(varp, nbuf);
16408 if (flags == NULL)
16409 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410 while (*flags != NUL)
16411 {
16412 switch (*flags)
16413 {
16414 case 'b': dir = BACKWARD; break;
16415 case 'w': p_ws = TRUE; break;
16416 case 'W': p_ws = FALSE; break;
16417 default: mask = 0;
16418 if (flagsp != NULL)
16419 switch (*flags)
16420 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016421 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016422 case 'e': mask = SP_END; break;
16423 case 'm': mask = SP_RETCOUNT; break;
16424 case 'n': mask = SP_NOMOVE; break;
16425 case 'p': mask = SP_SUBPAT; break;
16426 case 'r': mask = SP_REPEAT; break;
16427 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016428 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016429 }
16430 if (mask == 0)
16431 {
16432 EMSG2(_(e_invarg2), flags);
16433 dir = 0;
16434 }
16435 else
16436 *flagsp |= mask;
16437 }
16438 if (dir == 0)
16439 break;
16440 ++flags;
16441 }
16442 }
16443 return dir;
16444}
16445
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016447 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016449 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016450search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016452 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453 char_u *pat;
16454 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016455 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456 int save_p_ws = p_ws;
16457 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016458 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016459 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016460 proftime_T tm;
16461#ifdef FEAT_RELTIME
16462 long time_limit = 0;
16463#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016464 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016465 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016467 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016468 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016469 if (dir == 0)
16470 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016471 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016472 if (flags & SP_START)
16473 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016474 if (flags & SP_END)
16475 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016476 if (flags & SP_COLUMN)
16477 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016478
Bram Moolenaar76929292008-01-06 19:07:36 +000016479 /* Optional arguments: line number to stop searching and timeout. */
16480 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016481 {
16482 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16483 if (lnum_stop < 0)
16484 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016485#ifdef FEAT_RELTIME
16486 if (argvars[3].v_type != VAR_UNKNOWN)
16487 {
16488 time_limit = get_tv_number_chk(&argvars[3], NULL);
16489 if (time_limit < 0)
16490 goto theend;
16491 }
16492#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016493 }
16494
Bram Moolenaar76929292008-01-06 19:07:36 +000016495#ifdef FEAT_RELTIME
16496 /* Set the time limit, if there is one. */
16497 profile_setlimit(time_limit, &tm);
16498#endif
16499
Bram Moolenaar231334e2005-07-25 20:46:57 +000016500 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016501 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016502 * Check to make sure only those flags are set.
16503 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16504 * flags cannot be set. Check for that condition also.
16505 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016506 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016507 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016508 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016509 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016510 goto theend;
16511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016513 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016514 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016515 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016516 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016518 if (flags & SP_SUBPAT)
16519 retval = subpatnum;
16520 else
16521 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016522 if (flags & SP_SETPCMARK)
16523 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016525 if (match_pos != NULL)
16526 {
16527 /* Store the match cursor position */
16528 match_pos->lnum = pos.lnum;
16529 match_pos->col = pos.col + 1;
16530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 /* "/$" will put the cursor after the end of the line, may need to
16532 * correct that here */
16533 check_cursor();
16534 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016535
16536 /* If 'n' flag is used: restore cursor position. */
16537 if (flags & SP_NOMOVE)
16538 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016539 else
16540 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016541theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016543
16544 return retval;
16545}
16546
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016547#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016548
16549/*
16550 * round() is not in C90, use ceil() or floor() instead.
16551 */
16552 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016553vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016554{
16555 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16556}
16557
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016558/*
16559 * "round({float})" function
16560 */
16561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016562f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016563{
16564 float_T f;
16565
16566 rettv->v_type = VAR_FLOAT;
16567 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016568 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016569 else
16570 rettv->vval.v_float = 0.0;
16571}
16572#endif
16573
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016574/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016575 * "screenattr()" function
16576 */
16577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016578f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016579{
16580 int row;
16581 int col;
16582 int c;
16583
16584 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16585 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16586 if (row < 0 || row >= screen_Rows
16587 || col < 0 || col >= screen_Columns)
16588 c = -1;
16589 else
16590 c = ScreenAttrs[LineOffset[row] + col];
16591 rettv->vval.v_number = c;
16592}
16593
16594/*
16595 * "screenchar()" function
16596 */
16597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016598f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016599{
16600 int row;
16601 int col;
16602 int off;
16603 int c;
16604
16605 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16606 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16607 if (row < 0 || row >= screen_Rows
16608 || col < 0 || col >= screen_Columns)
16609 c = -1;
16610 else
16611 {
16612 off = LineOffset[row] + col;
16613#ifdef FEAT_MBYTE
16614 if (enc_utf8 && ScreenLinesUC[off] != 0)
16615 c = ScreenLinesUC[off];
16616 else
16617#endif
16618 c = ScreenLines[off];
16619 }
16620 rettv->vval.v_number = c;
16621}
16622
16623/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016624 * "screencol()" function
16625 *
16626 * First column is 1 to be consistent with virtcol().
16627 */
16628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016629f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016630{
16631 rettv->vval.v_number = screen_screencol() + 1;
16632}
16633
16634/*
16635 * "screenrow()" function
16636 */
16637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016638f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016639{
16640 rettv->vval.v_number = screen_screenrow() + 1;
16641}
16642
16643/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016644 * "search()" function
16645 */
16646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016647f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016648{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016649 int flags = 0;
16650
16651 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652}
16653
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016655 * "searchdecl()" function
16656 */
16657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016658f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016659{
16660 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016661 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016662 int error = FALSE;
16663 char_u *name;
16664
16665 rettv->vval.v_number = 1; /* default: FAIL */
16666
16667 name = get_tv_string_chk(&argvars[0]);
16668 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016669 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016670 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016671 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16672 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16673 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016674 if (!error && name != NULL)
16675 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016676 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016677}
16678
16679/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016680 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016682 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016683searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684{
16685 char_u *spat, *mpat, *epat;
16686 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688 int dir;
16689 int flags = 0;
16690 char_u nbuf1[NUMBUFLEN];
16691 char_u nbuf2[NUMBUFLEN];
16692 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016693 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016694 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016695 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016698 spat = get_tv_string_chk(&argvars[0]);
16699 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16700 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16701 if (spat == NULL || mpat == NULL || epat == NULL)
16702 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 /* Handle the optional fourth argument: flags */
16705 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016706 if (dir == 0)
16707 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016708
16709 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016710 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16711 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016712 if ((flags & (SP_END | SP_SUBPAT)) != 0
16713 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016714 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016715 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016716 goto theend;
16717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016719 /* Using 'r' implies 'W', otherwise it doesn't work. */
16720 if (flags & SP_REPEAT)
16721 p_ws = FALSE;
16722
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016723 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016724 if (argvars[3].v_type == VAR_UNKNOWN
16725 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726 skip = (char_u *)"";
16727 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016728 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016729 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016730 if (argvars[5].v_type != VAR_UNKNOWN)
16731 {
16732 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16733 if (lnum_stop < 0)
16734 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016735#ifdef FEAT_RELTIME
16736 if (argvars[6].v_type != VAR_UNKNOWN)
16737 {
16738 time_limit = get_tv_number_chk(&argvars[6], NULL);
16739 if (time_limit < 0)
16740 goto theend;
16741 }
16742#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016743 }
16744 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016745 if (skip == NULL)
16746 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016748 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016749 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016750
16751theend:
16752 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016753
16754 return retval;
16755}
16756
16757/*
16758 * "searchpair()" function
16759 */
16760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016761f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016762{
16763 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16764}
16765
16766/*
16767 * "searchpairpos()" function
16768 */
16769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016770f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016771{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016772 pos_T match_pos;
16773 int lnum = 0;
16774 int col = 0;
16775
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016776 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016777 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016778
16779 if (searchpair_cmn(argvars, &match_pos) > 0)
16780 {
16781 lnum = match_pos.lnum;
16782 col = match_pos.col;
16783 }
16784
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016785 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16786 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016787}
16788
16789/*
16790 * Search for a start/middle/end thing.
16791 * Used by searchpair(), see its documentation for the details.
16792 * Returns 0 or -1 for no match,
16793 */
16794 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010016795do_searchpair(
16796 char_u *spat, /* start pattern */
16797 char_u *mpat, /* middle pattern */
16798 char_u *epat, /* end pattern */
16799 int dir, /* BACKWARD or FORWARD */
16800 char_u *skip, /* skip expression */
16801 int flags, /* SP_SETPCMARK and other SP_ values */
16802 pos_T *match_pos,
16803 linenr_T lnum_stop, /* stop at this line if not zero */
16804 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016805{
16806 char_u *save_cpo;
16807 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16808 long retval = 0;
16809 pos_T pos;
16810 pos_T firstpos;
16811 pos_T foundpos;
16812 pos_T save_cursor;
16813 pos_T save_pos;
16814 int n;
16815 int r;
16816 int nest = 1;
16817 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016818 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016819 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016820
16821 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16822 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016823 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016824
Bram Moolenaar76929292008-01-06 19:07:36 +000016825#ifdef FEAT_RELTIME
16826 /* Set the time limit, if there is one. */
16827 profile_setlimit(time_limit, &tm);
16828#endif
16829
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016830 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16831 * start/middle/end (pat3, for the top pair). */
16832 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16833 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16834 if (pat2 == NULL || pat3 == NULL)
16835 goto theend;
16836 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16837 if (*mpat == NUL)
16838 STRCPY(pat3, pat2);
16839 else
16840 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16841 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016842 if (flags & SP_START)
16843 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016844
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845 save_cursor = curwin->w_cursor;
16846 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016847 clearpos(&firstpos);
16848 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 pat = pat3;
16850 for (;;)
16851 {
16852 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016853 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16855 /* didn't find it or found the first match again: FAIL */
16856 break;
16857
16858 if (firstpos.lnum == 0)
16859 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016860 if (equalpos(pos, foundpos))
16861 {
16862 /* Found the same position again. Can happen with a pattern that
16863 * has "\zs" at the end and searching backwards. Advance one
16864 * character and try again. */
16865 if (dir == BACKWARD)
16866 decl(&pos);
16867 else
16868 incl(&pos);
16869 }
16870 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016872 /* clear the start flag to avoid getting stuck here */
16873 options &= ~SEARCH_START;
16874
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 /* If the skip pattern matches, ignore this match. */
16876 if (*skip != NUL)
16877 {
16878 save_pos = curwin->w_cursor;
16879 curwin->w_cursor = pos;
16880 r = eval_to_bool(skip, &err, NULL, FALSE);
16881 curwin->w_cursor = save_pos;
16882 if (err)
16883 {
16884 /* Evaluating {skip} caused an error, break here. */
16885 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016886 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 break;
16888 }
16889 if (r)
16890 continue;
16891 }
16892
16893 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16894 {
16895 /* Found end when searching backwards or start when searching
16896 * forward: nested pair. */
16897 ++nest;
16898 pat = pat2; /* nested, don't search for middle */
16899 }
16900 else
16901 {
16902 /* Found end when searching forward or start when searching
16903 * backward: end of (nested) pair; or found middle in outer pair. */
16904 if (--nest == 1)
16905 pat = pat3; /* outer level, search for middle */
16906 }
16907
16908 if (nest == 0)
16909 {
16910 /* Found the match: return matchcount or line number. */
16911 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016912 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016914 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016915 if (flags & SP_SETPCMARK)
16916 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 curwin->w_cursor = pos;
16918 if (!(flags & SP_REPEAT))
16919 break;
16920 nest = 1; /* search for next unmatched */
16921 }
16922 }
16923
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016924 if (match_pos != NULL)
16925 {
16926 /* Store the match cursor position */
16927 match_pos->lnum = curwin->w_cursor.lnum;
16928 match_pos->col = curwin->w_cursor.col + 1;
16929 }
16930
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016932 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 curwin->w_cursor = save_cursor;
16934
16935theend:
16936 vim_free(pat2);
16937 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016938 if (p_cpo == empty_option)
16939 p_cpo = save_cpo;
16940 else
16941 /* Darn, evaluating the {skip} expression changed the value. */
16942 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016943
16944 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016945}
16946
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016947/*
16948 * "searchpos()" function
16949 */
16950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016951f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016952{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016953 pos_T match_pos;
16954 int lnum = 0;
16955 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016956 int n;
16957 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016958
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016959 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016960 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016961
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016962 n = search_cmn(argvars, &match_pos, &flags);
16963 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016964 {
16965 lnum = match_pos.lnum;
16966 col = match_pos.col;
16967 }
16968
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016969 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16970 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016971 if (flags & SP_SUBPAT)
16972 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016973}
16974
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016976f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978#ifdef FEAT_CLIENTSERVER
16979 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016980 char_u *server = get_tv_string_chk(&argvars[0]);
16981 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016984 if (server == NULL || reply == NULL)
16985 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986 if (check_restricted() || check_secure())
16987 return;
16988# ifdef FEAT_X11
16989 if (check_connection() == FAIL)
16990 return;
16991# endif
16992
16993 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016995 EMSG(_("E258: Unable to send to client"));
16996 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998 rettv->vval.v_number = 0;
16999#else
17000 rettv->vval.v_number = -1;
17001#endif
17002}
17003
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017005f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006{
17007 char_u *r = NULL;
17008
17009#ifdef FEAT_CLIENTSERVER
17010# ifdef WIN32
17011 r = serverGetVimNames();
17012# else
17013 make_connection();
17014 if (X_DISPLAY != NULL)
17015 r = serverGetVimNames(X_DISPLAY);
17016# endif
17017#endif
17018 rettv->v_type = VAR_STRING;
17019 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020}
17021
17022/*
17023 * "setbufvar()" function
17024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017026f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027{
17028 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017031 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032 char_u nbuf[NUMBUFLEN];
17033
17034 if (check_restricted() || check_secure())
17035 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017036 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17037 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017038 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 varp = &argvars[2];
17040
17041 if (buf != NULL && varname != NULL && varp != NULL)
17042 {
17043 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045
17046 if (*varname == '&')
17047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017048 long numval;
17049 char_u *strval;
17050 int error = FALSE;
17051
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017053 numval = get_tv_number_chk(varp, &error);
17054 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017055 if (!error && strval != NULL)
17056 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057 }
17058 else
17059 {
17060 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17061 if (bufvarname != NULL)
17062 {
17063 STRCPY(bufvarname, "b:");
17064 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017065 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 vim_free(bufvarname);
17067 }
17068 }
17069
17070 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073}
17074
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017076f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017077{
17078 dict_T *d;
17079 dictitem_T *di;
17080 char_u *csearch;
17081
17082 if (argvars[0].v_type != VAR_DICT)
17083 {
17084 EMSG(_(e_dictreq));
17085 return;
17086 }
17087
17088 if ((d = argvars[0].vval.v_dict) != NULL)
17089 {
17090 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17091 if (csearch != NULL)
17092 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017093#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017094 if (enc_utf8)
17095 {
17096 int pcc[MAX_MCO];
17097 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017098
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017099 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17100 }
17101 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017102#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017103 set_last_csearch(PTR2CHAR(csearch),
17104 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017105 }
17106
17107 di = dict_find(d, (char_u *)"forward", -1);
17108 if (di != NULL)
17109 set_csearch_direction(get_tv_number(&di->di_tv)
17110 ? FORWARD : BACKWARD);
17111
17112 di = dict_find(d, (char_u *)"until", -1);
17113 if (di != NULL)
17114 set_csearch_until(!!get_tv_number(&di->di_tv));
17115 }
17116}
17117
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118/*
17119 * "setcmdpos()" function
17120 */
17121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017122f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017124 int pos = (int)get_tv_number(&argvars[0]) - 1;
17125
17126 if (pos >= 0)
17127 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128}
17129
17130/*
17131 * "setline()" function
17132 */
17133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017134f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135{
17136 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017137 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017138 list_T *l = NULL;
17139 listitem_T *li = NULL;
17140 long added = 0;
17141 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017143 lnum = get_tv_lnum(&argvars[0]);
17144 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017146 l = argvars[1].vval.v_list;
17147 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017149 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017150 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017151
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017152 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017153 for (;;)
17154 {
17155 if (l != NULL)
17156 {
17157 /* list argument, get next string */
17158 if (li == NULL)
17159 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017160 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017161 li = li->li_next;
17162 }
17163
17164 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017165 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017166 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017167
17168 /* When coming here from Insert mode, sync undo, so that this can be
17169 * undone separately from what was previously inserted. */
17170 if (u_sync_once == 2)
17171 {
17172 u_sync_once = 1; /* notify that u_sync() was called */
17173 u_sync(TRUE);
17174 }
17175
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017176 if (lnum <= curbuf->b_ml.ml_line_count)
17177 {
17178 /* existing line, replace it */
17179 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17180 {
17181 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017182 if (lnum == curwin->w_cursor.lnum)
17183 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017184 rettv->vval.v_number = 0; /* OK */
17185 }
17186 }
17187 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17188 {
17189 /* lnum is one past the last line, append the line */
17190 ++added;
17191 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17192 rettv->vval.v_number = 0; /* OK */
17193 }
17194
17195 if (l == NULL) /* only one string argument */
17196 break;
17197 ++lnum;
17198 }
17199
17200 if (added > 0)
17201 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202}
17203
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017204static void set_qf_ll_list(win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv);
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017205
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017207 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017208 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017210set_qf_ll_list(
17211 win_T *wp UNUSED,
17212 typval_T *list_arg UNUSED,
17213 typval_T *action_arg UNUSED,
17214 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017215{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017216#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017217 char_u *act;
17218 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017219#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017220
Bram Moolenaar2641f772005-03-25 21:58:17 +000017221 rettv->vval.v_number = -1;
17222
17223#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017224 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017225 EMSG(_(e_listreq));
17226 else
17227 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017228 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017229
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017230 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017231 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017232 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017233 if (act == NULL)
17234 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017235 if (*act == 'a' || *act == 'r')
17236 action = *act;
17237 }
17238
Bram Moolenaar81484f42012-12-05 15:16:47 +010017239 if (l != NULL && set_errorlist(wp, l, action,
17240 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017241 rettv->vval.v_number = 0;
17242 }
17243#endif
17244}
17245
17246/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017247 * "setloclist()" function
17248 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017250f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017251{
17252 win_T *win;
17253
17254 rettv->vval.v_number = -1;
17255
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017256 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017257 if (win != NULL)
17258 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17259}
17260
17261/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017262 * "setmatches()" function
17263 */
17264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017265f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017266{
17267#ifdef FEAT_SEARCH_EXTRA
17268 list_T *l;
17269 listitem_T *li;
17270 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017271 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017272
17273 rettv->vval.v_number = -1;
17274 if (argvars[0].v_type != VAR_LIST)
17275 {
17276 EMSG(_(e_listreq));
17277 return;
17278 }
17279 if ((l = argvars[0].vval.v_list) != NULL)
17280 {
17281
17282 /* To some extent make sure that we are dealing with a list from
17283 * "getmatches()". */
17284 li = l->lv_first;
17285 while (li != NULL)
17286 {
17287 if (li->li_tv.v_type != VAR_DICT
17288 || (d = li->li_tv.vval.v_dict) == NULL)
17289 {
17290 EMSG(_(e_invarg));
17291 return;
17292 }
17293 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017294 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17295 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017296 && dict_find(d, (char_u *)"priority", -1) != NULL
17297 && dict_find(d, (char_u *)"id", -1) != NULL))
17298 {
17299 EMSG(_(e_invarg));
17300 return;
17301 }
17302 li = li->li_next;
17303 }
17304
17305 clear_matches(curwin);
17306 li = l->lv_first;
17307 while (li != NULL)
17308 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017309 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017310 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017311 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017312 char_u *group;
17313 int priority;
17314 int id;
17315 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017316
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017317 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017318 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17319 {
17320 if (s == NULL)
17321 {
17322 s = list_alloc();
17323 if (s == NULL)
17324 return;
17325 }
17326
17327 /* match from matchaddpos() */
17328 for (i = 1; i < 9; i++)
17329 {
17330 sprintf((char *)buf, (char *)"pos%d", i);
17331 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17332 {
17333 if (di->di_tv.v_type != VAR_LIST)
17334 return;
17335
17336 list_append_tv(s, &di->di_tv);
17337 s->lv_refcount++;
17338 }
17339 else
17340 break;
17341 }
17342 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017343
17344 group = get_dict_string(d, (char_u *)"group", FALSE);
17345 priority = (int)get_dict_number(d, (char_u *)"priority");
17346 id = (int)get_dict_number(d, (char_u *)"id");
17347 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17348 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17349 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017350 if (i == 0)
17351 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017352 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017353 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017354 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017355 }
17356 else
17357 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017358 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017359 list_unref(s);
17360 s = NULL;
17361 }
17362
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017363 li = li->li_next;
17364 }
17365 rettv->vval.v_number = 0;
17366 }
17367#endif
17368}
17369
17370/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017371 * "setpos()" function
17372 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017374f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017375{
17376 pos_T pos;
17377 int fnum;
17378 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017379 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017380
Bram Moolenaar08250432008-02-13 11:42:46 +000017381 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017382 name = get_tv_string_chk(argvars);
17383 if (name != NULL)
17384 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017385 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017386 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017387 if (--pos.col < 0)
17388 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017389 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017390 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017391 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017392 if (fnum == curbuf->b_fnum)
17393 {
17394 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017395 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017396 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017397 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017398 curwin->w_set_curswant = FALSE;
17399 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017400 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017401 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017402 }
17403 else
17404 EMSG(_(e_invarg));
17405 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017406 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17407 {
17408 /* set mark */
17409 if (setmark_pos(name[1], &pos, fnum) == OK)
17410 rettv->vval.v_number = 0;
17411 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017412 else
17413 EMSG(_(e_invarg));
17414 }
17415 }
17416}
17417
17418/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017419 * "setqflist()" function
17420 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017422f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017423{
17424 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17425}
17426
17427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 * "setreg()" function
17429 */
17430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017431f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432{
17433 int regname;
17434 char_u *strregname;
17435 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017436 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437 int append;
17438 char_u yank_type;
17439 long block_len;
17440
17441 block_len = -1;
17442 yank_type = MAUTO;
17443 append = FALSE;
17444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017445 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017446 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017448 if (strregname == NULL)
17449 return; /* type error; errmsg already given */
17450 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 if (regname == 0 || regname == '@')
17452 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017454 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017456 stropt = get_tv_string_chk(&argvars[2]);
17457 if (stropt == NULL)
17458 return; /* type error */
17459 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460 switch (*stropt)
17461 {
17462 case 'a': case 'A': /* append */
17463 append = TRUE;
17464 break;
17465 case 'v': case 'c': /* character-wise selection */
17466 yank_type = MCHAR;
17467 break;
17468 case 'V': case 'l': /* line-wise selection */
17469 yank_type = MLINE;
17470 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 case 'b': case Ctrl_V: /* block-wise selection */
17472 yank_type = MBLOCK;
17473 if (VIM_ISDIGIT(stropt[1]))
17474 {
17475 ++stropt;
17476 block_len = getdigits(&stropt) - 1;
17477 --stropt;
17478 }
17479 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 }
17481 }
17482
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017483 if (argvars[1].v_type == VAR_LIST)
17484 {
17485 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017486 char_u **allocval;
17487 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017488 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017489 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017490 int len = argvars[1].vval.v_list->lv_len;
17491 listitem_T *li;
17492
Bram Moolenaar7d647822014-04-05 21:28:56 +020017493 /* First half: use for pointers to result lines; second half: use for
17494 * pointers to allocated copies. */
17495 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017496 if (lstval == NULL)
17497 return;
17498 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017499 allocval = lstval + len + 2;
17500 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017501
17502 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17503 li = li->li_next)
17504 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017505 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017506 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017507 goto free_lstval;
17508 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017509 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017510 /* Need to make a copy, next get_tv_string_buf_chk() will
17511 * overwrite the string. */
17512 strval = vim_strsave(buf);
17513 if (strval == NULL)
17514 goto free_lstval;
17515 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017516 }
17517 *curval++ = strval;
17518 }
17519 *curval++ = NULL;
17520
17521 write_reg_contents_lst(regname, lstval, -1,
17522 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017523free_lstval:
17524 while (curallocval > allocval)
17525 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017526 vim_free(lstval);
17527 }
17528 else
17529 {
17530 strval = get_tv_string_chk(&argvars[1]);
17531 if (strval == NULL)
17532 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017533 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017535 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017536 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537}
17538
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017539/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017540 * "settabvar()" function
17541 */
17542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017543f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017544{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017545#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017546 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017547 tabpage_T *tp;
17548#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017549 char_u *varname, *tabvarname;
17550 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017551
17552 rettv->vval.v_number = 0;
17553
17554 if (check_restricted() || check_secure())
17555 return;
17556
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017557#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017558 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017559#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017560 varname = get_tv_string_chk(&argvars[1]);
17561 varp = &argvars[2];
17562
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017563 if (varname != NULL && varp != NULL
17564#ifdef FEAT_WINDOWS
17565 && tp != NULL
17566#endif
17567 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017568 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017569#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017570 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017571 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017572#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017573
17574 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17575 if (tabvarname != NULL)
17576 {
17577 STRCPY(tabvarname, "t:");
17578 STRCPY(tabvarname + 2, varname);
17579 set_var(tabvarname, varp, TRUE);
17580 vim_free(tabvarname);
17581 }
17582
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017583#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017584 /* Restore current tabpage */
17585 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017586 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017587#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017588 }
17589}
17590
17591/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017592 * "settabwinvar()" function
17593 */
17594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017595f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017596{
17597 setwinvar(argvars, rettv, 1);
17598}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599
17600/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017601 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017604f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017606 setwinvar(argvars, rettv, 0);
17607}
17608
17609/*
17610 * "setwinvar()" and "settabwinvar()" functions
17611 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017612
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017614setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017615{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616 win_T *win;
17617#ifdef FEAT_WINDOWS
17618 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017619 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017620 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621#endif
17622 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017623 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017625 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626
17627 if (check_restricted() || check_secure())
17628 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017629
17630#ifdef FEAT_WINDOWS
17631 if (off == 1)
17632 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17633 else
17634 tp = curtab;
17635#endif
17636 win = find_win_by_nr(&argvars[off], tp);
17637 varname = get_tv_string_chk(&argvars[off + 1]);
17638 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639
17640 if (win != NULL && varname != NULL && varp != NULL)
17641 {
17642#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017643 need_switch_win = !(tp == curtab && win == curwin);
17644 if (!need_switch_win
17645 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017648 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017650 long numval;
17651 char_u *strval;
17652 int error = FALSE;
17653
17654 ++varname;
17655 numval = get_tv_number_chk(varp, &error);
17656 strval = get_tv_string_buf_chk(varp, nbuf);
17657 if (!error && strval != NULL)
17658 set_option_value(varname, numval, strval, OPT_LOCAL);
17659 }
17660 else
17661 {
17662 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17663 if (winvarname != NULL)
17664 {
17665 STRCPY(winvarname, "w:");
17666 STRCPY(winvarname + 2, varname);
17667 set_var(winvarname, varp, TRUE);
17668 vim_free(winvarname);
17669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 }
17671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017673 if (need_switch_win)
17674 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675#endif
17676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677}
17678
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017679#ifdef FEAT_CRYPT
17680/*
17681 * "sha256({string})" function
17682 */
17683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017684f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017685{
17686 char_u *p;
17687
17688 p = get_tv_string(&argvars[0]);
17689 rettv->vval.v_string = vim_strsave(
17690 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17691 rettv->v_type = VAR_STRING;
17692}
17693#endif /* FEAT_CRYPT */
17694
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017696 * "shellescape({string})" function
17697 */
17698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017699f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017700{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017701 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017702 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017703 rettv->v_type = VAR_STRING;
17704}
17705
17706/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017707 * shiftwidth() function
17708 */
17709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017710f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017711{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017712 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017713}
17714
17715/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017716 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 */
17718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017719f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017721 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722
Bram Moolenaar0d660222005-01-07 21:51:51 +000017723 p = get_tv_string(&argvars[0]);
17724 rettv->vval.v_string = vim_strsave(p);
17725 simplify_filename(rettv->vval.v_string); /* simplify in place */
17726 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727}
17728
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017729#ifdef FEAT_FLOAT
17730/*
17731 * "sin()" function
17732 */
17733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017734f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017735{
17736 float_T f;
17737
17738 rettv->v_type = VAR_FLOAT;
17739 if (get_float_arg(argvars, &f) == OK)
17740 rettv->vval.v_float = sin(f);
17741 else
17742 rettv->vval.v_float = 0.0;
17743}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017744
17745/*
17746 * "sinh()" function
17747 */
17748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017749f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017750{
17751 float_T f;
17752
17753 rettv->v_type = VAR_FLOAT;
17754 if (get_float_arg(argvars, &f) == OK)
17755 rettv->vval.v_float = sinh(f);
17756 else
17757 rettv->vval.v_float = 0.0;
17758}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017759#endif
17760
Bram Moolenaar0d660222005-01-07 21:51:51 +000017761static int
17762#ifdef __BORLANDC__
17763 _RTLENTRYF
17764#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017765 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017766static int
17767#ifdef __BORLANDC__
17768 _RTLENTRYF
17769#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017770 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017771
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017772/* struct used in the array that's given to qsort() */
17773typedef struct
17774{
17775 listitem_T *item;
17776 int idx;
17777} sortItem_T;
17778
Bram Moolenaar0d660222005-01-07 21:51:51 +000017779static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017780static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017781static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017782#ifdef FEAT_FLOAT
17783static int item_compare_float;
17784#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017785static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017786static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017787static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017788static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017789static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017790#define ITEM_COMPARE_FAIL 999
17791
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017793 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017795 static int
17796#ifdef __BORLANDC__
17797_RTLENTRYF
17798#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017799item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017801 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017802 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017803 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017804 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017805 int res;
17806 char_u numbuf1[NUMBUFLEN];
17807 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017809 si1 = (sortItem_T *)s1;
17810 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017811 tv1 = &si1->item->li_tv;
17812 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017813
17814 if (item_compare_numbers)
17815 {
17816 long v1 = get_tv_number(tv1);
17817 long v2 = get_tv_number(tv2);
17818
17819 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17820 }
17821
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017822#ifdef FEAT_FLOAT
17823 if (item_compare_float)
17824 {
17825 float_T v1 = get_tv_float(tv1);
17826 float_T v2 = get_tv_float(tv2);
17827
17828 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
17829 }
17830#endif
17831
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017832 /* tv2string() puts quotes around a string and allocates memory. Don't do
17833 * that for string variables. Use a single quote when comparing with a
17834 * non-string to do what the docs promise. */
17835 if (tv1->v_type == VAR_STRING)
17836 {
17837 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17838 p1 = (char_u *)"'";
17839 else
17840 p1 = tv1->vval.v_string;
17841 }
17842 else
17843 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17844 if (tv2->v_type == VAR_STRING)
17845 {
17846 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17847 p2 = (char_u *)"'";
17848 else
17849 p2 = tv2->vval.v_string;
17850 }
17851 else
17852 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017853 if (p1 == NULL)
17854 p1 = (char_u *)"";
17855 if (p2 == NULL)
17856 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017857 if (!item_compare_numeric)
17858 {
17859 if (item_compare_ic)
17860 res = STRICMP(p1, p2);
17861 else
17862 res = STRCMP(p1, p2);
17863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017865 {
17866 double n1, n2;
17867 n1 = strtod((char *)p1, (char **)&p1);
17868 n2 = strtod((char *)p2, (char **)&p2);
17869 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17870 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017871
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017872 /* When the result would be zero, compare the item indexes. Makes the
17873 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017874 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017875 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017876
Bram Moolenaar0d660222005-01-07 21:51:51 +000017877 vim_free(tofree1);
17878 vim_free(tofree2);
17879 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880}
17881
17882 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017883#ifdef __BORLANDC__
17884_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010017886item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017887{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017888 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017889 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017890 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017891 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017892 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017894 /* shortcut after failure in previous call; compare all items equal */
17895 if (item_compare_func_err)
17896 return 0;
17897
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017898 si1 = (sortItem_T *)s1;
17899 si2 = (sortItem_T *)s2;
17900
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017901 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017902 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017903 copy_tv(&si1->item->li_tv, &argv[0]);
17904 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017905
17906 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017907 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017908 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17909 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017910 clear_tv(&argv[0]);
17911 clear_tv(&argv[1]);
17912
17913 if (res == FAIL)
17914 res = ITEM_COMPARE_FAIL;
17915 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017916 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17917 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017918 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017919 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017920
17921 /* When the result would be zero, compare the pointers themselves. Makes
17922 * the sort stable. */
17923 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017924 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017925
Bram Moolenaar0d660222005-01-07 21:51:51 +000017926 return res;
17927}
17928
17929/*
17930 * "sort({list})" function
17931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017933do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934{
Bram Moolenaar33570922005-01-25 22:26:29 +000017935 list_T *l;
17936 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017937 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017938 long len;
17939 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940
Bram Moolenaar0d660222005-01-07 21:51:51 +000017941 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017942 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 else
17944 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017945 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017946 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017947 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17948 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017949 return;
17950 rettv->vval.v_list = l;
17951 rettv->v_type = VAR_LIST;
17952 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953
Bram Moolenaar0d660222005-01-07 21:51:51 +000017954 len = list_len(l);
17955 if (len <= 1)
17956 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957
Bram Moolenaar0d660222005-01-07 21:51:51 +000017958 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017959 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017960 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017961#ifdef FEAT_FLOAT
17962 item_compare_float = FALSE;
17963#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017964 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017965 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017966 if (argvars[1].v_type != VAR_UNKNOWN)
17967 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017968 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017969 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017970 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017971 else
17972 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017973 int error = FALSE;
17974
17975 i = get_tv_number_chk(&argvars[1], &error);
17976 if (error)
17977 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017978 if (i == 1)
17979 item_compare_ic = TRUE;
17980 else
17981 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017982 if (item_compare_func != NULL)
17983 {
17984 if (STRCMP(item_compare_func, "n") == 0)
17985 {
17986 item_compare_func = NULL;
17987 item_compare_numeric = TRUE;
17988 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017989 else if (STRCMP(item_compare_func, "N") == 0)
17990 {
17991 item_compare_func = NULL;
17992 item_compare_numbers = TRUE;
17993 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010017994#ifdef FEAT_FLOAT
17995 else if (STRCMP(item_compare_func, "f") == 0)
17996 {
17997 item_compare_func = NULL;
17998 item_compare_float = TRUE;
17999 }
18000#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018001 else if (STRCMP(item_compare_func, "i") == 0)
18002 {
18003 item_compare_func = NULL;
18004 item_compare_ic = TRUE;
18005 }
18006 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018007 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018008
18009 if (argvars[2].v_type != VAR_UNKNOWN)
18010 {
18011 /* optional third argument: {dict} */
18012 if (argvars[2].v_type != VAR_DICT)
18013 {
18014 EMSG(_(e_dictreq));
18015 return;
18016 }
18017 item_compare_selfdict = argvars[2].vval.v_dict;
18018 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020
Bram Moolenaar0d660222005-01-07 21:51:51 +000018021 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018022 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018023 if (ptrs == NULL)
18024 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025
Bram Moolenaar327aa022014-03-25 18:24:23 +010018026 i = 0;
18027 if (sort)
18028 {
18029 /* sort(): ptrs will be the list to sort */
18030 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018031 {
18032 ptrs[i].item = li;
18033 ptrs[i].idx = i;
18034 ++i;
18035 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018036
18037 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018038 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018039 /* test the compare function */
18040 if (item_compare_func != NULL
18041 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018042 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018043 EMSG(_("E702: Sort compare function failed"));
18044 else
18045 {
18046 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018047 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018048 item_compare_func == NULL ? item_compare : item_compare2);
18049
18050 if (!item_compare_func_err)
18051 {
18052 /* Clear the List and append the items in sorted order. */
18053 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18054 l->lv_len = 0;
18055 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018056 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018057 }
18058 }
18059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018061 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018062 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018063
18064 /* f_uniq(): ptrs will be a stack of items to remove */
18065 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018066 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018067 item_compare_func_ptr = item_compare_func
18068 ? item_compare2 : item_compare;
18069
18070 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18071 li = li->li_next)
18072 {
18073 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18074 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018075 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018076 if (item_compare_func_err)
18077 {
18078 EMSG(_("E882: Uniq compare function failed"));
18079 break;
18080 }
18081 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018083 if (!item_compare_func_err)
18084 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018085 while (--i >= 0)
18086 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018087 li = ptrs[i].item->li_next;
18088 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018089 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018090 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018091 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018092 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018093 list_fix_watch(l, li);
18094 listitem_free(li);
18095 l->lv_len--;
18096 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018097 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018098 }
18099
18100 vim_free(ptrs);
18101 }
18102}
18103
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018104/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018105 * "sort({list})" function
18106 */
18107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018108f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018109{
18110 do_sort_uniq(argvars, rettv, TRUE);
18111}
18112
18113/*
18114 * "uniq({list})" function
18115 */
18116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018117f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018118{
18119 do_sort_uniq(argvars, rettv, FALSE);
18120}
18121
18122/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018123 * "soundfold({word})" function
18124 */
18125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018126f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018127{
18128 char_u *s;
18129
18130 rettv->v_type = VAR_STRING;
18131 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018132#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018133 rettv->vval.v_string = eval_soundfold(s);
18134#else
18135 rettv->vval.v_string = vim_strsave(s);
18136#endif
18137}
18138
18139/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018140 * "spellbadword()" function
18141 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018143f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018144{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018145 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018146 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018147 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018148
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018149 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018150 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018151
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018152#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018153 if (argvars[0].v_type == VAR_UNKNOWN)
18154 {
18155 /* Find the start and length of the badly spelled word. */
18156 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18157 if (len != 0)
18158 word = ml_get_cursor();
18159 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018160 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018161 {
18162 char_u *str = get_tv_string_chk(&argvars[0]);
18163 int capcol = -1;
18164
18165 if (str != NULL)
18166 {
18167 /* Check the argument for spelling. */
18168 while (*str != NUL)
18169 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018170 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018171 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018172 {
18173 word = str;
18174 break;
18175 }
18176 str += len;
18177 }
18178 }
18179 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018180#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018181
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018182 list_append_string(rettv->vval.v_list, word, len);
18183 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018184 attr == HLF_SPB ? "bad" :
18185 attr == HLF_SPR ? "rare" :
18186 attr == HLF_SPL ? "local" :
18187 attr == HLF_SPC ? "caps" :
18188 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018189}
18190
18191/*
18192 * "spellsuggest()" function
18193 */
18194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018195f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018196{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018197#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018198 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018199 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018200 int maxcount;
18201 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018202 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018203 listitem_T *li;
18204 int need_capital = FALSE;
18205#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018206
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018207 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018208 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018209
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018210#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018211 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018212 {
18213 str = get_tv_string(&argvars[0]);
18214 if (argvars[1].v_type != VAR_UNKNOWN)
18215 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018216 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018217 if (maxcount <= 0)
18218 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018219 if (argvars[2].v_type != VAR_UNKNOWN)
18220 {
18221 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18222 if (typeerr)
18223 return;
18224 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018225 }
18226 else
18227 maxcount = 25;
18228
Bram Moolenaar4770d092006-01-12 23:22:24 +000018229 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018230
18231 for (i = 0; i < ga.ga_len; ++i)
18232 {
18233 str = ((char_u **)ga.ga_data)[i];
18234
18235 li = listitem_alloc();
18236 if (li == NULL)
18237 vim_free(str);
18238 else
18239 {
18240 li->li_tv.v_type = VAR_STRING;
18241 li->li_tv.v_lock = 0;
18242 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018243 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018244 }
18245 }
18246 ga_clear(&ga);
18247 }
18248#endif
18249}
18250
Bram Moolenaar0d660222005-01-07 21:51:51 +000018251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018252f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018253{
18254 char_u *str;
18255 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018256 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018257 regmatch_T regmatch;
18258 char_u patbuf[NUMBUFLEN];
18259 char_u *save_cpo;
18260 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018261 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018262 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018263 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264
18265 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18266 save_cpo = p_cpo;
18267 p_cpo = (char_u *)"";
18268
18269 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018270 if (argvars[1].v_type != VAR_UNKNOWN)
18271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018272 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18273 if (pat == NULL)
18274 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018275 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018276 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018277 }
18278 if (pat == NULL || *pat == NUL)
18279 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018280
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018281 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018283 if (typeerr)
18284 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018285
Bram Moolenaar0d660222005-01-07 21:51:51 +000018286 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18287 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018289 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018290 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018291 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018292 if (*str == NUL)
18293 match = FALSE; /* empty item at the end */
18294 else
18295 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018296 if (match)
18297 end = regmatch.startp[0];
18298 else
18299 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018300 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18301 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018302 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018303 if (list_append_string(rettv->vval.v_list, str,
18304 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018305 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018306 }
18307 if (!match)
18308 break;
18309 /* Advance to just after the match. */
18310 if (regmatch.endp[0] > str)
18311 col = 0;
18312 else
18313 {
18314 /* Don't get stuck at the same match. */
18315#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018316 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018317#else
18318 col = 1;
18319#endif
18320 }
18321 str = regmatch.endp[0];
18322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323
Bram Moolenaar473de612013-06-08 18:19:48 +020018324 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326
Bram Moolenaar0d660222005-01-07 21:51:51 +000018327 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328}
18329
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018330#ifdef FEAT_FLOAT
18331/*
18332 * "sqrt()" function
18333 */
18334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018335f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018336{
18337 float_T f;
18338
18339 rettv->v_type = VAR_FLOAT;
18340 if (get_float_arg(argvars, &f) == OK)
18341 rettv->vval.v_float = sqrt(f);
18342 else
18343 rettv->vval.v_float = 0.0;
18344}
18345
18346/*
18347 * "str2float()" function
18348 */
18349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018350f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018351{
18352 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18353
18354 if (*p == '+')
18355 p = skipwhite(p + 1);
18356 (void)string2float(p, &rettv->vval.v_float);
18357 rettv->v_type = VAR_FLOAT;
18358}
18359#endif
18360
Bram Moolenaar2c932302006-03-18 21:42:09 +000018361/*
18362 * "str2nr()" function
18363 */
18364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018365f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018366{
18367 int base = 10;
18368 char_u *p;
18369 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018370 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018371
18372 if (argvars[1].v_type != VAR_UNKNOWN)
18373 {
18374 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018375 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018376 {
18377 EMSG(_(e_invarg));
18378 return;
18379 }
18380 }
18381
18382 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018383 if (*p == '+')
18384 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018385 switch (base)
18386 {
18387 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18388 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18389 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18390 default: what = 0;
18391 }
18392 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018393 rettv->vval.v_number = n;
18394}
18395
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396#ifdef HAVE_STRFTIME
18397/*
18398 * "strftime({format}[, {time}])" function
18399 */
18400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018401f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402{
18403 char_u result_buf[256];
18404 struct tm *curtime;
18405 time_t seconds;
18406 char_u *p;
18407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018408 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018410 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018411 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412 seconds = time(NULL);
18413 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018414 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415 curtime = localtime(&seconds);
18416 /* MSVC returns NULL for an invalid value of seconds. */
18417 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018418 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 else
18420 {
18421# ifdef FEAT_MBYTE
18422 vimconv_T conv;
18423 char_u *enc;
18424
18425 conv.vc_type = CONV_NONE;
18426 enc = enc_locale();
18427 convert_setup(&conv, p_enc, enc);
18428 if (conv.vc_type != CONV_NONE)
18429 p = string_convert(&conv, p, NULL);
18430# endif
18431 if (p != NULL)
18432 (void)strftime((char *)result_buf, sizeof(result_buf),
18433 (char *)p, curtime);
18434 else
18435 result_buf[0] = NUL;
18436
18437# ifdef FEAT_MBYTE
18438 if (conv.vc_type != CONV_NONE)
18439 vim_free(p);
18440 convert_setup(&conv, enc, p_enc);
18441 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018442 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443 else
18444# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018445 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446
18447# ifdef FEAT_MBYTE
18448 /* Release conversion descriptors */
18449 convert_setup(&conv, NULL, NULL);
18450 vim_free(enc);
18451# endif
18452 }
18453}
18454#endif
18455
18456/*
18457 * "stridx()" function
18458 */
18459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018460f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461{
18462 char_u buf[NUMBUFLEN];
18463 char_u *needle;
18464 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018465 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018467 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018469 needle = get_tv_string_chk(&argvars[1]);
18470 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018471 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018472 if (needle == NULL || haystack == NULL)
18473 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474
Bram Moolenaar33570922005-01-25 22:26:29 +000018475 if (argvars[2].v_type != VAR_UNKNOWN)
18476 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018477 int error = FALSE;
18478
18479 start_idx = get_tv_number_chk(&argvars[2], &error);
18480 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018481 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018482 if (start_idx >= 0)
18483 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018484 }
18485
18486 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18487 if (pos != NULL)
18488 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489}
18490
18491/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018492 * "string()" function
18493 */
18494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018495f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018496{
18497 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018498 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018499
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018500 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018501 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018502 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018503 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018504 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505}
18506
18507/*
18508 * "strlen()" function
18509 */
18510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018511f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018513 rettv->vval.v_number = (varnumber_T)(STRLEN(
18514 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515}
18516
18517/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018518 * "strchars()" function
18519 */
18520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018521f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018522{
18523 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018524 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018525#ifdef FEAT_MBYTE
18526 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018527 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018528#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018529
18530 if (argvars[1].v_type != VAR_UNKNOWN)
18531 skipcc = get_tv_number_chk(&argvars[1], NULL);
18532 if (skipcc < 0 || skipcc > 1)
18533 EMSG(_(e_invarg));
18534 else
18535 {
18536#ifdef FEAT_MBYTE
18537 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18538 while (*s != NUL)
18539 {
18540 func_mb_ptr2char_adv(&s);
18541 ++len;
18542 }
18543 rettv->vval.v_number = len;
18544#else
18545 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18546#endif
18547 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018548}
18549
18550/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018551 * "strdisplaywidth()" function
18552 */
18553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018554f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018555{
18556 char_u *s = get_tv_string(&argvars[0]);
18557 int col = 0;
18558
18559 if (argvars[1].v_type != VAR_UNKNOWN)
18560 col = get_tv_number(&argvars[1]);
18561
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018562 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018563}
18564
18565/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018566 * "strwidth()" function
18567 */
18568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018569f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018570{
18571 char_u *s = get_tv_string(&argvars[0]);
18572
18573 rettv->vval.v_number = (varnumber_T)(
18574#ifdef FEAT_MBYTE
18575 mb_string2cells(s, -1)
18576#else
18577 STRLEN(s)
18578#endif
18579 );
18580}
18581
18582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583 * "strpart()" function
18584 */
18585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018586f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587{
18588 char_u *p;
18589 int n;
18590 int len;
18591 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018592 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 slen = (int)STRLEN(p);
18596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018597 n = get_tv_number_chk(&argvars[1], &error);
18598 if (error)
18599 len = 0;
18600 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018601 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 else
18603 len = slen - n; /* default len: all bytes that are available. */
18604
18605 /*
18606 * Only return the overlap between the specified part and the actual
18607 * string.
18608 */
18609 if (n < 0)
18610 {
18611 len += n;
18612 n = 0;
18613 }
18614 else if (n > slen)
18615 n = slen;
18616 if (len < 0)
18617 len = 0;
18618 else if (n + len > slen)
18619 len = slen - n;
18620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018621 rettv->v_type = VAR_STRING;
18622 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623}
18624
18625/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018626 * "strridx()" function
18627 */
18628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018629f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018630{
18631 char_u buf[NUMBUFLEN];
18632 char_u *needle;
18633 char_u *haystack;
18634 char_u *rest;
18635 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018636 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018638 needle = get_tv_string_chk(&argvars[1]);
18639 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018640
18641 rettv->vval.v_number = -1;
18642 if (needle == NULL || haystack == NULL)
18643 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018644
18645 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018646 if (argvars[2].v_type != VAR_UNKNOWN)
18647 {
18648 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018649 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018650 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018651 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018652 }
18653 else
18654 end_idx = haystack_len;
18655
Bram Moolenaar0d660222005-01-07 21:51:51 +000018656 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018657 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018658 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018659 lastmatch = haystack + end_idx;
18660 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018661 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018662 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018663 for (rest = haystack; *rest != '\0'; ++rest)
18664 {
18665 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018666 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018667 break;
18668 lastmatch = rest;
18669 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018670 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018671
18672 if (lastmatch == NULL)
18673 rettv->vval.v_number = -1;
18674 else
18675 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18676}
18677
18678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 * "strtrans()" function
18680 */
18681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018682f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018684 rettv->v_type = VAR_STRING;
18685 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686}
18687
18688/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018689 * "submatch()" function
18690 */
18691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018692f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018693{
Bram Moolenaar41571762014-04-02 19:00:58 +020018694 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018695 int no;
18696 int retList = 0;
18697
18698 no = (int)get_tv_number_chk(&argvars[0], &error);
18699 if (error)
18700 return;
18701 error = FALSE;
18702 if (argvars[1].v_type != VAR_UNKNOWN)
18703 retList = get_tv_number_chk(&argvars[1], &error);
18704 if (error)
18705 return;
18706
18707 if (retList == 0)
18708 {
18709 rettv->v_type = VAR_STRING;
18710 rettv->vval.v_string = reg_submatch(no);
18711 }
18712 else
18713 {
18714 rettv->v_type = VAR_LIST;
18715 rettv->vval.v_list = reg_submatch_list(no);
18716 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018717}
18718
18719/*
18720 * "substitute()" function
18721 */
18722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018723f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018724{
18725 char_u patbuf[NUMBUFLEN];
18726 char_u subbuf[NUMBUFLEN];
18727 char_u flagsbuf[NUMBUFLEN];
18728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018729 char_u *str = get_tv_string_chk(&argvars[0]);
18730 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18731 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18732 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18733
Bram Moolenaar0d660222005-01-07 21:51:51 +000018734 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018735 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18736 rettv->vval.v_string = NULL;
18737 else
18738 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018739}
18740
18741/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018742 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018745f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746{
18747 int id = 0;
18748#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018749 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 long col;
18751 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018752 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018754 lnum = get_tv_lnum(argvars); /* -1 on type error */
18755 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18756 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018758 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018759 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018760 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761#endif
18762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018763 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764}
18765
18766/*
18767 * "synIDattr(id, what [, mode])" function
18768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018770f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771{
18772 char_u *p = NULL;
18773#ifdef FEAT_SYN_HL
18774 int id;
18775 char_u *what;
18776 char_u *mode;
18777 char_u modebuf[NUMBUFLEN];
18778 int modec;
18779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018780 id = get_tv_number(&argvars[0]);
18781 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018782 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018784 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018786 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 modec = 0; /* replace invalid with current */
18788 }
18789 else
18790 {
18791#ifdef FEAT_GUI
18792 if (gui.in_use)
18793 modec = 'g';
18794 else
18795#endif
18796 if (t_colors > 1)
18797 modec = 'c';
18798 else
18799 modec = 't';
18800 }
18801
18802
18803 switch (TOLOWER_ASC(what[0]))
18804 {
18805 case 'b':
18806 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18807 p = highlight_color(id, what, modec);
18808 else /* bold */
18809 p = highlight_has_attr(id, HL_BOLD, modec);
18810 break;
18811
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018812 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813 p = highlight_color(id, what, modec);
18814 break;
18815
18816 case 'i':
18817 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18818 p = highlight_has_attr(id, HL_INVERSE, modec);
18819 else /* italic */
18820 p = highlight_has_attr(id, HL_ITALIC, modec);
18821 break;
18822
18823 case 'n': /* name */
18824 p = get_highlight_name(NULL, id - 1);
18825 break;
18826
18827 case 'r': /* reverse */
18828 p = highlight_has_attr(id, HL_INVERSE, modec);
18829 break;
18830
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018831 case 's':
18832 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18833 p = highlight_color(id, what, modec);
18834 else /* standout */
18835 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 break;
18837
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018838 case 'u':
18839 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18840 /* underline */
18841 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18842 else
18843 /* undercurl */
18844 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 break;
18846 }
18847
18848 if (p != NULL)
18849 p = vim_strsave(p);
18850#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018851 rettv->v_type = VAR_STRING;
18852 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853}
18854
18855/*
18856 * "synIDtrans(id)" function
18857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018859f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860{
18861 int id;
18862
18863#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018864 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865
18866 if (id > 0)
18867 id = syn_get_final_id(id);
18868 else
18869#endif
18870 id = 0;
18871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018872 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018873}
18874
18875/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018876 * "synconcealed(lnum, col)" function
18877 */
18878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018879f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018880{
18881#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18882 long lnum;
18883 long col;
18884 int syntax_flags = 0;
18885 int cchar;
18886 int matchid = 0;
18887 char_u str[NUMBUFLEN];
18888#endif
18889
18890 rettv->v_type = VAR_LIST;
18891 rettv->vval.v_list = NULL;
18892
18893#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18894 lnum = get_tv_lnum(argvars); /* -1 on type error */
18895 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18896
18897 vim_memset(str, NUL, sizeof(str));
18898
18899 if (rettv_list_alloc(rettv) != FAIL)
18900 {
18901 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18902 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18903 && curwin->w_p_cole > 0)
18904 {
18905 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18906 syntax_flags = get_syntax_info(&matchid);
18907
18908 /* get the conceal character */
18909 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18910 {
18911 cchar = syn_get_sub_char();
18912 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18913 cchar = lcs_conceal;
18914 if (cchar != NUL)
18915 {
18916# ifdef FEAT_MBYTE
18917 if (has_mbyte)
18918 (*mb_char2bytes)(cchar, str);
18919 else
18920# endif
18921 str[0] = cchar;
18922 }
18923 }
18924 }
18925
18926 list_append_number(rettv->vval.v_list,
18927 (syntax_flags & HL_CONCEAL) != 0);
18928 /* -1 to auto-determine strlen */
18929 list_append_string(rettv->vval.v_list, str, -1);
18930 list_append_number(rettv->vval.v_list, matchid);
18931 }
18932#endif
18933}
18934
18935/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018936 * "synstack(lnum, col)" function
18937 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018939f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018940{
18941#ifdef FEAT_SYN_HL
18942 long lnum;
18943 long col;
18944 int i;
18945 int id;
18946#endif
18947
18948 rettv->v_type = VAR_LIST;
18949 rettv->vval.v_list = NULL;
18950
18951#ifdef FEAT_SYN_HL
18952 lnum = get_tv_lnum(argvars); /* -1 on type error */
18953 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18954
18955 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018956 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018957 && rettv_list_alloc(rettv) != FAIL)
18958 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018959 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018960 for (i = 0; ; ++i)
18961 {
18962 id = syn_get_stack_item(i);
18963 if (id < 0)
18964 break;
18965 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18966 break;
18967 }
18968 }
18969#endif
18970}
18971
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018973get_cmd_output_as_rettv(
18974 typval_T *argvars,
18975 typval_T *rettv,
18976 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018978 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018980 char_u *infile = NULL;
18981 char_u buf[NUMBUFLEN];
18982 int err = FALSE;
18983 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018984 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018985 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018987 rettv->v_type = VAR_STRING;
18988 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018989 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018990 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018991
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018992 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018993 {
18994 /*
18995 * Write the string to a temp file, to be used for input of the shell
18996 * command.
18997 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018998 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018999 {
19000 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019001 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019002 }
19003
19004 fd = mch_fopen((char *)infile, WRITEBIN);
19005 if (fd == NULL)
19006 {
19007 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019008 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019009 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019010 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019011 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019012 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19013 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019014 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019015 else
19016 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019017 size_t len;
19018
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019019 p = get_tv_string_buf_chk(&argvars[1], buf);
19020 if (p == NULL)
19021 {
19022 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019023 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019024 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019025 len = STRLEN(p);
19026 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019027 err = TRUE;
19028 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019029 if (fclose(fd) != 0)
19030 err = TRUE;
19031 if (err)
19032 {
19033 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019034 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019035 }
19036 }
19037
Bram Moolenaar52a72462014-08-29 15:53:52 +020019038 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19039 * echoes typeahead, that messes up the display. */
19040 if (!msg_silent)
19041 flags += SHELL_COOKED;
19042
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019043 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019045 int len;
19046 listitem_T *li;
19047 char_u *s = NULL;
19048 char_u *start;
19049 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019050 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051
Bram Moolenaar52a72462014-08-29 15:53:52 +020019052 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019053 if (res == NULL)
19054 goto errret;
19055
19056 list = list_alloc();
19057 if (list == NULL)
19058 goto errret;
19059
19060 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019062 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019063 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019064 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019065 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019066
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019067 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019068 if (s == NULL)
19069 goto errret;
19070
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019071 for (p = s; start < end; ++p, ++start)
19072 *p = *start == NUL ? NL : *start;
19073 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019074
19075 li = listitem_alloc();
19076 if (li == NULL)
19077 {
19078 vim_free(s);
19079 goto errret;
19080 }
19081 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019082 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019083 li->li_tv.vval.v_string = s;
19084 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019086
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019087 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019088 rettv->v_type = VAR_LIST;
19089 rettv->vval.v_list = list;
19090 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019092 else
19093 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019094 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019095#ifdef USE_CR
19096 /* translate <CR> into <NL> */
19097 if (res != NULL)
19098 {
19099 char_u *s;
19100
19101 for (s = res; *s; ++s)
19102 {
19103 if (*s == CAR)
19104 *s = NL;
19105 }
19106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107#else
19108# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019109 /* translate <CR><NL> into <NL> */
19110 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019112 char_u *s, *d;
19113
19114 d = res;
19115 for (s = res; *s; ++s)
19116 {
19117 if (s[0] == CAR && s[1] == NL)
19118 ++s;
19119 *d++ = *s;
19120 }
19121 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123# endif
19124#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019125 rettv->vval.v_string = res;
19126 res = NULL;
19127 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019128
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019129errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019130 if (infile != NULL)
19131 {
19132 mch_remove(infile);
19133 vim_free(infile);
19134 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019135 if (res != NULL)
19136 vim_free(res);
19137 if (list != NULL)
19138 list_free(list, TRUE);
19139}
19140
19141/*
19142 * "system()" function
19143 */
19144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019145f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019146{
19147 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19148}
19149
19150/*
19151 * "systemlist()" function
19152 */
19153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019154f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019155{
19156 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157}
19158
19159/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019160 * "tabpagebuflist()" function
19161 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019163f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019164{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019165#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019166 tabpage_T *tp;
19167 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019168
19169 if (argvars[0].v_type == VAR_UNKNOWN)
19170 wp = firstwin;
19171 else
19172 {
19173 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19174 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019175 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019176 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019177 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019178 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019179 for (; wp != NULL; wp = wp->w_next)
19180 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019181 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019182 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019183 }
19184#endif
19185}
19186
19187
19188/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019189 * "tabpagenr()" function
19190 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019192f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019193{
19194 int nr = 1;
19195#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019196 char_u *arg;
19197
19198 if (argvars[0].v_type != VAR_UNKNOWN)
19199 {
19200 arg = get_tv_string_chk(&argvars[0]);
19201 nr = 0;
19202 if (arg != NULL)
19203 {
19204 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019205 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019206 else
19207 EMSG2(_(e_invexpr2), arg);
19208 }
19209 }
19210 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019211 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019212#endif
19213 rettv->vval.v_number = nr;
19214}
19215
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019216
19217#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019218static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019219
19220/*
19221 * Common code for tabpagewinnr() and winnr().
19222 */
19223 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019224get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019225{
19226 win_T *twin;
19227 int nr = 1;
19228 win_T *wp;
19229 char_u *arg;
19230
19231 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19232 if (argvar->v_type != VAR_UNKNOWN)
19233 {
19234 arg = get_tv_string_chk(argvar);
19235 if (arg == NULL)
19236 nr = 0; /* type error; errmsg already given */
19237 else if (STRCMP(arg, "$") == 0)
19238 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19239 else if (STRCMP(arg, "#") == 0)
19240 {
19241 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19242 if (twin == NULL)
19243 nr = 0;
19244 }
19245 else
19246 {
19247 EMSG2(_(e_invexpr2), arg);
19248 nr = 0;
19249 }
19250 }
19251
19252 if (nr > 0)
19253 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19254 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019255 {
19256 if (wp == NULL)
19257 {
19258 /* didn't find it in this tabpage */
19259 nr = 0;
19260 break;
19261 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019262 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019263 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019264 return nr;
19265}
19266#endif
19267
19268/*
19269 * "tabpagewinnr()" function
19270 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019272f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019273{
19274 int nr = 1;
19275#ifdef FEAT_WINDOWS
19276 tabpage_T *tp;
19277
19278 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19279 if (tp == NULL)
19280 nr = 0;
19281 else
19282 nr = get_winnr(tp, &argvars[1]);
19283#endif
19284 rettv->vval.v_number = nr;
19285}
19286
19287
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019288/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019289 * "tagfiles()" function
19290 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019292f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019293{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019294 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019295 tagname_T tn;
19296 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019297
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019298 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019299 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019300 fname = alloc(MAXPATHL);
19301 if (fname == NULL)
19302 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019303
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019304 for (first = TRUE; ; first = FALSE)
19305 if (get_tagfname(&tn, first, fname) == FAIL
19306 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019307 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019308 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019309 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019310}
19311
19312/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019313 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019314 */
19315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019316f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019317{
19318 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019319
19320 tag_pattern = get_tv_string(&argvars[0]);
19321
19322 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019323 if (*tag_pattern == NUL)
19324 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019325
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019326 if (rettv_list_alloc(rettv) == OK)
19327 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019328}
19329
19330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331 * "tempname()" function
19332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019334f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335{
19336 static int x = 'A';
19337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019338 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019339 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340
19341 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19342 * names. Skip 'I' and 'O', they are used for shell redirection. */
19343 do
19344 {
19345 if (x == 'Z')
19346 x = '0';
19347 else if (x == '9')
19348 x = 'A';
19349 else
19350 {
19351#ifdef EBCDIC
19352 if (x == 'I')
19353 x = 'J';
19354 else if (x == 'R')
19355 x = 'S';
19356 else
19357#endif
19358 ++x;
19359 }
19360 } while (x == 'I' || x == 'O');
19361}
19362
19363/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019364 * "test(list)" function: Just checking the walls...
19365 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019367f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019368{
19369 /* Used for unit testing. Change the code below to your liking. */
19370#if 0
19371 listitem_T *li;
19372 list_T *l;
19373 char_u *bad, *good;
19374
19375 if (argvars[0].v_type != VAR_LIST)
19376 return;
19377 l = argvars[0].vval.v_list;
19378 if (l == NULL)
19379 return;
19380 li = l->lv_first;
19381 if (li == NULL)
19382 return;
19383 bad = get_tv_string(&li->li_tv);
19384 li = li->li_next;
19385 if (li == NULL)
19386 return;
19387 good = get_tv_string(&li->li_tv);
19388 rettv->vval.v_number = test_edit_score(bad, good);
19389#endif
19390}
19391
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019392#ifdef FEAT_FLOAT
19393/*
19394 * "tan()" function
19395 */
19396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019397f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019398{
19399 float_T f;
19400
19401 rettv->v_type = VAR_FLOAT;
19402 if (get_float_arg(argvars, &f) == OK)
19403 rettv->vval.v_float = tan(f);
19404 else
19405 rettv->vval.v_float = 0.0;
19406}
19407
19408/*
19409 * "tanh()" function
19410 */
19411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019412f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019413{
19414 float_T f;
19415
19416 rettv->v_type = VAR_FLOAT;
19417 if (get_float_arg(argvars, &f) == OK)
19418 rettv->vval.v_float = tanh(f);
19419 else
19420 rettv->vval.v_float = 0.0;
19421}
19422#endif
19423
Bram Moolenaard52d9742005-08-21 22:20:28 +000019424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425 * "tolower(string)" function
19426 */
19427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019428f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429{
19430 char_u *p;
19431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019432 p = vim_strsave(get_tv_string(&argvars[0]));
19433 rettv->v_type = VAR_STRING;
19434 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435
19436 if (p != NULL)
19437 while (*p != NUL)
19438 {
19439#ifdef FEAT_MBYTE
19440 int l;
19441
19442 if (enc_utf8)
19443 {
19444 int c, lc;
19445
19446 c = utf_ptr2char(p);
19447 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019448 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 /* TODO: reallocate string when byte count changes. */
19450 if (utf_char2len(lc) == l)
19451 utf_char2bytes(lc, p);
19452 p += l;
19453 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019454 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 p += l; /* skip multi-byte character */
19456 else
19457#endif
19458 {
19459 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19460 ++p;
19461 }
19462 }
19463}
19464
19465/*
19466 * "toupper(string)" function
19467 */
19468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019469f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019471 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019472 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473}
19474
19475/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019476 * "tr(string, fromstr, tostr)" function
19477 */
19478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019479f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019480{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019481 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019482 char_u *fromstr;
19483 char_u *tostr;
19484 char_u *p;
19485#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019486 int inlen;
19487 int fromlen;
19488 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019489 int idx;
19490 char_u *cpstr;
19491 int cplen;
19492 int first = TRUE;
19493#endif
19494 char_u buf[NUMBUFLEN];
19495 char_u buf2[NUMBUFLEN];
19496 garray_T ga;
19497
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019498 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019499 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19500 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019501
19502 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019503 rettv->v_type = VAR_STRING;
19504 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019505 if (fromstr == NULL || tostr == NULL)
19506 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019507 ga_init2(&ga, (int)sizeof(char), 80);
19508
19509#ifdef FEAT_MBYTE
19510 if (!has_mbyte)
19511#endif
19512 /* not multi-byte: fromstr and tostr must be the same length */
19513 if (STRLEN(fromstr) != STRLEN(tostr))
19514 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019515#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019516error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019517#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019518 EMSG2(_(e_invarg2), fromstr);
19519 ga_clear(&ga);
19520 return;
19521 }
19522
19523 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019524 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019525 {
19526#ifdef FEAT_MBYTE
19527 if (has_mbyte)
19528 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019529 inlen = (*mb_ptr2len)(in_str);
19530 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019531 cplen = inlen;
19532 idx = 0;
19533 for (p = fromstr; *p != NUL; p += fromlen)
19534 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019535 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019536 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019537 {
19538 for (p = tostr; *p != NUL; p += tolen)
19539 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019540 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019541 if (idx-- == 0)
19542 {
19543 cplen = tolen;
19544 cpstr = p;
19545 break;
19546 }
19547 }
19548 if (*p == NUL) /* tostr is shorter than fromstr */
19549 goto error;
19550 break;
19551 }
19552 ++idx;
19553 }
19554
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019555 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019556 {
19557 /* Check that fromstr and tostr have the same number of
19558 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019559 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019560 first = FALSE;
19561 for (p = tostr; *p != NUL; p += tolen)
19562 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019563 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019564 --idx;
19565 }
19566 if (idx != 0)
19567 goto error;
19568 }
19569
Bram Moolenaarcde88542015-08-11 19:14:00 +020019570 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019571 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019572 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019573
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019574 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019575 }
19576 else
19577#endif
19578 {
19579 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019580 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019581 if (p != NULL)
19582 ga_append(&ga, tostr[p - fromstr]);
19583 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019584 ga_append(&ga, *in_str);
19585 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019586 }
19587 }
19588
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019589 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019590 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019591 ga_append(&ga, NUL);
19592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019593 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019594}
19595
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019596#ifdef FEAT_FLOAT
19597/*
19598 * "trunc({float})" function
19599 */
19600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019601f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019602{
19603 float_T f;
19604
19605 rettv->v_type = VAR_FLOAT;
19606 if (get_float_arg(argvars, &f) == OK)
19607 /* trunc() is not in C90, use floor() or ceil() instead. */
19608 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19609 else
19610 rettv->vval.v_float = 0.0;
19611}
19612#endif
19613
Bram Moolenaar8299df92004-07-10 09:47:34 +000019614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 * "type(expr)" function
19616 */
19617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019618f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019620 int n;
19621
19622 switch (argvars[0].v_type)
19623 {
19624 case VAR_NUMBER: n = 0; break;
19625 case VAR_STRING: n = 1; break;
19626 case VAR_FUNC: n = 2; break;
19627 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019628 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019629#ifdef FEAT_FLOAT
19630 case VAR_FLOAT: n = 5; break;
19631#endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +010019632 case VAR_SPECIAL:
19633 if (argvars[0].vval.v_number == VVAL_FALSE
19634 || argvars[0].vval.v_number == VVAL_TRUE)
19635 n = 6;
19636 else
19637 n = 7;
19638 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019639 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19640 }
19641 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642}
19643
19644/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019645 * "undofile(name)" function
19646 */
19647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019648f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019649{
19650 rettv->v_type = VAR_STRING;
19651#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019652 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019653 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019654
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019655 if (*fname == NUL)
19656 {
19657 /* If there is no file name there will be no undo file. */
19658 rettv->vval.v_string = NULL;
19659 }
19660 else
19661 {
19662 char_u *ffname = FullName_save(fname, FALSE);
19663
19664 if (ffname != NULL)
19665 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19666 vim_free(ffname);
19667 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019668 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019669#else
19670 rettv->vval.v_string = NULL;
19671#endif
19672}
19673
19674/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019675 * "undotree()" function
19676 */
19677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019678f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020019679{
19680 if (rettv_dict_alloc(rettv) == OK)
19681 {
19682 dict_T *dict = rettv->vval.v_dict;
19683 list_T *list;
19684
Bram Moolenaar730cde92010-06-27 05:18:54 +020019685 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019686 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019687 dict_add_nr_str(dict, "save_last",
19688 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019689 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19690 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019691 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019692
19693 list = list_alloc();
19694 if (list != NULL)
19695 {
19696 u_eval_tree(curbuf->b_u_oldhead, list);
19697 dict_add_list(dict, "entries", list);
19698 }
19699 }
19700}
19701
19702/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019703 * "values(dict)" function
19704 */
19705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019706f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000019707{
19708 dict_list(argvars, rettv, 1);
19709}
19710
19711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712 * "virtcol(string)" function
19713 */
19714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019715f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716{
19717 colnr_T vcol = 0;
19718 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019719 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019721 fp = var2fpos(&argvars[0], FALSE, &fnum);
19722 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19723 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724 {
19725 getvvcol(curwin, fp, NULL, NULL, &vcol);
19726 ++vcol;
19727 }
19728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019729 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730}
19731
19732/*
19733 * "visualmode()" function
19734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019736f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738 char_u str[2];
19739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019740 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 str[0] = curbuf->b_visual_mode_eval;
19742 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019743 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744
19745 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019746 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748}
19749
19750/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019751 * "wildmenumode()" function
19752 */
19753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019754f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019755{
19756#ifdef FEAT_WILDMENU
19757 if (wild_menu_showing)
19758 rettv->vval.v_number = 1;
19759#endif
19760}
19761
19762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 * "winbufnr(nr)" function
19764 */
19765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019766f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767{
19768 win_T *wp;
19769
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019770 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019772 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019774 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775}
19776
19777/*
19778 * "wincol()" function
19779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019781f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782{
19783 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019784 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785}
19786
19787/*
19788 * "winheight(nr)" function
19789 */
19790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019791f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792{
19793 win_T *wp;
19794
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019795 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019797 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019799 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800}
19801
19802/*
19803 * "winline()" function
19804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019806f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807{
19808 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019809 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810}
19811
19812/*
19813 * "winnr()" function
19814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019816f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817{
19818 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019819
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019821 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019823 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824}
19825
19826/*
19827 * "winrestcmd()" function
19828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019830f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831{
19832#ifdef FEAT_WINDOWS
19833 win_T *wp;
19834 int winnr = 1;
19835 garray_T ga;
19836 char_u buf[50];
19837
19838 ga_init2(&ga, (int)sizeof(char), 70);
19839 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19840 {
19841 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19842 ga_concat(&ga, buf);
19843# ifdef FEAT_VERTSPLIT
19844 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19845 ga_concat(&ga, buf);
19846# endif
19847 ++winnr;
19848 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019849 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019851 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019853 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019855 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856}
19857
19858/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019859 * "winrestview()" function
19860 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019862f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019863{
19864 dict_T *dict;
19865
19866 if (argvars[0].v_type != VAR_DICT
19867 || (dict = argvars[0].vval.v_dict) == NULL)
19868 EMSG(_(e_invarg));
19869 else
19870 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019871 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19872 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19873 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19874 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019875#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019876 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19877 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019878#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019879 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19880 {
19881 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19882 curwin->w_set_curswant = FALSE;
19883 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019884
Bram Moolenaar82c25852014-05-28 16:47:16 +020019885 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19886 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019887#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019888 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19889 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019890#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019891 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19892 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19893 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19894 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019895
19896 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019897 win_new_height(curwin, curwin->w_height);
19898# ifdef FEAT_VERTSPLIT
19899 win_new_width(curwin, W_WIDTH(curwin));
19900# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019901 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019902
Bram Moolenaarb851a962014-10-31 15:45:52 +010019903 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019904 curwin->w_topline = 1;
19905 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19906 curwin->w_topline = curbuf->b_ml.ml_line_count;
19907#ifdef FEAT_DIFF
19908 check_topfill(curwin, TRUE);
19909#endif
19910 }
19911}
19912
19913/*
19914 * "winsaveview()" function
19915 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019917f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019918{
19919 dict_T *dict;
19920
Bram Moolenaara800b422010-06-27 01:15:55 +020019921 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019922 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019923 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019924
19925 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19926 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19927#ifdef FEAT_VIRTUALEDIT
19928 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19929#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019930 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019931 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19932
19933 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19934#ifdef FEAT_DIFF
19935 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19936#endif
19937 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19938 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19939}
19940
19941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942 * "winwidth(nr)" function
19943 */
19944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019945f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946{
19947 win_T *wp;
19948
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019949 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019951 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 else
19953#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019954 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019956 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957#endif
19958}
19959
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960/*
Bram Moolenaared767a22016-01-03 22:49:16 +010019961 * "wordcount()" function
19962 */
19963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019964f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010019965{
19966 if (rettv_dict_alloc(rettv) == FAIL)
19967 return;
19968 cursor_pos_info(rettv->vval.v_dict);
19969}
19970
19971/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019972 * Write list of strings to file
19973 */
19974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019975write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019976{
19977 listitem_T *li;
19978 int c;
19979 int ret = OK;
19980 char_u *s;
19981
19982 for (li = list->lv_first; li != NULL; li = li->li_next)
19983 {
19984 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19985 {
19986 if (*s == '\n')
19987 c = putc(NUL, fd);
19988 else
19989 c = putc(*s, fd);
19990 if (c == EOF)
19991 {
19992 ret = FAIL;
19993 break;
19994 }
19995 }
19996 if (!binary || li->li_next != NULL)
19997 if (putc('\n', fd) == EOF)
19998 {
19999 ret = FAIL;
20000 break;
20001 }
20002 if (ret == FAIL)
20003 {
20004 EMSG(_(e_write));
20005 break;
20006 }
20007 }
20008 return ret;
20009}
20010
20011/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020012 * "writefile()" function
20013 */
20014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020015f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020016{
20017 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020018 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020019 char_u *fname;
20020 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020021 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020022
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020023 if (check_restricted() || check_secure())
20024 return;
20025
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020026 if (argvars[0].v_type != VAR_LIST)
20027 {
20028 EMSG2(_(e_listarg), "writefile()");
20029 return;
20030 }
20031 if (argvars[0].vval.v_list == NULL)
20032 return;
20033
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020034 if (argvars[2].v_type != VAR_UNKNOWN)
20035 {
20036 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20037 binary = TRUE;
20038 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20039 append = TRUE;
20040 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020041
20042 /* Always open the file in binary mode, library functions have a mind of
20043 * their own about CR-LF conversion. */
20044 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020045 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20046 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020047 {
20048 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20049 ret = -1;
20050 }
20051 else
20052 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020053 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20054 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020055 fclose(fd);
20056 }
20057
20058 rettv->vval.v_number = ret;
20059}
20060
20061/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020062 * "xor(expr, expr)" function
20063 */
20064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020065f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020066{
20067 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20068 ^ get_tv_number_chk(&argvars[1], NULL);
20069}
20070
20071
20072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020074 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 */
20076 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020077var2fpos(
20078 typval_T *varp,
20079 int dollar_lnum, /* TRUE when $ is last line */
20080 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020082 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020084 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085
Bram Moolenaara5525202006-03-02 22:52:09 +000020086 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020087 if (varp->v_type == VAR_LIST)
20088 {
20089 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020090 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020091 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020092 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020093
20094 l = varp->vval.v_list;
20095 if (l == NULL)
20096 return NULL;
20097
20098 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020099 pos.lnum = list_find_nr(l, 0L, &error);
20100 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020101 return NULL; /* invalid line number */
20102
20103 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020104 pos.col = list_find_nr(l, 1L, &error);
20105 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020106 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020107 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020108
20109 /* We accept "$" for the column number: last column. */
20110 li = list_find(l, 1L);
20111 if (li != NULL && li->li_tv.v_type == VAR_STRING
20112 && li->li_tv.vval.v_string != NULL
20113 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20114 pos.col = len + 1;
20115
Bram Moolenaara5525202006-03-02 22:52:09 +000020116 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020117 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020118 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020119 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020120
Bram Moolenaara5525202006-03-02 22:52:09 +000020121#ifdef FEAT_VIRTUALEDIT
20122 /* Get the virtual offset. Defaults to zero. */
20123 pos.coladd = list_find_nr(l, 2L, &error);
20124 if (error)
20125 pos.coladd = 0;
20126#endif
20127
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020128 return &pos;
20129 }
20130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020131 name = get_tv_string_chk(varp);
20132 if (name == NULL)
20133 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020134 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020136 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20137 {
20138 if (VIsual_active)
20139 return &VIsual;
20140 return &curwin->w_cursor;
20141 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020142 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020144 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20146 return NULL;
20147 return pp;
20148 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020149
20150#ifdef FEAT_VIRTUALEDIT
20151 pos.coladd = 0;
20152#endif
20153
Bram Moolenaar477933c2007-07-17 14:32:23 +000020154 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020155 {
20156 pos.col = 0;
20157 if (name[1] == '0') /* "w0": first visible line */
20158 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020159 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020160 pos.lnum = curwin->w_topline;
20161 return &pos;
20162 }
20163 else if (name[1] == '$') /* "w$": last visible line */
20164 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020165 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020166 pos.lnum = curwin->w_botline - 1;
20167 return &pos;
20168 }
20169 }
20170 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020172 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173 {
20174 pos.lnum = curbuf->b_ml.ml_line_count;
20175 pos.col = 0;
20176 }
20177 else
20178 {
20179 pos.lnum = curwin->w_cursor.lnum;
20180 pos.col = (colnr_T)STRLEN(ml_get_curline());
20181 }
20182 return &pos;
20183 }
20184 return NULL;
20185}
20186
20187/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020188 * Convert list in "arg" into a position and optional file number.
20189 * When "fnump" is NULL there is no file number, only 3 items.
20190 * Note that the column is passed on as-is, the caller may want to decrement
20191 * it to use 1 for the first column.
20192 * Return FAIL when conversion is not possible, doesn't check the position for
20193 * validity.
20194 */
20195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020196list2fpos(
20197 typval_T *arg,
20198 pos_T *posp,
20199 int *fnump,
20200 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020201{
20202 list_T *l = arg->vval.v_list;
20203 long i = 0;
20204 long n;
20205
Bram Moolenaar493c1782014-05-28 14:34:46 +020020206 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20207 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020208 if (arg->v_type != VAR_LIST
20209 || l == NULL
20210 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020211 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020212 return FAIL;
20213
20214 if (fnump != NULL)
20215 {
20216 n = list_find_nr(l, i++, NULL); /* fnum */
20217 if (n < 0)
20218 return FAIL;
20219 if (n == 0)
20220 n = curbuf->b_fnum; /* current buffer */
20221 *fnump = n;
20222 }
20223
20224 n = list_find_nr(l, i++, NULL); /* lnum */
20225 if (n < 0)
20226 return FAIL;
20227 posp->lnum = n;
20228
20229 n = list_find_nr(l, i++, NULL); /* col */
20230 if (n < 0)
20231 return FAIL;
20232 posp->col = n;
20233
20234#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020235 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020236 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020237 posp->coladd = 0;
20238 else
20239 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020240#endif
20241
Bram Moolenaar493c1782014-05-28 14:34:46 +020020242 if (curswantp != NULL)
20243 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20244
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020245 return OK;
20246}
20247
20248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 * Get the length of an environment variable name.
20250 * Advance "arg" to the first character after the name.
20251 * Return 0 for error.
20252 */
20253 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020254get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255{
20256 char_u *p;
20257 int len;
20258
20259 for (p = *arg; vim_isIDc(*p); ++p)
20260 ;
20261 if (p == *arg) /* no name found */
20262 return 0;
20263
20264 len = (int)(p - *arg);
20265 *arg = p;
20266 return len;
20267}
20268
20269/*
20270 * Get the length of the name of a function or internal variable.
20271 * "arg" is advanced to the first non-white character after the name.
20272 * Return 0 if something is wrong.
20273 */
20274 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020275get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276{
20277 char_u *p;
20278 int len;
20279
20280 /* Find the end of the name. */
20281 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020282 {
20283 if (*p == ':')
20284 {
20285 /* "s:" is start of "s:var", but "n:" is not and can be used in
20286 * slice "[n:]". Also "xx:" is not a namespace. */
20287 len = (int)(p - *arg);
20288 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20289 || len > 1)
20290 break;
20291 }
20292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 if (p == *arg) /* no name found */
20294 return 0;
20295
20296 len = (int)(p - *arg);
20297 *arg = skipwhite(p);
20298
20299 return len;
20300}
20301
20302/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020303 * Get the length of the name of a variable or function.
20304 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020306 * Return -1 if curly braces expansion failed.
20307 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 * If the name contains 'magic' {}'s, expand them and return the
20309 * expanded name in an allocated string via 'alias' - caller must free.
20310 */
20311 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020312get_name_len(
20313 char_u **arg,
20314 char_u **alias,
20315 int evaluate,
20316 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317{
20318 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 char_u *p;
20320 char_u *expr_start;
20321 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322
20323 *alias = NULL; /* default to no alias */
20324
20325 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20326 && (*arg)[2] == (int)KE_SNR)
20327 {
20328 /* hard coded <SNR>, already translated */
20329 *arg += 3;
20330 return get_id_len(arg) + 3;
20331 }
20332 len = eval_fname_script(*arg);
20333 if (len > 0)
20334 {
20335 /* literal "<SID>", "s:" or "<SNR>" */
20336 *arg += len;
20337 }
20338
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020340 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020342 p = find_name_end(*arg, &expr_start, &expr_end,
20343 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 if (expr_start != NULL)
20345 {
20346 char_u *temp_string;
20347
20348 if (!evaluate)
20349 {
20350 len += (int)(p - *arg);
20351 *arg = skipwhite(p);
20352 return len;
20353 }
20354
20355 /*
20356 * Include any <SID> etc in the expanded string:
20357 * Thus the -len here.
20358 */
20359 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20360 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020361 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 *alias = temp_string;
20363 *arg = skipwhite(p);
20364 return (int)STRLEN(temp_string);
20365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366
20367 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020368 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 EMSG2(_(e_invexpr2), *arg);
20370
20371 return len;
20372}
20373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020374/*
20375 * Find the end of a variable or function name, taking care of magic braces.
20376 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20377 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020378 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020379 * Return a pointer to just after the name. Equal to "arg" if there is no
20380 * valid name.
20381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020383find_name_end(
20384 char_u *arg,
20385 char_u **expr_start,
20386 char_u **expr_end,
20387 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020389 int mb_nest = 0;
20390 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020392 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020394 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020396 *expr_start = NULL;
20397 *expr_end = NULL;
20398 }
20399
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020400 /* Quick check for valid starting character. */
20401 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20402 return arg;
20403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020404 for (p = arg; *p != NUL
20405 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020406 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020407 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020408 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020409 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020410 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020411 if (*p == '\'')
20412 {
20413 /* skip over 'string' to avoid counting [ and ] inside it. */
20414 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20415 ;
20416 if (*p == NUL)
20417 break;
20418 }
20419 else if (*p == '"')
20420 {
20421 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20422 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20423 if (*p == '\\' && p[1] != NUL)
20424 ++p;
20425 if (*p == NUL)
20426 break;
20427 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020428 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20429 {
20430 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020431 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020432 len = (int)(p - arg);
20433 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020434 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020435 break;
20436 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020438 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020440 if (*p == '[')
20441 ++br_nest;
20442 else if (*p == ']')
20443 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020446 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020448 if (*p == '{')
20449 {
20450 mb_nest++;
20451 if (expr_start != NULL && *expr_start == NULL)
20452 *expr_start = p;
20453 }
20454 else if (*p == '}')
20455 {
20456 mb_nest--;
20457 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20458 *expr_end = p;
20459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461 }
20462
20463 return p;
20464}
20465
20466/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020467 * Expands out the 'magic' {}'s in a variable/function name.
20468 * Note that this can call itself recursively, to deal with
20469 * constructs like foo{bar}{baz}{bam}
20470 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20471 * "in_start" ^
20472 * "expr_start" ^
20473 * "expr_end" ^
20474 * "in_end" ^
20475 *
20476 * Returns a new allocated string, which the caller must free.
20477 * Returns NULL for failure.
20478 */
20479 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020480make_expanded_name(
20481 char_u *in_start,
20482 char_u *expr_start,
20483 char_u *expr_end,
20484 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020485{
20486 char_u c1;
20487 char_u *retval = NULL;
20488 char_u *temp_result;
20489 char_u *nextcmd = NULL;
20490
20491 if (expr_end == NULL || in_end == NULL)
20492 return NULL;
20493 *expr_start = NUL;
20494 *expr_end = NUL;
20495 c1 = *in_end;
20496 *in_end = NUL;
20497
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020498 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020499 if (temp_result != NULL && nextcmd == NULL)
20500 {
20501 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20502 + (in_end - expr_end) + 1));
20503 if (retval != NULL)
20504 {
20505 STRCPY(retval, in_start);
20506 STRCAT(retval, temp_result);
20507 STRCAT(retval, expr_end + 1);
20508 }
20509 }
20510 vim_free(temp_result);
20511
20512 *in_end = c1; /* put char back for error messages */
20513 *expr_start = '{';
20514 *expr_end = '}';
20515
20516 if (retval != NULL)
20517 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020518 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020519 if (expr_start != NULL)
20520 {
20521 /* Further expansion! */
20522 temp_result = make_expanded_name(retval, expr_start,
20523 expr_end, temp_result);
20524 vim_free(retval);
20525 retval = temp_result;
20526 }
20527 }
20528
20529 return retval;
20530}
20531
20532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020534 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535 */
20536 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020537eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020539 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20540}
20541
20542/*
20543 * Return TRUE if character "c" can be used as the first character in a
20544 * variable or function name (excluding '{' and '}').
20545 */
20546 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020547eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020548{
20549 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550}
20551
20552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553 * Set number v: variable to "val".
20554 */
20555 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020556set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020558 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559}
20560
20561/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020562 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563 */
20564 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020565get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020567 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568}
20569
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020570/*
20571 * Get string v: variable value. Uses a static buffer, can only be used once.
20572 */
20573 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020574get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020575{
20576 return get_tv_string(&vimvars[idx].vv_tv);
20577}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020578
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020580 * Get List v: variable value. Caller must take care of reference count when
20581 * needed.
20582 */
20583 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020584get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020585{
20586 return vimvars[idx].vv_list;
20587}
20588
20589/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020590 * Set v:char to character "c".
20591 */
20592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020593set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020594{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020595 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020596
20597#ifdef FEAT_MBYTE
20598 if (has_mbyte)
20599 buf[(*mb_char2bytes)(c, buf)] = NUL;
20600 else
20601#endif
20602 {
20603 buf[0] = c;
20604 buf[1] = NUL;
20605 }
20606 set_vim_var_string(VV_CHAR, buf, -1);
20607}
20608
20609/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020610 * Set v:count to "count" and v:count1 to "count1".
20611 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 */
20613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020614set_vcount(
20615 long count,
20616 long count1,
20617 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020619 if (set_prevcount)
20620 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020621 vimvars[VV_COUNT].vv_nr = count;
20622 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623}
20624
20625/*
20626 * Set string v: variable to a copy of "val".
20627 */
20628 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020629set_vim_var_string(
20630 int idx,
20631 char_u *val,
20632 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633{
Bram Moolenaara542c682016-01-31 16:28:04 +010020634 clear_tv(&vimvars[idx].vv_di.di_tv);
20635 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020637 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020639 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020641 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642}
20643
20644/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020645 * Set List v: variable to "val".
20646 */
20647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020648set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000020649{
Bram Moolenaara542c682016-01-31 16:28:04 +010020650 clear_tv(&vimvars[idx].vv_di.di_tv);
20651 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000020652 vimvars[idx].vv_list = val;
20653 if (val != NULL)
20654 ++val->lv_refcount;
20655}
20656
20657/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020658 * Set Dictionary v: variable to "val".
20659 */
20660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020661set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020020662{
20663 int todo;
20664 hashitem_T *hi;
20665
Bram Moolenaara542c682016-01-31 16:28:04 +010020666 clear_tv(&vimvars[idx].vv_di.di_tv);
20667 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020020668 vimvars[idx].vv_dict = val;
20669 if (val != NULL)
20670 {
20671 ++val->dv_refcount;
20672
20673 /* Set readonly */
20674 todo = (int)val->dv_hashtab.ht_used;
20675 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20676 {
20677 if (HASHITEM_EMPTY(hi))
20678 continue;
20679 --todo;
20680 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20681 }
20682 }
20683}
20684
20685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 * Set v:register if needed.
20687 */
20688 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020689set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020690{
20691 char_u regname;
20692
20693 if (c == 0 || c == ' ')
20694 regname = '"';
20695 else
20696 regname = c;
20697 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020698 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 set_vim_var_string(VV_REG, &regname, 1);
20700}
20701
20702/*
20703 * Get or set v:exception. If "oldval" == NULL, return the current value.
20704 * Otherwise, restore the value to "oldval" and return NULL.
20705 * Must always be called in pairs to save and restore v:exception! Does not
20706 * take care of memory allocations.
20707 */
20708 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020709v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710{
20711 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020712 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713
Bram Moolenaare9a41262005-01-15 22:18:47 +000020714 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 return NULL;
20716}
20717
20718/*
20719 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20720 * Otherwise, restore the value to "oldval" and return NULL.
20721 * Must always be called in pairs to save and restore v:throwpoint! Does not
20722 * take care of memory allocations.
20723 */
20724 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020725v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726{
20727 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020728 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729
Bram Moolenaare9a41262005-01-15 22:18:47 +000020730 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 return NULL;
20732}
20733
20734#if defined(FEAT_AUTOCMD) || defined(PROTO)
20735/*
20736 * Set v:cmdarg.
20737 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20738 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20739 * Must always be called in pairs!
20740 */
20741 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020742set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743{
20744 char_u *oldval;
20745 char_u *newval;
20746 unsigned len;
20747
Bram Moolenaare9a41262005-01-15 22:18:47 +000020748 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020749 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020751 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020752 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020753 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754 }
20755
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020756 if (eap->force_bin == FORCE_BIN)
20757 len = 6;
20758 else if (eap->force_bin == FORCE_NOBIN)
20759 len = 8;
20760 else
20761 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020762
20763 if (eap->read_edit)
20764 len += 7;
20765
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020766 if (eap->force_ff != 0)
20767 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20768# ifdef FEAT_MBYTE
20769 if (eap->force_enc != 0)
20770 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020771 if (eap->bad_char != 0)
20772 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020773# endif
20774
20775 newval = alloc(len + 1);
20776 if (newval == NULL)
20777 return NULL;
20778
20779 if (eap->force_bin == FORCE_BIN)
20780 sprintf((char *)newval, " ++bin");
20781 else if (eap->force_bin == FORCE_NOBIN)
20782 sprintf((char *)newval, " ++nobin");
20783 else
20784 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020785
20786 if (eap->read_edit)
20787 STRCAT(newval, " ++edit");
20788
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020789 if (eap->force_ff != 0)
20790 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20791 eap->cmd + eap->force_ff);
20792# ifdef FEAT_MBYTE
20793 if (eap->force_enc != 0)
20794 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20795 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020796 if (eap->bad_char == BAD_KEEP)
20797 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20798 else if (eap->bad_char == BAD_DROP)
20799 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20800 else if (eap->bad_char != 0)
20801 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020802# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020803 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020804 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805}
20806#endif
20807
20808/*
20809 * Get the value of internal variable "name".
20810 * Return OK or FAIL.
20811 */
20812 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020813get_var_tv(
20814 char_u *name,
20815 int len, /* length of "name" */
20816 typval_T *rettv, /* NULL when only checking existence */
20817 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
20818 int verbose, /* may give error message */
20819 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820{
20821 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020822 typval_T *tv = NULL;
20823 typval_T atv;
20824 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826
20827 /* truncate the name, so that we can use strcmp() */
20828 cc = name[len];
20829 name[len] = NUL;
20830
20831 /*
20832 * Check for "b:changedtick".
20833 */
20834 if (STRCMP(name, "b:changedtick") == 0)
20835 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020836 atv.v_type = VAR_NUMBER;
20837 atv.vval.v_number = curbuf->b_changedtick;
20838 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 }
20840
20841 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 * Check for user-defined variables.
20843 */
20844 else
20845 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020846 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020848 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020849 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020850 if (dip != NULL)
20851 *dip = v;
20852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 }
20854
Bram Moolenaare9a41262005-01-15 22:18:47 +000020855 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020857 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020858 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859 ret = FAIL;
20860 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020861 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020862 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863
20864 name[len] = cc;
20865
20866 return ret;
20867}
20868
20869/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020870 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20871 * Also handle function call with Funcref variable: func(expr)
20872 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20873 */
20874 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020875handle_subscript(
20876 char_u **arg,
20877 typval_T *rettv,
20878 int evaluate, /* do more than finding the end */
20879 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020880{
20881 int ret = OK;
20882 dict_T *selfdict = NULL;
20883 char_u *s;
20884 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020885 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020886
20887 while (ret == OK
20888 && (**arg == '['
20889 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020890 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020891 && !vim_iswhite(*(*arg - 1)))
20892 {
20893 if (**arg == '(')
20894 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020895 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020896 if (evaluate)
20897 {
20898 functv = *rettv;
20899 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020900
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020901 /* Invoke the function. Recursive! */
20902 s = functv.vval.v_string;
20903 }
20904 else
20905 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020906 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020907 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20908 &len, evaluate, selfdict);
20909
20910 /* Clear the funcref afterwards, so that deleting it while
20911 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020912 if (evaluate)
20913 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020914
20915 /* Stop the expression evaluation when immediately aborting on
20916 * error, or when an interrupt occurred or an exception was thrown
20917 * but not caught. */
20918 if (aborting())
20919 {
20920 if (ret == OK)
20921 clear_tv(rettv);
20922 ret = FAIL;
20923 }
20924 dict_unref(selfdict);
20925 selfdict = NULL;
20926 }
20927 else /* **arg == '[' || **arg == '.' */
20928 {
20929 dict_unref(selfdict);
20930 if (rettv->v_type == VAR_DICT)
20931 {
20932 selfdict = rettv->vval.v_dict;
20933 if (selfdict != NULL)
20934 ++selfdict->dv_refcount;
20935 }
20936 else
20937 selfdict = NULL;
20938 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20939 {
20940 clear_tv(rettv);
20941 ret = FAIL;
20942 }
20943 }
20944 }
20945 dict_unref(selfdict);
20946 return ret;
20947}
20948
20949/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020950 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020951 * value).
20952 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010020953 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020954alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020955{
Bram Moolenaar33570922005-01-25 22:26:29 +000020956 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020957}
20958
20959/*
20960 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961 * The string "s" must have been allocated, it is consumed.
20962 * Return NULL for out of memory, the variable otherwise.
20963 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020964 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020965alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966{
Bram Moolenaar33570922005-01-25 22:26:29 +000020967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020969 rettv = alloc_tv();
20970 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020972 rettv->v_type = VAR_STRING;
20973 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 }
20975 else
20976 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020977 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978}
20979
20980/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020981 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020983 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020984free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985{
20986 if (varp != NULL)
20987 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020988 switch (varp->v_type)
20989 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020990 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020991 func_unref(varp->vval.v_string);
20992 /*FALLTHROUGH*/
20993 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020994 vim_free(varp->vval.v_string);
20995 break;
20996 case VAR_LIST:
20997 list_unref(varp->vval.v_list);
20998 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020999 case VAR_DICT:
21000 dict_unref(varp->vval.v_dict);
21001 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021002 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021003#ifdef FEAT_FLOAT
21004 case VAR_FLOAT:
21005#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021006 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021007 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021008 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021009 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021010 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021011 break;
21012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013 vim_free(varp);
21014 }
21015}
21016
21017/*
21018 * Free the memory for a variable value and set the value to NULL or 0.
21019 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021020 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021021clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022{
21023 if (varp != NULL)
21024 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021025 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021027 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021028 func_unref(varp->vval.v_string);
21029 /*FALLTHROUGH*/
21030 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021031 vim_free(varp->vval.v_string);
21032 varp->vval.v_string = NULL;
21033 break;
21034 case VAR_LIST:
21035 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021036 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021037 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021038 case VAR_DICT:
21039 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021040 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021041 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021042 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021043 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021044 varp->vval.v_number = 0;
21045 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021046#ifdef FEAT_FLOAT
21047 case VAR_FLOAT:
21048 varp->vval.v_float = 0.0;
21049 break;
21050#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021051 case VAR_UNKNOWN:
21052 break;
21053 default:
21054 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021056 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021057 }
21058}
21059
21060/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021061 * Set the value of a variable to NULL without freeing items.
21062 */
21063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021064init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021065{
21066 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021067 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021068}
21069
21070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 * Get the number value of a variable.
21072 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021073 * For incompatible types, return 0.
21074 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21075 * caller of incompatible types: it sets *denote to TRUE if "denote"
21076 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 */
21078 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021079get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021081 int error = FALSE;
21082
21083 return get_tv_number_chk(varp, &error); /* return 0L on error */
21084}
21085
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021086 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021087get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021088{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021089 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021091 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021093 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021094 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021095#ifdef FEAT_FLOAT
21096 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021097 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021098 break;
21099#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021100 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021101 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021102 break;
21103 case VAR_STRING:
21104 if (varp->vval.v_string != NULL)
21105 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021106 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021107 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021108 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021109 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021110 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021111 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021112 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021113 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021114 case VAR_SPECIAL:
21115 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21116 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021117 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021118 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021119 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021121 if (denote == NULL) /* useful for values that must be unsigned */
21122 n = -1;
21123 else
21124 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021125 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126}
21127
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021128#ifdef FEAT_FLOAT
21129 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021130get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021131{
21132 switch (varp->v_type)
21133 {
21134 case VAR_NUMBER:
21135 return (float_T)(varp->vval.v_number);
21136#ifdef FEAT_FLOAT
21137 case VAR_FLOAT:
21138 return varp->vval.v_float;
21139 break;
21140#endif
21141 case VAR_FUNC:
21142 EMSG(_("E891: Using a Funcref as a Float"));
21143 break;
21144 case VAR_STRING:
21145 EMSG(_("E892: Using a String as a Float"));
21146 break;
21147 case VAR_LIST:
21148 EMSG(_("E893: Using a List as a Float"));
21149 break;
21150 case VAR_DICT:
21151 EMSG(_("E894: Using a Dictionary as a Float"));
21152 break;
21153 default:
21154 EMSG2(_(e_intern2), "get_tv_float()");
21155 break;
21156 }
21157 return 0;
21158}
21159#endif
21160
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021162 * Get the lnum from the first argument.
21163 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021164 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 */
21166 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021167get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168{
Bram Moolenaar33570922005-01-25 22:26:29 +000021169 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170 linenr_T lnum;
21171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021172 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 if (lnum == 0) /* no valid number, try using line() */
21174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021175 rettv.v_type = VAR_NUMBER;
21176 f_line(argvars, &rettv);
21177 lnum = rettv.vval.v_number;
21178 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179 }
21180 return lnum;
21181}
21182
21183/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021184 * Get the lnum from the first argument.
21185 * Also accepts "$", then "buf" is used.
21186 * Returns 0 on error.
21187 */
21188 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021189get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021190{
21191 if (argvars[0].v_type == VAR_STRING
21192 && argvars[0].vval.v_string != NULL
21193 && argvars[0].vval.v_string[0] == '$'
21194 && buf != NULL)
21195 return buf->b_ml.ml_line_count;
21196 return get_tv_number_chk(&argvars[0], NULL);
21197}
21198
21199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200 * Get the string value of a variable.
21201 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021202 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21203 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204 * If the String variable has never been set, return an empty string.
21205 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021206 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21207 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208 */
21209 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021210get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211{
21212 static char_u mybuf[NUMBUFLEN];
21213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021214 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215}
21216
21217 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021218get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021220 char_u *res = get_tv_string_buf_chk(varp, buf);
21221
21222 return res != NULL ? res : (char_u *)"";
21223}
21224
Bram Moolenaar7d647822014-04-05 21:28:56 +020021225/*
21226 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21227 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021228 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021229get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021230{
21231 static char_u mybuf[NUMBUFLEN];
21232
21233 return get_tv_string_buf_chk(varp, mybuf);
21234}
21235
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021236 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021237get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021238{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021239 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021241 case VAR_NUMBER:
21242 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21243 return buf;
21244 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021245 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021246 break;
21247 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021248 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021249 break;
21250 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021251 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021252 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021253#ifdef FEAT_FLOAT
21254 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021255 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021256 break;
21257#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021258 case VAR_STRING:
21259 if (varp->vval.v_string != NULL)
21260 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021261 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021262 case VAR_SPECIAL:
21263 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21264 return buf;
21265
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021266 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021267 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021268 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021270 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271}
21272
21273/*
21274 * Find variable "name" in the list of variables.
21275 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021276 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021277 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021278 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021280 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021281find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021284 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285
Bram Moolenaara7043832005-01-21 11:56:39 +000021286 ht = find_var_ht(name, &varname);
21287 if (htp != NULL)
21288 *htp = ht;
21289 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021291 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292}
21293
21294/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021295 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021296 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021298 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021299find_var_in_ht(
21300 hashtab_T *ht,
21301 int htname,
21302 char_u *varname,
21303 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021304{
Bram Moolenaar33570922005-01-25 22:26:29 +000021305 hashitem_T *hi;
21306
21307 if (*varname == NUL)
21308 {
21309 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021310 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021311 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021312 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021313 case 'g': return &globvars_var;
21314 case 'v': return &vimvars_var;
21315 case 'b': return &curbuf->b_bufvar;
21316 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021317#ifdef FEAT_WINDOWS
21318 case 't': return &curtab->tp_winvar;
21319#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021320 case 'l': return current_funccal == NULL
21321 ? NULL : &current_funccal->l_vars_var;
21322 case 'a': return current_funccal == NULL
21323 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021324 }
21325 return NULL;
21326 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021327
21328 hi = hash_find(ht, varname);
21329 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021330 {
21331 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021332 * worked find the variable again. Don't auto-load a script if it was
21333 * loaded already, otherwise it would be loaded every time when
21334 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021335 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021336 {
21337 /* Note: script_autoload() may make "hi" invalid. It must either
21338 * be obtained again or not used. */
21339 if (!script_autoload(varname, FALSE) || aborting())
21340 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021341 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021342 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021343 if (HASHITEM_EMPTY(hi))
21344 return NULL;
21345 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021346 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021347}
21348
21349/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021350 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021351 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021352 * Set "varname" to the start of name without ':'.
21353 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021354 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021355find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021357 hashitem_T *hi;
21358
Bram Moolenaar73627d02015-08-11 15:46:09 +020021359 if (name[0] == NUL)
21360 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 if (name[1] != ':')
21362 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021363 /* The name must not start with a colon or #. */
21364 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 return NULL;
21366 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021367
21368 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021369 hi = hash_find(&compat_hashtab, name);
21370 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021371 return &compat_hashtab;
21372
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021374 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021375 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376 }
21377 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021378 if (*name == 'g') /* global variable */
21379 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021380 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21381 */
21382 if (vim_strchr(name + 2, ':') != NULL
21383 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021384 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021386 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021388 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021389#ifdef FEAT_WINDOWS
21390 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021391 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021392#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021393 if (*name == 'v') /* v: variable */
21394 return &vimvarht;
21395 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021396 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021397 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021398 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 if (*name == 's' /* script variable */
21400 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21401 return &SCRIPT_VARS(current_SID);
21402 return NULL;
21403}
21404
21405/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021406 * Get function call environment based on bactrace debug level
21407 */
21408 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021409get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021410{
21411 int i;
21412 funccall_T *funccal;
21413 funccall_T *temp_funccal;
21414
21415 funccal = current_funccal;
21416 if (debug_backtrace_level > 0)
21417 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021418 for (i = 0; i < debug_backtrace_level; i++)
21419 {
21420 temp_funccal = funccal->caller;
21421 if (temp_funccal)
21422 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021423 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021424 /* backtrace level overflow. reset to max */
21425 debug_backtrace_level = i;
21426 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021427 }
21428 return funccal;
21429}
21430
21431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021433 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 * Returns NULL when it doesn't exist.
21435 */
21436 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021437get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438{
Bram Moolenaar33570922005-01-25 22:26:29 +000021439 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021441 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 if (v == NULL)
21443 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021444 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445}
21446
21447/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021448 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 * sourcing this script and when executing functions defined in the script.
21450 */
21451 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021452new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453{
Bram Moolenaara7043832005-01-21 11:56:39 +000021454 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021455 hashtab_T *ht;
21456 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021457
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21459 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021460 /* Re-allocating ga_data means that an ht_array pointing to
21461 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021462 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021463 for (i = 1; i <= ga_scripts.ga_len; ++i)
21464 {
21465 ht = &SCRIPT_VARS(i);
21466 if (ht->ht_mask == HT_INIT_SIZE - 1)
21467 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021468 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021469 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021470 }
21471
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 while (ga_scripts.ga_len < id)
21473 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021474 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021475 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021476 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 }
21479 }
21480}
21481
21482/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021483 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21484 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 */
21486 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021487init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488{
Bram Moolenaar33570922005-01-25 22:26:29 +000021489 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021490 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021491 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021492 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021493 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021494 dict_var->di_tv.vval.v_dict = dict;
21495 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021496 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021497 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21498 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499}
21500
21501/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021502 * Unreference a dictionary initialized by init_var_dict().
21503 */
21504 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021505unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021506{
21507 /* Now the dict needs to be freed if no one else is using it, go back to
21508 * normal reference counting. */
21509 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21510 dict_unref(dict);
21511}
21512
21513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021515 * Frees all allocated variables and the value they contain.
21516 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517 */
21518 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021519vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021520{
21521 vars_clear_ext(ht, TRUE);
21522}
21523
21524/*
21525 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21526 */
21527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021528vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529{
Bram Moolenaara7043832005-01-21 11:56:39 +000021530 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021531 hashitem_T *hi;
21532 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533
Bram Moolenaar33570922005-01-25 22:26:29 +000021534 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021535 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021536 for (hi = ht->ht_array; todo > 0; ++hi)
21537 {
21538 if (!HASHITEM_EMPTY(hi))
21539 {
21540 --todo;
21541
Bram Moolenaar33570922005-01-25 22:26:29 +000021542 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021543 * ht_array might change then. hash_clear() takes care of it
21544 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021545 v = HI2DI(hi);
21546 if (free_val)
21547 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021548 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021549 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021550 }
21551 }
21552 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021553 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554}
21555
Bram Moolenaara7043832005-01-21 11:56:39 +000021556/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021557 * Delete a variable from hashtab "ht" at item "hi".
21558 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021561delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562{
Bram Moolenaar33570922005-01-25 22:26:29 +000021563 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021564
21565 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021566 clear_tv(&di->di_tv);
21567 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568}
21569
21570/*
21571 * List the value of one internal variable.
21572 */
21573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021574list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021576 char_u *tofree;
21577 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021578 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021579
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021580 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000021581 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021582 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021583 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584}
21585
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021587list_one_var_a(
21588 char_u *prefix,
21589 char_u *name,
21590 int type,
21591 char_u *string,
21592 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593{
Bram Moolenaar31859182007-08-14 20:41:13 +000021594 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21595 msg_start();
21596 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597 if (name != NULL) /* "a:" vars don't have a name stored */
21598 msg_puts(name);
21599 msg_putchar(' ');
21600 msg_advance(22);
21601 if (type == VAR_NUMBER)
21602 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021603 else if (type == VAR_FUNC)
21604 msg_putchar('*');
21605 else if (type == VAR_LIST)
21606 {
21607 msg_putchar('[');
21608 if (*string == '[')
21609 ++string;
21610 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021611 else if (type == VAR_DICT)
21612 {
21613 msg_putchar('{');
21614 if (*string == '{')
21615 ++string;
21616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617 else
21618 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021619
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021621
21622 if (type == VAR_FUNC)
21623 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021624 if (*first)
21625 {
21626 msg_clr_eos();
21627 *first = FALSE;
21628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629}
21630
21631/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021632 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 * If the variable already exists, the value is updated.
21634 * Otherwise the variable is created.
21635 */
21636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021637set_var(
21638 char_u *name,
21639 typval_T *tv,
21640 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641{
Bram Moolenaar33570922005-01-25 22:26:29 +000021642 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021644 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021646 ht = find_var_ht(name, &varname);
21647 if (ht == NULL || *varname == NUL)
21648 {
21649 EMSG2(_(e_illvar), name);
21650 return;
21651 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021652 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021653
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021654 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21655 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021656
Bram Moolenaar33570922005-01-25 22:26:29 +000021657 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021659 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021660 if (var_check_ro(v->di_flags, name, FALSE)
21661 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021662 return;
21663 if (v->di_tv.v_type != tv->v_type
21664 && !((v->di_tv.v_type == VAR_STRING
21665 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021666 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021667 || tv->v_type == VAR_NUMBER))
21668#ifdef FEAT_FLOAT
21669 && !((v->di_tv.v_type == VAR_NUMBER
21670 || v->di_tv.v_type == VAR_FLOAT)
21671 && (tv->v_type == VAR_NUMBER
21672 || tv->v_type == VAR_FLOAT))
21673#endif
21674 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021675 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021676 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021677 return;
21678 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021679
21680 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021681 * Handle setting internal v: variables separately where needed to
21682 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021683 */
21684 if (ht == &vimvarht)
21685 {
21686 if (v->di_tv.v_type == VAR_STRING)
21687 {
21688 vim_free(v->di_tv.vval.v_string);
21689 if (copy || tv->v_type != VAR_STRING)
21690 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21691 else
21692 {
21693 /* Take over the string to avoid an extra alloc/free. */
21694 v->di_tv.vval.v_string = tv->vval.v_string;
21695 tv->vval.v_string = NULL;
21696 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021697 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021698 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021699 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021700 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021701 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021702 if (STRCMP(varname, "searchforward") == 0)
21703 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021704#ifdef FEAT_SEARCH_EXTRA
21705 else if (STRCMP(varname, "hlsearch") == 0)
21706 {
21707 no_hlsearch = !v->di_tv.vval.v_number;
21708 redraw_all_later(SOME_VALID);
21709 }
21710#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021711 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021712 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021713 else if (v->di_tv.v_type != tv->v_type)
21714 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021715 }
21716
21717 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 }
21719 else /* add a new variable */
21720 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021721 /* Can't add "v:" variable. */
21722 if (ht == &vimvarht)
21723 {
21724 EMSG2(_(e_illvar), name);
21725 return;
21726 }
21727
Bram Moolenaar92124a32005-06-17 22:03:40 +000021728 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021729 if (!valid_varname(varname))
21730 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021731
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021732 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21733 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021734 if (v == NULL)
21735 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021736 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021737 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021739 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 return;
21741 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021742 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021744
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021745 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021746 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021747 else
21748 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021749 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021750 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021751 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753}
21754
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021755/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021756 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021757 * Also give an error message.
21758 */
21759 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021760var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021761{
21762 if (flags & DI_FLAGS_RO)
21763 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021764 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021765 return TRUE;
21766 }
21767 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21768 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021769 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021770 return TRUE;
21771 }
21772 return FALSE;
21773}
21774
21775/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021776 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21777 * Also give an error message.
21778 */
21779 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021780var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021781{
21782 if (flags & DI_FLAGS_FIX)
21783 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021784 EMSG2(_("E795: Cannot delete variable %s"),
21785 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021786 return TRUE;
21787 }
21788 return FALSE;
21789}
21790
21791/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021792 * Check if a funcref is assigned to a valid variable name.
21793 * Return TRUE and give an error if not.
21794 */
21795 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021796var_check_func_name(
21797 char_u *name, /* points to start of variable name */
21798 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021799{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021800 /* Allow for w: b: s: and t:. */
21801 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021802 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21803 ? name[2] : name[0]))
21804 {
21805 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21806 name);
21807 return TRUE;
21808 }
21809 /* Don't allow hiding a function. When "v" is not NULL we might be
21810 * assigning another function to the same var, the type is checked
21811 * below. */
21812 if (new_var && function_exists(name))
21813 {
21814 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21815 name);
21816 return TRUE;
21817 }
21818 return FALSE;
21819}
21820
21821/*
21822 * Check if a variable name is valid.
21823 * Return FALSE and give an error if not.
21824 */
21825 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021826valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021827{
21828 char_u *p;
21829
21830 for (p = varname; *p != NUL; ++p)
21831 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21832 && *p != AUTOLOAD_CHAR)
21833 {
21834 EMSG2(_(e_illvar), varname);
21835 return FALSE;
21836 }
21837 return TRUE;
21838}
21839
21840/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021841 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021842 * Also give an error message, using "name" or _("name") when use_gettext is
21843 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021844 */
21845 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021846tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021847{
21848 if (lock & VAR_LOCKED)
21849 {
21850 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021851 name == NULL ? (char_u *)_("Unknown")
21852 : use_gettext ? (char_u *)_(name)
21853 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021854 return TRUE;
21855 }
21856 if (lock & VAR_FIXED)
21857 {
21858 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021859 name == NULL ? (char_u *)_("Unknown")
21860 : use_gettext ? (char_u *)_(name)
21861 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021862 return TRUE;
21863 }
21864 return FALSE;
21865}
21866
21867/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021868 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021869 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021870 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021871 * It is OK for "from" and "to" to point to the same item. This is used to
21872 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021873 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021874 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021875copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021877 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021878 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021879 switch (from->v_type)
21880 {
21881 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021882 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021883 to->vval.v_number = from->vval.v_number;
21884 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021885#ifdef FEAT_FLOAT
21886 case VAR_FLOAT:
21887 to->vval.v_float = from->vval.v_float;
21888 break;
21889#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021890 case VAR_STRING:
21891 case VAR_FUNC:
21892 if (from->vval.v_string == NULL)
21893 to->vval.v_string = NULL;
21894 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021895 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021896 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021897 if (from->v_type == VAR_FUNC)
21898 func_ref(to->vval.v_string);
21899 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021900 break;
21901 case VAR_LIST:
21902 if (from->vval.v_list == NULL)
21903 to->vval.v_list = NULL;
21904 else
21905 {
21906 to->vval.v_list = from->vval.v_list;
21907 ++to->vval.v_list->lv_refcount;
21908 }
21909 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021910 case VAR_DICT:
21911 if (from->vval.v_dict == NULL)
21912 to->vval.v_dict = NULL;
21913 else
21914 {
21915 to->vval.v_dict = from->vval.v_dict;
21916 ++to->vval.v_dict->dv_refcount;
21917 }
21918 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021919 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021920 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021921 break;
21922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923}
21924
21925/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021926 * Make a copy of an item.
21927 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021928 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21929 * reference to an already copied list/dict can be used.
21930 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021931 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021932 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021933item_copy(
21934 typval_T *from,
21935 typval_T *to,
21936 int deep,
21937 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021938{
21939 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021940 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021941
Bram Moolenaar33570922005-01-25 22:26:29 +000021942 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021943 {
21944 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021945 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021946 }
21947 ++recurse;
21948
21949 switch (from->v_type)
21950 {
21951 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021952#ifdef FEAT_FLOAT
21953 case VAR_FLOAT:
21954#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021955 case VAR_STRING:
21956 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010021957 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000021958 copy_tv(from, to);
21959 break;
21960 case VAR_LIST:
21961 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021962 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021963 if (from->vval.v_list == NULL)
21964 to->vval.v_list = NULL;
21965 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21966 {
21967 /* use the copy made earlier */
21968 to->vval.v_list = from->vval.v_list->lv_copylist;
21969 ++to->vval.v_list->lv_refcount;
21970 }
21971 else
21972 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21973 if (to->vval.v_list == NULL)
21974 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021975 break;
21976 case VAR_DICT:
21977 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021978 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021979 if (from->vval.v_dict == NULL)
21980 to->vval.v_dict = NULL;
21981 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21982 {
21983 /* use the copy made earlier */
21984 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21985 ++to->vval.v_dict->dv_refcount;
21986 }
21987 else
21988 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21989 if (to->vval.v_dict == NULL)
21990 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021991 break;
21992 default:
21993 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021994 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021995 }
21996 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021997 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021998}
21999
22000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001 * ":echo expr1 ..." print each argument separated with a space, add a
22002 * newline at the end.
22003 * ":echon expr1 ..." print each argument plain.
22004 */
22005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022006ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007{
22008 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022009 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022010 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 char_u *p;
22012 int needclr = TRUE;
22013 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022014 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022015
22016 if (eap->skip)
22017 ++emsg_skip;
22018 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22019 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022020 /* If eval1() causes an error message the text from the command may
22021 * still need to be cleared. E.g., "echo 22,44". */
22022 need_clr_eos = needclr;
22023
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022025 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 {
22027 /*
22028 * Report the invalid expression unless the expression evaluation
22029 * has been cancelled due to an aborting error, an interrupt, or an
22030 * exception.
22031 */
22032 if (!aborting())
22033 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022034 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 break;
22036 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022037 need_clr_eos = FALSE;
22038
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039 if (!eap->skip)
22040 {
22041 if (atstart)
22042 {
22043 atstart = FALSE;
22044 /* Call msg_start() after eval1(), evaluating the expression
22045 * may cause a message to appear. */
22046 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022047 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022048 /* Mark the saved text as finishing the line, so that what
22049 * follows is displayed on a new line when scrolling back
22050 * at the more prompt. */
22051 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 }
22055 else if (eap->cmdidx == CMD_echo)
22056 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022057 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022058 if (p != NULL)
22059 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022061 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022063 if (*p != TAB && needclr)
22064 {
22065 /* remove any text still there from the command */
22066 msg_clr_eos();
22067 needclr = FALSE;
22068 }
22069 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 }
22071 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022072 {
22073#ifdef FEAT_MBYTE
22074 if (has_mbyte)
22075 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022076 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022077
22078 (void)msg_outtrans_len_attr(p, i, echo_attr);
22079 p += i - 1;
22080 }
22081 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022083 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022086 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022088 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 arg = skipwhite(arg);
22090 }
22091 eap->nextcmd = check_nextcmd(arg);
22092
22093 if (eap->skip)
22094 --emsg_skip;
22095 else
22096 {
22097 /* remove text that may still be there from the command */
22098 if (needclr)
22099 msg_clr_eos();
22100 if (eap->cmdidx == CMD_echo)
22101 msg_end();
22102 }
22103}
22104
22105/*
22106 * ":echohl {name}".
22107 */
22108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022109ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110{
22111 int id;
22112
22113 id = syn_name2id(eap->arg);
22114 if (id == 0)
22115 echo_attr = 0;
22116 else
22117 echo_attr = syn_id2attr(id);
22118}
22119
22120/*
22121 * ":execute expr1 ..." execute the result of an expression.
22122 * ":echomsg expr1 ..." Print a message
22123 * ":echoerr expr1 ..." Print an error
22124 * Each gets spaces around each argument and a newline at the end for
22125 * echo commands
22126 */
22127 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022128ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129{
22130 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022131 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 int ret = OK;
22133 char_u *p;
22134 garray_T ga;
22135 int len;
22136 int save_did_emsg;
22137
22138 ga_init2(&ga, 1, 80);
22139
22140 if (eap->skip)
22141 ++emsg_skip;
22142 while (*arg != NUL && *arg != '|' && *arg != '\n')
22143 {
22144 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022145 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 {
22147 /*
22148 * Report the invalid expression unless the expression evaluation
22149 * has been cancelled due to an aborting error, an interrupt, or an
22150 * exception.
22151 */
22152 if (!aborting())
22153 EMSG2(_(e_invexpr2), p);
22154 ret = FAIL;
22155 break;
22156 }
22157
22158 if (!eap->skip)
22159 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022160 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 len = (int)STRLEN(p);
22162 if (ga_grow(&ga, len + 2) == FAIL)
22163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022164 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165 ret = FAIL;
22166 break;
22167 }
22168 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 ga.ga_len += len;
22172 }
22173
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022174 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 arg = skipwhite(arg);
22176 }
22177
22178 if (ret != FAIL && ga.ga_data != NULL)
22179 {
22180 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022183 out_flush();
22184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185 else if (eap->cmdidx == CMD_echoerr)
22186 {
22187 /* We don't want to abort following commands, restore did_emsg. */
22188 save_did_emsg = did_emsg;
22189 EMSG((char_u *)ga.ga_data);
22190 if (!force_abort)
22191 did_emsg = save_did_emsg;
22192 }
22193 else if (eap->cmdidx == CMD_execute)
22194 do_cmdline((char_u *)ga.ga_data,
22195 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22196 }
22197
22198 ga_clear(&ga);
22199
22200 if (eap->skip)
22201 --emsg_skip;
22202
22203 eap->nextcmd = check_nextcmd(arg);
22204}
22205
22206/*
22207 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22208 * "arg" points to the "&" or '+' when called, to "option" when returning.
22209 * Returns NULL when no option name found. Otherwise pointer to the char
22210 * after the option name.
22211 */
22212 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022213find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214{
22215 char_u *p = *arg;
22216
22217 ++p;
22218 if (*p == 'g' && p[1] == ':')
22219 {
22220 *opt_flags = OPT_GLOBAL;
22221 p += 2;
22222 }
22223 else if (*p == 'l' && p[1] == ':')
22224 {
22225 *opt_flags = OPT_LOCAL;
22226 p += 2;
22227 }
22228 else
22229 *opt_flags = 0;
22230
22231 if (!ASCII_ISALPHA(*p))
22232 return NULL;
22233 *arg = p;
22234
22235 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22236 p += 4; /* termcap option */
22237 else
22238 while (ASCII_ISALPHA(*p))
22239 ++p;
22240 return p;
22241}
22242
22243/*
22244 * ":function"
22245 */
22246 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022247ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248{
22249 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022250 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251 int j;
22252 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022254 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 char_u *name = NULL;
22256 char_u *p;
22257 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022258 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259 garray_T newargs;
22260 garray_T newlines;
22261 int varargs = FALSE;
22262 int mustend = FALSE;
22263 int flags = 0;
22264 ufunc_T *fp;
22265 int indent;
22266 int nesting;
22267 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022268 dictitem_T *v;
22269 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022270 static int func_nr = 0; /* number for nameless function */
22271 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022272 hashtab_T *ht;
22273 int todo;
22274 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022275 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276
22277 /*
22278 * ":function" without argument: list functions.
22279 */
22280 if (ends_excmd(*eap->arg))
22281 {
22282 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022283 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022284 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022285 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022286 {
22287 if (!HASHITEM_EMPTY(hi))
22288 {
22289 --todo;
22290 fp = HI2UF(hi);
22291 if (!isdigit(*fp->uf_name))
22292 list_func_head(fp, FALSE);
22293 }
22294 }
22295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296 eap->nextcmd = check_nextcmd(eap->arg);
22297 return;
22298 }
22299
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022300 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022301 * ":function /pat": list functions matching pattern.
22302 */
22303 if (*eap->arg == '/')
22304 {
22305 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22306 if (!eap->skip)
22307 {
22308 regmatch_T regmatch;
22309
22310 c = *p;
22311 *p = NUL;
22312 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22313 *p = c;
22314 if (regmatch.regprog != NULL)
22315 {
22316 regmatch.rm_ic = p_ic;
22317
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022318 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022319 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22320 {
22321 if (!HASHITEM_EMPTY(hi))
22322 {
22323 --todo;
22324 fp = HI2UF(hi);
22325 if (!isdigit(*fp->uf_name)
22326 && vim_regexec(&regmatch, fp->uf_name, 0))
22327 list_func_head(fp, FALSE);
22328 }
22329 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022330 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022331 }
22332 }
22333 if (*p == '/')
22334 ++p;
22335 eap->nextcmd = check_nextcmd(p);
22336 return;
22337 }
22338
22339 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022340 * Get the function name. There are these situations:
22341 * func normal function name
22342 * "name" == func, "fudi.fd_dict" == NULL
22343 * dict.func new dictionary entry
22344 * "name" == NULL, "fudi.fd_dict" set,
22345 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22346 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022347 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022348 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22349 * dict.func existing dict entry that's not a Funcref
22350 * "name" == NULL, "fudi.fd_dict" set,
22351 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022352 * s:func script-local function name
22353 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022356 name = trans_function_name(&p, eap->skip, 0, &fudi);
22357 paren = (vim_strchr(p, '(') != NULL);
22358 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022359 {
22360 /*
22361 * Return on an invalid expression in braces, unless the expression
22362 * evaluation has been cancelled due to an aborting error, an
22363 * interrupt, or an exception.
22364 */
22365 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022366 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022367 if (!eap->skip && fudi.fd_newkey != NULL)
22368 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022369 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 else
22373 eap->skip = TRUE;
22374 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022375
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 /* An error in a function call during evaluation of an expression in magic
22377 * braces should not cause the function not to be defined. */
22378 saved_did_emsg = did_emsg;
22379 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380
22381 /*
22382 * ":function func" with only function name: list function.
22383 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022384 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 {
22386 if (!ends_excmd(*skipwhite(p)))
22387 {
22388 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022389 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 }
22391 eap->nextcmd = check_nextcmd(p);
22392 if (eap->nextcmd != NULL)
22393 *p = NUL;
22394 if (!eap->skip && !got_int)
22395 {
22396 fp = find_func(name);
22397 if (fp != NULL)
22398 {
22399 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022400 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022402 if (FUNCLINE(fp, j) == NULL)
22403 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022404 msg_putchar('\n');
22405 msg_outnum((long)(j + 1));
22406 if (j < 9)
22407 msg_putchar(' ');
22408 if (j < 99)
22409 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022410 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411 out_flush(); /* show a line at a time */
22412 ui_breakcheck();
22413 }
22414 if (!got_int)
22415 {
22416 msg_putchar('\n');
22417 msg_puts((char_u *)" endfunction");
22418 }
22419 }
22420 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022421 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022423 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424 }
22425
22426 /*
22427 * ":function name(arg1, arg2)" Define function.
22428 */
22429 p = skipwhite(p);
22430 if (*p != '(')
22431 {
22432 if (!eap->skip)
22433 {
22434 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022435 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 }
22437 /* attempt to continue by skipping some text */
22438 if (vim_strchr(p, '(') != NULL)
22439 p = vim_strchr(p, '(');
22440 }
22441 p = skipwhite(p + 1);
22442
22443 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22444 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22445
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022446 if (!eap->skip)
22447 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022448 /* Check the name of the function. Unless it's a dictionary function
22449 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022450 if (name != NULL)
22451 arg = name;
22452 else
22453 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022454 if (arg != NULL && (fudi.fd_di == NULL
22455 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022456 {
22457 if (*arg == K_SPECIAL)
22458 j = 3;
22459 else
22460 j = 0;
22461 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22462 : eval_isnamec(arg[j])))
22463 ++j;
22464 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022465 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022466 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022467 /* Disallow using the g: dict. */
22468 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22469 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022470 }
22471
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 /*
22473 * Isolate the arguments: "arg1, arg2, ...)"
22474 */
22475 while (*p != ')')
22476 {
22477 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22478 {
22479 varargs = TRUE;
22480 p += 3;
22481 mustend = TRUE;
22482 }
22483 else
22484 {
22485 arg = p;
22486 while (ASCII_ISALNUM(*p) || *p == '_')
22487 ++p;
22488 if (arg == p || isdigit(*arg)
22489 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22490 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22491 {
22492 if (!eap->skip)
22493 EMSG2(_("E125: Illegal argument: %s"), arg);
22494 break;
22495 }
22496 if (ga_grow(&newargs, 1) == FAIL)
22497 goto erret;
22498 c = *p;
22499 *p = NUL;
22500 arg = vim_strsave(arg);
22501 if (arg == NULL)
22502 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022503
22504 /* Check for duplicate argument name. */
22505 for (i = 0; i < newargs.ga_len; ++i)
22506 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22507 {
22508 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022509 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022510 goto erret;
22511 }
22512
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22514 *p = c;
22515 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516 if (*p == ',')
22517 ++p;
22518 else
22519 mustend = TRUE;
22520 }
22521 p = skipwhite(p);
22522 if (mustend && *p != ')')
22523 {
22524 if (!eap->skip)
22525 EMSG2(_(e_invarg2), eap->arg);
22526 break;
22527 }
22528 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022529 if (*p != ')')
22530 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 ++p; /* skip the ')' */
22532
Bram Moolenaare9a41262005-01-15 22:18:47 +000022533 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 for (;;)
22535 {
22536 p = skipwhite(p);
22537 if (STRNCMP(p, "range", 5) == 0)
22538 {
22539 flags |= FC_RANGE;
22540 p += 5;
22541 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022542 else if (STRNCMP(p, "dict", 4) == 0)
22543 {
22544 flags |= FC_DICT;
22545 p += 4;
22546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 else if (STRNCMP(p, "abort", 5) == 0)
22548 {
22549 flags |= FC_ABORT;
22550 p += 5;
22551 }
22552 else
22553 break;
22554 }
22555
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022556 /* When there is a line break use what follows for the function body.
22557 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22558 if (*p == '\n')
22559 line_arg = p + 1;
22560 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 EMSG(_(e_trailing));
22562
22563 /*
22564 * Read the body of the function, until ":endfunction" is found.
22565 */
22566 if (KeyTyped)
22567 {
22568 /* Check if the function already exists, don't let the user type the
22569 * whole function before telling him it doesn't work! For a script we
22570 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022571 if (!eap->skip && !eap->forceit)
22572 {
22573 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22574 EMSG(_(e_funcdict));
22575 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022576 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022579 if (!eap->skip && did_emsg)
22580 goto erret;
22581
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 msg_putchar('\n'); /* don't overwrite the function name */
22583 cmdline_row = msg_row;
22584 }
22585
22586 indent = 2;
22587 nesting = 0;
22588 for (;;)
22589 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022590 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022591 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022592 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022593 saved_wait_return = FALSE;
22594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022596 sourcing_lnum_off = sourcing_lnum;
22597
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022598 if (line_arg != NULL)
22599 {
22600 /* Use eap->arg, split up in parts by line breaks. */
22601 theline = line_arg;
22602 p = vim_strchr(theline, '\n');
22603 if (p == NULL)
22604 line_arg += STRLEN(line_arg);
22605 else
22606 {
22607 *p = NUL;
22608 line_arg = p + 1;
22609 }
22610 }
22611 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 theline = getcmdline(':', 0L, indent);
22613 else
22614 theline = eap->getline(':', eap->cookie, indent);
22615 if (KeyTyped)
22616 lines_left = Rows - 1;
22617 if (theline == NULL)
22618 {
22619 EMSG(_("E126: Missing :endfunction"));
22620 goto erret;
22621 }
22622
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022623 /* Detect line continuation: sourcing_lnum increased more than one. */
22624 if (sourcing_lnum > sourcing_lnum_off + 1)
22625 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22626 else
22627 sourcing_lnum_off = 0;
22628
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 if (skip_until != NULL)
22630 {
22631 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22632 * don't check for ":endfunc". */
22633 if (STRCMP(theline, skip_until) == 0)
22634 {
22635 vim_free(skip_until);
22636 skip_until = NULL;
22637 }
22638 }
22639 else
22640 {
22641 /* skip ':' and blanks*/
22642 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22643 ;
22644
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022645 /* Check for "endfunction". */
22646 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022648 if (line_arg == NULL)
22649 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650 break;
22651 }
22652
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022653 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 * at "end". */
22655 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22656 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022657 else if (STRNCMP(p, "if", 2) == 0
22658 || STRNCMP(p, "wh", 2) == 0
22659 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 || STRNCMP(p, "try", 3) == 0)
22661 indent += 2;
22662
22663 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022664 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022666 if (*p == '!')
22667 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022669 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22670 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022672 ++nesting;
22673 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022674 }
22675 }
22676
22677 /* Check for ":append" or ":insert". */
22678 p = skip_range(p, NULL);
22679 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22680 || (p[0] == 'i'
22681 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22682 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22683 skip_until = vim_strsave((char_u *)".");
22684
22685 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22686 arg = skipwhite(skiptowhite(p));
22687 if (arg[0] == '<' && arg[1] =='<'
22688 && ((p[0] == 'p' && p[1] == 'y'
22689 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22690 || (p[0] == 'p' && p[1] == 'e'
22691 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22692 || (p[0] == 't' && p[1] == 'c'
22693 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022694 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22695 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22697 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022698 || (p[0] == 'm' && p[1] == 'z'
22699 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700 ))
22701 {
22702 /* ":python <<" continues until a dot, like ":append" */
22703 p = skipwhite(arg + 2);
22704 if (*p == NUL)
22705 skip_until = vim_strsave((char_u *)".");
22706 else
22707 skip_until = vim_strsave(p);
22708 }
22709 }
22710
22711 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022712 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022713 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022714 if (line_arg == NULL)
22715 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022717 }
22718
22719 /* Copy the line to newly allocated memory. get_one_sourceline()
22720 * allocates 250 bytes per line, this saves 80% on average. The cost
22721 * is an extra alloc/free. */
22722 p = vim_strsave(theline);
22723 if (p != NULL)
22724 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022725 if (line_arg == NULL)
22726 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022727 theline = p;
22728 }
22729
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022730 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22731
22732 /* Add NULL lines for continuation lines, so that the line count is
22733 * equal to the index in the growarray. */
22734 while (sourcing_lnum_off-- > 0)
22735 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022736
22737 /* Check for end of eap->arg. */
22738 if (line_arg != NULL && *line_arg == NUL)
22739 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 }
22741
22742 /* Don't define the function when skipping commands or when an error was
22743 * detected. */
22744 if (eap->skip || did_emsg)
22745 goto erret;
22746
22747 /*
22748 * If there are no errors, add the function
22749 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022750 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022751 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022752 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022753 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022754 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022755 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022756 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022757 goto erret;
22758 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022759
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022760 fp = find_func(name);
22761 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022763 if (!eap->forceit)
22764 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022765 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022766 goto erret;
22767 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022768 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022769 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022770 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022771 name);
22772 goto erret;
22773 }
22774 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022775 ga_clear_strings(&(fp->uf_args));
22776 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022777 vim_free(name);
22778 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780 }
22781 else
22782 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022783 char numbuf[20];
22784
22785 fp = NULL;
22786 if (fudi.fd_newkey == NULL && !eap->forceit)
22787 {
22788 EMSG(_(e_funcdict));
22789 goto erret;
22790 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022791 if (fudi.fd_di == NULL)
22792 {
22793 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022794 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022795 goto erret;
22796 }
22797 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022798 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022799 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022800
22801 /* Give the function a sequential number. Can only be used with a
22802 * Funcref! */
22803 vim_free(name);
22804 sprintf(numbuf, "%d", ++func_nr);
22805 name = vim_strsave((char_u *)numbuf);
22806 if (name == NULL)
22807 goto erret;
22808 }
22809
22810 if (fp == NULL)
22811 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022812 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022813 {
22814 int slen, plen;
22815 char_u *scriptname;
22816
22817 /* Check that the autoload name matches the script name. */
22818 j = FAIL;
22819 if (sourcing_name != NULL)
22820 {
22821 scriptname = autoload_name(name);
22822 if (scriptname != NULL)
22823 {
22824 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022825 plen = (int)STRLEN(p);
22826 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022827 if (slen > plen && fnamecmp(p,
22828 sourcing_name + slen - plen) == 0)
22829 j = OK;
22830 vim_free(scriptname);
22831 }
22832 }
22833 if (j == FAIL)
22834 {
22835 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22836 goto erret;
22837 }
22838 }
22839
22840 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 if (fp == NULL)
22842 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022843
22844 if (fudi.fd_dict != NULL)
22845 {
22846 if (fudi.fd_di == NULL)
22847 {
22848 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022849 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022850 if (fudi.fd_di == NULL)
22851 {
22852 vim_free(fp);
22853 goto erret;
22854 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022855 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22856 {
22857 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022858 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022859 goto erret;
22860 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022861 }
22862 else
22863 /* overwrite existing dict entry */
22864 clear_tv(&fudi.fd_di->di_tv);
22865 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022866 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022867 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022868 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022869
22870 /* behave like "dict" was used */
22871 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022872 }
22873
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022875 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010022876 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
22877 {
22878 vim_free(fp);
22879 goto erret;
22880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022882 fp->uf_args = newargs;
22883 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022884#ifdef FEAT_PROFILE
22885 fp->uf_tml_count = NULL;
22886 fp->uf_tml_total = NULL;
22887 fp->uf_tml_self = NULL;
22888 fp->uf_profiling = FALSE;
22889 if (prof_def_func())
22890 func_do_profile(fp);
22891#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022892 fp->uf_varargs = varargs;
22893 fp->uf_flags = flags;
22894 fp->uf_calls = 0;
22895 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022896 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897
22898erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899 ga_clear_strings(&newargs);
22900 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022901ret_free:
22902 vim_free(skip_until);
22903 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022906 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907}
22908
22909/*
22910 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022911 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022913 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010022914 * TFN_INT: internal function name OK
22915 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022916 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917 * Advances "pp" to just after the function name (if no error).
22918 */
22919 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022920trans_function_name(
22921 char_u **pp,
22922 int skip, /* only find the end, don't evaluate */
22923 int flags,
22924 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022926 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 char_u *start;
22928 char_u *end;
22929 int lead;
22930 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022932 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022933
22934 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022935 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022937
22938 /* Check for hard coded <SNR>: already translated function ID (from a user
22939 * command). */
22940 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22941 && (*pp)[2] == (int)KE_SNR)
22942 {
22943 *pp += 3;
22944 len = get_id_len(pp) + 3;
22945 return vim_strnsave(start, len);
22946 }
22947
22948 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22949 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022951 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022953
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022954 /* Note that TFN_ flags use the same values as GLV_ flags. */
22955 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022956 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022957 if (end == start)
22958 {
22959 if (!skip)
22960 EMSG(_("E129: Function name required"));
22961 goto theend;
22962 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022963 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022964 {
22965 /*
22966 * Report an invalid expression in braces, unless the expression
22967 * evaluation has been cancelled due to an aborting error, an
22968 * interrupt, or an exception.
22969 */
22970 if (!aborting())
22971 {
22972 if (end != NULL)
22973 EMSG2(_(e_invarg2), start);
22974 }
22975 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022976 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022977 goto theend;
22978 }
22979
22980 if (lv.ll_tv != NULL)
22981 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022982 if (fdp != NULL)
22983 {
22984 fdp->fd_dict = lv.ll_dict;
22985 fdp->fd_newkey = lv.ll_newkey;
22986 lv.ll_newkey = NULL;
22987 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022989 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22990 {
22991 name = vim_strsave(lv.ll_tv->vval.v_string);
22992 *pp = end;
22993 }
22994 else
22995 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022996 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22997 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022998 EMSG(_(e_funcref));
22999 else
23000 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023001 name = NULL;
23002 }
23003 goto theend;
23004 }
23005
23006 if (lv.ll_name == NULL)
23007 {
23008 /* Error found, but continue after the function name. */
23009 *pp = end;
23010 goto theend;
23011 }
23012
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023013 /* Check if the name is a Funcref. If so, use the value. */
23014 if (lv.ll_exp_name != NULL)
23015 {
23016 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023017 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023018 if (name == lv.ll_exp_name)
23019 name = NULL;
23020 }
23021 else
23022 {
23023 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023024 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023025 if (name == *pp)
23026 name = NULL;
23027 }
23028 if (name != NULL)
23029 {
23030 name = vim_strsave(name);
23031 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023032 if (STRNCMP(name, "<SNR>", 5) == 0)
23033 {
23034 /* Change "<SNR>" to the byte sequence. */
23035 name[0] = K_SPECIAL;
23036 name[1] = KS_EXTRA;
23037 name[2] = (int)KE_SNR;
23038 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23039 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023040 goto theend;
23041 }
23042
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023043 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023044 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023045 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023046 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23047 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23048 {
23049 /* When there was "s:" already or the name expanded to get a
23050 * leading "s:" then remove it. */
23051 lv.ll_name += 2;
23052 len -= 2;
23053 lead = 2;
23054 }
23055 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023056 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023057 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023058 /* skip over "s:" and "g:" */
23059 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023060 lv.ll_name += 2;
23061 len = (int)(end - lv.ll_name);
23062 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023063
23064 /*
23065 * Copy the function name to allocated memory.
23066 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23067 * Accept <SNR>123_name() outside a script.
23068 */
23069 if (skip)
23070 lead = 0; /* do nothing */
23071 else if (lead > 0)
23072 {
23073 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023074 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23075 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023076 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023077 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023078 if (current_SID <= 0)
23079 {
23080 EMSG(_(e_usingsid));
23081 goto theend;
23082 }
23083 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23084 lead += (int)STRLEN(sid_buf);
23085 }
23086 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023087 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023088 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023089 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023090 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023091 goto theend;
23092 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023093 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023094 {
23095 char_u *cp = vim_strchr(lv.ll_name, ':');
23096
23097 if (cp != NULL && cp < end)
23098 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023099 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023100 goto theend;
23101 }
23102 }
23103
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023104 name = alloc((unsigned)(len + lead + 1));
23105 if (name != NULL)
23106 {
23107 if (lead > 0)
23108 {
23109 name[0] = K_SPECIAL;
23110 name[1] = KS_EXTRA;
23111 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023112 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023113 STRCPY(name + 3, sid_buf);
23114 }
23115 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023116 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023117 }
23118 *pp = end;
23119
23120theend:
23121 clear_lval(&lv);
23122 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123}
23124
23125/*
23126 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23127 * Return 2 if "p" starts with "s:".
23128 * Return 0 otherwise.
23129 */
23130 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023131eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023133 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23134 * the standard library function. */
23135 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23136 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 return 5;
23138 if (p[0] == 's' && p[1] == ':')
23139 return 2;
23140 return 0;
23141}
23142
23143/*
23144 * Return TRUE if "p" starts with "<SID>" or "s:".
23145 * Only works if eval_fname_script() returned non-zero for "p"!
23146 */
23147 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023148eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149{
23150 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23151}
23152
23153/*
23154 * List the head of the function: "name(arg1, arg2)".
23155 */
23156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023157list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158{
23159 int j;
23160
23161 msg_start();
23162 if (indent)
23163 MSG_PUTS(" ");
23164 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023165 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 {
23167 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023168 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169 }
23170 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023171 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023173 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174 {
23175 if (j)
23176 MSG_PUTS(", ");
23177 msg_puts(FUNCARG(fp, j));
23178 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023179 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 {
23181 if (j)
23182 MSG_PUTS(", ");
23183 MSG_PUTS("...");
23184 }
23185 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023186 if (fp->uf_flags & FC_ABORT)
23187 MSG_PUTS(" abort");
23188 if (fp->uf_flags & FC_RANGE)
23189 MSG_PUTS(" range");
23190 if (fp->uf_flags & FC_DICT)
23191 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023192 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023193 if (p_verbose > 0)
23194 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195}
23196
23197/*
23198 * Find a function by name, return pointer to it in ufuncs.
23199 * Return NULL for unknown function.
23200 */
23201 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023202find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023204 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023206 hi = hash_find(&func_hashtab, name);
23207 if (!HASHITEM_EMPTY(hi))
23208 return HI2UF(hi);
23209 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210}
23211
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023212#if defined(EXITFREE) || defined(PROTO)
23213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023214free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023215{
23216 hashitem_T *hi;
23217
23218 /* Need to start all over every time, because func_free() may change the
23219 * hash table. */
23220 while (func_hashtab.ht_used > 0)
23221 for (hi = func_hashtab.ht_array; ; ++hi)
23222 if (!HASHITEM_EMPTY(hi))
23223 {
23224 func_free(HI2UF(hi));
23225 break;
23226 }
23227}
23228#endif
23229
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023230 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023231translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023232{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023233 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023234 return find_internal_func(name) >= 0;
23235 return find_func(name) != NULL;
23236}
23237
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023238/*
23239 * Return TRUE if a function "name" exists.
23240 */
23241 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023242function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023243{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023244 char_u *nm = name;
23245 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023246 int n = FALSE;
23247
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023248 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23249 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023250 nm = skipwhite(nm);
23251
23252 /* Only accept "funcname", "funcname ", "funcname (..." and
23253 * "funcname(...", not "funcname!...". */
23254 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023255 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023256 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023257 return n;
23258}
23259
Bram Moolenaara1544c02013-05-30 12:35:52 +020023260 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023261get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023262{
23263 char_u *nm = name;
23264 char_u *p;
23265
23266 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23267
23268 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023269 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023270 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023271
Bram Moolenaara1544c02013-05-30 12:35:52 +020023272 vim_free(p);
23273 return NULL;
23274}
23275
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023276/*
23277 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023278 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23279 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023280 */
23281 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023282builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023283{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023284 char_u *p;
23285
23286 if (!ASCII_ISLOWER(name[0]))
23287 return FALSE;
23288 p = vim_strchr(name, AUTOLOAD_CHAR);
23289 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023290}
23291
Bram Moolenaar05159a02005-02-26 23:04:13 +000023292#if defined(FEAT_PROFILE) || defined(PROTO)
23293/*
23294 * Start profiling function "fp".
23295 */
23296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023297func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023298{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023299 int len = fp->uf_lines.ga_len;
23300
23301 if (len == 0)
23302 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023303 fp->uf_tm_count = 0;
23304 profile_zero(&fp->uf_tm_self);
23305 profile_zero(&fp->uf_tm_total);
23306 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023307 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023308 if (fp->uf_tml_total == NULL)
23309 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023310 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023311 if (fp->uf_tml_self == NULL)
23312 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023313 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023314 fp->uf_tml_idx = -1;
23315 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23316 || fp->uf_tml_self == NULL)
23317 return; /* out of memory */
23318
23319 fp->uf_profiling = TRUE;
23320}
23321
23322/*
23323 * Dump the profiling results for all functions in file "fd".
23324 */
23325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023326func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023327{
23328 hashitem_T *hi;
23329 int todo;
23330 ufunc_T *fp;
23331 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023332 ufunc_T **sorttab;
23333 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023334
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023335 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023336 if (todo == 0)
23337 return; /* nothing to dump */
23338
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023339 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023340
Bram Moolenaar05159a02005-02-26 23:04:13 +000023341 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23342 {
23343 if (!HASHITEM_EMPTY(hi))
23344 {
23345 --todo;
23346 fp = HI2UF(hi);
23347 if (fp->uf_profiling)
23348 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023349 if (sorttab != NULL)
23350 sorttab[st_len++] = fp;
23351
Bram Moolenaar05159a02005-02-26 23:04:13 +000023352 if (fp->uf_name[0] == K_SPECIAL)
23353 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23354 else
23355 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23356 if (fp->uf_tm_count == 1)
23357 fprintf(fd, "Called 1 time\n");
23358 else
23359 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23360 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23361 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23362 fprintf(fd, "\n");
23363 fprintf(fd, "count total (s) self (s)\n");
23364
23365 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23366 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023367 if (FUNCLINE(fp, i) == NULL)
23368 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023369 prof_func_line(fd, fp->uf_tml_count[i],
23370 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023371 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23372 }
23373 fprintf(fd, "\n");
23374 }
23375 }
23376 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023377
23378 if (sorttab != NULL && st_len > 0)
23379 {
23380 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23381 prof_total_cmp);
23382 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23383 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23384 prof_self_cmp);
23385 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23386 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023387
23388 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023389}
Bram Moolenaar73830342005-02-28 22:48:19 +000023390
23391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023392prof_sort_list(
23393 FILE *fd,
23394 ufunc_T **sorttab,
23395 int st_len,
23396 char *title,
23397 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023398{
23399 int i;
23400 ufunc_T *fp;
23401
23402 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23403 fprintf(fd, "count total (s) self (s) function\n");
23404 for (i = 0; i < 20 && i < st_len; ++i)
23405 {
23406 fp = sorttab[i];
23407 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23408 prefer_self);
23409 if (fp->uf_name[0] == K_SPECIAL)
23410 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23411 else
23412 fprintf(fd, " %s()\n", fp->uf_name);
23413 }
23414 fprintf(fd, "\n");
23415}
23416
23417/*
23418 * Print the count and times for one function or function line.
23419 */
23420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023421prof_func_line(
23422 FILE *fd,
23423 int count,
23424 proftime_T *total,
23425 proftime_T *self,
23426 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023427{
23428 if (count > 0)
23429 {
23430 fprintf(fd, "%5d ", count);
23431 if (prefer_self && profile_equal(total, self))
23432 fprintf(fd, " ");
23433 else
23434 fprintf(fd, "%s ", profile_msg(total));
23435 if (!prefer_self && profile_equal(total, self))
23436 fprintf(fd, " ");
23437 else
23438 fprintf(fd, "%s ", profile_msg(self));
23439 }
23440 else
23441 fprintf(fd, " ");
23442}
23443
23444/*
23445 * Compare function for total time sorting.
23446 */
23447 static int
23448#ifdef __BORLANDC__
23449_RTLENTRYF
23450#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023451prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023452{
23453 ufunc_T *p1, *p2;
23454
23455 p1 = *(ufunc_T **)s1;
23456 p2 = *(ufunc_T **)s2;
23457 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23458}
23459
23460/*
23461 * Compare function for self time sorting.
23462 */
23463 static int
23464#ifdef __BORLANDC__
23465_RTLENTRYF
23466#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023467prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023468{
23469 ufunc_T *p1, *p2;
23470
23471 p1 = *(ufunc_T **)s1;
23472 p2 = *(ufunc_T **)s2;
23473 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23474}
23475
Bram Moolenaar05159a02005-02-26 23:04:13 +000023476#endif
23477
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023478/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023479 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023480 * Return TRUE if a package was loaded.
23481 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023482 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023483script_autoload(
23484 char_u *name,
23485 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023486{
23487 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023488 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023489 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023490 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023491
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023492 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023493 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023494 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023495 return FALSE;
23496
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023497 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023498
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023499 /* Find the name in the list of previously loaded package names. Skip
23500 * "autoload/", it's always the same. */
23501 for (i = 0; i < ga_loaded.ga_len; ++i)
23502 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23503 break;
23504 if (!reload && i < ga_loaded.ga_len)
23505 ret = FALSE; /* was loaded already */
23506 else
23507 {
23508 /* Remember the name if it wasn't loaded already. */
23509 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23510 {
23511 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23512 tofree = NULL;
23513 }
23514
23515 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023516 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023517 ret = TRUE;
23518 }
23519
23520 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023521 return ret;
23522}
23523
23524/*
23525 * Return the autoload script name for a function or variable name.
23526 * Returns NULL when out of memory.
23527 */
23528 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023529autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023530{
23531 char_u *p;
23532 char_u *scriptname;
23533
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023534 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023535 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23536 if (scriptname == NULL)
23537 return FALSE;
23538 STRCPY(scriptname, "autoload/");
23539 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023540 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023541 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023542 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023543 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023544 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023545}
23546
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23548
23549/*
23550 * Function given to ExpandGeneric() to obtain the list of user defined
23551 * function names.
23552 */
23553 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023554get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023556 static long_u done;
23557 static hashitem_T *hi;
23558 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559
23560 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023562 done = 0;
23563 hi = func_hashtab.ht_array;
23564 }
23565 if (done < func_hashtab.ht_used)
23566 {
23567 if (done++ > 0)
23568 ++hi;
23569 while (HASHITEM_EMPTY(hi))
23570 ++hi;
23571 fp = HI2UF(hi);
23572
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023573 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023574 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023575
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023576 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23577 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578
23579 cat_func_name(IObuff, fp);
23580 if (xp->xp_context != EXPAND_USER_FUNC)
23581 {
23582 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023583 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023584 STRCAT(IObuff, ")");
23585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586 return IObuff;
23587 }
23588 return NULL;
23589}
23590
23591#endif /* FEAT_CMDL_COMPL */
23592
23593/*
23594 * Copy the function name of "fp" to buffer "buf".
23595 * "buf" must be able to hold the function name plus three bytes.
23596 * Takes care of script-local function names.
23597 */
23598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023599cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023601 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023602 {
23603 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023604 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605 }
23606 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023607 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608}
23609
23610/*
23611 * ":delfunction {name}"
23612 */
23613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023614ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023616 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 char_u *p;
23618 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023619 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620
23621 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023622 name = trans_function_name(&p, eap->skip, 0, &fudi);
23623 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023625 {
23626 if (fudi.fd_dict != NULL && !eap->skip)
23627 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630 if (!ends_excmd(*skipwhite(p)))
23631 {
23632 vim_free(name);
23633 EMSG(_(e_trailing));
23634 return;
23635 }
23636 eap->nextcmd = check_nextcmd(p);
23637 if (eap->nextcmd != NULL)
23638 *p = NUL;
23639
23640 if (!eap->skip)
23641 fp = find_func(name);
23642 vim_free(name);
23643
23644 if (!eap->skip)
23645 {
23646 if (fp == NULL)
23647 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023648 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 return;
23650 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023651 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652 {
23653 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23654 return;
23655 }
23656
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023657 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023659 /* Delete the dict item that refers to the function, it will
23660 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023661 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023662 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023663 else
23664 func_free(fp);
23665 }
23666}
23667
23668/*
23669 * Free a function and remove it from the list of functions.
23670 */
23671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023672func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023673{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023674 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023675
23676 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023677 ga_clear_strings(&(fp->uf_args));
23678 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023679#ifdef FEAT_PROFILE
23680 vim_free(fp->uf_tml_count);
23681 vim_free(fp->uf_tml_total);
23682 vim_free(fp->uf_tml_self);
23683#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023684
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023685 /* remove the function from the function hashtable */
23686 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23687 if (HASHITEM_EMPTY(hi))
23688 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023689 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023690 hash_remove(&func_hashtab, hi);
23691
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023692 vim_free(fp);
23693}
23694
23695/*
23696 * Unreference a Function: decrement the reference count and free it when it
23697 * becomes zero. Only for numbered functions.
23698 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023699 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023700func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023701{
23702 ufunc_T *fp;
23703
23704 if (name != NULL && isdigit(*name))
23705 {
23706 fp = find_func(name);
23707 if (fp == NULL)
23708 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023709 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023710 {
23711 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023712 * when "uf_calls" becomes zero. */
23713 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023714 func_free(fp);
23715 }
23716 }
23717}
23718
23719/*
23720 * Count a reference to a Function.
23721 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023722 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023723func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023724{
23725 ufunc_T *fp;
23726
23727 if (name != NULL && isdigit(*name))
23728 {
23729 fp = find_func(name);
23730 if (fp == NULL)
23731 EMSG2(_(e_intern2), "func_ref()");
23732 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023733 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734 }
23735}
23736
23737/*
23738 * Call a user function.
23739 */
23740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023741call_user_func(
23742 ufunc_T *fp, /* pointer to function */
23743 int argcount, /* nr of args */
23744 typval_T *argvars, /* arguments */
23745 typval_T *rettv, /* return value */
23746 linenr_T firstline, /* first line of range */
23747 linenr_T lastline, /* last line of range */
23748 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023749{
Bram Moolenaar33570922005-01-25 22:26:29 +000023750 char_u *save_sourcing_name;
23751 linenr_T save_sourcing_lnum;
23752 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023753 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023754 int save_did_emsg;
23755 static int depth = 0;
23756 dictitem_T *v;
23757 int fixvar_idx = 0; /* index in fixvar[] */
23758 int i;
23759 int ai;
23760 char_u numbuf[NUMBUFLEN];
23761 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023762 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023763#ifdef FEAT_PROFILE
23764 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023765 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767
23768 /* If depth of calling is getting too high, don't execute the function */
23769 if (depth >= p_mfd)
23770 {
23771 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023772 rettv->v_type = VAR_NUMBER;
23773 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023774 return;
23775 }
23776 ++depth;
23777
23778 line_breakcheck(); /* check for CTRL-C hit */
23779
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023780 fc = (funccall_T *)alloc(sizeof(funccall_T));
23781 fc->caller = current_funccal;
23782 current_funccal = fc;
23783 fc->func = fp;
23784 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023785 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023786 fc->linenr = 0;
23787 fc->returned = FALSE;
23788 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023789 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023790 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23791 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792
Bram Moolenaar33570922005-01-25 22:26:29 +000023793 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023794 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023795 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23796 * each argument variable and saves a lot of time.
23797 */
23798 /*
23799 * Init l: variables.
23800 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023801 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023802 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023803 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023804 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23805 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023806 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023807 name = v->di_key;
23808 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023809 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023810 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023811 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023812 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023813 v->di_tv.vval.v_dict = selfdict;
23814 ++selfdict->dv_refcount;
23815 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023816
Bram Moolenaar33570922005-01-25 22:26:29 +000023817 /*
23818 * Init a: variables.
23819 * Set a:0 to "argcount".
23820 * Set a:000 to a list with room for the "..." arguments.
23821 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023822 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023823 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023824 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023825 /* Use "name" to avoid a warning from some compiler that checks the
23826 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023827 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023828 name = v->di_key;
23829 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023830 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023831 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023832 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023833 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023834 v->di_tv.vval.v_list = &fc->l_varlist;
23835 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23836 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23837 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023838
23839 /*
23840 * Set a:firstline to "firstline" and a:lastline to "lastline".
23841 * Set a:name to named arguments.
23842 * Set a:N to the "..." arguments.
23843 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023844 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023845 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023846 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023847 (varnumber_T)lastline);
23848 for (i = 0; i < argcount; ++i)
23849 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023850 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023851 if (ai < 0)
23852 /* named argument a:name */
23853 name = FUNCARG(fp, i);
23854 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023855 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023856 /* "..." argument a:1, a:2, etc. */
23857 sprintf((char *)numbuf, "%d", ai + 1);
23858 name = numbuf;
23859 }
23860 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23861 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023862 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023863 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23864 }
23865 else
23866 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023867 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23868 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023869 if (v == NULL)
23870 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023871 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023872 }
23873 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023874 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023875
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023876 /* Note: the values are copied directly to avoid alloc/free.
23877 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023878 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023879 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023880
23881 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23882 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023883 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23884 fc->l_listitems[ai].li_tv = argvars[i];
23885 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023886 }
23887 }
23888
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 /* Don't redraw while executing the function. */
23890 ++RedrawingDisabled;
23891 save_sourcing_name = sourcing_name;
23892 save_sourcing_lnum = sourcing_lnum;
23893 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023894 /* need space for function name + ("function " + 3) or "[number]" */
23895 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
23896 + STRLEN(fp->uf_name) + 20;
23897 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 if (sourcing_name != NULL)
23899 {
23900 if (save_sourcing_name != NULL
23901 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020023902 sprintf((char *)sourcing_name, "%s[%d]..",
23903 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 else
23905 STRCPY(sourcing_name, "function ");
23906 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23907
23908 if (p_verbose >= 12)
23909 {
23910 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023911 verbose_enter_scroll();
23912
Bram Moolenaar555b2802005-05-19 21:08:39 +000023913 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023914 if (p_verbose >= 14)
23915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023917 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023918 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023919 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920
23921 msg_puts((char_u *)"(");
23922 for (i = 0; i < argcount; ++i)
23923 {
23924 if (i > 0)
23925 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023926 if (argvars[i].v_type == VAR_NUMBER)
23927 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023928 else
23929 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023930 /* Do not want errors such as E724 here. */
23931 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023932 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023933 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023934 if (s != NULL)
23935 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023936 if (vim_strsize(s) > MSG_BUF_CLEN)
23937 {
23938 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23939 s = buf;
23940 }
23941 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023942 vim_free(tofree);
23943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 }
23945 }
23946 msg_puts((char_u *)")");
23947 }
23948 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023949
23950 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951 --no_wait_return;
23952 }
23953 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023954#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023955 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023956 {
23957 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23958 func_do_profile(fp);
23959 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023960 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023961 {
23962 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023963 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023964 profile_zero(&fp->uf_tm_children);
23965 }
23966 script_prof_save(&wait_start);
23967 }
23968#endif
23969
Bram Moolenaar071d4272004-06-13 20:20:40 +000023970 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023971 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 save_did_emsg = did_emsg;
23973 did_emsg = FALSE;
23974
23975 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023976 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023977 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23978
23979 --RedrawingDisabled;
23980
23981 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023982 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023984 clear_tv(rettv);
23985 rettv->v_type = VAR_NUMBER;
23986 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 }
23988
Bram Moolenaar05159a02005-02-26 23:04:13 +000023989#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023990 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023991 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023992 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023993 profile_end(&call_start);
23994 profile_sub_wait(&wait_start, &call_start);
23995 profile_add(&fp->uf_tm_total, &call_start);
23996 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023997 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023998 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023999 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24000 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024001 }
24002 }
24003#endif
24004
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 /* when being verbose, mention the return value */
24006 if (p_verbose >= 12)
24007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024009 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010
Bram Moolenaar071d4272004-06-13 20:20:40 +000024011 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024012 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024013 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024014 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024015 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024016 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024017 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024018 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024019 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024020 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024021 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024022
Bram Moolenaar555b2802005-05-19 21:08:39 +000024023 /* The value may be very long. Skip the middle part, so that we
24024 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024025 * truncate it at the end. Don't want errors such as E724 here. */
24026 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024027 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024028 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024029 if (s != NULL)
24030 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024031 if (vim_strsize(s) > MSG_BUF_CLEN)
24032 {
24033 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24034 s = buf;
24035 }
24036 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024037 vim_free(tofree);
24038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039 }
24040 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024041
24042 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024043 --no_wait_return;
24044 }
24045
24046 vim_free(sourcing_name);
24047 sourcing_name = save_sourcing_name;
24048 sourcing_lnum = save_sourcing_lnum;
24049 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024050#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024051 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024052 script_prof_restore(&wait_start);
24053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054
24055 if (p_verbose >= 12 && sourcing_name != NULL)
24056 {
24057 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024058 verbose_enter_scroll();
24059
Bram Moolenaar555b2802005-05-19 21:08:39 +000024060 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024062
24063 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064 --no_wait_return;
24065 }
24066
24067 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024068 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024070
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024071 /* If the a:000 list and the l: and a: dicts are not referenced we can
24072 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024073 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24074 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24075 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24076 {
24077 free_funccal(fc, FALSE);
24078 }
24079 else
24080 {
24081 hashitem_T *hi;
24082 listitem_T *li;
24083 int todo;
24084
24085 /* "fc" is still in use. This can happen when returning "a:000" or
24086 * assigning "l:" to a global variable.
24087 * Link "fc" in the list for garbage collection later. */
24088 fc->caller = previous_funccal;
24089 previous_funccal = fc;
24090
24091 /* Make a copy of the a: variables, since we didn't do that above. */
24092 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24093 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24094 {
24095 if (!HASHITEM_EMPTY(hi))
24096 {
24097 --todo;
24098 v = HI2DI(hi);
24099 copy_tv(&v->di_tv, &v->di_tv);
24100 }
24101 }
24102
24103 /* Make a copy of the a:000 items, since we didn't do that above. */
24104 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24105 copy_tv(&li->li_tv, &li->li_tv);
24106 }
24107}
24108
24109/*
24110 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024111 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024112 */
24113 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024114can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024115{
24116 return (fc->l_varlist.lv_copyID != copyID
24117 && fc->l_vars.dv_copyID != copyID
24118 && fc->l_avars.dv_copyID != copyID);
24119}
24120
24121/*
24122 * Free "fc" and what it contains.
24123 */
24124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024125free_funccal(
24126 funccall_T *fc,
24127 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024128{
24129 listitem_T *li;
24130
24131 /* The a: variables typevals may not have been allocated, only free the
24132 * allocated variables. */
24133 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24134
24135 /* free all l: variables */
24136 vars_clear(&fc->l_vars.dv_hashtab);
24137
24138 /* Free the a:000 variables if they were allocated. */
24139 if (free_val)
24140 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24141 clear_tv(&li->li_tv);
24142
24143 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144}
24145
24146/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024147 * Add a number variable "name" to dict "dp" with value "nr".
24148 */
24149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024150add_nr_var(
24151 dict_T *dp,
24152 dictitem_T *v,
24153 char *name,
24154 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024155{
24156 STRCPY(v->di_key, name);
24157 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24158 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24159 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024160 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024161 v->di_tv.vval.v_number = nr;
24162}
24163
24164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165 * ":return [expr]"
24166 */
24167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024168ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024169{
24170 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024171 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024172 int returning = FALSE;
24173
24174 if (current_funccal == NULL)
24175 {
24176 EMSG(_("E133: :return not inside a function"));
24177 return;
24178 }
24179
24180 if (eap->skip)
24181 ++emsg_skip;
24182
24183 eap->nextcmd = NULL;
24184 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024185 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186 {
24187 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024188 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024190 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191 }
24192 /* It's safer to return also on error. */
24193 else if (!eap->skip)
24194 {
24195 /*
24196 * Return unless the expression evaluation has been cancelled due to an
24197 * aborting error, an interrupt, or an exception.
24198 */
24199 if (!aborting())
24200 returning = do_return(eap, FALSE, TRUE, NULL);
24201 }
24202
24203 /* When skipping or the return gets pending, advance to the next command
24204 * in this line (!returning). Otherwise, ignore the rest of the line.
24205 * Following lines will be ignored by get_func_line(). */
24206 if (returning)
24207 eap->nextcmd = NULL;
24208 else if (eap->nextcmd == NULL) /* no argument */
24209 eap->nextcmd = check_nextcmd(arg);
24210
24211 if (eap->skip)
24212 --emsg_skip;
24213}
24214
24215/*
24216 * Return from a function. Possibly makes the return pending. Also called
24217 * for a pending return at the ":endtry" or after returning from an extra
24218 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024219 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024220 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024221 * FALSE when the return gets pending.
24222 */
24223 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024224do_return(
24225 exarg_T *eap,
24226 int reanimate,
24227 int is_cmd,
24228 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229{
24230 int idx;
24231 struct condstack *cstack = eap->cstack;
24232
24233 if (reanimate)
24234 /* Undo the return. */
24235 current_funccal->returned = FALSE;
24236
24237 /*
24238 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24239 * not in its finally clause (which then is to be executed next) is found.
24240 * In this case, make the ":return" pending for execution at the ":endtry".
24241 * Otherwise, return normally.
24242 */
24243 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24244 if (idx >= 0)
24245 {
24246 cstack->cs_pending[idx] = CSTP_RETURN;
24247
24248 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024249 /* A pending return again gets pending. "rettv" points to an
24250 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024252 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024253 else
24254 {
24255 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024256 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024257 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024258 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024260 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024261 {
24262 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024263 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024264 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265 else
24266 EMSG(_(e_outofmem));
24267 }
24268 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024269 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270
24271 if (reanimate)
24272 {
24273 /* The pending return value could be overwritten by a ":return"
24274 * without argument in a finally clause; reset the default
24275 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024276 current_funccal->rettv->v_type = VAR_NUMBER;
24277 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278 }
24279 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024280 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 }
24282 else
24283 {
24284 current_funccal->returned = TRUE;
24285
24286 /* If the return is carried out now, store the return value. For
24287 * a return immediately after reanimation, the value is already
24288 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024289 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024291 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024292 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024294 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295 }
24296 }
24297
24298 return idx < 0;
24299}
24300
24301/*
24302 * Free the variable with a pending return value.
24303 */
24304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024305discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024306{
Bram Moolenaar33570922005-01-25 22:26:29 +000024307 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308}
24309
24310/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024311 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024312 * is an allocated string. Used by report_pending() for verbose messages.
24313 */
24314 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024315get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024316{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024317 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024318 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024319 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024320
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024321 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024322 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024323 if (s == NULL)
24324 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024325
24326 STRCPY(IObuff, ":return ");
24327 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24328 if (STRLEN(s) + 8 >= IOSIZE)
24329 STRCPY(IObuff + IOSIZE - 4, "...");
24330 vim_free(tofree);
24331 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332}
24333
24334/*
24335 * Get next function line.
24336 * Called by do_cmdline() to get the next line.
24337 * Returns allocated string, or NULL for end of function.
24338 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024340get_func_line(
24341 int c UNUSED,
24342 void *cookie,
24343 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024344{
Bram Moolenaar33570922005-01-25 22:26:29 +000024345 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024346 ufunc_T *fp = fcp->func;
24347 char_u *retval;
24348 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349
24350 /* If breakpoints have been added/deleted need to check for it. */
24351 if (fcp->dbg_tick != debug_tick)
24352 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024353 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354 sourcing_lnum);
24355 fcp->dbg_tick = debug_tick;
24356 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024357#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024358 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024359 func_line_end(cookie);
24360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361
Bram Moolenaar05159a02005-02-26 23:04:13 +000024362 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024363 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24364 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024365 retval = NULL;
24366 else
24367 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024368 /* Skip NULL lines (continuation lines). */
24369 while (fcp->linenr < gap->ga_len
24370 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24371 ++fcp->linenr;
24372 if (fcp->linenr >= gap->ga_len)
24373 retval = NULL;
24374 else
24375 {
24376 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24377 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024378#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024379 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024380 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024381#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 }
24384
24385 /* Did we encounter a breakpoint? */
24386 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24387 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024388 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024389 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024390 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024391 sourcing_lnum);
24392 fcp->dbg_tick = debug_tick;
24393 }
24394
24395 return retval;
24396}
24397
Bram Moolenaar05159a02005-02-26 23:04:13 +000024398#if defined(FEAT_PROFILE) || defined(PROTO)
24399/*
24400 * Called when starting to read a function line.
24401 * "sourcing_lnum" must be correct!
24402 * When skipping lines it may not actually be executed, but we won't find out
24403 * until later and we need to store the time now.
24404 */
24405 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024406func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024407{
24408 funccall_T *fcp = (funccall_T *)cookie;
24409 ufunc_T *fp = fcp->func;
24410
24411 if (fp->uf_profiling && sourcing_lnum >= 1
24412 && sourcing_lnum <= fp->uf_lines.ga_len)
24413 {
24414 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024415 /* Skip continuation lines. */
24416 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24417 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024418 fp->uf_tml_execed = FALSE;
24419 profile_start(&fp->uf_tml_start);
24420 profile_zero(&fp->uf_tml_children);
24421 profile_get_wait(&fp->uf_tml_wait);
24422 }
24423}
24424
24425/*
24426 * Called when actually executing a function line.
24427 */
24428 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024429func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024430{
24431 funccall_T *fcp = (funccall_T *)cookie;
24432 ufunc_T *fp = fcp->func;
24433
24434 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24435 fp->uf_tml_execed = TRUE;
24436}
24437
24438/*
24439 * Called when done with a function line.
24440 */
24441 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024442func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024443{
24444 funccall_T *fcp = (funccall_T *)cookie;
24445 ufunc_T *fp = fcp->func;
24446
24447 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24448 {
24449 if (fp->uf_tml_execed)
24450 {
24451 ++fp->uf_tml_count[fp->uf_tml_idx];
24452 profile_end(&fp->uf_tml_start);
24453 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024454 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024455 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24456 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024457 }
24458 fp->uf_tml_idx = -1;
24459 }
24460}
24461#endif
24462
Bram Moolenaar071d4272004-06-13 20:20:40 +000024463/*
24464 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024465 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024466 */
24467 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024468func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024469{
Bram Moolenaar33570922005-01-25 22:26:29 +000024470 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024471
24472 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24473 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024474 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024475 || fcp->returned);
24476}
24477
24478/*
24479 * return TRUE if cookie indicates a function which "abort"s on errors.
24480 */
24481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024482func_has_abort(
24483 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024485 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024486}
24487
24488#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24489typedef enum
24490{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024491 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24492 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24493 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024494} var_flavour_T;
24495
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024496static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024497
24498 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024499var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024500{
24501 char_u *p = varname;
24502
24503 if (ASCII_ISUPPER(*p))
24504 {
24505 while (*(++p))
24506 if (ASCII_ISLOWER(*p))
24507 return VAR_FLAVOUR_SESSION;
24508 return VAR_FLAVOUR_VIMINFO;
24509 }
24510 else
24511 return VAR_FLAVOUR_DEFAULT;
24512}
24513#endif
24514
24515#if defined(FEAT_VIMINFO) || defined(PROTO)
24516/*
24517 * Restore global vars that start with a capital from the viminfo file
24518 */
24519 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024520read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521{
24522 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024523 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024524 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024525 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024526
24527 if (!writing && (find_viminfo_parameter('!') != NULL))
24528 {
24529 tab = vim_strchr(virp->vir_line + 1, '\t');
24530 if (tab != NULL)
24531 {
24532 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024533 switch (*tab)
24534 {
24535 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024536#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024537 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024538#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024539 case 'D': type = VAR_DICT; break;
24540 case 'L': type = VAR_LIST; break;
24541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024542
24543 tab = vim_strchr(tab, '\t');
24544 if (tab != NULL)
24545 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024546 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024547 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024548 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024549 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024550#ifdef FEAT_FLOAT
24551 else if (type == VAR_FLOAT)
24552 (void)string2float(tab + 1, &tv.vval.v_float);
24553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024554 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024555 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024556 if (type == VAR_DICT || type == VAR_LIST)
24557 {
24558 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24559
24560 if (etv == NULL)
24561 /* Failed to parse back the dict or list, use it as a
24562 * string. */
24563 tv.v_type = VAR_STRING;
24564 else
24565 {
24566 vim_free(tv.vval.v_string);
24567 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024568 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024569 }
24570 }
24571
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024572 /* when in a function use global variables */
24573 save_funccal = current_funccal;
24574 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024575 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024576 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024577
24578 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024579 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024580 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24581 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024582 }
24583 }
24584 }
24585
24586 return viminfo_readline(virp);
24587}
24588
24589/*
24590 * Write global vars that start with a capital to the viminfo file
24591 */
24592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024593write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024594{
Bram Moolenaar33570922005-01-25 22:26:29 +000024595 hashitem_T *hi;
24596 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024597 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024598 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024599 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024600 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024601 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024602
24603 if (find_viminfo_parameter('!') == NULL)
24604 return;
24605
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024606 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024607
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024608 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024609 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024610 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024611 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024613 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024614 this_var = HI2DI(hi);
24615 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024616 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024617 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024618 {
24619 case VAR_STRING: s = "STR"; break;
24620 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024621#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024622 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024623#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024624 case VAR_DICT: s = "DIC"; break;
24625 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024626 default: continue;
24627 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024628 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024629 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024630 if (p != NULL)
24631 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024632 vim_free(tofree);
24633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024634 }
24635 }
24636}
24637#endif
24638
24639#if defined(FEAT_SESSION) || defined(PROTO)
24640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024641store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642{
Bram Moolenaar33570922005-01-25 22:26:29 +000024643 hashitem_T *hi;
24644 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024645 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024646 char_u *p, *t;
24647
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024648 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024649 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024651 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024653 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024654 this_var = HI2DI(hi);
24655 if ((this_var->di_tv.v_type == VAR_NUMBER
24656 || this_var->di_tv.v_type == VAR_STRING)
24657 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024658 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024659 /* Escape special characters with a backslash. Turn a LF and
24660 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024661 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024662 (char_u *)"\\\"\n\r");
24663 if (p == NULL) /* out of memory */
24664 break;
24665 for (t = p; *t != NUL; ++t)
24666 if (*t == '\n')
24667 *t = 'n';
24668 else if (*t == '\r')
24669 *t = 'r';
24670 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024671 this_var->di_key,
24672 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24673 : ' ',
24674 p,
24675 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24676 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024677 || put_eol(fd) == FAIL)
24678 {
24679 vim_free(p);
24680 return FAIL;
24681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682 vim_free(p);
24683 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024684#ifdef FEAT_FLOAT
24685 else if (this_var->di_tv.v_type == VAR_FLOAT
24686 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24687 {
24688 float_T f = this_var->di_tv.vval.v_float;
24689 int sign = ' ';
24690
24691 if (f < 0)
24692 {
24693 f = -f;
24694 sign = '-';
24695 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024696 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024697 this_var->di_key, sign, f) < 0)
24698 || put_eol(fd) == FAIL)
24699 return FAIL;
24700 }
24701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702 }
24703 }
24704 return OK;
24705}
24706#endif
24707
Bram Moolenaar661b1822005-07-28 22:36:45 +000024708/*
24709 * Display script name where an item was last set.
24710 * Should only be invoked when 'verbose' is non-zero.
24711 */
24712 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024713last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000024714{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024715 char_u *p;
24716
Bram Moolenaar661b1822005-07-28 22:36:45 +000024717 if (scriptID != 0)
24718 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024719 p = home_replace_save(NULL, get_scriptname(scriptID));
24720 if (p != NULL)
24721 {
24722 verbose_enter();
24723 MSG_PUTS(_("\n\tLast set from "));
24724 MSG_PUTS(p);
24725 vim_free(p);
24726 verbose_leave();
24727 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024728 }
24729}
24730
Bram Moolenaard812df62008-11-09 12:46:09 +000024731/*
24732 * List v:oldfiles in a nice way.
24733 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024734 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024735ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000024736{
24737 list_T *l = vimvars[VV_OLDFILES].vv_list;
24738 listitem_T *li;
24739 int nr = 0;
24740
24741 if (l == NULL)
24742 msg((char_u *)_("No old files"));
24743 else
24744 {
24745 msg_start();
24746 msg_scroll = TRUE;
24747 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24748 {
24749 msg_outnum((long)++nr);
24750 MSG_PUTS(": ");
24751 msg_outtrans(get_tv_string(&li->li_tv));
24752 msg_putchar('\n');
24753 out_flush(); /* output one line at a time */
24754 ui_breakcheck();
24755 }
24756 /* Assume "got_int" was set to truncate the listing. */
24757 got_int = FALSE;
24758
24759#ifdef FEAT_BROWSE_CMD
24760 if (cmdmod.browse)
24761 {
24762 quit_more = FALSE;
24763 nr = prompt_for_number(FALSE);
24764 msg_starthere();
24765 if (nr > 0)
24766 {
24767 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24768 (long)nr);
24769
24770 if (p != NULL)
24771 {
24772 p = expand_env_save(p);
24773 eap->arg = p;
24774 eap->cmdidx = CMD_edit;
24775 cmdmod.browse = FALSE;
24776 do_exedit(eap, NULL);
24777 vim_free(p);
24778 }
24779 }
24780 }
24781#endif
24782 }
24783}
24784
Bram Moolenaar53744302015-07-17 17:38:22 +020024785/* reset v:option_new, v:option_old and v:option_type */
24786 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024787reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020024788{
24789 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
24790 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
24791 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
24792}
24793
24794
Bram Moolenaar071d4272004-06-13 20:20:40 +000024795#endif /* FEAT_EVAL */
24796
Bram Moolenaar071d4272004-06-13 20:20:40 +000024797
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024798#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024799
24800#ifdef WIN3264
24801/*
24802 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24803 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024804static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
24805static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
24806static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807
24808/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024809 * Get the short path (8.3) for the filename in "fnamep".
24810 * Only works for a valid file name.
24811 * When the path gets longer "fnamep" is changed and the allocated buffer
24812 * is put in "bufp".
24813 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24814 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024815 */
24816 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024817get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024818{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024819 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024820 char_u *newbuf;
24821
24822 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024823 l = GetShortPathName(*fnamep, *fnamep, len);
24824 if (l > len - 1)
24825 {
24826 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024827 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024828 newbuf = vim_strnsave(*fnamep, l);
24829 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024830 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024831
24832 vim_free(*bufp);
24833 *fnamep = *bufp = newbuf;
24834
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024835 /* Really should always succeed, as the buffer is big enough. */
24836 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024837 }
24838
24839 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024840 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841}
24842
24843/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024844 * Get the short path (8.3) for the filename in "fname". The converted
24845 * path is returned in "bufp".
24846 *
24847 * Some of the directories specified in "fname" may not exist. This function
24848 * will shorten the existing directories at the beginning of the path and then
24849 * append the remaining non-existing path.
24850 *
24851 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024852 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024853 * bufp - Pointer to an allocated buffer for the filename.
24854 * fnamelen - Length of the filename pointed to by fname
24855 *
24856 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857 */
24858 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024859shortpath_for_invalid_fname(
24860 char_u **fname,
24861 char_u **bufp,
24862 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024863{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024864 char_u *short_fname, *save_fname, *pbuf_unused;
24865 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024866 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024867 int old_len, len;
24868 int new_len, sfx_len;
24869 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870
24871 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024872 old_len = *fnamelen;
24873 save_fname = vim_strnsave(*fname, old_len);
24874 pbuf_unused = NULL;
24875 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024876
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024877 endp = save_fname + old_len - 1; /* Find the end of the copy */
24878 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024879
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024880 /*
24881 * Try shortening the supplied path till it succeeds by removing one
24882 * directory at a time from the tail of the path.
24883 */
24884 len = 0;
24885 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024887 /* go back one path-separator */
24888 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24889 --endp;
24890 if (endp <= save_fname)
24891 break; /* processed the complete path */
24892
24893 /*
24894 * Replace the path separator with a NUL and try to shorten the
24895 * resulting path.
24896 */
24897 ch = *endp;
24898 *endp = 0;
24899 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024900 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024901 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24902 {
24903 retval = FAIL;
24904 goto theend;
24905 }
24906 *endp = ch; /* preserve the string */
24907
24908 if (len > 0)
24909 break; /* successfully shortened the path */
24910
24911 /* failed to shorten the path. Skip the path separator */
24912 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913 }
24914
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024915 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024917 /*
24918 * Succeeded in shortening the path. Now concatenate the shortened
24919 * path with the remaining path at the tail.
24920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024921
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024922 /* Compute the length of the new path. */
24923 sfx_len = (int)(save_endp - endp) + 1;
24924 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024926 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024928 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024929 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024930 /* There is not enough space in the currently allocated string,
24931 * copy it to a buffer big enough. */
24932 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024934 {
24935 retval = FAIL;
24936 goto theend;
24937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024938 }
24939 else
24940 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024941 /* Transfer short_fname to the main buffer (it's big enough),
24942 * unless get_short_pathname() did its work in-place. */
24943 *fname = *bufp = save_fname;
24944 if (short_fname != save_fname)
24945 vim_strncpy(save_fname, short_fname, len);
24946 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024947 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024948
24949 /* concat the not-shortened part of the path */
24950 vim_strncpy(*fname + len, endp, sfx_len);
24951 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024952 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024953
24954theend:
24955 vim_free(pbuf_unused);
24956 vim_free(save_fname);
24957
24958 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024959}
24960
24961/*
24962 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024963 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964 */
24965 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024966shortpath_for_partial(
24967 char_u **fnamep,
24968 char_u **bufp,
24969 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970{
24971 int sepcount, len, tflen;
24972 char_u *p;
24973 char_u *pbuf, *tfname;
24974 int hasTilde;
24975
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024976 /* Count up the path separators from the RHS.. so we know which part
24977 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024978 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024979 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024980 if (vim_ispathsep(*p))
24981 ++sepcount;
24982
24983 /* Need full path first (use expand_env() to remove a "~/") */
24984 hasTilde = (**fnamep == '~');
24985 if (hasTilde)
24986 pbuf = tfname = expand_env_save(*fnamep);
24987 else
24988 pbuf = tfname = FullName_save(*fnamep, FALSE);
24989
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024990 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024991
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024992 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24993 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024994
24995 if (len == 0)
24996 {
24997 /* Don't have a valid filename, so shorten the rest of the
24998 * path if we can. This CAN give us invalid 8.3 filenames, but
24999 * there's not a lot of point in guessing what it might be.
25000 */
25001 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025002 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25003 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004 }
25005
25006 /* Count the paths backward to find the beginning of the desired string. */
25007 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025008 {
25009#ifdef FEAT_MBYTE
25010 if (has_mbyte)
25011 p -= mb_head_off(tfname, p);
25012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 if (vim_ispathsep(*p))
25014 {
25015 if (sepcount == 0 || (hasTilde && sepcount == 1))
25016 break;
25017 else
25018 sepcount --;
25019 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025021 if (hasTilde)
25022 {
25023 --p;
25024 if (p >= tfname)
25025 *p = '~';
25026 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025027 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025028 }
25029 else
25030 ++p;
25031
25032 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25033 vim_free(*bufp);
25034 *fnamelen = (int)STRLEN(p);
25035 *bufp = pbuf;
25036 *fnamep = p;
25037
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025038 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039}
25040#endif /* WIN3264 */
25041
25042/*
25043 * Adjust a filename, according to a string of modifiers.
25044 * *fnamep must be NUL terminated when called. When returning, the length is
25045 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025046 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025047 * When there is an error, *fnamep is set to NULL.
25048 */
25049 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025050modify_fname(
25051 char_u *src, /* string with modifiers */
25052 int *usedlen, /* characters after src that are used */
25053 char_u **fnamep, /* file name so far */
25054 char_u **bufp, /* buffer for allocated file name or NULL */
25055 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025056{
25057 int valid = 0;
25058 char_u *tail;
25059 char_u *s, *p, *pbuf;
25060 char_u dirname[MAXPATHL];
25061 int c;
25062 int has_fullname = 0;
25063#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025064 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025065 int has_shortname = 0;
25066#endif
25067
25068repeat:
25069 /* ":p" - full path/file_name */
25070 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25071 {
25072 has_fullname = 1;
25073
25074 valid |= VALID_PATH;
25075 *usedlen += 2;
25076
25077 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25078 if ((*fnamep)[0] == '~'
25079#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25080 && ((*fnamep)[1] == '/'
25081# ifdef BACKSLASH_IN_FILENAME
25082 || (*fnamep)[1] == '\\'
25083# endif
25084 || (*fnamep)[1] == NUL)
25085
25086#endif
25087 )
25088 {
25089 *fnamep = expand_env_save(*fnamep);
25090 vim_free(*bufp); /* free any allocated file name */
25091 *bufp = *fnamep;
25092 if (*fnamep == NULL)
25093 return -1;
25094 }
25095
25096 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025097 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025098 {
25099 if (vim_ispathsep(*p)
25100 && p[1] == '.'
25101 && (p[2] == NUL
25102 || vim_ispathsep(p[2])
25103 || (p[2] == '.'
25104 && (p[3] == NUL || vim_ispathsep(p[3])))))
25105 break;
25106 }
25107
25108 /* FullName_save() is slow, don't use it when not needed. */
25109 if (*p != NUL || !vim_isAbsName(*fnamep))
25110 {
25111 *fnamep = FullName_save(*fnamep, *p != NUL);
25112 vim_free(*bufp); /* free any allocated file name */
25113 *bufp = *fnamep;
25114 if (*fnamep == NULL)
25115 return -1;
25116 }
25117
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025118#ifdef WIN3264
25119# if _WIN32_WINNT >= 0x0500
25120 if (vim_strchr(*fnamep, '~') != NULL)
25121 {
25122 /* Expand 8.3 filename to full path. Needed to make sure the same
25123 * file does not have two different names.
25124 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25125 p = alloc(_MAX_PATH + 1);
25126 if (p != NULL)
25127 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025128 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025129 {
25130 vim_free(*bufp);
25131 *bufp = *fnamep = p;
25132 }
25133 else
25134 vim_free(p);
25135 }
25136 }
25137# endif
25138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139 /* Append a path separator to a directory. */
25140 if (mch_isdir(*fnamep))
25141 {
25142 /* Make room for one or two extra characters. */
25143 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25144 vim_free(*bufp); /* free any allocated file name */
25145 *bufp = *fnamep;
25146 if (*fnamep == NULL)
25147 return -1;
25148 add_pathsep(*fnamep);
25149 }
25150 }
25151
25152 /* ":." - path relative to the current directory */
25153 /* ":~" - path relative to the home directory */
25154 /* ":8" - shortname path - postponed till after */
25155 while (src[*usedlen] == ':'
25156 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25157 {
25158 *usedlen += 2;
25159 if (c == '8')
25160 {
25161#ifdef WIN3264
25162 has_shortname = 1; /* Postpone this. */
25163#endif
25164 continue;
25165 }
25166 pbuf = NULL;
25167 /* Need full path first (use expand_env() to remove a "~/") */
25168 if (!has_fullname)
25169 {
25170 if (c == '.' && **fnamep == '~')
25171 p = pbuf = expand_env_save(*fnamep);
25172 else
25173 p = pbuf = FullName_save(*fnamep, FALSE);
25174 }
25175 else
25176 p = *fnamep;
25177
25178 has_fullname = 0;
25179
25180 if (p != NULL)
25181 {
25182 if (c == '.')
25183 {
25184 mch_dirname(dirname, MAXPATHL);
25185 s = shorten_fname(p, dirname);
25186 if (s != NULL)
25187 {
25188 *fnamep = s;
25189 if (pbuf != NULL)
25190 {
25191 vim_free(*bufp); /* free any allocated file name */
25192 *bufp = pbuf;
25193 pbuf = NULL;
25194 }
25195 }
25196 }
25197 else
25198 {
25199 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25200 /* Only replace it when it starts with '~' */
25201 if (*dirname == '~')
25202 {
25203 s = vim_strsave(dirname);
25204 if (s != NULL)
25205 {
25206 *fnamep = s;
25207 vim_free(*bufp);
25208 *bufp = s;
25209 }
25210 }
25211 }
25212 vim_free(pbuf);
25213 }
25214 }
25215
25216 tail = gettail(*fnamep);
25217 *fnamelen = (int)STRLEN(*fnamep);
25218
25219 /* ":h" - head, remove "/file_name", can be repeated */
25220 /* Don't remove the first "/" or "c:\" */
25221 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25222 {
25223 valid |= VALID_HEAD;
25224 *usedlen += 2;
25225 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025226 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025227 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228 *fnamelen = (int)(tail - *fnamep);
25229#ifdef VMS
25230 if (*fnamelen > 0)
25231 *fnamelen += 1; /* the path separator is part of the path */
25232#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025233 if (*fnamelen == 0)
25234 {
25235 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25236 p = vim_strsave((char_u *)".");
25237 if (p == NULL)
25238 return -1;
25239 vim_free(*bufp);
25240 *bufp = *fnamep = tail = p;
25241 *fnamelen = 1;
25242 }
25243 else
25244 {
25245 while (tail > s && !after_pathsep(s, tail))
25246 mb_ptr_back(*fnamep, tail);
25247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248 }
25249
25250 /* ":8" - shortname */
25251 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25252 {
25253 *usedlen += 2;
25254#ifdef WIN3264
25255 has_shortname = 1;
25256#endif
25257 }
25258
25259#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025260 /*
25261 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025262 */
25263 if (has_shortname)
25264 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025265 /* Copy the string if it is shortened by :h and when it wasn't copied
25266 * yet, because we are going to change it in place. Avoids changing
25267 * the buffer name for "%:8". */
25268 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025269 {
25270 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025271 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025272 return -1;
25273 vim_free(*bufp);
25274 *bufp = *fnamep = p;
25275 }
25276
25277 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025278 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279 if (!has_fullname && !vim_isAbsName(*fnamep))
25280 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025281 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025282 return -1;
25283 }
25284 else
25285 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025286 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287
Bram Moolenaardc935552011-08-17 15:23:23 +020025288 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025290 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025291 return -1;
25292
25293 if (l == 0)
25294 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025295 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025297 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025298 return -1;
25299 }
25300 *fnamelen = l;
25301 }
25302 }
25303#endif /* WIN3264 */
25304
25305 /* ":t" - tail, just the basename */
25306 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25307 {
25308 *usedlen += 2;
25309 *fnamelen -= (int)(tail - *fnamep);
25310 *fnamep = tail;
25311 }
25312
25313 /* ":e" - extension, can be repeated */
25314 /* ":r" - root, without extension, can be repeated */
25315 while (src[*usedlen] == ':'
25316 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25317 {
25318 /* find a '.' in the tail:
25319 * - for second :e: before the current fname
25320 * - otherwise: The last '.'
25321 */
25322 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25323 s = *fnamep - 2;
25324 else
25325 s = *fnamep + *fnamelen - 1;
25326 for ( ; s > tail; --s)
25327 if (s[0] == '.')
25328 break;
25329 if (src[*usedlen + 1] == 'e') /* :e */
25330 {
25331 if (s > tail)
25332 {
25333 *fnamelen += (int)(*fnamep - (s + 1));
25334 *fnamep = s + 1;
25335#ifdef VMS
25336 /* cut version from the extension */
25337 s = *fnamep + *fnamelen - 1;
25338 for ( ; s > *fnamep; --s)
25339 if (s[0] == ';')
25340 break;
25341 if (s > *fnamep)
25342 *fnamelen = s - *fnamep;
25343#endif
25344 }
25345 else if (*fnamep <= tail)
25346 *fnamelen = 0;
25347 }
25348 else /* :r */
25349 {
25350 if (s > tail) /* remove one extension */
25351 *fnamelen = (int)(s - *fnamep);
25352 }
25353 *usedlen += 2;
25354 }
25355
25356 /* ":s?pat?foo?" - substitute */
25357 /* ":gs?pat?foo?" - global substitute */
25358 if (src[*usedlen] == ':'
25359 && (src[*usedlen + 1] == 's'
25360 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25361 {
25362 char_u *str;
25363 char_u *pat;
25364 char_u *sub;
25365 int sep;
25366 char_u *flags;
25367 int didit = FALSE;
25368
25369 flags = (char_u *)"";
25370 s = src + *usedlen + 2;
25371 if (src[*usedlen + 1] == 'g')
25372 {
25373 flags = (char_u *)"g";
25374 ++s;
25375 }
25376
25377 sep = *s++;
25378 if (sep)
25379 {
25380 /* find end of pattern */
25381 p = vim_strchr(s, sep);
25382 if (p != NULL)
25383 {
25384 pat = vim_strnsave(s, (int)(p - s));
25385 if (pat != NULL)
25386 {
25387 s = p + 1;
25388 /* find end of substitution */
25389 p = vim_strchr(s, sep);
25390 if (p != NULL)
25391 {
25392 sub = vim_strnsave(s, (int)(p - s));
25393 str = vim_strnsave(*fnamep, *fnamelen);
25394 if (sub != NULL && str != NULL)
25395 {
25396 *usedlen = (int)(p + 1 - src);
25397 s = do_string_sub(str, pat, sub, flags);
25398 if (s != NULL)
25399 {
25400 *fnamep = s;
25401 *fnamelen = (int)STRLEN(s);
25402 vim_free(*bufp);
25403 *bufp = s;
25404 didit = TRUE;
25405 }
25406 }
25407 vim_free(sub);
25408 vim_free(str);
25409 }
25410 vim_free(pat);
25411 }
25412 }
25413 /* after using ":s", repeat all the modifiers */
25414 if (didit)
25415 goto repeat;
25416 }
25417 }
25418
Bram Moolenaar26df0922014-02-23 23:39:13 +010025419 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25420 {
25421 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25422 if (p == NULL)
25423 return -1;
25424 vim_free(*bufp);
25425 *bufp = *fnamep = p;
25426 *fnamelen = (int)STRLEN(p);
25427 *usedlen += 2;
25428 }
25429
Bram Moolenaar071d4272004-06-13 20:20:40 +000025430 return valid;
25431}
25432
25433/*
25434 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25435 * "flags" can be "g" to do a global substitute.
25436 * Returns an allocated string, NULL for error.
25437 */
25438 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025439do_string_sub(
25440 char_u *str,
25441 char_u *pat,
25442 char_u *sub,
25443 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444{
25445 int sublen;
25446 regmatch_T regmatch;
25447 int i;
25448 int do_all;
25449 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025450 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025451 garray_T ga;
25452 char_u *ret;
25453 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025454 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455
25456 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25457 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025458 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025459
25460 ga_init2(&ga, 1, 200);
25461
25462 do_all = (flags[0] == 'g');
25463
25464 regmatch.rm_ic = p_ic;
25465 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25466 if (regmatch.regprog != NULL)
25467 {
25468 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025469 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025470 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25471 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025472 /* Skip empty match except for first match. */
25473 if (regmatch.startp[0] == regmatch.endp[0])
25474 {
25475 if (zero_width == regmatch.startp[0])
25476 {
25477 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025478 i = MB_PTR2LEN(tail);
25479 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25480 (size_t)i);
25481 ga.ga_len += i;
25482 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025483 continue;
25484 }
25485 zero_width = regmatch.startp[0];
25486 }
25487
Bram Moolenaar071d4272004-06-13 20:20:40 +000025488 /*
25489 * Get some space for a temporary buffer to do the substitution
25490 * into. It will contain:
25491 * - The text up to where the match is.
25492 * - The substituted text.
25493 * - The text after the match.
25494 */
25495 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025496 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025497 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25498 {
25499 ga_clear(&ga);
25500 break;
25501 }
25502
25503 /* copy the text up to where the match is */
25504 i = (int)(regmatch.startp[0] - tail);
25505 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25506 /* add the substituted text */
25507 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25508 + ga.ga_len + i, TRUE, TRUE, FALSE);
25509 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025510 tail = regmatch.endp[0];
25511 if (*tail == NUL)
25512 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513 if (!do_all)
25514 break;
25515 }
25516
25517 if (ga.ga_data != NULL)
25518 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25519
Bram Moolenaar473de612013-06-08 18:19:48 +020025520 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521 }
25522
25523 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25524 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025525 if (p_cpo == empty_option)
25526 p_cpo = save_cpo;
25527 else
25528 /* Darn, evaluating {sub} expression changed the value. */
25529 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530
25531 return ret;
25532}
25533
25534#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */