blob: 3b1172ecd8879c991c4adbc0672b7d419bfb64de [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
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100503static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100504# ifdef FEAT_JOB
505static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
506# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100507static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
509static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100510static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100511static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
513static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100514static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100515static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100516#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100517static void f_changenr(typval_T *argvars, typval_T *rettv);
518static void f_char2nr(typval_T *argvars, typval_T *rettv);
519static void f_cindent(typval_T *argvars, typval_T *rettv);
520static void f_clearmatches(typval_T *argvars, typval_T *rettv);
521static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000522#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100523static void f_complete(typval_T *argvars, typval_T *rettv);
524static void f_complete_add(typval_T *argvars, typval_T *rettv);
525static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_confirm(typval_T *argvars, typval_T *rettv);
528static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_cos(typval_T *argvars, typval_T *rettv);
531static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_count(typval_T *argvars, typval_T *rettv);
534static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
535static void f_cursor(typval_T *argsvars, typval_T *rettv);
536static void f_deepcopy(typval_T *argvars, typval_T *rettv);
537static void f_delete(typval_T *argvars, typval_T *rettv);
538static void f_did_filetype(typval_T *argvars, typval_T *rettv);
539static void f_diff_filler(typval_T *argvars, typval_T *rettv);
540static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100541static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_empty(typval_T *argvars, typval_T *rettv);
543static void f_escape(typval_T *argvars, typval_T *rettv);
544static void f_eval(typval_T *argvars, typval_T *rettv);
545static void f_eventhandler(typval_T *argvars, typval_T *rettv);
546static void f_executable(typval_T *argvars, typval_T *rettv);
547static void f_exepath(typval_T *argvars, typval_T *rettv);
548static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200551#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100552static void f_expand(typval_T *argvars, typval_T *rettv);
553static void f_extend(typval_T *argvars, typval_T *rettv);
554static void f_feedkeys(typval_T *argvars, typval_T *rettv);
555static void f_filereadable(typval_T *argvars, typval_T *rettv);
556static void f_filewritable(typval_T *argvars, typval_T *rettv);
557static void f_filter(typval_T *argvars, typval_T *rettv);
558static void f_finddir(typval_T *argvars, typval_T *rettv);
559static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000560#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_float2nr(typval_T *argvars, typval_T *rettv);
562static void f_floor(typval_T *argvars, typval_T *rettv);
563static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000564#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100565static void f_fnameescape(typval_T *argvars, typval_T *rettv);
566static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
567static void f_foldclosed(typval_T *argvars, typval_T *rettv);
568static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
569static void f_foldlevel(typval_T *argvars, typval_T *rettv);
570static void f_foldtext(typval_T *argvars, typval_T *rettv);
571static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
572static void f_foreground(typval_T *argvars, typval_T *rettv);
573static void f_function(typval_T *argvars, typval_T *rettv);
574static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
575static void f_get(typval_T *argvars, typval_T *rettv);
576static void f_getbufline(typval_T *argvars, typval_T *rettv);
577static void f_getbufvar(typval_T *argvars, typval_T *rettv);
578static void f_getchar(typval_T *argvars, typval_T *rettv);
579static void f_getcharmod(typval_T *argvars, typval_T *rettv);
580static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
581static void f_getcmdline(typval_T *argvars, typval_T *rettv);
582static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
583static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
584static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
585static void f_getcwd(typval_T *argvars, typval_T *rettv);
586static void f_getfontname(typval_T *argvars, typval_T *rettv);
587static void f_getfperm(typval_T *argvars, typval_T *rettv);
588static void f_getfsize(typval_T *argvars, typval_T *rettv);
589static void f_getftime(typval_T *argvars, typval_T *rettv);
590static void f_getftype(typval_T *argvars, typval_T *rettv);
591static void f_getline(typval_T *argvars, typval_T *rettv);
592static void f_getmatches(typval_T *argvars, typval_T *rettv);
593static void f_getpid(typval_T *argvars, typval_T *rettv);
594static void f_getcurpos(typval_T *argvars, typval_T *rettv);
595static void f_getpos(typval_T *argvars, typval_T *rettv);
596static void f_getqflist(typval_T *argvars, typval_T *rettv);
597static void f_getreg(typval_T *argvars, typval_T *rettv);
598static void f_getregtype(typval_T *argvars, typval_T *rettv);
599static void f_gettabvar(typval_T *argvars, typval_T *rettv);
600static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
601static void f_getwinposx(typval_T *argvars, typval_T *rettv);
602static void f_getwinposy(typval_T *argvars, typval_T *rettv);
603static void f_getwinvar(typval_T *argvars, typval_T *rettv);
604static void f_glob(typval_T *argvars, typval_T *rettv);
605static void f_globpath(typval_T *argvars, typval_T *rettv);
606static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
607static void f_has(typval_T *argvars, typval_T *rettv);
608static void f_has_key(typval_T *argvars, typval_T *rettv);
609static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
610static void f_hasmapto(typval_T *argvars, typval_T *rettv);
611static void f_histadd(typval_T *argvars, typval_T *rettv);
612static void f_histdel(typval_T *argvars, typval_T *rettv);
613static void f_histget(typval_T *argvars, typval_T *rettv);
614static void f_histnr(typval_T *argvars, typval_T *rettv);
615static void f_hlID(typval_T *argvars, typval_T *rettv);
616static void f_hlexists(typval_T *argvars, typval_T *rettv);
617static void f_hostname(typval_T *argvars, typval_T *rettv);
618static void f_iconv(typval_T *argvars, typval_T *rettv);
619static void f_indent(typval_T *argvars, typval_T *rettv);
620static void f_index(typval_T *argvars, typval_T *rettv);
621static void f_input(typval_T *argvars, typval_T *rettv);
622static void f_inputdialog(typval_T *argvars, typval_T *rettv);
623static void f_inputlist(typval_T *argvars, typval_T *rettv);
624static void f_inputrestore(typval_T *argvars, typval_T *rettv);
625static void f_inputsave(typval_T *argvars, typval_T *rettv);
626static void f_inputsecret(typval_T *argvars, typval_T *rettv);
627static void f_insert(typval_T *argvars, typval_T *rettv);
628static void f_invert(typval_T *argvars, typval_T *rettv);
629static void f_isdirectory(typval_T *argvars, typval_T *rettv);
630static void f_islocked(typval_T *argvars, typval_T *rettv);
631static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100632#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100633# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100634static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100635# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +0100636static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100637static void f_job_start(typval_T *argvars, typval_T *rettv);
638static void f_job_stop(typval_T *argvars, typval_T *rettv);
639static void f_job_status(typval_T *argvars, typval_T *rettv);
640#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100641static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100642static void f_js_decode(typval_T *argvars, typval_T *rettv);
643static void f_js_encode(typval_T *argvars, typval_T *rettv);
644static void f_json_decode(typval_T *argvars, typval_T *rettv);
645static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100646static void f_keys(typval_T *argvars, typval_T *rettv);
647static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
648static void f_len(typval_T *argvars, typval_T *rettv);
649static void f_libcall(typval_T *argvars, typval_T *rettv);
650static void f_libcallnr(typval_T *argvars, typval_T *rettv);
651static void f_line(typval_T *argvars, typval_T *rettv);
652static void f_line2byte(typval_T *argvars, typval_T *rettv);
653static void f_lispindent(typval_T *argvars, typval_T *rettv);
654static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000655#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_log(typval_T *argvars, typval_T *rettv);
657static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200659#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200661#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100662static void f_map(typval_T *argvars, typval_T *rettv);
663static void f_maparg(typval_T *argvars, typval_T *rettv);
664static void f_mapcheck(typval_T *argvars, typval_T *rettv);
665static void f_match(typval_T *argvars, typval_T *rettv);
666static void f_matchadd(typval_T *argvars, typval_T *rettv);
667static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
668static void f_matcharg(typval_T *argvars, typval_T *rettv);
669static void f_matchdelete(typval_T *argvars, typval_T *rettv);
670static void f_matchend(typval_T *argvars, typval_T *rettv);
671static void f_matchlist(typval_T *argvars, typval_T *rettv);
672static void f_matchstr(typval_T *argvars, typval_T *rettv);
673static void f_max(typval_T *argvars, typval_T *rettv);
674static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000675#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000677#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100679#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100681#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100682static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
683static void f_nr2char(typval_T *argvars, typval_T *rettv);
684static void f_or(typval_T *argvars, typval_T *rettv);
685static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100686#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100687static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100688#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
693static void f_printf(typval_T *argvars, typval_T *rettv);
694static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200695#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200697#endif
698#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200700#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_range(typval_T *argvars, typval_T *rettv);
702static void f_readfile(typval_T *argvars, typval_T *rettv);
703static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100704#ifdef FEAT_FLOAT
705static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
706#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_reltimestr(typval_T *argvars, typval_T *rettv);
708static void f_remote_expr(typval_T *argvars, typval_T *rettv);
709static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
710static void f_remote_peek(typval_T *argvars, typval_T *rettv);
711static void f_remote_read(typval_T *argvars, typval_T *rettv);
712static void f_remote_send(typval_T *argvars, typval_T *rettv);
713static void f_remove(typval_T *argvars, typval_T *rettv);
714static void f_rename(typval_T *argvars, typval_T *rettv);
715static void f_repeat(typval_T *argvars, typval_T *rettv);
716static void f_resolve(typval_T *argvars, typval_T *rettv);
717static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000720#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_screenattr(typval_T *argvars, typval_T *rettv);
722static void f_screenchar(typval_T *argvars, typval_T *rettv);
723static void f_screencol(typval_T *argvars, typval_T *rettv);
724static void f_screenrow(typval_T *argvars, typval_T *rettv);
725static void f_search(typval_T *argvars, typval_T *rettv);
726static void f_searchdecl(typval_T *argvars, typval_T *rettv);
727static void f_searchpair(typval_T *argvars, typval_T *rettv);
728static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
729static void f_searchpos(typval_T *argvars, typval_T *rettv);
730static void f_server2client(typval_T *argvars, typval_T *rettv);
731static void f_serverlist(typval_T *argvars, typval_T *rettv);
732static void f_setbufvar(typval_T *argvars, typval_T *rettv);
733static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
734static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
735static void f_setline(typval_T *argvars, typval_T *rettv);
736static void f_setloclist(typval_T *argvars, typval_T *rettv);
737static void f_setmatches(typval_T *argvars, typval_T *rettv);
738static void f_setpos(typval_T *argvars, typval_T *rettv);
739static void f_setqflist(typval_T *argvars, typval_T *rettv);
740static void f_setreg(typval_T *argvars, typval_T *rettv);
741static void f_settabvar(typval_T *argvars, typval_T *rettv);
742static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
743static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100744#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100745static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100746#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_shellescape(typval_T *argvars, typval_T *rettv);
748static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
749static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000750#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_sin(typval_T *argvars, typval_T *rettv);
752static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sort(typval_T *argvars, typval_T *rettv);
755static void f_soundfold(typval_T *argvars, typval_T *rettv);
756static void f_spellbadword(typval_T *argvars, typval_T *rettv);
757static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
758static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000759#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_sqrt(typval_T *argvars, typval_T *rettv);
761static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_str2nr(typval_T *argvars, typval_T *rettv);
764static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000765#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000767#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_stridx(typval_T *argvars, typval_T *rettv);
769static void f_string(typval_T *argvars, typval_T *rettv);
770static void f_strlen(typval_T *argvars, typval_T *rettv);
771static void f_strpart(typval_T *argvars, typval_T *rettv);
772static void f_strridx(typval_T *argvars, typval_T *rettv);
773static void f_strtrans(typval_T *argvars, typval_T *rettv);
774static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
775static void f_strwidth(typval_T *argvars, typval_T *rettv);
776static void f_submatch(typval_T *argvars, typval_T *rettv);
777static void f_substitute(typval_T *argvars, typval_T *rettv);
778static void f_synID(typval_T *argvars, typval_T *rettv);
779static void f_synIDattr(typval_T *argvars, typval_T *rettv);
780static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
781static void f_synstack(typval_T *argvars, typval_T *rettv);
782static void f_synconcealed(typval_T *argvars, typval_T *rettv);
783static void f_system(typval_T *argvars, typval_T *rettv);
784static void f_systemlist(typval_T *argvars, typval_T *rettv);
785static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
786static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
787static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
788static void f_taglist(typval_T *argvars, typval_T *rettv);
789static void f_tagfiles(typval_T *argvars, typval_T *rettv);
790static void f_tempname(typval_T *argvars, typval_T *rettv);
791static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200792#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100793static void f_tan(typval_T *argvars, typval_T *rettv);
794static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200795#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_tolower(typval_T *argvars, typval_T *rettv);
797static void f_toupper(typval_T *argvars, typval_T *rettv);
798static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000799#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000801#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100802static void f_type(typval_T *argvars, typval_T *rettv);
803static void f_undofile(typval_T *argvars, typval_T *rettv);
804static void f_undotree(typval_T *argvars, typval_T *rettv);
805static void f_uniq(typval_T *argvars, typval_T *rettv);
806static void f_values(typval_T *argvars, typval_T *rettv);
807static void f_virtcol(typval_T *argvars, typval_T *rettv);
808static void f_visualmode(typval_T *argvars, typval_T *rettv);
809static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
810static void f_winbufnr(typval_T *argvars, typval_T *rettv);
811static void f_wincol(typval_T *argvars, typval_T *rettv);
812static void f_winheight(typval_T *argvars, typval_T *rettv);
813static void f_winline(typval_T *argvars, typval_T *rettv);
814static void f_winnr(typval_T *argvars, typval_T *rettv);
815static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
816static void f_winrestview(typval_T *argvars, typval_T *rettv);
817static void f_winsaveview(typval_T *argvars, typval_T *rettv);
818static void f_winwidth(typval_T *argvars, typval_T *rettv);
819static void f_writefile(typval_T *argvars, typval_T *rettv);
820static void f_wordcount(typval_T *argvars, typval_T *rettv);
821static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000822
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100823static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
824static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
825static int get_env_len(char_u **arg);
826static int get_id_len(char_u **arg);
827static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
828static 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 +0000829#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
830#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
831 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
833static int eval_isnamec(int c);
834static int eval_isnamec1(int c);
835static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
836static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static typval_T *alloc_string_tv(char_u *string);
838static void init_tv(typval_T *varp);
839static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100840#ifdef FEAT_FLOAT
841static float_T get_tv_float(typval_T *varp);
842#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100843static linenr_T get_tv_lnum(typval_T *argvars);
844static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
845static char_u *get_tv_string(typval_T *varp);
846static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
847static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
848static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
849static hashtab_T *find_var_ht(char_u *name, char_u **varname);
850static funccall_T *get_funccal(void);
851static void vars_clear_ext(hashtab_T *ht, int free_val);
852static void delete_var(hashtab_T *ht, hashitem_T *hi);
853static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
854static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
855static void set_var(char_u *name, typval_T *varp, int copy);
856static int var_check_ro(int flags, char_u *name, int use_gettext);
857static int var_check_fixed(int flags, char_u *name, int use_gettext);
858static int var_check_func_name(char_u *name, int new_var);
859static int valid_varname(char_u *varname);
860static int tv_check_lock(int lock, char_u *name, int use_gettext);
861static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
862static char_u *find_option_end(char_u **arg, int *opt_flags);
863static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
864static int eval_fname_script(char_u *p);
865static int eval_fname_sid(char_u *p);
866static void list_func_head(ufunc_T *fp, int indent);
867static ufunc_T *find_func(char_u *name);
868static int function_exists(char_u *name);
869static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000870#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100871static void func_do_profile(ufunc_T *fp);
872static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
873static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000874static int
875# ifdef __BORLANDC__
876 _RTLENTRYF
877# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000879static int
880# ifdef __BORLANDC__
881 _RTLENTRYF
882# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100883 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000884#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885static int script_autoload(char_u *name, int reload);
886static char_u *autoload_name(char_u *name);
887static void cat_func_name(char_u *buf, ufunc_T *fp);
888static void func_free(ufunc_T *fp);
889static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
890static int can_free_funccal(funccall_T *fc, int copyID) ;
891static void free_funccal(funccall_T *fc, int free_val);
892static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
893static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
894static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
895static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
896static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
897static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
898static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
899static int write_list(FILE *fd, list_T *list, int binary);
900static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000901
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200902
903#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100904static int compare_func_name(const void *s1, const void *s2);
905static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200906#endif
907
Bram Moolenaar33570922005-01-25 22:26:29 +0000908/*
909 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000910 */
911 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100912eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000913{
Bram Moolenaar33570922005-01-25 22:26:29 +0000914 int i;
915 struct vimvar *p;
916
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200917 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
918 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200919 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000920 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000921 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000922
923 for (i = 0; i < VV_LEN; ++i)
924 {
925 p = &vimvars[i];
926 STRCPY(p->vv_di.di_key, p->vv_name);
927 if (p->vv_flags & VV_RO)
928 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
929 else if (p->vv_flags & VV_RO_SBX)
930 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
931 else
932 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000933
934 /* add to v: scope dict, unless the value is not always available */
935 if (p->vv_type != VAR_UNKNOWN)
936 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000937 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000938 /* add to compat scope dict */
939 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000940 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100941 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
942
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000943 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100944 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200945 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100946 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100947
948 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
949 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
950 set_vim_var_nr(VV_NONE, VVAL_NONE);
951 set_vim_var_nr(VV_NULL, VVAL_NULL);
952
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200953 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200954
955#ifdef EBCDIC
956 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100957 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200958 */
959 sortFunctions();
960#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000961}
962
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000963#if defined(EXITFREE) || defined(PROTO)
964 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100965eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000966{
967 int i;
968 struct vimvar *p;
969
970 for (i = 0; i < VV_LEN; ++i)
971 {
972 p = &vimvars[i];
973 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000974 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000975 vim_free(p->vv_str);
976 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000977 }
978 else if (p->vv_di.di_tv.v_type == VAR_LIST)
979 {
980 list_unref(p->vv_list);
981 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000982 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000983 }
984 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000985 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000986 hash_clear(&compat_hashtab);
987
Bram Moolenaard9fba312005-06-26 22:34:35 +0000988 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100989# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200990 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100991# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000992
993 /* global variables */
994 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000995
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000996 /* autoloaded script names */
997 ga_clear_strings(&ga_loaded);
998
Bram Moolenaarcca74132013-09-25 21:00:28 +0200999 /* Script-local variables. First clear all the variables and in a second
1000 * loop free the scriptvar_T, because a variable in one script might hold
1001 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001002 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001003 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001004 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001005 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001006 ga_clear(&ga_scripts);
1007
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001008 /* unreferenced lists and dicts */
1009 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001010
1011 /* functions */
1012 free_all_functions();
1013 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001014}
1015#endif
1016
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 * Return the name of the executed function.
1019 */
1020 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001021func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024}
1025
1026/*
1027 * Return the address holding the next breakpoint line for a funccall cookie.
1028 */
1029 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001030func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031{
Bram Moolenaar33570922005-01-25 22:26:29 +00001032 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033}
1034
1035/*
1036 * Return the address holding the debug tick for a funccall cookie.
1037 */
1038 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001039func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
Bram Moolenaar33570922005-01-25 22:26:29 +00001041 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042}
1043
1044/*
1045 * Return the nesting level for a funccall cookie.
1046 */
1047 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001048func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
Bram Moolenaar33570922005-01-25 22:26:29 +00001050 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051}
1052
1053/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001054funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001056/* pointer to list of previously used funccal, still around because some
1057 * item in it is still being used. */
1058funccall_T *previous_funccal = NULL;
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060/*
1061 * Return TRUE when a function was ended by a ":return" command.
1062 */
1063 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001064current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
1066 return current_funccal->returned;
1067}
1068
1069
1070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 * Set an internal variable to a string value. Creates the variable if it does
1072 * not already exist.
1073 */
1074 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001075set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001077 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001078 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
1080 val = vim_strsave(value);
1081 if (val != NULL)
1082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001083 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001084 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001086 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001087 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089 }
1090}
1091
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001093static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094static char_u *redir_endp = NULL;
1095static char_u *redir_varname = NULL;
1096
1097/*
1098 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001099 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100 * Returns OK if successfully completed the setup. FAIL otherwise.
1101 */
1102 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001103var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104{
1105 int save_emsg;
1106 int err;
1107 typval_T tv;
1108
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001109 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001110 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 {
1112 EMSG(_(e_invarg));
1113 return FAIL;
1114 }
1115
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001116 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 redir_varname = vim_strsave(name);
1118 if (redir_varname == NULL)
1119 return FAIL;
1120
1121 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1122 if (redir_lval == NULL)
1123 {
1124 var_redir_stop();
1125 return FAIL;
1126 }
1127
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001128 /* The output is stored in growarray "redir_ga" until redirection ends. */
1129 ga_init2(&redir_ga, (int)sizeof(char), 500);
1130
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001132 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001133 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1135 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001136 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_endp != NULL && *redir_endp != NUL)
1138 /* Trailing characters are present after the variable name */
1139 EMSG(_(e_trailing));
1140 else
1141 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 var_redir_stop();
1144 return FAIL;
1145 }
1146
1147 /* check if we can write to the variable: set it to or append an empty
1148 * string */
1149 save_emsg = did_emsg;
1150 did_emsg = FALSE;
1151 tv.v_type = VAR_STRING;
1152 tv.vval.v_string = (char_u *)"";
1153 if (append)
1154 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1155 else
1156 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001157 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001159 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 if (err)
1161 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001162 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 var_redir_stop();
1164 return FAIL;
1165 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166
1167 return OK;
1168}
1169
1170/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001171 * Append "value[value_len]" to the variable set by var_redir_start().
1172 * The actual appending is postponed until redirection ends, because the value
1173 * appended may in fact be the string we write to, changing it may cause freed
1174 * memory to be used:
1175 * :redir => foo
1176 * :let foo
1177 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 */
1179 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001180var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001182 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183
1184 if (redir_lval == NULL)
1185 return;
1186
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001187 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001190 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001192 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001193 {
1194 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001195 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001196 }
1197 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199}
1200
1201/*
1202 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001203 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204 */
1205 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001206var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001208 typval_T tv;
1209
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210 if (redir_lval != NULL)
1211 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001212 /* If there was no error: assign the text to the variable. */
1213 if (redir_endp != NULL)
1214 {
1215 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1216 tv.v_type = VAR_STRING;
1217 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001218 /* Call get_lval() again, if it's inside a Dict or List it may
1219 * have changed. */
1220 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001221 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001222 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1223 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1224 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001225 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001226
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001227 /* free the collected output */
1228 vim_free(redir_ga.ga_data);
1229 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001230
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001231 vim_free(redir_lval);
1232 redir_lval = NULL;
1233 }
1234 vim_free(redir_varname);
1235 redir_varname = NULL;
1236}
1237
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238# if defined(FEAT_MBYTE) || defined(PROTO)
1239 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001240eval_charconvert(
1241 char_u *enc_from,
1242 char_u *enc_to,
1243 char_u *fname_from,
1244 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245{
1246 int err = FALSE;
1247
1248 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1249 set_vim_var_string(VV_CC_TO, enc_to, -1);
1250 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1251 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1252 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1253 err = TRUE;
1254 set_vim_var_string(VV_CC_FROM, NULL, -1);
1255 set_vim_var_string(VV_CC_TO, NULL, -1);
1256 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1257 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1258
1259 if (err)
1260 return FAIL;
1261 return OK;
1262}
1263# endif
1264
1265# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001267eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268{
1269 int err = FALSE;
1270
1271 set_vim_var_string(VV_FNAME_IN, fname, -1);
1272 set_vim_var_string(VV_CMDARG, args, -1);
1273 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1274 err = TRUE;
1275 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1276 set_vim_var_string(VV_CMDARG, NULL, -1);
1277
1278 if (err)
1279 {
1280 mch_remove(fname);
1281 return FAIL;
1282 }
1283 return OK;
1284}
1285# endif
1286
1287# if defined(FEAT_DIFF) || defined(PROTO)
1288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001289eval_diff(
1290 char_u *origfile,
1291 char_u *newfile,
1292 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 int err = FALSE;
1295
1296 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1297 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1298 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1299 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1300 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1301 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1302 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1303}
1304
1305 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001306eval_patch(
1307 char_u *origfile,
1308 char_u *difffile,
1309 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
1311 int err;
1312
1313 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1314 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1315 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1316 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1317 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1318 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1319 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1320}
1321# endif
1322
1323/*
1324 * Top level evaluation function, returning a boolean.
1325 * Sets "error" to TRUE if there was an error.
1326 * Return TRUE or FALSE.
1327 */
1328 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001329eval_to_bool(
1330 char_u *arg,
1331 int *error,
1332 char_u **nextcmd,
1333 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
Bram Moolenaar33570922005-01-25 22:26:29 +00001335 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 int retval = FALSE;
1337
1338 if (skip)
1339 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 else
1343 {
1344 *error = FALSE;
1345 if (!skip)
1346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001347 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350 }
1351 if (skip)
1352 --emsg_skip;
1353
1354 return retval;
1355}
1356
1357/*
1358 * Top level evaluation function, returning a string. If "skip" is TRUE,
1359 * only parsing to "nextcmd" is done, without reporting errors. Return
1360 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1361 */
1362 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001363eval_to_string_skip(
1364 char_u *arg,
1365 char_u **nextcmd,
1366 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367{
Bram Moolenaar33570922005-01-25 22:26:29 +00001368 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 char_u *retval;
1370
1371 if (skip)
1372 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 retval = NULL;
1375 else
1376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 retval = vim_strsave(get_tv_string(&tv));
1378 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 }
1380 if (skip)
1381 --emsg_skip;
1382
1383 return retval;
1384}
1385
1386/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001387 * Skip over an expression at "*pp".
1388 * Return FAIL for an error, OK otherwise.
1389 */
1390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001391skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001392{
Bram Moolenaar33570922005-01-25 22:26:29 +00001393 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001394
1395 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001396 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001397}
1398
1399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001401 * When "convert" is TRUE convert a List into a sequence of lines and convert
1402 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 * Return pointer to allocated memory, or NULL for failure.
1404 */
1405 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001406eval_to_string(
1407 char_u *arg,
1408 char_u **nextcmd,
1409 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001413 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001414#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001415 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 retval = NULL;
1420 else
1421 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001422 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001423 {
1424 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001425 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001426 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001427 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001428 if (tv.vval.v_list->lv_len > 0)
1429 ga_append(&ga, NL);
1430 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001431 ga_append(&ga, NUL);
1432 retval = (char_u *)ga.ga_data;
1433 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001434#ifdef FEAT_FLOAT
1435 else if (convert && tv.v_type == VAR_FLOAT)
1436 {
1437 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1438 retval = vim_strsave(numbuf);
1439 }
1440#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001441 else
1442 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001443 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445
1446 return retval;
1447}
1448
1449/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001450 * Call eval_to_string() without using current local variables and using
1451 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 */
1453 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001454eval_to_string_safe(
1455 char_u *arg,
1456 char_u **nextcmd,
1457 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 char_u *retval;
1460 void *save_funccalp;
1461
1462 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001463 if (use_sandbox)
1464 ++sandbox;
1465 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001466 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001467 if (use_sandbox)
1468 --sandbox;
1469 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 restore_funccal(save_funccalp);
1471 return retval;
1472}
1473
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474/*
1475 * Top level evaluation function, returning a number.
1476 * Evaluates "expr" silently.
1477 * Returns -1 for an error.
1478 */
1479 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001480eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481{
Bram Moolenaar33570922005-01-25 22:26:29 +00001482 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001484 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
1486 ++emsg_off;
1487
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001488 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 retval = -1;
1490 else
1491 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001492 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001493 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
1495 --emsg_off;
1496
1497 return retval;
1498}
1499
Bram Moolenaara40058a2005-07-11 22:42:07 +00001500/*
1501 * Prepare v: variable "idx" to be used.
1502 * Save the current typeval in "save_tv".
1503 * When not used yet add the variable to the v: hashtable.
1504 */
1505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001506prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001507{
1508 *save_tv = vimvars[idx].vv_tv;
1509 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1510 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1511}
1512
1513/*
1514 * Restore v: variable "idx" to typeval "save_tv".
1515 * When no longer defined, remove the variable from the v: hashtable.
1516 */
1517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001518restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001519{
1520 hashitem_T *hi;
1521
Bram Moolenaara40058a2005-07-11 22:42:07 +00001522 vimvars[idx].vv_tv = *save_tv;
1523 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1524 {
1525 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1526 if (HASHITEM_EMPTY(hi))
1527 EMSG2(_(e_intern2), "restore_vimvar()");
1528 else
1529 hash_remove(&vimvarht, hi);
1530 }
1531}
1532
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001533#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001534/*
1535 * Evaluate an expression to a list with suggestions.
1536 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001537 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001538 */
1539 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001540eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001541{
1542 typval_T save_val;
1543 typval_T rettv;
1544 list_T *list = NULL;
1545 char_u *p = skipwhite(expr);
1546
1547 /* Set "v:val" to the bad word. */
1548 prepare_vimvar(VV_VAL, &save_val);
1549 vimvars[VV_VAL].vv_type = VAR_STRING;
1550 vimvars[VV_VAL].vv_str = badword;
1551 if (p_verbose == 0)
1552 ++emsg_off;
1553
1554 if (eval1(&p, &rettv, TRUE) == OK)
1555 {
1556 if (rettv.v_type != VAR_LIST)
1557 clear_tv(&rettv);
1558 else
1559 list = rettv.vval.v_list;
1560 }
1561
1562 if (p_verbose == 0)
1563 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001564 restore_vimvar(VV_VAL, &save_val);
1565
1566 return list;
1567}
1568
1569/*
1570 * "list" is supposed to contain two items: a word and a number. Return the
1571 * word in "pp" and the number as the return value.
1572 * Return -1 if anything isn't right.
1573 * Used to get the good word and score from the eval_spell_expr() result.
1574 */
1575 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001576get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001577{
1578 listitem_T *li;
1579
1580 li = list->lv_first;
1581 if (li == NULL)
1582 return -1;
1583 *pp = get_tv_string(&li->li_tv);
1584
1585 li = li->li_next;
1586 if (li == NULL)
1587 return -1;
1588 return get_tv_number(&li->li_tv);
1589}
1590#endif
1591
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001592/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001593 * Top level evaluation function.
1594 * Returns an allocated typval_T with the result.
1595 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001596 */
1597 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001598eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001599{
1600 typval_T *tv;
1601
1602 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001603 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001604 {
1605 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001606 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001607 }
1608
1609 return tv;
1610}
1611
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001615 * Uses argv[argc] for the function arguments. Only Number and String
1616 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001619 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001620call_vim_function(
1621 char_u *func,
1622 int argc,
1623 char_u **argv,
1624 int safe, /* use the sandbox */
1625 int str_arg_only, /* all arguments are strings */
1626 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627{
Bram Moolenaar33570922005-01-25 22:26:29 +00001628 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 long n;
1630 int len;
1631 int i;
1632 int doesrange;
1633 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001636 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
1640 for (i = 0; i < argc; i++)
1641 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001642 /* Pass a NULL or empty argument as an empty string */
1643 if (argv[i] == NULL || *argv[i] == NUL)
1644 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001645 argvars[i].v_type = VAR_STRING;
1646 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001647 continue;
1648 }
1649
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001650 if (str_arg_only)
1651 len = 0;
1652 else
1653 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001654 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (len != 0 && len == (int)STRLEN(argv[i]))
1656 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001657 argvars[i].v_type = VAR_NUMBER;
1658 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 }
1660 else
1661 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001662 argvars[i].v_type = VAR_STRING;
1663 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 }
1665 }
1666
1667 if (safe)
1668 {
1669 save_funccalp = save_funccal();
1670 ++sandbox;
1671 }
1672
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1674 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (safe)
1678 {
1679 --sandbox;
1680 restore_funccal(save_funccalp);
1681 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 vim_free(argvars);
1683
1684 if (ret == FAIL)
1685 clear_tv(rettv);
1686
1687 return ret;
1688}
1689
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001690/*
1691 * Call vimL function "func" and return the result as a number.
1692 * Returns -1 when calling the function fails.
1693 * Uses argv[argc] for the function arguments.
1694 */
1695 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001696call_func_retnr(
1697 char_u *func,
1698 int argc,
1699 char_u **argv,
1700 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001701{
1702 typval_T rettv;
1703 long retval;
1704
1705 /* All arguments are passed as strings, no conversion to number. */
1706 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1707 return -1;
1708
1709 retval = get_tv_number_chk(&rettv, NULL);
1710 clear_tv(&rettv);
1711 return retval;
1712}
1713
1714#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1715 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1716
Bram Moolenaar4f688582007-07-24 12:34:30 +00001717# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001719 * Call vimL function "func" and return the result as a string.
1720 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 * Uses argv[argc] for the function arguments.
1722 */
1723 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001724call_func_retstr(
1725 char_u *func,
1726 int argc,
1727 char_u **argv,
1728 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001729{
1730 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001731 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732
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 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 return retval;
1740}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001741# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001742
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001743/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001744 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001746 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747 */
1748 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001749call_func_retlist(
1750 char_u *func,
1751 int argc,
1752 char_u **argv,
1753 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001754{
1755 typval_T rettv;
1756
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001757 /* All arguments are passed as strings, no conversion to number. */
1758 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001759 return NULL;
1760
1761 if (rettv.v_type != VAR_LIST)
1762 {
1763 clear_tv(&rettv);
1764 return NULL;
1765 }
1766
1767 return rettv.vval.v_list;
1768}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769#endif
1770
1771/*
1772 * Save the current function call pointer, and set it to NULL.
1773 * Used when executing autocommands and for ":source".
1774 */
1775 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001776save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001778 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 current_funccal = NULL;
1781 return (void *)fc;
1782}
1783
1784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001785restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001787 funccall_T *fc = (funccall_T *)vfc;
1788
1789 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790}
1791
Bram Moolenaar05159a02005-02-26 23:04:13 +00001792#if defined(FEAT_PROFILE) || defined(PROTO)
1793/*
1794 * Prepare profiling for entering a child or something else that is not
1795 * counted for the script/function itself.
1796 * Should always be called in pair with prof_child_exit().
1797 */
1798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001799prof_child_enter(
1800 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001801{
1802 funccall_T *fc = current_funccal;
1803
1804 if (fc != NULL && fc->func->uf_profiling)
1805 profile_start(&fc->prof_child);
1806 script_prof_save(tm);
1807}
1808
1809/*
1810 * Take care of time spent in a child.
1811 * Should always be called after prof_child_enter().
1812 */
1813 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001814prof_child_exit(
1815 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001816{
1817 funccall_T *fc = current_funccal;
1818
1819 if (fc != NULL && fc->func->uf_profiling)
1820 {
1821 profile_end(&fc->prof_child);
1822 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1823 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1824 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1825 }
1826 script_prof_restore(tm);
1827}
1828#endif
1829
1830
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#ifdef FEAT_FOLDING
1832/*
1833 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1834 * it in "*cp". Doesn't give error messages.
1835 */
1836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001837eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838{
Bram Moolenaar33570922005-01-25 22:26:29 +00001839 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 int retval;
1841 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001842 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1843 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
1845 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001846 if (use_sandbox)
1847 ++sandbox;
1848 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 retval = 0;
1852 else
1853 {
1854 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 if (tv.v_type == VAR_NUMBER)
1856 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001857 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 retval = 0;
1859 else
1860 {
1861 /* If the result is a string, check if there is a non-digit before
1862 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (!VIM_ISDIGIT(*s) && *s != '-')
1865 *cp = *s++;
1866 retval = atol((char *)s);
1867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 }
1870 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001871 if (use_sandbox)
1872 --sandbox;
1873 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874
1875 return retval;
1876}
1877#endif
1878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880 * ":let" list all variable values
1881 * ":let var1 var2" list variable values
1882 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001883 * ":let var += expr" assignment command.
1884 * ":let var -= expr" assignment command.
1885 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 */
1888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001889ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890{
1891 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001893 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 int var_count = 0;
1896 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001898 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001899 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
Bram Moolenaardb552d602006-03-23 22:59:57 +00001901 argend = skip_var_list(arg, &var_count, &semicolon);
1902 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001904 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1905 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001906 expr = skipwhite(argend);
1907 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1908 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001910 /*
1911 * ":let" without "=": list variables
1912 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 if (*arg == '[')
1914 EMSG(_(e_invarg));
1915 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001917 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001921 list_glob_vars(&first);
1922 list_buf_vars(&first);
1923 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001924#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001925 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001926#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001927 list_script_vars(&first);
1928 list_func_vars(&first);
1929 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 eap->nextcmd = check_nextcmd(arg);
1932 }
1933 else
1934 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 op[0] = '=';
1936 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001937 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001939 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1940 op[0] = *expr; /* +=, -= or .= */
1941 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001942 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001943 else
1944 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (eap->skip)
1947 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (eap->skip)
1950 {
1951 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 --emsg_skip;
1954 }
1955 else if (i != FAIL)
1956 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
1961 }
1962}
1963
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964/*
1965 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1966 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 * When "nextchars" is not NULL it points to a string with characters that
1968 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1969 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 * Returns OK or FAIL;
1971 */
1972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001973ex_let_vars(
1974 char_u *arg_start,
1975 typval_T *tv,
1976 int copy, /* copy values from "tv", don't move */
1977 int semicolon, /* from skip_var_list() */
1978 int var_count, /* from skip_var_list() */
1979 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980{
1981 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001984 listitem_T *item;
1985 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986
1987 if (*arg != '[')
1988 {
1989 /*
1990 * ":let var = expr" or ":for var in list"
1991 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001992 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993 return FAIL;
1994 return OK;
1995 }
1996
1997 /*
1998 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1999 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002000 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 {
2002 EMSG(_(e_listreq));
2003 return FAIL;
2004 }
2005
2006 i = list_len(l);
2007 if (semicolon == 0 && var_count < i)
2008 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002009 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010 return FAIL;
2011 }
2012 if (var_count - semicolon > i)
2013 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002014 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002015 return FAIL;
2016 }
2017
2018 item = l->lv_first;
2019 while (*arg != ']')
2020 {
2021 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 item = item->li_next;
2024 if (arg == NULL)
2025 return FAIL;
2026
2027 arg = skipwhite(arg);
2028 if (*arg == ';')
2029 {
2030 /* Put the rest of the list (may be empty) in the var after ';'.
2031 * Create a new list for this. */
2032 l = list_alloc();
2033 if (l == NULL)
2034 return FAIL;
2035 while (item != NULL)
2036 {
2037 list_append_tv(l, &item->li_tv);
2038 item = item->li_next;
2039 }
2040
2041 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002042 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002043 ltv.vval.v_list = l;
2044 l->lv_refcount = 1;
2045
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002046 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2047 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 clear_tv(&ltv);
2049 if (arg == NULL)
2050 return FAIL;
2051 break;
2052 }
2053 else if (*arg != ',' && *arg != ']')
2054 {
2055 EMSG2(_(e_intern2), "ex_let_vars()");
2056 return FAIL;
2057 }
2058 }
2059
2060 return OK;
2061}
2062
2063/*
2064 * Skip over assignable variable "var" or list of variables "[var, var]".
2065 * Used for ":let varvar = expr" and ":for varvar in expr".
2066 * For "[var, var]" increment "*var_count" for each variable.
2067 * for "[var, var; var]" set "semicolon".
2068 * Return NULL for an error.
2069 */
2070 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002071skip_var_list(
2072 char_u *arg,
2073 int *var_count,
2074 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002075{
2076 char_u *p, *s;
2077
2078 if (*arg == '[')
2079 {
2080 /* "[var, var]": find the matching ']'. */
2081 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002082 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002083 {
2084 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2085 s = skip_var_one(p);
2086 if (s == p)
2087 {
2088 EMSG2(_(e_invarg2), p);
2089 return NULL;
2090 }
2091 ++*var_count;
2092
2093 p = skipwhite(s);
2094 if (*p == ']')
2095 break;
2096 else if (*p == ';')
2097 {
2098 if (*semicolon == 1)
2099 {
2100 EMSG(_("Double ; in list of variables"));
2101 return NULL;
2102 }
2103 *semicolon = 1;
2104 }
2105 else if (*p != ',')
2106 {
2107 EMSG2(_(e_invarg2), p);
2108 return NULL;
2109 }
2110 }
2111 return p + 1;
2112 }
2113 else
2114 return skip_var_one(arg);
2115}
2116
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002117/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002118 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002119 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002122skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002123{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002124 if (*arg == '@' && arg[1] != NUL)
2125 return arg + 2;
2126 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2127 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002128}
2129
Bram Moolenaara7043832005-01-21 11:56:39 +00002130/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002131 * List variables for hashtab "ht" with prefix "prefix".
2132 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002133 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002135list_hashtable_vars(
2136 hashtab_T *ht,
2137 char_u *prefix,
2138 int empty,
2139 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002140{
Bram Moolenaar33570922005-01-25 22:26:29 +00002141 hashitem_T *hi;
2142 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002143 int todo;
2144
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002145 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002146 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2147 {
2148 if (!HASHITEM_EMPTY(hi))
2149 {
2150 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002151 di = HI2DI(hi);
2152 if (empty || di->di_tv.v_type != VAR_STRING
2153 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002155 }
2156 }
2157}
2158
2159/*
2160 * List global variables.
2161 */
2162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002163list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002164{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002166}
2167
2168/*
2169 * List buffer variables.
2170 */
2171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002172list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002173{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002174 char_u numbuf[NUMBUFLEN];
2175
Bram Moolenaar429fa852013-04-15 12:27:36 +02002176 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002178
2179 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2181 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002182}
2183
2184/*
2185 * List window variables.
2186 */
2187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002188list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002189{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002190 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002192}
2193
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002194#ifdef FEAT_WINDOWS
2195/*
2196 * List tab page variables.
2197 */
2198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002199list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002200{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002201 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203}
2204#endif
2205
Bram Moolenaara7043832005-01-21 11:56:39 +00002206/*
2207 * List Vim variables.
2208 */
2209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002210list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002212 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213}
2214
2215/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216 * List script-local variables, if there is a script.
2217 */
2218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002219list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220{
2221 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002222 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2223 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224}
2225
2226/*
2227 * List function variables, if there is a function.
2228 */
2229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002230list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002231{
2232 if (current_funccal != NULL)
2233 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002234 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002235}
2236
2237/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 * List variables in "arg".
2239 */
2240 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002241list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242{
2243 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 char_u *name_start;
2247 char_u *arg_subsc;
2248 char_u *tofree;
2249 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250
2251 while (!ends_excmd(*arg) && !got_int)
2252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002255 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2257 {
2258 emsg_severe = TRUE;
2259 EMSG(_(e_trailing));
2260 break;
2261 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 /* get_name_len() takes care of expanding curly braces */
2266 name_start = name = arg;
2267 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2268 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 /* This is mainly to keep test 49 working: when expanding
2271 * curly braces fails overrule the exception error message. */
2272 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 emsg_severe = TRUE;
2275 EMSG2(_(e_invarg2), arg);
2276 break;
2277 }
2278 error = TRUE;
2279 }
2280 else
2281 {
2282 if (tofree != NULL)
2283 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002284 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 else
2287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 /* handle d.key, l[idx], f(expr) */
2289 arg_subsc = arg;
2290 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002298 case 'g': list_glob_vars(first); break;
2299 case 'b': list_buf_vars(first); break;
2300 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002301#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002303#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002304 case 'v': list_vim_vars(first); break;
2305 case 's': list_script_vars(first); break;
2306 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 default:
2308 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002309 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002310 }
2311 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 {
2313 char_u numbuf[NUMBUFLEN];
2314 char_u *tf;
2315 int c;
2316 char_u *s;
2317
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002318 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002319 c = *arg;
2320 *arg = NUL;
2321 list_one_var_a((char_u *)"",
2322 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002323 tv.v_type,
2324 s == NULL ? (char_u *)"" : s,
2325 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326 *arg = c;
2327 vim_free(tf);
2328 }
2329 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 }
2332 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002333
2334 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336
2337 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
2339
2340 return arg;
2341}
2342
2343/*
2344 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2345 * Returns a pointer to the char just after the var name.
2346 * Returns NULL if there is an error.
2347 */
2348 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002349ex_let_one(
2350 char_u *arg, /* points to variable name */
2351 typval_T *tv, /* value to assign to variable */
2352 int copy, /* copy value from "tv" */
2353 char_u *endchars, /* valid chars after variable name or NULL */
2354 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355{
2356 int c1;
2357 char_u *name;
2358 char_u *p;
2359 char_u *arg_end = NULL;
2360 int len;
2361 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002362 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363
2364 /*
2365 * ":let $VAR = expr": Set environment variable.
2366 */
2367 if (*arg == '$')
2368 {
2369 /* Find the end of the name. */
2370 ++arg;
2371 name = arg;
2372 len = get_env_len(&arg);
2373 if (len == 0)
2374 EMSG2(_(e_invarg2), name - 1);
2375 else
2376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 if (op != NULL && (*op == '+' || *op == '-'))
2378 EMSG2(_(e_letwrong), op);
2379 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002380 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002382 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 {
2384 c1 = name[len];
2385 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 p = get_tv_string_chk(tv);
2387 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 {
2389 int mustfree = FALSE;
2390 char_u *s = vim_getenv(name, &mustfree);
2391
2392 if (s != NULL)
2393 {
2394 p = tofree = concat_str(s, p);
2395 if (mustfree)
2396 vim_free(s);
2397 }
2398 }
2399 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002400 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002402 if (STRICMP(name, "HOME") == 0)
2403 init_homedir();
2404 else if (didset_vim && STRICMP(name, "VIM") == 0)
2405 didset_vim = FALSE;
2406 else if (didset_vimruntime
2407 && STRICMP(name, "VIMRUNTIME") == 0)
2408 didset_vimruntime = FALSE;
2409 arg_end = arg;
2410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002411 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 }
2414 }
2415 }
2416
2417 /*
2418 * ":let &option = expr": Set option value.
2419 * ":let &l:option = expr": Set local option value.
2420 * ":let &g:option = expr": Set global option value.
2421 */
2422 else if (*arg == '&')
2423 {
2424 /* Find the end of the name. */
2425 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002426 if (p == NULL || (endchars != NULL
2427 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 EMSG(_(e_letunexp));
2429 else
2430 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 long n;
2432 int opt_type;
2433 long numval;
2434 char_u *stringval = NULL;
2435 char_u *s;
2436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 c1 = *p;
2438 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439
2440 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 s = get_tv_string_chk(tv); /* != NULL if number or string */
2442 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 {
2444 opt_type = get_option_value(arg, &numval,
2445 &stringval, opt_flags);
2446 if ((opt_type == 1 && *op == '.')
2447 || (opt_type == 0 && *op != '.'))
2448 EMSG2(_(e_letwrong), op);
2449 else
2450 {
2451 if (opt_type == 1) /* number */
2452 {
2453 if (*op == '+')
2454 n = numval + n;
2455 else
2456 n = numval - n;
2457 }
2458 else if (opt_type == 0 && stringval != NULL) /* string */
2459 {
2460 s = concat_str(stringval, s);
2461 vim_free(stringval);
2462 stringval = s;
2463 }
2464 }
2465 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002466 if (s != NULL)
2467 {
2468 set_option_value(arg, n, s, opt_flags);
2469 arg_end = p;
2470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
2474 }
2475
2476 /*
2477 * ":let @r = expr": Set register contents.
2478 */
2479 else if (*arg == '@')
2480 {
2481 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 if (op != NULL && (*op == '+' || *op == '-'))
2483 EMSG2(_(e_letwrong), op);
2484 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002485 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002486 EMSG(_(e_letunexp));
2487 else
2488 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002489 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 char_u *s;
2491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 p = get_tv_string_chk(tv);
2493 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002495 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 if (s != NULL)
2497 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002498 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 vim_free(s);
2500 }
2501 }
2502 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002504 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 arg_end = arg + 1;
2506 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002507 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 }
2509 }
2510
2511 /*
2512 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002515 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002517 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002519 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2523 EMSG(_(e_letunexp));
2524 else
2525 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002526 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 arg_end = p;
2528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 }
2532
2533 else
2534 EMSG2(_(e_invarg2), arg);
2535
2536 return arg_end;
2537}
2538
2539/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2541 */
2542 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002543check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544{
2545 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2546 {
2547 EMSG2(_(e_readonlyvar), arg);
2548 return TRUE;
2549 }
2550 return FALSE;
2551}
2552
2553/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 * Get an lval: variable, Dict item or List item that can be assigned a value
2555 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2556 * "name.key", "name.key[expr]" etc.
2557 * Indexing only works if "name" is an existing List or Dictionary.
2558 * "name" points to the start of the name.
2559 * If "rettv" is not NULL it points to the value to be assigned.
2560 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2561 * wrong; must end in space or cmd separator.
2562 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002563 * flags:
2564 * GLV_QUIET: do not give error messages
2565 * GLV_NO_AUTOLOAD: do not use script autoloading
2566 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002568 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 */
2571 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002572get_lval(
2573 char_u *name,
2574 typval_T *rettv,
2575 lval_T *lp,
2576 int unlet,
2577 int skip,
2578 int flags, /* GLV_ values */
2579 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 char_u *expr_start, *expr_end;
2583 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 dictitem_T *v;
2585 typval_T var1;
2586 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002589 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002592 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002595 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596
2597 if (skip)
2598 {
2599 /* When skipping just find the end of the name. */
2600 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002601 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 }
2603
2604 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002605 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (expr_start != NULL)
2607 {
2608 /* Don't expand the name when we already know there is an error. */
2609 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2610 && *p != '[' && *p != '.')
2611 {
2612 EMSG(_(e_trailing));
2613 return NULL;
2614 }
2615
2616 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2617 if (lp->ll_exp_name == NULL)
2618 {
2619 /* Report an invalid expression in braces, unless the
2620 * expression evaluation has been cancelled due to an
2621 * aborting error, an interrupt, or an exception. */
2622 if (!aborting() && !quiet)
2623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002624 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 EMSG2(_(e_invarg2), name);
2626 return NULL;
2627 }
2628 }
2629 lp->ll_name = lp->ll_exp_name;
2630 }
2631 else
2632 lp->ll_name = name;
2633
2634 /* Without [idx] or .key we are done. */
2635 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2636 return p;
2637
2638 cc = *p;
2639 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002640 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (v == NULL && !quiet)
2642 EMSG2(_(e_undefvar), lp->ll_name);
2643 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002644 if (v == NULL)
2645 return NULL;
2646
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 /*
2648 * Loop until no more [idx] or .key is following.
2649 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002650 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2654 && !(lp->ll_tv->v_type == VAR_DICT
2655 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (!quiet)
2658 EMSG(_("E689: Can only index a List or Dictionary"));
2659 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!quiet)
2664 EMSG(_("E708: [:] must come last"));
2665 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 len = -1;
2669 if (*p == '.')
2670 {
2671 key = p + 1;
2672 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2673 ;
2674 if (len == 0)
2675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
2677 EMSG(_(e_emptykey));
2678 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 }
2680 p = key + len;
2681 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002682 else
2683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 if (*p == ':')
2687 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 else
2689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 empty1 = FALSE;
2691 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002693 if (get_tv_string_chk(&var1) == NULL)
2694 {
2695 /* not a number or string */
2696 clear_tv(&var1);
2697 return NULL;
2698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
2700
2701 /* Optionally get the second index [ :expr]. */
2702 if (*p == ':')
2703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002707 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002708 if (!empty1)
2709 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (rettv != NULL && (rettv->v_type != VAR_LIST
2713 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (!quiet)
2716 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (!empty1)
2718 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 }
2721 p = skipwhite(p + 1);
2722 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 else
2725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2728 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (!empty1)
2730 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002733 if (get_tv_string_chk(&var2) == NULL)
2734 {
2735 /* not a number or string */
2736 if (!empty1)
2737 clear_tv(&var1);
2738 clear_tv(&var2);
2739 return NULL;
2740 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002743 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (!quiet)
2750 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 if (!empty1)
2752 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 }
2757
2758 /* Skip to past ']'. */
2759 ++p;
2760 }
2761
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 {
2764 if (len == -1)
2765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002767 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (*key == NUL)
2769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 if (!quiet)
2771 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 }
2775 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002776 lp->ll_list = NULL;
2777 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002778 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002780 /* When assigning to a scope dictionary check that a function and
2781 * variable name is valid (only variable name unless it is l: or
2782 * g: dictionary). Disallow overwriting a builtin function. */
2783 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002784 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002785 int prevval;
2786 int wrong;
2787
2788 if (len != -1)
2789 {
2790 prevval = key[len];
2791 key[len] = NUL;
2792 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002793 else
2794 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002795 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2796 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002797 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002798 || !valid_varname(key);
2799 if (len != -1)
2800 key[len] = prevval;
2801 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002802 return NULL;
2803 }
2804
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002807 /* Can't add "v:" variable. */
2808 if (lp->ll_dict == &vimvardict)
2809 {
2810 EMSG2(_(e_illvar), name);
2811 return NULL;
2812 }
2813
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002814 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002818 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 if (len == -1)
2820 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 }
2823 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 if (len == -1)
2828 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 p = NULL;
2831 break;
2832 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002834 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002835 return NULL;
2836
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 if (len == -1)
2838 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 }
2841 else
2842 {
2843 /*
2844 * Get the number and item for the only or first index of the List.
2845 */
2846 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 else
2849 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002850 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 clear_tv(&var1);
2852 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002853 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 lp->ll_list = lp->ll_tv->vval.v_list;
2855 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2856 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002858 if (lp->ll_n1 < 0)
2859 {
2860 lp->ll_n1 = 0;
2861 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2862 }
2863 }
2864 if (lp->ll_li == NULL)
2865 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002868 if (!quiet)
2869 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 }
2872
2873 /*
2874 * May need to find the item or absolute index for the second
2875 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 * When no index given: "lp->ll_empty2" is TRUE.
2877 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002881 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002887 {
2888 if (!quiet)
2889 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002891 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 }
2894
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2896 if (lp->ll_n1 < 0)
2897 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2898 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002899 {
2900 if (!quiet)
2901 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002903 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002904 }
2905
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002907 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002908 }
2909
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 return p;
2911}
2912
2913/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 */
2916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002917clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918{
2919 vim_free(lp->ll_exp_name);
2920 vim_free(lp->ll_newkey);
2921}
2922
2923/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002924 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 */
2928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002929set_var_lval(
2930 lval_T *lp,
2931 char_u *endp,
2932 typval_T *rettv,
2933 int copy,
2934 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935{
2936 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002937 listitem_T *ri;
2938 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939
2940 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002941 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 cc = *endp;
2945 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 if (op != NULL && *op != '=')
2947 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002948 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002950 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002951 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002952 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002953 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002955 if ((di == NULL
2956 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2957 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2958 FALSE)))
2959 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002960 set_var(lp->ll_name, &tv, FALSE);
2961 clear_tv(&tv);
2962 }
2963 }
2964 else
2965 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002967 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002969 else if (tv_check_lock(lp->ll_newkey == NULL
2970 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002971 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002972 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 else if (lp->ll_range)
2974 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002975 listitem_T *ll_li = lp->ll_li;
2976 int ll_n1 = lp->ll_n1;
2977
2978 /*
2979 * Check whether any of the list items is locked
2980 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002981 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002982 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002983 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002984 return;
2985 ri = ri->li_next;
2986 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2987 break;
2988 ll_li = ll_li->li_next;
2989 ++ll_n1;
2990 }
2991
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 /*
2993 * Assign the List values to the list items.
2994 */
2995 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002996 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 if (op != NULL && *op != '=')
2998 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2999 else
3000 {
3001 clear_tv(&lp->ll_li->li_tv);
3002 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3003 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 ri = ri->li_next;
3005 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3006 break;
3007 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003010 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003011 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 ri = NULL;
3013 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003014 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003015 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 lp->ll_li = lp->ll_li->li_next;
3017 ++lp->ll_n1;
3018 }
3019 if (ri != NULL)
3020 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003021 else if (lp->ll_empty2
3022 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 : lp->ll_n1 != lp->ll_n2)
3024 EMSG(_("E711: List value has not enough items"));
3025 }
3026 else
3027 {
3028 /*
3029 * Assign to a List or Dictionary item.
3030 */
3031 if (lp->ll_newkey != NULL)
3032 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003033 if (op != NULL && *op != '=')
3034 {
3035 EMSG2(_(e_letwrong), op);
3036 return;
3037 }
3038
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003040 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 if (di == NULL)
3042 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003043 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3044 {
3045 vim_free(di);
3046 return;
3047 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003049 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003050 else if (op != NULL && *op != '=')
3051 {
3052 tv_op(lp->ll_tv, rettv, op);
3053 return;
3054 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003055 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003056 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003057
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003058 /*
3059 * Assign the value to the variable or list item.
3060 */
3061 if (copy)
3062 copy_tv(rettv, lp->ll_tv);
3063 else
3064 {
3065 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003066 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003068 }
3069 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003070}
3071
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003072/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3074 * Returns OK or FAIL.
3075 */
3076 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003077tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003078{
3079 long n;
3080 char_u numbuf[NUMBUFLEN];
3081 char_u *s;
3082
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003083 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3084 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3085 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086 {
3087 switch (tv1->v_type)
3088 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003089 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 case VAR_DICT:
3091 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003092 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003093 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003094 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003095 break;
3096
3097 case VAR_LIST:
3098 if (*op != '+' || tv2->v_type != VAR_LIST)
3099 break;
3100 /* List += List */
3101 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3102 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3103 return OK;
3104
3105 case VAR_NUMBER:
3106 case VAR_STRING:
3107 if (tv2->v_type == VAR_LIST)
3108 break;
3109 if (*op == '+' || *op == '-')
3110 {
3111 /* nr += nr or nr -= nr*/
3112 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003113#ifdef FEAT_FLOAT
3114 if (tv2->v_type == VAR_FLOAT)
3115 {
3116 float_T f = n;
3117
3118 if (*op == '+')
3119 f += tv2->vval.v_float;
3120 else
3121 f -= tv2->vval.v_float;
3122 clear_tv(tv1);
3123 tv1->v_type = VAR_FLOAT;
3124 tv1->vval.v_float = f;
3125 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127#endif
3128 {
3129 if (*op == '+')
3130 n += get_tv_number(tv2);
3131 else
3132 n -= get_tv_number(tv2);
3133 clear_tv(tv1);
3134 tv1->v_type = VAR_NUMBER;
3135 tv1->vval.v_number = n;
3136 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 }
3138 else
3139 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003140 if (tv2->v_type == VAR_FLOAT)
3141 break;
3142
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003143 /* str .= str */
3144 s = get_tv_string(tv1);
3145 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3146 clear_tv(tv1);
3147 tv1->v_type = VAR_STRING;
3148 tv1->vval.v_string = s;
3149 }
3150 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003151
3152#ifdef FEAT_FLOAT
3153 case VAR_FLOAT:
3154 {
3155 float_T f;
3156
3157 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3158 && tv2->v_type != VAR_NUMBER
3159 && tv2->v_type != VAR_STRING))
3160 break;
3161 if (tv2->v_type == VAR_FLOAT)
3162 f = tv2->vval.v_float;
3163 else
3164 f = get_tv_number(tv2);
3165 if (*op == '+')
3166 tv1->vval.v_float += f;
3167 else
3168 tv1->vval.v_float -= f;
3169 }
3170 return OK;
3171#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003172 }
3173 }
3174
3175 EMSG2(_(e_letwrong), op);
3176 return FAIL;
3177}
3178
3179/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180 * Add a watcher to a list.
3181 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003183list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184{
3185 lw->lw_next = l->lv_watch;
3186 l->lv_watch = lw;
3187}
3188
3189/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003190 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 * No warning when it isn't found...
3192 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003194list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195{
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197
3198 lwp = &l->lv_watch;
3199 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3200 {
3201 if (lw == lwrem)
3202 {
3203 *lwp = lw->lw_next;
3204 break;
3205 }
3206 lwp = &lw->lw_next;
3207 }
3208}
3209
3210/*
3211 * Just before removing an item from a list: advance watchers to the next
3212 * item.
3213 */
3214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003215list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216{
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218
3219 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3220 if (lw->lw_item == item)
3221 lw->lw_item = item->li_next;
3222}
3223
3224/*
3225 * Evaluate the expression used in a ":for var in expr" command.
3226 * "arg" points to "var".
3227 * Set "*errp" to TRUE for an error, FALSE otherwise;
3228 * Return a pointer that holds the info. Null when there is an error.
3229 */
3230 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003231eval_for_line(
3232 char_u *arg,
3233 int *errp,
3234 char_u **nextcmdp,
3235 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236{
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 typval_T tv;
3240 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241
3242 *errp = TRUE; /* default: there is an error */
3243
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 if (fi == NULL)
3246 return NULL;
3247
3248 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3249 if (expr == NULL)
3250 return fi;
3251
3252 expr = skipwhite(expr);
3253 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3254 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003255 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256 return fi;
3257 }
3258
3259 if (skip)
3260 ++emsg_skip;
3261 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3262 {
3263 *errp = FALSE;
3264 if (!skip)
3265 {
3266 l = tv.vval.v_list;
3267 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003270 clear_tv(&tv);
3271 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 else
3273 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003274 /* No need to increment the refcount, it's already set for the
3275 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 fi->fi_list = l;
3277 list_add_watch(l, &fi->fi_lw);
3278 fi->fi_lw.lw_item = l->lv_first;
3279 }
3280 }
3281 }
3282 if (skip)
3283 --emsg_skip;
3284
3285 return fi;
3286}
3287
3288/*
3289 * Use the first item in a ":for" list. Advance to the next.
3290 * Assign the values to the variable (list). "arg" points to the first one.
3291 * Return TRUE when a valid item was found, FALSE when at end of list or
3292 * something wrong.
3293 */
3294 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003295next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003297 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300
3301 item = fi->fi_lw.lw_item;
3302 if (item == NULL)
3303 result = FALSE;
3304 else
3305 {
3306 fi->fi_lw.lw_item = item->li_next;
3307 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3308 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3309 }
3310 return result;
3311}
3312
3313/*
3314 * Free the structure used to store info used by ":for".
3315 */
3316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003317free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318{
Bram Moolenaar33570922005-01-25 22:26:29 +00003319 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003321 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003322 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003324 list_unref(fi->fi_list);
3325 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 vim_free(fi);
3327}
3328
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3330
3331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003332set_context_for_expression(
3333 expand_T *xp,
3334 char_u *arg,
3335 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336{
3337 int got_eq = FALSE;
3338 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 if (cmdidx == CMD_let)
3342 {
3343 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003344 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 {
3346 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003347 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 {
3349 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003350 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 if (vim_iswhite(*p))
3352 break;
3353 }
3354 return;
3355 }
3356 }
3357 else
3358 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3359 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 while ((xp->xp_pattern = vim_strpbrk(arg,
3361 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3362 {
3363 c = *xp->xp_pattern;
3364 if (c == '&')
3365 {
3366 c = xp->xp_pattern[1];
3367 if (c == '&')
3368 {
3369 ++xp->xp_pattern;
3370 xp->xp_context = cmdidx != CMD_let || got_eq
3371 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3372 }
3373 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003376 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3377 xp->xp_pattern += 2;
3378
3379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381 else if (c == '$')
3382 {
3383 /* environment variable */
3384 xp->xp_context = EXPAND_ENV_VARS;
3385 }
3386 else if (c == '=')
3387 {
3388 got_eq = TRUE;
3389 xp->xp_context = EXPAND_EXPRESSION;
3390 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003391 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 && xp->xp_context == EXPAND_FUNCTIONS
3393 && vim_strchr(xp->xp_pattern, '(') == NULL)
3394 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003395 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 break;
3397 }
3398 else if (cmdidx != CMD_let || got_eq)
3399 {
3400 if (c == '"') /* string */
3401 {
3402 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3403 if (c == '\\' && xp->xp_pattern[1] != NUL)
3404 ++xp->xp_pattern;
3405 xp->xp_context = EXPAND_NOTHING;
3406 }
3407 else if (c == '\'') /* literal string */
3408 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003409 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3411 /* skip */ ;
3412 xp->xp_context = EXPAND_NOTHING;
3413 }
3414 else if (c == '|')
3415 {
3416 if (xp->xp_pattern[1] == '|')
3417 {
3418 ++xp->xp_pattern;
3419 xp->xp_context = EXPAND_EXPRESSION;
3420 }
3421 else
3422 xp->xp_context = EXPAND_COMMANDS;
3423 }
3424 else
3425 xp->xp_context = EXPAND_EXPRESSION;
3426 }
3427 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003428 /* Doesn't look like something valid, expand as an expression
3429 * anyway. */
3430 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 arg = xp->xp_pattern;
3432 if (*arg != NUL)
3433 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3434 /* skip */ ;
3435 }
3436 xp->xp_pattern = arg;
3437}
3438
3439#endif /* FEAT_CMDL_COMPL */
3440
3441/*
3442 * ":1,25call func(arg1, arg2)" function call.
3443 */
3444 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003445ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 char_u *arg = eap->arg;
3448 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003450 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003452 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 linenr_T lnum;
3454 int doesrange;
3455 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003458 if (eap->skip)
3459 {
3460 /* trans_function_name() doesn't work well when skipping, use eval0()
3461 * instead to skip to any following command, e.g. for:
3462 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003463 ++emsg_skip;
3464 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3465 clear_tv(&rettv);
3466 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003467 return;
3468 }
3469
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003470 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003471 if (fudi.fd_newkey != NULL)
3472 {
3473 /* Still need to give an error message for missing key. */
3474 EMSG2(_(e_dictkey), fudi.fd_newkey);
3475 vim_free(fudi.fd_newkey);
3476 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003477 if (tofree == NULL)
3478 return;
3479
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003480 /* Increase refcount on dictionary, it could get deleted when evaluating
3481 * the arguments. */
3482 if (fudi.fd_dict != NULL)
3483 ++fudi.fd_dict->dv_refcount;
3484
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003485 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003486 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003487 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaar532c7802005-01-27 14:44:31 +00003489 /* Skip white space to allow ":call func ()". Not good, but required for
3490 * backward compatibility. */
3491 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 if (*startarg != '(')
3495 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003496 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 goto end;
3498 }
3499
3500 /*
3501 * When skipping, evaluate the function once, to find the end of the
3502 * arguments.
3503 * When the function takes a range, this is discovered after the first
3504 * call, and the loop is broken.
3505 */
3506 if (eap->skip)
3507 {
3508 ++emsg_skip;
3509 lnum = eap->line2; /* do it once, also with an invalid range */
3510 }
3511 else
3512 lnum = eap->line1;
3513 for ( ; lnum <= eap->line2; ++lnum)
3514 {
3515 if (!eap->skip && eap->addr_count > 0)
3516 {
3517 curwin->w_cursor.lnum = lnum;
3518 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003519#ifdef FEAT_VIRTUALEDIT
3520 curwin->w_cursor.coladd = 0;
3521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003524 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003525 eap->line1, eap->line2, &doesrange,
3526 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 failed = TRUE;
3529 break;
3530 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003531
3532 /* Handle a function returning a Funcref, Dictionary or List. */
3533 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3534 {
3535 failed = TRUE;
3536 break;
3537 }
3538
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (doesrange || eap->skip)
3541 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003545 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003546 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (aborting())
3548 break;
3549 }
3550 if (eap->skip)
3551 --emsg_skip;
3552
3553 if (!failed)
3554 {
3555 /* Check for trailing illegal characters and a following command. */
3556 if (!ends_excmd(*arg))
3557 {
3558 emsg_severe = TRUE;
3559 EMSG(_(e_trailing));
3560 }
3561 else
3562 eap->nextcmd = check_nextcmd(arg);
3563 }
3564
3565end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003566 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003567 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568}
3569
3570/*
3571 * ":unlet[!] var1 ... " command.
3572 */
3573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003574ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003576 ex_unletlock(eap, eap->arg, 0);
3577}
3578
3579/*
3580 * ":lockvar" and ":unlockvar" commands
3581 */
3582 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003583ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003586 int deep = 2;
3587
3588 if (eap->forceit)
3589 deep = -1;
3590 else if (vim_isdigit(*arg))
3591 {
3592 deep = getdigits(&arg);
3593 arg = skipwhite(arg);
3594 }
3595
3596 ex_unletlock(eap, arg, deep);
3597}
3598
3599/*
3600 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3601 */
3602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003603ex_unletlock(
3604 exarg_T *eap,
3605 char_u *argstart,
3606 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607{
3608 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613 do
3614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003616 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003617 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 if (lv.ll_name == NULL)
3619 error = TRUE; /* error but continue parsing */
3620 if (name_end == NULL || (!vim_iswhite(*name_end)
3621 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 if (name_end != NULL)
3624 {
3625 emsg_severe = TRUE;
3626 EMSG(_(e_trailing));
3627 }
3628 if (!(eap->skip || error))
3629 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 break;
3631 }
3632
3633 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634 {
3635 if (eap->cmdidx == CMD_unlet)
3636 {
3637 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3638 error = TRUE;
3639 }
3640 else
3641 {
3642 if (do_lock_var(&lv, name_end, deep,
3643 eap->cmdidx == CMD_lockvar) == FAIL)
3644 error = TRUE;
3645 }
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 if (!eap->skip)
3649 clear_lval(&lv);
3650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 arg = skipwhite(name_end);
3652 } while (!ends_excmd(*arg));
3653
3654 eap->nextcmd = check_nextcmd(arg);
3655}
3656
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003658do_unlet_var(
3659 lval_T *lp,
3660 char_u *name_end,
3661 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 int ret = OK;
3664 int cc;
3665
3666 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003668 cc = *name_end;
3669 *name_end = NUL;
3670
3671 /* Normal name or expanded name. */
3672 if (check_changedtick(lp->ll_name))
3673 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003677 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003678 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003679 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003680 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003681 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003682 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 else if (lp->ll_range)
3684 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003685 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003686 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003687 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003688
3689 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3690 {
3691 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003692 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003693 return FAIL;
3694 ll_li = li;
3695 ++ll_n1;
3696 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697
3698 /* Delete a range of List items. */
3699 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3700 {
3701 li = lp->ll_li->li_next;
3702 listitem_remove(lp->ll_list, lp->ll_li);
3703 lp->ll_li = li;
3704 ++lp->ll_n1;
3705 }
3706 }
3707 else
3708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 /* unlet a List item. */
3711 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003714 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 }
3716
3717 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003718}
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720/*
3721 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003722 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 */
3724 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003725do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
Bram Moolenaar33570922005-01-25 22:26:29 +00003727 hashtab_T *ht;
3728 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003729 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003730 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003731 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 ht = find_var_ht(name, &varname);
3734 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003736 if (ht == &globvarht)
3737 d = &globvardict;
3738 else if (current_funccal != NULL
3739 && ht == &current_funccal->l_vars.dv_hashtab)
3740 d = &current_funccal->l_vars;
3741 else if (ht == &compat_hashtab)
3742 d = &vimvardict;
3743 else
3744 {
3745 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3746 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3747 }
3748 if (d == NULL)
3749 {
3750 EMSG2(_(e_intern2), "do_unlet()");
3751 return FAIL;
3752 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003753 hi = hash_find(ht, varname);
3754 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003755 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003756 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003757 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003758 || var_check_ro(di->di_flags, name, FALSE)
3759 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003760 return FAIL;
3761
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003762 delete_var(ht, hi);
3763 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003766 if (forceit)
3767 return OK;
3768 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 return FAIL;
3770}
3771
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003772/*
3773 * Lock or unlock variable indicated by "lp".
3774 * "deep" is the levels to go (-1 for unlimited);
3775 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3776 */
3777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003778do_lock_var(
3779 lval_T *lp,
3780 char_u *name_end,
3781 int deep,
3782 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003783{
3784 int ret = OK;
3785 int cc;
3786 dictitem_T *di;
3787
3788 if (deep == 0) /* nothing to do */
3789 return OK;
3790
3791 if (lp->ll_tv == NULL)
3792 {
3793 cc = *name_end;
3794 *name_end = NUL;
3795
3796 /* Normal name or expanded name. */
3797 if (check_changedtick(lp->ll_name))
3798 ret = FAIL;
3799 else
3800 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003801 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003802 if (di == NULL)
3803 ret = FAIL;
3804 else
3805 {
3806 if (lock)
3807 di->di_flags |= DI_FLAGS_LOCK;
3808 else
3809 di->di_flags &= ~DI_FLAGS_LOCK;
3810 item_lock(&di->di_tv, deep, lock);
3811 }
3812 }
3813 *name_end = cc;
3814 }
3815 else if (lp->ll_range)
3816 {
3817 listitem_T *li = lp->ll_li;
3818
3819 /* (un)lock a range of List items. */
3820 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3821 {
3822 item_lock(&li->li_tv, deep, lock);
3823 li = li->li_next;
3824 ++lp->ll_n1;
3825 }
3826 }
3827 else if (lp->ll_list != NULL)
3828 /* (un)lock a List item. */
3829 item_lock(&lp->ll_li->li_tv, deep, lock);
3830 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003831 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003832 item_lock(&lp->ll_di->di_tv, deep, lock);
3833
3834 return ret;
3835}
3836
3837/*
3838 * Lock or unlock an item. "deep" is nr of levels to go.
3839 */
3840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003841item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003842{
3843 static int recurse = 0;
3844 list_T *l;
3845 listitem_T *li;
3846 dict_T *d;
3847 hashitem_T *hi;
3848 int todo;
3849
3850 if (recurse >= DICT_MAXNEST)
3851 {
3852 EMSG(_("E743: variable nested too deep for (un)lock"));
3853 return;
3854 }
3855 if (deep == 0)
3856 return;
3857 ++recurse;
3858
3859 /* lock/unlock the item itself */
3860 if (lock)
3861 tv->v_lock |= VAR_LOCKED;
3862 else
3863 tv->v_lock &= ~VAR_LOCKED;
3864
3865 switch (tv->v_type)
3866 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003867 case VAR_UNKNOWN:
3868 case VAR_NUMBER:
3869 case VAR_STRING:
3870 case VAR_FUNC:
3871 case VAR_FLOAT:
3872 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003873 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003874 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003875 break;
3876
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003877 case VAR_LIST:
3878 if ((l = tv->vval.v_list) != NULL)
3879 {
3880 if (lock)
3881 l->lv_lock |= VAR_LOCKED;
3882 else
3883 l->lv_lock &= ~VAR_LOCKED;
3884 if (deep < 0 || deep > 1)
3885 /* recursive: lock/unlock the items the List contains */
3886 for (li = l->lv_first; li != NULL; li = li->li_next)
3887 item_lock(&li->li_tv, deep - 1, lock);
3888 }
3889 break;
3890 case VAR_DICT:
3891 if ((d = tv->vval.v_dict) != NULL)
3892 {
3893 if (lock)
3894 d->dv_lock |= VAR_LOCKED;
3895 else
3896 d->dv_lock &= ~VAR_LOCKED;
3897 if (deep < 0 || deep > 1)
3898 {
3899 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003900 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003901 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3902 {
3903 if (!HASHITEM_EMPTY(hi))
3904 {
3905 --todo;
3906 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3907 }
3908 }
3909 }
3910 }
3911 }
3912 --recurse;
3913}
3914
Bram Moolenaara40058a2005-07-11 22:42:07 +00003915/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003916 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3917 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003918 */
3919 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003920tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003921{
3922 return (tv->v_lock & VAR_LOCKED)
3923 || (tv->v_type == VAR_LIST
3924 && tv->vval.v_list != NULL
3925 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3926 || (tv->v_type == VAR_DICT
3927 && tv->vval.v_dict != NULL
3928 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3929}
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3932/*
3933 * Delete all "menutrans_" variables.
3934 */
3935 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003936del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
Bram Moolenaar33570922005-01-25 22:26:29 +00003938 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003942 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 {
3945 if (!HASHITEM_EMPTY(hi))
3946 {
3947 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3949 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 }
3951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953}
3954#endif
3955
3956#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3957
3958/*
3959 * Local string buffer for the next two functions to store a variable name
3960 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3961 * get_user_var_name().
3962 */
3963
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003964static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965
3966static char_u *varnamebuf = NULL;
3967static int varnamebuflen = 0;
3968
3969/*
3970 * Function to concatenate a prefix and a variable name.
3971 */
3972 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003973cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
3975 int len;
3976
3977 len = (int)STRLEN(name) + 3;
3978 if (len > varnamebuflen)
3979 {
3980 vim_free(varnamebuf);
3981 len += 10; /* some additional space */
3982 varnamebuf = alloc(len);
3983 if (varnamebuf == NULL)
3984 {
3985 varnamebuflen = 0;
3986 return NULL;
3987 }
3988 varnamebuflen = len;
3989 }
3990 *varnamebuf = prefix;
3991 varnamebuf[1] = ':';
3992 STRCPY(varnamebuf + 2, name);
3993 return varnamebuf;
3994}
3995
3996/*
3997 * Function given to ExpandGeneric() to obtain the list of user defined
3998 * (global/buffer/window/built-in) variable names.
3999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004003 static long_u gdone;
4004 static long_u bdone;
4005 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004006#ifdef FEAT_WINDOWS
4007 static long_u tdone;
4008#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004009 static int vidx;
4010 static hashitem_T *hi;
4011 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
4013 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004014 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004015 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004016#ifdef FEAT_WINDOWS
4017 tdone = 0;
4018#endif
4019 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004020
4021 /* Global variables */
4022 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004026 else
4027 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004028 while (HASHITEM_EMPTY(hi))
4029 ++hi;
4030 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4031 return cat_prefix_varname('g', hi->hi_key);
4032 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004034
4035 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004036 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004037 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004039 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004041 else
4042 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 while (HASHITEM_EMPTY(hi))
4044 ++hi;
4045 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004049 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 return (char_u *)"b:changedtick";
4051 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004052
4053 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004054 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004057 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004059 else
4060 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004061 while (HASHITEM_EMPTY(hi))
4062 ++hi;
4063 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004065
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004066#ifdef FEAT_WINDOWS
4067 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004068 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069 if (tdone < ht->ht_used)
4070 {
4071 if (tdone++ == 0)
4072 hi = ht->ht_array;
4073 else
4074 ++hi;
4075 while (HASHITEM_EMPTY(hi))
4076 ++hi;
4077 return cat_prefix_varname('t', hi->hi_key);
4078 }
4079#endif
4080
Bram Moolenaar33570922005-01-25 22:26:29 +00004081 /* v: variables */
4082 if (vidx < VV_LEN)
4083 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 vim_free(varnamebuf);
4086 varnamebuf = NULL;
4087 varnamebuflen = 0;
4088 return NULL;
4089}
4090
4091#endif /* FEAT_CMDL_COMPL */
4092
4093/*
4094 * types for expressions.
4095 */
4096typedef enum
4097{
4098 TYPE_UNKNOWN = 0
4099 , TYPE_EQUAL /* == */
4100 , TYPE_NEQUAL /* != */
4101 , TYPE_GREATER /* > */
4102 , TYPE_GEQUAL /* >= */
4103 , TYPE_SMALLER /* < */
4104 , TYPE_SEQUAL /* <= */
4105 , TYPE_MATCH /* =~ */
4106 , TYPE_NOMATCH /* !~ */
4107} exptype_T;
4108
4109/*
4110 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4113 */
4114
4115/*
4116 * Handle zero level expression.
4117 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004119 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 * Return OK or FAIL.
4121 */
4122 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004123eval0(
4124 char_u *arg,
4125 typval_T *rettv,
4126 char_u **nextcmd,
4127 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
4129 int ret;
4130 char_u *p;
4131
4132 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 if (ret == FAIL || !ends_excmd(*p))
4135 {
4136 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 /*
4139 * Report the invalid expression unless the expression evaluation has
4140 * been cancelled due to an aborting error, an interrupt, or an
4141 * exception.
4142 */
4143 if (!aborting())
4144 EMSG2(_(e_invexpr2), arg);
4145 ret = FAIL;
4146 }
4147 if (nextcmd != NULL)
4148 *nextcmd = check_nextcmd(p);
4149
4150 return ret;
4151}
4152
4153/*
4154 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004155 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 *
4157 * "arg" must point to the first non-white of the expression.
4158 * "arg" is advanced to the next non-white after the recognized expression.
4159 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004160 * Note: "rettv.v_lock" is not set.
4161 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 * Return OK or FAIL.
4163 */
4164 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004165eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
4167 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004168 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
4170 /*
4171 * Get the first variable.
4172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return FAIL;
4175
4176 if ((*arg)[0] == '?')
4177 {
4178 result = FALSE;
4179 if (evaluate)
4180 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004181 int error = FALSE;
4182
4183 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 if (error)
4187 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189
4190 /*
4191 * Get the second variable.
4192 */
4193 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 return FAIL;
4196
4197 /*
4198 * Check for the ":".
4199 */
4200 if ((*arg)[0] != ':')
4201 {
4202 EMSG(_("E109: Missing ':' after '?'"));
4203 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 return FAIL;
4206 }
4207
4208 /*
4209 * Get the third variable.
4210 */
4211 *arg = skipwhite(*arg + 1);
4212 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4213 {
4214 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return FAIL;
4217 }
4218 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221
4222 return OK;
4223}
4224
4225/*
4226 * Handle first level expression:
4227 * expr2 || expr2 || expr2 logical OR
4228 *
4229 * "arg" must point to the first non-white of the expression.
4230 * "arg" is advanced to the next non-white after the recognized expression.
4231 *
4232 * Return OK or FAIL.
4233 */
4234 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004235eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
Bram Moolenaar33570922005-01-25 22:26:29 +00004237 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 long result;
4239 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004240 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 /*
4243 * Get the first variable.
4244 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 return FAIL;
4247
4248 /*
4249 * Repeat until there is no following "||".
4250 */
4251 first = TRUE;
4252 result = FALSE;
4253 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4254 {
4255 if (evaluate && first)
4256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004257 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 if (error)
4261 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 first = FALSE;
4263 }
4264
4265 /*
4266 * Get the second variable.
4267 */
4268 *arg = skipwhite(*arg + 2);
4269 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4270 return FAIL;
4271
4272 /*
4273 * Compute the result.
4274 */
4275 if (evaluate && !result)
4276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004277 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (error)
4281 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283 if (evaluate)
4284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004285 rettv->v_type = VAR_NUMBER;
4286 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 }
4289
4290 return OK;
4291}
4292
4293/*
4294 * Handle second level expression:
4295 * expr3 && expr3 && expr3 logical AND
4296 *
4297 * "arg" must point to the first non-white of the expression.
4298 * "arg" is advanced to the next non-white after the recognized expression.
4299 *
4300 * Return OK or FAIL.
4301 */
4302 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004303eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304{
Bram Moolenaar33570922005-01-25 22:26:29 +00004305 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 long result;
4307 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004308 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 /*
4311 * Get the first variable.
4312 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 return FAIL;
4315
4316 /*
4317 * Repeat until there is no following "&&".
4318 */
4319 first = TRUE;
4320 result = TRUE;
4321 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4322 {
4323 if (evaluate && first)
4324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004325 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004327 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004328 if (error)
4329 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 first = FALSE;
4331 }
4332
4333 /*
4334 * Get the second variable.
4335 */
4336 *arg = skipwhite(*arg + 2);
4337 if (eval4(arg, &var2, evaluate && result) == FAIL)
4338 return FAIL;
4339
4340 /*
4341 * Compute the result.
4342 */
4343 if (evaluate && result)
4344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004345 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004347 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 if (error)
4349 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
4351 if (evaluate)
4352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004353 rettv->v_type = VAR_NUMBER;
4354 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356 }
4357
4358 return OK;
4359}
4360
4361/*
4362 * Handle third level expression:
4363 * var1 == var2
4364 * var1 =~ var2
4365 * var1 != var2
4366 * var1 !~ var2
4367 * var1 > var2
4368 * var1 >= var2
4369 * var1 < var2
4370 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004371 * var1 is var2
4372 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 *
4374 * "arg" must point to the first non-white of the expression.
4375 * "arg" is advanced to the next non-white after the recognized expression.
4376 *
4377 * Return OK or FAIL.
4378 */
4379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004380eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
Bram Moolenaar33570922005-01-25 22:26:29 +00004382 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 char_u *p;
4384 int i;
4385 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004386 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 int len = 2;
4388 long n1, n2;
4389 char_u *s1, *s2;
4390 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4391 regmatch_T regmatch;
4392 int ic;
4393 char_u *save_cpo;
4394
4395 /*
4396 * Get the first variable.
4397 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400
4401 p = *arg;
4402 switch (p[0])
4403 {
4404 case '=': if (p[1] == '=')
4405 type = TYPE_EQUAL;
4406 else if (p[1] == '~')
4407 type = TYPE_MATCH;
4408 break;
4409 case '!': if (p[1] == '=')
4410 type = TYPE_NEQUAL;
4411 else if (p[1] == '~')
4412 type = TYPE_NOMATCH;
4413 break;
4414 case '>': if (p[1] != '=')
4415 {
4416 type = TYPE_GREATER;
4417 len = 1;
4418 }
4419 else
4420 type = TYPE_GEQUAL;
4421 break;
4422 case '<': if (p[1] != '=')
4423 {
4424 type = TYPE_SMALLER;
4425 len = 1;
4426 }
4427 else
4428 type = TYPE_SEQUAL;
4429 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 case 'i': if (p[1] == 's')
4431 {
4432 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4433 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004434 i = p[len];
4435 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004436 {
4437 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4438 type_is = TRUE;
4439 }
4440 }
4441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443
4444 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004445 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 */
4447 if (type != TYPE_UNKNOWN)
4448 {
4449 /* extra question mark appended: ignore case */
4450 if (p[len] == '?')
4451 {
4452 ic = TRUE;
4453 ++len;
4454 }
4455 /* extra '#' appended: match case */
4456 else if (p[len] == '#')
4457 {
4458 ic = FALSE;
4459 ++len;
4460 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004461 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 else
4463 ic = p_ic;
4464
4465 /*
4466 * Get the second variable.
4467 */
4468 *arg = skipwhite(p + len);
4469 if (eval5(arg, &var2, evaluate) == FAIL)
4470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004471 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 return FAIL;
4473 }
4474
4475 if (evaluate)
4476 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004477 if (type_is && rettv->v_type != var2.v_type)
4478 {
4479 /* For "is" a different type always means FALSE, for "notis"
4480 * it means TRUE. */
4481 n1 = (type == TYPE_NEQUAL);
4482 }
4483 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4484 {
4485 if (type_is)
4486 {
4487 n1 = (rettv->v_type == var2.v_type
4488 && rettv->vval.v_list == var2.vval.v_list);
4489 if (type == TYPE_NEQUAL)
4490 n1 = !n1;
4491 }
4492 else if (rettv->v_type != var2.v_type
4493 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4494 {
4495 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004496 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004497 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004498 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004499 clear_tv(rettv);
4500 clear_tv(&var2);
4501 return FAIL;
4502 }
4503 else
4504 {
4505 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004506 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4507 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004508 if (type == TYPE_NEQUAL)
4509 n1 = !n1;
4510 }
4511 }
4512
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004513 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4514 {
4515 if (type_is)
4516 {
4517 n1 = (rettv->v_type == var2.v_type
4518 && rettv->vval.v_dict == var2.vval.v_dict);
4519 if (type == TYPE_NEQUAL)
4520 n1 = !n1;
4521 }
4522 else if (rettv->v_type != var2.v_type
4523 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4524 {
4525 if (rettv->v_type != var2.v_type)
4526 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4527 else
4528 EMSG(_("E736: Invalid operation for Dictionary"));
4529 clear_tv(rettv);
4530 clear_tv(&var2);
4531 return FAIL;
4532 }
4533 else
4534 {
4535 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004536 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4537 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004538 if (type == TYPE_NEQUAL)
4539 n1 = !n1;
4540 }
4541 }
4542
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004543 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4544 {
4545 if (rettv->v_type != var2.v_type
4546 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4547 {
4548 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004549 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004551 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004552 clear_tv(rettv);
4553 clear_tv(&var2);
4554 return FAIL;
4555 }
4556 else
4557 {
4558 /* Compare two Funcrefs for being equal or unequal. */
4559 if (rettv->vval.v_string == NULL
4560 || var2.vval.v_string == NULL)
4561 n1 = FALSE;
4562 else
4563 n1 = STRCMP(rettv->vval.v_string,
4564 var2.vval.v_string) == 0;
4565 if (type == TYPE_NEQUAL)
4566 n1 = !n1;
4567 }
4568 }
4569
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004570#ifdef FEAT_FLOAT
4571 /*
4572 * If one of the two variables is a float, compare as a float.
4573 * When using "=~" or "!~", always compare as string.
4574 */
4575 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4576 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4577 {
4578 float_T f1, f2;
4579
4580 if (rettv->v_type == VAR_FLOAT)
4581 f1 = rettv->vval.v_float;
4582 else
4583 f1 = get_tv_number(rettv);
4584 if (var2.v_type == VAR_FLOAT)
4585 f2 = var2.vval.v_float;
4586 else
4587 f2 = get_tv_number(&var2);
4588 n1 = FALSE;
4589 switch (type)
4590 {
4591 case TYPE_EQUAL: n1 = (f1 == f2); break;
4592 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4593 case TYPE_GREATER: n1 = (f1 > f2); break;
4594 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4595 case TYPE_SMALLER: n1 = (f1 < f2); break;
4596 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4597 case TYPE_UNKNOWN:
4598 case TYPE_MATCH:
4599 case TYPE_NOMATCH: break; /* avoid gcc warning */
4600 }
4601 }
4602#endif
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 /*
4605 * If one of the two variables is a number, compare as a number.
4606 * When using "=~" or "!~", always compare as string.
4607 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004608 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004611 n1 = get_tv_number(rettv);
4612 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 switch (type)
4614 {
4615 case TYPE_EQUAL: n1 = (n1 == n2); break;
4616 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4617 case TYPE_GREATER: n1 = (n1 > n2); break;
4618 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4619 case TYPE_SMALLER: n1 = (n1 < n2); break;
4620 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4621 case TYPE_UNKNOWN:
4622 case TYPE_MATCH:
4623 case TYPE_NOMATCH: break; /* avoid gcc warning */
4624 }
4625 }
4626 else
4627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 s1 = get_tv_string_buf(rettv, buf1);
4629 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4631 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4632 else
4633 i = 0;
4634 n1 = FALSE;
4635 switch (type)
4636 {
4637 case TYPE_EQUAL: n1 = (i == 0); break;
4638 case TYPE_NEQUAL: n1 = (i != 0); break;
4639 case TYPE_GREATER: n1 = (i > 0); break;
4640 case TYPE_GEQUAL: n1 = (i >= 0); break;
4641 case TYPE_SMALLER: n1 = (i < 0); break;
4642 case TYPE_SEQUAL: n1 = (i <= 0); break;
4643
4644 case TYPE_MATCH:
4645 case TYPE_NOMATCH:
4646 /* avoid 'l' flag in 'cpoptions' */
4647 save_cpo = p_cpo;
4648 p_cpo = (char_u *)"";
4649 regmatch.regprog = vim_regcomp(s2,
4650 RE_MAGIC + RE_STRING);
4651 regmatch.rm_ic = ic;
4652 if (regmatch.regprog != NULL)
4653 {
4654 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004655 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (type == TYPE_NOMATCH)
4657 n1 = !n1;
4658 }
4659 p_cpo = save_cpo;
4660 break;
4661
4662 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4663 }
4664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004665 clear_tv(rettv);
4666 clear_tv(&var2);
4667 rettv->v_type = VAR_NUMBER;
4668 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
4670 }
4671
4672 return OK;
4673}
4674
4675/*
4676 * Handle fourth level expression:
4677 * + number addition
4678 * - number subtraction
4679 * . string concatenation
4680 *
4681 * "arg" must point to the first non-white of the expression.
4682 * "arg" is advanced to the next non-white after the recognized expression.
4683 *
4684 * Return OK or FAIL.
4685 */
4686 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004687eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
Bram Moolenaar33570922005-01-25 22:26:29 +00004689 typval_T var2;
4690 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 int op;
4692 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004693#ifdef FEAT_FLOAT
4694 float_T f1 = 0, f2 = 0;
4695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 char_u *s1, *s2;
4697 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4698 char_u *p;
4699
4700 /*
4701 * Get the first variable.
4702 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004703 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 return FAIL;
4705
4706 /*
4707 * Repeat computing, until no '+', '-' or '.' is following.
4708 */
4709 for (;;)
4710 {
4711 op = **arg;
4712 if (op != '+' && op != '-' && op != '.')
4713 break;
4714
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 if ((op != '+' || rettv->v_type != VAR_LIST)
4716#ifdef FEAT_FLOAT
4717 && (op == '.' || rettv->v_type != VAR_FLOAT)
4718#endif
4719 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 {
4721 /* For "list + ...", an illegal use of the first operand as
4722 * a number cannot be determined before evaluating the 2nd
4723 * operand: if this is also a list, all is ok.
4724 * For "something . ...", "something - ..." or "non-list + ...",
4725 * we know that the first operand needs to be a string or number
4726 * without evaluating the 2nd operand. So check before to avoid
4727 * side effects after an error. */
4728 if (evaluate && get_tv_string_chk(rettv) == NULL)
4729 {
4730 clear_tv(rettv);
4731 return FAIL;
4732 }
4733 }
4734
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 /*
4736 * Get the second variable.
4737 */
4738 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004739 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004741 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 return FAIL;
4743 }
4744
4745 if (evaluate)
4746 {
4747 /*
4748 * Compute the result.
4749 */
4750 if (op == '.')
4751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004752 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4753 s2 = get_tv_string_buf_chk(&var2, buf2);
4754 if (s2 == NULL) /* type error ? */
4755 {
4756 clear_tv(rettv);
4757 clear_tv(&var2);
4758 return FAIL;
4759 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004760 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 clear_tv(rettv);
4762 rettv->v_type = VAR_STRING;
4763 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004765 else if (op == '+' && rettv->v_type == VAR_LIST
4766 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004767 {
4768 /* concatenate Lists */
4769 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4770 &var3) == FAIL)
4771 {
4772 clear_tv(rettv);
4773 clear_tv(&var2);
4774 return FAIL;
4775 }
4776 clear_tv(rettv);
4777 *rettv = var3;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else
4780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 int error = FALSE;
4782
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783#ifdef FEAT_FLOAT
4784 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 f1 = rettv->vval.v_float;
4787 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 else
4790#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 n1 = get_tv_number_chk(rettv, &error);
4793 if (error)
4794 {
4795 /* This can only happen for "list + non-list". For
4796 * "non-list + ..." or "something - ...", we returned
4797 * before evaluating the 2nd operand. */
4798 clear_tv(rettv);
4799 return FAIL;
4800 }
4801#ifdef FEAT_FLOAT
4802 if (var2.v_type == VAR_FLOAT)
4803 f1 = n1;
4804#endif
4805 }
4806#ifdef FEAT_FLOAT
4807 if (var2.v_type == VAR_FLOAT)
4808 {
4809 f2 = var2.vval.v_float;
4810 n2 = 0;
4811 }
4812 else
4813#endif
4814 {
4815 n2 = get_tv_number_chk(&var2, &error);
4816 if (error)
4817 {
4818 clear_tv(rettv);
4819 clear_tv(&var2);
4820 return FAIL;
4821 }
4822#ifdef FEAT_FLOAT
4823 if (rettv->v_type == VAR_FLOAT)
4824 f2 = n2;
4825#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828
4829#ifdef FEAT_FLOAT
4830 /* If there is a float on either side the result is a float. */
4831 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4832 {
4833 if (op == '+')
4834 f1 = f1 + f2;
4835 else
4836 f1 = f1 - f2;
4837 rettv->v_type = VAR_FLOAT;
4838 rettv->vval.v_float = f1;
4839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841#endif
4842 {
4843 if (op == '+')
4844 n1 = n1 + n2;
4845 else
4846 n1 = n1 - n2;
4847 rettv->v_type = VAR_NUMBER;
4848 rettv->vval.v_number = n1;
4849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 }
4854 return OK;
4855}
4856
4857/*
4858 * Handle fifth level expression:
4859 * * number multiplication
4860 * / number division
4861 * % number modulo
4862 *
4863 * "arg" must point to the first non-white of the expression.
4864 * "arg" is advanced to the next non-white after the recognized expression.
4865 *
4866 * Return OK or FAIL.
4867 */
4868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004869eval6(
4870 char_u **arg,
4871 typval_T *rettv,
4872 int evaluate,
4873 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874{
Bram Moolenaar33570922005-01-25 22:26:29 +00004875 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 int op;
4877 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004878#ifdef FEAT_FLOAT
4879 int use_float = FALSE;
4880 float_T f1 = 0, f2;
4881#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004882 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 /*
4885 * Get the first variable.
4886 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004887 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 return FAIL;
4889
4890 /*
4891 * Repeat computing, until no '*', '/' or '%' is following.
4892 */
4893 for (;;)
4894 {
4895 op = **arg;
4896 if (op != '*' && op != '/' && op != '%')
4897 break;
4898
4899 if (evaluate)
4900 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901#ifdef FEAT_FLOAT
4902 if (rettv->v_type == VAR_FLOAT)
4903 {
4904 f1 = rettv->vval.v_float;
4905 use_float = TRUE;
4906 n1 = 0;
4907 }
4908 else
4909#endif
4910 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004912 if (error)
4913 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 else
4916 n1 = 0;
4917
4918 /*
4919 * Get the second variable.
4920 */
4921 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004922 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 return FAIL;
4924
4925 if (evaluate)
4926 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927#ifdef FEAT_FLOAT
4928 if (var2.v_type == VAR_FLOAT)
4929 {
4930 if (!use_float)
4931 {
4932 f1 = n1;
4933 use_float = TRUE;
4934 }
4935 f2 = var2.vval.v_float;
4936 n2 = 0;
4937 }
4938 else
4939#endif
4940 {
4941 n2 = get_tv_number_chk(&var2, &error);
4942 clear_tv(&var2);
4943 if (error)
4944 return FAIL;
4945#ifdef FEAT_FLOAT
4946 if (use_float)
4947 f2 = n2;
4948#endif
4949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
4951 /*
4952 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955#ifdef FEAT_FLOAT
4956 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958 if (op == '*')
4959 f1 = f1 * f2;
4960 else if (op == '/')
4961 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004962# ifdef VMS
4963 /* VMS crashes on divide by zero, work around it */
4964 if (f2 == 0.0)
4965 {
4966 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004967 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004969 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004971 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004972 }
4973 else
4974 f1 = f1 / f2;
4975# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 /* We rely on the floating point library to handle divide
4977 * by zero to result in "inf" and not a crash. */
4978 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004979# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004983 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 return FAIL;
4985 }
4986 rettv->v_type = VAR_FLOAT;
4987 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 if (op == '*')
4993 n1 = n1 * n2;
4994 else if (op == '/')
4995 {
4996 if (n2 == 0) /* give an error message? */
4997 {
4998 if (n1 == 0)
4999 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5000 else if (n1 < 0)
5001 n1 = -0x7fffffffL;
5002 else
5003 n1 = 0x7fffffffL;
5004 }
5005 else
5006 n1 = n1 / n2;
5007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 {
5010 if (n2 == 0) /* give an error message? */
5011 n1 = 0;
5012 else
5013 n1 = n1 % n2;
5014 }
5015 rettv->v_type = VAR_NUMBER;
5016 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 }
5020
5021 return OK;
5022}
5023
5024/*
5025 * Handle sixth level expression:
5026 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005027 * "string" string constant
5028 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 * &option-name option value
5030 * @r register contents
5031 * identifier variable value
5032 * function() function call
5033 * $VAR environment variable
5034 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005035 * [expr, expr] List
5036 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 *
5038 * Also handle:
5039 * ! in front logical NOT
5040 * - in front unary minus
5041 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 * trailing [] subscript in String or List
5043 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 *
5045 * "arg" must point to the first non-white of the expression.
5046 * "arg" is advanced to the next non-white after the recognized expression.
5047 *
5048 * Return OK or FAIL.
5049 */
5050 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005051eval7(
5052 char_u **arg,
5053 typval_T *rettv,
5054 int evaluate,
5055 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 long n;
5058 int len;
5059 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 char_u *start_leader, *end_leader;
5061 int ret = OK;
5062 char_u *alias;
5063
5064 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005066 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 /*
5071 * Skip '!' and '-' characters. They are handled later.
5072 */
5073 start_leader = *arg;
5074 while (**arg == '!' || **arg == '-' || **arg == '+')
5075 *arg = skipwhite(*arg + 1);
5076 end_leader = *arg;
5077
5078 switch (**arg)
5079 {
5080 /*
5081 * Number constant.
5082 */
5083 case '0':
5084 case '1':
5085 case '2':
5086 case '3':
5087 case '4':
5088 case '5':
5089 case '6':
5090 case '7':
5091 case '8':
5092 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 {
5094#ifdef FEAT_FLOAT
5095 char_u *p = skipdigits(*arg + 1);
5096 int get_float = FALSE;
5097
5098 /* We accept a float when the format matches
5099 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005100 * strict to avoid backwards compatibility problems.
5101 * Don't look for a float after the "." operator, so that
5102 * ":let vers = 1.2.3" doesn't fail. */
5103 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 get_float = TRUE;
5106 p = skipdigits(p + 2);
5107 if (*p == 'e' || *p == 'E')
5108 {
5109 ++p;
5110 if (*p == '-' || *p == '+')
5111 ++p;
5112 if (!vim_isdigit(*p))
5113 get_float = FALSE;
5114 else
5115 p = skipdigits(p + 1);
5116 }
5117 if (ASCII_ISALPHA(*p) || *p == '.')
5118 get_float = FALSE;
5119 }
5120 if (get_float)
5121 {
5122 float_T f;
5123
5124 *arg += string2float(*arg, &f);
5125 if (evaluate)
5126 {
5127 rettv->v_type = VAR_FLOAT;
5128 rettv->vval.v_float = f;
5129 }
5130 }
5131 else
5132#endif
5133 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005134 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005135 *arg += len;
5136 if (evaluate)
5137 {
5138 rettv->v_type = VAR_NUMBER;
5139 rettv->vval.v_number = n;
5140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 /*
5146 * String constant: "string".
5147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005148 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 break;
5150
5151 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005155 break;
5156
5157 /*
5158 * List: [expr, expr]
5159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 break;
5162
5163 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 * Dictionary: {key: val, key: val}
5165 */
5166 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5167 break;
5168
5169 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005170 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005172 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174
5175 /*
5176 * Environment variable: $VAR.
5177 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 break;
5180
5181 /*
5182 * Register contents: @r.
5183 */
5184 case '@': ++*arg;
5185 if (evaluate)
5186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005188 rettv->vval.v_string = get_reg_contents(**arg,
5189 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 if (**arg != NUL)
5192 ++*arg;
5193 break;
5194
5195 /*
5196 * nested expression: (expression).
5197 */
5198 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if (**arg == ')')
5201 ++*arg;
5202 else if (ret == OK)
5203 {
5204 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005205 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 ret = FAIL;
5207 }
5208 break;
5209
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 break;
5212 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213
5214 if (ret == NOTDONE)
5215 {
5216 /*
5217 * Must be a variable or function name.
5218 * Can also be a curly-braces kind of name: {expr}.
5219 */
5220 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005221 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 if (alias != NULL)
5223 s = alias;
5224
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005225 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 ret = FAIL;
5227 else
5228 {
5229 if (**arg == '(') /* recursive! */
5230 {
5231 /* If "s" is the name of a variable of type VAR_FUNC
5232 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005233 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234
5235 /* Invoke the function. */
5236 ret = get_func_tv(s, len, rettv, arg,
5237 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005238 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005239
5240 /* If evaluate is FALSE rettv->v_type was not set in
5241 * get_func_tv, but it's needed in handle_subscript() to parse
5242 * what follows. So set it here. */
5243 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5244 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005245 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005246 rettv->v_type = VAR_FUNC;
5247 }
5248
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 /* Stop the expression evaluation when immediately
5250 * aborting on error, or when an interrupt occurred or
5251 * an exception was thrown but not caught. */
5252 if (aborting())
5253 {
5254 if (ret == OK)
5255 clear_tv(rettv);
5256 ret = FAIL;
5257 }
5258 }
5259 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005260 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005261 else
5262 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005264 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 }
5266
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 *arg = skipwhite(*arg);
5268
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005269 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5270 * expr(expr). */
5271 if (ret == OK)
5272 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273
5274 /*
5275 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5276 */
5277 if (ret == OK && evaluate && end_leader > start_leader)
5278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005279 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005280 int val = 0;
5281#ifdef FEAT_FLOAT
5282 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005284 if (rettv->v_type == VAR_FLOAT)
5285 f = rettv->vval.v_float;
5286 else
5287#endif
5288 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005289 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 clear_tv(rettv);
5292 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 else
5295 {
5296 while (end_leader > start_leader)
5297 {
5298 --end_leader;
5299 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005300 {
5301#ifdef FEAT_FLOAT
5302 if (rettv->v_type == VAR_FLOAT)
5303 f = !f;
5304 else
5305#endif
5306 val = !val;
5307 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005309 {
5310#ifdef FEAT_FLOAT
5311 if (rettv->v_type == VAR_FLOAT)
5312 f = -f;
5313 else
5314#endif
5315 val = -val;
5316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005317 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005318#ifdef FEAT_FLOAT
5319 if (rettv->v_type == VAR_FLOAT)
5320 {
5321 clear_tv(rettv);
5322 rettv->vval.v_float = f;
5323 }
5324 else
5325#endif
5326 {
5327 clear_tv(rettv);
5328 rettv->v_type = VAR_NUMBER;
5329 rettv->vval.v_number = val;
5330 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333
5334 return ret;
5335}
5336
5337/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005338 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5339 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5341 */
5342 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005343eval_index(
5344 char_u **arg,
5345 typval_T *rettv,
5346 int evaluate,
5347 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348{
5349 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005352 long len = -1;
5353 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356
Bram Moolenaara03f2332016-02-06 18:09:59 +01005357 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005359 case VAR_FUNC:
5360 if (verbose)
5361 EMSG(_("E695: Cannot index a Funcref"));
5362 return FAIL;
5363 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005364#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005365 if (verbose)
5366 EMSG(_(e_float_as_string));
5367 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005368#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005369 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005370 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005371 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005372 if (verbose)
5373 EMSG(_("E909: Cannot index a special variable"));
5374 return FAIL;
5375 case VAR_UNKNOWN:
5376 if (evaluate)
5377 return FAIL;
5378 /* FALLTHROUGH */
5379
5380 case VAR_STRING:
5381 case VAR_NUMBER:
5382 case VAR_LIST:
5383 case VAR_DICT:
5384 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005385 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005387 init_tv(&var1);
5388 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 /*
5392 * dict.name
5393 */
5394 key = *arg + 1;
5395 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5396 ;
5397 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 }
5401 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005403 /*
5404 * something[idx]
5405 *
5406 * Get the (first) variable from inside the [].
5407 */
5408 *arg = skipwhite(*arg + 1);
5409 if (**arg == ':')
5410 empty1 = TRUE;
5411 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5412 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005413 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5414 {
5415 /* not a number or string */
5416 clear_tv(&var1);
5417 return FAIL;
5418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419
5420 /*
5421 * Get the second variable from inside the [:].
5422 */
5423 if (**arg == ':')
5424 {
5425 range = TRUE;
5426 *arg = skipwhite(*arg + 1);
5427 if (**arg == ']')
5428 empty2 = TRUE;
5429 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005431 if (!empty1)
5432 clear_tv(&var1);
5433 return FAIL;
5434 }
5435 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5436 {
5437 /* not a number or string */
5438 if (!empty1)
5439 clear_tv(&var1);
5440 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441 return FAIL;
5442 }
5443 }
5444
5445 /* Check for the ']'. */
5446 if (**arg != ']')
5447 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005448 if (verbose)
5449 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 clear_tv(&var1);
5451 if (range)
5452 clear_tv(&var2);
5453 return FAIL;
5454 }
5455 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 }
5457
5458 if (evaluate)
5459 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460 n1 = 0;
5461 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005463 n1 = get_tv_number(&var1);
5464 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 }
5466 if (range)
5467 {
5468 if (empty2)
5469 n2 = -1;
5470 else
5471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472 n2 = get_tv_number(&var2);
5473 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 }
5475 }
5476
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005479 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005480 case VAR_FUNC:
5481 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005482 case VAR_SPECIAL:
5483 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005484 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005485 break; /* not evaluating, skipping over subscript */
5486
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 case VAR_NUMBER:
5488 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005489 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 len = (long)STRLEN(s);
5491 if (range)
5492 {
5493 /* The resulting variable is a substring. If the indexes
5494 * are out of range the result is empty. */
5495 if (n1 < 0)
5496 {
5497 n1 = len + n1;
5498 if (n1 < 0)
5499 n1 = 0;
5500 }
5501 if (n2 < 0)
5502 n2 = len + n2;
5503 else if (n2 >= len)
5504 n2 = len;
5505 if (n1 >= len || n2 < 0 || n1 > n2)
5506 s = NULL;
5507 else
5508 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5509 }
5510 else
5511 {
5512 /* The resulting variable is a string of a single
5513 * character. If the index is too big or negative the
5514 * result is empty. */
5515 if (n1 >= len || n1 < 0)
5516 s = NULL;
5517 else
5518 s = vim_strnsave(s + n1, 1);
5519 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005520 clear_tv(rettv);
5521 rettv->v_type = VAR_STRING;
5522 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 break;
5524
5525 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005526 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 if (n1 < 0)
5528 n1 = len + n1;
5529 if (!empty1 && (n1 < 0 || n1 >= len))
5530 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005531 /* For a range we allow invalid values and return an empty
5532 * list. A list index out of range is an error. */
5533 if (!range)
5534 {
5535 if (verbose)
5536 EMSGN(_(e_listidx), n1);
5537 return FAIL;
5538 }
5539 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005540 }
5541 if (range)
5542 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005543 list_T *l;
5544 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545
5546 if (n2 < 0)
5547 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005548 else if (n2 >= len)
5549 n2 = len - 1;
5550 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005551 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 l = list_alloc();
5553 if (l == NULL)
5554 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 n1 <= n2; ++n1)
5557 {
5558 if (list_append_tv(l, &item->li_tv) == FAIL)
5559 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005560 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 return FAIL;
5562 }
5563 item = item->li_next;
5564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 clear_tv(rettv);
5566 rettv->v_type = VAR_LIST;
5567 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005568 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005569 }
5570 else
5571 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005572 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 clear_tv(rettv);
5574 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005575 }
5576 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577
5578 case VAR_DICT:
5579 if (range)
5580 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005581 if (verbose)
5582 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 if (len == -1)
5584 clear_tv(&var1);
5585 return FAIL;
5586 }
5587 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005588 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589
5590 if (len == -1)
5591 {
5592 key = get_tv_string(&var1);
5593 if (*key == NUL)
5594 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005595 if (verbose)
5596 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005597 clear_tv(&var1);
5598 return FAIL;
5599 }
5600 }
5601
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005602 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005604 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005605 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606 if (len == -1)
5607 clear_tv(&var1);
5608 if (item == NULL)
5609 return FAIL;
5610
5611 copy_tv(&item->di_tv, &var1);
5612 clear_tv(rettv);
5613 *rettv = var1;
5614 }
5615 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616 }
5617 }
5618
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 return OK;
5620}
5621
5622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 * Get an option value.
5624 * "arg" points to the '&' or '+' before the option name.
5625 * "arg" is advanced to character after the option name.
5626 * Return OK or FAIL.
5627 */
5628 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005629get_option_tv(
5630 char_u **arg,
5631 typval_T *rettv, /* when NULL, only check if option exists */
5632 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
5634 char_u *option_end;
5635 long numval;
5636 char_u *stringval;
5637 int opt_type;
5638 int c;
5639 int working = (**arg == '+'); /* has("+option") */
5640 int ret = OK;
5641 int opt_flags;
5642
5643 /*
5644 * Isolate the option name and find its value.
5645 */
5646 option_end = find_option_end(arg, &opt_flags);
5647 if (option_end == NULL)
5648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 EMSG2(_("E112: Option name missing: %s"), *arg);
5651 return FAIL;
5652 }
5653
5654 if (!evaluate)
5655 {
5656 *arg = option_end;
5657 return OK;
5658 }
5659
5660 c = *option_end;
5661 *option_end = NUL;
5662 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
5665 if (opt_type == -3) /* invalid name */
5666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 EMSG2(_("E113: Unknown option: %s"), *arg);
5669 ret = FAIL;
5670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {
5673 if (opt_type == -2) /* hidden string option */
5674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 rettv->v_type = VAR_STRING;
5676 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678 else if (opt_type == -1) /* hidden number option */
5679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005680 rettv->v_type = VAR_NUMBER;
5681 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
5683 else if (opt_type == 1) /* number option */
5684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005685 rettv->v_type = VAR_NUMBER;
5686 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688 else /* string option */
5689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005690 rettv->v_type = VAR_STRING;
5691 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
5693 }
5694 else if (working && (opt_type == -2 || opt_type == -1))
5695 ret = FAIL;
5696
5697 *option_end = c; /* put back for error messages */
5698 *arg = option_end;
5699
5700 return ret;
5701}
5702
5703/*
5704 * Allocate a variable for a string constant.
5705 * Return OK or FAIL.
5706 */
5707 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005708get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
5710 char_u *p;
5711 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 int extra = 0;
5713
5714 /*
5715 * Find the end of the string, skipping backslashed characters.
5716 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005717 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719 if (*p == '\\' && p[1] != NUL)
5720 {
5721 ++p;
5722 /* A "\<x>" form occupies at least 4 characters, and produces up
5723 * to 6 characters: reserve space for 2 extra */
5724 if (*p == '<')
5725 extra += 2;
5726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
5728
5729 if (*p != '"')
5730 {
5731 EMSG2(_("E114: Missing quote: %s"), *arg);
5732 return FAIL;
5733 }
5734
5735 /* If only parsing, set *arg and return here */
5736 if (!evaluate)
5737 {
5738 *arg = p + 1;
5739 return OK;
5740 }
5741
5742 /*
5743 * Copy the string into allocated memory, handling backslashed
5744 * characters.
5745 */
5746 name = alloc((unsigned)(p - *arg + extra));
5747 if (name == NULL)
5748 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 rettv->v_type = VAR_STRING;
5750 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
5754 if (*p == '\\')
5755 {
5756 switch (*++p)
5757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 case 'b': *name++ = BS; ++p; break;
5759 case 'e': *name++ = ESC; ++p; break;
5760 case 'f': *name++ = FF; ++p; break;
5761 case 'n': *name++ = NL; ++p; break;
5762 case 'r': *name++ = CAR; ++p; break;
5763 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
5765 case 'X': /* hex: "\x1", "\x12" */
5766 case 'x':
5767 case 'u': /* Unicode: "\u0023" */
5768 case 'U':
5769 if (vim_isxdigit(p[1]))
5770 {
5771 int n, nr;
5772 int c = toupper(*p);
5773
5774 if (c == 'X')
5775 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005776 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005778 else
5779 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 nr = 0;
5781 while (--n >= 0 && vim_isxdigit(p[1]))
5782 {
5783 ++p;
5784 nr = (nr << 4) + hex2nr(*p);
5785 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787#ifdef FEAT_MBYTE
5788 /* For "\u" store the number according to
5789 * 'encoding'. */
5790 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 else
5793#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 break;
5797
5798 /* octal: "\1", "\12", "\123" */
5799 case '0':
5800 case '1':
5801 case '2':
5802 case '3':
5803 case '4':
5804 case '5':
5805 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 case '7': *name = *p++ - '0';
5807 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 *name = (*name << 3) + *p++ - '0';
5810 if (*p >= '0' && *p <= '7')
5811 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 break;
5815
5816 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 if (extra != 0)
5819 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 break;
5822 }
5823 /* FALLTHROUGH */
5824
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 break;
5827 }
5828 }
5829 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 *arg = p + 1;
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 return OK;
5837}
5838
5839/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 * Return OK or FAIL.
5842 */
5843 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005844get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845{
5846 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 int reduce = 0;
5849
5850 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 */
5853 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 break;
5859 ++reduce;
5860 ++p;
5861 }
5862 }
5863
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 return FAIL;
5868 }
5869
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871 if (!evaluate)
5872 {
5873 *arg = p + 1;
5874 return OK;
5875 }
5876
5877 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879 */
5880 str = alloc((unsigned)((p - *arg) - reduce));
5881 if (str == NULL)
5882 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 rettv->v_type = VAR_STRING;
5884 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 break;
5892 ++p;
5893 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 *arg = p + 1;
5898
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 return OK;
5900}
5901
5902/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 * Allocate a variable for a List and fill it from "*arg".
5904 * Return OK or FAIL.
5905 */
5906 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005907get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908{
Bram Moolenaar33570922005-01-25 22:26:29 +00005909 list_T *l = NULL;
5910 typval_T tv;
5911 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912
5913 if (evaluate)
5914 {
5915 l = list_alloc();
5916 if (l == NULL)
5917 return FAIL;
5918 }
5919
5920 *arg = skipwhite(*arg + 1);
5921 while (**arg != ']' && **arg != NUL)
5922 {
5923 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5924 goto failret;
5925 if (evaluate)
5926 {
5927 item = listitem_alloc();
5928 if (item != NULL)
5929 {
5930 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005931 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 list_append(l, item);
5933 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005934 else
5935 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 }
5937
5938 if (**arg == ']')
5939 break;
5940 if (**arg != ',')
5941 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005942 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 goto failret;
5944 }
5945 *arg = skipwhite(*arg + 1);
5946 }
5947
5948 if (**arg != ']')
5949 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005950 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951failret:
5952 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005953 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954 return FAIL;
5955 }
5956
5957 *arg = skipwhite(*arg + 1);
5958 if (evaluate)
5959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005960 rettv->v_type = VAR_LIST;
5961 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 ++l->lv_refcount;
5963 }
5964
5965 return OK;
5966}
5967
5968/*
5969 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005970 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005972 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005973list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005975 list_T *l;
5976
5977 l = (list_T *)alloc_clear(sizeof(list_T));
5978 if (l != NULL)
5979 {
5980 /* Prepend the list to the list of lists for garbage collection. */
5981 if (first_list != NULL)
5982 first_list->lv_used_prev = l;
5983 l->lv_used_prev = NULL;
5984 l->lv_used_next = first_list;
5985 first_list = l;
5986 }
5987 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988}
5989
5990/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005991 * Allocate an empty list for a return value.
5992 * Returns OK or FAIL.
5993 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005995rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005996{
5997 list_T *l = list_alloc();
5998
5999 if (l == NULL)
6000 return FAIL;
6001
6002 rettv->vval.v_list = l;
6003 rettv->v_type = VAR_LIST;
6004 ++l->lv_refcount;
6005 return OK;
6006}
6007
6008/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 * Unreference a list: decrement the reference count and free it when it
6010 * becomes zero.
6011 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006013list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006015 if (l != NULL && --l->lv_refcount <= 0)
6016 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017}
6018
6019/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006020 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 * Ignores the reference count.
6022 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006024list_free(
6025 list_T *l,
6026 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027{
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006030 /* Remove the list from the list of lists for garbage collection. */
6031 if (l->lv_used_prev == NULL)
6032 first_list = l->lv_used_next;
6033 else
6034 l->lv_used_prev->lv_used_next = l->lv_used_next;
6035 if (l->lv_used_next != NULL)
6036 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6037
Bram Moolenaard9fba312005-06-26 22:34:35 +00006038 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006040 /* Remove the item before deleting it. */
6041 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006042 if (recurse || (item->li_tv.v_type != VAR_LIST
6043 && item->li_tv.v_type != VAR_DICT))
6044 clear_tv(&item->li_tv);
6045 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 }
6047 vim_free(l);
6048}
6049
6050/*
6051 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006052 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006054 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006055listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058}
6059
6060/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006061 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006064listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006066 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067 vim_free(item);
6068}
6069
6070/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006071 * Remove a list item from a List and free it. Also clears the value.
6072 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006074listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006076 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006077 listitem_free(item);
6078}
6079
6080/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 * Get the number of items in a list.
6082 */
6083 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006084list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086 if (l == NULL)
6087 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006088 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089}
6090
6091/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092 * Return TRUE when two lists have exactly the same values.
6093 */
6094 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006095list_equal(
6096 list_T *l1,
6097 list_T *l2,
6098 int ic, /* ignore case for strings */
6099 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100{
Bram Moolenaar33570922005-01-25 22:26:29 +00006101 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006103 if (l1 == NULL || l2 == NULL)
6104 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006105 if (l1 == l2)
6106 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006107 if (list_len(l1) != list_len(l2))
6108 return FALSE;
6109
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 for (item1 = l1->lv_first, item2 = l2->lv_first;
6111 item1 != NULL && item2 != NULL;
6112 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 return FALSE;
6115 return item1 == NULL && item2 == NULL;
6116}
6117
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006118/*
6119 * Return the dictitem that an entry in a hashtable points to.
6120 */
6121 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006122dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006123{
6124 return HI2DI(hi);
6125}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006126
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006127/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006128 * Return TRUE when two dictionaries have exactly the same key/values.
6129 */
6130 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006131dict_equal(
6132 dict_T *d1,
6133 dict_T *d2,
6134 int ic, /* ignore case for strings */
6135 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136{
Bram Moolenaar33570922005-01-25 22:26:29 +00006137 hashitem_T *hi;
6138 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006139 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006140
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006141 if (d1 == NULL || d2 == NULL)
6142 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006143 if (d1 == d2)
6144 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006145 if (dict_len(d1) != dict_len(d2))
6146 return FALSE;
6147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006148 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006150 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006151 if (!HASHITEM_EMPTY(hi))
6152 {
6153 item2 = dict_find(d2, hi->hi_key, -1);
6154 if (item2 == NULL)
6155 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006156 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006157 return FALSE;
6158 --todo;
6159 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006160 }
6161 return TRUE;
6162}
6163
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006164static int tv_equal_recurse_limit;
6165
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006166/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006167 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006168 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006169 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006170 */
6171 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006172tv_equal(
6173 typval_T *tv1,
6174 typval_T *tv2,
6175 int ic, /* ignore case */
6176 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006177{
6178 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006179 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006180 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006181 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006183 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006184 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006186 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187 * recursiveness to a limit. We guess they are equal then.
6188 * A fixed limit has the problem of still taking an awful long time.
6189 * Reduce the limit every time running into it. That should work fine for
6190 * deeply linked structures that are not recursively linked and catch
6191 * recursiveness quickly. */
6192 if (!recursive)
6193 tv_equal_recurse_limit = 1000;
6194 if (recursive_cnt >= tv_equal_recurse_limit)
6195 {
6196 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006197 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006198 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006199
6200 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006201 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006202 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006203 ++recursive_cnt;
6204 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6205 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006206 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006207
6208 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006209 ++recursive_cnt;
6210 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6211 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006212 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006213
6214 case VAR_FUNC:
6215 return (tv1->vval.v_string != NULL
6216 && tv2->vval.v_string != NULL
6217 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6218
6219 case VAR_NUMBER:
6220 return tv1->vval.v_number == tv2->vval.v_number;
6221
6222 case VAR_STRING:
6223 s1 = get_tv_string_buf(tv1, buf1);
6224 s2 = get_tv_string_buf(tv2, buf2);
6225 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006226
6227 case VAR_SPECIAL:
6228 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006229
6230 case VAR_FLOAT:
6231#ifdef FEAT_FLOAT
6232 return tv1->vval.v_float == tv2->vval.v_float;
6233#endif
6234 case VAR_JOB:
6235#ifdef FEAT_JOB
6236 return tv1->vval.v_job == tv2->vval.v_job;
6237#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006238 case VAR_CHANNEL:
6239#ifdef FEAT_CHANNEL
6240 return tv1->vval.v_channel == tv2->vval.v_channel;
6241#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006242 case VAR_UNKNOWN:
6243 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006244 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006245
Bram Moolenaara03f2332016-02-06 18:09:59 +01006246 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6247 * does not equal anything, not even itself. */
6248 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249}
6250
6251/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 * Locate item with index "n" in list "l" and return it.
6253 * A negative index is counted from the end; -1 is the last item.
6254 * Returns NULL when "n" is out of range.
6255 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006256 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006257list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258{
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260 long idx;
6261
6262 if (l == NULL)
6263 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006264
6265 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267 n = l->lv_len + n;
6268
6269 /* Check for index out of range. */
6270 if (n < 0 || n >= l->lv_len)
6271 return NULL;
6272
6273 /* When there is a cached index may start search from there. */
6274 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006276 if (n < l->lv_idx / 2)
6277 {
6278 /* closest to the start of the list */
6279 item = l->lv_first;
6280 idx = 0;
6281 }
6282 else if (n > (l->lv_idx + l->lv_len) / 2)
6283 {
6284 /* closest to the end of the list */
6285 item = l->lv_last;
6286 idx = l->lv_len - 1;
6287 }
6288 else
6289 {
6290 /* closest to the cached index */
6291 item = l->lv_idx_item;
6292 idx = l->lv_idx;
6293 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294 }
6295 else
6296 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006297 if (n < l->lv_len / 2)
6298 {
6299 /* closest to the start of the list */
6300 item = l->lv_first;
6301 idx = 0;
6302 }
6303 else
6304 {
6305 /* closest to the end of the list */
6306 item = l->lv_last;
6307 idx = l->lv_len - 1;
6308 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006310
6311 while (n > idx)
6312 {
6313 /* search forward */
6314 item = item->li_next;
6315 ++idx;
6316 }
6317 while (n < idx)
6318 {
6319 /* search backward */
6320 item = item->li_prev;
6321 --idx;
6322 }
6323
6324 /* cache the used index */
6325 l->lv_idx = idx;
6326 l->lv_idx_item = item;
6327
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006328 return item;
6329}
6330
6331/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006332 * Get list item "l[idx]" as a number.
6333 */
6334 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006335list_find_nr(
6336 list_T *l,
6337 long idx,
6338 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006339{
6340 listitem_T *li;
6341
6342 li = list_find(l, idx);
6343 if (li == NULL)
6344 {
6345 if (errorp != NULL)
6346 *errorp = TRUE;
6347 return -1L;
6348 }
6349 return get_tv_number_chk(&li->li_tv, errorp);
6350}
6351
6352/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006353 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6354 */
6355 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006356list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006357{
6358 listitem_T *li;
6359
6360 li = list_find(l, idx - 1);
6361 if (li == NULL)
6362 {
6363 EMSGN(_(e_listidx), idx);
6364 return NULL;
6365 }
6366 return get_tv_string(&li->li_tv);
6367}
6368
6369/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006370 * Locate "item" list "l" and return its index.
6371 * Returns -1 when "item" is not in the list.
6372 */
6373 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006374list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006375{
6376 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006378
6379 if (l == NULL)
6380 return -1;
6381 idx = 0;
6382 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6383 ++idx;
6384 if (li == NULL)
6385 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006386 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006387}
6388
6389/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 * Append item "item" to the end of list "l".
6391 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006393list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394{
6395 if (l->lv_last == NULL)
6396 {
6397 /* empty list */
6398 l->lv_first = item;
6399 l->lv_last = item;
6400 item->li_prev = NULL;
6401 }
6402 else
6403 {
6404 l->lv_last->li_next = item;
6405 item->li_prev = l->lv_last;
6406 l->lv_last = item;
6407 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006408 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006409 item->li_next = NULL;
6410}
6411
6412/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006414 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006417list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006419 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420
Bram Moolenaar05159a02005-02-26 23:04:13 +00006421 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006423 copy_tv(tv, &li->li_tv);
6424 list_append(l, li);
6425 return OK;
6426}
6427
6428/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006429 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 * Return FAIL when out of memory.
6431 */
6432 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006433list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434{
6435 listitem_T *li = listitem_alloc();
6436
6437 if (li == NULL)
6438 return FAIL;
6439 li->li_tv.v_type = VAR_DICT;
6440 li->li_tv.v_lock = 0;
6441 li->li_tv.vval.v_dict = dict;
6442 list_append(list, li);
6443 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006444 return OK;
6445}
6446
6447/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006448 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006449 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006450 * Returns FAIL when out of memory.
6451 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006453list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006454{
6455 listitem_T *li = listitem_alloc();
6456
6457 if (li == NULL)
6458 return FAIL;
6459 list_append(l, li);
6460 li->li_tv.v_type = VAR_STRING;
6461 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006462 if (str == NULL)
6463 li->li_tv.vval.v_string = NULL;
6464 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006465 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006466 return FAIL;
6467 return OK;
6468}
6469
6470/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006471 * Append "n" to list "l".
6472 * Returns FAIL when out of memory.
6473 */
6474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006475list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006476{
6477 listitem_T *li;
6478
6479 li = listitem_alloc();
6480 if (li == NULL)
6481 return FAIL;
6482 li->li_tv.v_type = VAR_NUMBER;
6483 li->li_tv.v_lock = 0;
6484 li->li_tv.vval.v_number = n;
6485 list_append(l, li);
6486 return OK;
6487}
6488
6489/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006490 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006491 * If "item" is NULL append at the end.
6492 * Return FAIL when out of memory.
6493 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006495list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496{
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498
6499 if (ni == NULL)
6500 return FAIL;
6501 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006502 list_insert(l, ni, item);
6503 return OK;
6504}
6505
6506 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006507list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006508{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509 if (item == NULL)
6510 /* Append new item at end of list. */
6511 list_append(l, ni);
6512 else
6513 {
6514 /* Insert new item before existing item. */
6515 ni->li_prev = item->li_prev;
6516 ni->li_next = item;
6517 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006518 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006520 ++l->lv_idx;
6521 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006525 l->lv_idx_item = NULL;
6526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530}
6531
6532/*
6533 * Extend "l1" with "l2".
6534 * If "bef" is NULL append at the end, otherwise insert before this item.
6535 * Returns FAIL when out of memory.
6536 */
6537 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539{
Bram Moolenaar33570922005-01-25 22:26:29 +00006540 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006541 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006543 /* We also quit the loop when we have inserted the original item count of
6544 * the list, avoid a hang when we extend a list with itself. */
6545 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6547 return FAIL;
6548 return OK;
6549}
6550
6551/*
6552 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6553 * Return FAIL when out of memory.
6554 */
6555 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006556list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557{
Bram Moolenaar33570922005-01-25 22:26:29 +00006558 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006560 if (l1 == NULL || l2 == NULL)
6561 return FAIL;
6562
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006564 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 if (l == NULL)
6566 return FAIL;
6567 tv->v_type = VAR_LIST;
6568 tv->vval.v_list = l;
6569
6570 /* append all items from the second list */
6571 return list_extend(l, l2, NULL);
6572}
6573
6574/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006575 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006577 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578 * Returns NULL when out of memory.
6579 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006581list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582{
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 list_T *copy;
6584 listitem_T *item;
6585 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586
6587 if (orig == NULL)
6588 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589
6590 copy = list_alloc();
6591 if (copy != NULL)
6592 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006593 if (copyID != 0)
6594 {
6595 /* Do this before adding the items, because one of the items may
6596 * refer back to this list. */
6597 orig->lv_copyID = copyID;
6598 orig->lv_copylist = copy;
6599 }
6600 for (item = orig->lv_first; item != NULL && !got_int;
6601 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602 {
6603 ni = listitem_alloc();
6604 if (ni == NULL)
6605 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006606 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 {
6608 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6609 {
6610 vim_free(ni);
6611 break;
6612 }
6613 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006615 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616 list_append(copy, ni);
6617 }
6618 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006619 if (item != NULL)
6620 {
6621 list_unref(copy);
6622 copy = NULL;
6623 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006624 }
6625
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006626 return copy;
6627}
6628
6629/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006630 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006631 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006632 * This used to be called list_remove, but that conflicts with a Sun header
6633 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006634 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006636vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006637{
Bram Moolenaar33570922005-01-25 22:26:29 +00006638 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006639
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006640 /* notify watchers */
6641 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006642 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006643 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644 list_fix_watch(l, ip);
6645 if (ip == item2)
6646 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006648
6649 if (item2->li_next == NULL)
6650 l->lv_last = item->li_prev;
6651 else
6652 item2->li_next->li_prev = item->li_prev;
6653 if (item->li_prev == NULL)
6654 l->lv_first = item2->li_next;
6655 else
6656 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006657 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006658}
6659
6660/*
6661 * Return an allocated string with the string representation of a list.
6662 * May return NULL.
6663 */
6664 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006665list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666{
6667 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668
6669 if (tv->vval.v_list == NULL)
6670 return NULL;
6671 ga_init2(&ga, (int)sizeof(char), 80);
6672 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006673 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006674 {
6675 vim_free(ga.ga_data);
6676 return NULL;
6677 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678 ga_append(&ga, ']');
6679 ga_append(&ga, NUL);
6680 return (char_u *)ga.ga_data;
6681}
6682
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006683typedef struct join_S {
6684 char_u *s;
6685 char_u *tofree;
6686} join_T;
6687
6688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006689list_join_inner(
6690 garray_T *gap, /* to store the result in */
6691 list_T *l,
6692 char_u *sep,
6693 int echo_style,
6694 int copyID,
6695 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006696{
6697 int i;
6698 join_T *p;
6699 int len;
6700 int sumlen = 0;
6701 int first = TRUE;
6702 char_u *tofree;
6703 char_u numbuf[NUMBUFLEN];
6704 listitem_T *item;
6705 char_u *s;
6706
6707 /* Stringify each item in the list. */
6708 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6709 {
6710 if (echo_style)
6711 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6712 else
6713 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6714 if (s == NULL)
6715 return FAIL;
6716
6717 len = (int)STRLEN(s);
6718 sumlen += len;
6719
Bram Moolenaarcde88542015-08-11 19:14:00 +02006720 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006721 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6722 if (tofree != NULL || s != numbuf)
6723 {
6724 p->s = s;
6725 p->tofree = tofree;
6726 }
6727 else
6728 {
6729 p->s = vim_strnsave(s, len);
6730 p->tofree = p->s;
6731 }
6732
6733 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006734 if (did_echo_string_emsg) /* recursion error, bail out */
6735 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006736 }
6737
6738 /* Allocate result buffer with its total size, avoid re-allocation and
6739 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6740 if (join_gap->ga_len >= 2)
6741 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6742 if (ga_grow(gap, sumlen + 2) == FAIL)
6743 return FAIL;
6744
6745 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6746 {
6747 if (first)
6748 first = FALSE;
6749 else
6750 ga_concat(gap, sep);
6751 p = ((join_T *)join_gap->ga_data) + i;
6752
6753 if (p->s != NULL)
6754 ga_concat(gap, p->s);
6755 line_breakcheck();
6756 }
6757
6758 return OK;
6759}
6760
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006761/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006762 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006763 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006764 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006765 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006766 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006767list_join(
6768 garray_T *gap,
6769 list_T *l,
6770 char_u *sep,
6771 int echo_style,
6772 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006773{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006774 garray_T join_ga;
6775 int retval;
6776 join_T *p;
6777 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006778
Bram Moolenaard39a7512015-04-16 22:51:22 +02006779 if (l->lv_len < 1)
6780 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006781 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6782 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6783
6784 /* Dispose each item in join_ga. */
6785 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006786 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006787 p = (join_T *)join_ga.ga_data;
6788 for (i = 0; i < join_ga.ga_len; ++i)
6789 {
6790 vim_free(p->tofree);
6791 ++p;
6792 }
6793 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006794 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006795
6796 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797}
6798
6799/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006800 * Return the next (unique) copy ID.
6801 * Used for serializing nested structures.
6802 */
6803 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006804get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006805{
6806 current_copyID += COPYID_INC;
6807 return current_copyID;
6808}
6809
6810/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006811 * Garbage collection for lists and dictionaries.
6812 *
6813 * We use reference counts to be able to free most items right away when they
6814 * are no longer used. But for composite items it's possible that it becomes
6815 * unused while the reference count is > 0: When there is a recursive
6816 * reference. Example:
6817 * :let l = [1, 2, 3]
6818 * :let d = {9: l}
6819 * :let l[1] = d
6820 *
6821 * Since this is quite unusual we handle this with garbage collection: every
6822 * once in a while find out which lists and dicts are not referenced from any
6823 * variable.
6824 *
6825 * Here is a good reference text about garbage collection (refers to Python
6826 * but it applies to all reference-counting mechanisms):
6827 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006828 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006829
6830/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006831 * Do garbage collection for lists and dicts.
6832 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006833 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006835garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006837 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006838 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839 buf_T *buf;
6840 win_T *wp;
6841 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006842 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006843 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006844 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006845#ifdef FEAT_WINDOWS
6846 tabpage_T *tp;
6847#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006848
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006849 /* Only do this once. */
6850 want_garbage_collect = FALSE;
6851 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006852 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006853
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 /* We advance by two because we add one for items referenced through
6855 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006856 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 /*
6859 * 1. Go through all accessible variables and mark all lists and dicts
6860 * with copyID.
6861 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006862
6863 /* Don't free variables in the previous_funccal list unless they are only
6864 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006865 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6867 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006868 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6869 NULL);
6870 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6871 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006872 }
6873
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 /* script-local variables */
6875 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006876 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877
6878 /* buffer-local variables */
6879 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006880 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6881 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006882
6883 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006884 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6886 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006887#ifdef FEAT_AUTOCMD
6888 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6890 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006891#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006892
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006893#ifdef FEAT_WINDOWS
6894 /* tabpage-local variables */
6895 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6897 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006898#endif
6899
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902
6903 /* function-local variables */
6904 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6905 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006906 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6907 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908 }
6909
Bram Moolenaard812df62008-11-09 12:46:09 +00006910 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006911 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006912
Bram Moolenaar1dced572012-04-05 16:54:08 +02006913#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006915#endif
6916
Bram Moolenaardb913952012-06-29 12:54:53 +02006917#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006919#endif
6920
6921#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006923#endif
6924
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006925#ifdef FEAT_CHANNEL
6926 abort = abort || set_ref_in_channel(copyID);
6927#endif
6928
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006930 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 /*
6932 * 2. Free lists and dictionaries that are not referenced.
6933 */
6934 did_free = free_unref_items(copyID);
6935
6936 /*
6937 * 3. Check if any funccal can be freed now.
6938 */
6939 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006940 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006941 if (can_free_funccal(*pfc, copyID))
6942 {
6943 fc = *pfc;
6944 *pfc = fc->caller;
6945 free_funccal(fc, TRUE);
6946 did_free = TRUE;
6947 did_free_funccal = TRUE;
6948 }
6949 else
6950 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006951 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006952 if (did_free_funccal)
6953 /* When a funccal was freed some more items might be garbage
6954 * collected, so run again. */
6955 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006956 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006957 else if (p_verbose > 0)
6958 {
6959 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6960 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006961
6962 return did_free;
6963}
6964
6965/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006966 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 */
6968 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006969free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006971 dict_T *dd, *dd_next;
6972 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973 int did_free = FALSE;
6974
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977 */
6978 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006979 {
6980 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006983 /* Free the Dictionary and ordinary items it contains, but don't
6984 * recurse into Lists and Dictionaries, they will be in the list
6985 * of dicts or list of lists. */
6986 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006987 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006989 dd = dd_next;
6990 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991
6992 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006993 * Go through the list of lists and free items without the copyID.
6994 * But don't free a list that has a watcher (used in a for loop), these
6995 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 */
6997 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006998 {
6999 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007000 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7001 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007003 /* Free the List and ordinary items it contains, but don't recurse
7004 * into Lists and Dictionaries, they will be in the list of dicts
7005 * or list of lists. */
7006 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007009 ll = ll_next;
7010 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007011
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 return did_free;
7013}
7014
7015/*
7016 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007017 * "list_stack" is used to add lists to be marked. Can be NULL.
7018 *
7019 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007021 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007022set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023{
7024 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 hashtab_T *cur_ht;
7028 ht_stack_T *ht_stack = NULL;
7029 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 cur_ht = ht;
7032 for (;;)
7033 {
7034 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007036 /* Mark each item in the hashtab. If the item contains a hashtab
7037 * it is added to ht_stack, if it contains a list it is added to
7038 * list_stack. */
7039 todo = (int)cur_ht->ht_used;
7040 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7041 if (!HASHITEM_EMPTY(hi))
7042 {
7043 --todo;
7044 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7045 &ht_stack, list_stack);
7046 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007047 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048
7049 if (ht_stack == NULL)
7050 break;
7051
7052 /* take an item from the stack */
7053 cur_ht = ht_stack->ht;
7054 tempitem = ht_stack;
7055 ht_stack = ht_stack->prev;
7056 free(tempitem);
7057 }
7058
7059 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060}
7061
7062/*
7063 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7065 *
7066 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007067 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007069set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007071 listitem_T *li;
7072 int abort = FALSE;
7073 list_T *cur_l;
7074 list_stack_T *list_stack = NULL;
7075 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007077 cur_l = l;
7078 for (;;)
7079 {
7080 if (!abort)
7081 /* Mark each item in the list. If the item contains a hashtab
7082 * it is added to ht_stack, if it contains a list it is added to
7083 * list_stack. */
7084 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7085 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7086 ht_stack, &list_stack);
7087 if (list_stack == NULL)
7088 break;
7089
7090 /* take an item from the stack */
7091 cur_l = list_stack->list;
7092 tempitem = list_stack;
7093 list_stack = list_stack->prev;
7094 free(tempitem);
7095 }
7096
7097 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007098}
7099
7100/*
7101 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007102 * "list_stack" is used to add lists to be marked. Can be NULL.
7103 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7104 *
7105 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007106 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007107 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007108set_ref_in_item(
7109 typval_T *tv,
7110 int copyID,
7111 ht_stack_T **ht_stack,
7112 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007113{
7114 dict_T *dd;
7115 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007116 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007117
Bram Moolenaara03f2332016-02-06 18:09:59 +01007118 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007119 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007120 dd = tv->vval.v_dict;
7121 if (dd != NULL && dd->dv_copyID != copyID)
7122 {
7123 /* Didn't see this dict yet. */
7124 dd->dv_copyID = copyID;
7125 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007126 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007127 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7128 }
7129 else
7130 {
7131 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7132 if (newitem == NULL)
7133 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007134 else
7135 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007136 newitem->ht = &dd->dv_hashtab;
7137 newitem->prev = *ht_stack;
7138 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007139 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007140 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007141 }
7142 }
7143 else if (tv->v_type == VAR_LIST)
7144 {
7145 ll = tv->vval.v_list;
7146 if (ll != NULL && ll->lv_copyID != copyID)
7147 {
7148 /* Didn't see this list yet. */
7149 ll->lv_copyID = copyID;
7150 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007151 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007152 abort = set_ref_in_list(ll, copyID, ht_stack);
7153 }
7154 else
7155 {
7156 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007157 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007158 if (newitem == NULL)
7159 abort = TRUE;
7160 else
7161 {
7162 newitem->list = ll;
7163 newitem->prev = *list_stack;
7164 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007165 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007166 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007167 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007168 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007169 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007170}
7171
7172/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007173 * Allocate an empty header for a dictionary.
7174 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007175 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007176dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177{
Bram Moolenaar33570922005-01-25 22:26:29 +00007178 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007179
Bram Moolenaar33570922005-01-25 22:26:29 +00007180 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007181 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007182 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007183 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007184 if (first_dict != NULL)
7185 first_dict->dv_used_prev = d;
7186 d->dv_used_next = first_dict;
7187 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007188 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007189
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007191 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007192 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007193 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007194 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007195 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197}
7198
7199/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007200 * Allocate an empty dict for a return value.
7201 * Returns OK or FAIL.
7202 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007203 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007204rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007205{
7206 dict_T *d = dict_alloc();
7207
7208 if (d == NULL)
7209 return FAIL;
7210
7211 rettv->vval.v_dict = d;
7212 rettv->v_type = VAR_DICT;
7213 ++d->dv_refcount;
7214 return OK;
7215}
7216
7217
7218/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 * Unreference a Dictionary: decrement the reference count and free it when it
7220 * becomes zero.
7221 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007223dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007225 if (d != NULL && --d->dv_refcount <= 0)
7226 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227}
7228
7229/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007230 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231 * Ignores the reference count.
7232 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007234dict_free(
7235 dict_T *d,
7236 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007238 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007239 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007240 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007242 /* Remove the dict from the list of dicts for garbage collection. */
7243 if (d->dv_used_prev == NULL)
7244 first_dict = d->dv_used_next;
7245 else
7246 d->dv_used_prev->dv_used_next = d->dv_used_next;
7247 if (d->dv_used_next != NULL)
7248 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7249
7250 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007251 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007252 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255 if (!HASHITEM_EMPTY(hi))
7256 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007257 /* Remove the item before deleting it, just in case there is
7258 * something recursive causing trouble. */
7259 di = HI2DI(hi);
7260 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007261 if (recurse || (di->di_tv.v_type != VAR_LIST
7262 && di->di_tv.v_type != VAR_DICT))
7263 clear_tv(&di->di_tv);
7264 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007265 --todo;
7266 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007268 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269 vim_free(d);
7270}
7271
7272/*
7273 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274 * The "key" is copied to the new item.
7275 * Note that the value of the item "di_tv" still needs to be initialized!
7276 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007278 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007279dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280{
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007283 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007285 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007287 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007288 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290}
7291
7292/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293 * Make a copy of a Dictionary item.
7294 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007295 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007296dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297{
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007300 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7301 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 if (di != NULL)
7303 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007305 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 copy_tv(&org->di_tv, &di->di_tv);
7307 }
7308 return di;
7309}
7310
7311/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312 * Remove item "item" from Dictionary "dict" and free it.
7313 */
7314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007315dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316{
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if (HASHITEM_EMPTY(hi))
7321 EMSG2(_(e_intern2), "dictitem_remove()");
7322 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 dictitem_free(item);
7325}
7326
7327/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007328 * Free a dict item. Also clears the value.
7329 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007330 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007331dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007334 if (item->di_flags & DI_FLAGS_ALLOC)
7335 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336}
7337
7338/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7340 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007341 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342 * Returns NULL when out of memory.
7343 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007345dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007346{
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 dict_T *copy;
7348 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007351
7352 if (orig == NULL)
7353 return NULL;
7354
7355 copy = dict_alloc();
7356 if (copy != NULL)
7357 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007358 if (copyID != 0)
7359 {
7360 orig->dv_copyID = copyID;
7361 orig->dv_copydict = copy;
7362 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007363 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007364 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007368 --todo;
7369
7370 di = dictitem_alloc(hi->hi_key);
7371 if (di == NULL)
7372 break;
7373 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374 {
7375 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7376 copyID) == FAIL)
7377 {
7378 vim_free(di);
7379 break;
7380 }
7381 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382 else
7383 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7384 if (dict_add(copy, di) == FAIL)
7385 {
7386 dictitem_free(di);
7387 break;
7388 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007393 if (todo > 0)
7394 {
7395 dict_unref(copy);
7396 copy = NULL;
7397 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 }
7399
7400 return copy;
7401}
7402
7403/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007405 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007407 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007408dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409{
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411}
7412
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007414 * Add a number or string entry to dictionary "d".
7415 * When "str" is NULL use number "nr", otherwise use "str".
7416 * Returns FAIL when out of memory and when key already exists.
7417 */
7418 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007419dict_add_nr_str(
7420 dict_T *d,
7421 char *key,
7422 long nr,
7423 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007424{
7425 dictitem_T *item;
7426
7427 item = dictitem_alloc((char_u *)key);
7428 if (item == NULL)
7429 return FAIL;
7430 item->di_tv.v_lock = 0;
7431 if (str == NULL)
7432 {
7433 item->di_tv.v_type = VAR_NUMBER;
7434 item->di_tv.vval.v_number = nr;
7435 }
7436 else
7437 {
7438 item->di_tv.v_type = VAR_STRING;
7439 item->di_tv.vval.v_string = vim_strsave(str);
7440 }
7441 if (dict_add(d, item) == FAIL)
7442 {
7443 dictitem_free(item);
7444 return FAIL;
7445 }
7446 return OK;
7447}
7448
7449/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007450 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007451 * Returns FAIL when out of memory and when key already exists.
7452 */
7453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007454dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007455{
7456 dictitem_T *item;
7457
7458 item = dictitem_alloc((char_u *)key);
7459 if (item == NULL)
7460 return FAIL;
7461 item->di_tv.v_lock = 0;
7462 item->di_tv.v_type = VAR_LIST;
7463 item->di_tv.vval.v_list = list;
7464 if (dict_add(d, item) == FAIL)
7465 {
7466 dictitem_free(item);
7467 return FAIL;
7468 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007469 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007470 return OK;
7471}
7472
7473/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007474 * Get the number of items in a Dictionary.
7475 */
7476 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007477dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007479 if (d == NULL)
7480 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007481 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007482}
7483
7484/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 * Find item "key[len]" in Dictionary "d".
7486 * If "len" is negative use strlen(key).
7487 * Returns NULL when not found.
7488 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007489 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007490dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007492#define AKEYLEN 200
7493 char_u buf[AKEYLEN];
7494 char_u *akey;
7495 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007498 if (len < 0)
7499 akey = key;
7500 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007501 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 tofree = akey = vim_strnsave(key, len);
7503 if (akey == NULL)
7504 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007505 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 else
7507 {
7508 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007509 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 akey = buf;
7511 }
7512
Bram Moolenaar33570922005-01-25 22:26:29 +00007513 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 vim_free(tofree);
7515 if (HASHITEM_EMPTY(hi))
7516 return NULL;
7517 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518}
7519
7520/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007521 * Get a string item from a dictionary.
7522 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007523 * Returns NULL if the entry doesn't exist or out of memory.
7524 */
7525 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007526get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007527{
7528 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007529 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007530
7531 di = dict_find(d, key, -1);
7532 if (di == NULL)
7533 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007534 s = get_tv_string(&di->di_tv);
7535 if (save && s != NULL)
7536 s = vim_strsave(s);
7537 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007538}
7539
7540/*
7541 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007542 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007543 */
7544 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007545get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007546{
7547 dictitem_T *di;
7548
7549 di = dict_find(d, key, -1);
7550 if (di == NULL)
7551 return 0;
7552 return get_tv_number(&di->di_tv);
7553}
7554
7555/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 * Return an allocated string with the string representation of a Dictionary.
7557 * May return NULL.
7558 */
7559 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007560dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561{
7562 garray_T ga;
7563 int first = TRUE;
7564 char_u *tofree;
7565 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007569 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007571 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572 return NULL;
7573 ga_init2(&ga, (int)sizeof(char), 80);
7574 ga_append(&ga, '{');
7575
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007576 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007577 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007579 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007581 --todo;
7582
7583 if (first)
7584 first = FALSE;
7585 else
7586 ga_concat(&ga, (char_u *)", ");
7587
7588 tofree = string_quote(hi->hi_key, FALSE);
7589 if (tofree != NULL)
7590 {
7591 ga_concat(&ga, tofree);
7592 vim_free(tofree);
7593 }
7594 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007595 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007596 if (s != NULL)
7597 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007599 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007600 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007601 line_breakcheck();
7602
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007605 if (todo > 0)
7606 {
7607 vim_free(ga.ga_data);
7608 return NULL;
7609 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610
7611 ga_append(&ga, '}');
7612 ga_append(&ga, NUL);
7613 return (char_u *)ga.ga_data;
7614}
7615
7616/*
7617 * Allocate a variable for a Dictionary and fill it from "*arg".
7618 * Return OK or FAIL. Returns NOTDONE for {expr}.
7619 */
7620 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007621get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622{
Bram Moolenaar33570922005-01-25 22:26:29 +00007623 dict_T *d = NULL;
7624 typval_T tvkey;
7625 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007626 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007629 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630
7631 /*
7632 * First check if it's not a curly-braces thing: {expr}.
7633 * Must do this without evaluating, otherwise a function may be called
7634 * twice. Unfortunately this means we need to call eval1() twice for the
7635 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007636 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007638 if (*start != '}')
7639 {
7640 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7641 return FAIL;
7642 if (*start == '}')
7643 return NOTDONE;
7644 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007645
7646 if (evaluate)
7647 {
7648 d = dict_alloc();
7649 if (d == NULL)
7650 return FAIL;
7651 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007652 tvkey.v_type = VAR_UNKNOWN;
7653 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654
7655 *arg = skipwhite(*arg + 1);
7656 while (**arg != '}' && **arg != NUL)
7657 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007658 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 goto failret;
7660 if (**arg != ':')
7661 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007662 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007663 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 goto failret;
7665 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007666 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007668 key = get_tv_string_buf_chk(&tvkey, buf);
7669 if (key == NULL || *key == NUL)
7670 {
7671 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7672 if (key != NULL)
7673 EMSG(_(e_emptykey));
7674 clear_tv(&tvkey);
7675 goto failret;
7676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678
7679 *arg = skipwhite(*arg + 1);
7680 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7681 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007682 if (evaluate)
7683 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 goto failret;
7685 }
7686 if (evaluate)
7687 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007688 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 if (item != NULL)
7690 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007691 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007692 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 clear_tv(&tv);
7694 goto failret;
7695 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007696 item = dictitem_alloc(key);
7697 clear_tv(&tvkey);
7698 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007701 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007702 if (dict_add(d, item) == FAIL)
7703 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 }
7705 }
7706
7707 if (**arg == '}')
7708 break;
7709 if (**arg != ',')
7710 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007711 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712 goto failret;
7713 }
7714 *arg = skipwhite(*arg + 1);
7715 }
7716
7717 if (**arg != '}')
7718 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007719 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720failret:
7721 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007722 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 return FAIL;
7724 }
7725
7726 *arg = skipwhite(*arg + 1);
7727 if (evaluate)
7728 {
7729 rettv->v_type = VAR_DICT;
7730 rettv->vval.v_dict = d;
7731 ++d->dv_refcount;
7732 }
7733
7734 return OK;
7735}
7736
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007737#if defined(FEAT_CHANNEL) || defined(PROTO)
7738/*
7739 * Decrement the reference count on "channel" and free it when it goes down to
7740 * zero.
7741 * Returns TRUE when the channel was freed.
7742 */
7743 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007744channel_unref(channel_T *channel)
7745{
7746 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007747 {
Bram Moolenaar77073442016-02-13 23:23:53 +01007748 channel_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007749 return TRUE;
7750 }
7751 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007752}
7753#endif
7754
Bram Moolenaar65edff82016-02-21 16:40:11 +01007755#if defined(FEAT_JOB) || defined(PROTO)
7756static job_T *first_job = NULL;
7757
Bram Moolenaar835dc632016-02-07 14:27:38 +01007758 static void
7759job_free(job_T *job)
7760{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007761# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007762 if (job->jv_channel != NULL)
7763 {
7764 /* The channel doesn't count as a references for the job, we need to
7765 * NULL the reference when the job is freed. */
7766 job->jv_channel->ch_job = NULL;
7767 channel_unref(job->jv_channel);
7768 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007769# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007770 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007771
7772 if (job->jv_next != NULL)
7773 job->jv_next->jv_prev = job->jv_prev;
7774 if (job->jv_prev == NULL)
7775 first_job = job->jv_next;
7776 else
7777 job->jv_prev->jv_next = job->jv_next;
7778
7779 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007780 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007781 vim_free(job);
7782}
7783
7784 static void
7785job_unref(job_T *job)
7786{
7787 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007788 {
7789 /* Do not free the job when it has not ended yet and there is a
7790 * "stoponexit" flag or an exit callback. */
7791 if (job->jv_status != JOB_STARTED
7792 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
7793 job_free(job);
7794 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007795}
7796
7797/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007798 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007799 */
7800 static job_T *
7801job_alloc(void)
7802{
7803 job_T *job;
7804
7805 job = (job_T *)alloc_clear(sizeof(job_T));
7806 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007807 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007808 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007809 job->jv_stoponexit = vim_strsave((char_u *)"term");
7810
7811 if (first_job != NULL)
7812 {
7813 first_job->jv_prev = job;
7814 job->jv_next = first_job;
7815 }
7816 first_job = job;
7817 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007818 return job;
7819}
7820
Bram Moolenaar65edff82016-02-21 16:40:11 +01007821 static void
7822job_set_options(job_T *job, jobopt_T *opt)
7823{
7824 if (opt->jo_set & JO_STOPONEXIT)
7825 {
7826 vim_free(job->jv_stoponexit);
7827 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7828 job->jv_stoponexit = NULL;
7829 else
7830 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7831 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007832 if (opt->jo_set & JO_EXIT_CB)
7833 {
7834 vim_free(job->jv_exit_cb);
7835 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7836 job->jv_exit_cb = NULL;
7837 else
7838 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7839 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007840}
7841
7842/*
7843 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7844 */
7845 void
7846job_stop_on_exit()
7847{
7848 job_T *job;
7849
7850 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007851 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007852 mch_stop_job(job, job->jv_stoponexit);
7853}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007854#endif
7855
Bram Moolenaar17a13432016-01-24 14:22:10 +01007856 static char *
7857get_var_special_name(int nr)
7858{
7859 switch (nr)
7860 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007861 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007862 case VVAL_TRUE: return "v:true";
7863 case VVAL_NONE: return "v:none";
7864 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007865 }
7866 EMSG2(_(e_intern2), "get_var_special_name()");
7867 return "42";
7868}
7869
Bram Moolenaar8c711452005-01-14 21:53:12 +00007870/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007871 * Return a string with the string representation of a variable.
7872 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007873 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007874 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007875 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007876 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007877 */
7878 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007879echo_string(
7880 typval_T *tv,
7881 char_u **tofree,
7882 char_u *numbuf,
7883 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007884{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007885 static int recurse = 0;
7886 char_u *r = NULL;
7887
Bram Moolenaar33570922005-01-25 22:26:29 +00007888 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007889 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007890 if (!did_echo_string_emsg)
7891 {
7892 /* Only give this message once for a recursive call to avoid
7893 * flooding the user with errors. And stop iterating over lists
7894 * and dicts. */
7895 did_echo_string_emsg = TRUE;
7896 EMSG(_("E724: variable nested too deep for displaying"));
7897 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007899 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007900 }
7901 ++recurse;
7902
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007903 switch (tv->v_type)
7904 {
7905 case VAR_FUNC:
7906 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007907 r = tv->vval.v_string;
7908 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007909
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007910 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007911 if (tv->vval.v_list == NULL)
7912 {
7913 *tofree = NULL;
7914 r = NULL;
7915 }
7916 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7917 {
7918 *tofree = NULL;
7919 r = (char_u *)"[...]";
7920 }
7921 else
7922 {
7923 tv->vval.v_list->lv_copyID = copyID;
7924 *tofree = list2string(tv, copyID);
7925 r = *tofree;
7926 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007927 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007928
Bram Moolenaar8c711452005-01-14 21:53:12 +00007929 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007930 if (tv->vval.v_dict == NULL)
7931 {
7932 *tofree = NULL;
7933 r = NULL;
7934 }
7935 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7936 {
7937 *tofree = NULL;
7938 r = (char_u *)"{...}";
7939 }
7940 else
7941 {
7942 tv->vval.v_dict->dv_copyID = copyID;
7943 *tofree = dict2string(tv, copyID);
7944 r = *tofree;
7945 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007946 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007947
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007948 case VAR_STRING:
7949 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007950 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007951 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007952 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007953 *tofree = NULL;
7954 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007955 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007956
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007957 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007958#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007959 *tofree = NULL;
7960 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7961 r = numbuf;
7962 break;
7963#endif
7964
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007965 case VAR_SPECIAL:
7966 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007967 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007968 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007969 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007970
Bram Moolenaar8502c702014-06-17 12:51:16 +02007971 if (--recurse == 0)
7972 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007973 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007974}
7975
7976/*
7977 * Return a string with the string representation of a variable.
7978 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7979 * "numbuf" is used for a number.
7980 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007981 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007982 */
7983 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007984tv2string(
7985 typval_T *tv,
7986 char_u **tofree,
7987 char_u *numbuf,
7988 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007989{
7990 switch (tv->v_type)
7991 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007992 case VAR_FUNC:
7993 *tofree = string_quote(tv->vval.v_string, TRUE);
7994 return *tofree;
7995 case VAR_STRING:
7996 *tofree = string_quote(tv->vval.v_string, FALSE);
7997 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007998#ifdef FEAT_FLOAT
7999 case VAR_FLOAT:
8000 *tofree = NULL;
8001 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8002 return numbuf;
8003#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008004 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008005 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008006 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008007 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008008 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008009 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008010 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008011 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008012 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008013 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008014}
8015
8016/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 * Return string "str" in ' quotes, doubling ' characters.
8018 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008019 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008020 */
8021 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008022string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008023{
Bram Moolenaar33570922005-01-25 22:26:29 +00008024 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008025 char_u *p, *r, *s;
8026
Bram Moolenaar33570922005-01-25 22:26:29 +00008027 len = (function ? 13 : 3);
8028 if (str != NULL)
8029 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008030 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008031 for (p = str; *p != NUL; mb_ptr_adv(p))
8032 if (*p == '\'')
8033 ++len;
8034 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008035 s = r = alloc(len);
8036 if (r != NULL)
8037 {
8038 if (function)
8039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008040 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008041 r += 10;
8042 }
8043 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008044 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 if (str != NULL)
8046 for (p = str; *p != NUL; )
8047 {
8048 if (*p == '\'')
8049 *r++ = '\'';
8050 MB_COPY_CHAR(p, r);
8051 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008052 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008053 if (function)
8054 *r++ = ')';
8055 *r++ = NUL;
8056 }
8057 return s;
8058}
8059
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008060#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008061/*
8062 * Convert the string "text" to a floating point number.
8063 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8064 * this always uses a decimal point.
8065 * Returns the length of the text that was consumed.
8066 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008068string2float(
8069 char_u *text,
8070 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008071{
8072 char *s = (char *)text;
8073 float_T f;
8074
8075 f = strtod(s, &s);
8076 *value = f;
8077 return (int)((char_u *)s - text);
8078}
8079#endif
8080
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 * Get the value of an environment variable.
8083 * "arg" is pointing to the '$'. It is advanced to after the name.
8084 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008085 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 */
8087 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008088get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089{
8090 char_u *string = NULL;
8091 int len;
8092 int cc;
8093 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008094 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095
8096 ++*arg;
8097 name = *arg;
8098 len = get_env_len(arg);
8099 if (evaluate)
8100 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008101 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008102 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008103
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008104 cc = name[len];
8105 name[len] = NUL;
8106 /* first try vim_getenv(), fast for normal environment vars */
8107 string = vim_getenv(name, &mustfree);
8108 if (string != NULL && *string != NUL)
8109 {
8110 if (!mustfree)
8111 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008113 else
8114 {
8115 if (mustfree)
8116 vim_free(string);
8117
8118 /* next try expanding things like $VIM and ${HOME} */
8119 string = expand_env_save(name - 1);
8120 if (string != NULL && *string == '$')
8121 {
8122 vim_free(string);
8123 string = NULL;
8124 }
8125 }
8126 name[len] = cc;
8127
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008128 rettv->v_type = VAR_STRING;
8129 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 }
8131
8132 return OK;
8133}
8134
8135/*
8136 * Array with names and number of arguments of all internal functions
8137 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8138 */
8139static struct fst
8140{
8141 char *f_name; /* function name */
8142 char f_min_argc; /* minimal number of arguments */
8143 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008144 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008145 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146} functions[] =
8147{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008148#ifdef FEAT_FLOAT
8149 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008150 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008151#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008152 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008153 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008154 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"append", 2, 2, f_append},
8156 {"argc", 0, 0, f_argc},
8157 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008158 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008159 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008160#ifdef FEAT_FLOAT
8161 {"asin", 1, 1, f_asin}, /* WJMc */
8162#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008163 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008164 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008165 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008166 {"assert_false", 1, 2, f_assert_false},
8167 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168#ifdef FEAT_FLOAT
8169 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008170 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008173 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {"bufexists", 1, 1, f_bufexists},
8175 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8176 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8177 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8178 {"buflisted", 1, 1, f_buflisted},
8179 {"bufloaded", 1, 1, f_bufloaded},
8180 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008181 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"bufwinnr", 1, 1, f_bufwinnr},
8183 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008184 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008185 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008186 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008187#ifdef FEAT_FLOAT
8188 {"ceil", 1, 1, f_ceil},
8189#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008190#ifdef FEAT_CHANNEL
8191 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008192# ifdef FEAT_JOB
8193 {"ch_getjob", 1, 1, f_ch_getjob},
8194# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008195 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008196 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008197 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008198 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008199 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008200 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8201 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008202 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008203 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008204#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008205 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008206 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008208 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008210#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008211 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008212 {"complete_add", 1, 1, f_complete_add},
8213 {"complete_check", 0, 0, f_complete_check},
8214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008216 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008217#ifdef FEAT_FLOAT
8218 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008219 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008220#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008221 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008223 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008224 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008225 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008227 {"diff_filler", 1, 1, f_diff_filler},
8228 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008229 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008230 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008232 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {"eventhandler", 0, 0, f_eventhandler},
8234 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008235 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008237#ifdef FEAT_FLOAT
8238 {"exp", 1, 1, f_exp},
8239#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008240 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008241 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008242 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8244 {"filereadable", 1, 1, f_filereadable},
8245 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008246 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008247 {"finddir", 1, 3, f_finddir},
8248 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008249#ifdef FEAT_FLOAT
8250 {"float2nr", 1, 1, f_float2nr},
8251 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008252 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008253#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008254 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {"fnamemodify", 2, 2, f_fnamemodify},
8256 {"foldclosed", 1, 1, f_foldclosed},
8257 {"foldclosedend", 1, 1, f_foldclosedend},
8258 {"foldlevel", 1, 1, f_foldlevel},
8259 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008260 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008262 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008263 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008264 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008265 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008266 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {"getchar", 0, 1, f_getchar},
8268 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008269 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"getcmdline", 0, 0, f_getcmdline},
8271 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008272 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008273 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008274 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008275 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008276 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008277 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {"getfsize", 1, 1, f_getfsize},
8279 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008280 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008281 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008282 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008283 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008284 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008285 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008286 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008287 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008289 {"gettabvar", 2, 3, f_gettabvar},
8290 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"getwinposx", 0, 0, f_getwinposx},
8292 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008293 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008294 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008295 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008296 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008298 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008299 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008300 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8302 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8303 {"histadd", 2, 2, f_histadd},
8304 {"histdel", 1, 2, f_histdel},
8305 {"histget", 1, 2, f_histget},
8306 {"histnr", 1, 1, f_histnr},
8307 {"hlID", 1, 1, f_hlID},
8308 {"hlexists", 1, 1, f_hlexists},
8309 {"hostname", 0, 0, f_hostname},
8310 {"iconv", 3, 3, f_iconv},
8311 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008312 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008313 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008315 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {"inputrestore", 0, 0, f_inputrestore},
8317 {"inputsave", 0, 0, f_inputsave},
8318 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008319 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008320 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008322 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008323 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008324#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008325# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008326 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008327# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008328 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008329 {"job_start", 1, 2, f_job_start},
8330 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008331 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008332#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008333 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008334 {"js_decode", 1, 1, f_js_decode},
8335 {"js_encode", 1, 1, f_js_encode},
8336 {"json_decode", 1, 1, f_json_decode},
8337 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008338 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008340 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {"libcall", 3, 3, f_libcall},
8342 {"libcallnr", 3, 3, f_libcallnr},
8343 {"line", 1, 1, f_line},
8344 {"line2byte", 1, 1, f_line2byte},
8345 {"lispindent", 1, 1, f_lispindent},
8346 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008347#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008348 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008349 {"log10", 1, 1, f_log10},
8350#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008351#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008352 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008353#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008354 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008355 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008356 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008357 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008358 {"matchadd", 2, 5, f_matchadd},
8359 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008360 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008361 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008362 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008363 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008364 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008365 {"max", 1, 1, f_max},
8366 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008367#ifdef vim_mkdir
8368 {"mkdir", 1, 3, f_mkdir},
8369#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008370 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008371#ifdef FEAT_MZSCHEME
8372 {"mzeval", 1, 1, f_mzeval},
8373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008375 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008376 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008377 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008378#ifdef FEAT_PERL
8379 {"perleval", 1, 1, f_perleval},
8380#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008381#ifdef FEAT_FLOAT
8382 {"pow", 2, 2, f_pow},
8383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008385 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008386 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008387#ifdef FEAT_PYTHON3
8388 {"py3eval", 1, 1, f_py3eval},
8389#endif
8390#ifdef FEAT_PYTHON
8391 {"pyeval", 1, 1, f_pyeval},
8392#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008393 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008394 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008395 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008396 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008397 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 {"remote_expr", 2, 3, f_remote_expr},
8399 {"remote_foreground", 1, 1, f_remote_foreground},
8400 {"remote_peek", 1, 2, f_remote_peek},
8401 {"remote_read", 1, 1, f_remote_read},
8402 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008403 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008405 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008407 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008408#ifdef FEAT_FLOAT
8409 {"round", 1, 1, f_round},
8410#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008411 {"screenattr", 2, 2, f_screenattr},
8412 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008413 {"screencol", 0, 0, f_screencol},
8414 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008415 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008416 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008417 {"searchpair", 3, 7, f_searchpair},
8418 {"searchpairpos", 3, 7, f_searchpairpos},
8419 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {"server2client", 2, 2, f_server2client},
8421 {"serverlist", 0, 0, f_serverlist},
8422 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008423 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"setcmdpos", 1, 1, f_setcmdpos},
8425 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008426 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008427 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008428 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008429 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008431 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008432 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008434#ifdef FEAT_CRYPT
8435 {"sha256", 1, 1, f_sha256},
8436#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008437 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008438 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008440#ifdef FEAT_FLOAT
8441 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008442 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008443#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008444 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008445 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008446 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008447 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008448 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008449#ifdef FEAT_FLOAT
8450 {"sqrt", 1, 1, f_sqrt},
8451 {"str2float", 1, 1, f_str2float},
8452#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008453 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008454 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008455 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456#ifdef HAVE_STRFTIME
8457 {"strftime", 1, 2, f_strftime},
8458#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008459 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008460 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {"strlen", 1, 1, f_strlen},
8462 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008463 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008465 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008466 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {"substitute", 4, 4, f_substitute},
8468 {"synID", 3, 3, f_synID},
8469 {"synIDattr", 2, 3, f_synIDattr},
8470 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008471 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008472 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008473 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008474 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008475 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008476 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008477 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008478 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008479 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008480#ifdef FEAT_FLOAT
8481 {"tan", 1, 1, f_tan},
8482 {"tanh", 1, 1, f_tanh},
8483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008485 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 {"tolower", 1, 1, f_tolower},
8487 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008488 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008489#ifdef FEAT_FLOAT
8490 {"trunc", 1, 1, f_trunc},
8491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008493 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008494 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008495 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008496 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 {"virtcol", 1, 1, f_virtcol},
8498 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008499 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {"winbufnr", 1, 1, f_winbufnr},
8501 {"wincol", 0, 0, f_wincol},
8502 {"winheight", 1, 1, f_winheight},
8503 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008504 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008506 {"winrestview", 1, 1, f_winrestview},
8507 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008509 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008510 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008511 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512};
8513
8514#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8515
8516/*
8517 * Function given to ExpandGeneric() to obtain the list of internal
8518 * or user defined function names.
8519 */
8520 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008521get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 static int intidx = -1;
8524 char_u *name;
8525
8526 if (idx == 0)
8527 intidx = -1;
8528 if (intidx < 0)
8529 {
8530 name = get_user_func_name(xp, idx);
8531 if (name != NULL)
8532 return name;
8533 }
8534 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8535 {
8536 STRCPY(IObuff, functions[intidx].f_name);
8537 STRCAT(IObuff, "(");
8538 if (functions[intidx].f_max_argc == 0)
8539 STRCAT(IObuff, ")");
8540 return IObuff;
8541 }
8542
8543 return NULL;
8544}
8545
8546/*
8547 * Function given to ExpandGeneric() to obtain the list of internal or
8548 * user defined variable or function names.
8549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008551get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
8553 static int intidx = -1;
8554 char_u *name;
8555
8556 if (idx == 0)
8557 intidx = -1;
8558 if (intidx < 0)
8559 {
8560 name = get_function_name(xp, idx);
8561 if (name != NULL)
8562 return name;
8563 }
8564 return get_user_var_name(xp, ++intidx);
8565}
8566
8567#endif /* FEAT_CMDL_COMPL */
8568
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008569#if defined(EBCDIC) || defined(PROTO)
8570/*
8571 * Compare struct fst by function name.
8572 */
8573 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008574compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008575{
8576 struct fst *p1 = (struct fst *)s1;
8577 struct fst *p2 = (struct fst *)s2;
8578
8579 return STRCMP(p1->f_name, p2->f_name);
8580}
8581
8582/*
8583 * Sort the function table by function name.
8584 * The sorting of the table above is ASCII dependant.
8585 * On machines using EBCDIC we have to sort it.
8586 */
8587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008588sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008589{
8590 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8591
8592 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8593}
8594#endif
8595
8596
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597/*
8598 * Find internal function in table above.
8599 * Return index, or -1 if not found
8600 */
8601 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008602find_internal_func(
8603 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604{
8605 int first = 0;
8606 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8607 int cmp;
8608 int x;
8609
8610 /*
8611 * Find the function name in the table. Binary search.
8612 */
8613 while (first <= last)
8614 {
8615 x = first + ((unsigned)(last - first) >> 1);
8616 cmp = STRCMP(name, functions[x].f_name);
8617 if (cmp < 0)
8618 last = x - 1;
8619 else if (cmp > 0)
8620 first = x + 1;
8621 else
8622 return x;
8623 }
8624 return -1;
8625}
8626
8627/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008628 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8629 * name it contains, otherwise return "name".
8630 */
8631 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008632deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008633{
Bram Moolenaar33570922005-01-25 22:26:29 +00008634 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008635 int cc;
8636
8637 cc = name[*lenp];
8638 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008639 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008640 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008641 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008642 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008643 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008644 {
8645 *lenp = 0;
8646 return (char_u *)""; /* just in case */
8647 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008648 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008649 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008650 }
8651
8652 return name;
8653}
8654
8655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 * Allocate a variable for the result of a function.
8657 * Return OK or FAIL.
8658 */
8659 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008660get_func_tv(
8661 char_u *name, /* name of the function */
8662 int len, /* length of "name" */
8663 typval_T *rettv,
8664 char_u **arg, /* argument, pointing to the '(' */
8665 linenr_T firstline, /* first line of range */
8666 linenr_T lastline, /* last line of range */
8667 int *doesrange, /* return: function handled range */
8668 int evaluate,
8669 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670{
8671 char_u *argp;
8672 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008673 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 int argcount = 0; /* number of arguments found */
8675
8676 /*
8677 * Get the arguments.
8678 */
8679 argp = *arg;
8680 while (argcount < MAX_FUNC_ARGS)
8681 {
8682 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8683 if (*argp == ')' || *argp == ',' || *argp == NUL)
8684 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8686 {
8687 ret = FAIL;
8688 break;
8689 }
8690 ++argcount;
8691 if (*argp != ',')
8692 break;
8693 }
8694 if (*argp == ')')
8695 ++argp;
8696 else
8697 ret = FAIL;
8698
8699 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008700 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008701 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008703 {
8704 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008705 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008706 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008707 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709
8710 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712
8713 *arg = skipwhite(argp);
8714 return ret;
8715}
8716
8717
8718/*
8719 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008720 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008721 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008723 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008724call_func(
8725 char_u *funcname, /* name of the function */
8726 int len, /* length of "name" */
8727 typval_T *rettv, /* return value goes here */
8728 int argcount, /* number of "argvars" */
8729 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008730 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008731 linenr_T firstline, /* first line of range */
8732 linenr_T lastline, /* last line of range */
8733 int *doesrange, /* return: function handled range */
8734 int evaluate,
8735 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736{
8737 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738#define ERROR_UNKNOWN 0
8739#define ERROR_TOOMANY 1
8740#define ERROR_TOOFEW 2
8741#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008742#define ERROR_DICT 4
8743#define ERROR_NONE 5
8744#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 int error = ERROR_NONE;
8746 int i;
8747 int llen;
8748 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749#define FLEN_FIXED 40
8750 char_u fname_buf[FLEN_FIXED + 1];
8751 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008752 char_u *name;
8753
8754 /* Make a copy of the name, if it comes from a funcref variable it could
8755 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008756 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008757 if (name == NULL)
8758 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759
8760 /*
8761 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8762 * Change <SNR>123_name() to K_SNR 123_name().
8763 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 llen = eval_fname_script(name);
8766 if (llen > 0)
8767 {
8768 fname_buf[0] = K_SPECIAL;
8769 fname_buf[1] = KS_EXTRA;
8770 fname_buf[2] = (int)KE_SNR;
8771 i = 3;
8772 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8773 {
8774 if (current_SID <= 0)
8775 error = ERROR_SCRIPT;
8776 else
8777 {
8778 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8779 i = (int)STRLEN(fname_buf);
8780 }
8781 }
8782 if (i + STRLEN(name + llen) < FLEN_FIXED)
8783 {
8784 STRCPY(fname_buf + i, name + llen);
8785 fname = fname_buf;
8786 }
8787 else
8788 {
8789 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8790 if (fname == NULL)
8791 error = ERROR_OTHER;
8792 else
8793 {
8794 mch_memmove(fname, fname_buf, (size_t)i);
8795 STRCPY(fname + i, name + llen);
8796 }
8797 }
8798 }
8799 else
8800 fname = name;
8801
8802 *doesrange = FALSE;
8803
8804
8805 /* execute the function if no errors detected and executing */
8806 if (evaluate && error == ERROR_NONE)
8807 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008808 char_u *rfname = fname;
8809
8810 /* Ignore "g:" before a function name. */
8811 if (fname[0] == 'g' && fname[1] == ':')
8812 rfname = fname + 2;
8813
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008814 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8815 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 error = ERROR_UNKNOWN;
8817
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008818 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 {
8820 /*
8821 * User defined function.
8822 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008823 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008824
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008826 /* Trigger FuncUndefined event, may load the function. */
8827 if (fp == NULL
8828 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008829 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008830 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008832 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008833 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 }
8835#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008836 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008837 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008838 {
8839 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008840 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008841 }
8842
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 if (fp != NULL)
8844 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008845 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008847 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008849 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008851 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008852 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 else
8854 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008855 int did_save_redo = FALSE;
8856
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 /*
8858 * Call the user function.
8859 * Save and restore search patterns, script variables and
8860 * redo buffer.
8861 */
8862 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008863#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008864 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008865#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008866 {
8867 saveRedobuff();
8868 did_save_redo = TRUE;
8869 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008870 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008872 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008873 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8874 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8875 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008876 /* Function was unreferenced while being used, free it
8877 * now. */
8878 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008879 if (did_save_redo)
8880 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 restore_search_patterns();
8882 error = ERROR_NONE;
8883 }
8884 }
8885 }
8886 else
8887 {
8888 /*
8889 * Find the function name in the table, call its implementation.
8890 */
8891 i = find_internal_func(fname);
8892 if (i >= 0)
8893 {
8894 if (argcount < functions[i].f_min_argc)
8895 error = ERROR_TOOFEW;
8896 else if (argcount > functions[i].f_max_argc)
8897 error = ERROR_TOOMANY;
8898 else
8899 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008900 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008901 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 error = ERROR_NONE;
8903 }
8904 }
8905 }
8906 /*
8907 * The function call (or "FuncUndefined" autocommand sequence) might
8908 * have been aborted by an error, an interrupt, or an explicitly thrown
8909 * exception that has not been caught so far. This situation can be
8910 * tested for by calling aborting(). For an error in an internal
8911 * function or for the "E132" error in call_user_func(), however, the
8912 * throw point at which the "force_abort" flag (temporarily reset by
8913 * emsg()) is normally updated has not been reached yet. We need to
8914 * update that flag first to make aborting() reliable.
8915 */
8916 update_force_abort();
8917 }
8918 if (error == ERROR_NONE)
8919 ret = OK;
8920
8921 /*
8922 * Report an error unless the argument evaluation or function call has been
8923 * cancelled due to an aborting error, an interrupt, or an exception.
8924 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008925 if (!aborting())
8926 {
8927 switch (error)
8928 {
8929 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008930 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008931 break;
8932 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008933 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008934 break;
8935 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008936 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008937 name);
8938 break;
8939 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008940 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008941 name);
8942 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008943 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008944 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008945 name);
8946 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008947 }
8948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 if (fname != name && fname != fname_buf)
8951 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008952 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
8954 return ret;
8955}
8956
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008957/*
8958 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008959 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008960 */
8961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008962emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008963{
8964 char_u *p;
8965
8966 if (*name == K_SPECIAL)
8967 p = concat_str((char_u *)"<SNR>", name + 3);
8968 else
8969 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008970 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008971 if (p != name)
8972 vim_free(p);
8973}
8974
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008975/*
8976 * Return TRUE for a non-zero Number and a non-empty String.
8977 */
8978 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008979non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008980{
8981 return ((argvars[0].v_type == VAR_NUMBER
8982 && argvars[0].vval.v_number != 0)
8983 || (argvars[0].v_type == VAR_STRING
8984 && argvars[0].vval.v_string != NULL
8985 && *argvars[0].vval.v_string != NUL));
8986}
8987
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988/*********************************************
8989 * Implementation of the built-in functions
8990 */
8991
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008992#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008993static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008994
8995/*
8996 * Get the float value of "argvars[0]" into "f".
8997 * Returns FAIL when the argument is not a Number or Float.
8998 */
8999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009000get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009001{
9002 if (argvars[0].v_type == VAR_FLOAT)
9003 {
9004 *f = argvars[0].vval.v_float;
9005 return OK;
9006 }
9007 if (argvars[0].v_type == VAR_NUMBER)
9008 {
9009 *f = (float_T)argvars[0].vval.v_number;
9010 return OK;
9011 }
9012 EMSG(_("E808: Number or Float required"));
9013 return FAIL;
9014}
9015
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009016/*
9017 * "abs(expr)" function
9018 */
9019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009020f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009021{
9022 if (argvars[0].v_type == VAR_FLOAT)
9023 {
9024 rettv->v_type = VAR_FLOAT;
9025 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9026 }
9027 else
9028 {
9029 varnumber_T n;
9030 int error = FALSE;
9031
9032 n = get_tv_number_chk(&argvars[0], &error);
9033 if (error)
9034 rettv->vval.v_number = -1;
9035 else if (n > 0)
9036 rettv->vval.v_number = n;
9037 else
9038 rettv->vval.v_number = -n;
9039 }
9040}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009041
9042/*
9043 * "acos()" function
9044 */
9045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009046f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009047{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009048 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009049
9050 rettv->v_type = VAR_FLOAT;
9051 if (get_float_arg(argvars, &f) == OK)
9052 rettv->vval.v_float = acos(f);
9053 else
9054 rettv->vval.v_float = 0.0;
9055}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009056#endif
9057
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009059 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 */
9061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009062f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
Bram Moolenaar33570922005-01-25 22:26:29 +00009064 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009066 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009067 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009069 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009070 && !tv_check_lock(l->lv_lock,
9071 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009072 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009073 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009074 }
9075 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009076 EMSG(_(e_listreq));
9077}
9078
9079/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009080 * "alloc_fail(id, countdown, repeat)" function
9081 */
9082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009083f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009084{
9085 if (argvars[0].v_type != VAR_NUMBER
9086 || argvars[0].vval.v_number <= 0
9087 || argvars[1].v_type != VAR_NUMBER
9088 || argvars[1].vval.v_number < 0
9089 || argvars[2].v_type != VAR_NUMBER)
9090 EMSG(_(e_invarg));
9091 else
9092 {
9093 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009094 if (alloc_fail_id >= aid_last)
9095 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009096 alloc_fail_countdown = argvars[1].vval.v_number;
9097 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009098 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009099 }
9100}
9101
9102/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009103 * "and(expr, expr)" function
9104 */
9105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009106f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009107{
9108 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9109 & get_tv_number_chk(&argvars[1], NULL);
9110}
9111
9112/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009113 * "append(lnum, string/list)" function
9114 */
9115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009116f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009117{
9118 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009119 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009120 list_T *l = NULL;
9121 listitem_T *li = NULL;
9122 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009123 long added = 0;
9124
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009125 /* When coming here from Insert mode, sync undo, so that this can be
9126 * undone separately from what was previously inserted. */
9127 if (u_sync_once == 2)
9128 {
9129 u_sync_once = 1; /* notify that u_sync() was called */
9130 u_sync(TRUE);
9131 }
9132
Bram Moolenaar0d660222005-01-07 21:51:51 +00009133 lnum = get_tv_lnum(argvars);
9134 if (lnum >= 0
9135 && lnum <= curbuf->b_ml.ml_line_count
9136 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009137 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009138 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009139 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009140 l = argvars[1].vval.v_list;
9141 if (l == NULL)
9142 return;
9143 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009144 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009145 for (;;)
9146 {
9147 if (l == NULL)
9148 tv = &argvars[1]; /* append a string */
9149 else if (li == NULL)
9150 break; /* end of list */
9151 else
9152 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009153 line = get_tv_string_chk(tv);
9154 if (line == NULL) /* type error */
9155 {
9156 rettv->vval.v_number = 1; /* Failed */
9157 break;
9158 }
9159 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009160 ++added;
9161 if (l == NULL)
9162 break;
9163 li = li->li_next;
9164 }
9165
9166 appended_lines_mark(lnum, added);
9167 if (curwin->w_cursor.lnum > lnum)
9168 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009170 else
9171 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172}
9173
9174/*
9175 * "argc()" function
9176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009178f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181}
9182
9183/*
9184 * "argidx()" function
9185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009187f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190}
9191
9192/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009193 * "arglistid()" function
9194 */
9195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009196f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009197{
9198 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009199
9200 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009201 wp = find_tabwin(&argvars[0], &argvars[1]);
9202 if (wp != NULL)
9203 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009204}
9205
9206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 * "argv(nr)" function
9208 */
9209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009210f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211{
9212 int idx;
9213
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009214 if (argvars[0].v_type != VAR_UNKNOWN)
9215 {
9216 idx = get_tv_number_chk(&argvars[0], NULL);
9217 if (idx >= 0 && idx < ARGCOUNT)
9218 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9219 else
9220 rettv->vval.v_string = NULL;
9221 rettv->v_type = VAR_STRING;
9222 }
9223 else if (rettv_list_alloc(rettv) == OK)
9224 for (idx = 0; idx < ARGCOUNT; ++idx)
9225 list_append_string(rettv->vval.v_list,
9226 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227}
9228
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009229static void prepare_assert_error(garray_T*gap);
9230static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9231static void assert_error(garray_T *gap);
9232static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009233
9234/*
9235 * Prepare "gap" for an assert error and add the sourcing position.
9236 */
9237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009238prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009239{
9240 char buf[NUMBUFLEN];
9241
9242 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009243 if (sourcing_name != NULL)
9244 {
9245 ga_concat(gap, sourcing_name);
9246 if (sourcing_lnum > 0)
9247 ga_concat(gap, (char_u *)" ");
9248 }
9249 if (sourcing_lnum > 0)
9250 {
9251 sprintf(buf, "line %ld", (long)sourcing_lnum);
9252 ga_concat(gap, (char_u *)buf);
9253 }
9254 if (sourcing_name != NULL || sourcing_lnum > 0)
9255 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009256}
9257
9258/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009259 * Append "str" to "gap", escaping unprintable characters.
9260 * Changes NL to \n, CR to \r, etc.
9261 */
9262 static void
9263ga_concat_esc(garray_T *gap, char_u *str)
9264{
9265 char_u *p;
9266 char_u buf[NUMBUFLEN];
9267
9268 for (p = str; *p != NUL; ++p)
9269 switch (*p)
9270 {
9271 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9272 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9273 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9274 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9275 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9276 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9277 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9278 default:
9279 if (*p < ' ')
9280 {
9281 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9282 ga_concat(gap, buf);
9283 }
9284 else
9285 ga_append(gap, *p);
9286 break;
9287 }
9288}
9289
9290/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009291 * Fill "gap" with information about an assert error.
9292 */
9293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009294fill_assert_error(
9295 garray_T *gap,
9296 typval_T *opt_msg_tv,
9297 char_u *exp_str,
9298 typval_T *exp_tv,
9299 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009300{
9301 char_u numbuf[NUMBUFLEN];
9302 char_u *tofree;
9303
9304 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9305 {
9306 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9307 vim_free(tofree);
9308 }
9309 else
9310 {
9311 ga_concat(gap, (char_u *)"Expected ");
9312 if (exp_str == NULL)
9313 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009314 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009315 vim_free(tofree);
9316 }
9317 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009318 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009319 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009320 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009321 vim_free(tofree);
9322 }
9323}
Bram Moolenaar43345542015-11-29 17:35:35 +01009324
9325/*
9326 * Add an assert error to v:errors.
9327 */
9328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009329assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009330{
9331 struct vimvar *vp = &vimvars[VV_ERRORS];
9332
9333 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9334 /* Make sure v:errors is a list. */
9335 set_vim_var_list(VV_ERRORS, list_alloc());
9336 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9337}
9338
Bram Moolenaar43345542015-11-29 17:35:35 +01009339/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009340 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009341 */
9342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009343f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009344{
9345 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009346
9347 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9348 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009349 prepare_assert_error(&ga);
9350 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9351 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009352 ga_clear(&ga);
9353 }
9354}
9355
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009356/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009357 * "assert_exception(string[, msg])" function
9358 */
9359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009360f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009361{
9362 garray_T ga;
9363 char *error;
9364
9365 error = (char *)get_tv_string_chk(&argvars[0]);
9366 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9367 {
9368 prepare_assert_error(&ga);
9369 ga_concat(&ga, (char_u *)"v:exception is not set");
9370 assert_error(&ga);
9371 ga_clear(&ga);
9372 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009373 else if (error != NULL
9374 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009375 {
9376 prepare_assert_error(&ga);
9377 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9378 &vimvars[VV_EXCEPTION].vv_tv);
9379 assert_error(&ga);
9380 ga_clear(&ga);
9381 }
9382}
9383
9384/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009385 * "assert_fails(cmd [, error])" function
9386 */
9387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009388f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009389{
9390 char_u *cmd = get_tv_string_chk(&argvars[0]);
9391 garray_T ga;
9392
9393 called_emsg = FALSE;
9394 suppress_errthrow = TRUE;
9395 emsg_silent = TRUE;
9396 do_cmdline_cmd(cmd);
9397 if (!called_emsg)
9398 {
9399 prepare_assert_error(&ga);
9400 ga_concat(&ga, (char_u *)"command did not fail: ");
9401 ga_concat(&ga, cmd);
9402 assert_error(&ga);
9403 ga_clear(&ga);
9404 }
9405 else if (argvars[1].v_type != VAR_UNKNOWN)
9406 {
9407 char_u buf[NUMBUFLEN];
9408 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9409
9410 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9411 {
9412 prepare_assert_error(&ga);
9413 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9414 &vimvars[VV_ERRMSG].vv_tv);
9415 assert_error(&ga);
9416 ga_clear(&ga);
9417 }
9418 }
9419
9420 called_emsg = FALSE;
9421 suppress_errthrow = FALSE;
9422 emsg_silent = FALSE;
9423 emsg_on_display = FALSE;
9424 set_vim_var_string(VV_ERRMSG, NULL, 0);
9425}
9426
9427/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009428 * Common for assert_true() and assert_false().
9429 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009431assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009432{
9433 int error = FALSE;
9434 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009435
Bram Moolenaar37127922016-02-06 20:29:28 +01009436 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009437 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009438 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009439 if (argvars[0].v_type != VAR_NUMBER
9440 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9441 || error)
9442 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009443 prepare_assert_error(&ga);
9444 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009445 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009446 NULL, &argvars[0]);
9447 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009448 ga_clear(&ga);
9449 }
9450}
9451
9452/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009453 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009454 */
9455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009456f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009457{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009458 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009459}
9460
9461/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009462 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009463 */
9464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009465f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009466{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009467 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009468}
9469
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009470#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009471/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009472 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009473 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009475f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009476{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009477 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009478
9479 rettv->v_type = VAR_FLOAT;
9480 if (get_float_arg(argvars, &f) == OK)
9481 rettv->vval.v_float = asin(f);
9482 else
9483 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009484}
9485
9486/*
9487 * "atan()" function
9488 */
9489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009490f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009491{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009492 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009493
9494 rettv->v_type = VAR_FLOAT;
9495 if (get_float_arg(argvars, &f) == OK)
9496 rettv->vval.v_float = atan(f);
9497 else
9498 rettv->vval.v_float = 0.0;
9499}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009500
9501/*
9502 * "atan2()" function
9503 */
9504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009505f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009506{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009507 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009508
9509 rettv->v_type = VAR_FLOAT;
9510 if (get_float_arg(argvars, &fx) == OK
9511 && get_float_arg(&argvars[1], &fy) == OK)
9512 rettv->vval.v_float = atan2(fx, fy);
9513 else
9514 rettv->vval.v_float = 0.0;
9515}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009516#endif
9517
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518/*
9519 * "browse(save, title, initdir, default)" function
9520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009522f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523{
9524#ifdef FEAT_BROWSE
9525 int save;
9526 char_u *title;
9527 char_u *initdir;
9528 char_u *defname;
9529 char_u buf[NUMBUFLEN];
9530 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009531 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009533 save = get_tv_number_chk(&argvars[0], &error);
9534 title = get_tv_string_chk(&argvars[1]);
9535 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9536 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009538 if (error || title == NULL || initdir == NULL || defname == NULL)
9539 rettv->vval.v_string = NULL;
9540 else
9541 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009542 do_browse(save ? BROWSE_SAVE : 0,
9543 title, defname, NULL, initdir, NULL, curbuf);
9544#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009546#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009548}
9549
9550/*
9551 * "browsedir(title, initdir)" function
9552 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009554f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009555{
9556#ifdef FEAT_BROWSE
9557 char_u *title;
9558 char_u *initdir;
9559 char_u buf[NUMBUFLEN];
9560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009561 title = get_tv_string_chk(&argvars[0]);
9562 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009564 if (title == NULL || initdir == NULL)
9565 rettv->vval.v_string = NULL;
9566 else
9567 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009568 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573}
9574
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009575static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009576
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577/*
9578 * Find a buffer by number or exact name.
9579 */
9580 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009581find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582{
9583 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009585 if (avar->v_type == VAR_NUMBER)
9586 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009587 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009589 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009590 if (buf == NULL)
9591 {
9592 /* No full path name match, try a match with a URL or a "nofile"
9593 * buffer, these don't use the full path. */
9594 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9595 if (buf->b_fname != NULL
9596 && (path_with_url(buf->b_fname)
9597#ifdef FEAT_QUICKFIX
9598 || bt_nofile(buf)
9599#endif
9600 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009601 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009602 break;
9603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 }
9605 return buf;
9606}
9607
9608/*
9609 * "bufexists(expr)" function
9610 */
9611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009612f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615}
9616
9617/*
9618 * "buflisted(expr)" function
9619 */
9620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009621f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622{
9623 buf_T *buf;
9624
9625 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627}
9628
9629/*
9630 * "bufloaded(expr)" function
9631 */
9632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009633f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
9635 buf_T *buf;
9636
9637 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639}
9640
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009641static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009642
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643/*
9644 * Get buffer by number or pattern.
9645 */
9646 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009647get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 int save_magic;
9651 char_u *save_cpo;
9652 buf_T *buf;
9653
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009654 if (tv->v_type == VAR_NUMBER)
9655 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009656 if (tv->v_type != VAR_STRING)
9657 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 if (name == NULL || *name == NUL)
9659 return curbuf;
9660 if (name[0] == '$' && name[1] == NUL)
9661 return lastbuf;
9662
9663 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9664 save_magic = p_magic;
9665 p_magic = TRUE;
9666 save_cpo = p_cpo;
9667 p_cpo = (char_u *)"";
9668
9669 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009670 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671
9672 p_magic = save_magic;
9673 p_cpo = save_cpo;
9674
9675 /* If not found, try expanding the name, like done for bufexists(). */
9676 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678
9679 return buf;
9680}
9681
9682/*
9683 * "bufname(expr)" function
9684 */
9685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009686f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
9688 buf_T *buf;
9689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009690 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009692 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009693 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 --emsg_off;
9699}
9700
9701/*
9702 * "bufnr(expr)" function
9703 */
9704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009705f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
9707 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009708 int error = FALSE;
9709 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009711 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009713 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009714 --emsg_off;
9715
9716 /* If the buffer isn't found and the second argument is not zero create a
9717 * new buffer. */
9718 if (buf == NULL
9719 && argvars[1].v_type != VAR_UNKNOWN
9720 && get_tv_number_chk(&argvars[1], &error) != 0
9721 && !error
9722 && (name = get_tv_string_chk(&argvars[0])) != NULL
9723 && !error)
9724 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9725
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730}
9731
9732/*
9733 * "bufwinnr(nr)" function
9734 */
9735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009736f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737{
9738#ifdef FEAT_WINDOWS
9739 win_T *wp;
9740 int winnr = 0;
9741#endif
9742 buf_T *buf;
9743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009744 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009746 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747#ifdef FEAT_WINDOWS
9748 for (wp = firstwin; wp; wp = wp->w_next)
9749 {
9750 ++winnr;
9751 if (wp->w_buffer == buf)
9752 break;
9753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757#endif
9758 --emsg_off;
9759}
9760
9761/*
9762 * "byte2line(byte)" function
9763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009765f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
9767#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769#else
9770 long boff = 0;
9771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009772 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 (linenr_T)0, &boff);
9778#endif
9779}
9780
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009782byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009783{
9784#ifdef FEAT_MBYTE
9785 char_u *t;
9786#endif
9787 char_u *str;
9788 long idx;
9789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009790 str = get_tv_string_chk(&argvars[0]);
9791 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009793 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009794 return;
9795
9796#ifdef FEAT_MBYTE
9797 t = str;
9798 for ( ; idx > 0; idx--)
9799 {
9800 if (*t == NUL) /* EOL reached */
9801 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009802 if (enc_utf8 && comp)
9803 t += utf_ptr2len(t);
9804 else
9805 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009806 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009807 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009808#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009809 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009811#endif
9812}
9813
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009814/*
9815 * "byteidx()" function
9816 */
9817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009818f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009819{
9820 byteidx(argvars, rettv, FALSE);
9821}
9822
9823/*
9824 * "byteidxcomp()" function
9825 */
9826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009827f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009828{
9829 byteidx(argvars, rettv, TRUE);
9830}
9831
Bram Moolenaardb913952012-06-29 12:54:53 +02009832 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009833func_call(
9834 char_u *name,
9835 typval_T *args,
9836 dict_T *selfdict,
9837 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009838{
9839 listitem_T *item;
9840 typval_T argv[MAX_FUNC_ARGS + 1];
9841 int argc = 0;
9842 int dummy;
9843 int r = 0;
9844
9845 for (item = args->vval.v_list->lv_first; item != NULL;
9846 item = item->li_next)
9847 {
9848 if (argc == MAX_FUNC_ARGS)
9849 {
9850 EMSG(_("E699: Too many arguments"));
9851 break;
9852 }
9853 /* Make a copy of each argument. This is needed to be able to set
9854 * v_lock to VAR_FIXED in the copy without changing the original list.
9855 */
9856 copy_tv(&item->li_tv, &argv[argc++]);
9857 }
9858
9859 if (item == NULL)
9860 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9861 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9862 &dummy, TRUE, selfdict);
9863
9864 /* Free the arguments. */
9865 while (argc > 0)
9866 clear_tv(&argv[--argc]);
9867
9868 return r;
9869}
9870
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009871/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009872 * "call(func, arglist)" function
9873 */
9874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009875f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009876{
9877 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009878 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009879
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009880 if (argvars[1].v_type != VAR_LIST)
9881 {
9882 EMSG(_(e_listreq));
9883 return;
9884 }
9885 if (argvars[1].vval.v_list == NULL)
9886 return;
9887
9888 if (argvars[0].v_type == VAR_FUNC)
9889 func = argvars[0].vval.v_string;
9890 else
9891 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009892 if (*func == NUL)
9893 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009894
Bram Moolenaare9a41262005-01-15 22:18:47 +00009895 if (argvars[2].v_type != VAR_UNKNOWN)
9896 {
9897 if (argvars[2].v_type != VAR_DICT)
9898 {
9899 EMSG(_(e_dictreq));
9900 return;
9901 }
9902 selfdict = argvars[2].vval.v_dict;
9903 }
9904
Bram Moolenaardb913952012-06-29 12:54:53 +02009905 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009906}
9907
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009908#ifdef FEAT_FLOAT
9909/*
9910 * "ceil({float})" function
9911 */
9912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009913f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009914{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009915 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009916
9917 rettv->v_type = VAR_FLOAT;
9918 if (get_float_arg(argvars, &f) == OK)
9919 rettv->vval.v_float = ceil(f);
9920 else
9921 rettv->vval.v_float = 0.0;
9922}
9923#endif
9924
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009925#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009926/*
9927 * Get a callback from "arg". It can be a Funcref or a function name.
9928 * When "arg" is zero return an empty string.
9929 * Return NULL for an invalid argument.
9930 */
9931 static char_u *
9932get_callback(typval_T *arg)
9933{
9934 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9935 return arg->vval.v_string;
9936 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9937 return (char_u *)"";
9938 EMSG(_("E999: Invalid callback argument"));
9939 return NULL;
9940}
9941
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009942 static int
9943handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9944{
9945 char_u *val = get_tv_string(item);
9946
9947 opt->jo_set |= jo;
9948 if (STRCMP(val, "nl") == 0)
9949 *modep = MODE_NL;
9950 else if (STRCMP(val, "raw") == 0)
9951 *modep = MODE_RAW;
9952 else if (STRCMP(val, "js") == 0)
9953 *modep = MODE_JS;
9954 else if (STRCMP(val, "json") == 0)
9955 *modep = MODE_JSON;
9956 else
9957 {
9958 EMSG2(_(e_invarg2), val);
9959 return FAIL;
9960 }
9961 return OK;
9962}
9963
9964 static void
9965clear_job_options(jobopt_T *opt)
9966{
9967 vim_memset(opt, 0, sizeof(jobopt_T));
9968}
9969
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009970/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009971 * Get the option entries from the dict in "tv", parse them and put the result
9972 * in "opt".
9973 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009974 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009975 */
9976 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009977get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009978{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009979 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01009980 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009981 dict_T *dict;
9982 int todo;
9983 hashitem_T *hi;
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009984
Bram Moolenaar8600ace2016-02-19 23:31:40 +01009985 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009986 if (tv->v_type == VAR_UNKNOWN)
9987 return OK;
9988 if (tv->v_type != VAR_DICT)
9989 {
9990 EMSG(_(e_invarg));
9991 return FAIL;
9992 }
9993 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009994 if (dict == NULL)
9995 return OK;
9996
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009997 todo = (int)dict->dv_hashtab.ht_used;
9998 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
9999 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010000 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010001 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010002
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010003 if (STRCMP(hi->hi_key, "mode") == 0)
10004 {
10005 if (!(supported & JO_MODE))
10006 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010007 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010008 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010009 }
10010 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10011 {
10012 if (!(supported & JO_IN_MODE))
10013 break;
10014 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10015 == FAIL)
10016 return FAIL;
10017 }
10018 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10019 {
10020 if (!(supported & JO_OUT_MODE))
10021 break;
10022 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10023 == FAIL)
10024 return FAIL;
10025 }
10026 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10027 {
10028 if (!(supported & JO_ERR_MODE))
10029 break;
10030 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10031 == FAIL)
10032 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010033 }
10034 else if (STRCMP(hi->hi_key, "callback") == 0)
10035 {
10036 if (!(supported & JO_CALLBACK))
10037 break;
10038 opt->jo_set |= JO_CALLBACK;
10039 opt->jo_callback = get_callback(item);
10040 if (opt->jo_callback == NULL)
10041 {
10042 EMSG2(_(e_invarg2), "callback");
10043 return FAIL;
10044 }
10045 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010046 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10047 {
10048 if (!(supported & JO_OUT_CALLBACK))
10049 break;
10050 opt->jo_set |= JO_OUT_CALLBACK;
10051 opt->jo_out_cb = get_callback(item);
10052 if (opt->jo_out_cb == NULL)
10053 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010054 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010055 return FAIL;
10056 }
10057 }
10058 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10059 {
10060 if (!(supported & JO_ERR_CALLBACK))
10061 break;
10062 opt->jo_set |= JO_ERR_CALLBACK;
10063 opt->jo_err_cb = get_callback(item);
10064 if (opt->jo_err_cb == NULL)
10065 {
10066 EMSG2(_(e_invarg2), "err-cb");
10067 return FAIL;
10068 }
10069 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010070 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10071 {
10072 if (!(supported & JO_CLOSE_CALLBACK))
10073 break;
10074 opt->jo_set |= JO_CLOSE_CALLBACK;
10075 opt->jo_close_cb = get_callback(item);
10076 if (opt->jo_close_cb == NULL)
10077 {
10078 EMSG2(_(e_invarg2), "close-cb");
10079 return FAIL;
10080 }
10081 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010082 else if (STRCMP(hi->hi_key, "waittime") == 0)
10083 {
10084 if (!(supported & JO_WAITTIME))
10085 break;
10086 opt->jo_set |= JO_WAITTIME;
10087 opt->jo_waittime = get_tv_number(item);
10088 }
10089 else if (STRCMP(hi->hi_key, "timeout") == 0)
10090 {
10091 if (!(supported & JO_TIMEOUT))
10092 break;
10093 opt->jo_set |= JO_TIMEOUT;
10094 opt->jo_timeout = get_tv_number(item);
10095 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010096 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10097 {
10098 if (!(supported & JO_OUT_TIMEOUT))
10099 break;
10100 opt->jo_set |= JO_OUT_TIMEOUT;
10101 opt->jo_out_timeout = get_tv_number(item);
10102 }
10103 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10104 {
10105 if (!(supported & JO_ERR_TIMEOUT))
10106 break;
10107 opt->jo_set |= JO_ERR_TIMEOUT;
10108 opt->jo_err_timeout = get_tv_number(item);
10109 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010110 else if (STRCMP(hi->hi_key, "part") == 0)
10111 {
10112 if (!(supported & JO_PART))
10113 break;
10114 opt->jo_set |= JO_PART;
10115 val = get_tv_string(item);
10116 if (STRCMP(val, "err") == 0)
10117 opt->jo_part = PART_ERR;
10118 else
10119 {
10120 EMSG2(_(e_invarg2), val);
10121 return FAIL;
10122 }
10123 }
10124 else if (STRCMP(hi->hi_key, "id") == 0)
10125 {
10126 if (!(supported & JO_ID))
10127 break;
10128 opt->jo_set |= JO_ID;
10129 opt->jo_id = get_tv_number(item);
10130 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010131 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10132 {
10133 if (!(supported & JO_STOPONEXIT))
10134 break;
10135 opt->jo_set |= JO_STOPONEXIT;
10136 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10137 opt->jo_soe_buf);
10138 if (opt->jo_stoponexit == NULL)
10139 {
10140 EMSG2(_(e_invarg2), "stoponexit");
10141 return FAIL;
10142 }
10143 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010144 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10145 {
10146 if (!(supported & JO_EXIT_CB))
10147 break;
10148 opt->jo_set |= JO_EXIT_CB;
10149 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010150 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010151 {
10152 EMSG2(_(e_invarg2), "exit-cb");
10153 return FAIL;
10154 }
10155 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010156 else
10157 break;
10158 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010159 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010160 if (todo > 0)
10161 {
10162 EMSG2(_(e_invarg2), hi->hi_key);
10163 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010164 }
10165
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010166 return OK;
10167}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010168#endif
10169
10170#ifdef FEAT_CHANNEL
10171/*
10172 * Get the channel from the argument.
10173 * Returns NULL if the handle is invalid.
10174 */
10175 static channel_T *
10176get_channel_arg(typval_T *tv)
10177{
10178 channel_T *channel;
10179
10180 if (tv->v_type != VAR_CHANNEL)
10181 {
10182 EMSG2(_(e_invarg2), get_tv_string(tv));
10183 return NULL;
10184 }
10185 channel = tv->vval.v_channel;
10186
10187 if (channel == NULL || !channel_is_open(channel))
10188 {
10189 EMSG(_("E906: not an open channel"));
10190 return NULL;
10191 }
10192 return channel;
10193}
10194
10195/*
10196 * "ch_close()" function
10197 */
10198 static void
10199f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10200{
10201 channel_T *channel = get_channel_arg(&argvars[0]);
10202
10203 if (channel != NULL)
10204 channel_close(channel);
10205}
10206
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010207# ifdef FEAT_JOB
10208/*
10209 * "ch_getjob()" function
10210 */
10211 static void
10212f_ch_getjob(typval_T *argvars, typval_T *rettv)
10213{
10214 channel_T *channel = get_channel_arg(&argvars[0]);
10215
10216 if (channel != NULL)
10217 {
10218 rettv->v_type = VAR_JOB;
10219 rettv->vval.v_job = channel->ch_job;
10220 if (channel->ch_job != NULL)
10221 ++channel->ch_job->jv_refcount;
10222 }
10223}
10224# endif
10225
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010226/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010227 * "ch_log()" function
10228 */
10229 static void
10230f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10231{
10232 char_u *msg = get_tv_string(&argvars[0]);
10233 channel_T *channel = NULL;
10234
10235 if (argvars[1].v_type != VAR_UNKNOWN)
10236 channel = get_channel_arg(&argvars[1]);
10237
10238 ch_log(channel, (char *)msg);
10239}
10240
10241/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010242 * "ch_logfile()" function
10243 */
10244 static void
10245f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10246{
10247 char_u *fname;
10248 char_u *opt = (char_u *)"";
10249 char_u buf[NUMBUFLEN];
10250 FILE *file = NULL;
10251
10252 fname = get_tv_string(&argvars[0]);
10253 if (argvars[1].v_type == VAR_STRING)
10254 opt = get_tv_string_buf(&argvars[1], buf);
10255 if (*fname != NUL)
10256 {
10257 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10258 if (file == NULL)
10259 {
10260 EMSG2(_(e_notopen), fname);
10261 return;
10262 }
10263 }
10264 ch_logfile(file);
10265}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010266
10267/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010268 * "ch_open()" function
10269 */
10270 static void
10271f_ch_open(typval_T *argvars, typval_T *rettv)
10272{
10273 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010274 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010275 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010276 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010277 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010278 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010279
10280 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010281 rettv->v_type = VAR_CHANNEL;
10282 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010283
10284 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010285 if (argvars[1].v_type != VAR_UNKNOWN
10286 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010287 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010288 EMSG(_(e_invarg));
10289 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010290 }
10291
10292 /* parse address */
10293 p = vim_strchr(address, ':');
10294 if (p == NULL)
10295 {
10296 EMSG2(_(e_invarg2), address);
10297 return;
10298 }
10299 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010300 port = strtol((char *)p, &rest, 10);
10301 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010302 {
10303 p[-1] = ':';
10304 EMSG2(_(e_invarg2), address);
10305 return;
10306 }
10307
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010308 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010309 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010310 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010311 opt.jo_timeout = 2000;
10312 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010313 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010314 return;
10315 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010316 {
10317 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010318 return;
10319 }
10320
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010321 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010322 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010323 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010324 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010325 opt.jo_set = JO_ALL;
10326 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010327 }
10328}
10329
10330/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010331 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010332 */
10333 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010334common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010335{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010336 channel_T *channel;
10337 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010338 jobopt_T opt;
10339 int mode;
10340 int timeout;
10341 int id = -1;
10342 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010343
10344 /* return an empty string by default */
10345 rettv->v_type = VAR_STRING;
10346 rettv->vval.v_string = NULL;
10347
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010348 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010349 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10350 == FAIL)
10351 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010352
Bram Moolenaar77073442016-02-13 23:23:53 +010010353 channel = get_channel_arg(&argvars[0]);
10354 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010355 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010356 if (opt.jo_set & JO_PART)
10357 part = opt.jo_part;
10358 else
10359 part = channel_part_read(channel);
10360 mode = channel_get_mode(channel, part);
10361 timeout = channel_get_timeout(channel, part);
10362 if (opt.jo_set & JO_TIMEOUT)
10363 timeout = opt.jo_timeout;
10364
10365 if (raw || mode == MODE_RAW || mode == MODE_NL)
10366 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10367 else
10368 {
10369 if (opt.jo_set & JO_ID)
10370 id = opt.jo_id;
10371 channel_read_json_block(channel, part, timeout, id, &listtv);
10372 if (listtv != NULL)
10373 *rettv = *listtv;
10374 else
10375 {
10376 rettv->v_type = VAR_SPECIAL;
10377 rettv->vval.v_number = VVAL_NONE;
10378 }
10379 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010380 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010381}
10382
10383/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010384 * "ch_read()" function
10385 */
10386 static void
10387f_ch_read(typval_T *argvars, typval_T *rettv)
10388{
10389 common_channel_read(argvars, rettv, FALSE);
10390}
10391
10392/*
10393 * "ch_readraw()" function
10394 */
10395 static void
10396f_ch_readraw(typval_T *argvars, typval_T *rettv)
10397{
10398 common_channel_read(argvars, rettv, TRUE);
10399}
10400
10401/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010402 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010403 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010404 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010405 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010406 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010407 static channel_T *
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010408send_common(typval_T *argvars, char_u *text, int id, char *fun, int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010409{
Bram Moolenaar77073442016-02-13 23:23:53 +010010410 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010411 jobopt_T opt;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010412 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010413
Bram Moolenaar77073442016-02-13 23:23:53 +010010414 channel = get_channel_arg(&argvars[0]);
10415 if (channel == NULL)
10416 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010417 part_send = channel_part_send(channel);
10418 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010419
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010420 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010421 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10422 return NULL;
10423
Bram Moolenaara07fec92016-02-05 21:04:08 +010010424 /* Set the callback. An empty callback means no callback and not reading
10425 * the response. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010426 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010427 channel_set_req_callback(channel, part_send, opt.jo_callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010428
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010429 if (channel_send(channel, part_send, text, fun) == OK
10430 && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010431 return channel;
10432 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010433}
10434
10435/*
10436 * "ch_sendexpr()" function
10437 */
10438 static void
10439f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10440{
10441 char_u *text;
10442 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010443 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010444 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010445 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010446 int part_send;
10447 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010448 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010449
10450 /* return an empty string by default */
10451 rettv->v_type = VAR_STRING;
10452 rettv->vval.v_string = NULL;
10453
Bram Moolenaar77073442016-02-13 23:23:53 +010010454 channel = get_channel_arg(&argvars[0]);
10455 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010456 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010457 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010458
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010459 ch_mode = channel_get_mode(channel, part_send);
10460 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010461 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010462 EMSG(_("E912: cannot use ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010463 return;
10464 }
10465
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010466 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010467 text = json_encode_nr_expr(id, &argvars[1],
10468 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010469 if (text == NULL)
10470 return;
10471
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010472 channel = send_common(argvars, text, id, "sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010473 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010474 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010475 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010476 /* TODO: timeout from options */
10477 timeout = channel_get_timeout(channel, part_read);
10478 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10479 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010480 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010481 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010482
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010483 /* Move the item from the list and then change the type to
10484 * avoid the value being freed. */
10485 *rettv = list->lv_last->li_tv;
10486 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010487 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010488 }
10489 }
10490}
10491
10492/*
10493 * "ch_sendraw()" function
10494 */
10495 static void
10496f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10497{
10498 char_u buf[NUMBUFLEN];
10499 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010500 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010501 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010502 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010503
10504 /* return an empty string by default */
10505 rettv->v_type = VAR_STRING;
10506 rettv->vval.v_string = NULL;
10507
10508 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010509 channel = send_common(argvars, text, 0, "sendraw", &part_read);
Bram Moolenaar77073442016-02-13 23:23:53 +010010510 if (channel != NULL)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010511 {
10512 /* TODO: timeout from options */
10513 timeout = channel_get_timeout(channel, part_read);
10514 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10515 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010516}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010517
10518/*
10519 * "ch_setoptions()" function
10520 */
10521 static void
10522f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10523{
10524 channel_T *channel;
10525 jobopt_T opt;
10526
10527 channel = get_channel_arg(&argvars[0]);
10528 if (channel == NULL)
10529 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010530 clear_job_options(&opt);
10531 if (get_job_options(&argvars[1], &opt,
10532 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010533 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010534 channel_set_options(channel, &opt);
10535}
10536
10537/*
10538 * "ch_status()" function
10539 */
10540 static void
10541f_ch_status(typval_T *argvars, typval_T *rettv)
10542{
10543 /* return an empty string by default */
10544 rettv->v_type = VAR_STRING;
10545
10546 if (argvars[0].v_type != VAR_CHANNEL)
10547 {
10548 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10549 rettv->vval.v_string = NULL;
10550 }
10551 else
10552 rettv->vval.v_string = vim_strsave(
10553 (char_u *)channel_status(argvars[0].vval.v_channel));
10554}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010555#endif
10556
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010557/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010558 * "changenr()" function
10559 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010561f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010562{
10563 rettv->vval.v_number = curbuf->b_u_seq_cur;
10564}
10565
10566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 * "char2nr(string)" function
10568 */
10569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010570f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571{
10572#ifdef FEAT_MBYTE
10573 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010574 {
10575 int utf8 = 0;
10576
10577 if (argvars[1].v_type != VAR_UNKNOWN)
10578 utf8 = get_tv_number_chk(&argvars[1], NULL);
10579
10580 if (utf8)
10581 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10582 else
10583 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 else
10586#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010587 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588}
10589
10590/*
10591 * "cindent(lnum)" function
10592 */
10593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010594f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595{
10596#ifdef FEAT_CINDENT
10597 pos_T pos;
10598 linenr_T lnum;
10599
10600 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010601 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10603 {
10604 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010605 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 curwin->w_cursor = pos;
10607 }
10608 else
10609#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010610 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611}
10612
10613/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010614 * "clearmatches()" function
10615 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010617f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010618{
10619#ifdef FEAT_SEARCH_EXTRA
10620 clear_matches(curwin);
10621#endif
10622}
10623
10624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 * "col(string)" function
10626 */
10627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010628f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629{
10630 colnr_T col = 0;
10631 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010632 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010634 fp = var2fpos(&argvars[0], FALSE, &fnum);
10635 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 {
10637 if (fp->col == MAXCOL)
10638 {
10639 /* '> can be MAXCOL, get the length of the line then */
10640 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010641 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 else
10643 col = MAXCOL;
10644 }
10645 else
10646 {
10647 col = fp->col + 1;
10648#ifdef FEAT_VIRTUALEDIT
10649 /* col(".") when the cursor is on the NUL at the end of the line
10650 * because of "coladd" can be seen as an extra column. */
10651 if (virtual_active() && fp == &curwin->w_cursor)
10652 {
10653 char_u *p = ml_get_cursor();
10654
10655 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10656 curwin->w_virtcol - curwin->w_cursor.coladd))
10657 {
10658# ifdef FEAT_MBYTE
10659 int l;
10660
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010661 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 col += l;
10663# else
10664 if (*p != NUL && p[1] == NUL)
10665 ++col;
10666# endif
10667 }
10668 }
10669#endif
10670 }
10671 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010672 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673}
10674
Bram Moolenaar572cb562005-08-05 21:35:02 +000010675#if defined(FEAT_INS_EXPAND)
10676/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010677 * "complete()" function
10678 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010680f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010681{
10682 int startcol;
10683
10684 if ((State & INSERT) == 0)
10685 {
10686 EMSG(_("E785: complete() can only be used in Insert mode"));
10687 return;
10688 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010689
10690 /* Check for undo allowed here, because if something was already inserted
10691 * the line was already saved for undo and this check isn't done. */
10692 if (!undo_allowed())
10693 return;
10694
Bram Moolenaarade00832006-03-10 21:46:58 +000010695 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10696 {
10697 EMSG(_(e_invarg));
10698 return;
10699 }
10700
10701 startcol = get_tv_number_chk(&argvars[0], NULL);
10702 if (startcol <= 0)
10703 return;
10704
10705 set_completion(startcol - 1, argvars[1].vval.v_list);
10706}
10707
10708/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010709 * "complete_add()" function
10710 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010712f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010713{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010714 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010715}
10716
10717/*
10718 * "complete_check()" function
10719 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010721f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010722{
10723 int saved = RedrawingDisabled;
10724
10725 RedrawingDisabled = 0;
10726 ins_compl_check_keys(0);
10727 rettv->vval.v_number = compl_interrupted;
10728 RedrawingDisabled = saved;
10729}
10730#endif
10731
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732/*
10733 * "confirm(message, buttons[, default [, type]])" function
10734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010736f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737{
10738#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10739 char_u *message;
10740 char_u *buttons = NULL;
10741 char_u buf[NUMBUFLEN];
10742 char_u buf2[NUMBUFLEN];
10743 int def = 1;
10744 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010745 char_u *typestr;
10746 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010748 message = get_tv_string_chk(&argvars[0]);
10749 if (message == NULL)
10750 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010751 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010753 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10754 if (buttons == NULL)
10755 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010756 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010758 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010759 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010761 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10762 if (typestr == NULL)
10763 error = TRUE;
10764 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010766 switch (TOUPPER_ASC(*typestr))
10767 {
10768 case 'E': type = VIM_ERROR; break;
10769 case 'Q': type = VIM_QUESTION; break;
10770 case 'I': type = VIM_INFO; break;
10771 case 'W': type = VIM_WARNING; break;
10772 case 'G': type = VIM_GENERIC; break;
10773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 }
10775 }
10776 }
10777 }
10778
10779 if (buttons == NULL || *buttons == NUL)
10780 buttons = (char_u *)_("&Ok");
10781
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010782 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010783 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010784 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785#endif
10786}
10787
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010788/*
10789 * "copy()" function
10790 */
10791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010792f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010793{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010794 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010795}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010797#ifdef FEAT_FLOAT
10798/*
10799 * "cos()" function
10800 */
10801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010802f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010803{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010804 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010805
10806 rettv->v_type = VAR_FLOAT;
10807 if (get_float_arg(argvars, &f) == OK)
10808 rettv->vval.v_float = cos(f);
10809 else
10810 rettv->vval.v_float = 0.0;
10811}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010812
10813/*
10814 * "cosh()" function
10815 */
10816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010817f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010818{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010819 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010820
10821 rettv->v_type = VAR_FLOAT;
10822 if (get_float_arg(argvars, &f) == OK)
10823 rettv->vval.v_float = cosh(f);
10824 else
10825 rettv->vval.v_float = 0.0;
10826}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010827#endif
10828
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010830 * "count()" function
10831 */
10832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010833f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010834{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010835 long n = 0;
10836 int ic = FALSE;
10837
Bram Moolenaare9a41262005-01-15 22:18:47 +000010838 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010839 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010840 listitem_T *li;
10841 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010842 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010843
Bram Moolenaare9a41262005-01-15 22:18:47 +000010844 if ((l = argvars[0].vval.v_list) != NULL)
10845 {
10846 li = l->lv_first;
10847 if (argvars[2].v_type != VAR_UNKNOWN)
10848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 int error = FALSE;
10850
10851 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010852 if (argvars[3].v_type != VAR_UNKNOWN)
10853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010854 idx = get_tv_number_chk(&argvars[3], &error);
10855 if (!error)
10856 {
10857 li = list_find(l, idx);
10858 if (li == NULL)
10859 EMSGN(_(e_listidx), idx);
10860 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010861 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010862 if (error)
10863 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010864 }
10865
10866 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010867 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010868 ++n;
10869 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010870 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010871 else if (argvars[0].v_type == VAR_DICT)
10872 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010873 int todo;
10874 dict_T *d;
10875 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010876
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010877 if ((d = argvars[0].vval.v_dict) != NULL)
10878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010879 int error = FALSE;
10880
Bram Moolenaare9a41262005-01-15 22:18:47 +000010881 if (argvars[2].v_type != VAR_UNKNOWN)
10882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010883 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010884 if (argvars[3].v_type != VAR_UNKNOWN)
10885 EMSG(_(e_invarg));
10886 }
10887
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010888 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010889 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010890 {
10891 if (!HASHITEM_EMPTY(hi))
10892 {
10893 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010894 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010895 ++n;
10896 }
10897 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010898 }
10899 }
10900 else
10901 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010902 rettv->vval.v_number = n;
10903}
10904
10905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10907 *
10908 * Checks the existence of a cscope connection.
10909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010911f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912{
10913#ifdef FEAT_CSCOPE
10914 int num = 0;
10915 char_u *dbpath = NULL;
10916 char_u *prepend = NULL;
10917 char_u buf[NUMBUFLEN];
10918
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010919 if (argvars[0].v_type != VAR_UNKNOWN
10920 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922 num = (int)get_tv_number(&argvars[0]);
10923 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010924 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010925 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 }
10927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929#endif
10930}
10931
10932/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010933 * "cursor(lnum, col)" function, or
10934 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010936 * Moves the cursor to the specified line and column.
10937 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010940f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941{
10942 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010943#ifdef FEAT_VIRTUALEDIT
10944 long coladd = 0;
10945#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010946 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010948 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010949 if (argvars[1].v_type == VAR_UNKNOWN)
10950 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010951 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010952 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010953
Bram Moolenaar493c1782014-05-28 14:34:46 +020010954 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010955 {
10956 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010957 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010958 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010959 line = pos.lnum;
10960 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010961#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010962 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010963#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010964 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010965 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010966 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010967 set_curswant = FALSE;
10968 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010969 }
10970 else
10971 {
10972 line = get_tv_lnum(argvars);
10973 col = get_tv_number_chk(&argvars[1], NULL);
10974#ifdef FEAT_VIRTUALEDIT
10975 if (argvars[2].v_type != VAR_UNKNOWN)
10976 coladd = get_tv_number_chk(&argvars[2], NULL);
10977#endif
10978 }
10979 if (line < 0 || col < 0
10980#ifdef FEAT_VIRTUALEDIT
10981 || coladd < 0
10982#endif
10983 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010984 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 if (line > 0)
10986 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 if (col > 0)
10988 curwin->w_cursor.col = col - 1;
10989#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010990 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991#endif
10992
10993 /* Make sure the cursor is in a valid position. */
10994 check_cursor();
10995#ifdef FEAT_MBYTE
10996 /* Correct cursor for multi-byte character. */
10997 if (has_mbyte)
10998 mb_adjust_cursor();
10999#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011000
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011001 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011002 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003}
11004
11005/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011006 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 */
11008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011009f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011011 int noref = 0;
11012
11013 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011014 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011015 if (noref < 0 || noref > 1)
11016 EMSG(_(e_invarg));
11017 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011018 {
11019 current_copyID += COPYID_INC;
11020 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022}
11023
11024/*
11025 * "delete()" function
11026 */
11027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011028f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011030 char_u nbuf[NUMBUFLEN];
11031 char_u *name;
11032 char_u *flags;
11033
11034 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011036 return;
11037
11038 name = get_tv_string(&argvars[0]);
11039 if (name == NULL || *name == NUL)
11040 {
11041 EMSG(_(e_invarg));
11042 return;
11043 }
11044
11045 if (argvars[1].v_type != VAR_UNKNOWN)
11046 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011048 flags = (char_u *)"";
11049
11050 if (*flags == NUL)
11051 /* delete a file */
11052 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11053 else if (STRCMP(flags, "d") == 0)
11054 /* delete an empty directory */
11055 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11056 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011057 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011058 rettv->vval.v_number = delete_recursive(name);
11059 else
11060 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061}
11062
11063/*
11064 * "did_filetype()" function
11065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011067f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068{
11069#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071#endif
11072}
11073
11074/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011075 * "diff_filler()" function
11076 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011078f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011079{
11080#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011082#endif
11083}
11084
11085/*
11086 * "diff_hlID()" function
11087 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011089f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011090{
11091#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011092 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011093 static linenr_T prev_lnum = 0;
11094 static int changedtick = 0;
11095 static int fnum = 0;
11096 static int change_start = 0;
11097 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011098 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011099 int filler_lines;
11100 int col;
11101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011102 if (lnum < 0) /* ignore type error in {lnum} arg */
11103 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011104 if (lnum != prev_lnum
11105 || changedtick != curbuf->b_changedtick
11106 || fnum != curbuf->b_fnum)
11107 {
11108 /* New line, buffer, change: need to get the values. */
11109 filler_lines = diff_check(curwin, lnum);
11110 if (filler_lines < 0)
11111 {
11112 if (filler_lines == -1)
11113 {
11114 change_start = MAXCOL;
11115 change_end = -1;
11116 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11117 hlID = HLF_ADD; /* added line */
11118 else
11119 hlID = HLF_CHD; /* changed line */
11120 }
11121 else
11122 hlID = HLF_ADD; /* added line */
11123 }
11124 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011125 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011126 prev_lnum = lnum;
11127 changedtick = curbuf->b_changedtick;
11128 fnum = curbuf->b_fnum;
11129 }
11130
11131 if (hlID == HLF_CHD || hlID == HLF_TXD)
11132 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011133 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011134 if (col >= change_start && col <= change_end)
11135 hlID = HLF_TXD; /* changed text */
11136 else
11137 hlID = HLF_CHD; /* changed line */
11138 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011139 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011140#endif
11141}
11142
11143/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011144 * "disable_char_avail_for_testing({expr})" function
11145 */
11146 static void
11147f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11148{
11149 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11150}
11151
11152/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011153 * "empty({expr})" function
11154 */
11155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011156f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011157{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011158 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011159
11160 switch (argvars[0].v_type)
11161 {
11162 case VAR_STRING:
11163 case VAR_FUNC:
11164 n = argvars[0].vval.v_string == NULL
11165 || *argvars[0].vval.v_string == NUL;
11166 break;
11167 case VAR_NUMBER:
11168 n = argvars[0].vval.v_number == 0;
11169 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011170 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011171#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011172 n = argvars[0].vval.v_float == 0.0;
11173 break;
11174#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011175 case VAR_LIST:
11176 n = argvars[0].vval.v_list == NULL
11177 || argvars[0].vval.v_list->lv_first == NULL;
11178 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011179 case VAR_DICT:
11180 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011181 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011182 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011183 case VAR_SPECIAL:
11184 n = argvars[0].vval.v_number != VVAL_TRUE;
11185 break;
11186
Bram Moolenaar835dc632016-02-07 14:27:38 +010011187 case VAR_JOB:
11188#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011189 n = argvars[0].vval.v_job == NULL
11190 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11191 break;
11192#endif
11193 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011194#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011195 n = argvars[0].vval.v_channel == NULL
11196 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011197 break;
11198#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011199 case VAR_UNKNOWN:
11200 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11201 n = TRUE;
11202 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011203 }
11204
11205 rettv->vval.v_number = n;
11206}
11207
11208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 * "escape({string}, {chars})" function
11210 */
11211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011212f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213{
11214 char_u buf[NUMBUFLEN];
11215
Bram Moolenaar758711c2005-02-02 23:11:38 +000011216 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11217 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219}
11220
11221/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011222 * "eval()" function
11223 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011225f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011226{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011227 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011229 s = get_tv_string_chk(&argvars[0]);
11230 if (s != NULL)
11231 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011232
Bram Moolenaar615b9972015-01-14 17:15:05 +010011233 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011234 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11235 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011236 if (p != NULL && !aborting())
11237 EMSG2(_(e_invexpr2), p);
11238 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011239 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011240 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011242 else if (*s != NUL)
11243 EMSG(_(e_trailing));
11244}
11245
11246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 * "eventhandler()" function
11248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011250f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253}
11254
11255/*
11256 * "executable()" function
11257 */
11258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011259f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011261 char_u *name = get_tv_string(&argvars[0]);
11262
11263 /* Check in $PATH and also check directly if there is a directory name. */
11264 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11265 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011266}
11267
11268/*
11269 * "exepath()" function
11270 */
11271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011272f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011273{
11274 char_u *p = NULL;
11275
Bram Moolenaarb5971142015-03-21 17:32:19 +010011276 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011277 rettv->v_type = VAR_STRING;
11278 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279}
11280
11281/*
11282 * "exists()" function
11283 */
11284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011285f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286{
11287 char_u *p;
11288 char_u *name;
11289 int n = FALSE;
11290 int len = 0;
11291
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011292 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 if (*p == '$') /* environment variable */
11294 {
11295 /* first try "normal" environment variables (fast) */
11296 if (mch_getenv(p + 1) != NULL)
11297 n = TRUE;
11298 else
11299 {
11300 /* try expanding things like $VIM and ${HOME} */
11301 p = expand_env_save(p);
11302 if (p != NULL && *p != '$')
11303 n = TRUE;
11304 vim_free(p);
11305 }
11306 }
11307 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011310 if (*skipwhite(p) != NUL)
11311 n = FALSE; /* trailing garbage */
11312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 else if (*p == '*') /* internal or user defined function */
11314 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011315 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 }
11317 else if (*p == ':')
11318 {
11319 n = cmd_exists(p + 1);
11320 }
11321 else if (*p == '#')
11322 {
11323#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011324 if (p[1] == '#')
11325 n = autocmd_supported(p + 2);
11326 else
11327 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328#endif
11329 }
11330 else /* internal variable */
11331 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011332 char_u *tofree;
11333 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011335 /* get_name_len() takes care of expanding curly braces */
11336 name = p;
11337 len = get_name_len(&p, &tofree, TRUE, FALSE);
11338 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011340 if (tofree != NULL)
11341 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011342 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011343 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011345 /* handle d.key, l[idx], f(expr) */
11346 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11347 if (n)
11348 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 }
11350 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011351 if (*p != NUL)
11352 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011354 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 }
11356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358}
11359
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011360#ifdef FEAT_FLOAT
11361/*
11362 * "exp()" function
11363 */
11364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011365f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011366{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011367 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011368
11369 rettv->v_type = VAR_FLOAT;
11370 if (get_float_arg(argvars, &f) == OK)
11371 rettv->vval.v_float = exp(f);
11372 else
11373 rettv->vval.v_float = 0.0;
11374}
11375#endif
11376
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377/*
11378 * "expand()" function
11379 */
11380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011381f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382{
11383 char_u *s;
11384 int len;
11385 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011386 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011388 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011389 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011391 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011392 if (argvars[1].v_type != VAR_UNKNOWN
11393 && argvars[2].v_type != VAR_UNKNOWN
11394 && get_tv_number_chk(&argvars[2], &error)
11395 && !error)
11396 {
11397 rettv->v_type = VAR_LIST;
11398 rettv->vval.v_list = NULL;
11399 }
11400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 if (*s == '%' || *s == '#' || *s == '<')
11403 {
11404 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011405 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011407 if (rettv->v_type == VAR_LIST)
11408 {
11409 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11410 list_append_string(rettv->vval.v_list, result, -1);
11411 else
11412 vim_free(result);
11413 }
11414 else
11415 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416 }
11417 else
11418 {
11419 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011420 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011421 if (argvars[1].v_type != VAR_UNKNOWN
11422 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011423 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011424 if (!error)
11425 {
11426 ExpandInit(&xpc);
11427 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011428 if (p_wic)
11429 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011430 if (rettv->v_type == VAR_STRING)
11431 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11432 options, WILD_ALL);
11433 else if (rettv_list_alloc(rettv) != FAIL)
11434 {
11435 int i;
11436
11437 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11438 for (i = 0; i < xpc.xp_numfiles; i++)
11439 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11440 ExpandCleanup(&xpc);
11441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011442 }
11443 else
11444 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 }
11446}
11447
11448/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011449 * Go over all entries in "d2" and add them to "d1".
11450 * When "action" is "error" then a duplicate key is an error.
11451 * When "action" is "force" then a duplicate key is overwritten.
11452 * Otherwise duplicate keys are ignored ("action" is "keep").
11453 */
11454 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011455dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011456{
11457 dictitem_T *di1;
11458 hashitem_T *hi2;
11459 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011460 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011461
11462 todo = (int)d2->dv_hashtab.ht_used;
11463 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11464 {
11465 if (!HASHITEM_EMPTY(hi2))
11466 {
11467 --todo;
11468 di1 = dict_find(d1, hi2->hi_key, -1);
11469 if (d1->dv_scope != 0)
11470 {
11471 /* Disallow replacing a builtin function in l: and g:.
11472 * Check the key to be valid when adding to any
11473 * scope. */
11474 if (d1->dv_scope == VAR_DEF_SCOPE
11475 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11476 && var_check_func_name(hi2->hi_key,
11477 di1 == NULL))
11478 break;
11479 if (!valid_varname(hi2->hi_key))
11480 break;
11481 }
11482 if (di1 == NULL)
11483 {
11484 di1 = dictitem_copy(HI2DI(hi2));
11485 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11486 dictitem_free(di1);
11487 }
11488 else if (*action == 'e')
11489 {
11490 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11491 break;
11492 }
11493 else if (*action == 'f' && HI2DI(hi2) != di1)
11494 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011495 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11496 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011497 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011498 clear_tv(&di1->di_tv);
11499 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11500 }
11501 }
11502 }
11503}
11504
11505/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011506 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011507 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011508 */
11509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011510f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011511{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011512 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011513
Bram Moolenaare9a41262005-01-15 22:18:47 +000011514 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011515 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011516 list_T *l1, *l2;
11517 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011518 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011519 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011520
Bram Moolenaare9a41262005-01-15 22:18:47 +000011521 l1 = argvars[0].vval.v_list;
11522 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011523 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011524 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011525 {
11526 if (argvars[2].v_type != VAR_UNKNOWN)
11527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011528 before = get_tv_number_chk(&argvars[2], &error);
11529 if (error)
11530 return; /* type error; errmsg already given */
11531
Bram Moolenaar758711c2005-02-02 23:11:38 +000011532 if (before == l1->lv_len)
11533 item = NULL;
11534 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011535 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011536 item = list_find(l1, before);
11537 if (item == NULL)
11538 {
11539 EMSGN(_(e_listidx), before);
11540 return;
11541 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011542 }
11543 }
11544 else
11545 item = NULL;
11546 list_extend(l1, l2, item);
11547
Bram Moolenaare9a41262005-01-15 22:18:47 +000011548 copy_tv(&argvars[0], rettv);
11549 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011550 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011551 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11552 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011553 dict_T *d1, *d2;
11554 char_u *action;
11555 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011556
11557 d1 = argvars[0].vval.v_dict;
11558 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011559 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011560 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011561 {
11562 /* Check the third argument. */
11563 if (argvars[2].v_type != VAR_UNKNOWN)
11564 {
11565 static char *(av[]) = {"keep", "force", "error"};
11566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011567 action = get_tv_string_chk(&argvars[2]);
11568 if (action == NULL)
11569 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011570 for (i = 0; i < 3; ++i)
11571 if (STRCMP(action, av[i]) == 0)
11572 break;
11573 if (i == 3)
11574 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011575 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011576 return;
11577 }
11578 }
11579 else
11580 action = (char_u *)"force";
11581
Bram Moolenaara9922d62013-05-30 13:01:18 +020011582 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011583
Bram Moolenaare9a41262005-01-15 22:18:47 +000011584 copy_tv(&argvars[0], rettv);
11585 }
11586 }
11587 else
11588 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011589}
11590
11591/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011592 * "feedkeys()" function
11593 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011595f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011596{
11597 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011598 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011599 char_u *keys, *flags;
11600 char_u nbuf[NUMBUFLEN];
11601 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011602 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011603 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011604
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011605 /* This is not allowed in the sandbox. If the commands would still be
11606 * executed in the sandbox it would be OK, but it probably happens later,
11607 * when "sandbox" is no longer set. */
11608 if (check_secure())
11609 return;
11610
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011611 keys = get_tv_string(&argvars[0]);
11612 if (*keys != NUL)
11613 {
11614 if (argvars[1].v_type != VAR_UNKNOWN)
11615 {
11616 flags = get_tv_string_buf(&argvars[1], nbuf);
11617 for ( ; *flags != NUL; ++flags)
11618 {
11619 switch (*flags)
11620 {
11621 case 'n': remap = FALSE; break;
11622 case 'm': remap = TRUE; break;
11623 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011624 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011625 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011626 }
11627 }
11628 }
11629
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011630 /* Need to escape K_SPECIAL and CSI before putting the string in the
11631 * typeahead buffer. */
11632 keys_esc = vim_strsave_escape_csi(keys);
11633 if (keys_esc != NULL)
11634 {
11635 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011636 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011637 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011638 if (vgetc_busy)
11639 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011640 if (execute)
11641 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011642 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011643 }
11644}
11645
11646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647 * "filereadable()" function
11648 */
11649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011650f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011652 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653 char_u *p;
11654 int n;
11655
Bram Moolenaarc236c162008-07-13 17:41:49 +000011656#ifndef O_NONBLOCK
11657# define O_NONBLOCK 0
11658#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011660 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11661 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 {
11663 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011664 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665 }
11666 else
11667 n = FALSE;
11668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011669 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670}
11671
11672/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011673 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 * rights to write into.
11675 */
11676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011677f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011679 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680}
11681
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011682 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011683findfilendir(
11684 typval_T *argvars UNUSED,
11685 typval_T *rettv,
11686 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011687{
11688#ifdef FEAT_SEARCHPATH
11689 char_u *fname;
11690 char_u *fresult = NULL;
11691 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11692 char_u *p;
11693 char_u pathbuf[NUMBUFLEN];
11694 int count = 1;
11695 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011696 int error = FALSE;
11697#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011698
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011699 rettv->vval.v_string = NULL;
11700 rettv->v_type = VAR_STRING;
11701
11702#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011703 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011704
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011705 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11708 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011709 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011710 else
11711 {
11712 if (*p != NUL)
11713 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011715 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011716 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011717 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011718 }
11719
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011720 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11721 error = TRUE;
11722
11723 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011725 do
11726 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011727 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011728 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011729 fresult = find_file_in_path_option(first ? fname : NULL,
11730 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011731 0, first, path,
11732 find_what,
11733 curbuf->b_ffname,
11734 find_what == FINDFILE_DIR
11735 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011736 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011737
11738 if (fresult != NULL && rettv->v_type == VAR_LIST)
11739 list_append_string(rettv->vval.v_list, fresult, -1);
11740
11741 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011742 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011743
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011744 if (rettv->v_type == VAR_STRING)
11745 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011746#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011747}
11748
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011749static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11750static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011751
11752/*
11753 * Implementation of map() and filter().
11754 */
11755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011756filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011757{
11758 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011759 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011760 listitem_T *li, *nli;
11761 list_T *l = NULL;
11762 dictitem_T *di;
11763 hashtab_T *ht;
11764 hashitem_T *hi;
11765 dict_T *d = NULL;
11766 typval_T save_val;
11767 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011768 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011769 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011770 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011771 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011772 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011773 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011774 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011775
Bram Moolenaare9a41262005-01-15 22:18:47 +000011776 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011777 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011778 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011779 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011780 return;
11781 }
11782 else if (argvars[0].v_type == VAR_DICT)
11783 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011784 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011785 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011786 return;
11787 }
11788 else
11789 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011790 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011791 return;
11792 }
11793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011794 expr = get_tv_string_buf_chk(&argvars[1], buf);
11795 /* On type errors, the preceding call has already displayed an error
11796 * message. Avoid a misleading error message for an empty string that
11797 * was not passed as argument. */
11798 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011800 prepare_vimvar(VV_VAL, &save_val);
11801 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011802
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011803 /* We reset "did_emsg" to be able to detect whether an error
11804 * occurred during evaluation of the expression. */
11805 save_did_emsg = did_emsg;
11806 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011807
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011808 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011809 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011811 vimvars[VV_KEY].vv_type = VAR_STRING;
11812
11813 ht = &d->dv_hashtab;
11814 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011815 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011816 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011817 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818 if (!HASHITEM_EMPTY(hi))
11819 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011820 int r;
11821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 --todo;
11823 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011824 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011825 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11826 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011827 break;
11828 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011829 r = filter_map_one(&di->di_tv, expr, map, &rem);
11830 clear_tv(&vimvars[VV_KEY].vv_tv);
11831 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011832 break;
11833 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011834 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011835 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11836 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011837 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011838 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011839 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011840 }
11841 }
11842 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011843 }
11844 else
11845 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011846 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011848 for (li = l->lv_first; li != NULL; li = nli)
11849 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011850 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011851 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011852 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011853 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011854 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011855 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011856 break;
11857 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011858 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011859 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011860 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011861 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011862
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011863 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011864 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011865
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011866 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011868
11869 copy_tv(&argvars[0], rettv);
11870}
11871
11872 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011873filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011874{
Bram Moolenaar33570922005-01-25 22:26:29 +000011875 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011876 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011877 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011878
Bram Moolenaar33570922005-01-25 22:26:29 +000011879 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011880 s = expr;
11881 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011882 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011883 if (*s != NUL) /* check for trailing chars after expr */
11884 {
11885 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011886 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011887 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011888 }
11889 if (map)
11890 {
11891 /* map(): replace the list item value */
11892 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011893 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011894 *tv = rettv;
11895 }
11896 else
11897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011898 int error = FALSE;
11899
Bram Moolenaare9a41262005-01-15 22:18:47 +000011900 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011901 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011902 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 /* On type error, nothing has been removed; return FAIL to stop the
11904 * loop. The error message was given by get_tv_number_chk(). */
11905 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011906 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011907 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011908 retval = OK;
11909theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011910 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011911 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011912}
11913
11914/*
11915 * "filter()" function
11916 */
11917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011918f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011919{
11920 filter_map(argvars, rettv, FALSE);
11921}
11922
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011923/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011924 * "finddir({fname}[, {path}[, {count}]])" function
11925 */
11926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011927f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011928{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011929 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011930}
11931
11932/*
11933 * "findfile({fname}[, {path}[, {count}]])" function
11934 */
11935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011936f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011937{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011938 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011939}
11940
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011941#ifdef FEAT_FLOAT
11942/*
11943 * "float2nr({float})" function
11944 */
11945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011946f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011947{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011948 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011949
11950 if (get_float_arg(argvars, &f) == OK)
11951 {
11952 if (f < -0x7fffffff)
11953 rettv->vval.v_number = -0x7fffffff;
11954 else if (f > 0x7fffffff)
11955 rettv->vval.v_number = 0x7fffffff;
11956 else
11957 rettv->vval.v_number = (varnumber_T)f;
11958 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011959}
11960
11961/*
11962 * "floor({float})" function
11963 */
11964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011965f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011966{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011967 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011968
11969 rettv->v_type = VAR_FLOAT;
11970 if (get_float_arg(argvars, &f) == OK)
11971 rettv->vval.v_float = floor(f);
11972 else
11973 rettv->vval.v_float = 0.0;
11974}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011975
11976/*
11977 * "fmod()" function
11978 */
11979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011980f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011981{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011982 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011983
11984 rettv->v_type = VAR_FLOAT;
11985 if (get_float_arg(argvars, &fx) == OK
11986 && get_float_arg(&argvars[1], &fy) == OK)
11987 rettv->vval.v_float = fmod(fx, fy);
11988 else
11989 rettv->vval.v_float = 0.0;
11990}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011991#endif
11992
Bram Moolenaar0d660222005-01-07 21:51:51 +000011993/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011994 * "fnameescape({string})" function
11995 */
11996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011997f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011998{
11999 rettv->vval.v_string = vim_strsave_fnameescape(
12000 get_tv_string(&argvars[0]), FALSE);
12001 rettv->v_type = VAR_STRING;
12002}
12003
12004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 * "fnamemodify({fname}, {mods})" function
12006 */
12007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012008f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009{
12010 char_u *fname;
12011 char_u *mods;
12012 int usedlen = 0;
12013 int len;
12014 char_u *fbuf = NULL;
12015 char_u buf[NUMBUFLEN];
12016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012017 fname = get_tv_string_chk(&argvars[0]);
12018 mods = get_tv_string_buf_chk(&argvars[1], buf);
12019 if (fname == NULL || mods == NULL)
12020 fname = NULL;
12021 else
12022 {
12023 len = (int)STRLEN(fname);
12024 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012027 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012029 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032 vim_free(fbuf);
12033}
12034
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012035static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036
12037/*
12038 * "foldclosed()" function
12039 */
12040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012041foldclosed_both(
12042 typval_T *argvars UNUSED,
12043 typval_T *rettv,
12044 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045{
12046#ifdef FEAT_FOLDING
12047 linenr_T lnum;
12048 linenr_T first, last;
12049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12052 {
12053 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12054 {
12055 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012056 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 return;
12060 }
12061 }
12062#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064}
12065
12066/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012067 * "foldclosed()" function
12068 */
12069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012070f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012071{
12072 foldclosed_both(argvars, rettv, FALSE);
12073}
12074
12075/*
12076 * "foldclosedend()" function
12077 */
12078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012079f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012080{
12081 foldclosed_both(argvars, rettv, TRUE);
12082}
12083
12084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 * "foldlevel()" function
12086 */
12087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012088f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089{
12090#ifdef FEAT_FOLDING
12091 linenr_T lnum;
12092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097}
12098
12099/*
12100 * "foldtext()" function
12101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012103f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104{
12105#ifdef FEAT_FOLDING
12106 linenr_T lnum;
12107 char_u *s;
12108 char_u *r;
12109 int len;
12110 char *txt;
12111#endif
12112
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012113 rettv->v_type = VAR_STRING;
12114 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012116 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12117 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12118 <= curbuf->b_ml.ml_line_count
12119 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120 {
12121 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012122 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12123 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124 {
12125 if (!linewhite(lnum))
12126 break;
12127 ++lnum;
12128 }
12129
12130 /* Find interesting text in this line. */
12131 s = skipwhite(ml_get(lnum));
12132 /* skip C comment-start */
12133 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012136 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012137 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012138 {
12139 s = skipwhite(ml_get(lnum + 1));
12140 if (*s == '*')
12141 s = skipwhite(s + 1);
12142 }
12143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144 txt = _("+-%s%3ld lines: ");
12145 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012146 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147 + 20 /* for %3ld */
12148 + STRLEN(s))); /* concatenated */
12149 if (r != NULL)
12150 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012151 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12152 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12153 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154 len = (int)STRLEN(r);
12155 STRCAT(r, s);
12156 /* remove 'foldmarker' and 'commentstring' */
12157 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 }
12160 }
12161#endif
12162}
12163
12164/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012165 * "foldtextresult(lnum)" function
12166 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012168f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012169{
12170#ifdef FEAT_FOLDING
12171 linenr_T lnum;
12172 char_u *text;
12173 char_u buf[51];
12174 foldinfo_T foldinfo;
12175 int fold_count;
12176#endif
12177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012178 rettv->v_type = VAR_STRING;
12179 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012180#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012181 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012182 /* treat illegal types and illegal string values for {lnum} the same */
12183 if (lnum < 0)
12184 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012185 fold_count = foldedCount(curwin, lnum, &foldinfo);
12186 if (fold_count > 0)
12187 {
12188 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12189 &foldinfo, buf);
12190 if (text == buf)
12191 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012192 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012193 }
12194#endif
12195}
12196
12197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 * "foreground()" function
12199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012201f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203#ifdef FEAT_GUI
12204 if (gui.in_use)
12205 gui_mch_set_foreground();
12206#else
12207# ifdef WIN32
12208 win32_set_foreground();
12209# endif
12210#endif
12211}
12212
12213/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012214 * "function()" function
12215 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012217f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012218{
12219 char_u *s;
12220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012221 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012222 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012223 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012224 /* Don't check an autoload name for existence here. */
12225 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012226 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012227 else
12228 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012229 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012230 {
12231 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012232 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012233
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012234 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12235 * also be called from another script. Using trans_function_name()
12236 * would also work, but some plugins depend on the name being
12237 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012238 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012239 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012240 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012241 if (rettv->vval.v_string != NULL)
12242 {
12243 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012244 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012245 }
12246 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012247 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012248 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012249 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012250 }
12251}
12252
12253/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012254 * "garbagecollect()" function
12255 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012257f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012258{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012259 /* This is postponed until we are back at the toplevel, because we may be
12260 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12261 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012262
12263 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12264 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012265}
12266
12267/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012268 * "get()" function
12269 */
12270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012271f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012272{
Bram Moolenaar33570922005-01-25 22:26:29 +000012273 listitem_T *li;
12274 list_T *l;
12275 dictitem_T *di;
12276 dict_T *d;
12277 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012278
Bram Moolenaare9a41262005-01-15 22:18:47 +000012279 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012280 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012281 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012283 int error = FALSE;
12284
12285 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12286 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012287 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012288 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012289 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012290 else if (argvars[0].v_type == VAR_DICT)
12291 {
12292 if ((d = argvars[0].vval.v_dict) != NULL)
12293 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012294 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012295 if (di != NULL)
12296 tv = &di->di_tv;
12297 }
12298 }
12299 else
12300 EMSG2(_(e_listdictarg), "get()");
12301
12302 if (tv == NULL)
12303 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012304 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012305 copy_tv(&argvars[2], rettv);
12306 }
12307 else
12308 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012309}
12310
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012311static 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 +000012312
12313/*
12314 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012315 * Return a range (from start to end) of lines in rettv from the specified
12316 * buffer.
12317 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012318 */
12319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012320get_buffer_lines(
12321 buf_T *buf,
12322 linenr_T start,
12323 linenr_T end,
12324 int retlist,
12325 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012326{
12327 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012328
Bram Moolenaar959a1432013-12-14 12:17:38 +010012329 rettv->v_type = VAR_STRING;
12330 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012331 if (retlist && rettv_list_alloc(rettv) == FAIL)
12332 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012333
12334 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12335 return;
12336
12337 if (!retlist)
12338 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012339 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12340 p = ml_get_buf(buf, start, FALSE);
12341 else
12342 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012343 rettv->vval.v_string = vim_strsave(p);
12344 }
12345 else
12346 {
12347 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012348 return;
12349
12350 if (start < 1)
12351 start = 1;
12352 if (end > buf->b_ml.ml_line_count)
12353 end = buf->b_ml.ml_line_count;
12354 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012355 if (list_append_string(rettv->vval.v_list,
12356 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012357 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012358 }
12359}
12360
12361/*
12362 * "getbufline()" function
12363 */
12364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012365f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012366{
12367 linenr_T lnum;
12368 linenr_T end;
12369 buf_T *buf;
12370
12371 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12372 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012373 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012374 --emsg_off;
12375
Bram Moolenaar661b1822005-07-28 22:36:45 +000012376 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012377 if (argvars[2].v_type == VAR_UNKNOWN)
12378 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012379 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012380 end = get_tv_lnum_buf(&argvars[2], buf);
12381
Bram Moolenaar342337a2005-07-21 21:11:17 +000012382 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012383}
12384
Bram Moolenaar0d660222005-01-07 21:51:51 +000012385/*
12386 * "getbufvar()" function
12387 */
12388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012389f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012390{
12391 buf_T *buf;
12392 buf_T *save_curbuf;
12393 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012394 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012395 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012397 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12398 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012399 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012400 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012401
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012402 rettv->v_type = VAR_STRING;
12403 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012404
12405 if (buf != NULL && varname != NULL)
12406 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012407 /* set curbuf to be our buf, temporarily */
12408 save_curbuf = curbuf;
12409 curbuf = buf;
12410
Bram Moolenaar0d660222005-01-07 21:51:51 +000012411 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012412 {
12413 if (get_option_tv(&varname, rettv, TRUE) == OK)
12414 done = TRUE;
12415 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012416 else if (STRCMP(varname, "changedtick") == 0)
12417 {
12418 rettv->v_type = VAR_NUMBER;
12419 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012420 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012421 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012422 else
12423 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012424 /* Look up the variable. */
12425 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12426 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12427 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012428 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012429 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012430 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012431 done = TRUE;
12432 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012433 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012434
12435 /* restore previous notion of curbuf */
12436 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012437 }
12438
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012439 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12440 /* use the default value */
12441 copy_tv(&argvars[2], rettv);
12442
Bram Moolenaar0d660222005-01-07 21:51:51 +000012443 --emsg_off;
12444}
12445
12446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447 * "getchar()" function
12448 */
12449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012450f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451{
12452 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012453 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012455 /* Position the cursor. Needed after a message that ends in a space. */
12456 windgoto(msg_row, msg_col);
12457
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458 ++no_mapping;
12459 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012460 for (;;)
12461 {
12462 if (argvars[0].v_type == VAR_UNKNOWN)
12463 /* getchar(): blocking wait. */
12464 n = safe_vgetc();
12465 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12466 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012467 n = vpeekc_any();
12468 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012469 /* illegal argument or getchar(0) and no char avail: return zero */
12470 n = 0;
12471 else
12472 /* getchar(0) and char avail: return char */
12473 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012474
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012475 if (n == K_IGNORE)
12476 continue;
12477 break;
12478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479 --no_mapping;
12480 --allow_keys;
12481
Bram Moolenaar219b8702006-11-01 14:32:36 +000012482 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12483 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12484 vimvars[VV_MOUSE_COL].vv_nr = 0;
12485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012486 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487 if (IS_SPECIAL(n) || mod_mask != 0)
12488 {
12489 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12490 int i = 0;
12491
12492 /* Turn a special key into three bytes, plus modifier. */
12493 if (mod_mask != 0)
12494 {
12495 temp[i++] = K_SPECIAL;
12496 temp[i++] = KS_MODIFIER;
12497 temp[i++] = mod_mask;
12498 }
12499 if (IS_SPECIAL(n))
12500 {
12501 temp[i++] = K_SPECIAL;
12502 temp[i++] = K_SECOND(n);
12503 temp[i++] = K_THIRD(n);
12504 }
12505#ifdef FEAT_MBYTE
12506 else if (has_mbyte)
12507 i += (*mb_char2bytes)(n, temp + i);
12508#endif
12509 else
12510 temp[i++] = n;
12511 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 rettv->v_type = VAR_STRING;
12513 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012514
12515#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012516 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012517 {
12518 int row = mouse_row;
12519 int col = mouse_col;
12520 win_T *win;
12521 linenr_T lnum;
12522# ifdef FEAT_WINDOWS
12523 win_T *wp;
12524# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012525 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012526
12527 if (row >= 0 && col >= 0)
12528 {
12529 /* Find the window at the mouse coordinates and compute the
12530 * text position. */
12531 win = mouse_find_win(&row, &col);
12532 (void)mouse_comp_pos(win, &row, &col, &lnum);
12533# ifdef FEAT_WINDOWS
12534 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012535 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012536# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012537 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012538 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12539 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12540 }
12541 }
12542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543 }
12544}
12545
12546/*
12547 * "getcharmod()" function
12548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012550f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012552 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553}
12554
12555/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012556 * "getcharsearch()" function
12557 */
12558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012559f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012560{
12561 if (rettv_dict_alloc(rettv) != FAIL)
12562 {
12563 dict_T *dict = rettv->vval.v_dict;
12564
12565 dict_add_nr_str(dict, "char", 0L, last_csearch());
12566 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12567 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12568 }
12569}
12570
12571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572 * "getcmdline()" function
12573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012575f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012577 rettv->v_type = VAR_STRING;
12578 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579}
12580
12581/*
12582 * "getcmdpos()" function
12583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012585f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012587 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588}
12589
12590/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012591 * "getcmdtype()" function
12592 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012594f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012595{
12596 rettv->v_type = VAR_STRING;
12597 rettv->vval.v_string = alloc(2);
12598 if (rettv->vval.v_string != NULL)
12599 {
12600 rettv->vval.v_string[0] = get_cmdline_type();
12601 rettv->vval.v_string[1] = NUL;
12602 }
12603}
12604
12605/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012606 * "getcmdwintype()" function
12607 */
12608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012609f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012610{
12611 rettv->v_type = VAR_STRING;
12612 rettv->vval.v_string = NULL;
12613#ifdef FEAT_CMDWIN
12614 rettv->vval.v_string = alloc(2);
12615 if (rettv->vval.v_string != NULL)
12616 {
12617 rettv->vval.v_string[0] = cmdwin_type;
12618 rettv->vval.v_string[1] = NUL;
12619 }
12620#endif
12621}
12622
12623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624 * "getcwd()" function
12625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012627f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012628{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012629 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012630 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012632 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012633 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012634
12635 wp = find_tabwin(&argvars[0], &argvars[1]);
12636 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012638 if (wp->w_localdir != NULL)
12639 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12640 else if(globaldir != NULL)
12641 rettv->vval.v_string = vim_strsave(globaldir);
12642 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012643 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012644 cwd = alloc(MAXPATHL);
12645 if (cwd != NULL)
12646 {
12647 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12648 rettv->vval.v_string = vim_strsave(cwd);
12649 vim_free(cwd);
12650 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012651 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012652#ifdef BACKSLASH_IN_FILENAME
12653 if (rettv->vval.v_string != NULL)
12654 slash_adjust(rettv->vval.v_string);
12655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656 }
12657}
12658
12659/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012660 * "getfontname()" function
12661 */
12662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012663f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012664{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012665 rettv->v_type = VAR_STRING;
12666 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012667#ifdef FEAT_GUI
12668 if (gui.in_use)
12669 {
12670 GuiFont font;
12671 char_u *name = NULL;
12672
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012673 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012674 {
12675 /* Get the "Normal" font. Either the name saved by
12676 * hl_set_font_name() or from the font ID. */
12677 font = gui.norm_font;
12678 name = hl_get_font_name();
12679 }
12680 else
12681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012683 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12684 return;
12685 font = gui_mch_get_font(name, FALSE);
12686 if (font == NOFONT)
12687 return; /* Invalid font name, return empty string. */
12688 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012690 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012691 gui_mch_free_font(font);
12692 }
12693#endif
12694}
12695
12696/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012697 * "getfperm({fname})" function
12698 */
12699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012700f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012701{
12702 char_u *fname;
12703 struct stat st;
12704 char_u *perm = NULL;
12705 char_u flags[] = "rwx";
12706 int i;
12707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012708 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012710 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012711 if (mch_stat((char *)fname, &st) >= 0)
12712 {
12713 perm = vim_strsave((char_u *)"---------");
12714 if (perm != NULL)
12715 {
12716 for (i = 0; i < 9; i++)
12717 {
12718 if (st.st_mode & (1 << (8 - i)))
12719 perm[i] = flags[i % 3];
12720 }
12721 }
12722 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012723 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012724}
12725
12726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727 * "getfsize({fname})" function
12728 */
12729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012730f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731{
12732 char_u *fname;
12733 struct stat st;
12734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012737 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738
12739 if (mch_stat((char *)fname, &st) >= 0)
12740 {
12741 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012745 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012746
12747 /* non-perfect check for overflow */
12748 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12749 rettv->vval.v_number = -2;
12750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751 }
12752 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012753 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754}
12755
12756/*
12757 * "getftime({fname})" function
12758 */
12759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012760f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761{
12762 char_u *fname;
12763 struct stat st;
12764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012765 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766
12767 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771}
12772
12773/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012774 * "getftype({fname})" function
12775 */
12776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012777f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012778{
12779 char_u *fname;
12780 struct stat st;
12781 char_u *type = NULL;
12782 char *t;
12783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012784 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012785
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012787 if (mch_lstat((char *)fname, &st) >= 0)
12788 {
12789#ifdef S_ISREG
12790 if (S_ISREG(st.st_mode))
12791 t = "file";
12792 else if (S_ISDIR(st.st_mode))
12793 t = "dir";
12794# ifdef S_ISLNK
12795 else if (S_ISLNK(st.st_mode))
12796 t = "link";
12797# endif
12798# ifdef S_ISBLK
12799 else if (S_ISBLK(st.st_mode))
12800 t = "bdev";
12801# endif
12802# ifdef S_ISCHR
12803 else if (S_ISCHR(st.st_mode))
12804 t = "cdev";
12805# endif
12806# ifdef S_ISFIFO
12807 else if (S_ISFIFO(st.st_mode))
12808 t = "fifo";
12809# endif
12810# ifdef S_ISSOCK
12811 else if (S_ISSOCK(st.st_mode))
12812 t = "fifo";
12813# endif
12814 else
12815 t = "other";
12816#else
12817# ifdef S_IFMT
12818 switch (st.st_mode & S_IFMT)
12819 {
12820 case S_IFREG: t = "file"; break;
12821 case S_IFDIR: t = "dir"; break;
12822# ifdef S_IFLNK
12823 case S_IFLNK: t = "link"; break;
12824# endif
12825# ifdef S_IFBLK
12826 case S_IFBLK: t = "bdev"; break;
12827# endif
12828# ifdef S_IFCHR
12829 case S_IFCHR: t = "cdev"; break;
12830# endif
12831# ifdef S_IFIFO
12832 case S_IFIFO: t = "fifo"; break;
12833# endif
12834# ifdef S_IFSOCK
12835 case S_IFSOCK: t = "socket"; break;
12836# endif
12837 default: t = "other";
12838 }
12839# else
12840 if (mch_isdir(fname))
12841 t = "dir";
12842 else
12843 t = "file";
12844# endif
12845#endif
12846 type = vim_strsave((char_u *)t);
12847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012849}
12850
12851/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012852 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012853 */
12854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012855f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012856{
12857 linenr_T lnum;
12858 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012859 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012860
12861 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012862 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012863 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012864 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012865 retlist = FALSE;
12866 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012867 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012868 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012869 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012870 retlist = TRUE;
12871 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012872
Bram Moolenaar342337a2005-07-21 21:11:17 +000012873 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012874}
12875
12876/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012877 * "getmatches()" function
12878 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012880f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012881{
12882#ifdef FEAT_SEARCH_EXTRA
12883 dict_T *dict;
12884 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012885 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012886
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012887 if (rettv_list_alloc(rettv) == OK)
12888 {
12889 while (cur != NULL)
12890 {
12891 dict = dict_alloc();
12892 if (dict == NULL)
12893 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012894 if (cur->match.regprog == NULL)
12895 {
12896 /* match added with matchaddpos() */
12897 for (i = 0; i < MAXPOSMATCH; ++i)
12898 {
12899 llpos_T *llpos;
12900 char buf[6];
12901 list_T *l;
12902
12903 llpos = &cur->pos.pos[i];
12904 if (llpos->lnum == 0)
12905 break;
12906 l = list_alloc();
12907 if (l == NULL)
12908 break;
12909 list_append_number(l, (varnumber_T)llpos->lnum);
12910 if (llpos->col > 0)
12911 {
12912 list_append_number(l, (varnumber_T)llpos->col);
12913 list_append_number(l, (varnumber_T)llpos->len);
12914 }
12915 sprintf(buf, "pos%d", i + 1);
12916 dict_add_list(dict, buf, l);
12917 }
12918 }
12919 else
12920 {
12921 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12922 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012923 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012924 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12925 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012926# ifdef FEAT_CONCEAL
12927 if (cur->conceal_char)
12928 {
12929 char_u buf[MB_MAXBYTES + 1];
12930
12931 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12932 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12933 }
12934# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012935 list_append_dict(rettv->vval.v_list, dict);
12936 cur = cur->next;
12937 }
12938 }
12939#endif
12940}
12941
12942/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012943 * "getpid()" function
12944 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012946f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012947{
12948 rettv->vval.v_number = mch_get_pid();
12949}
12950
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012951static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012952
12953/*
12954 * "getcurpos()" function
12955 */
12956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012957f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012958{
12959 getpos_both(argvars, rettv, TRUE);
12960}
12961
Bram Moolenaar18081e32008-02-20 19:11:07 +000012962/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012963 * "getpos(string)" function
12964 */
12965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012966f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012967{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012968 getpos_both(argvars, rettv, FALSE);
12969}
12970
12971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012972getpos_both(
12973 typval_T *argvars,
12974 typval_T *rettv,
12975 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012976{
Bram Moolenaara5525202006-03-02 22:52:09 +000012977 pos_T *fp;
12978 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012979 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012980
12981 if (rettv_list_alloc(rettv) == OK)
12982 {
12983 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012984 if (getcurpos)
12985 fp = &curwin->w_cursor;
12986 else
12987 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012988 if (fnum != -1)
12989 list_append_number(l, (varnumber_T)fnum);
12990 else
12991 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012992 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12993 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012994 list_append_number(l, (fp != NULL)
12995 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012996 : (varnumber_T)0);
12997 list_append_number(l,
12998#ifdef FEAT_VIRTUALEDIT
12999 (fp != NULL) ? (varnumber_T)fp->coladd :
13000#endif
13001 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013002 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013003 {
13004 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013005 list_append_number(l, curwin->w_curswant == MAXCOL ?
13006 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013007 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013008 }
13009 else
13010 rettv->vval.v_number = FALSE;
13011}
13012
13013/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013014 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013015 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013017f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013018{
13019#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013020 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013021#endif
13022
Bram Moolenaar2641f772005-03-25 21:58:17 +000013023#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013024 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013025 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013026 wp = NULL;
13027 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13028 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013029 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013030 if (wp == NULL)
13031 return;
13032 }
13033
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013034 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013035 }
13036#endif
13037}
13038
13039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040 * "getreg()" function
13041 */
13042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013043f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044{
13045 char_u *strregname;
13046 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013047 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013048 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013049 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013051 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013052 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013053 strregname = get_tv_string_chk(&argvars[0]);
13054 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013055 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013056 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013057 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013058 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13059 return_list = get_tv_number_chk(&argvars[2], &error);
13060 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013063 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013064
13065 if (error)
13066 return;
13067
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 regname = (strregname == NULL ? '"' : *strregname);
13069 if (regname == 0)
13070 regname = '"';
13071
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013072 if (return_list)
13073 {
13074 rettv->v_type = VAR_LIST;
13075 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13076 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013077 if (rettv->vval.v_list != NULL)
13078 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013079 }
13080 else
13081 {
13082 rettv->v_type = VAR_STRING;
13083 rettv->vval.v_string = get_reg_contents(regname,
13084 arg2 ? GREG_EXPR_SRC : 0);
13085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086}
13087
13088/*
13089 * "getregtype()" function
13090 */
13091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013092f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093{
13094 char_u *strregname;
13095 int regname;
13096 char_u buf[NUMBUFLEN + 2];
13097 long reglen = 0;
13098
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013099 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013100 {
13101 strregname = get_tv_string_chk(&argvars[0]);
13102 if (strregname == NULL) /* type error; errmsg already given */
13103 {
13104 rettv->v_type = VAR_STRING;
13105 rettv->vval.v_string = NULL;
13106 return;
13107 }
13108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109 else
13110 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013111 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112
13113 regname = (strregname == NULL ? '"' : *strregname);
13114 if (regname == 0)
13115 regname = '"';
13116
13117 buf[0] = NUL;
13118 buf[1] = NUL;
13119 switch (get_reg_type(regname, &reglen))
13120 {
13121 case MLINE: buf[0] = 'V'; break;
13122 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 case MBLOCK:
13124 buf[0] = Ctrl_V;
13125 sprintf((char *)buf + 1, "%ld", reglen + 1);
13126 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128 rettv->v_type = VAR_STRING;
13129 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130}
13131
13132/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013133 * "gettabvar()" function
13134 */
13135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013136f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013137{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013138 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013139 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013140 dictitem_T *v;
13141 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013142 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013143
13144 rettv->v_type = VAR_STRING;
13145 rettv->vval.v_string = NULL;
13146
13147 varname = get_tv_string_chk(&argvars[1]);
13148 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13149 if (tp != NULL && varname != NULL)
13150 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013151 /* Set tp to be our tabpage, temporarily. Also set the window to the
13152 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013153 if (switch_win(&oldcurwin, &oldtabpage,
13154 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013155 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013156 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013157 /* look up the variable */
13158 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13159 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13160 if (v != NULL)
13161 {
13162 copy_tv(&v->di_tv, rettv);
13163 done = TRUE;
13164 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013165 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013166
13167 /* restore previous notion of curwin */
13168 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013169 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013170
13171 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013172 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013173}
13174
13175/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013176 * "gettabwinvar()" function
13177 */
13178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013179f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013180{
13181 getwinvar(argvars, rettv, 1);
13182}
13183
13184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185 * "getwinposx()" function
13186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013188f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013190 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191#ifdef FEAT_GUI
13192 if (gui.in_use)
13193 {
13194 int x, y;
13195
13196 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013197 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198 }
13199#endif
13200}
13201
13202/*
13203 * "getwinposy()" function
13204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013206f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013208 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209#ifdef FEAT_GUI
13210 if (gui.in_use)
13211 {
13212 int x, y;
13213
13214 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013215 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 }
13217#endif
13218}
13219
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013220/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013221 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013222 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013223 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013224find_win_by_nr(
13225 typval_T *vp,
13226 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013227{
13228#ifdef FEAT_WINDOWS
13229 win_T *wp;
13230#endif
13231 int nr;
13232
13233 nr = get_tv_number_chk(vp, NULL);
13234
13235#ifdef FEAT_WINDOWS
13236 if (nr < 0)
13237 return NULL;
13238 if (nr == 0)
13239 return curwin;
13240
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013241 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13242 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013243 if (--nr <= 0)
13244 break;
13245 return wp;
13246#else
13247 if (nr == 0 || nr == 1)
13248 return curwin;
13249 return NULL;
13250#endif
13251}
13252
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013254 * Find window specified by "wvp" in tabpage "tvp".
13255 */
13256 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013257find_tabwin(
13258 typval_T *wvp, /* VAR_UNKNOWN for current window */
13259 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013260{
13261 win_T *wp = NULL;
13262 tabpage_T *tp = NULL;
13263 long n;
13264
13265 if (wvp->v_type != VAR_UNKNOWN)
13266 {
13267 if (tvp->v_type != VAR_UNKNOWN)
13268 {
13269 n = get_tv_number(tvp);
13270 if (n >= 0)
13271 tp = find_tabpage(n);
13272 }
13273 else
13274 tp = curtab;
13275
13276 if (tp != NULL)
13277 wp = find_win_by_nr(wvp, tp);
13278 }
13279 else
13280 wp = curwin;
13281
13282 return wp;
13283}
13284
13285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286 * "getwinvar()" function
13287 */
13288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013289f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013291 getwinvar(argvars, rettv, 0);
13292}
13293
13294/*
13295 * getwinvar() and gettabwinvar()
13296 */
13297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013298getwinvar(
13299 typval_T *argvars,
13300 typval_T *rettv,
13301 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013302{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013303 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013305 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013306 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013307 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013308#ifdef FEAT_WINDOWS
13309 win_T *oldcurwin;
13310 tabpage_T *oldtabpage;
13311 int need_switch_win;
13312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013314#ifdef FEAT_WINDOWS
13315 if (off == 1)
13316 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13317 else
13318 tp = curtab;
13319#endif
13320 win = find_win_by_nr(&argvars[off], tp);
13321 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013322 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013324 rettv->v_type = VAR_STRING;
13325 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326
13327 if (win != NULL && varname != NULL)
13328 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013329#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013330 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013331 * otherwise the window is not valid. Only do this when needed,
13332 * autocommands get blocked. */
13333 need_switch_win = !(tp == curtab && win == curwin);
13334 if (!need_switch_win
13335 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13336#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013337 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013338 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013339 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013340 if (get_option_tv(&varname, rettv, 1) == OK)
13341 done = TRUE;
13342 }
13343 else
13344 {
13345 /* Look up the variable. */
13346 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13347 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13348 varname, FALSE);
13349 if (v != NULL)
13350 {
13351 copy_tv(&v->di_tv, rettv);
13352 done = TRUE;
13353 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013356
Bram Moolenaarba117c22015-09-29 16:53:22 +020013357#ifdef FEAT_WINDOWS
13358 if (need_switch_win)
13359 /* restore previous notion of curwin */
13360 restore_win(oldcurwin, oldtabpage, TRUE);
13361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 }
13363
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013364 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13365 /* use the default return value */
13366 copy_tv(&argvars[off + 2], rettv);
13367
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368 --emsg_off;
13369}
13370
13371/*
13372 * "glob()" function
13373 */
13374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013375f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013377 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013379 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013381 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013382 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013383 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013384 if (argvars[1].v_type != VAR_UNKNOWN)
13385 {
13386 if (get_tv_number_chk(&argvars[1], &error))
13387 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013388 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013389 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013390 if (get_tv_number_chk(&argvars[2], &error))
13391 {
13392 rettv->v_type = VAR_LIST;
13393 rettv->vval.v_list = NULL;
13394 }
13395 if (argvars[3].v_type != VAR_UNKNOWN
13396 && get_tv_number_chk(&argvars[3], &error))
13397 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013398 }
13399 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013400 if (!error)
13401 {
13402 ExpandInit(&xpc);
13403 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013404 if (p_wic)
13405 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013406 if (rettv->v_type == VAR_STRING)
13407 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013408 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013409 else if (rettv_list_alloc(rettv) != FAIL)
13410 {
13411 int i;
13412
13413 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13414 NULL, options, WILD_ALL_KEEP);
13415 for (i = 0; i < xpc.xp_numfiles; i++)
13416 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13417
13418 ExpandCleanup(&xpc);
13419 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013420 }
13421 else
13422 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423}
13424
13425/*
13426 * "globpath()" function
13427 */
13428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013429f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013431 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013433 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013434 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013435 garray_T ga;
13436 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013438 /* When the optional second argument is non-zero, don't remove matches
13439 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013440 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013441 if (argvars[2].v_type != VAR_UNKNOWN)
13442 {
13443 if (get_tv_number_chk(&argvars[2], &error))
13444 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013445 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013446 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013447 if (get_tv_number_chk(&argvars[3], &error))
13448 {
13449 rettv->v_type = VAR_LIST;
13450 rettv->vval.v_list = NULL;
13451 }
13452 if (argvars[4].v_type != VAR_UNKNOWN
13453 && get_tv_number_chk(&argvars[4], &error))
13454 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013455 }
13456 }
13457 if (file != NULL && !error)
13458 {
13459 ga_init2(&ga, (int)sizeof(char_u *), 10);
13460 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13461 if (rettv->v_type == VAR_STRING)
13462 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13463 else if (rettv_list_alloc(rettv) != FAIL)
13464 for (i = 0; i < ga.ga_len; ++i)
13465 list_append_string(rettv->vval.v_list,
13466 ((char_u **)(ga.ga_data))[i], -1);
13467 ga_clear_strings(&ga);
13468 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013469 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013470 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471}
13472
13473/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013474 * "glob2regpat()" function
13475 */
13476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013477f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013478{
13479 char_u *pat = get_tv_string_chk(&argvars[0]);
13480
13481 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013482 rettv->vval.v_string = (pat == NULL)
13483 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013484}
13485
13486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487 * "has()" function
13488 */
13489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013490f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491{
13492 int i;
13493 char_u *name;
13494 int n = FALSE;
13495 static char *(has_list[]) =
13496 {
13497#ifdef AMIGA
13498 "amiga",
13499# ifdef FEAT_ARP
13500 "arp",
13501# endif
13502#endif
13503#ifdef __BEOS__
13504 "beos",
13505#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013506#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507 "mac",
13508#endif
13509#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013510 "macunix", /* built with 'darwin' enabled */
13511#endif
13512#if defined(__APPLE__) && __APPLE__ == 1
13513 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515#ifdef __QNX__
13516 "qnx",
13517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518#ifdef UNIX
13519 "unix",
13520#endif
13521#ifdef VMS
13522 "vms",
13523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524#ifdef WIN32
13525 "win32",
13526#endif
13527#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13528 "win32unix",
13529#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013530#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 "win64",
13532#endif
13533#ifdef EBCDIC
13534 "ebcdic",
13535#endif
13536#ifndef CASE_INSENSITIVE_FILENAME
13537 "fname_case",
13538#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013539#ifdef HAVE_ACL
13540 "acl",
13541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542#ifdef FEAT_ARABIC
13543 "arabic",
13544#endif
13545#ifdef FEAT_AUTOCMD
13546 "autocmd",
13547#endif
13548#ifdef FEAT_BEVAL
13549 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013550# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13551 "balloon_multiline",
13552# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553#endif
13554#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13555 "builtin_terms",
13556# ifdef ALL_BUILTIN_TCAPS
13557 "all_builtin_terms",
13558# endif
13559#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013560#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13561 || defined(FEAT_GUI_W32) \
13562 || defined(FEAT_GUI_MOTIF))
13563 "browsefilter",
13564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565#ifdef FEAT_BYTEOFF
13566 "byte_offset",
13567#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013568#ifdef FEAT_CHANNEL
13569 "channel",
13570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571#ifdef FEAT_CINDENT
13572 "cindent",
13573#endif
13574#ifdef FEAT_CLIENTSERVER
13575 "clientserver",
13576#endif
13577#ifdef FEAT_CLIPBOARD
13578 "clipboard",
13579#endif
13580#ifdef FEAT_CMDL_COMPL
13581 "cmdline_compl",
13582#endif
13583#ifdef FEAT_CMDHIST
13584 "cmdline_hist",
13585#endif
13586#ifdef FEAT_COMMENTS
13587 "comments",
13588#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013589#ifdef FEAT_CONCEAL
13590 "conceal",
13591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592#ifdef FEAT_CRYPT
13593 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013594 "crypt-blowfish",
13595 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596#endif
13597#ifdef FEAT_CSCOPE
13598 "cscope",
13599#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013600#ifdef FEAT_CURSORBIND
13601 "cursorbind",
13602#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013603#ifdef CURSOR_SHAPE
13604 "cursorshape",
13605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606#ifdef DEBUG
13607 "debug",
13608#endif
13609#ifdef FEAT_CON_DIALOG
13610 "dialog_con",
13611#endif
13612#ifdef FEAT_GUI_DIALOG
13613 "dialog_gui",
13614#endif
13615#ifdef FEAT_DIFF
13616 "diff",
13617#endif
13618#ifdef FEAT_DIGRAPHS
13619 "digraphs",
13620#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013621#ifdef FEAT_DIRECTX
13622 "directx",
13623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624#ifdef FEAT_DND
13625 "dnd",
13626#endif
13627#ifdef FEAT_EMACS_TAGS
13628 "emacs_tags",
13629#endif
13630 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013631 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632#ifdef FEAT_SEARCH_EXTRA
13633 "extra_search",
13634#endif
13635#ifdef FEAT_FKMAP
13636 "farsi",
13637#endif
13638#ifdef FEAT_SEARCHPATH
13639 "file_in_path",
13640#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013641#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013642 "filterpipe",
13643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644#ifdef FEAT_FIND_ID
13645 "find_in_path",
13646#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013647#ifdef FEAT_FLOAT
13648 "float",
13649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650#ifdef FEAT_FOLDING
13651 "folding",
13652#endif
13653#ifdef FEAT_FOOTER
13654 "footer",
13655#endif
13656#if !defined(USE_SYSTEM) && defined(UNIX)
13657 "fork",
13658#endif
13659#ifdef FEAT_GETTEXT
13660 "gettext",
13661#endif
13662#ifdef FEAT_GUI
13663 "gui",
13664#endif
13665#ifdef FEAT_GUI_ATHENA
13666# ifdef FEAT_GUI_NEXTAW
13667 "gui_neXtaw",
13668# else
13669 "gui_athena",
13670# endif
13671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672#ifdef FEAT_GUI_GTK
13673 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013676#ifdef FEAT_GUI_GNOME
13677 "gui_gnome",
13678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679#ifdef FEAT_GUI_MAC
13680 "gui_mac",
13681#endif
13682#ifdef FEAT_GUI_MOTIF
13683 "gui_motif",
13684#endif
13685#ifdef FEAT_GUI_PHOTON
13686 "gui_photon",
13687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688#ifdef FEAT_GUI_W32
13689 "gui_win32",
13690#endif
13691#ifdef FEAT_HANGULIN
13692 "hangul_input",
13693#endif
13694#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13695 "iconv",
13696#endif
13697#ifdef FEAT_INS_EXPAND
13698 "insert_expand",
13699#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013700#ifdef FEAT_JOB
13701 "job",
13702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703#ifdef FEAT_JUMPLIST
13704 "jumplist",
13705#endif
13706#ifdef FEAT_KEYMAP
13707 "keymap",
13708#endif
13709#ifdef FEAT_LANGMAP
13710 "langmap",
13711#endif
13712#ifdef FEAT_LIBCALL
13713 "libcall",
13714#endif
13715#ifdef FEAT_LINEBREAK
13716 "linebreak",
13717#endif
13718#ifdef FEAT_LISP
13719 "lispindent",
13720#endif
13721#ifdef FEAT_LISTCMDS
13722 "listcmds",
13723#endif
13724#ifdef FEAT_LOCALMAP
13725 "localmap",
13726#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013727#ifdef FEAT_LUA
13728# ifndef DYNAMIC_LUA
13729 "lua",
13730# endif
13731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732#ifdef FEAT_MENU
13733 "menu",
13734#endif
13735#ifdef FEAT_SESSION
13736 "mksession",
13737#endif
13738#ifdef FEAT_MODIFY_FNAME
13739 "modify_fname",
13740#endif
13741#ifdef FEAT_MOUSE
13742 "mouse",
13743#endif
13744#ifdef FEAT_MOUSESHAPE
13745 "mouseshape",
13746#endif
13747#if defined(UNIX) || defined(VMS)
13748# ifdef FEAT_MOUSE_DEC
13749 "mouse_dec",
13750# endif
13751# ifdef FEAT_MOUSE_GPM
13752 "mouse_gpm",
13753# endif
13754# ifdef FEAT_MOUSE_JSB
13755 "mouse_jsbterm",
13756# endif
13757# ifdef FEAT_MOUSE_NET
13758 "mouse_netterm",
13759# endif
13760# ifdef FEAT_MOUSE_PTERM
13761 "mouse_pterm",
13762# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013763# ifdef FEAT_MOUSE_SGR
13764 "mouse_sgr",
13765# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013766# ifdef FEAT_SYSMOUSE
13767 "mouse_sysmouse",
13768# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013769# ifdef FEAT_MOUSE_URXVT
13770 "mouse_urxvt",
13771# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772# ifdef FEAT_MOUSE_XTERM
13773 "mouse_xterm",
13774# endif
13775#endif
13776#ifdef FEAT_MBYTE
13777 "multi_byte",
13778#endif
13779#ifdef FEAT_MBYTE_IME
13780 "multi_byte_ime",
13781#endif
13782#ifdef FEAT_MULTI_LANG
13783 "multi_lang",
13784#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013785#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013786#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013787 "mzscheme",
13788#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790#ifdef FEAT_OLE
13791 "ole",
13792#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013793 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794#ifdef FEAT_PATH_EXTRA
13795 "path_extra",
13796#endif
13797#ifdef FEAT_PERL
13798#ifndef DYNAMIC_PERL
13799 "perl",
13800#endif
13801#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013802#ifdef FEAT_PERSISTENT_UNDO
13803 "persistent_undo",
13804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805#ifdef FEAT_PYTHON
13806#ifndef DYNAMIC_PYTHON
13807 "python",
13808#endif
13809#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013810#ifdef FEAT_PYTHON3
13811#ifndef DYNAMIC_PYTHON3
13812 "python3",
13813#endif
13814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815#ifdef FEAT_POSTSCRIPT
13816 "postscript",
13817#endif
13818#ifdef FEAT_PRINTER
13819 "printer",
13820#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013821#ifdef FEAT_PROFILE
13822 "profile",
13823#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013824#ifdef FEAT_RELTIME
13825 "reltime",
13826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827#ifdef FEAT_QUICKFIX
13828 "quickfix",
13829#endif
13830#ifdef FEAT_RIGHTLEFT
13831 "rightleft",
13832#endif
13833#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13834 "ruby",
13835#endif
13836#ifdef FEAT_SCROLLBIND
13837 "scrollbind",
13838#endif
13839#ifdef FEAT_CMDL_INFO
13840 "showcmd",
13841 "cmdline_info",
13842#endif
13843#ifdef FEAT_SIGNS
13844 "signs",
13845#endif
13846#ifdef FEAT_SMARTINDENT
13847 "smartindent",
13848#endif
13849#ifdef FEAT_SNIFF
13850 "sniff",
13851#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013852#ifdef STARTUPTIME
13853 "startuptime",
13854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855#ifdef FEAT_STL_OPT
13856 "statusline",
13857#endif
13858#ifdef FEAT_SUN_WORKSHOP
13859 "sun_workshop",
13860#endif
13861#ifdef FEAT_NETBEANS_INTG
13862 "netbeans_intg",
13863#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013864#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013865 "spell",
13866#endif
13867#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 "syntax",
13869#endif
13870#if defined(USE_SYSTEM) || !defined(UNIX)
13871 "system",
13872#endif
13873#ifdef FEAT_TAG_BINS
13874 "tag_binary",
13875#endif
13876#ifdef FEAT_TAG_OLDSTATIC
13877 "tag_old_static",
13878#endif
13879#ifdef FEAT_TAG_ANYWHITE
13880 "tag_any_white",
13881#endif
13882#ifdef FEAT_TCL
13883# ifndef DYNAMIC_TCL
13884 "tcl",
13885# endif
13886#endif
13887#ifdef TERMINFO
13888 "terminfo",
13889#endif
13890#ifdef FEAT_TERMRESPONSE
13891 "termresponse",
13892#endif
13893#ifdef FEAT_TEXTOBJ
13894 "textobjects",
13895#endif
13896#ifdef HAVE_TGETENT
13897 "tgetent",
13898#endif
13899#ifdef FEAT_TITLE
13900 "title",
13901#endif
13902#ifdef FEAT_TOOLBAR
13903 "toolbar",
13904#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013905#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13906 "unnamedplus",
13907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908#ifdef FEAT_USR_CMDS
13909 "user-commands", /* was accidentally included in 5.4 */
13910 "user_commands",
13911#endif
13912#ifdef FEAT_VIMINFO
13913 "viminfo",
13914#endif
13915#ifdef FEAT_VERTSPLIT
13916 "vertsplit",
13917#endif
13918#ifdef FEAT_VIRTUALEDIT
13919 "virtualedit",
13920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922#ifdef FEAT_VISUALEXTRA
13923 "visualextra",
13924#endif
13925#ifdef FEAT_VREPLACE
13926 "vreplace",
13927#endif
13928#ifdef FEAT_WILDIGN
13929 "wildignore",
13930#endif
13931#ifdef FEAT_WILDMENU
13932 "wildmenu",
13933#endif
13934#ifdef FEAT_WINDOWS
13935 "windows",
13936#endif
13937#ifdef FEAT_WAK
13938 "winaltkeys",
13939#endif
13940#ifdef FEAT_WRITEBACKUP
13941 "writebackup",
13942#endif
13943#ifdef FEAT_XIM
13944 "xim",
13945#endif
13946#ifdef FEAT_XFONTSET
13947 "xfontset",
13948#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013949#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013950 "xpm",
13951 "xpm_w32", /* for backward compatibility */
13952#else
13953# if defined(HAVE_XPM)
13954 "xpm",
13955# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957#ifdef USE_XSMP
13958 "xsmp",
13959#endif
13960#ifdef USE_XSMP_INTERACT
13961 "xsmp_interact",
13962#endif
13963#ifdef FEAT_XCLIPBOARD
13964 "xterm_clipboard",
13965#endif
13966#ifdef FEAT_XTERM_SAVE
13967 "xterm_save",
13968#endif
13969#if defined(UNIX) && defined(FEAT_X11)
13970 "X11",
13971#endif
13972 NULL
13973 };
13974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 for (i = 0; has_list[i] != NULL; ++i)
13977 if (STRICMP(name, has_list[i]) == 0)
13978 {
13979 n = TRUE;
13980 break;
13981 }
13982
13983 if (n == FALSE)
13984 {
13985 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013986 {
13987 if (name[5] == '-'
13988 && STRLEN(name) > 11
13989 && vim_isdigit(name[6])
13990 && vim_isdigit(name[8])
13991 && vim_isdigit(name[10]))
13992 {
13993 int major = atoi((char *)name + 6);
13994 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013995
13996 /* Expect "patch-9.9.01234". */
13997 n = (major < VIM_VERSION_MAJOR
13998 || (major == VIM_VERSION_MAJOR
13999 && (minor < VIM_VERSION_MINOR
14000 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014001 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014002 }
14003 else
14004 n = has_patch(atoi((char *)name + 5));
14005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006 else if (STRICMP(name, "vim_starting") == 0)
14007 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014008#ifdef FEAT_MBYTE
14009 else if (STRICMP(name, "multi_byte_encoding") == 0)
14010 n = has_mbyte;
14011#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014012#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14013 else if (STRICMP(name, "balloon_multiline") == 0)
14014 n = multiline_balloon_available();
14015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016#ifdef DYNAMIC_TCL
14017 else if (STRICMP(name, "tcl") == 0)
14018 n = tcl_enabled(FALSE);
14019#endif
14020#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14021 else if (STRICMP(name, "iconv") == 0)
14022 n = iconv_enabled(FALSE);
14023#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014024#ifdef DYNAMIC_LUA
14025 else if (STRICMP(name, "lua") == 0)
14026 n = lua_enabled(FALSE);
14027#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014028#ifdef DYNAMIC_MZSCHEME
14029 else if (STRICMP(name, "mzscheme") == 0)
14030 n = mzscheme_enabled(FALSE);
14031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032#ifdef DYNAMIC_RUBY
14033 else if (STRICMP(name, "ruby") == 0)
14034 n = ruby_enabled(FALSE);
14035#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014036#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037#ifdef DYNAMIC_PYTHON
14038 else if (STRICMP(name, "python") == 0)
14039 n = python_enabled(FALSE);
14040#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014041#endif
14042#ifdef FEAT_PYTHON3
14043#ifdef DYNAMIC_PYTHON3
14044 else if (STRICMP(name, "python3") == 0)
14045 n = python3_enabled(FALSE);
14046#endif
14047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048#ifdef DYNAMIC_PERL
14049 else if (STRICMP(name, "perl") == 0)
14050 n = perl_enabled(FALSE);
14051#endif
14052#ifdef FEAT_GUI
14053 else if (STRICMP(name, "gui_running") == 0)
14054 n = (gui.in_use || gui.starting);
14055# ifdef FEAT_GUI_W32
14056 else if (STRICMP(name, "gui_win32s") == 0)
14057 n = gui_is_win32s();
14058# endif
14059# ifdef FEAT_BROWSE
14060 else if (STRICMP(name, "browse") == 0)
14061 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14062# endif
14063#endif
14064#ifdef FEAT_SYN_HL
14065 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014066 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067#endif
14068#if defined(WIN3264)
14069 else if (STRICMP(name, "win95") == 0)
14070 n = mch_windows95();
14071#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014072#ifdef FEAT_NETBEANS_INTG
14073 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014074 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 }
14077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014078 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079}
14080
14081/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014082 * "has_key()" function
14083 */
14084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014085f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014086{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014087 if (argvars[0].v_type != VAR_DICT)
14088 {
14089 EMSG(_(e_dictreq));
14090 return;
14091 }
14092 if (argvars[0].vval.v_dict == NULL)
14093 return;
14094
14095 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014096 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014097}
14098
14099/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014100 * "haslocaldir()" function
14101 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014103f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014104{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014105 win_T *wp = NULL;
14106
14107 wp = find_tabwin(&argvars[0], &argvars[1]);
14108 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014109}
14110
14111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112 * "hasmapto()" function
14113 */
14114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014115f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116{
14117 char_u *name;
14118 char_u *mode;
14119 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014120 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014122 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014123 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 mode = (char_u *)"nvo";
14125 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014127 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014128 if (argvars[2].v_type != VAR_UNKNOWN)
14129 abbr = get_tv_number(&argvars[2]);
14130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131
Bram Moolenaar2c932302006-03-18 21:42:09 +000014132 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014133 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014135 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136}
14137
14138/*
14139 * "histadd()" function
14140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014142f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143{
14144#ifdef FEAT_CMDHIST
14145 int histype;
14146 char_u *str;
14147 char_u buf[NUMBUFLEN];
14148#endif
14149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014150 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 if (check_restricted() || check_secure())
14152 return;
14153#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014154 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14155 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156 if (histype >= 0)
14157 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014158 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159 if (*str != NUL)
14160 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014161 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014163 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164 return;
14165 }
14166 }
14167#endif
14168}
14169
14170/*
14171 * "histdel()" function
14172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014174f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175{
14176#ifdef FEAT_CMDHIST
14177 int n;
14178 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014179 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014181 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14182 if (str == NULL)
14183 n = 0;
14184 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014186 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014187 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014190 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191 else
14192 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014194 get_tv_string_buf(&argvars[1], buf));
14195 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196#endif
14197}
14198
14199/*
14200 * "histget()" function
14201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014203f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204{
14205#ifdef FEAT_CMDHIST
14206 int type;
14207 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014210 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14211 if (str == NULL)
14212 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014214 {
14215 type = get_histtype(str);
14216 if (argvars[1].v_type == VAR_UNKNOWN)
14217 idx = get_history_idx(type);
14218 else
14219 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14220 /* -1 on type error */
14221 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014224 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014226 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227}
14228
14229/*
14230 * "histnr()" function
14231 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014233f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234{
14235 int i;
14236
14237#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 char_u *history = get_tv_string_chk(&argvars[0]);
14239
14240 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 if (i >= HIST_CMD && i < HIST_COUNT)
14242 i = get_history_idx(i);
14243 else
14244#endif
14245 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014246 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247}
14248
14249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250 * "highlightID(name)" function
14251 */
14252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014253f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014255 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256}
14257
14258/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014259 * "highlight_exists()" function
14260 */
14261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014262f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014263{
14264 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14265}
14266
14267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268 * "hostname()" function
14269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014271f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272{
14273 char_u hostname[256];
14274
14275 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014276 rettv->v_type = VAR_STRING;
14277 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278}
14279
14280/*
14281 * iconv() function
14282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014284f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285{
14286#ifdef FEAT_MBYTE
14287 char_u buf1[NUMBUFLEN];
14288 char_u buf2[NUMBUFLEN];
14289 char_u *from, *to, *str;
14290 vimconv_T vimconv;
14291#endif
14292
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014293 rettv->v_type = VAR_STRING;
14294 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295
14296#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014297 str = get_tv_string(&argvars[0]);
14298 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14299 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300 vimconv.vc_type = CONV_NONE;
14301 convert_setup(&vimconv, from, to);
14302
14303 /* If the encodings are equal, no conversion needed. */
14304 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014305 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014307 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308
14309 convert_setup(&vimconv, NULL, NULL);
14310 vim_free(from);
14311 vim_free(to);
14312#endif
14313}
14314
14315/*
14316 * "indent()" function
14317 */
14318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014319f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320{
14321 linenr_T lnum;
14322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014323 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014325 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328}
14329
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014330/*
14331 * "index()" function
14332 */
14333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014334f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014335{
Bram Moolenaar33570922005-01-25 22:26:29 +000014336 list_T *l;
14337 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014338 long idx = 0;
14339 int ic = FALSE;
14340
14341 rettv->vval.v_number = -1;
14342 if (argvars[0].v_type != VAR_LIST)
14343 {
14344 EMSG(_(e_listreq));
14345 return;
14346 }
14347 l = argvars[0].vval.v_list;
14348 if (l != NULL)
14349 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014350 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014351 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014353 int error = FALSE;
14354
Bram Moolenaar758711c2005-02-02 23:11:38 +000014355 /* Start at specified item. Use the cached index that list_find()
14356 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014358 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014359 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014360 ic = get_tv_number_chk(&argvars[3], &error);
14361 if (error)
14362 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014363 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014364
Bram Moolenaar758711c2005-02-02 23:11:38 +000014365 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014366 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014367 {
14368 rettv->vval.v_number = idx;
14369 break;
14370 }
14371 }
14372}
14373
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374static int inputsecret_flag = 0;
14375
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014376static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014377
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014379 * This function is used by f_input() and f_inputdialog() functions. The third
14380 * argument to f_input() specifies the type of completion to use at the
14381 * prompt. The third argument to f_inputdialog() specifies the value to return
14382 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 */
14384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014385get_user_input(
14386 typval_T *argvars,
14387 typval_T *rettv,
14388 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014390 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 char_u *p = NULL;
14392 int c;
14393 char_u buf[NUMBUFLEN];
14394 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014395 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014396 int xp_type = EXPAND_NOTHING;
14397 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014399 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014400 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401
14402#ifdef NO_CONSOLE_INPUT
14403 /* While starting up, there is no place to enter text. */
14404 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406#endif
14407
14408 cmd_silent = FALSE; /* Want to see the prompt. */
14409 if (prompt != NULL)
14410 {
14411 /* Only the part of the message after the last NL is considered as
14412 * prompt for the command line */
14413 p = vim_strrchr(prompt, '\n');
14414 if (p == NULL)
14415 p = prompt;
14416 else
14417 {
14418 ++p;
14419 c = *p;
14420 *p = NUL;
14421 msg_start();
14422 msg_clr_eos();
14423 msg_puts_attr(prompt, echo_attr);
14424 msg_didout = FALSE;
14425 msg_starthere();
14426 *p = c;
14427 }
14428 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014430 if (argvars[1].v_type != VAR_UNKNOWN)
14431 {
14432 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14433 if (defstr != NULL)
14434 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014436 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014437 {
14438 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014439 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014440 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014441
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014442 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014443 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014444
Bram Moolenaar4463f292005-09-25 22:20:24 +000014445 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14446 if (xp_name == NULL)
14447 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014448
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014449 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014450
Bram Moolenaar4463f292005-09-25 22:20:24 +000014451 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14452 &xp_arg) == FAIL)
14453 return;
14454 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014455 }
14456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014457 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014458 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014459 int save_ex_normal_busy = ex_normal_busy;
14460 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014461 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014462 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14463 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014464 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014465 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014466 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014467 && argvars[1].v_type != VAR_UNKNOWN
14468 && argvars[2].v_type != VAR_UNKNOWN)
14469 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14470 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014471
14472 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014474 /* since the user typed this, no need to wait for return */
14475 need_wait_return = FALSE;
14476 msg_didout = FALSE;
14477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 cmd_silent = cmd_silent_save;
14479}
14480
14481/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014482 * "input()" function
14483 * Also handles inputsecret() when inputsecret is set.
14484 */
14485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014486f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014487{
14488 get_user_input(argvars, rettv, FALSE);
14489}
14490
14491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492 * "inputdialog()" function
14493 */
14494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014495f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496{
14497#if defined(FEAT_GUI_TEXTDIALOG)
14498 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14499 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14500 {
14501 char_u *message;
14502 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014503 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014505 message = get_tv_string_chk(&argvars[0]);
14506 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014507 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014508 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 else
14510 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014511 if (message != NULL && defstr != NULL
14512 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014513 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014514 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 else
14516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014517 if (message != NULL && defstr != NULL
14518 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014519 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014520 rettv->vval.v_string = vim_strsave(
14521 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014523 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014525 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526 }
14527 else
14528#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014529 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530}
14531
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014532/*
14533 * "inputlist()" function
14534 */
14535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014536f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014537{
14538 listitem_T *li;
14539 int selected;
14540 int mouse_used;
14541
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014542#ifdef NO_CONSOLE_INPUT
14543 /* While starting up, there is no place to enter text. */
14544 if (no_console_input())
14545 return;
14546#endif
14547 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14548 {
14549 EMSG2(_(e_listarg), "inputlist()");
14550 return;
14551 }
14552
14553 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014554 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014555 lines_left = Rows; /* avoid more prompt */
14556 msg_scroll = TRUE;
14557 msg_clr_eos();
14558
14559 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14560 {
14561 msg_puts(get_tv_string(&li->li_tv));
14562 msg_putchar('\n');
14563 }
14564
14565 /* Ask for choice. */
14566 selected = prompt_for_number(&mouse_used);
14567 if (mouse_used)
14568 selected -= lines_left;
14569
14570 rettv->vval.v_number = selected;
14571}
14572
14573
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14575
14576/*
14577 * "inputrestore()" function
14578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014580f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581{
14582 if (ga_userinput.ga_len > 0)
14583 {
14584 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14586 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014587 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 }
14589 else if (p_verbose > 1)
14590 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014591 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014592 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 }
14594}
14595
14596/*
14597 * "inputsave()" function
14598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014600f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014602 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603 if (ga_grow(&ga_userinput, 1) == OK)
14604 {
14605 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14606 + ga_userinput.ga_len);
14607 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014608 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609 }
14610 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014611 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612}
14613
14614/*
14615 * "inputsecret()" function
14616 */
14617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014618f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619{
14620 ++cmdline_star;
14621 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014622 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623 --cmdline_star;
14624 --inputsecret_flag;
14625}
14626
14627/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014628 * "insert()" function
14629 */
14630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014631f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014632{
14633 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014634 listitem_T *item;
14635 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014637
14638 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014639 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014640 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014641 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014642 {
14643 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014644 before = get_tv_number_chk(&argvars[2], &error);
14645 if (error)
14646 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014647
Bram Moolenaar758711c2005-02-02 23:11:38 +000014648 if (before == l->lv_len)
14649 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014650 else
14651 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014652 item = list_find(l, before);
14653 if (item == NULL)
14654 {
14655 EMSGN(_(e_listidx), before);
14656 l = NULL;
14657 }
14658 }
14659 if (l != NULL)
14660 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014661 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014662 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014663 }
14664 }
14665}
14666
14667/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014668 * "invert(expr)" function
14669 */
14670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014671f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014672{
14673 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14674}
14675
14676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 * "isdirectory()" function
14678 */
14679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014680f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683}
14684
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014685/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014686 * "islocked()" function
14687 */
14688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014689f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014690{
14691 lval_T lv;
14692 char_u *end;
14693 dictitem_T *di;
14694
14695 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014696 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14697 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014698 if (end != NULL && lv.ll_name != NULL)
14699 {
14700 if (*end != NUL)
14701 EMSG(_(e_trailing));
14702 else
14703 {
14704 if (lv.ll_tv == NULL)
14705 {
14706 if (check_changedtick(lv.ll_name))
14707 rettv->vval.v_number = 1; /* always locked */
14708 else
14709 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014710 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014711 if (di != NULL)
14712 {
14713 /* Consider a variable locked when:
14714 * 1. the variable itself is locked
14715 * 2. the value of the variable is locked.
14716 * 3. the List or Dict value is locked.
14717 */
14718 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14719 || tv_islocked(&di->di_tv));
14720 }
14721 }
14722 }
14723 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014724 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014725 else if (lv.ll_newkey != NULL)
14726 EMSG2(_(e_dictkey), lv.ll_newkey);
14727 else if (lv.ll_list != NULL)
14728 /* List item. */
14729 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14730 else
14731 /* Dictionary item. */
14732 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14733 }
14734 }
14735
14736 clear_lval(&lv);
14737}
14738
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014739static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014740
14741/*
14742 * Turn a dict into a list:
14743 * "what" == 0: list of keys
14744 * "what" == 1: list of values
14745 * "what" == 2: list of items
14746 */
14747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014748dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014749{
Bram Moolenaar33570922005-01-25 22:26:29 +000014750 list_T *l2;
14751 dictitem_T *di;
14752 hashitem_T *hi;
14753 listitem_T *li;
14754 listitem_T *li2;
14755 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014756 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014757
Bram Moolenaar8c711452005-01-14 21:53:12 +000014758 if (argvars[0].v_type != VAR_DICT)
14759 {
14760 EMSG(_(e_dictreq));
14761 return;
14762 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014763 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014764 return;
14765
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014766 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014767 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014768
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014769 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014770 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014771 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014772 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014773 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014774 --todo;
14775 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014776
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014777 li = listitem_alloc();
14778 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014779 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014780 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014781
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014782 if (what == 0)
14783 {
14784 /* keys() */
14785 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014786 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014787 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14788 }
14789 else if (what == 1)
14790 {
14791 /* values() */
14792 copy_tv(&di->di_tv, &li->li_tv);
14793 }
14794 else
14795 {
14796 /* items() */
14797 l2 = list_alloc();
14798 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014799 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014800 li->li_tv.vval.v_list = l2;
14801 if (l2 == NULL)
14802 break;
14803 ++l2->lv_refcount;
14804
14805 li2 = listitem_alloc();
14806 if (li2 == NULL)
14807 break;
14808 list_append(l2, li2);
14809 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014810 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014811 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14812
14813 li2 = listitem_alloc();
14814 if (li2 == NULL)
14815 break;
14816 list_append(l2, li2);
14817 copy_tv(&di->di_tv, &li2->li_tv);
14818 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014819 }
14820 }
14821}
14822
14823/*
14824 * "items(dict)" function
14825 */
14826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014827f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014828{
14829 dict_list(argvars, rettv, 2);
14830}
14831
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014832#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014833/*
14834 * Get the job from the argument.
14835 * Returns NULL if the job is invalid.
14836 */
14837 static job_T *
14838get_job_arg(typval_T *tv)
14839{
14840 job_T *job;
14841
14842 if (tv->v_type != VAR_JOB)
14843 {
14844 EMSG2(_(e_invarg2), get_tv_string(tv));
14845 return NULL;
14846 }
14847 job = tv->vval.v_job;
14848
14849 if (job == NULL)
14850 EMSG(_("E916: not a valid job"));
14851 return job;
14852}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014853
14854# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014855/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014856 * "job_getchannel()" function
14857 */
14858 static void
14859f_job_getchannel(typval_T *argvars, typval_T *rettv)
14860{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014861 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014862
Bram Moolenaar65edff82016-02-21 16:40:11 +010014863 if (job != NULL)
14864 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014865 rettv->v_type = VAR_CHANNEL;
14866 rettv->vval.v_channel = job->jv_channel;
14867 if (job->jv_channel != NULL)
14868 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014869 }
14870}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014871# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014872
14873/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014874 * "job_setoptions()" function
14875 */
14876 static void
14877f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14878{
14879 job_T *job = get_job_arg(&argvars[0]);
14880 jobopt_T opt;
14881
14882 if (job == NULL)
14883 return;
14884 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014885 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014886 return;
14887 job_set_options(job, &opt);
14888}
14889
14890/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014891 * "job_start()" function
14892 */
14893 static void
14894f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14895{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014896 job_T *job;
14897 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014898#if defined(UNIX)
14899# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014900 char **argv = NULL;
14901 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014902#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014903 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014904#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014905 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014906
14907 rettv->v_type = VAR_JOB;
14908 job = job_alloc();
14909 rettv->vval.v_job = job;
14910 if (job == NULL)
14911 return;
14912
14913 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014914
14915 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010014916 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014917 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010014918 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014919 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
14920 + JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014921 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010014922 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014923
Bram Moolenaar835dc632016-02-07 14:27:38 +010014924#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014925 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014926#endif
14927
14928 if (argvars[0].v_type == VAR_STRING)
14929 {
14930 /* Command is a string. */
14931 cmd = argvars[0].vval.v_string;
14932#ifdef USE_ARGV
14933 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14934 return;
14935 argv[argc] = NULL;
14936#endif
14937 }
14938 else if (argvars[0].v_type != VAR_LIST
14939 || argvars[0].vval.v_list == NULL
14940 || argvars[0].vval.v_list->lv_len < 1)
14941 {
14942 EMSG(_(e_invarg));
14943 return;
14944 }
14945 else
14946 {
14947 list_T *l = argvars[0].vval.v_list;
14948 listitem_T *li;
14949 char_u *s;
14950
14951#ifdef USE_ARGV
14952 /* Pass argv[] to mch_call_shell(). */
14953 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14954 if (argv == NULL)
14955 return;
14956#endif
14957 for (li = l->lv_first; li != NULL; li = li->li_next)
14958 {
14959 s = get_tv_string_chk(&li->li_tv);
14960 if (s == NULL)
14961 goto theend;
14962#ifdef USE_ARGV
14963 argv[argc++] = (char *)s;
14964#else
14965 if (li != l->lv_first)
14966 {
14967 s = vim_strsave_shellescape(s, FALSE, TRUE);
14968 if (s == NULL)
14969 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014970 ga_concat(&ga, s);
14971 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014972 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014973 else
14974 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014975 if (li->li_next != NULL)
14976 ga_append(&ga, ' ');
14977#endif
14978 }
14979#ifdef USE_ARGV
14980 argv[argc] = NULL;
14981#else
14982 cmd = ga.ga_data;
14983#endif
14984 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014985
Bram Moolenaar835dc632016-02-07 14:27:38 +010014986#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014987# ifdef FEAT_CHANNEL
14988 if (ch_log_active())
14989 {
14990 garray_T ga;
14991 int i;
14992
14993 ga_init2(&ga, (int)sizeof(char), 200);
14994 for (i = 0; i < argc; ++i)
14995 {
14996 if (i > 0)
14997 ga_concat(&ga, (char_u *)" ");
14998 ga_concat(&ga, (char_u *)argv[i]);
14999 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015000 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015001 ga_clear(&ga);
15002 }
15003# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015004 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015005#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015006# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015007 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015008# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015009 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015010#endif
15011
15012theend:
15013#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015014 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015015#else
15016 vim_free(ga.ga_data);
15017#endif
15018}
15019
15020/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015021 * Get the status of "job" and invoke the exit callback when needed.
15022 * The returned string is not allocated.
15023 */
15024 static char *
15025job_status(job_T *job)
15026{
15027 char *result;
15028
15029 if (job->jv_status == JOB_ENDED)
15030 /* No need to check, dead is dead. */
15031 result = "dead";
15032 else if (job->jv_status == JOB_FAILED)
15033 result = "fail";
15034 else
15035 {
15036 result = mch_job_status(job);
15037# ifdef FEAT_CHANNEL
15038 if (job->jv_status == JOB_ENDED)
15039 ch_log(job->jv_channel, "Job ended");
15040# endif
15041 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15042 {
15043 typval_T argv[3];
15044 typval_T rettv;
15045 int dummy;
15046
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015047 /* invoke the exit callback; make sure the refcount is > 0 */
15048 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015049 argv[0].v_type = VAR_JOB;
15050 argv[0].vval.v_job = job;
15051 argv[1].v_type = VAR_NUMBER;
15052 argv[1].vval.v_number = job->jv_exitval;
15053 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15054 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15055 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015056 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015057 }
15058 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15059 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015060 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015061 * freed. Careful: caller must not use "job" after this! */
15062 job_free(job);
15063 }
15064 }
15065 return result;
15066}
15067
15068/*
15069 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15070 */
15071 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015072job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015073{
15074 static time_t last_check = 0;
15075 time_t now;
15076 job_T *job;
15077 job_T *next;
15078
15079 /* Only do this once in 10 seconds. */
15080 now = time(NULL);
15081 if (last_check + 10 < now)
15082 {
15083 last_check = now;
15084 for (job = first_job; job != NULL; job = next)
15085 {
15086 next = job->jv_next;
15087 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15088 job_status(job); /* may free "job" */
15089 }
15090 }
15091}
15092
15093/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015094 * "job_status()" function
15095 */
15096 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015097f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015098{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015099 job_T *job = get_job_arg(&argvars[0]);
15100 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015101
Bram Moolenaar65edff82016-02-21 16:40:11 +010015102 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015103 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015104 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015105 rettv->v_type = VAR_STRING;
15106 rettv->vval.v_string = vim_strsave((char_u *)result);
15107 }
15108}
15109
15110/*
15111 * "job_stop()" function
15112 */
15113 static void
15114f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15115{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015116 job_T *job = get_job_arg(&argvars[0]);
15117
15118 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015119 {
15120 char_u *arg;
15121
15122 if (argvars[1].v_type == VAR_UNKNOWN)
15123 arg = (char_u *)"";
15124 else
15125 {
15126 arg = get_tv_string_chk(&argvars[1]);
15127 if (arg == NULL)
15128 {
15129 EMSG(_(e_invarg));
15130 return;
15131 }
15132 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010015133 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015134 rettv->vval.v_number = 0;
15135 else
15136 rettv->vval.v_number = 1;
15137 }
15138}
15139#endif
15140
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015142 * "join()" function
15143 */
15144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015145f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015146{
15147 garray_T ga;
15148 char_u *sep;
15149
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015150 if (argvars[0].v_type != VAR_LIST)
15151 {
15152 EMSG(_(e_listreq));
15153 return;
15154 }
15155 if (argvars[0].vval.v_list == NULL)
15156 return;
15157 if (argvars[1].v_type == VAR_UNKNOWN)
15158 sep = (char_u *)" ";
15159 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015160 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015161
15162 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015163
15164 if (sep != NULL)
15165 {
15166 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015167 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015168 ga_append(&ga, NUL);
15169 rettv->vval.v_string = (char_u *)ga.ga_data;
15170 }
15171 else
15172 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015173}
15174
15175/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015176 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015177 */
15178 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015179f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015180{
15181 js_read_T reader;
15182
15183 reader.js_buf = get_tv_string(&argvars[0]);
15184 reader.js_fill = NULL;
15185 reader.js_used = 0;
15186 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15187 EMSG(_(e_invarg));
15188}
15189
15190/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015191 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015192 */
15193 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015194f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015195{
15196 rettv->v_type = VAR_STRING;
15197 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15198}
15199
15200/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015201 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015202 */
15203 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015204f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015205{
15206 js_read_T reader;
15207
15208 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015209 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015210 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015211 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015212 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015213}
15214
15215/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015216 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015217 */
15218 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015219f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015220{
15221 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015222 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015223}
15224
15225/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015226 * "keys()" function
15227 */
15228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015229f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015230{
15231 dict_list(argvars, rettv, 0);
15232}
15233
15234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235 * "last_buffer_nr()" function.
15236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015238f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015239{
15240 int n = 0;
15241 buf_T *buf;
15242
15243 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15244 if (n < buf->b_fnum)
15245 n = buf->b_fnum;
15246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015247 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015248}
15249
15250/*
15251 * "len()" function
15252 */
15253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015254f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015255{
15256 switch (argvars[0].v_type)
15257 {
15258 case VAR_STRING:
15259 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015260 rettv->vval.v_number = (varnumber_T)STRLEN(
15261 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015262 break;
15263 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015264 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015265 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015266 case VAR_DICT:
15267 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15268 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015269 case VAR_UNKNOWN:
15270 case VAR_SPECIAL:
15271 case VAR_FLOAT:
15272 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015273 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015274 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015275 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015276 break;
15277 }
15278}
15279
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015280static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015281
15282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015283libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015284{
15285#ifdef FEAT_LIBCALL
15286 char_u *string_in;
15287 char_u **string_result;
15288 int nr_result;
15289#endif
15290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015291 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015292 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015293 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015294
15295 if (check_restricted() || check_secure())
15296 return;
15297
15298#ifdef FEAT_LIBCALL
15299 /* The first two args must be strings, otherwise its meaningless */
15300 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15301 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015302 string_in = NULL;
15303 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015304 string_in = argvars[2].vval.v_string;
15305 if (type == VAR_NUMBER)
15306 string_result = NULL;
15307 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015308 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015309 if (mch_libcall(argvars[0].vval.v_string,
15310 argvars[1].vval.v_string,
15311 string_in,
15312 argvars[2].vval.v_number,
15313 string_result,
15314 &nr_result) == OK
15315 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015316 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015317 }
15318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319}
15320
15321/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015322 * "libcall()" function
15323 */
15324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015325f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015326{
15327 libcall_common(argvars, rettv, VAR_STRING);
15328}
15329
15330/*
15331 * "libcallnr()" function
15332 */
15333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015334f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015335{
15336 libcall_common(argvars, rettv, VAR_NUMBER);
15337}
15338
15339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340 * "line(string)" function
15341 */
15342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015343f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344{
15345 linenr_T lnum = 0;
15346 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015347 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015349 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 if (fp != NULL)
15351 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015352 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353}
15354
15355/*
15356 * "line2byte(lnum)" function
15357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015359f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360{
15361#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015362 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363#else
15364 linenr_T lnum;
15365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015368 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015370 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15371 if (rettv->vval.v_number >= 0)
15372 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373#endif
15374}
15375
15376/*
15377 * "lispindent(lnum)" function
15378 */
15379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015380f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381{
15382#ifdef FEAT_LISP
15383 pos_T pos;
15384 linenr_T lnum;
15385
15386 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015387 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15389 {
15390 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015391 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392 curwin->w_cursor = pos;
15393 }
15394 else
15395#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015396 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397}
15398
15399/*
15400 * "localtime()" function
15401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015403f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015405 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406}
15407
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015408static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409
15410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015411get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412{
15413 char_u *keys;
15414 char_u *which;
15415 char_u buf[NUMBUFLEN];
15416 char_u *keys_buf = NULL;
15417 char_u *rhs;
15418 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015419 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015420 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015421 mapblock_T *mp;
15422 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423
15424 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015425 rettv->v_type = VAR_STRING;
15426 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015428 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 if (*keys == NUL)
15430 return;
15431
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015432 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015434 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015435 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015436 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015437 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015438 if (argvars[3].v_type != VAR_UNKNOWN)
15439 get_dict = get_tv_number(&argvars[3]);
15440 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 else
15443 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015444 if (which == NULL)
15445 return;
15446
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447 mode = get_map_mode(&which, 0);
15448
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015449 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015450 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015452
15453 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015455 /* Return a string. */
15456 if (rhs != NULL)
15457 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458
Bram Moolenaarbd743252010-10-20 21:23:33 +020015459 }
15460 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15461 {
15462 /* Return a dictionary. */
15463 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15464 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15465 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466
Bram Moolenaarbd743252010-10-20 21:23:33 +020015467 dict_add_nr_str(dict, "lhs", 0L, lhs);
15468 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15469 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15470 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15471 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15472 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15473 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015474 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015475 dict_add_nr_str(dict, "mode", 0L, mapmode);
15476
15477 vim_free(lhs);
15478 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479 }
15480}
15481
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015482#ifdef FEAT_FLOAT
15483/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015484 * "log()" function
15485 */
15486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015487f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015488{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015489 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015490
15491 rettv->v_type = VAR_FLOAT;
15492 if (get_float_arg(argvars, &f) == OK)
15493 rettv->vval.v_float = log(f);
15494 else
15495 rettv->vval.v_float = 0.0;
15496}
15497
15498/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015499 * "log10()" function
15500 */
15501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015502f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015503{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015504 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015505
15506 rettv->v_type = VAR_FLOAT;
15507 if (get_float_arg(argvars, &f) == OK)
15508 rettv->vval.v_float = log10(f);
15509 else
15510 rettv->vval.v_float = 0.0;
15511}
15512#endif
15513
Bram Moolenaar1dced572012-04-05 16:54:08 +020015514#ifdef FEAT_LUA
15515/*
15516 * "luaeval()" function
15517 */
15518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015519f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015520{
15521 char_u *str;
15522 char_u buf[NUMBUFLEN];
15523
15524 str = get_tv_string_buf(&argvars[0], buf);
15525 do_luaeval(str, argvars + 1, rettv);
15526}
15527#endif
15528
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015530 * "map()" function
15531 */
15532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015533f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015534{
15535 filter_map(argvars, rettv, TRUE);
15536}
15537
15538/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015539 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 */
15541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015542f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015544 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545}
15546
15547/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015548 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 */
15550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015551f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015553 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554}
15555
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015556static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557
15558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015559find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015561 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015562 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015563 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564 char_u *pat;
15565 regmatch_T regmatch;
15566 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015567 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 char_u *save_cpo;
15569 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015570 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015571 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015572 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015573 list_T *l = NULL;
15574 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015575 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015576 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577
15578 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15579 save_cpo = p_cpo;
15580 p_cpo = (char_u *)"";
15581
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015582 rettv->vval.v_number = -1;
15583 if (type == 3)
15584 {
15585 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015586 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015587 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015588 }
15589 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015591 rettv->v_type = VAR_STRING;
15592 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015595 if (argvars[0].v_type == VAR_LIST)
15596 {
15597 if ((l = argvars[0].vval.v_list) == NULL)
15598 goto theend;
15599 li = l->lv_first;
15600 }
15601 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015602 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015603 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015604 len = (long)STRLEN(str);
15605 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015607 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15608 if (pat == NULL)
15609 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015610
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015611 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015613 int error = FALSE;
15614
15615 start = get_tv_number_chk(&argvars[2], &error);
15616 if (error)
15617 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015618 if (l != NULL)
15619 {
15620 li = list_find(l, start);
15621 if (li == NULL)
15622 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015623 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015624 }
15625 else
15626 {
15627 if (start < 0)
15628 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015629 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015630 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015631 /* When "count" argument is there ignore matches before "start",
15632 * otherwise skip part of the string. Differs when pattern is "^"
15633 * or "\<". */
15634 if (argvars[3].v_type != VAR_UNKNOWN)
15635 startcol = start;
15636 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015637 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015638 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015639 len -= start;
15640 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015641 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015642
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015643 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015644 nth = get_tv_number_chk(&argvars[3], &error);
15645 if (error)
15646 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 }
15648
15649 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15650 if (regmatch.regprog != NULL)
15651 {
15652 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015653
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015654 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015655 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015656 if (l != NULL)
15657 {
15658 if (li == NULL)
15659 {
15660 match = FALSE;
15661 break;
15662 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015663 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015664 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015665 if (str == NULL)
15666 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015667 }
15668
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015669 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015670
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015671 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015672 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015673 if (l == NULL && !match)
15674 break;
15675
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015676 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015677 if (l != NULL)
15678 {
15679 li = li->li_next;
15680 ++idx;
15681 }
15682 else
15683 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015684#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015685 startcol = (colnr_T)(regmatch.startp[0]
15686 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015687#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015688 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015689#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015690 if (startcol > (colnr_T)len
15691 || str + startcol <= regmatch.startp[0])
15692 {
15693 match = FALSE;
15694 break;
15695 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015696 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015697 }
15698
15699 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015701 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015702 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015703 int i;
15704
15705 /* return list with matched string and submatches */
15706 for (i = 0; i < NSUBEXP; ++i)
15707 {
15708 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015709 {
15710 if (list_append_string(rettv->vval.v_list,
15711 (char_u *)"", 0) == FAIL)
15712 break;
15713 }
15714 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015715 regmatch.startp[i],
15716 (int)(regmatch.endp[i] - regmatch.startp[i]))
15717 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015718 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015719 }
15720 }
15721 else if (type == 2)
15722 {
15723 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015724 if (l != NULL)
15725 copy_tv(&li->li_tv, rettv);
15726 else
15727 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015729 }
15730 else if (l != NULL)
15731 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 else
15733 {
15734 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015735 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 (varnumber_T)(regmatch.startp[0] - str);
15737 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015738 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015740 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 }
15742 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015743 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 }
15745
15746theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015747 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 p_cpo = save_cpo;
15749}
15750
15751/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015752 * "match()" function
15753 */
15754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015755f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015756{
15757 find_some_match(argvars, rettv, 1);
15758}
15759
15760/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015761 * "matchadd()" function
15762 */
15763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015764f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015765{
15766#ifdef FEAT_SEARCH_EXTRA
15767 char_u buf[NUMBUFLEN];
15768 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15769 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15770 int prio = 10; /* default priority */
15771 int id = -1;
15772 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015773 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015774
15775 rettv->vval.v_number = -1;
15776
15777 if (grp == NULL || pat == NULL)
15778 return;
15779 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015780 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015781 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015782 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015783 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015784 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015785 if (argvars[4].v_type != VAR_UNKNOWN)
15786 {
15787 if (argvars[4].v_type != VAR_DICT)
15788 {
15789 EMSG(_(e_dictreq));
15790 return;
15791 }
15792 if (dict_find(argvars[4].vval.v_dict,
15793 (char_u *)"conceal", -1) != NULL)
15794 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15795 (char_u *)"conceal", FALSE);
15796 }
15797 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015798 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015799 if (error == TRUE)
15800 return;
15801 if (id >= 1 && id <= 3)
15802 {
15803 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15804 return;
15805 }
15806
Bram Moolenaar6561d522015-07-21 15:48:27 +020015807 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15808 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015809#endif
15810}
15811
15812/*
15813 * "matchaddpos()" function
15814 */
15815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015816f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015817{
15818#ifdef FEAT_SEARCH_EXTRA
15819 char_u buf[NUMBUFLEN];
15820 char_u *group;
15821 int prio = 10;
15822 int id = -1;
15823 int error = FALSE;
15824 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015825 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015826
15827 rettv->vval.v_number = -1;
15828
15829 group = get_tv_string_buf_chk(&argvars[0], buf);
15830 if (group == NULL)
15831 return;
15832
15833 if (argvars[1].v_type != VAR_LIST)
15834 {
15835 EMSG2(_(e_listarg), "matchaddpos()");
15836 return;
15837 }
15838 l = argvars[1].vval.v_list;
15839 if (l == NULL)
15840 return;
15841
15842 if (argvars[2].v_type != VAR_UNKNOWN)
15843 {
15844 prio = get_tv_number_chk(&argvars[2], &error);
15845 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015846 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015847 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015848 if (argvars[4].v_type != VAR_UNKNOWN)
15849 {
15850 if (argvars[4].v_type != VAR_DICT)
15851 {
15852 EMSG(_(e_dictreq));
15853 return;
15854 }
15855 if (dict_find(argvars[4].vval.v_dict,
15856 (char_u *)"conceal", -1) != NULL)
15857 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15858 (char_u *)"conceal", FALSE);
15859 }
15860 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015861 }
15862 if (error == TRUE)
15863 return;
15864
15865 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15866 if (id == 1 || id == 2)
15867 {
15868 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15869 return;
15870 }
15871
Bram Moolenaar6561d522015-07-21 15:48:27 +020015872 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15873 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015874#endif
15875}
15876
15877/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015878 * "matcharg()" function
15879 */
15880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015881f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015882{
15883 if (rettv_list_alloc(rettv) == OK)
15884 {
15885#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015886 int id = get_tv_number(&argvars[0]);
15887 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015888
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015889 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015890 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015891 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15892 {
15893 list_append_string(rettv->vval.v_list,
15894 syn_id2name(m->hlg_id), -1);
15895 list_append_string(rettv->vval.v_list, m->pattern, -1);
15896 }
15897 else
15898 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015899 list_append_string(rettv->vval.v_list, NULL, -1);
15900 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015901 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015902 }
15903#endif
15904 }
15905}
15906
15907/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015908 * "matchdelete()" function
15909 */
15910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015911f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015912{
15913#ifdef FEAT_SEARCH_EXTRA
15914 rettv->vval.v_number = match_delete(curwin,
15915 (int)get_tv_number(&argvars[0]), TRUE);
15916#endif
15917}
15918
15919/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015920 * "matchend()" function
15921 */
15922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015923f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015924{
15925 find_some_match(argvars, rettv, 0);
15926}
15927
15928/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015929 * "matchlist()" function
15930 */
15931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015932f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015933{
15934 find_some_match(argvars, rettv, 3);
15935}
15936
15937/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015938 * "matchstr()" function
15939 */
15940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015941f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015942{
15943 find_some_match(argvars, rettv, 2);
15944}
15945
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015946static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015947
15948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015949max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015950{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015951 long n = 0;
15952 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015953 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015954
15955 if (argvars[0].v_type == VAR_LIST)
15956 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015957 list_T *l;
15958 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015959
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015960 l = argvars[0].vval.v_list;
15961 if (l != NULL)
15962 {
15963 li = l->lv_first;
15964 if (li != NULL)
15965 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015966 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015967 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015968 {
15969 li = li->li_next;
15970 if (li == NULL)
15971 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015972 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015973 if (domax ? i > n : i < n)
15974 n = i;
15975 }
15976 }
15977 }
15978 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015979 else if (argvars[0].v_type == VAR_DICT)
15980 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015981 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015982 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015983 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015984 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015985
15986 d = argvars[0].vval.v_dict;
15987 if (d != NULL)
15988 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015989 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015990 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015991 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015992 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015993 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015994 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015995 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015996 if (first)
15997 {
15998 n = i;
15999 first = FALSE;
16000 }
16001 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016002 n = i;
16003 }
16004 }
16005 }
16006 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016007 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016008 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016009 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016010}
16011
16012/*
16013 * "max()" function
16014 */
16015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016016f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016017{
16018 max_min(argvars, rettv, TRUE);
16019}
16020
16021/*
16022 * "min()" function
16023 */
16024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016025f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016026{
16027 max_min(argvars, rettv, FALSE);
16028}
16029
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016030static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016031
16032/*
16033 * Create the directory in which "dir" is located, and higher levels when
16034 * needed.
16035 */
16036 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016037mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016038{
16039 char_u *p;
16040 char_u *updir;
16041 int r = FAIL;
16042
16043 /* Get end of directory name in "dir".
16044 * We're done when it's "/" or "c:/". */
16045 p = gettail_sep(dir);
16046 if (p <= get_past_head(dir))
16047 return OK;
16048
16049 /* If the directory exists we're done. Otherwise: create it.*/
16050 updir = vim_strnsave(dir, (int)(p - dir));
16051 if (updir == NULL)
16052 return FAIL;
16053 if (mch_isdir(updir))
16054 r = OK;
16055 else if (mkdir_recurse(updir, prot) == OK)
16056 r = vim_mkdir_emsg(updir, prot);
16057 vim_free(updir);
16058 return r;
16059}
16060
16061#ifdef vim_mkdir
16062/*
16063 * "mkdir()" function
16064 */
16065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016066f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016067{
16068 char_u *dir;
16069 char_u buf[NUMBUFLEN];
16070 int prot = 0755;
16071
16072 rettv->vval.v_number = FAIL;
16073 if (check_restricted() || check_secure())
16074 return;
16075
16076 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016077 if (*dir == NUL)
16078 rettv->vval.v_number = FAIL;
16079 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016080 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016081 if (*gettail(dir) == NUL)
16082 /* remove trailing slashes */
16083 *gettail_sep(dir) = NUL;
16084
16085 if (argvars[1].v_type != VAR_UNKNOWN)
16086 {
16087 if (argvars[2].v_type != VAR_UNKNOWN)
16088 prot = get_tv_number_chk(&argvars[2], NULL);
16089 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16090 mkdir_recurse(dir, prot);
16091 }
16092 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016093 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016094}
16095#endif
16096
Bram Moolenaar0d660222005-01-07 21:51:51 +000016097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098 * "mode()" function
16099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016101f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016103 char_u buf[3];
16104
16105 buf[1] = NUL;
16106 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108 if (VIsual_active)
16109 {
16110 if (VIsual_select)
16111 buf[0] = VIsual_mode + 's' - 'v';
16112 else
16113 buf[0] = VIsual_mode;
16114 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016115 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016116 || State == CONFIRM)
16117 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016119 if (State == ASKMORE)
16120 buf[1] = 'm';
16121 else if (State == CONFIRM)
16122 buf[1] = '?';
16123 }
16124 else if (State == EXTERNCMD)
16125 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126 else if (State & INSERT)
16127 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016128#ifdef FEAT_VREPLACE
16129 if (State & VREPLACE_FLAG)
16130 {
16131 buf[0] = 'R';
16132 buf[1] = 'v';
16133 }
16134 else
16135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136 if (State & REPLACE_FLAG)
16137 buf[0] = 'R';
16138 else
16139 buf[0] = 'i';
16140 }
16141 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016144 if (exmode_active)
16145 buf[1] = 'v';
16146 }
16147 else if (exmode_active)
16148 {
16149 buf[0] = 'c';
16150 buf[1] = 'e';
16151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016152 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016155 if (finish_op)
16156 buf[1] = 'o';
16157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016159 /* Clear out the minor mode when the argument is not a non-zero number or
16160 * non-empty string. */
16161 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016162 buf[1] = NUL;
16163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016164 rettv->vval.v_string = vim_strsave(buf);
16165 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166}
16167
Bram Moolenaar429fa852013-04-15 12:27:36 +020016168#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016169/*
16170 * "mzeval()" function
16171 */
16172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016173f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016174{
16175 char_u *str;
16176 char_u buf[NUMBUFLEN];
16177
16178 str = get_tv_string_buf(&argvars[0], buf);
16179 do_mzeval(str, rettv);
16180}
Bram Moolenaar75676462013-01-30 14:55:42 +010016181
16182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016183mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016184{
16185 typval_T argvars[3];
16186
16187 argvars[0].v_type = VAR_STRING;
16188 argvars[0].vval.v_string = name;
16189 copy_tv(args, &argvars[1]);
16190 argvars[2].v_type = VAR_UNKNOWN;
16191 f_call(argvars, rettv);
16192 clear_tv(&argvars[1]);
16193}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016194#endif
16195
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016197 * "nextnonblank()" function
16198 */
16199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016200f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016201{
16202 linenr_T lnum;
16203
16204 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016206 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016207 {
16208 lnum = 0;
16209 break;
16210 }
16211 if (*skipwhite(ml_get(lnum)) != NUL)
16212 break;
16213 }
16214 rettv->vval.v_number = lnum;
16215}
16216
16217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218 * "nr2char()" function
16219 */
16220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016221f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222{
16223 char_u buf[NUMBUFLEN];
16224
16225#ifdef FEAT_MBYTE
16226 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016227 {
16228 int utf8 = 0;
16229
16230 if (argvars[1].v_type != VAR_UNKNOWN)
16231 utf8 = get_tv_number_chk(&argvars[1], NULL);
16232 if (utf8)
16233 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16234 else
16235 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 else
16238#endif
16239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016240 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 buf[1] = NUL;
16242 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016243 rettv->v_type = VAR_STRING;
16244 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016245}
16246
16247/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016248 * "or(expr, expr)" function
16249 */
16250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016251f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016252{
16253 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16254 | get_tv_number_chk(&argvars[1], NULL);
16255}
16256
16257/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016258 * "pathshorten()" function
16259 */
16260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016261f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016262{
16263 char_u *p;
16264
16265 rettv->v_type = VAR_STRING;
16266 p = get_tv_string_chk(&argvars[0]);
16267 if (p == NULL)
16268 rettv->vval.v_string = NULL;
16269 else
16270 {
16271 p = vim_strsave(p);
16272 rettv->vval.v_string = p;
16273 if (p != NULL)
16274 shorten_dir(p);
16275 }
16276}
16277
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016278#ifdef FEAT_PERL
16279/*
16280 * "perleval()" function
16281 */
16282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016283f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016284{
16285 char_u *str;
16286 char_u buf[NUMBUFLEN];
16287
16288 str = get_tv_string_buf(&argvars[0], buf);
16289 do_perleval(str, rettv);
16290}
16291#endif
16292
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016293#ifdef FEAT_FLOAT
16294/*
16295 * "pow()" function
16296 */
16297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016298f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016299{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016300 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016301
16302 rettv->v_type = VAR_FLOAT;
16303 if (get_float_arg(argvars, &fx) == OK
16304 && get_float_arg(&argvars[1], &fy) == OK)
16305 rettv->vval.v_float = pow(fx, fy);
16306 else
16307 rettv->vval.v_float = 0.0;
16308}
16309#endif
16310
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016311/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016312 * "prevnonblank()" function
16313 */
16314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016315f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316{
16317 linenr_T lnum;
16318
16319 lnum = get_tv_lnum(argvars);
16320 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16321 lnum = 0;
16322 else
16323 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16324 --lnum;
16325 rettv->vval.v_number = lnum;
16326}
16327
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016328/* This dummy va_list is here because:
16329 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16330 * - locally in the function results in a "used before set" warning
16331 * - using va_start() to initialize it gives "function with fixed args" error */
16332static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016333
Bram Moolenaar8c711452005-01-14 21:53:12 +000016334/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016335 * "printf()" function
16336 */
16337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016338f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016339{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016340 char_u buf[NUMBUFLEN];
16341 int len;
16342 char_u *s;
16343 int saved_did_emsg = did_emsg;
16344 char *fmt;
16345
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016346 rettv->v_type = VAR_STRING;
16347 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016348
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016349 /* Get the required length, allocate the buffer and do it for real. */
16350 did_emsg = FALSE;
16351 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16352 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16353 if (!did_emsg)
16354 {
16355 s = alloc(len + 1);
16356 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016357 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016358 rettv->vval.v_string = s;
16359 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016360 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016361 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016362 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016363}
16364
16365/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016366 * "pumvisible()" function
16367 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016369f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016370{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016371#ifdef FEAT_INS_EXPAND
16372 if (pum_visible())
16373 rettv->vval.v_number = 1;
16374#endif
16375}
16376
Bram Moolenaardb913952012-06-29 12:54:53 +020016377#ifdef FEAT_PYTHON3
16378/*
16379 * "py3eval()" function
16380 */
16381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016382f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016383{
16384 char_u *str;
16385 char_u buf[NUMBUFLEN];
16386
16387 str = get_tv_string_buf(&argvars[0], buf);
16388 do_py3eval(str, rettv);
16389}
16390#endif
16391
16392#ifdef FEAT_PYTHON
16393/*
16394 * "pyeval()" function
16395 */
16396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016397f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016398{
16399 char_u *str;
16400 char_u buf[NUMBUFLEN];
16401
16402 str = get_tv_string_buf(&argvars[0], buf);
16403 do_pyeval(str, rettv);
16404}
16405#endif
16406
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016407/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016408 * "range()" function
16409 */
16410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016411f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016412{
16413 long start;
16414 long end;
16415 long stride = 1;
16416 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016417 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016419 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016420 if (argvars[1].v_type == VAR_UNKNOWN)
16421 {
16422 end = start - 1;
16423 start = 0;
16424 }
16425 else
16426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016427 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016428 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016429 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016430 }
16431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016432 if (error)
16433 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016434 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016435 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016436 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016437 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016438 else
16439 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016440 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016441 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016442 if (list_append_number(rettv->vval.v_list,
16443 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016444 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016445 }
16446}
16447
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016448/*
16449 * "readfile()" function
16450 */
16451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016452f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016453{
16454 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016455 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016456 char_u *fname;
16457 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016458 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16459 int io_size = sizeof(buf);
16460 int readlen; /* size of last fread() */
16461 char_u *prev = NULL; /* previously read bytes, if any */
16462 long prevlen = 0; /* length of data in prev */
16463 long prevsize = 0; /* size of prev buffer */
16464 long maxline = MAXLNUM;
16465 long cnt = 0;
16466 char_u *p; /* position in buf */
16467 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016468
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016469 if (argvars[1].v_type != VAR_UNKNOWN)
16470 {
16471 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16472 binary = TRUE;
16473 if (argvars[2].v_type != VAR_UNKNOWN)
16474 maxline = get_tv_number(&argvars[2]);
16475 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016476
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016477 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016478 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016479
16480 /* Always open the file in binary mode, library functions have a mind of
16481 * their own about CR-LF conversion. */
16482 fname = get_tv_string(&argvars[0]);
16483 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16484 {
16485 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16486 return;
16487 }
16488
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016489 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016490 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016491 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016492
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016493 /* This for loop processes what was read, but is also entered at end
16494 * of file so that either:
16495 * - an incomplete line gets written
16496 * - a "binary" file gets an empty line at the end if it ends in a
16497 * newline. */
16498 for (p = buf, start = buf;
16499 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16500 ++p)
16501 {
16502 if (*p == '\n' || readlen <= 0)
16503 {
16504 listitem_T *li;
16505 char_u *s = NULL;
16506 long_u len = p - start;
16507
16508 /* Finished a line. Remove CRs before NL. */
16509 if (readlen > 0 && !binary)
16510 {
16511 while (len > 0 && start[len - 1] == '\r')
16512 --len;
16513 /* removal may cross back to the "prev" string */
16514 if (len == 0)
16515 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16516 --prevlen;
16517 }
16518 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016519 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016520 else
16521 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016522 /* Change "prev" buffer to be the right size. This way
16523 * the bytes are only copied once, and very long lines are
16524 * allocated only once. */
16525 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016526 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016527 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016528 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016529 prev = NULL; /* the list will own the string */
16530 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016531 }
16532 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016533 if (s == NULL)
16534 {
16535 do_outofmem_msg((long_u) prevlen + len + 1);
16536 failed = TRUE;
16537 break;
16538 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016539
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016540 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016541 {
16542 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016543 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016544 break;
16545 }
16546 li->li_tv.v_type = VAR_STRING;
16547 li->li_tv.v_lock = 0;
16548 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016549 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016550
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016551 start = p + 1; /* step over newline */
16552 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016553 break;
16554 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016555 else if (*p == NUL)
16556 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016557#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016558 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16559 * when finding the BF and check the previous two bytes. */
16560 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016561 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016562 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16563 * + 1, these may be in the "prev" string. */
16564 char_u back1 = p >= buf + 1 ? p[-1]
16565 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16566 char_u back2 = p >= buf + 2 ? p[-2]
16567 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16568 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016569
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016570 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016571 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016572 char_u *dest = p - 2;
16573
16574 /* Usually a BOM is at the beginning of a file, and so at
16575 * the beginning of a line; then we can just step over it.
16576 */
16577 if (start == dest)
16578 start = p + 1;
16579 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016580 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016581 /* have to shuffle buf to close gap */
16582 int adjust_prevlen = 0;
16583
16584 if (dest < buf)
16585 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016586 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016587 dest = buf;
16588 }
16589 if (readlen > p - buf + 1)
16590 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16591 readlen -= 3 - adjust_prevlen;
16592 prevlen -= adjust_prevlen;
16593 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016594 }
16595 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016596 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016597#endif
16598 } /* for */
16599
16600 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16601 break;
16602 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016603 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016604 /* There's part of a line in buf, store it in "prev". */
16605 if (p - start + prevlen >= prevsize)
16606 {
16607 /* need bigger "prev" buffer */
16608 char_u *newprev;
16609
16610 /* A common use case is ordinary text files and "prev" gets a
16611 * fragment of a line, so the first allocation is made
16612 * small, to avoid repeatedly 'allocing' large and
16613 * 'reallocing' small. */
16614 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016615 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016616 else
16617 {
16618 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016619 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016620 prevsize = grow50pc > growmin ? grow50pc : growmin;
16621 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016622 newprev = prev == NULL ? alloc(prevsize)
16623 : vim_realloc(prev, prevsize);
16624 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016625 {
16626 do_outofmem_msg((long_u)prevsize);
16627 failed = TRUE;
16628 break;
16629 }
16630 prev = newprev;
16631 }
16632 /* Add the line part to end of "prev". */
16633 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016634 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016635 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016636 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016637
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016638 /*
16639 * For a negative line count use only the lines at the end of the file,
16640 * free the rest.
16641 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016642 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016643 while (cnt > -maxline)
16644 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016645 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016646 --cnt;
16647 }
16648
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016649 if (failed)
16650 {
16651 list_free(rettv->vval.v_list, TRUE);
16652 /* readfile doc says an empty list is returned on error */
16653 rettv->vval.v_list = list_alloc();
16654 }
16655
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016656 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016657 fclose(fd);
16658}
16659
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016660#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016661static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016662
16663/*
16664 * Convert a List to proftime_T.
16665 * Return FAIL when there is something wrong.
16666 */
16667 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016668list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016669{
16670 long n1, n2;
16671 int error = FALSE;
16672
16673 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16674 || arg->vval.v_list->lv_len != 2)
16675 return FAIL;
16676 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16677 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16678# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016679 tm->HighPart = n1;
16680 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016681# else
16682 tm->tv_sec = n1;
16683 tm->tv_usec = n2;
16684# endif
16685 return error ? FAIL : OK;
16686}
16687#endif /* FEAT_RELTIME */
16688
16689/*
16690 * "reltime()" function
16691 */
16692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016693f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016694{
16695#ifdef FEAT_RELTIME
16696 proftime_T res;
16697 proftime_T start;
16698
16699 if (argvars[0].v_type == VAR_UNKNOWN)
16700 {
16701 /* No arguments: get current time. */
16702 profile_start(&res);
16703 }
16704 else if (argvars[1].v_type == VAR_UNKNOWN)
16705 {
16706 if (list2proftime(&argvars[0], &res) == FAIL)
16707 return;
16708 profile_end(&res);
16709 }
16710 else
16711 {
16712 /* Two arguments: compute the difference. */
16713 if (list2proftime(&argvars[0], &start) == FAIL
16714 || list2proftime(&argvars[1], &res) == FAIL)
16715 return;
16716 profile_sub(&res, &start);
16717 }
16718
16719 if (rettv_list_alloc(rettv) == OK)
16720 {
16721 long n1, n2;
16722
16723# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016724 n1 = res.HighPart;
16725 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016726# else
16727 n1 = res.tv_sec;
16728 n2 = res.tv_usec;
16729# endif
16730 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16731 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16732 }
16733#endif
16734}
16735
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016736#ifdef FEAT_FLOAT
16737/*
16738 * "reltimefloat()" function
16739 */
16740 static void
16741f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16742{
16743# ifdef FEAT_RELTIME
16744 proftime_T tm;
16745# endif
16746
16747 rettv->v_type = VAR_FLOAT;
16748 rettv->vval.v_float = 0;
16749# ifdef FEAT_RELTIME
16750 if (list2proftime(&argvars[0], &tm) == OK)
16751 rettv->vval.v_float = profile_float(&tm);
16752# endif
16753}
16754#endif
16755
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016756/*
16757 * "reltimestr()" function
16758 */
16759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016760f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016761{
16762#ifdef FEAT_RELTIME
16763 proftime_T tm;
16764#endif
16765
16766 rettv->v_type = VAR_STRING;
16767 rettv->vval.v_string = NULL;
16768#ifdef FEAT_RELTIME
16769 if (list2proftime(&argvars[0], &tm) == OK)
16770 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16771#endif
16772}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016773
Bram Moolenaar0d660222005-01-07 21:51:51 +000016774#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016775static void make_connection(void);
16776static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016777
16778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016779make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016780{
16781 if (X_DISPLAY == NULL
16782# ifdef FEAT_GUI
16783 && !gui.in_use
16784# endif
16785 )
16786 {
16787 x_force_connect = TRUE;
16788 setup_term_clip();
16789 x_force_connect = FALSE;
16790 }
16791}
16792
16793 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016794check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016795{
16796 make_connection();
16797 if (X_DISPLAY == NULL)
16798 {
16799 EMSG(_("E240: No connection to Vim server"));
16800 return FAIL;
16801 }
16802 return OK;
16803}
16804#endif
16805
16806#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016807static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016808
16809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016810remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016811{
16812 char_u *server_name;
16813 char_u *keys;
16814 char_u *r = NULL;
16815 char_u buf[NUMBUFLEN];
16816# ifdef WIN32
16817 HWND w;
16818# else
16819 Window w;
16820# endif
16821
16822 if (check_restricted() || check_secure())
16823 return;
16824
16825# ifdef FEAT_X11
16826 if (check_connection() == FAIL)
16827 return;
16828# endif
16829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016830 server_name = get_tv_string_chk(&argvars[0]);
16831 if (server_name == NULL)
16832 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016833 keys = get_tv_string_buf(&argvars[1], buf);
16834# ifdef WIN32
16835 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16836# else
16837 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16838 < 0)
16839# endif
16840 {
16841 if (r != NULL)
16842 EMSG(r); /* sending worked but evaluation failed */
16843 else
16844 EMSG2(_("E241: Unable to send to %s"), server_name);
16845 return;
16846 }
16847
16848 rettv->vval.v_string = r;
16849
16850 if (argvars[2].v_type != VAR_UNKNOWN)
16851 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016852 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016853 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016854 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016855
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016856 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016857 v.di_tv.v_type = VAR_STRING;
16858 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016859 idvar = get_tv_string_chk(&argvars[2]);
16860 if (idvar != NULL)
16861 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016862 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016863 }
16864}
16865#endif
16866
16867/*
16868 * "remote_expr()" function
16869 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016871f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016872{
16873 rettv->v_type = VAR_STRING;
16874 rettv->vval.v_string = NULL;
16875#ifdef FEAT_CLIENTSERVER
16876 remote_common(argvars, rettv, TRUE);
16877#endif
16878}
16879
16880/*
16881 * "remote_foreground()" function
16882 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016884f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016885{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016886#ifdef FEAT_CLIENTSERVER
16887# ifdef WIN32
16888 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016889 {
16890 char_u *server_name = get_tv_string_chk(&argvars[0]);
16891
16892 if (server_name != NULL)
16893 serverForeground(server_name);
16894 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016895# else
16896 /* Send a foreground() expression to the server. */
16897 argvars[1].v_type = VAR_STRING;
16898 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16899 argvars[2].v_type = VAR_UNKNOWN;
16900 remote_common(argvars, rettv, TRUE);
16901 vim_free(argvars[1].vval.v_string);
16902# endif
16903#endif
16904}
16905
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016907f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016908{
16909#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016910 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016911 char_u *s = NULL;
16912# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016913 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016915 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016916
16917 if (check_restricted() || check_secure())
16918 {
16919 rettv->vval.v_number = -1;
16920 return;
16921 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016922 serverid = get_tv_string_chk(&argvars[0]);
16923 if (serverid == NULL)
16924 {
16925 rettv->vval.v_number = -1;
16926 return; /* type error; errmsg already given */
16927 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016928# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016929 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930 if (n == 0)
16931 rettv->vval.v_number = -1;
16932 else
16933 {
16934 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16935 rettv->vval.v_number = (s != NULL);
16936 }
16937# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016938 if (check_connection() == FAIL)
16939 return;
16940
16941 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016942 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943# endif
16944
16945 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16946 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016947 char_u *retvar;
16948
Bram Moolenaar33570922005-01-25 22:26:29 +000016949 v.di_tv.v_type = VAR_STRING;
16950 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016951 retvar = get_tv_string_chk(&argvars[1]);
16952 if (retvar != NULL)
16953 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016954 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955 }
16956#else
16957 rettv->vval.v_number = -1;
16958#endif
16959}
16960
Bram Moolenaar0d660222005-01-07 21:51:51 +000016961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016962f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016963{
16964 char_u *r = NULL;
16965
16966#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016967 char_u *serverid = get_tv_string_chk(&argvars[0]);
16968
16969 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970 {
16971# ifdef WIN32
16972 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016973 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016975 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016976 if (n != 0)
16977 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16978 if (r == NULL)
16979# else
16980 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016981 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982# endif
16983 EMSG(_("E277: Unable to read a server reply"));
16984 }
16985#endif
16986 rettv->v_type = VAR_STRING;
16987 rettv->vval.v_string = r;
16988}
16989
16990/*
16991 * "remote_send()" function
16992 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016994f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016995{
16996 rettv->v_type = VAR_STRING;
16997 rettv->vval.v_string = NULL;
16998#ifdef FEAT_CLIENTSERVER
16999 remote_common(argvars, rettv, FALSE);
17000#endif
17001}
17002
17003/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017004 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017005 */
17006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017007f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017008{
Bram Moolenaar33570922005-01-25 22:26:29 +000017009 list_T *l;
17010 listitem_T *item, *item2;
17011 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017012 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017013 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017014 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017015 dict_T *d;
17016 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017017 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017018
Bram Moolenaar8c711452005-01-14 21:53:12 +000017019 if (argvars[0].v_type == VAR_DICT)
17020 {
17021 if (argvars[2].v_type != VAR_UNKNOWN)
17022 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017023 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017024 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017026 key = get_tv_string_chk(&argvars[1]);
17027 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017029 di = dict_find(d, key, -1);
17030 if (di == NULL)
17031 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017032 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17033 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017034 {
17035 *rettv = di->di_tv;
17036 init_tv(&di->di_tv);
17037 dictitem_remove(d, di);
17038 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017039 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017040 }
17041 }
17042 else if (argvars[0].v_type != VAR_LIST)
17043 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017044 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017045 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017047 int error = FALSE;
17048
17049 idx = get_tv_number_chk(&argvars[1], &error);
17050 if (error)
17051 ; /* type error: do nothing, errmsg already given */
17052 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017053 EMSGN(_(e_listidx), idx);
17054 else
17055 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017056 if (argvars[2].v_type == VAR_UNKNOWN)
17057 {
17058 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017059 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017060 *rettv = item->li_tv;
17061 vim_free(item);
17062 }
17063 else
17064 {
17065 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017066 end = get_tv_number_chk(&argvars[2], &error);
17067 if (error)
17068 ; /* type error: do nothing */
17069 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017070 EMSGN(_(e_listidx), end);
17071 else
17072 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017073 int cnt = 0;
17074
17075 for (li = item; li != NULL; li = li->li_next)
17076 {
17077 ++cnt;
17078 if (li == item2)
17079 break;
17080 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017081 if (li == NULL) /* didn't find "item2" after "item" */
17082 EMSG(_(e_invrange));
17083 else
17084 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017085 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017086 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017087 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017088 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017089 l->lv_first = item;
17090 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017091 item->li_prev = NULL;
17092 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017093 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017094 }
17095 }
17096 }
17097 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017098 }
17099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100}
17101
17102/*
17103 * "rename({from}, {to})" function
17104 */
17105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017106f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107{
17108 char_u buf[NUMBUFLEN];
17109
17110 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017111 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017113 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17114 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115}
17116
17117/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017118 * "repeat()" function
17119 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017121f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017122{
17123 char_u *p;
17124 int n;
17125 int slen;
17126 int len;
17127 char_u *r;
17128 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017129
17130 n = get_tv_number(&argvars[1]);
17131 if (argvars[0].v_type == VAR_LIST)
17132 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017133 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017134 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017135 if (list_extend(rettv->vval.v_list,
17136 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017137 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017138 }
17139 else
17140 {
17141 p = get_tv_string(&argvars[0]);
17142 rettv->v_type = VAR_STRING;
17143 rettv->vval.v_string = NULL;
17144
17145 slen = (int)STRLEN(p);
17146 len = slen * n;
17147 if (len <= 0)
17148 return;
17149
17150 r = alloc(len + 1);
17151 if (r != NULL)
17152 {
17153 for (i = 0; i < n; i++)
17154 mch_memmove(r + i * slen, p, (size_t)slen);
17155 r[len] = NUL;
17156 }
17157
17158 rettv->vval.v_string = r;
17159 }
17160}
17161
17162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 * "resolve()" function
17164 */
17165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017166f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167{
17168 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017169#ifdef HAVE_READLINK
17170 char_u *buf = NULL;
17171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017173 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174#ifdef FEAT_SHORTCUT
17175 {
17176 char_u *v = NULL;
17177
17178 v = mch_resolve_shortcut(p);
17179 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017180 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017182 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183 }
17184#else
17185# ifdef HAVE_READLINK
17186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 char_u *cpy;
17188 int len;
17189 char_u *remain = NULL;
17190 char_u *q;
17191 int is_relative_to_current = FALSE;
17192 int has_trailing_pathsep = FALSE;
17193 int limit = 100;
17194
17195 p = vim_strsave(p);
17196
17197 if (p[0] == '.' && (vim_ispathsep(p[1])
17198 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17199 is_relative_to_current = TRUE;
17200
17201 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017202 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017205 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207
17208 q = getnextcomp(p);
17209 if (*q != NUL)
17210 {
17211 /* Separate the first path component in "p", and keep the
17212 * remainder (beginning with the path separator). */
17213 remain = vim_strsave(q - 1);
17214 q[-1] = NUL;
17215 }
17216
Bram Moolenaard9462e32011-04-11 21:35:11 +020017217 buf = alloc(MAXPATHL + 1);
17218 if (buf == NULL)
17219 goto fail;
17220
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221 for (;;)
17222 {
17223 for (;;)
17224 {
17225 len = readlink((char *)p, (char *)buf, MAXPATHL);
17226 if (len <= 0)
17227 break;
17228 buf[len] = NUL;
17229
17230 if (limit-- == 0)
17231 {
17232 vim_free(p);
17233 vim_free(remain);
17234 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017235 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 goto fail;
17237 }
17238
17239 /* Ensure that the result will have a trailing path separator
17240 * if the argument has one. */
17241 if (remain == NULL && has_trailing_pathsep)
17242 add_pathsep(buf);
17243
17244 /* Separate the first path component in the link value and
17245 * concatenate the remainders. */
17246 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17247 if (*q != NUL)
17248 {
17249 if (remain == NULL)
17250 remain = vim_strsave(q - 1);
17251 else
17252 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017253 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254 if (cpy != NULL)
17255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256 vim_free(remain);
17257 remain = cpy;
17258 }
17259 }
17260 q[-1] = NUL;
17261 }
17262
17263 q = gettail(p);
17264 if (q > p && *q == NUL)
17265 {
17266 /* Ignore trailing path separator. */
17267 q[-1] = NUL;
17268 q = gettail(p);
17269 }
17270 if (q > p && !mch_isFullName(buf))
17271 {
17272 /* symlink is relative to directory of argument */
17273 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17274 if (cpy != NULL)
17275 {
17276 STRCPY(cpy, p);
17277 STRCPY(gettail(cpy), buf);
17278 vim_free(p);
17279 p = cpy;
17280 }
17281 }
17282 else
17283 {
17284 vim_free(p);
17285 p = vim_strsave(buf);
17286 }
17287 }
17288
17289 if (remain == NULL)
17290 break;
17291
17292 /* Append the first path component of "remain" to "p". */
17293 q = getnextcomp(remain + 1);
17294 len = q - remain - (*q != NUL);
17295 cpy = vim_strnsave(p, STRLEN(p) + len);
17296 if (cpy != NULL)
17297 {
17298 STRNCAT(cpy, remain, len);
17299 vim_free(p);
17300 p = cpy;
17301 }
17302 /* Shorten "remain". */
17303 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017304 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305 else
17306 {
17307 vim_free(remain);
17308 remain = NULL;
17309 }
17310 }
17311
17312 /* If the result is a relative path name, make it explicitly relative to
17313 * the current directory if and only if the argument had this form. */
17314 if (!vim_ispathsep(*p))
17315 {
17316 if (is_relative_to_current
17317 && *p != NUL
17318 && !(p[0] == '.'
17319 && (p[1] == NUL
17320 || vim_ispathsep(p[1])
17321 || (p[1] == '.'
17322 && (p[2] == NUL
17323 || vim_ispathsep(p[2]))))))
17324 {
17325 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017326 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 if (cpy != NULL)
17328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 vim_free(p);
17330 p = cpy;
17331 }
17332 }
17333 else if (!is_relative_to_current)
17334 {
17335 /* Strip leading "./". */
17336 q = p;
17337 while (q[0] == '.' && vim_ispathsep(q[1]))
17338 q += 2;
17339 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017340 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341 }
17342 }
17343
17344 /* Ensure that the result will have no trailing path separator
17345 * if the argument had none. But keep "/" or "//". */
17346 if (!has_trailing_pathsep)
17347 {
17348 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017349 if (after_pathsep(p, q))
17350 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 }
17352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017353 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354 }
17355# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017356 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357# endif
17358#endif
17359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017360 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361
17362#ifdef HAVE_READLINK
17363fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017364 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017366 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367}
17368
17369/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017370 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371 */
17372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017373f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374{
Bram Moolenaar33570922005-01-25 22:26:29 +000017375 list_T *l;
17376 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377
Bram Moolenaar0d660222005-01-07 21:51:51 +000017378 if (argvars[0].v_type != VAR_LIST)
17379 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017380 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017381 && !tv_check_lock(l->lv_lock,
17382 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017383 {
17384 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017385 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017386 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017387 while (li != NULL)
17388 {
17389 ni = li->li_prev;
17390 list_append(l, li);
17391 li = ni;
17392 }
17393 rettv->vval.v_list = l;
17394 rettv->v_type = VAR_LIST;
17395 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017396 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398}
17399
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017400#define SP_NOMOVE 0x01 /* don't move cursor */
17401#define SP_REPEAT 0x02 /* repeat to find outer pair */
17402#define SP_RETCOUNT 0x04 /* return matchcount */
17403#define SP_SETPCMARK 0x08 /* set previous context mark */
17404#define SP_START 0x10 /* accept match at start position */
17405#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17406#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017407#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017408
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017409static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017410
17411/*
17412 * Get flags for a search function.
17413 * Possibly sets "p_ws".
17414 * Returns BACKWARD, FORWARD or zero (for an error).
17415 */
17416 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017417get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017418{
17419 int dir = FORWARD;
17420 char_u *flags;
17421 char_u nbuf[NUMBUFLEN];
17422 int mask;
17423
17424 if (varp->v_type != VAR_UNKNOWN)
17425 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017426 flags = get_tv_string_buf_chk(varp, nbuf);
17427 if (flags == NULL)
17428 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017429 while (*flags != NUL)
17430 {
17431 switch (*flags)
17432 {
17433 case 'b': dir = BACKWARD; break;
17434 case 'w': p_ws = TRUE; break;
17435 case 'W': p_ws = FALSE; break;
17436 default: mask = 0;
17437 if (flagsp != NULL)
17438 switch (*flags)
17439 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017440 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017441 case 'e': mask = SP_END; break;
17442 case 'm': mask = SP_RETCOUNT; break;
17443 case 'n': mask = SP_NOMOVE; break;
17444 case 'p': mask = SP_SUBPAT; break;
17445 case 'r': mask = SP_REPEAT; break;
17446 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017447 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017448 }
17449 if (mask == 0)
17450 {
17451 EMSG2(_(e_invarg2), flags);
17452 dir = 0;
17453 }
17454 else
17455 *flagsp |= mask;
17456 }
17457 if (dir == 0)
17458 break;
17459 ++flags;
17460 }
17461 }
17462 return dir;
17463}
17464
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017466 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017469search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017470{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017471 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472 char_u *pat;
17473 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017474 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475 int save_p_ws = p_ws;
17476 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017477 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017478 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017479 proftime_T tm;
17480#ifdef FEAT_RELTIME
17481 long time_limit = 0;
17482#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017483 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017484 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017486 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017487 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017488 if (dir == 0)
17489 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017490 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017491 if (flags & SP_START)
17492 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017493 if (flags & SP_END)
17494 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017495 if (flags & SP_COLUMN)
17496 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017497
Bram Moolenaar76929292008-01-06 19:07:36 +000017498 /* Optional arguments: line number to stop searching and timeout. */
17499 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017500 {
17501 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17502 if (lnum_stop < 0)
17503 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017504#ifdef FEAT_RELTIME
17505 if (argvars[3].v_type != VAR_UNKNOWN)
17506 {
17507 time_limit = get_tv_number_chk(&argvars[3], NULL);
17508 if (time_limit < 0)
17509 goto theend;
17510 }
17511#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017512 }
17513
Bram Moolenaar76929292008-01-06 19:07:36 +000017514#ifdef FEAT_RELTIME
17515 /* Set the time limit, if there is one. */
17516 profile_setlimit(time_limit, &tm);
17517#endif
17518
Bram Moolenaar231334e2005-07-25 20:46:57 +000017519 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017520 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017521 * Check to make sure only those flags are set.
17522 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17523 * flags cannot be set. Check for that condition also.
17524 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017525 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017526 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017528 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017529 goto theend;
17530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017532 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017533 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017534 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017535 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017537 if (flags & SP_SUBPAT)
17538 retval = subpatnum;
17539 else
17540 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017541 if (flags & SP_SETPCMARK)
17542 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017544 if (match_pos != NULL)
17545 {
17546 /* Store the match cursor position */
17547 match_pos->lnum = pos.lnum;
17548 match_pos->col = pos.col + 1;
17549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 /* "/$" will put the cursor after the end of the line, may need to
17551 * correct that here */
17552 check_cursor();
17553 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017554
17555 /* If 'n' flag is used: restore cursor position. */
17556 if (flags & SP_NOMOVE)
17557 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017558 else
17559 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017560theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017562
17563 return retval;
17564}
17565
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017566#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017567
17568/*
17569 * round() is not in C90, use ceil() or floor() instead.
17570 */
17571 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017572vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017573{
17574 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17575}
17576
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017577/*
17578 * "round({float})" function
17579 */
17580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017581f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017582{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017583 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017584
17585 rettv->v_type = VAR_FLOAT;
17586 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017587 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017588 else
17589 rettv->vval.v_float = 0.0;
17590}
17591#endif
17592
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017593/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017594 * "screenattr()" function
17595 */
17596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017597f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017598{
17599 int row;
17600 int col;
17601 int c;
17602
17603 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17604 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17605 if (row < 0 || row >= screen_Rows
17606 || col < 0 || col >= screen_Columns)
17607 c = -1;
17608 else
17609 c = ScreenAttrs[LineOffset[row] + col];
17610 rettv->vval.v_number = c;
17611}
17612
17613/*
17614 * "screenchar()" function
17615 */
17616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017617f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017618{
17619 int row;
17620 int col;
17621 int off;
17622 int c;
17623
17624 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17625 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17626 if (row < 0 || row >= screen_Rows
17627 || col < 0 || col >= screen_Columns)
17628 c = -1;
17629 else
17630 {
17631 off = LineOffset[row] + col;
17632#ifdef FEAT_MBYTE
17633 if (enc_utf8 && ScreenLinesUC[off] != 0)
17634 c = ScreenLinesUC[off];
17635 else
17636#endif
17637 c = ScreenLines[off];
17638 }
17639 rettv->vval.v_number = c;
17640}
17641
17642/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017643 * "screencol()" function
17644 *
17645 * First column is 1 to be consistent with virtcol().
17646 */
17647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017648f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017649{
17650 rettv->vval.v_number = screen_screencol() + 1;
17651}
17652
17653/*
17654 * "screenrow()" function
17655 */
17656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017657f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017658{
17659 rettv->vval.v_number = screen_screenrow() + 1;
17660}
17661
17662/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017663 * "search()" function
17664 */
17665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017666f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017667{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017668 int flags = 0;
17669
17670 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671}
17672
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017674 * "searchdecl()" function
17675 */
17676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017677f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017678{
17679 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017680 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017681 int error = FALSE;
17682 char_u *name;
17683
17684 rettv->vval.v_number = 1; /* default: FAIL */
17685
17686 name = get_tv_string_chk(&argvars[0]);
17687 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017688 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017689 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017690 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17691 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17692 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017693 if (!error && name != NULL)
17694 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017695 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017696}
17697
17698/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017699 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017701 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017702searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703{
17704 char_u *spat, *mpat, *epat;
17705 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 int dir;
17708 int flags = 0;
17709 char_u nbuf1[NUMBUFLEN];
17710 char_u nbuf2[NUMBUFLEN];
17711 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017712 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017713 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017714 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017717 spat = get_tv_string_chk(&argvars[0]);
17718 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17719 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17720 if (spat == NULL || mpat == NULL || epat == NULL)
17721 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 /* Handle the optional fourth argument: flags */
17724 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017725 if (dir == 0)
17726 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017727
17728 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017729 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17730 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017731 if ((flags & (SP_END | SP_SUBPAT)) != 0
17732 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017733 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017734 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017735 goto theend;
17736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017738 /* Using 'r' implies 'W', otherwise it doesn't work. */
17739 if (flags & SP_REPEAT)
17740 p_ws = FALSE;
17741
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017742 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017743 if (argvars[3].v_type == VAR_UNKNOWN
17744 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745 skip = (char_u *)"";
17746 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017748 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017749 if (argvars[5].v_type != VAR_UNKNOWN)
17750 {
17751 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17752 if (lnum_stop < 0)
17753 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017754#ifdef FEAT_RELTIME
17755 if (argvars[6].v_type != VAR_UNKNOWN)
17756 {
17757 time_limit = get_tv_number_chk(&argvars[6], NULL);
17758 if (time_limit < 0)
17759 goto theend;
17760 }
17761#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017762 }
17763 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017764 if (skip == NULL)
17765 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017767 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017768 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017769
17770theend:
17771 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017772
17773 return retval;
17774}
17775
17776/*
17777 * "searchpair()" function
17778 */
17779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017780f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017781{
17782 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17783}
17784
17785/*
17786 * "searchpairpos()" function
17787 */
17788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017789f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017790{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017791 pos_T match_pos;
17792 int lnum = 0;
17793 int col = 0;
17794
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017795 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017796 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017797
17798 if (searchpair_cmn(argvars, &match_pos) > 0)
17799 {
17800 lnum = match_pos.lnum;
17801 col = match_pos.col;
17802 }
17803
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017804 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17805 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017806}
17807
17808/*
17809 * Search for a start/middle/end thing.
17810 * Used by searchpair(), see its documentation for the details.
17811 * Returns 0 or -1 for no match,
17812 */
17813 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017814do_searchpair(
17815 char_u *spat, /* start pattern */
17816 char_u *mpat, /* middle pattern */
17817 char_u *epat, /* end pattern */
17818 int dir, /* BACKWARD or FORWARD */
17819 char_u *skip, /* skip expression */
17820 int flags, /* SP_SETPCMARK and other SP_ values */
17821 pos_T *match_pos,
17822 linenr_T lnum_stop, /* stop at this line if not zero */
17823 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017824{
17825 char_u *save_cpo;
17826 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17827 long retval = 0;
17828 pos_T pos;
17829 pos_T firstpos;
17830 pos_T foundpos;
17831 pos_T save_cursor;
17832 pos_T save_pos;
17833 int n;
17834 int r;
17835 int nest = 1;
17836 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017837 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017838 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017839
17840 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17841 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017842 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017843
Bram Moolenaar76929292008-01-06 19:07:36 +000017844#ifdef FEAT_RELTIME
17845 /* Set the time limit, if there is one. */
17846 profile_setlimit(time_limit, &tm);
17847#endif
17848
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017849 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17850 * start/middle/end (pat3, for the top pair). */
17851 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17852 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17853 if (pat2 == NULL || pat3 == NULL)
17854 goto theend;
17855 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17856 if (*mpat == NUL)
17857 STRCPY(pat3, pat2);
17858 else
17859 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17860 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017861 if (flags & SP_START)
17862 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017863
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864 save_cursor = curwin->w_cursor;
17865 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017866 clearpos(&firstpos);
17867 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 pat = pat3;
17869 for (;;)
17870 {
17871 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017872 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17874 /* didn't find it or found the first match again: FAIL */
17875 break;
17876
17877 if (firstpos.lnum == 0)
17878 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017879 if (equalpos(pos, foundpos))
17880 {
17881 /* Found the same position again. Can happen with a pattern that
17882 * has "\zs" at the end and searching backwards. Advance one
17883 * character and try again. */
17884 if (dir == BACKWARD)
17885 decl(&pos);
17886 else
17887 incl(&pos);
17888 }
17889 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017891 /* clear the start flag to avoid getting stuck here */
17892 options &= ~SEARCH_START;
17893
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894 /* If the skip pattern matches, ignore this match. */
17895 if (*skip != NUL)
17896 {
17897 save_pos = curwin->w_cursor;
17898 curwin->w_cursor = pos;
17899 r = eval_to_bool(skip, &err, NULL, FALSE);
17900 curwin->w_cursor = save_pos;
17901 if (err)
17902 {
17903 /* Evaluating {skip} caused an error, break here. */
17904 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017905 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906 break;
17907 }
17908 if (r)
17909 continue;
17910 }
17911
17912 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17913 {
17914 /* Found end when searching backwards or start when searching
17915 * forward: nested pair. */
17916 ++nest;
17917 pat = pat2; /* nested, don't search for middle */
17918 }
17919 else
17920 {
17921 /* Found end when searching forward or start when searching
17922 * backward: end of (nested) pair; or found middle in outer pair. */
17923 if (--nest == 1)
17924 pat = pat3; /* outer level, search for middle */
17925 }
17926
17927 if (nest == 0)
17928 {
17929 /* Found the match: return matchcount or line number. */
17930 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017931 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017933 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017934 if (flags & SP_SETPCMARK)
17935 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 curwin->w_cursor = pos;
17937 if (!(flags & SP_REPEAT))
17938 break;
17939 nest = 1; /* search for next unmatched */
17940 }
17941 }
17942
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017943 if (match_pos != NULL)
17944 {
17945 /* Store the match cursor position */
17946 match_pos->lnum = curwin->w_cursor.lnum;
17947 match_pos->col = curwin->w_cursor.col + 1;
17948 }
17949
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017951 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952 curwin->w_cursor = save_cursor;
17953
17954theend:
17955 vim_free(pat2);
17956 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017957 if (p_cpo == empty_option)
17958 p_cpo = save_cpo;
17959 else
17960 /* Darn, evaluating the {skip} expression changed the value. */
17961 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017962
17963 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964}
17965
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017966/*
17967 * "searchpos()" function
17968 */
17969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017970f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017971{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017972 pos_T match_pos;
17973 int lnum = 0;
17974 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017975 int n;
17976 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017977
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017978 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017979 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017980
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017981 n = search_cmn(argvars, &match_pos, &flags);
17982 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017983 {
17984 lnum = match_pos.lnum;
17985 col = match_pos.col;
17986 }
17987
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017988 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17989 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017990 if (flags & SP_SUBPAT)
17991 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017992}
17993
Bram Moolenaar0d660222005-01-07 21:51:51 +000017994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017995f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017997#ifdef FEAT_CLIENTSERVER
17998 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017999 char_u *server = get_tv_string_chk(&argvars[0]);
18000 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001
Bram Moolenaar0d660222005-01-07 21:51:51 +000018002 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018003 if (server == NULL || reply == NULL)
18004 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018005 if (check_restricted() || check_secure())
18006 return;
18007# ifdef FEAT_X11
18008 if (check_connection() == FAIL)
18009 return;
18010# endif
18011
18012 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018014 EMSG(_("E258: Unable to send to client"));
18015 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018017 rettv->vval.v_number = 0;
18018#else
18019 rettv->vval.v_number = -1;
18020#endif
18021}
18022
Bram Moolenaar0d660222005-01-07 21:51:51 +000018023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018024f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018025{
18026 char_u *r = NULL;
18027
18028#ifdef FEAT_CLIENTSERVER
18029# ifdef WIN32
18030 r = serverGetVimNames();
18031# else
18032 make_connection();
18033 if (X_DISPLAY != NULL)
18034 r = serverGetVimNames(X_DISPLAY);
18035# endif
18036#endif
18037 rettv->v_type = VAR_STRING;
18038 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039}
18040
18041/*
18042 * "setbufvar()" function
18043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018045f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046{
18047 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018050 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 char_u nbuf[NUMBUFLEN];
18052
18053 if (check_restricted() || check_secure())
18054 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018055 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18056 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018057 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058 varp = &argvars[2];
18059
18060 if (buf != NULL && varname != NULL && varp != NULL)
18061 {
18062 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064
18065 if (*varname == '&')
18066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018067 long numval;
18068 char_u *strval;
18069 int error = FALSE;
18070
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018072 numval = get_tv_number_chk(varp, &error);
18073 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018074 if (!error && strval != NULL)
18075 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076 }
18077 else
18078 {
18079 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18080 if (bufvarname != NULL)
18081 {
18082 STRCPY(bufvarname, "b:");
18083 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018084 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 vim_free(bufvarname);
18086 }
18087 }
18088
18089 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092}
18093
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018095f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018096{
18097 dict_T *d;
18098 dictitem_T *di;
18099 char_u *csearch;
18100
18101 if (argvars[0].v_type != VAR_DICT)
18102 {
18103 EMSG(_(e_dictreq));
18104 return;
18105 }
18106
18107 if ((d = argvars[0].vval.v_dict) != NULL)
18108 {
18109 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18110 if (csearch != NULL)
18111 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018112#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018113 if (enc_utf8)
18114 {
18115 int pcc[MAX_MCO];
18116 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018117
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018118 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18119 }
18120 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018121#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018122 set_last_csearch(PTR2CHAR(csearch),
18123 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018124 }
18125
18126 di = dict_find(d, (char_u *)"forward", -1);
18127 if (di != NULL)
18128 set_csearch_direction(get_tv_number(&di->di_tv)
18129 ? FORWARD : BACKWARD);
18130
18131 di = dict_find(d, (char_u *)"until", -1);
18132 if (di != NULL)
18133 set_csearch_until(!!get_tv_number(&di->di_tv));
18134 }
18135}
18136
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137/*
18138 * "setcmdpos()" function
18139 */
18140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018141f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018143 int pos = (int)get_tv_number(&argvars[0]) - 1;
18144
18145 if (pos >= 0)
18146 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147}
18148
18149/*
18150 * "setline()" function
18151 */
18152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018153f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154{
18155 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018156 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018157 list_T *l = NULL;
18158 listitem_T *li = NULL;
18159 long added = 0;
18160 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018162 lnum = get_tv_lnum(&argvars[0]);
18163 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018165 l = argvars[1].vval.v_list;
18166 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018168 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018169 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018170
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018171 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018172 for (;;)
18173 {
18174 if (l != NULL)
18175 {
18176 /* list argument, get next string */
18177 if (li == NULL)
18178 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018179 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018180 li = li->li_next;
18181 }
18182
18183 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018184 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018185 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018186
18187 /* When coming here from Insert mode, sync undo, so that this can be
18188 * undone separately from what was previously inserted. */
18189 if (u_sync_once == 2)
18190 {
18191 u_sync_once = 1; /* notify that u_sync() was called */
18192 u_sync(TRUE);
18193 }
18194
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018195 if (lnum <= curbuf->b_ml.ml_line_count)
18196 {
18197 /* existing line, replace it */
18198 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18199 {
18200 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018201 if (lnum == curwin->w_cursor.lnum)
18202 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018203 rettv->vval.v_number = 0; /* OK */
18204 }
18205 }
18206 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18207 {
18208 /* lnum is one past the last line, append the line */
18209 ++added;
18210 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18211 rettv->vval.v_number = 0; /* OK */
18212 }
18213
18214 if (l == NULL) /* only one string argument */
18215 break;
18216 ++lnum;
18217 }
18218
18219 if (added > 0)
18220 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221}
18222
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018223static 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 +000018224
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018226 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018227 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018229set_qf_ll_list(
18230 win_T *wp UNUSED,
18231 typval_T *list_arg UNUSED,
18232 typval_T *action_arg UNUSED,
18233 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018234{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018235#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018236 char_u *act;
18237 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018238#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018239
Bram Moolenaar2641f772005-03-25 21:58:17 +000018240 rettv->vval.v_number = -1;
18241
18242#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018243 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018244 EMSG(_(e_listreq));
18245 else
18246 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018247 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018248
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018249 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018250 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018251 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018252 if (act == NULL)
18253 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018254 if (*act == 'a' || *act == 'r')
18255 action = *act;
18256 }
18257
Bram Moolenaar81484f42012-12-05 15:16:47 +010018258 if (l != NULL && set_errorlist(wp, l, action,
18259 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018260 rettv->vval.v_number = 0;
18261 }
18262#endif
18263}
18264
18265/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018266 * "setloclist()" function
18267 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018269f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018270{
18271 win_T *win;
18272
18273 rettv->vval.v_number = -1;
18274
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018275 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018276 if (win != NULL)
18277 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18278}
18279
18280/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018281 * "setmatches()" function
18282 */
18283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018284f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018285{
18286#ifdef FEAT_SEARCH_EXTRA
18287 list_T *l;
18288 listitem_T *li;
18289 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018290 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018291
18292 rettv->vval.v_number = -1;
18293 if (argvars[0].v_type != VAR_LIST)
18294 {
18295 EMSG(_(e_listreq));
18296 return;
18297 }
18298 if ((l = argvars[0].vval.v_list) != NULL)
18299 {
18300
18301 /* To some extent make sure that we are dealing with a list from
18302 * "getmatches()". */
18303 li = l->lv_first;
18304 while (li != NULL)
18305 {
18306 if (li->li_tv.v_type != VAR_DICT
18307 || (d = li->li_tv.vval.v_dict) == NULL)
18308 {
18309 EMSG(_(e_invarg));
18310 return;
18311 }
18312 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018313 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18314 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018315 && dict_find(d, (char_u *)"priority", -1) != NULL
18316 && dict_find(d, (char_u *)"id", -1) != NULL))
18317 {
18318 EMSG(_(e_invarg));
18319 return;
18320 }
18321 li = li->li_next;
18322 }
18323
18324 clear_matches(curwin);
18325 li = l->lv_first;
18326 while (li != NULL)
18327 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018328 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018329 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018330 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018331 char_u *group;
18332 int priority;
18333 int id;
18334 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018335
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018336 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018337 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18338 {
18339 if (s == NULL)
18340 {
18341 s = list_alloc();
18342 if (s == NULL)
18343 return;
18344 }
18345
18346 /* match from matchaddpos() */
18347 for (i = 1; i < 9; i++)
18348 {
18349 sprintf((char *)buf, (char *)"pos%d", i);
18350 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18351 {
18352 if (di->di_tv.v_type != VAR_LIST)
18353 return;
18354
18355 list_append_tv(s, &di->di_tv);
18356 s->lv_refcount++;
18357 }
18358 else
18359 break;
18360 }
18361 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018362
18363 group = get_dict_string(d, (char_u *)"group", FALSE);
18364 priority = (int)get_dict_number(d, (char_u *)"priority");
18365 id = (int)get_dict_number(d, (char_u *)"id");
18366 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18367 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18368 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018369 if (i == 0)
18370 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018371 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018372 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018373 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018374 }
18375 else
18376 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018377 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018378 list_unref(s);
18379 s = NULL;
18380 }
18381
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018382 li = li->li_next;
18383 }
18384 rettv->vval.v_number = 0;
18385 }
18386#endif
18387}
18388
18389/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018390 * "setpos()" function
18391 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018393f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018394{
18395 pos_T pos;
18396 int fnum;
18397 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018398 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018399
Bram Moolenaar08250432008-02-13 11:42:46 +000018400 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018401 name = get_tv_string_chk(argvars);
18402 if (name != NULL)
18403 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018404 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018405 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018406 if (--pos.col < 0)
18407 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018408 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018409 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018410 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018411 if (fnum == curbuf->b_fnum)
18412 {
18413 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018414 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018415 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018416 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018417 curwin->w_set_curswant = FALSE;
18418 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018419 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018420 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018421 }
18422 else
18423 EMSG(_(e_invarg));
18424 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018425 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18426 {
18427 /* set mark */
18428 if (setmark_pos(name[1], &pos, fnum) == OK)
18429 rettv->vval.v_number = 0;
18430 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018431 else
18432 EMSG(_(e_invarg));
18433 }
18434 }
18435}
18436
18437/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018438 * "setqflist()" function
18439 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018441f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018442{
18443 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18444}
18445
18446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447 * "setreg()" function
18448 */
18449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018450f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451{
18452 int regname;
18453 char_u *strregname;
18454 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018455 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456 int append;
18457 char_u yank_type;
18458 long block_len;
18459
18460 block_len = -1;
18461 yank_type = MAUTO;
18462 append = FALSE;
18463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018464 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018465 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018467 if (strregname == NULL)
18468 return; /* type error; errmsg already given */
18469 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470 if (regname == 0 || regname == '@')
18471 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018473 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018475 stropt = get_tv_string_chk(&argvars[2]);
18476 if (stropt == NULL)
18477 return; /* type error */
18478 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479 switch (*stropt)
18480 {
18481 case 'a': case 'A': /* append */
18482 append = TRUE;
18483 break;
18484 case 'v': case 'c': /* character-wise selection */
18485 yank_type = MCHAR;
18486 break;
18487 case 'V': case 'l': /* line-wise selection */
18488 yank_type = MLINE;
18489 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490 case 'b': case Ctrl_V: /* block-wise selection */
18491 yank_type = MBLOCK;
18492 if (VIM_ISDIGIT(stropt[1]))
18493 {
18494 ++stropt;
18495 block_len = getdigits(&stropt) - 1;
18496 --stropt;
18497 }
18498 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499 }
18500 }
18501
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018502 if (argvars[1].v_type == VAR_LIST)
18503 {
18504 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018505 char_u **allocval;
18506 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018507 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018508 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018509 int len = argvars[1].vval.v_list->lv_len;
18510 listitem_T *li;
18511
Bram Moolenaar7d647822014-04-05 21:28:56 +020018512 /* First half: use for pointers to result lines; second half: use for
18513 * pointers to allocated copies. */
18514 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018515 if (lstval == NULL)
18516 return;
18517 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018518 allocval = lstval + len + 2;
18519 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018520
18521 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18522 li = li->li_next)
18523 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018524 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018525 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018526 goto free_lstval;
18527 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018528 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018529 /* Need to make a copy, next get_tv_string_buf_chk() will
18530 * overwrite the string. */
18531 strval = vim_strsave(buf);
18532 if (strval == NULL)
18533 goto free_lstval;
18534 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018535 }
18536 *curval++ = strval;
18537 }
18538 *curval++ = NULL;
18539
18540 write_reg_contents_lst(regname, lstval, -1,
18541 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018542free_lstval:
18543 while (curallocval > allocval)
18544 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018545 vim_free(lstval);
18546 }
18547 else
18548 {
18549 strval = get_tv_string_chk(&argvars[1]);
18550 if (strval == NULL)
18551 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018552 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018555 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556}
18557
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018558/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018559 * "settabvar()" function
18560 */
18561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018562f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018563{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018564#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018565 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018566 tabpage_T *tp;
18567#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018568 char_u *varname, *tabvarname;
18569 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018570
18571 rettv->vval.v_number = 0;
18572
18573 if (check_restricted() || check_secure())
18574 return;
18575
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018576#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018577 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018578#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018579 varname = get_tv_string_chk(&argvars[1]);
18580 varp = &argvars[2];
18581
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018582 if (varname != NULL && varp != NULL
18583#ifdef FEAT_WINDOWS
18584 && tp != NULL
18585#endif
18586 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018587 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018588#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018589 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018590 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018591#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018592
18593 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18594 if (tabvarname != NULL)
18595 {
18596 STRCPY(tabvarname, "t:");
18597 STRCPY(tabvarname + 2, varname);
18598 set_var(tabvarname, varp, TRUE);
18599 vim_free(tabvarname);
18600 }
18601
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018602#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018603 /* Restore current tabpage */
18604 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018605 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018606#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018607 }
18608}
18609
18610/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018611 * "settabwinvar()" function
18612 */
18613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018614f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018615{
18616 setwinvar(argvars, rettv, 1);
18617}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618
18619/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018620 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018623f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018625 setwinvar(argvars, rettv, 0);
18626}
18627
18628/*
18629 * "setwinvar()" and "settabwinvar()" functions
18630 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018631
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018633setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018634{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 win_T *win;
18636#ifdef FEAT_WINDOWS
18637 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018638 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018639 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640#endif
18641 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018642 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018644 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645
18646 if (check_restricted() || check_secure())
18647 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018648
18649#ifdef FEAT_WINDOWS
18650 if (off == 1)
18651 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18652 else
18653 tp = curtab;
18654#endif
18655 win = find_win_by_nr(&argvars[off], tp);
18656 varname = get_tv_string_chk(&argvars[off + 1]);
18657 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658
18659 if (win != NULL && varname != NULL && varp != NULL)
18660 {
18661#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018662 need_switch_win = !(tp == curtab && win == curwin);
18663 if (!need_switch_win
18664 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018667 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018669 long numval;
18670 char_u *strval;
18671 int error = FALSE;
18672
18673 ++varname;
18674 numval = get_tv_number_chk(varp, &error);
18675 strval = get_tv_string_buf_chk(varp, nbuf);
18676 if (!error && strval != NULL)
18677 set_option_value(varname, numval, strval, OPT_LOCAL);
18678 }
18679 else
18680 {
18681 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18682 if (winvarname != NULL)
18683 {
18684 STRCPY(winvarname, "w:");
18685 STRCPY(winvarname + 2, varname);
18686 set_var(winvarname, varp, TRUE);
18687 vim_free(winvarname);
18688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689 }
18690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018692 if (need_switch_win)
18693 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694#endif
18695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696}
18697
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018698#ifdef FEAT_CRYPT
18699/*
18700 * "sha256({string})" function
18701 */
18702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018703f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018704{
18705 char_u *p;
18706
18707 p = get_tv_string(&argvars[0]);
18708 rettv->vval.v_string = vim_strsave(
18709 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18710 rettv->v_type = VAR_STRING;
18711}
18712#endif /* FEAT_CRYPT */
18713
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018715 * "shellescape({string})" function
18716 */
18717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018718f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018719{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018720 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018721 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018722 rettv->v_type = VAR_STRING;
18723}
18724
18725/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018726 * shiftwidth() function
18727 */
18728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018729f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018730{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018731 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018732}
18733
18734/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018735 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736 */
18737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018738f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018740 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741
Bram Moolenaar0d660222005-01-07 21:51:51 +000018742 p = get_tv_string(&argvars[0]);
18743 rettv->vval.v_string = vim_strsave(p);
18744 simplify_filename(rettv->vval.v_string); /* simplify in place */
18745 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746}
18747
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018748#ifdef FEAT_FLOAT
18749/*
18750 * "sin()" function
18751 */
18752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018753f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018754{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018755 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018756
18757 rettv->v_type = VAR_FLOAT;
18758 if (get_float_arg(argvars, &f) == OK)
18759 rettv->vval.v_float = sin(f);
18760 else
18761 rettv->vval.v_float = 0.0;
18762}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018763
18764/*
18765 * "sinh()" function
18766 */
18767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018768f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018769{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018770 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018771
18772 rettv->v_type = VAR_FLOAT;
18773 if (get_float_arg(argvars, &f) == OK)
18774 rettv->vval.v_float = sinh(f);
18775 else
18776 rettv->vval.v_float = 0.0;
18777}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018778#endif
18779
Bram Moolenaar0d660222005-01-07 21:51:51 +000018780static int
18781#ifdef __BORLANDC__
18782 _RTLENTRYF
18783#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018784 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018785static int
18786#ifdef __BORLANDC__
18787 _RTLENTRYF
18788#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018789 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018790
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018791/* struct used in the array that's given to qsort() */
18792typedef struct
18793{
18794 listitem_T *item;
18795 int idx;
18796} sortItem_T;
18797
Bram Moolenaar0b962472016-02-22 22:51:33 +010018798/* struct storing information about current sort */
18799typedef struct
18800{
18801 int item_compare_ic;
18802 int item_compare_numeric;
18803 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018804#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018805 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018806#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018807 char_u *item_compare_func;
18808 dict_T *item_compare_selfdict;
18809 int item_compare_func_err;
18810 int item_compare_keep_zero;
18811} sortinfo_T;
18812static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018813static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018814#define ITEM_COMPARE_FAIL 999
18815
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018817 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018819 static int
18820#ifdef __BORLANDC__
18821_RTLENTRYF
18822#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018823item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018825 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018826 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018827 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018828 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018829 int res;
18830 char_u numbuf1[NUMBUFLEN];
18831 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018833 si1 = (sortItem_T *)s1;
18834 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018835 tv1 = &si1->item->li_tv;
18836 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018837
Bram Moolenaar0b962472016-02-22 22:51:33 +010018838 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018839 {
18840 long v1 = get_tv_number(tv1);
18841 long v2 = get_tv_number(tv2);
18842
18843 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18844 }
18845
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018846#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018847 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018848 {
18849 float_T v1 = get_tv_float(tv1);
18850 float_T v2 = get_tv_float(tv2);
18851
18852 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18853 }
18854#endif
18855
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018856 /* tv2string() puts quotes around a string and allocates memory. Don't do
18857 * that for string variables. Use a single quote when comparing with a
18858 * non-string to do what the docs promise. */
18859 if (tv1->v_type == VAR_STRING)
18860 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018861 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018862 p1 = (char_u *)"'";
18863 else
18864 p1 = tv1->vval.v_string;
18865 }
18866 else
18867 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18868 if (tv2->v_type == VAR_STRING)
18869 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018870 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018871 p2 = (char_u *)"'";
18872 else
18873 p2 = tv2->vval.v_string;
18874 }
18875 else
18876 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018877 if (p1 == NULL)
18878 p1 = (char_u *)"";
18879 if (p2 == NULL)
18880 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018881 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018882 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018883 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018884 res = STRICMP(p1, p2);
18885 else
18886 res = STRCMP(p1, p2);
18887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018889 {
18890 double n1, n2;
18891 n1 = strtod((char *)p1, (char **)&p1);
18892 n2 = strtod((char *)p2, (char **)&p2);
18893 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18894 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018895
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018896 /* When the result would be zero, compare the item indexes. Makes the
18897 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018898 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018899 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018900
Bram Moolenaar0d660222005-01-07 21:51:51 +000018901 vim_free(tofree1);
18902 vim_free(tofree2);
18903 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904}
18905
18906 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018907#ifdef __BORLANDC__
18908_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018910item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018911{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018912 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018913 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018914 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018915 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018916 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018918 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018919 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018920 return 0;
18921
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018922 si1 = (sortItem_T *)s1;
18923 si2 = (sortItem_T *)s2;
18924
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018925 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018926 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018927 copy_tv(&si1->item->li_tv, &argv[0]);
18928 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018929
18930 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018931 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010018932 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018933 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010018934 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018935 clear_tv(&argv[0]);
18936 clear_tv(&argv[1]);
18937
18938 if (res == FAIL)
18939 res = ITEM_COMPARE_FAIL;
18940 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018941 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18942 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018943 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018944 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018945
18946 /* When the result would be zero, compare the pointers themselves. Makes
18947 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018948 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018949 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018950
Bram Moolenaar0d660222005-01-07 21:51:51 +000018951 return res;
18952}
18953
18954/*
18955 * "sort({list})" function
18956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018958do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959{
Bram Moolenaar33570922005-01-25 22:26:29 +000018960 list_T *l;
18961 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018962 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018963 sortinfo_T *old_sortinfo;
18964 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018965 long len;
18966 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967
Bram Moolenaar0b962472016-02-22 22:51:33 +010018968 /* Pointer to current info struct used in compare function. Save and
18969 * restore the current one for nested calls. */
18970 old_sortinfo = sortinfo;
18971 sortinfo = &info;
18972
Bram Moolenaar0d660222005-01-07 21:51:51 +000018973 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018974 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 else
18976 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018977 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018978 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018979 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18980 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018981 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018982 rettv->vval.v_list = l;
18983 rettv->v_type = VAR_LIST;
18984 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985
Bram Moolenaar0d660222005-01-07 21:51:51 +000018986 len = list_len(l);
18987 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018988 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989
Bram Moolenaar0b962472016-02-22 22:51:33 +010018990 info.item_compare_ic = FALSE;
18991 info.item_compare_numeric = FALSE;
18992 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018993#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018994 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018995#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018996 info.item_compare_func = NULL;
18997 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018998 if (argvars[1].v_type != VAR_UNKNOWN)
18999 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019000 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019001 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019002 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019003 else
19004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019005 int error = FALSE;
19006
19007 i = get_tv_number_chk(&argvars[1], &error);
19008 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019009 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019010 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019011 info.item_compare_ic = TRUE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019012 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019013 info.item_compare_func = get_tv_string(&argvars[1]);
19014 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019015 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019016 if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019017 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019018 info.item_compare_func = NULL;
19019 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019020 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019021 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019022 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019023 info.item_compare_func = NULL;
19024 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019025 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019026#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019027 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019028 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019029 info.item_compare_func = NULL;
19030 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019031 }
19032#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019033 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019034 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019035 info.item_compare_func = NULL;
19036 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019037 }
19038 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019039 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019040
19041 if (argvars[2].v_type != VAR_UNKNOWN)
19042 {
19043 /* optional third argument: {dict} */
19044 if (argvars[2].v_type != VAR_DICT)
19045 {
19046 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019047 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019048 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019049 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019050 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052
Bram Moolenaar0d660222005-01-07 21:51:51 +000019053 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019054 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019055 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019056 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057
Bram Moolenaar327aa022014-03-25 18:24:23 +010019058 i = 0;
19059 if (sort)
19060 {
19061 /* sort(): ptrs will be the list to sort */
19062 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019063 {
19064 ptrs[i].item = li;
19065 ptrs[i].idx = i;
19066 ++i;
19067 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019068
Bram Moolenaar0b962472016-02-22 22:51:33 +010019069 info.item_compare_func_err = FALSE;
19070 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019071 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019072 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019073 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019074 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019075 EMSG(_("E702: Sort compare function failed"));
19076 else
19077 {
19078 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019079 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019080 info.item_compare_func == NULL
19081 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019082
Bram Moolenaar0b962472016-02-22 22:51:33 +010019083 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019084 {
19085 /* Clear the List and append the items in sorted order. */
19086 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19087 l->lv_len = 0;
19088 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019089 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019090 }
19091 }
19092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019094 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019095 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019096
19097 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019098 info.item_compare_func_err = FALSE;
19099 info.item_compare_keep_zero = TRUE;
19100 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019101 ? item_compare2 : item_compare;
19102
19103 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19104 li = li->li_next)
19105 {
19106 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19107 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019108 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019109 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019110 {
19111 EMSG(_("E882: Uniq compare function failed"));
19112 break;
19113 }
19114 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019115
Bram Moolenaar0b962472016-02-22 22:51:33 +010019116 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019117 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019118 while (--i >= 0)
19119 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019120 li = ptrs[i].item->li_next;
19121 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019122 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019123 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019124 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019125 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019126 list_fix_watch(l, li);
19127 listitem_free(li);
19128 l->lv_len--;
19129 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019130 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019131 }
19132
19133 vim_free(ptrs);
19134 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019135theend:
19136 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019137}
19138
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019139/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019140 * "sort({list})" function
19141 */
19142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019143f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019144{
19145 do_sort_uniq(argvars, rettv, TRUE);
19146}
19147
19148/*
19149 * "uniq({list})" function
19150 */
19151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019152f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019153{
19154 do_sort_uniq(argvars, rettv, FALSE);
19155}
19156
19157/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019158 * "soundfold({word})" function
19159 */
19160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019161f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019162{
19163 char_u *s;
19164
19165 rettv->v_type = VAR_STRING;
19166 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019167#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019168 rettv->vval.v_string = eval_soundfold(s);
19169#else
19170 rettv->vval.v_string = vim_strsave(s);
19171#endif
19172}
19173
19174/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019175 * "spellbadword()" function
19176 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019178f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019179{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019180 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019181 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019182 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019183
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019184 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019185 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019186
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019187#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019188 if (argvars[0].v_type == VAR_UNKNOWN)
19189 {
19190 /* Find the start and length of the badly spelled word. */
19191 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19192 if (len != 0)
19193 word = ml_get_cursor();
19194 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019195 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019196 {
19197 char_u *str = get_tv_string_chk(&argvars[0]);
19198 int capcol = -1;
19199
19200 if (str != NULL)
19201 {
19202 /* Check the argument for spelling. */
19203 while (*str != NUL)
19204 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019205 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019206 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019207 {
19208 word = str;
19209 break;
19210 }
19211 str += len;
19212 }
19213 }
19214 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019215#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019216
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019217 list_append_string(rettv->vval.v_list, word, len);
19218 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019219 attr == HLF_SPB ? "bad" :
19220 attr == HLF_SPR ? "rare" :
19221 attr == HLF_SPL ? "local" :
19222 attr == HLF_SPC ? "caps" :
19223 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019224}
19225
19226/*
19227 * "spellsuggest()" function
19228 */
19229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019230f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019231{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019232#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019233 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019234 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019235 int maxcount;
19236 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019237 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019238 listitem_T *li;
19239 int need_capital = FALSE;
19240#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019241
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019242 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019243 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019244
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019245#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019246 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019247 {
19248 str = get_tv_string(&argvars[0]);
19249 if (argvars[1].v_type != VAR_UNKNOWN)
19250 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019251 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019252 if (maxcount <= 0)
19253 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019254 if (argvars[2].v_type != VAR_UNKNOWN)
19255 {
19256 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19257 if (typeerr)
19258 return;
19259 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019260 }
19261 else
19262 maxcount = 25;
19263
Bram Moolenaar4770d092006-01-12 23:22:24 +000019264 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019265
19266 for (i = 0; i < ga.ga_len; ++i)
19267 {
19268 str = ((char_u **)ga.ga_data)[i];
19269
19270 li = listitem_alloc();
19271 if (li == NULL)
19272 vim_free(str);
19273 else
19274 {
19275 li->li_tv.v_type = VAR_STRING;
19276 li->li_tv.v_lock = 0;
19277 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019278 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019279 }
19280 }
19281 ga_clear(&ga);
19282 }
19283#endif
19284}
19285
Bram Moolenaar0d660222005-01-07 21:51:51 +000019286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019287f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019288{
19289 char_u *str;
19290 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019291 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019292 regmatch_T regmatch;
19293 char_u patbuf[NUMBUFLEN];
19294 char_u *save_cpo;
19295 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019296 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019297 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019298 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019299
19300 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19301 save_cpo = p_cpo;
19302 p_cpo = (char_u *)"";
19303
19304 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019305 if (argvars[1].v_type != VAR_UNKNOWN)
19306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019307 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19308 if (pat == NULL)
19309 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019310 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019311 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019312 }
19313 if (pat == NULL || *pat == NUL)
19314 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019315
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019316 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019318 if (typeerr)
19319 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320
Bram Moolenaar0d660222005-01-07 21:51:51 +000019321 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19322 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019324 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019325 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019326 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019327 if (*str == NUL)
19328 match = FALSE; /* empty item at the end */
19329 else
19330 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019331 if (match)
19332 end = regmatch.startp[0];
19333 else
19334 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019335 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19336 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019337 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019338 if (list_append_string(rettv->vval.v_list, str,
19339 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019340 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019341 }
19342 if (!match)
19343 break;
19344 /* Advance to just after the match. */
19345 if (regmatch.endp[0] > str)
19346 col = 0;
19347 else
19348 {
19349 /* Don't get stuck at the same match. */
19350#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019351 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019352#else
19353 col = 1;
19354#endif
19355 }
19356 str = regmatch.endp[0];
19357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358
Bram Moolenaar473de612013-06-08 18:19:48 +020019359 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361
Bram Moolenaar0d660222005-01-07 21:51:51 +000019362 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363}
19364
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019365#ifdef FEAT_FLOAT
19366/*
19367 * "sqrt()" function
19368 */
19369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019370f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019371{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019372 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019373
19374 rettv->v_type = VAR_FLOAT;
19375 if (get_float_arg(argvars, &f) == OK)
19376 rettv->vval.v_float = sqrt(f);
19377 else
19378 rettv->vval.v_float = 0.0;
19379}
19380
19381/*
19382 * "str2float()" function
19383 */
19384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019385f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019386{
19387 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19388
19389 if (*p == '+')
19390 p = skipwhite(p + 1);
19391 (void)string2float(p, &rettv->vval.v_float);
19392 rettv->v_type = VAR_FLOAT;
19393}
19394#endif
19395
Bram Moolenaar2c932302006-03-18 21:42:09 +000019396/*
19397 * "str2nr()" function
19398 */
19399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019400f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019401{
19402 int base = 10;
19403 char_u *p;
19404 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019405 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019406
19407 if (argvars[1].v_type != VAR_UNKNOWN)
19408 {
19409 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019410 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019411 {
19412 EMSG(_(e_invarg));
19413 return;
19414 }
19415 }
19416
19417 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019418 if (*p == '+')
19419 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019420 switch (base)
19421 {
19422 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19423 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19424 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19425 default: what = 0;
19426 }
19427 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019428 rettv->vval.v_number = n;
19429}
19430
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431#ifdef HAVE_STRFTIME
19432/*
19433 * "strftime({format}[, {time}])" function
19434 */
19435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019436f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437{
19438 char_u result_buf[256];
19439 struct tm *curtime;
19440 time_t seconds;
19441 char_u *p;
19442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019443 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019445 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019446 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447 seconds = time(NULL);
19448 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019449 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450 curtime = localtime(&seconds);
19451 /* MSVC returns NULL for an invalid value of seconds. */
19452 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019453 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 else
19455 {
19456# ifdef FEAT_MBYTE
19457 vimconv_T conv;
19458 char_u *enc;
19459
19460 conv.vc_type = CONV_NONE;
19461 enc = enc_locale();
19462 convert_setup(&conv, p_enc, enc);
19463 if (conv.vc_type != CONV_NONE)
19464 p = string_convert(&conv, p, NULL);
19465# endif
19466 if (p != NULL)
19467 (void)strftime((char *)result_buf, sizeof(result_buf),
19468 (char *)p, curtime);
19469 else
19470 result_buf[0] = NUL;
19471
19472# ifdef FEAT_MBYTE
19473 if (conv.vc_type != CONV_NONE)
19474 vim_free(p);
19475 convert_setup(&conv, enc, p_enc);
19476 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019477 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 else
19479# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019480 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481
19482# ifdef FEAT_MBYTE
19483 /* Release conversion descriptors */
19484 convert_setup(&conv, NULL, NULL);
19485 vim_free(enc);
19486# endif
19487 }
19488}
19489#endif
19490
19491/*
19492 * "stridx()" function
19493 */
19494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019495f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496{
19497 char_u buf[NUMBUFLEN];
19498 char_u *needle;
19499 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019500 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019502 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019504 needle = get_tv_string_chk(&argvars[1]);
19505 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019506 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019507 if (needle == NULL || haystack == NULL)
19508 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509
Bram Moolenaar33570922005-01-25 22:26:29 +000019510 if (argvars[2].v_type != VAR_UNKNOWN)
19511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019512 int error = FALSE;
19513
19514 start_idx = get_tv_number_chk(&argvars[2], &error);
19515 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019516 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019517 if (start_idx >= 0)
19518 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019519 }
19520
19521 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19522 if (pos != NULL)
19523 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524}
19525
19526/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019527 * "string()" function
19528 */
19529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019530f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019531{
19532 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019533 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019535 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019536 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019537 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019538 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019539 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540}
19541
19542/*
19543 * "strlen()" function
19544 */
19545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019546f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019548 rettv->vval.v_number = (varnumber_T)(STRLEN(
19549 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550}
19551
19552/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019553 * "strchars()" function
19554 */
19555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019556f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019557{
19558 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019559 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019560#ifdef FEAT_MBYTE
19561 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019562 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019563#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019564
19565 if (argvars[1].v_type != VAR_UNKNOWN)
19566 skipcc = get_tv_number_chk(&argvars[1], NULL);
19567 if (skipcc < 0 || skipcc > 1)
19568 EMSG(_(e_invarg));
19569 else
19570 {
19571#ifdef FEAT_MBYTE
19572 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19573 while (*s != NUL)
19574 {
19575 func_mb_ptr2char_adv(&s);
19576 ++len;
19577 }
19578 rettv->vval.v_number = len;
19579#else
19580 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19581#endif
19582 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019583}
19584
19585/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019586 * "strdisplaywidth()" function
19587 */
19588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019589f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019590{
19591 char_u *s = get_tv_string(&argvars[0]);
19592 int col = 0;
19593
19594 if (argvars[1].v_type != VAR_UNKNOWN)
19595 col = get_tv_number(&argvars[1]);
19596
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019597 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019598}
19599
19600/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019601 * "strwidth()" function
19602 */
19603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019604f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019605{
19606 char_u *s = get_tv_string(&argvars[0]);
19607
19608 rettv->vval.v_number = (varnumber_T)(
19609#ifdef FEAT_MBYTE
19610 mb_string2cells(s, -1)
19611#else
19612 STRLEN(s)
19613#endif
19614 );
19615}
19616
19617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618 * "strpart()" function
19619 */
19620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019621f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622{
19623 char_u *p;
19624 int n;
19625 int len;
19626 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019627 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019629 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 slen = (int)STRLEN(p);
19631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019632 n = get_tv_number_chk(&argvars[1], &error);
19633 if (error)
19634 len = 0;
19635 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019636 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 else
19638 len = slen - n; /* default len: all bytes that are available. */
19639
19640 /*
19641 * Only return the overlap between the specified part and the actual
19642 * string.
19643 */
19644 if (n < 0)
19645 {
19646 len += n;
19647 n = 0;
19648 }
19649 else if (n > slen)
19650 n = slen;
19651 if (len < 0)
19652 len = 0;
19653 else if (n + len > slen)
19654 len = slen - n;
19655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019656 rettv->v_type = VAR_STRING;
19657 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658}
19659
19660/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019661 * "strridx()" function
19662 */
19663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019664f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019665{
19666 char_u buf[NUMBUFLEN];
19667 char_u *needle;
19668 char_u *haystack;
19669 char_u *rest;
19670 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019671 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019673 needle = get_tv_string_chk(&argvars[1]);
19674 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019675
19676 rettv->vval.v_number = -1;
19677 if (needle == NULL || haystack == NULL)
19678 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019679
19680 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019681 if (argvars[2].v_type != VAR_UNKNOWN)
19682 {
19683 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019684 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019685 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019686 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019687 }
19688 else
19689 end_idx = haystack_len;
19690
Bram Moolenaar0d660222005-01-07 21:51:51 +000019691 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019692 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019693 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019694 lastmatch = haystack + end_idx;
19695 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019696 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019697 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019698 for (rest = haystack; *rest != '\0'; ++rest)
19699 {
19700 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019701 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019702 break;
19703 lastmatch = rest;
19704 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019705 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019706
19707 if (lastmatch == NULL)
19708 rettv->vval.v_number = -1;
19709 else
19710 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19711}
19712
19713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 * "strtrans()" function
19715 */
19716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019717f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019719 rettv->v_type = VAR_STRING;
19720 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721}
19722
19723/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019724 * "submatch()" function
19725 */
19726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019727f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019728{
Bram Moolenaar41571762014-04-02 19:00:58 +020019729 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019730 int no;
19731 int retList = 0;
19732
19733 no = (int)get_tv_number_chk(&argvars[0], &error);
19734 if (error)
19735 return;
19736 error = FALSE;
19737 if (argvars[1].v_type != VAR_UNKNOWN)
19738 retList = get_tv_number_chk(&argvars[1], &error);
19739 if (error)
19740 return;
19741
19742 if (retList == 0)
19743 {
19744 rettv->v_type = VAR_STRING;
19745 rettv->vval.v_string = reg_submatch(no);
19746 }
19747 else
19748 {
19749 rettv->v_type = VAR_LIST;
19750 rettv->vval.v_list = reg_submatch_list(no);
19751 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019752}
19753
19754/*
19755 * "substitute()" function
19756 */
19757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019758f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019759{
19760 char_u patbuf[NUMBUFLEN];
19761 char_u subbuf[NUMBUFLEN];
19762 char_u flagsbuf[NUMBUFLEN];
19763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019764 char_u *str = get_tv_string_chk(&argvars[0]);
19765 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19766 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19767 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19768
Bram Moolenaar0d660222005-01-07 21:51:51 +000019769 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019770 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19771 rettv->vval.v_string = NULL;
19772 else
19773 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019774}
19775
19776/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019777 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019780f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781{
19782 int id = 0;
19783#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019784 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785 long col;
19786 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019787 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019789 lnum = get_tv_lnum(argvars); /* -1 on type error */
19790 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19791 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019793 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019794 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019795 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796#endif
19797
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019798 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799}
19800
19801/*
19802 * "synIDattr(id, what [, mode])" function
19803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019805f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806{
19807 char_u *p = NULL;
19808#ifdef FEAT_SYN_HL
19809 int id;
19810 char_u *what;
19811 char_u *mode;
19812 char_u modebuf[NUMBUFLEN];
19813 int modec;
19814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019815 id = get_tv_number(&argvars[0]);
19816 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019817 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019819 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019821 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 modec = 0; /* replace invalid with current */
19823 }
19824 else
19825 {
19826#ifdef FEAT_GUI
19827 if (gui.in_use)
19828 modec = 'g';
19829 else
19830#endif
19831 if (t_colors > 1)
19832 modec = 'c';
19833 else
19834 modec = 't';
19835 }
19836
19837
19838 switch (TOLOWER_ASC(what[0]))
19839 {
19840 case 'b':
19841 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19842 p = highlight_color(id, what, modec);
19843 else /* bold */
19844 p = highlight_has_attr(id, HL_BOLD, modec);
19845 break;
19846
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019847 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 p = highlight_color(id, what, modec);
19849 break;
19850
19851 case 'i':
19852 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19853 p = highlight_has_attr(id, HL_INVERSE, modec);
19854 else /* italic */
19855 p = highlight_has_attr(id, HL_ITALIC, modec);
19856 break;
19857
19858 case 'n': /* name */
19859 p = get_highlight_name(NULL, id - 1);
19860 break;
19861
19862 case 'r': /* reverse */
19863 p = highlight_has_attr(id, HL_INVERSE, modec);
19864 break;
19865
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019866 case 's':
19867 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19868 p = highlight_color(id, what, modec);
19869 else /* standout */
19870 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 break;
19872
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019873 case 'u':
19874 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19875 /* underline */
19876 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19877 else
19878 /* undercurl */
19879 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 break;
19881 }
19882
19883 if (p != NULL)
19884 p = vim_strsave(p);
19885#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019886 rettv->v_type = VAR_STRING;
19887 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888}
19889
19890/*
19891 * "synIDtrans(id)" function
19892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019894f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895{
19896 int id;
19897
19898#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019899 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019900
19901 if (id > 0)
19902 id = syn_get_final_id(id);
19903 else
19904#endif
19905 id = 0;
19906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019907 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908}
19909
19910/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019911 * "synconcealed(lnum, col)" function
19912 */
19913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019914f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019915{
19916#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19917 long lnum;
19918 long col;
19919 int syntax_flags = 0;
19920 int cchar;
19921 int matchid = 0;
19922 char_u str[NUMBUFLEN];
19923#endif
19924
19925 rettv->v_type = VAR_LIST;
19926 rettv->vval.v_list = NULL;
19927
19928#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19929 lnum = get_tv_lnum(argvars); /* -1 on type error */
19930 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19931
19932 vim_memset(str, NUL, sizeof(str));
19933
19934 if (rettv_list_alloc(rettv) != FAIL)
19935 {
19936 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19937 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19938 && curwin->w_p_cole > 0)
19939 {
19940 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19941 syntax_flags = get_syntax_info(&matchid);
19942
19943 /* get the conceal character */
19944 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19945 {
19946 cchar = syn_get_sub_char();
19947 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19948 cchar = lcs_conceal;
19949 if (cchar != NUL)
19950 {
19951# ifdef FEAT_MBYTE
19952 if (has_mbyte)
19953 (*mb_char2bytes)(cchar, str);
19954 else
19955# endif
19956 str[0] = cchar;
19957 }
19958 }
19959 }
19960
19961 list_append_number(rettv->vval.v_list,
19962 (syntax_flags & HL_CONCEAL) != 0);
19963 /* -1 to auto-determine strlen */
19964 list_append_string(rettv->vval.v_list, str, -1);
19965 list_append_number(rettv->vval.v_list, matchid);
19966 }
19967#endif
19968}
19969
19970/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019971 * "synstack(lnum, col)" function
19972 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019974f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019975{
19976#ifdef FEAT_SYN_HL
19977 long lnum;
19978 long col;
19979 int i;
19980 int id;
19981#endif
19982
19983 rettv->v_type = VAR_LIST;
19984 rettv->vval.v_list = NULL;
19985
19986#ifdef FEAT_SYN_HL
19987 lnum = get_tv_lnum(argvars); /* -1 on type error */
19988 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19989
19990 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019991 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019992 && rettv_list_alloc(rettv) != FAIL)
19993 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019994 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019995 for (i = 0; ; ++i)
19996 {
19997 id = syn_get_stack_item(i);
19998 if (id < 0)
19999 break;
20000 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20001 break;
20002 }
20003 }
20004#endif
20005}
20006
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020008get_cmd_output_as_rettv(
20009 typval_T *argvars,
20010 typval_T *rettv,
20011 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020013 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020015 char_u *infile = NULL;
20016 char_u buf[NUMBUFLEN];
20017 int err = FALSE;
20018 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020019 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020020 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020022 rettv->v_type = VAR_STRING;
20023 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020024 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020025 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020026
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020027 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020028 {
20029 /*
20030 * Write the string to a temp file, to be used for input of the shell
20031 * command.
20032 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020033 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020034 {
20035 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020036 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020037 }
20038
20039 fd = mch_fopen((char *)infile, WRITEBIN);
20040 if (fd == NULL)
20041 {
20042 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020043 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020044 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020045 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020046 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020047 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20048 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020049 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020050 else
20051 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020052 size_t len;
20053
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020054 p = get_tv_string_buf_chk(&argvars[1], buf);
20055 if (p == NULL)
20056 {
20057 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020058 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020059 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020060 len = STRLEN(p);
20061 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020062 err = TRUE;
20063 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020064 if (fclose(fd) != 0)
20065 err = TRUE;
20066 if (err)
20067 {
20068 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020069 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020070 }
20071 }
20072
Bram Moolenaar52a72462014-08-29 15:53:52 +020020073 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20074 * echoes typeahead, that messes up the display. */
20075 if (!msg_silent)
20076 flags += SHELL_COOKED;
20077
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020078 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020080 int len;
20081 listitem_T *li;
20082 char_u *s = NULL;
20083 char_u *start;
20084 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020085 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086
Bram Moolenaar52a72462014-08-29 15:53:52 +020020087 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020088 if (res == NULL)
20089 goto errret;
20090
20091 list = list_alloc();
20092 if (list == NULL)
20093 goto errret;
20094
20095 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020097 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020098 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020099 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020100 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020101
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020102 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020103 if (s == NULL)
20104 goto errret;
20105
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020106 for (p = s; start < end; ++p, ++start)
20107 *p = *start == NUL ? NL : *start;
20108 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020109
20110 li = listitem_alloc();
20111 if (li == NULL)
20112 {
20113 vim_free(s);
20114 goto errret;
20115 }
20116 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020117 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020118 li->li_tv.vval.v_string = s;
20119 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020121
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020122 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020123 rettv->v_type = VAR_LIST;
20124 rettv->vval.v_list = list;
20125 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020127 else
20128 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020129 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020130#ifdef USE_CR
20131 /* translate <CR> into <NL> */
20132 if (res != NULL)
20133 {
20134 char_u *s;
20135
20136 for (s = res; *s; ++s)
20137 {
20138 if (*s == CAR)
20139 *s = NL;
20140 }
20141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142#else
20143# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020144 /* translate <CR><NL> into <NL> */
20145 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020147 char_u *s, *d;
20148
20149 d = res;
20150 for (s = res; *s; ++s)
20151 {
20152 if (s[0] == CAR && s[1] == NL)
20153 ++s;
20154 *d++ = *s;
20155 }
20156 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158# endif
20159#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020160 rettv->vval.v_string = res;
20161 res = NULL;
20162 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020163
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020164errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020165 if (infile != NULL)
20166 {
20167 mch_remove(infile);
20168 vim_free(infile);
20169 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020170 if (res != NULL)
20171 vim_free(res);
20172 if (list != NULL)
20173 list_free(list, TRUE);
20174}
20175
20176/*
20177 * "system()" function
20178 */
20179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020180f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020181{
20182 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20183}
20184
20185/*
20186 * "systemlist()" function
20187 */
20188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020189f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020190{
20191 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192}
20193
20194/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020195 * "tabpagebuflist()" function
20196 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020198f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020199{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020200#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020201 tabpage_T *tp;
20202 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020203
20204 if (argvars[0].v_type == VAR_UNKNOWN)
20205 wp = firstwin;
20206 else
20207 {
20208 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20209 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020210 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020211 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020212 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020213 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020214 for (; wp != NULL; wp = wp->w_next)
20215 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020216 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020217 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020218 }
20219#endif
20220}
20221
20222
20223/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020224 * "tabpagenr()" function
20225 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020227f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020228{
20229 int nr = 1;
20230#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020231 char_u *arg;
20232
20233 if (argvars[0].v_type != VAR_UNKNOWN)
20234 {
20235 arg = get_tv_string_chk(&argvars[0]);
20236 nr = 0;
20237 if (arg != NULL)
20238 {
20239 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020240 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020241 else
20242 EMSG2(_(e_invexpr2), arg);
20243 }
20244 }
20245 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020246 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020247#endif
20248 rettv->vval.v_number = nr;
20249}
20250
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020251
20252#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020253static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020254
20255/*
20256 * Common code for tabpagewinnr() and winnr().
20257 */
20258 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020259get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020260{
20261 win_T *twin;
20262 int nr = 1;
20263 win_T *wp;
20264 char_u *arg;
20265
20266 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20267 if (argvar->v_type != VAR_UNKNOWN)
20268 {
20269 arg = get_tv_string_chk(argvar);
20270 if (arg == NULL)
20271 nr = 0; /* type error; errmsg already given */
20272 else if (STRCMP(arg, "$") == 0)
20273 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20274 else if (STRCMP(arg, "#") == 0)
20275 {
20276 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20277 if (twin == NULL)
20278 nr = 0;
20279 }
20280 else
20281 {
20282 EMSG2(_(e_invexpr2), arg);
20283 nr = 0;
20284 }
20285 }
20286
20287 if (nr > 0)
20288 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20289 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020290 {
20291 if (wp == NULL)
20292 {
20293 /* didn't find it in this tabpage */
20294 nr = 0;
20295 break;
20296 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020297 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020298 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020299 return nr;
20300}
20301#endif
20302
20303/*
20304 * "tabpagewinnr()" function
20305 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020307f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020308{
20309 int nr = 1;
20310#ifdef FEAT_WINDOWS
20311 tabpage_T *tp;
20312
20313 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20314 if (tp == NULL)
20315 nr = 0;
20316 else
20317 nr = get_winnr(tp, &argvars[1]);
20318#endif
20319 rettv->vval.v_number = nr;
20320}
20321
20322
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020323/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020324 * "tagfiles()" function
20325 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020327f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020328{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020329 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020330 tagname_T tn;
20331 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020332
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020333 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020334 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020335 fname = alloc(MAXPATHL);
20336 if (fname == NULL)
20337 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020338
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020339 for (first = TRUE; ; first = FALSE)
20340 if (get_tagfname(&tn, first, fname) == FAIL
20341 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020342 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020343 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020344 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020345}
20346
20347/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020348 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020349 */
20350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020351f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020352{
20353 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020354
20355 tag_pattern = get_tv_string(&argvars[0]);
20356
20357 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020358 if (*tag_pattern == NUL)
20359 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020360
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020361 if (rettv_list_alloc(rettv) == OK)
20362 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020363}
20364
20365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 * "tempname()" function
20367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020369f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370{
20371 static int x = 'A';
20372
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020373 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020374 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375
20376 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20377 * names. Skip 'I' and 'O', they are used for shell redirection. */
20378 do
20379 {
20380 if (x == 'Z')
20381 x = '0';
20382 else if (x == '9')
20383 x = 'A';
20384 else
20385 {
20386#ifdef EBCDIC
20387 if (x == 'I')
20388 x = 'J';
20389 else if (x == 'R')
20390 x = 'S';
20391 else
20392#endif
20393 ++x;
20394 }
20395 } while (x == 'I' || x == 'O');
20396}
20397
20398/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020399 * "test(list)" function: Just checking the walls...
20400 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020402f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020403{
20404 /* Used for unit testing. Change the code below to your liking. */
20405#if 0
20406 listitem_T *li;
20407 list_T *l;
20408 char_u *bad, *good;
20409
20410 if (argvars[0].v_type != VAR_LIST)
20411 return;
20412 l = argvars[0].vval.v_list;
20413 if (l == NULL)
20414 return;
20415 li = l->lv_first;
20416 if (li == NULL)
20417 return;
20418 bad = get_tv_string(&li->li_tv);
20419 li = li->li_next;
20420 if (li == NULL)
20421 return;
20422 good = get_tv_string(&li->li_tv);
20423 rettv->vval.v_number = test_edit_score(bad, good);
20424#endif
20425}
20426
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020427#ifdef FEAT_FLOAT
20428/*
20429 * "tan()" function
20430 */
20431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020432f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020433{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020434 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020435
20436 rettv->v_type = VAR_FLOAT;
20437 if (get_float_arg(argvars, &f) == OK)
20438 rettv->vval.v_float = tan(f);
20439 else
20440 rettv->vval.v_float = 0.0;
20441}
20442
20443/*
20444 * "tanh()" function
20445 */
20446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020447f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020448{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020449 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020450
20451 rettv->v_type = VAR_FLOAT;
20452 if (get_float_arg(argvars, &f) == OK)
20453 rettv->vval.v_float = tanh(f);
20454 else
20455 rettv->vval.v_float = 0.0;
20456}
20457#endif
20458
Bram Moolenaard52d9742005-08-21 22:20:28 +000020459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460 * "tolower(string)" function
20461 */
20462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020463f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464{
20465 char_u *p;
20466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020467 p = vim_strsave(get_tv_string(&argvars[0]));
20468 rettv->v_type = VAR_STRING;
20469 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470
20471 if (p != NULL)
20472 while (*p != NUL)
20473 {
20474#ifdef FEAT_MBYTE
20475 int l;
20476
20477 if (enc_utf8)
20478 {
20479 int c, lc;
20480
20481 c = utf_ptr2char(p);
20482 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020483 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484 /* TODO: reallocate string when byte count changes. */
20485 if (utf_char2len(lc) == l)
20486 utf_char2bytes(lc, p);
20487 p += l;
20488 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020489 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490 p += l; /* skip multi-byte character */
20491 else
20492#endif
20493 {
20494 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20495 ++p;
20496 }
20497 }
20498}
20499
20500/*
20501 * "toupper(string)" function
20502 */
20503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020504f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020506 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020507 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508}
20509
20510/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020511 * "tr(string, fromstr, tostr)" function
20512 */
20513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020514f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020515{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020516 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020517 char_u *fromstr;
20518 char_u *tostr;
20519 char_u *p;
20520#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020521 int inlen;
20522 int fromlen;
20523 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020524 int idx;
20525 char_u *cpstr;
20526 int cplen;
20527 int first = TRUE;
20528#endif
20529 char_u buf[NUMBUFLEN];
20530 char_u buf2[NUMBUFLEN];
20531 garray_T ga;
20532
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020533 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020534 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20535 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020536
20537 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020538 rettv->v_type = VAR_STRING;
20539 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020540 if (fromstr == NULL || tostr == NULL)
20541 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020542 ga_init2(&ga, (int)sizeof(char), 80);
20543
20544#ifdef FEAT_MBYTE
20545 if (!has_mbyte)
20546#endif
20547 /* not multi-byte: fromstr and tostr must be the same length */
20548 if (STRLEN(fromstr) != STRLEN(tostr))
20549 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020550#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020551error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020552#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020553 EMSG2(_(e_invarg2), fromstr);
20554 ga_clear(&ga);
20555 return;
20556 }
20557
20558 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020559 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020560 {
20561#ifdef FEAT_MBYTE
20562 if (has_mbyte)
20563 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020564 inlen = (*mb_ptr2len)(in_str);
20565 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020566 cplen = inlen;
20567 idx = 0;
20568 for (p = fromstr; *p != NUL; p += fromlen)
20569 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020570 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020571 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020572 {
20573 for (p = tostr; *p != NUL; p += tolen)
20574 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020575 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020576 if (idx-- == 0)
20577 {
20578 cplen = tolen;
20579 cpstr = p;
20580 break;
20581 }
20582 }
20583 if (*p == NUL) /* tostr is shorter than fromstr */
20584 goto error;
20585 break;
20586 }
20587 ++idx;
20588 }
20589
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020590 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020591 {
20592 /* Check that fromstr and tostr have the same number of
20593 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020594 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020595 first = FALSE;
20596 for (p = tostr; *p != NUL; p += tolen)
20597 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020598 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020599 --idx;
20600 }
20601 if (idx != 0)
20602 goto error;
20603 }
20604
Bram Moolenaarcde88542015-08-11 19:14:00 +020020605 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020606 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020607 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020608
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020609 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020610 }
20611 else
20612#endif
20613 {
20614 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020615 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020616 if (p != NULL)
20617 ga_append(&ga, tostr[p - fromstr]);
20618 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020619 ga_append(&ga, *in_str);
20620 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020621 }
20622 }
20623
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020624 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020625 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020626 ga_append(&ga, NUL);
20627
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020628 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020629}
20630
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020631#ifdef FEAT_FLOAT
20632/*
20633 * "trunc({float})" function
20634 */
20635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020636f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020637{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020638 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020639
20640 rettv->v_type = VAR_FLOAT;
20641 if (get_float_arg(argvars, &f) == OK)
20642 /* trunc() is not in C90, use floor() or ceil() instead. */
20643 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20644 else
20645 rettv->vval.v_float = 0.0;
20646}
20647#endif
20648
Bram Moolenaar8299df92004-07-10 09:47:34 +000020649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 * "type(expr)" function
20651 */
20652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020653f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020655 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020656
20657 switch (argvars[0].v_type)
20658 {
20659 case VAR_NUMBER: n = 0; break;
20660 case VAR_STRING: n = 1; break;
20661 case VAR_FUNC: n = 2; break;
20662 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020663 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020664 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020665 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020666 if (argvars[0].vval.v_number == VVAL_FALSE
20667 || argvars[0].vval.v_number == VVAL_TRUE)
20668 n = 6;
20669 else
20670 n = 7;
20671 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020672 case VAR_JOB: n = 8; break;
20673 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020674 case VAR_UNKNOWN:
20675 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20676 n = -1;
20677 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020678 }
20679 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680}
20681
20682/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020683 * "undofile(name)" function
20684 */
20685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020686f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020687{
20688 rettv->v_type = VAR_STRING;
20689#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020690 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020691 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020692
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020693 if (*fname == NUL)
20694 {
20695 /* If there is no file name there will be no undo file. */
20696 rettv->vval.v_string = NULL;
20697 }
20698 else
20699 {
20700 char_u *ffname = FullName_save(fname, FALSE);
20701
20702 if (ffname != NULL)
20703 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20704 vim_free(ffname);
20705 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020706 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020707#else
20708 rettv->vval.v_string = NULL;
20709#endif
20710}
20711
20712/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020713 * "undotree()" function
20714 */
20715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020716f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020717{
20718 if (rettv_dict_alloc(rettv) == OK)
20719 {
20720 dict_T *dict = rettv->vval.v_dict;
20721 list_T *list;
20722
Bram Moolenaar730cde92010-06-27 05:18:54 +020020723 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020724 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020725 dict_add_nr_str(dict, "save_last",
20726 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020727 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20728 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020729 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020730
20731 list = list_alloc();
20732 if (list != NULL)
20733 {
20734 u_eval_tree(curbuf->b_u_oldhead, list);
20735 dict_add_list(dict, "entries", list);
20736 }
20737 }
20738}
20739
20740/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020741 * "values(dict)" function
20742 */
20743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020744f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020745{
20746 dict_list(argvars, rettv, 1);
20747}
20748
20749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 * "virtcol(string)" function
20751 */
20752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020753f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754{
20755 colnr_T vcol = 0;
20756 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020757 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020759 fp = var2fpos(&argvars[0], FALSE, &fnum);
20760 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20761 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 {
20763 getvvcol(curwin, fp, NULL, NULL, &vcol);
20764 ++vcol;
20765 }
20766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020767 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768}
20769
20770/*
20771 * "visualmode()" function
20772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020774f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 char_u str[2];
20777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020778 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 str[0] = curbuf->b_visual_mode_eval;
20780 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020781 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782
20783 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020784 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786}
20787
20788/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020789 * "wildmenumode()" function
20790 */
20791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020792f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020793{
20794#ifdef FEAT_WILDMENU
20795 if (wild_menu_showing)
20796 rettv->vval.v_number = 1;
20797#endif
20798}
20799
20800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801 * "winbufnr(nr)" function
20802 */
20803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020804f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805{
20806 win_T *wp;
20807
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020808 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020812 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813}
20814
20815/*
20816 * "wincol()" function
20817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020819f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820{
20821 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020822 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823}
20824
20825/*
20826 * "winheight(nr)" function
20827 */
20828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020829f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830{
20831 win_T *wp;
20832
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020833 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020835 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020837 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838}
20839
20840/*
20841 * "winline()" function
20842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020844f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845{
20846 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020847 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848}
20849
20850/*
20851 * "winnr()" function
20852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020854f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855{
20856 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020857
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020859 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020861 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862}
20863
20864/*
20865 * "winrestcmd()" function
20866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020868f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869{
20870#ifdef FEAT_WINDOWS
20871 win_T *wp;
20872 int winnr = 1;
20873 garray_T ga;
20874 char_u buf[50];
20875
20876 ga_init2(&ga, (int)sizeof(char), 70);
20877 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20878 {
20879 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20880 ga_concat(&ga, buf);
20881# ifdef FEAT_VERTSPLIT
20882 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20883 ga_concat(&ga, buf);
20884# endif
20885 ++winnr;
20886 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020887 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020889 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020891 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020893 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894}
20895
20896/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020897 * "winrestview()" function
20898 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020900f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020901{
20902 dict_T *dict;
20903
20904 if (argvars[0].v_type != VAR_DICT
20905 || (dict = argvars[0].vval.v_dict) == NULL)
20906 EMSG(_(e_invarg));
20907 else
20908 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020909 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20910 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20911 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20912 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020913#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020914 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20915 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020916#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020917 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20918 {
20919 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20920 curwin->w_set_curswant = FALSE;
20921 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020922
Bram Moolenaar82c25852014-05-28 16:47:16 +020020923 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20924 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020925#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020926 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20927 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020928#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020929 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20930 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20931 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20932 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020933
20934 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020935 win_new_height(curwin, curwin->w_height);
20936# ifdef FEAT_VERTSPLIT
20937 win_new_width(curwin, W_WIDTH(curwin));
20938# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020939 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020940
Bram Moolenaarb851a962014-10-31 15:45:52 +010020941 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020942 curwin->w_topline = 1;
20943 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20944 curwin->w_topline = curbuf->b_ml.ml_line_count;
20945#ifdef FEAT_DIFF
20946 check_topfill(curwin, TRUE);
20947#endif
20948 }
20949}
20950
20951/*
20952 * "winsaveview()" function
20953 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020955f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020956{
20957 dict_T *dict;
20958
Bram Moolenaara800b422010-06-27 01:15:55 +020020959 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020960 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020961 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020962
20963 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20964 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20965#ifdef FEAT_VIRTUALEDIT
20966 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20967#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020968 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020969 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20970
20971 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20972#ifdef FEAT_DIFF
20973 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20974#endif
20975 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20976 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20977}
20978
20979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 * "winwidth(nr)" function
20981 */
20982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020983f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984{
20985 win_T *wp;
20986
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020987 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020989 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990 else
20991#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020992 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020994 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995#endif
20996}
20997
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020999 * "wordcount()" function
21000 */
21001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021002f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021003{
21004 if (rettv_dict_alloc(rettv) == FAIL)
21005 return;
21006 cursor_pos_info(rettv->vval.v_dict);
21007}
21008
21009/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021010 * Write list of strings to file
21011 */
21012 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021013write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021014{
21015 listitem_T *li;
21016 int c;
21017 int ret = OK;
21018 char_u *s;
21019
21020 for (li = list->lv_first; li != NULL; li = li->li_next)
21021 {
21022 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21023 {
21024 if (*s == '\n')
21025 c = putc(NUL, fd);
21026 else
21027 c = putc(*s, fd);
21028 if (c == EOF)
21029 {
21030 ret = FAIL;
21031 break;
21032 }
21033 }
21034 if (!binary || li->li_next != NULL)
21035 if (putc('\n', fd) == EOF)
21036 {
21037 ret = FAIL;
21038 break;
21039 }
21040 if (ret == FAIL)
21041 {
21042 EMSG(_(e_write));
21043 break;
21044 }
21045 }
21046 return ret;
21047}
21048
21049/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021050 * "writefile()" function
21051 */
21052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021053f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021054{
21055 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021056 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021057 char_u *fname;
21058 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021059 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021060
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021061 if (check_restricted() || check_secure())
21062 return;
21063
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021064 if (argvars[0].v_type != VAR_LIST)
21065 {
21066 EMSG2(_(e_listarg), "writefile()");
21067 return;
21068 }
21069 if (argvars[0].vval.v_list == NULL)
21070 return;
21071
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021072 if (argvars[2].v_type != VAR_UNKNOWN)
21073 {
21074 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21075 binary = TRUE;
21076 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21077 append = TRUE;
21078 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021079
21080 /* Always open the file in binary mode, library functions have a mind of
21081 * their own about CR-LF conversion. */
21082 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021083 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21084 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021085 {
21086 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21087 ret = -1;
21088 }
21089 else
21090 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021091 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21092 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021093 fclose(fd);
21094 }
21095
21096 rettv->vval.v_number = ret;
21097}
21098
21099/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021100 * "xor(expr, expr)" function
21101 */
21102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021103f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021104{
21105 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21106 ^ get_tv_number_chk(&argvars[1], NULL);
21107}
21108
21109
21110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021112 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113 */
21114 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021115var2fpos(
21116 typval_T *varp,
21117 int dollar_lnum, /* TRUE when $ is last line */
21118 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021120 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021122 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123
Bram Moolenaara5525202006-03-02 22:52:09 +000021124 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021125 if (varp->v_type == VAR_LIST)
21126 {
21127 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021128 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021129 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021130 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021131
21132 l = varp->vval.v_list;
21133 if (l == NULL)
21134 return NULL;
21135
21136 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021137 pos.lnum = list_find_nr(l, 0L, &error);
21138 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021139 return NULL; /* invalid line number */
21140
21141 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021142 pos.col = list_find_nr(l, 1L, &error);
21143 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021144 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021145 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021146
21147 /* We accept "$" for the column number: last column. */
21148 li = list_find(l, 1L);
21149 if (li != NULL && li->li_tv.v_type == VAR_STRING
21150 && li->li_tv.vval.v_string != NULL
21151 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21152 pos.col = len + 1;
21153
Bram Moolenaara5525202006-03-02 22:52:09 +000021154 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021155 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021156 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021157 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021158
Bram Moolenaara5525202006-03-02 22:52:09 +000021159#ifdef FEAT_VIRTUALEDIT
21160 /* Get the virtual offset. Defaults to zero. */
21161 pos.coladd = list_find_nr(l, 2L, &error);
21162 if (error)
21163 pos.coladd = 0;
21164#endif
21165
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021166 return &pos;
21167 }
21168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021169 name = get_tv_string_chk(varp);
21170 if (name == NULL)
21171 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021172 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021174 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21175 {
21176 if (VIsual_active)
21177 return &VIsual;
21178 return &curwin->w_cursor;
21179 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021180 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021182 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21184 return NULL;
21185 return pp;
21186 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021187
21188#ifdef FEAT_VIRTUALEDIT
21189 pos.coladd = 0;
21190#endif
21191
Bram Moolenaar477933c2007-07-17 14:32:23 +000021192 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021193 {
21194 pos.col = 0;
21195 if (name[1] == '0') /* "w0": first visible line */
21196 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021197 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021198 pos.lnum = curwin->w_topline;
21199 return &pos;
21200 }
21201 else if (name[1] == '$') /* "w$": last visible line */
21202 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021203 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021204 pos.lnum = curwin->w_botline - 1;
21205 return &pos;
21206 }
21207 }
21208 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021210 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211 {
21212 pos.lnum = curbuf->b_ml.ml_line_count;
21213 pos.col = 0;
21214 }
21215 else
21216 {
21217 pos.lnum = curwin->w_cursor.lnum;
21218 pos.col = (colnr_T)STRLEN(ml_get_curline());
21219 }
21220 return &pos;
21221 }
21222 return NULL;
21223}
21224
21225/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021226 * Convert list in "arg" into a position and optional file number.
21227 * When "fnump" is NULL there is no file number, only 3 items.
21228 * Note that the column is passed on as-is, the caller may want to decrement
21229 * it to use 1 for the first column.
21230 * Return FAIL when conversion is not possible, doesn't check the position for
21231 * validity.
21232 */
21233 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021234list2fpos(
21235 typval_T *arg,
21236 pos_T *posp,
21237 int *fnump,
21238 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021239{
21240 list_T *l = arg->vval.v_list;
21241 long i = 0;
21242 long n;
21243
Bram Moolenaar493c1782014-05-28 14:34:46 +020021244 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21245 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021246 if (arg->v_type != VAR_LIST
21247 || l == NULL
21248 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021249 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021250 return FAIL;
21251
21252 if (fnump != NULL)
21253 {
21254 n = list_find_nr(l, i++, NULL); /* fnum */
21255 if (n < 0)
21256 return FAIL;
21257 if (n == 0)
21258 n = curbuf->b_fnum; /* current buffer */
21259 *fnump = n;
21260 }
21261
21262 n = list_find_nr(l, i++, NULL); /* lnum */
21263 if (n < 0)
21264 return FAIL;
21265 posp->lnum = n;
21266
21267 n = list_find_nr(l, i++, NULL); /* col */
21268 if (n < 0)
21269 return FAIL;
21270 posp->col = n;
21271
21272#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021273 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021274 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021275 posp->coladd = 0;
21276 else
21277 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021278#endif
21279
Bram Moolenaar493c1782014-05-28 14:34:46 +020021280 if (curswantp != NULL)
21281 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21282
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021283 return OK;
21284}
21285
21286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 * Get the length of an environment variable name.
21288 * Advance "arg" to the first character after the name.
21289 * Return 0 for error.
21290 */
21291 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021292get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293{
21294 char_u *p;
21295 int len;
21296
21297 for (p = *arg; vim_isIDc(*p); ++p)
21298 ;
21299 if (p == *arg) /* no name found */
21300 return 0;
21301
21302 len = (int)(p - *arg);
21303 *arg = p;
21304 return len;
21305}
21306
21307/*
21308 * Get the length of the name of a function or internal variable.
21309 * "arg" is advanced to the first non-white character after the name.
21310 * Return 0 if something is wrong.
21311 */
21312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021313get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314{
21315 char_u *p;
21316 int len;
21317
21318 /* Find the end of the name. */
21319 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021320 {
21321 if (*p == ':')
21322 {
21323 /* "s:" is start of "s:var", but "n:" is not and can be used in
21324 * slice "[n:]". Also "xx:" is not a namespace. */
21325 len = (int)(p - *arg);
21326 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21327 || len > 1)
21328 break;
21329 }
21330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 if (p == *arg) /* no name found */
21332 return 0;
21333
21334 len = (int)(p - *arg);
21335 *arg = skipwhite(p);
21336
21337 return len;
21338}
21339
21340/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021341 * Get the length of the name of a variable or function.
21342 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021344 * Return -1 if curly braces expansion failed.
21345 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 * If the name contains 'magic' {}'s, expand them and return the
21347 * expanded name in an allocated string via 'alias' - caller must free.
21348 */
21349 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021350get_name_len(
21351 char_u **arg,
21352 char_u **alias,
21353 int evaluate,
21354 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355{
21356 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357 char_u *p;
21358 char_u *expr_start;
21359 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360
21361 *alias = NULL; /* default to no alias */
21362
21363 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21364 && (*arg)[2] == (int)KE_SNR)
21365 {
21366 /* hard coded <SNR>, already translated */
21367 *arg += 3;
21368 return get_id_len(arg) + 3;
21369 }
21370 len = eval_fname_script(*arg);
21371 if (len > 0)
21372 {
21373 /* literal "<SID>", "s:" or "<SNR>" */
21374 *arg += len;
21375 }
21376
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021378 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021380 p = find_name_end(*arg, &expr_start, &expr_end,
21381 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 if (expr_start != NULL)
21383 {
21384 char_u *temp_string;
21385
21386 if (!evaluate)
21387 {
21388 len += (int)(p - *arg);
21389 *arg = skipwhite(p);
21390 return len;
21391 }
21392
21393 /*
21394 * Include any <SID> etc in the expanded string:
21395 * Thus the -len here.
21396 */
21397 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21398 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021399 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400 *alias = temp_string;
21401 *arg = skipwhite(p);
21402 return (int)STRLEN(temp_string);
21403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404
21405 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021406 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 EMSG2(_(e_invexpr2), *arg);
21408
21409 return len;
21410}
21411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021412/*
21413 * Find the end of a variable or function name, taking care of magic braces.
21414 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21415 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021416 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021417 * Return a pointer to just after the name. Equal to "arg" if there is no
21418 * valid name.
21419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021421find_name_end(
21422 char_u *arg,
21423 char_u **expr_start,
21424 char_u **expr_end,
21425 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021427 int mb_nest = 0;
21428 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021430 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021432 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021434 *expr_start = NULL;
21435 *expr_end = NULL;
21436 }
21437
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021438 /* Quick check for valid starting character. */
21439 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21440 return arg;
21441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021442 for (p = arg; *p != NUL
21443 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021444 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021445 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021446 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021447 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021448 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021449 if (*p == '\'')
21450 {
21451 /* skip over 'string' to avoid counting [ and ] inside it. */
21452 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21453 ;
21454 if (*p == NUL)
21455 break;
21456 }
21457 else if (*p == '"')
21458 {
21459 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21460 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21461 if (*p == '\\' && p[1] != NUL)
21462 ++p;
21463 if (*p == NUL)
21464 break;
21465 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021466 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21467 {
21468 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021469 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021470 len = (int)(p - arg);
21471 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021472 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021473 break;
21474 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021476 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021478 if (*p == '[')
21479 ++br_nest;
21480 else if (*p == ']')
21481 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021483
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021484 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021486 if (*p == '{')
21487 {
21488 mb_nest++;
21489 if (expr_start != NULL && *expr_start == NULL)
21490 *expr_start = p;
21491 }
21492 else if (*p == '}')
21493 {
21494 mb_nest--;
21495 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21496 *expr_end = p;
21497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 }
21500
21501 return p;
21502}
21503
21504/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021505 * Expands out the 'magic' {}'s in a variable/function name.
21506 * Note that this can call itself recursively, to deal with
21507 * constructs like foo{bar}{baz}{bam}
21508 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21509 * "in_start" ^
21510 * "expr_start" ^
21511 * "expr_end" ^
21512 * "in_end" ^
21513 *
21514 * Returns a new allocated string, which the caller must free.
21515 * Returns NULL for failure.
21516 */
21517 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021518make_expanded_name(
21519 char_u *in_start,
21520 char_u *expr_start,
21521 char_u *expr_end,
21522 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021523{
21524 char_u c1;
21525 char_u *retval = NULL;
21526 char_u *temp_result;
21527 char_u *nextcmd = NULL;
21528
21529 if (expr_end == NULL || in_end == NULL)
21530 return NULL;
21531 *expr_start = NUL;
21532 *expr_end = NUL;
21533 c1 = *in_end;
21534 *in_end = NUL;
21535
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021536 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021537 if (temp_result != NULL && nextcmd == NULL)
21538 {
21539 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21540 + (in_end - expr_end) + 1));
21541 if (retval != NULL)
21542 {
21543 STRCPY(retval, in_start);
21544 STRCAT(retval, temp_result);
21545 STRCAT(retval, expr_end + 1);
21546 }
21547 }
21548 vim_free(temp_result);
21549
21550 *in_end = c1; /* put char back for error messages */
21551 *expr_start = '{';
21552 *expr_end = '}';
21553
21554 if (retval != NULL)
21555 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021556 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021557 if (expr_start != NULL)
21558 {
21559 /* Further expansion! */
21560 temp_result = make_expanded_name(retval, expr_start,
21561 expr_end, temp_result);
21562 vim_free(retval);
21563 retval = temp_result;
21564 }
21565 }
21566
21567 return retval;
21568}
21569
21570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021571 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021572 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 */
21574 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021575eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021577 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21578}
21579
21580/*
21581 * Return TRUE if character "c" can be used as the first character in a
21582 * variable or function name (excluding '{' and '}').
21583 */
21584 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021585eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021586{
21587 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588}
21589
21590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 * Set number v: variable to "val".
21592 */
21593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021594set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021596 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597}
21598
21599/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021600 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 */
21602 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021603get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021605 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606}
21607
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021608/*
21609 * Get string v: variable value. Uses a static buffer, can only be used once.
21610 */
21611 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021612get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021613{
21614 return get_tv_string(&vimvars[idx].vv_tv);
21615}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021616
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021618 * Get List v: variable value. Caller must take care of reference count when
21619 * needed.
21620 */
21621 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021622get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021623{
21624 return vimvars[idx].vv_list;
21625}
21626
21627/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021628 * Set v:char to character "c".
21629 */
21630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021631set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021632{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021633 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021634
21635#ifdef FEAT_MBYTE
21636 if (has_mbyte)
21637 buf[(*mb_char2bytes)(c, buf)] = NUL;
21638 else
21639#endif
21640 {
21641 buf[0] = c;
21642 buf[1] = NUL;
21643 }
21644 set_vim_var_string(VV_CHAR, buf, -1);
21645}
21646
21647/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021648 * Set v:count to "count" and v:count1 to "count1".
21649 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 */
21651 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021652set_vcount(
21653 long count,
21654 long count1,
21655 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021657 if (set_prevcount)
21658 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021659 vimvars[VV_COUNT].vv_nr = count;
21660 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661}
21662
21663/*
21664 * Set string v: variable to a copy of "val".
21665 */
21666 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021667set_vim_var_string(
21668 int idx,
21669 char_u *val,
21670 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021671{
Bram Moolenaara542c682016-01-31 16:28:04 +010021672 clear_tv(&vimvars[idx].vv_di.di_tv);
21673 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021675 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021677 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021679 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680}
21681
21682/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021683 * Set List v: variable to "val".
21684 */
21685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021686set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021687{
Bram Moolenaara542c682016-01-31 16:28:04 +010021688 clear_tv(&vimvars[idx].vv_di.di_tv);
21689 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021690 vimvars[idx].vv_list = val;
21691 if (val != NULL)
21692 ++val->lv_refcount;
21693}
21694
21695/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021696 * Set Dictionary v: variable to "val".
21697 */
21698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021699set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021700{
21701 int todo;
21702 hashitem_T *hi;
21703
Bram Moolenaara542c682016-01-31 16:28:04 +010021704 clear_tv(&vimvars[idx].vv_di.di_tv);
21705 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021706 vimvars[idx].vv_dict = val;
21707 if (val != NULL)
21708 {
21709 ++val->dv_refcount;
21710
21711 /* Set readonly */
21712 todo = (int)val->dv_hashtab.ht_used;
21713 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21714 {
21715 if (HASHITEM_EMPTY(hi))
21716 continue;
21717 --todo;
21718 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21719 }
21720 }
21721}
21722
21723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724 * Set v:register if needed.
21725 */
21726 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021727set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728{
21729 char_u regname;
21730
21731 if (c == 0 || c == ' ')
21732 regname = '"';
21733 else
21734 regname = c;
21735 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021736 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737 set_vim_var_string(VV_REG, &regname, 1);
21738}
21739
21740/*
21741 * Get or set v:exception. If "oldval" == NULL, return the current value.
21742 * Otherwise, restore the value to "oldval" and return NULL.
21743 * Must always be called in pairs to save and restore v:exception! Does not
21744 * take care of memory allocations.
21745 */
21746 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021747v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748{
21749 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021750 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751
Bram Moolenaare9a41262005-01-15 22:18:47 +000021752 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753 return NULL;
21754}
21755
21756/*
21757 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21758 * Otherwise, restore the value to "oldval" and return NULL.
21759 * Must always be called in pairs to save and restore v:throwpoint! Does not
21760 * take care of memory allocations.
21761 */
21762 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021763v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764{
21765 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021766 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767
Bram Moolenaare9a41262005-01-15 22:18:47 +000021768 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 return NULL;
21770}
21771
21772#if defined(FEAT_AUTOCMD) || defined(PROTO)
21773/*
21774 * Set v:cmdarg.
21775 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21776 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21777 * Must always be called in pairs!
21778 */
21779 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021780set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781{
21782 char_u *oldval;
21783 char_u *newval;
21784 unsigned len;
21785
Bram Moolenaare9a41262005-01-15 22:18:47 +000021786 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021787 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021788 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021789 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021790 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021791 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792 }
21793
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021794 if (eap->force_bin == FORCE_BIN)
21795 len = 6;
21796 else if (eap->force_bin == FORCE_NOBIN)
21797 len = 8;
21798 else
21799 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021800
21801 if (eap->read_edit)
21802 len += 7;
21803
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021804 if (eap->force_ff != 0)
21805 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21806# ifdef FEAT_MBYTE
21807 if (eap->force_enc != 0)
21808 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021809 if (eap->bad_char != 0)
21810 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021811# endif
21812
21813 newval = alloc(len + 1);
21814 if (newval == NULL)
21815 return NULL;
21816
21817 if (eap->force_bin == FORCE_BIN)
21818 sprintf((char *)newval, " ++bin");
21819 else if (eap->force_bin == FORCE_NOBIN)
21820 sprintf((char *)newval, " ++nobin");
21821 else
21822 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021823
21824 if (eap->read_edit)
21825 STRCAT(newval, " ++edit");
21826
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021827 if (eap->force_ff != 0)
21828 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21829 eap->cmd + eap->force_ff);
21830# ifdef FEAT_MBYTE
21831 if (eap->force_enc != 0)
21832 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21833 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021834 if (eap->bad_char == BAD_KEEP)
21835 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21836 else if (eap->bad_char == BAD_DROP)
21837 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21838 else if (eap->bad_char != 0)
21839 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021840# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021841 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021842 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843}
21844#endif
21845
21846/*
21847 * Get the value of internal variable "name".
21848 * Return OK or FAIL.
21849 */
21850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021851get_var_tv(
21852 char_u *name,
21853 int len, /* length of "name" */
21854 typval_T *rettv, /* NULL when only checking existence */
21855 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21856 int verbose, /* may give error message */
21857 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858{
21859 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021860 typval_T *tv = NULL;
21861 typval_T atv;
21862 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864
21865 /* truncate the name, so that we can use strcmp() */
21866 cc = name[len];
21867 name[len] = NUL;
21868
21869 /*
21870 * Check for "b:changedtick".
21871 */
21872 if (STRCMP(name, "b:changedtick") == 0)
21873 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021874 atv.v_type = VAR_NUMBER;
21875 atv.vval.v_number = curbuf->b_changedtick;
21876 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 }
21878
21879 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880 * Check for user-defined variables.
21881 */
21882 else
21883 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021884 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021886 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021887 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021888 if (dip != NULL)
21889 *dip = v;
21890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891 }
21892
Bram Moolenaare9a41262005-01-15 22:18:47 +000021893 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021895 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021896 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897 ret = FAIL;
21898 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021899 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021900 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901
21902 name[len] = cc;
21903
21904 return ret;
21905}
21906
21907/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021908 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21909 * Also handle function call with Funcref variable: func(expr)
21910 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21911 */
21912 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021913handle_subscript(
21914 char_u **arg,
21915 typval_T *rettv,
21916 int evaluate, /* do more than finding the end */
21917 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021918{
21919 int ret = OK;
21920 dict_T *selfdict = NULL;
21921 char_u *s;
21922 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021923 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021924
21925 while (ret == OK
21926 && (**arg == '['
21927 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021928 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021929 && !vim_iswhite(*(*arg - 1)))
21930 {
21931 if (**arg == '(')
21932 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021933 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021934 if (evaluate)
21935 {
21936 functv = *rettv;
21937 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021938
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021939 /* Invoke the function. Recursive! */
21940 s = functv.vval.v_string;
21941 }
21942 else
21943 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021944 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021945 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21946 &len, evaluate, selfdict);
21947
21948 /* Clear the funcref afterwards, so that deleting it while
21949 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021950 if (evaluate)
21951 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021952
21953 /* Stop the expression evaluation when immediately aborting on
21954 * error, or when an interrupt occurred or an exception was thrown
21955 * but not caught. */
21956 if (aborting())
21957 {
21958 if (ret == OK)
21959 clear_tv(rettv);
21960 ret = FAIL;
21961 }
21962 dict_unref(selfdict);
21963 selfdict = NULL;
21964 }
21965 else /* **arg == '[' || **arg == '.' */
21966 {
21967 dict_unref(selfdict);
21968 if (rettv->v_type == VAR_DICT)
21969 {
21970 selfdict = rettv->vval.v_dict;
21971 if (selfdict != NULL)
21972 ++selfdict->dv_refcount;
21973 }
21974 else
21975 selfdict = NULL;
21976 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21977 {
21978 clear_tv(rettv);
21979 ret = FAIL;
21980 }
21981 }
21982 }
21983 dict_unref(selfdict);
21984 return ret;
21985}
21986
21987/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021988 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021989 * value).
21990 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021991 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021992alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021993{
Bram Moolenaar33570922005-01-25 22:26:29 +000021994 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021995}
21996
21997/*
21998 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 * The string "s" must have been allocated, it is consumed.
22000 * Return NULL for out of memory, the variable otherwise.
22001 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022002 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022003alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004{
Bram Moolenaar33570922005-01-25 22:26:29 +000022005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022007 rettv = alloc_tv();
22008 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022010 rettv->v_type = VAR_STRING;
22011 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 }
22013 else
22014 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022015 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016}
22017
22018/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022019 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022021 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022022free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023{
22024 if (varp != NULL)
22025 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022026 switch (varp->v_type)
22027 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022028 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022029 func_unref(varp->vval.v_string);
22030 /*FALLTHROUGH*/
22031 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022032 vim_free(varp->vval.v_string);
22033 break;
22034 case VAR_LIST:
22035 list_unref(varp->vval.v_list);
22036 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022037 case VAR_DICT:
22038 dict_unref(varp->vval.v_dict);
22039 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022040 case VAR_JOB:
22041#ifdef FEAT_JOB
22042 job_unref(varp->vval.v_job);
22043 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022044#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022045 case VAR_CHANNEL:
22046#ifdef FEAT_CHANNEL
22047 channel_unref(varp->vval.v_channel);
22048 break;
22049#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022050 case VAR_NUMBER:
22051 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022052 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022053 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022054 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056 vim_free(varp);
22057 }
22058}
22059
22060/*
22061 * Free the memory for a variable value and set the value to NULL or 0.
22062 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022064clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065{
22066 if (varp != NULL)
22067 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022068 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022070 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022071 func_unref(varp->vval.v_string);
22072 /*FALLTHROUGH*/
22073 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022074 vim_free(varp->vval.v_string);
22075 varp->vval.v_string = NULL;
22076 break;
22077 case VAR_LIST:
22078 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022079 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022080 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022081 case VAR_DICT:
22082 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022083 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022084 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022085 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022086 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022087 varp->vval.v_number = 0;
22088 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022089 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022090#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022091 varp->vval.v_float = 0.0;
22092 break;
22093#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022094 case VAR_JOB:
22095#ifdef FEAT_JOB
22096 job_unref(varp->vval.v_job);
22097 varp->vval.v_job = NULL;
22098#endif
22099 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022100 case VAR_CHANNEL:
22101#ifdef FEAT_CHANNEL
22102 channel_unref(varp->vval.v_channel);
22103 varp->vval.v_channel = NULL;
22104#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022105 case VAR_UNKNOWN:
22106 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022108 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109 }
22110}
22111
22112/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022113 * Set the value of a variable to NULL without freeing items.
22114 */
22115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022116init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022117{
22118 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022119 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022120}
22121
22122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 * Get the number value of a variable.
22124 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022125 * For incompatible types, return 0.
22126 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22127 * caller of incompatible types: it sets *denote to TRUE if "denote"
22128 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129 */
22130 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022131get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022133 int error = FALSE;
22134
22135 return get_tv_number_chk(varp, &error); /* return 0L on error */
22136}
22137
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022138 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022139get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022140{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022141 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022143 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022145 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022146 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022147 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022148#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022149 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022150 break;
22151#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022152 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022153 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022154 break;
22155 case VAR_STRING:
22156 if (varp->vval.v_string != NULL)
22157 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022158 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022159 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022160 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022161 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022162 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022163 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022164 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022165 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022166 case VAR_SPECIAL:
22167 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22168 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022169 case VAR_JOB:
22170#ifdef FEAT_JOB
22171 EMSG(_("E910: Using a Job as a Number"));
22172 break;
22173#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022174 case VAR_CHANNEL:
22175#ifdef FEAT_CHANNEL
22176 EMSG(_("E913: Using a Channel as a Number"));
22177 break;
22178#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022179 case VAR_UNKNOWN:
22180 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022181 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022183 if (denote == NULL) /* useful for values that must be unsigned */
22184 n = -1;
22185 else
22186 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022187 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188}
22189
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022190#ifdef FEAT_FLOAT
22191 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022192get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022193{
22194 switch (varp->v_type)
22195 {
22196 case VAR_NUMBER:
22197 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022198 case VAR_FLOAT:
22199 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022200 case VAR_FUNC:
22201 EMSG(_("E891: Using a Funcref as a Float"));
22202 break;
22203 case VAR_STRING:
22204 EMSG(_("E892: Using a String as a Float"));
22205 break;
22206 case VAR_LIST:
22207 EMSG(_("E893: Using a List as a Float"));
22208 break;
22209 case VAR_DICT:
22210 EMSG(_("E894: Using a Dictionary as a Float"));
22211 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022212 case VAR_SPECIAL:
22213 EMSG(_("E907: Using a special value as a Float"));
22214 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022215 case VAR_JOB:
22216# ifdef FEAT_JOB
22217 EMSG(_("E911: Using a Job as a Float"));
22218 break;
22219# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022220 case VAR_CHANNEL:
22221# ifdef FEAT_CHANNEL
22222 EMSG(_("E914: Using a Channel as a Float"));
22223 break;
22224# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022225 case VAR_UNKNOWN:
22226 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022227 break;
22228 }
22229 return 0;
22230}
22231#endif
22232
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022234 * Get the lnum from the first argument.
22235 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022236 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237 */
22238 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022239get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240{
Bram Moolenaar33570922005-01-25 22:26:29 +000022241 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 linenr_T lnum;
22243
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022244 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 if (lnum == 0) /* no valid number, try using line() */
22246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022247 rettv.v_type = VAR_NUMBER;
22248 f_line(argvars, &rettv);
22249 lnum = rettv.vval.v_number;
22250 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251 }
22252 return lnum;
22253}
22254
22255/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022256 * Get the lnum from the first argument.
22257 * Also accepts "$", then "buf" is used.
22258 * Returns 0 on error.
22259 */
22260 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022261get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022262{
22263 if (argvars[0].v_type == VAR_STRING
22264 && argvars[0].vval.v_string != NULL
22265 && argvars[0].vval.v_string[0] == '$'
22266 && buf != NULL)
22267 return buf->b_ml.ml_line_count;
22268 return get_tv_number_chk(&argvars[0], NULL);
22269}
22270
22271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 * Get the string value of a variable.
22273 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022274 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22275 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 * If the String variable has never been set, return an empty string.
22277 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022278 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22279 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 */
22281 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022282get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283{
22284 static char_u mybuf[NUMBUFLEN];
22285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022286 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287}
22288
22289 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022290get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022292 char_u *res = get_tv_string_buf_chk(varp, buf);
22293
22294 return res != NULL ? res : (char_u *)"";
22295}
22296
Bram Moolenaar7d647822014-04-05 21:28:56 +020022297/*
22298 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22299 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022300 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022301get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022302{
22303 static char_u mybuf[NUMBUFLEN];
22304
22305 return get_tv_string_buf_chk(varp, mybuf);
22306}
22307
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022308 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022309get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022310{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022311 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022312 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022313 case VAR_NUMBER:
22314 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22315 return buf;
22316 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022317 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022318 break;
22319 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022320 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022321 break;
22322 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022323 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022324 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022325 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022326#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022327 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022328 break;
22329#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022330 case VAR_STRING:
22331 if (varp->vval.v_string != NULL)
22332 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022333 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022334 case VAR_SPECIAL:
22335 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22336 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022337 case VAR_JOB:
22338#ifdef FEAT_JOB
22339 {
22340 job_T *job = varp->vval.v_job;
22341 char *status = job->jv_status == JOB_FAILED ? "fail"
22342 : job->jv_status == JOB_ENDED ? "dead"
22343 : "run";
22344# ifdef UNIX
22345 vim_snprintf((char *)buf, NUMBUFLEN,
22346 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022347# elif defined(WIN32)
22348 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022349 "process %ld %s",
22350 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022351 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022352# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022353 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022354 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22355# endif
22356 return buf;
22357 }
22358#endif
22359 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022360 case VAR_CHANNEL:
22361#ifdef FEAT_CHANNEL
22362 {
22363 channel_T *channel = varp->vval.v_channel;
22364 char *status = channel_status(channel);
22365
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022366 if (channel == NULL)
22367 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22368 else
22369 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022370 "channel %d %s", channel->ch_id, status);
22371 return buf;
22372 }
22373#endif
22374 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022375 case VAR_UNKNOWN:
22376 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022379 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380}
22381
22382/*
22383 * Find variable "name" in the list of variables.
22384 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022385 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022386 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022387 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022389 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022390find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022393 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394
Bram Moolenaara7043832005-01-21 11:56:39 +000022395 ht = find_var_ht(name, &varname);
22396 if (htp != NULL)
22397 *htp = ht;
22398 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022400 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401}
22402
22403/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022404 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022405 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022407 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022408find_var_in_ht(
22409 hashtab_T *ht,
22410 int htname,
22411 char_u *varname,
22412 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022413{
Bram Moolenaar33570922005-01-25 22:26:29 +000022414 hashitem_T *hi;
22415
22416 if (*varname == NUL)
22417 {
22418 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022419 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022420 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022421 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022422 case 'g': return &globvars_var;
22423 case 'v': return &vimvars_var;
22424 case 'b': return &curbuf->b_bufvar;
22425 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022426#ifdef FEAT_WINDOWS
22427 case 't': return &curtab->tp_winvar;
22428#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022429 case 'l': return current_funccal == NULL
22430 ? NULL : &current_funccal->l_vars_var;
22431 case 'a': return current_funccal == NULL
22432 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022433 }
22434 return NULL;
22435 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022436
22437 hi = hash_find(ht, varname);
22438 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022439 {
22440 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022441 * worked find the variable again. Don't auto-load a script if it was
22442 * loaded already, otherwise it would be loaded every time when
22443 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022444 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022445 {
22446 /* Note: script_autoload() may make "hi" invalid. It must either
22447 * be obtained again or not used. */
22448 if (!script_autoload(varname, FALSE) || aborting())
22449 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022450 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022451 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022452 if (HASHITEM_EMPTY(hi))
22453 return NULL;
22454 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022455 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022456}
22457
22458/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022459 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022460 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022461 * Set "varname" to the start of name without ':'.
22462 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022463 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022464find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022466 hashitem_T *hi;
22467
Bram Moolenaar73627d02015-08-11 15:46:09 +020022468 if (name[0] == NUL)
22469 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470 if (name[1] != ':')
22471 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022472 /* The name must not start with a colon or #. */
22473 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 return NULL;
22475 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022476
22477 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022478 hi = hash_find(&compat_hashtab, name);
22479 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022480 return &compat_hashtab;
22481
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022483 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022484 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485 }
22486 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022487 if (*name == 'g') /* global variable */
22488 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022489 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22490 */
22491 if (vim_strchr(name + 2, ':') != NULL
22492 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022493 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022495 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022497 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022498#ifdef FEAT_WINDOWS
22499 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022500 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022501#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022502 if (*name == 'v') /* v: variable */
22503 return &vimvarht;
22504 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022505 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022506 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022507 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508 if (*name == 's' /* script variable */
22509 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22510 return &SCRIPT_VARS(current_SID);
22511 return NULL;
22512}
22513
22514/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022515 * Get function call environment based on bactrace debug level
22516 */
22517 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022518get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022519{
22520 int i;
22521 funccall_T *funccal;
22522 funccall_T *temp_funccal;
22523
22524 funccal = current_funccal;
22525 if (debug_backtrace_level > 0)
22526 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022527 for (i = 0; i < debug_backtrace_level; i++)
22528 {
22529 temp_funccal = funccal->caller;
22530 if (temp_funccal)
22531 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022532 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022533 /* backtrace level overflow. reset to max */
22534 debug_backtrace_level = i;
22535 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022536 }
22537 return funccal;
22538}
22539
22540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022541 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022542 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 * Returns NULL when it doesn't exist.
22544 */
22545 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022546get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547{
Bram Moolenaar33570922005-01-25 22:26:29 +000022548 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022550 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 if (v == NULL)
22552 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022553 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554}
22555
22556/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022557 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 * sourcing this script and when executing functions defined in the script.
22559 */
22560 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022561new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562{
Bram Moolenaara7043832005-01-21 11:56:39 +000022563 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022564 hashtab_T *ht;
22565 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022566
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22568 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022569 /* Re-allocating ga_data means that an ht_array pointing to
22570 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022571 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022572 for (i = 1; i <= ga_scripts.ga_len; ++i)
22573 {
22574 ht = &SCRIPT_VARS(i);
22575 if (ht->ht_mask == HT_INIT_SIZE - 1)
22576 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022577 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022579 }
22580
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 while (ga_scripts.ga_len < id)
22582 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022583 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022584 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022585 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587 }
22588 }
22589}
22590
22591/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22593 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 */
22595 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022596init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597{
Bram Moolenaar33570922005-01-25 22:26:29 +000022598 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022599 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022600 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022601 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022602 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022603 dict_var->di_tv.vval.v_dict = dict;
22604 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022605 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022606 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22607 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022608}
22609
22610/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022611 * Unreference a dictionary initialized by init_var_dict().
22612 */
22613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022614unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022615{
22616 /* Now the dict needs to be freed if no one else is using it, go back to
22617 * normal reference counting. */
22618 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22619 dict_unref(dict);
22620}
22621
22622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022624 * Frees all allocated variables and the value they contain.
22625 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 */
22627 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022628vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022629{
22630 vars_clear_ext(ht, TRUE);
22631}
22632
22633/*
22634 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22635 */
22636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022637vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638{
Bram Moolenaara7043832005-01-21 11:56:39 +000022639 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022640 hashitem_T *hi;
22641 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022642
Bram Moolenaar33570922005-01-25 22:26:29 +000022643 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022644 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022645 for (hi = ht->ht_array; todo > 0; ++hi)
22646 {
22647 if (!HASHITEM_EMPTY(hi))
22648 {
22649 --todo;
22650
Bram Moolenaar33570922005-01-25 22:26:29 +000022651 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022652 * ht_array might change then. hash_clear() takes care of it
22653 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022654 v = HI2DI(hi);
22655 if (free_val)
22656 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022657 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022658 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022659 }
22660 }
22661 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022662 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663}
22664
Bram Moolenaara7043832005-01-21 11:56:39 +000022665/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022666 * Delete a variable from hashtab "ht" at item "hi".
22667 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022670delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671{
Bram Moolenaar33570922005-01-25 22:26:29 +000022672 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022673
22674 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022675 clear_tv(&di->di_tv);
22676 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677}
22678
22679/*
22680 * List the value of one internal variable.
22681 */
22682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022683list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022685 char_u *tofree;
22686 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022687 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022688
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022689 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022690 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022691 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022692 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693}
22694
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022696list_one_var_a(
22697 char_u *prefix,
22698 char_u *name,
22699 int type,
22700 char_u *string,
22701 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702{
Bram Moolenaar31859182007-08-14 20:41:13 +000022703 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22704 msg_start();
22705 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 if (name != NULL) /* "a:" vars don't have a name stored */
22707 msg_puts(name);
22708 msg_putchar(' ');
22709 msg_advance(22);
22710 if (type == VAR_NUMBER)
22711 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022712 else if (type == VAR_FUNC)
22713 msg_putchar('*');
22714 else if (type == VAR_LIST)
22715 {
22716 msg_putchar('[');
22717 if (*string == '[')
22718 ++string;
22719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022720 else if (type == VAR_DICT)
22721 {
22722 msg_putchar('{');
22723 if (*string == '{')
22724 ++string;
22725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 else
22727 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022728
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022730
22731 if (type == VAR_FUNC)
22732 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022733 if (*first)
22734 {
22735 msg_clr_eos();
22736 *first = FALSE;
22737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738}
22739
22740/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022741 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742 * If the variable already exists, the value is updated.
22743 * Otherwise the variable is created.
22744 */
22745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022746set_var(
22747 char_u *name,
22748 typval_T *tv,
22749 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750{
Bram Moolenaar33570922005-01-25 22:26:29 +000022751 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022753 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022755 ht = find_var_ht(name, &varname);
22756 if (ht == NULL || *varname == NUL)
22757 {
22758 EMSG2(_(e_illvar), name);
22759 return;
22760 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022761 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022762
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022763 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22764 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022765
Bram Moolenaar33570922005-01-25 22:26:29 +000022766 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022767 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022768 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022769 if (var_check_ro(v->di_flags, name, FALSE)
22770 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022771 return;
22772 if (v->di_tv.v_type != tv->v_type
22773 && !((v->di_tv.v_type == VAR_STRING
22774 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022775 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022776 || tv->v_type == VAR_NUMBER))
22777#ifdef FEAT_FLOAT
22778 && !((v->di_tv.v_type == VAR_NUMBER
22779 || v->di_tv.v_type == VAR_FLOAT)
22780 && (tv->v_type == VAR_NUMBER
22781 || tv->v_type == VAR_FLOAT))
22782#endif
22783 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022784 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022785 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022786 return;
22787 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022788
22789 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022790 * Handle setting internal v: variables separately where needed to
22791 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022792 */
22793 if (ht == &vimvarht)
22794 {
22795 if (v->di_tv.v_type == VAR_STRING)
22796 {
22797 vim_free(v->di_tv.vval.v_string);
22798 if (copy || tv->v_type != VAR_STRING)
22799 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22800 else
22801 {
22802 /* Take over the string to avoid an extra alloc/free. */
22803 v->di_tv.vval.v_string = tv->vval.v_string;
22804 tv->vval.v_string = NULL;
22805 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022806 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022807 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022808 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022809 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022810 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022811 if (STRCMP(varname, "searchforward") == 0)
22812 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022813#ifdef FEAT_SEARCH_EXTRA
22814 else if (STRCMP(varname, "hlsearch") == 0)
22815 {
22816 no_hlsearch = !v->di_tv.vval.v_number;
22817 redraw_all_later(SOME_VALID);
22818 }
22819#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022820 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022821 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022822 else if (v->di_tv.v_type != tv->v_type)
22823 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022824 }
22825
22826 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022827 }
22828 else /* add a new variable */
22829 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022830 /* Can't add "v:" variable. */
22831 if (ht == &vimvarht)
22832 {
22833 EMSG2(_(e_illvar), name);
22834 return;
22835 }
22836
Bram Moolenaar92124a32005-06-17 22:03:40 +000022837 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022838 if (!valid_varname(varname))
22839 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022840
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022841 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22842 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022843 if (v == NULL)
22844 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022845 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022846 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022848 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 return;
22850 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022851 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022853
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022854 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022855 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022856 else
22857 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022858 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022859 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022860 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862}
22863
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022864/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022865 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022866 * Also give an error message.
22867 */
22868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022869var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022870{
22871 if (flags & DI_FLAGS_RO)
22872 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022873 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022874 return TRUE;
22875 }
22876 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22877 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022878 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022879 return TRUE;
22880 }
22881 return FALSE;
22882}
22883
22884/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022885 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22886 * Also give an error message.
22887 */
22888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022889var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022890{
22891 if (flags & DI_FLAGS_FIX)
22892 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022893 EMSG2(_("E795: Cannot delete variable %s"),
22894 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022895 return TRUE;
22896 }
22897 return FALSE;
22898}
22899
22900/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022901 * Check if a funcref is assigned to a valid variable name.
22902 * Return TRUE and give an error if not.
22903 */
22904 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022905var_check_func_name(
22906 char_u *name, /* points to start of variable name */
22907 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022908{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022909 /* Allow for w: b: s: and t:. */
22910 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022911 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22912 ? name[2] : name[0]))
22913 {
22914 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22915 name);
22916 return TRUE;
22917 }
22918 /* Don't allow hiding a function. When "v" is not NULL we might be
22919 * assigning another function to the same var, the type is checked
22920 * below. */
22921 if (new_var && function_exists(name))
22922 {
22923 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22924 name);
22925 return TRUE;
22926 }
22927 return FALSE;
22928}
22929
22930/*
22931 * Check if a variable name is valid.
22932 * Return FALSE and give an error if not.
22933 */
22934 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022935valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022936{
22937 char_u *p;
22938
22939 for (p = varname; *p != NUL; ++p)
22940 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22941 && *p != AUTOLOAD_CHAR)
22942 {
22943 EMSG2(_(e_illvar), varname);
22944 return FALSE;
22945 }
22946 return TRUE;
22947}
22948
22949/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022950 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022951 * Also give an error message, using "name" or _("name") when use_gettext is
22952 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022953 */
22954 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022955tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022956{
22957 if (lock & VAR_LOCKED)
22958 {
22959 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022960 name == NULL ? (char_u *)_("Unknown")
22961 : use_gettext ? (char_u *)_(name)
22962 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022963 return TRUE;
22964 }
22965 if (lock & VAR_FIXED)
22966 {
22967 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022968 name == NULL ? (char_u *)_("Unknown")
22969 : use_gettext ? (char_u *)_(name)
22970 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022971 return TRUE;
22972 }
22973 return FALSE;
22974}
22975
22976/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022977 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022978 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022979 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022980 * It is OK for "from" and "to" to point to the same item. This is used to
22981 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022982 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022983 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022984copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022986 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022987 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022988 switch (from->v_type)
22989 {
22990 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022991 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022992 to->vval.v_number = from->vval.v_number;
22993 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022994 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022995#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022996 to->vval.v_float = from->vval.v_float;
22997 break;
22998#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022999 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023000#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023001 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023002 if (to->vval.v_job != NULL)
23003 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023004 break;
23005#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023006 case VAR_CHANNEL:
23007#ifdef FEAT_CHANNEL
23008 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023009 if (to->vval.v_channel != NULL)
23010 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023011 break;
23012#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023013 case VAR_STRING:
23014 case VAR_FUNC:
23015 if (from->vval.v_string == NULL)
23016 to->vval.v_string = NULL;
23017 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023018 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023019 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023020 if (from->v_type == VAR_FUNC)
23021 func_ref(to->vval.v_string);
23022 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023023 break;
23024 case VAR_LIST:
23025 if (from->vval.v_list == NULL)
23026 to->vval.v_list = NULL;
23027 else
23028 {
23029 to->vval.v_list = from->vval.v_list;
23030 ++to->vval.v_list->lv_refcount;
23031 }
23032 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023033 case VAR_DICT:
23034 if (from->vval.v_dict == NULL)
23035 to->vval.v_dict = NULL;
23036 else
23037 {
23038 to->vval.v_dict = from->vval.v_dict;
23039 ++to->vval.v_dict->dv_refcount;
23040 }
23041 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023042 case VAR_UNKNOWN:
23043 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023044 break;
23045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046}
23047
23048/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023049 * Make a copy of an item.
23050 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023051 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23052 * reference to an already copied list/dict can be used.
23053 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023054 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023055 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023056item_copy(
23057 typval_T *from,
23058 typval_T *to,
23059 int deep,
23060 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023061{
23062 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023063 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023064
Bram Moolenaar33570922005-01-25 22:26:29 +000023065 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023066 {
23067 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023068 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023069 }
23070 ++recurse;
23071
23072 switch (from->v_type)
23073 {
23074 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023075 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023076 case VAR_STRING:
23077 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023078 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023079 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023080 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023081 copy_tv(from, to);
23082 break;
23083 case VAR_LIST:
23084 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023085 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023086 if (from->vval.v_list == NULL)
23087 to->vval.v_list = NULL;
23088 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23089 {
23090 /* use the copy made earlier */
23091 to->vval.v_list = from->vval.v_list->lv_copylist;
23092 ++to->vval.v_list->lv_refcount;
23093 }
23094 else
23095 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23096 if (to->vval.v_list == NULL)
23097 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023098 break;
23099 case VAR_DICT:
23100 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023101 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023102 if (from->vval.v_dict == NULL)
23103 to->vval.v_dict = NULL;
23104 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23105 {
23106 /* use the copy made earlier */
23107 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23108 ++to->vval.v_dict->dv_refcount;
23109 }
23110 else
23111 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23112 if (to->vval.v_dict == NULL)
23113 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023114 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023115 case VAR_UNKNOWN:
23116 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023117 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023118 }
23119 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023120 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023121}
23122
23123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 * ":echo expr1 ..." print each argument separated with a space, add a
23125 * newline at the end.
23126 * ":echon expr1 ..." print each argument plain.
23127 */
23128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023129ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023130{
23131 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023132 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023133 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 char_u *p;
23135 int needclr = TRUE;
23136 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023137 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138
23139 if (eap->skip)
23140 ++emsg_skip;
23141 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23142 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023143 /* If eval1() causes an error message the text from the command may
23144 * still need to be cleared. E.g., "echo 22,44". */
23145 need_clr_eos = needclr;
23146
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023148 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 {
23150 /*
23151 * Report the invalid expression unless the expression evaluation
23152 * has been cancelled due to an aborting error, an interrupt, or an
23153 * exception.
23154 */
23155 if (!aborting())
23156 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023157 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 break;
23159 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023160 need_clr_eos = FALSE;
23161
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 if (!eap->skip)
23163 {
23164 if (atstart)
23165 {
23166 atstart = FALSE;
23167 /* Call msg_start() after eval1(), evaluating the expression
23168 * may cause a message to appear. */
23169 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023170 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023171 /* Mark the saved text as finishing the line, so that what
23172 * follows is displayed on a new line when scrolling back
23173 * at the more prompt. */
23174 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023175 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177 }
23178 else if (eap->cmdidx == CMD_echo)
23179 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023180 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023181 if (p != NULL)
23182 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023184 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023186 if (*p != TAB && needclr)
23187 {
23188 /* remove any text still there from the command */
23189 msg_clr_eos();
23190 needclr = FALSE;
23191 }
23192 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 }
23194 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023195 {
23196#ifdef FEAT_MBYTE
23197 if (has_mbyte)
23198 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023199 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023200
23201 (void)msg_outtrans_len_attr(p, i, echo_attr);
23202 p += i - 1;
23203 }
23204 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023206 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023209 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023211 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212 arg = skipwhite(arg);
23213 }
23214 eap->nextcmd = check_nextcmd(arg);
23215
23216 if (eap->skip)
23217 --emsg_skip;
23218 else
23219 {
23220 /* remove text that may still be there from the command */
23221 if (needclr)
23222 msg_clr_eos();
23223 if (eap->cmdidx == CMD_echo)
23224 msg_end();
23225 }
23226}
23227
23228/*
23229 * ":echohl {name}".
23230 */
23231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023232ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233{
23234 int id;
23235
23236 id = syn_name2id(eap->arg);
23237 if (id == 0)
23238 echo_attr = 0;
23239 else
23240 echo_attr = syn_id2attr(id);
23241}
23242
23243/*
23244 * ":execute expr1 ..." execute the result of an expression.
23245 * ":echomsg expr1 ..." Print a message
23246 * ":echoerr expr1 ..." Print an error
23247 * Each gets spaces around each argument and a newline at the end for
23248 * echo commands
23249 */
23250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023251ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252{
23253 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023254 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023255 int ret = OK;
23256 char_u *p;
23257 garray_T ga;
23258 int len;
23259 int save_did_emsg;
23260
23261 ga_init2(&ga, 1, 80);
23262
23263 if (eap->skip)
23264 ++emsg_skip;
23265 while (*arg != NUL && *arg != '|' && *arg != '\n')
23266 {
23267 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023268 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 {
23270 /*
23271 * Report the invalid expression unless the expression evaluation
23272 * has been cancelled due to an aborting error, an interrupt, or an
23273 * exception.
23274 */
23275 if (!aborting())
23276 EMSG2(_(e_invexpr2), p);
23277 ret = FAIL;
23278 break;
23279 }
23280
23281 if (!eap->skip)
23282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023283 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284 len = (int)STRLEN(p);
23285 if (ga_grow(&ga, len + 2) == FAIL)
23286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023287 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023288 ret = FAIL;
23289 break;
23290 }
23291 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023294 ga.ga_len += len;
23295 }
23296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023297 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298 arg = skipwhite(arg);
23299 }
23300
23301 if (ret != FAIL && ga.ga_data != NULL)
23302 {
23303 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023306 out_flush();
23307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023308 else if (eap->cmdidx == CMD_echoerr)
23309 {
23310 /* We don't want to abort following commands, restore did_emsg. */
23311 save_did_emsg = did_emsg;
23312 EMSG((char_u *)ga.ga_data);
23313 if (!force_abort)
23314 did_emsg = save_did_emsg;
23315 }
23316 else if (eap->cmdidx == CMD_execute)
23317 do_cmdline((char_u *)ga.ga_data,
23318 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23319 }
23320
23321 ga_clear(&ga);
23322
23323 if (eap->skip)
23324 --emsg_skip;
23325
23326 eap->nextcmd = check_nextcmd(arg);
23327}
23328
23329/*
23330 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23331 * "arg" points to the "&" or '+' when called, to "option" when returning.
23332 * Returns NULL when no option name found. Otherwise pointer to the char
23333 * after the option name.
23334 */
23335 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023336find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023337{
23338 char_u *p = *arg;
23339
23340 ++p;
23341 if (*p == 'g' && p[1] == ':')
23342 {
23343 *opt_flags = OPT_GLOBAL;
23344 p += 2;
23345 }
23346 else if (*p == 'l' && p[1] == ':')
23347 {
23348 *opt_flags = OPT_LOCAL;
23349 p += 2;
23350 }
23351 else
23352 *opt_flags = 0;
23353
23354 if (!ASCII_ISALPHA(*p))
23355 return NULL;
23356 *arg = p;
23357
23358 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23359 p += 4; /* termcap option */
23360 else
23361 while (ASCII_ISALPHA(*p))
23362 ++p;
23363 return p;
23364}
23365
23366/*
23367 * ":function"
23368 */
23369 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023370ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371{
23372 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023373 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374 int j;
23375 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023377 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378 char_u *name = NULL;
23379 char_u *p;
23380 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023381 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 garray_T newargs;
23383 garray_T newlines;
23384 int varargs = FALSE;
23385 int mustend = FALSE;
23386 int flags = 0;
23387 ufunc_T *fp;
23388 int indent;
23389 int nesting;
23390 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023391 dictitem_T *v;
23392 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023393 static int func_nr = 0; /* number for nameless function */
23394 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023395 hashtab_T *ht;
23396 int todo;
23397 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023398 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023399
23400 /*
23401 * ":function" without argument: list functions.
23402 */
23403 if (ends_excmd(*eap->arg))
23404 {
23405 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023406 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023407 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023408 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023409 {
23410 if (!HASHITEM_EMPTY(hi))
23411 {
23412 --todo;
23413 fp = HI2UF(hi);
23414 if (!isdigit(*fp->uf_name))
23415 list_func_head(fp, FALSE);
23416 }
23417 }
23418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419 eap->nextcmd = check_nextcmd(eap->arg);
23420 return;
23421 }
23422
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023423 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023424 * ":function /pat": list functions matching pattern.
23425 */
23426 if (*eap->arg == '/')
23427 {
23428 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23429 if (!eap->skip)
23430 {
23431 regmatch_T regmatch;
23432
23433 c = *p;
23434 *p = NUL;
23435 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23436 *p = c;
23437 if (regmatch.regprog != NULL)
23438 {
23439 regmatch.rm_ic = p_ic;
23440
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023441 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023442 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23443 {
23444 if (!HASHITEM_EMPTY(hi))
23445 {
23446 --todo;
23447 fp = HI2UF(hi);
23448 if (!isdigit(*fp->uf_name)
23449 && vim_regexec(&regmatch, fp->uf_name, 0))
23450 list_func_head(fp, FALSE);
23451 }
23452 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023453 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023454 }
23455 }
23456 if (*p == '/')
23457 ++p;
23458 eap->nextcmd = check_nextcmd(p);
23459 return;
23460 }
23461
23462 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023463 * Get the function name. There are these situations:
23464 * func normal function name
23465 * "name" == func, "fudi.fd_dict" == NULL
23466 * dict.func new dictionary entry
23467 * "name" == NULL, "fudi.fd_dict" set,
23468 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23469 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023470 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023471 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23472 * dict.func existing dict entry that's not a Funcref
23473 * "name" == NULL, "fudi.fd_dict" set,
23474 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023475 * s:func script-local function name
23476 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023479 name = trans_function_name(&p, eap->skip, 0, &fudi);
23480 paren = (vim_strchr(p, '(') != NULL);
23481 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 {
23483 /*
23484 * Return on an invalid expression in braces, unless the expression
23485 * evaluation has been cancelled due to an aborting error, an
23486 * interrupt, or an exception.
23487 */
23488 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023489 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023490 if (!eap->skip && fudi.fd_newkey != NULL)
23491 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023492 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495 else
23496 eap->skip = TRUE;
23497 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023498
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 /* An error in a function call during evaluation of an expression in magic
23500 * braces should not cause the function not to be defined. */
23501 saved_did_emsg = did_emsg;
23502 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503
23504 /*
23505 * ":function func" with only function name: list function.
23506 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023507 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508 {
23509 if (!ends_excmd(*skipwhite(p)))
23510 {
23511 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023512 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023513 }
23514 eap->nextcmd = check_nextcmd(p);
23515 if (eap->nextcmd != NULL)
23516 *p = NUL;
23517 if (!eap->skip && !got_int)
23518 {
23519 fp = find_func(name);
23520 if (fp != NULL)
23521 {
23522 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023523 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023525 if (FUNCLINE(fp, j) == NULL)
23526 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 msg_putchar('\n');
23528 msg_outnum((long)(j + 1));
23529 if (j < 9)
23530 msg_putchar(' ');
23531 if (j < 99)
23532 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023533 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023534 out_flush(); /* show a line at a time */
23535 ui_breakcheck();
23536 }
23537 if (!got_int)
23538 {
23539 msg_putchar('\n');
23540 msg_puts((char_u *)" endfunction");
23541 }
23542 }
23543 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023544 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023546 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547 }
23548
23549 /*
23550 * ":function name(arg1, arg2)" Define function.
23551 */
23552 p = skipwhite(p);
23553 if (*p != '(')
23554 {
23555 if (!eap->skip)
23556 {
23557 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023558 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 }
23560 /* attempt to continue by skipping some text */
23561 if (vim_strchr(p, '(') != NULL)
23562 p = vim_strchr(p, '(');
23563 }
23564 p = skipwhite(p + 1);
23565
23566 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23567 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23568
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023569 if (!eap->skip)
23570 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023571 /* Check the name of the function. Unless it's a dictionary function
23572 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023573 if (name != NULL)
23574 arg = name;
23575 else
23576 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023577 if (arg != NULL && (fudi.fd_di == NULL
23578 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023579 {
23580 if (*arg == K_SPECIAL)
23581 j = 3;
23582 else
23583 j = 0;
23584 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23585 : eval_isnamec(arg[j])))
23586 ++j;
23587 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023588 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023589 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023590 /* Disallow using the g: dict. */
23591 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23592 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023593 }
23594
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 /*
23596 * Isolate the arguments: "arg1, arg2, ...)"
23597 */
23598 while (*p != ')')
23599 {
23600 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23601 {
23602 varargs = TRUE;
23603 p += 3;
23604 mustend = TRUE;
23605 }
23606 else
23607 {
23608 arg = p;
23609 while (ASCII_ISALNUM(*p) || *p == '_')
23610 ++p;
23611 if (arg == p || isdigit(*arg)
23612 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23613 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23614 {
23615 if (!eap->skip)
23616 EMSG2(_("E125: Illegal argument: %s"), arg);
23617 break;
23618 }
23619 if (ga_grow(&newargs, 1) == FAIL)
23620 goto erret;
23621 c = *p;
23622 *p = NUL;
23623 arg = vim_strsave(arg);
23624 if (arg == NULL)
23625 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023626
23627 /* Check for duplicate argument name. */
23628 for (i = 0; i < newargs.ga_len; ++i)
23629 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23630 {
23631 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023632 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023633 goto erret;
23634 }
23635
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23637 *p = c;
23638 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639 if (*p == ',')
23640 ++p;
23641 else
23642 mustend = TRUE;
23643 }
23644 p = skipwhite(p);
23645 if (mustend && *p != ')')
23646 {
23647 if (!eap->skip)
23648 EMSG2(_(e_invarg2), eap->arg);
23649 break;
23650 }
23651 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023652 if (*p != ')')
23653 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654 ++p; /* skip the ')' */
23655
Bram Moolenaare9a41262005-01-15 22:18:47 +000023656 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 for (;;)
23658 {
23659 p = skipwhite(p);
23660 if (STRNCMP(p, "range", 5) == 0)
23661 {
23662 flags |= FC_RANGE;
23663 p += 5;
23664 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023665 else if (STRNCMP(p, "dict", 4) == 0)
23666 {
23667 flags |= FC_DICT;
23668 p += 4;
23669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 else if (STRNCMP(p, "abort", 5) == 0)
23671 {
23672 flags |= FC_ABORT;
23673 p += 5;
23674 }
23675 else
23676 break;
23677 }
23678
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023679 /* When there is a line break use what follows for the function body.
23680 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23681 if (*p == '\n')
23682 line_arg = p + 1;
23683 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684 EMSG(_(e_trailing));
23685
23686 /*
23687 * Read the body of the function, until ":endfunction" is found.
23688 */
23689 if (KeyTyped)
23690 {
23691 /* Check if the function already exists, don't let the user type the
23692 * whole function before telling him it doesn't work! For a script we
23693 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023694 if (!eap->skip && !eap->forceit)
23695 {
23696 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23697 EMSG(_(e_funcdict));
23698 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023699 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023701
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023702 if (!eap->skip && did_emsg)
23703 goto erret;
23704
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705 msg_putchar('\n'); /* don't overwrite the function name */
23706 cmdline_row = msg_row;
23707 }
23708
23709 indent = 2;
23710 nesting = 0;
23711 for (;;)
23712 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023713 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023714 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023715 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023716 saved_wait_return = FALSE;
23717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023719 sourcing_lnum_off = sourcing_lnum;
23720
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023721 if (line_arg != NULL)
23722 {
23723 /* Use eap->arg, split up in parts by line breaks. */
23724 theline = line_arg;
23725 p = vim_strchr(theline, '\n');
23726 if (p == NULL)
23727 line_arg += STRLEN(line_arg);
23728 else
23729 {
23730 *p = NUL;
23731 line_arg = p + 1;
23732 }
23733 }
23734 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023735 theline = getcmdline(':', 0L, indent);
23736 else
23737 theline = eap->getline(':', eap->cookie, indent);
23738 if (KeyTyped)
23739 lines_left = Rows - 1;
23740 if (theline == NULL)
23741 {
23742 EMSG(_("E126: Missing :endfunction"));
23743 goto erret;
23744 }
23745
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023746 /* Detect line continuation: sourcing_lnum increased more than one. */
23747 if (sourcing_lnum > sourcing_lnum_off + 1)
23748 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23749 else
23750 sourcing_lnum_off = 0;
23751
Bram Moolenaar071d4272004-06-13 20:20:40 +000023752 if (skip_until != NULL)
23753 {
23754 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23755 * don't check for ":endfunc". */
23756 if (STRCMP(theline, skip_until) == 0)
23757 {
23758 vim_free(skip_until);
23759 skip_until = NULL;
23760 }
23761 }
23762 else
23763 {
23764 /* skip ':' and blanks*/
23765 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23766 ;
23767
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023768 /* Check for "endfunction". */
23769 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023770 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023771 if (line_arg == NULL)
23772 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773 break;
23774 }
23775
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023776 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023777 * at "end". */
23778 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23779 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023780 else if (STRNCMP(p, "if", 2) == 0
23781 || STRNCMP(p, "wh", 2) == 0
23782 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023783 || STRNCMP(p, "try", 3) == 0)
23784 indent += 2;
23785
23786 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023787 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023789 if (*p == '!')
23790 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023791 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023792 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23793 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023794 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023795 ++nesting;
23796 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023797 }
23798 }
23799
23800 /* Check for ":append" or ":insert". */
23801 p = skip_range(p, NULL);
23802 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23803 || (p[0] == 'i'
23804 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23805 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23806 skip_until = vim_strsave((char_u *)".");
23807
23808 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23809 arg = skipwhite(skiptowhite(p));
23810 if (arg[0] == '<' && arg[1] =='<'
23811 && ((p[0] == 'p' && p[1] == 'y'
23812 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23813 || (p[0] == 'p' && p[1] == 'e'
23814 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23815 || (p[0] == 't' && p[1] == 'c'
23816 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023817 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23818 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23820 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023821 || (p[0] == 'm' && p[1] == 'z'
23822 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023823 ))
23824 {
23825 /* ":python <<" continues until a dot, like ":append" */
23826 p = skipwhite(arg + 2);
23827 if (*p == NUL)
23828 skip_until = vim_strsave((char_u *)".");
23829 else
23830 skip_until = vim_strsave(p);
23831 }
23832 }
23833
23834 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023835 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023836 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023837 if (line_arg == NULL)
23838 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023840 }
23841
23842 /* Copy the line to newly allocated memory. get_one_sourceline()
23843 * allocates 250 bytes per line, this saves 80% on average. The cost
23844 * is an extra alloc/free. */
23845 p = vim_strsave(theline);
23846 if (p != NULL)
23847 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023848 if (line_arg == NULL)
23849 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023850 theline = p;
23851 }
23852
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023853 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23854
23855 /* Add NULL lines for continuation lines, so that the line count is
23856 * equal to the index in the growarray. */
23857 while (sourcing_lnum_off-- > 0)
23858 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023859
23860 /* Check for end of eap->arg. */
23861 if (line_arg != NULL && *line_arg == NUL)
23862 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863 }
23864
23865 /* Don't define the function when skipping commands or when an error was
23866 * detected. */
23867 if (eap->skip || did_emsg)
23868 goto erret;
23869
23870 /*
23871 * If there are no errors, add the function
23872 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023873 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023874 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023875 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023876 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023877 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023878 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023879 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023880 goto erret;
23881 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023882
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023883 fp = find_func(name);
23884 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023885 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023886 if (!eap->forceit)
23887 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023888 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023889 goto erret;
23890 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023891 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023892 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023893 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023894 name);
23895 goto erret;
23896 }
23897 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023898 ga_clear_strings(&(fp->uf_args));
23899 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023900 vim_free(name);
23901 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 }
23904 else
23905 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023906 char numbuf[20];
23907
23908 fp = NULL;
23909 if (fudi.fd_newkey == NULL && !eap->forceit)
23910 {
23911 EMSG(_(e_funcdict));
23912 goto erret;
23913 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023914 if (fudi.fd_di == NULL)
23915 {
23916 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023917 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023918 goto erret;
23919 }
23920 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023921 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023922 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023923
23924 /* Give the function a sequential number. Can only be used with a
23925 * Funcref! */
23926 vim_free(name);
23927 sprintf(numbuf, "%d", ++func_nr);
23928 name = vim_strsave((char_u *)numbuf);
23929 if (name == NULL)
23930 goto erret;
23931 }
23932
23933 if (fp == NULL)
23934 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023935 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023936 {
23937 int slen, plen;
23938 char_u *scriptname;
23939
23940 /* Check that the autoload name matches the script name. */
23941 j = FAIL;
23942 if (sourcing_name != NULL)
23943 {
23944 scriptname = autoload_name(name);
23945 if (scriptname != NULL)
23946 {
23947 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023948 plen = (int)STRLEN(p);
23949 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023950 if (slen > plen && fnamecmp(p,
23951 sourcing_name + slen - plen) == 0)
23952 j = OK;
23953 vim_free(scriptname);
23954 }
23955 }
23956 if (j == FAIL)
23957 {
23958 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23959 goto erret;
23960 }
23961 }
23962
23963 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964 if (fp == NULL)
23965 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023966
23967 if (fudi.fd_dict != NULL)
23968 {
23969 if (fudi.fd_di == NULL)
23970 {
23971 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023972 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023973 if (fudi.fd_di == NULL)
23974 {
23975 vim_free(fp);
23976 goto erret;
23977 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023978 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23979 {
23980 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023981 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023982 goto erret;
23983 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023984 }
23985 else
23986 /* overwrite existing dict entry */
23987 clear_tv(&fudi.fd_di->di_tv);
23988 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023989 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023990 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023991 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023992
23993 /* behave like "dict" was used */
23994 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023995 }
23996
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023998 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023999 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24000 {
24001 vim_free(fp);
24002 goto erret;
24003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024005 fp->uf_args = newargs;
24006 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024007#ifdef FEAT_PROFILE
24008 fp->uf_tml_count = NULL;
24009 fp->uf_tml_total = NULL;
24010 fp->uf_tml_self = NULL;
24011 fp->uf_profiling = FALSE;
24012 if (prof_def_func())
24013 func_do_profile(fp);
24014#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024015 fp->uf_varargs = varargs;
24016 fp->uf_flags = flags;
24017 fp->uf_calls = 0;
24018 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024019 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024020
24021erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024022 ga_clear_strings(&newargs);
24023 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024024ret_free:
24025 vim_free(skip_until);
24026 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024028 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024029 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030}
24031
24032/*
24033 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024034 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024036 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024037 * TFN_INT: internal function name OK
24038 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024039 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024040 * Advances "pp" to just after the function name (if no error).
24041 */
24042 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024043trans_function_name(
24044 char_u **pp,
24045 int skip, /* only find the end, don't evaluate */
24046 int flags,
24047 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024049 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 char_u *start;
24051 char_u *end;
24052 int lead;
24053 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024055 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024056
24057 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024058 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024060
24061 /* Check for hard coded <SNR>: already translated function ID (from a user
24062 * command). */
24063 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24064 && (*pp)[2] == (int)KE_SNR)
24065 {
24066 *pp += 3;
24067 len = get_id_len(pp) + 3;
24068 return vim_strnsave(start, len);
24069 }
24070
24071 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24072 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024074 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024075 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024076
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024077 /* Note that TFN_ flags use the same values as GLV_ flags. */
24078 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024079 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024080 if (end == start)
24081 {
24082 if (!skip)
24083 EMSG(_("E129: Function name required"));
24084 goto theend;
24085 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024086 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024087 {
24088 /*
24089 * Report an invalid expression in braces, unless the expression
24090 * evaluation has been cancelled due to an aborting error, an
24091 * interrupt, or an exception.
24092 */
24093 if (!aborting())
24094 {
24095 if (end != NULL)
24096 EMSG2(_(e_invarg2), start);
24097 }
24098 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024099 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024100 goto theend;
24101 }
24102
24103 if (lv.ll_tv != NULL)
24104 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024105 if (fdp != NULL)
24106 {
24107 fdp->fd_dict = lv.ll_dict;
24108 fdp->fd_newkey = lv.ll_newkey;
24109 lv.ll_newkey = NULL;
24110 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024111 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024112 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24113 {
24114 name = vim_strsave(lv.ll_tv->vval.v_string);
24115 *pp = end;
24116 }
24117 else
24118 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024119 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24120 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024121 EMSG(_(e_funcref));
24122 else
24123 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024124 name = NULL;
24125 }
24126 goto theend;
24127 }
24128
24129 if (lv.ll_name == NULL)
24130 {
24131 /* Error found, but continue after the function name. */
24132 *pp = end;
24133 goto theend;
24134 }
24135
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024136 /* Check if the name is a Funcref. If so, use the value. */
24137 if (lv.ll_exp_name != NULL)
24138 {
24139 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024140 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024141 if (name == lv.ll_exp_name)
24142 name = NULL;
24143 }
24144 else
24145 {
24146 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024147 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024148 if (name == *pp)
24149 name = NULL;
24150 }
24151 if (name != NULL)
24152 {
24153 name = vim_strsave(name);
24154 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024155 if (STRNCMP(name, "<SNR>", 5) == 0)
24156 {
24157 /* Change "<SNR>" to the byte sequence. */
24158 name[0] = K_SPECIAL;
24159 name[1] = KS_EXTRA;
24160 name[2] = (int)KE_SNR;
24161 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24162 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024163 goto theend;
24164 }
24165
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024166 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024167 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024168 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024169 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24170 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24171 {
24172 /* When there was "s:" already or the name expanded to get a
24173 * leading "s:" then remove it. */
24174 lv.ll_name += 2;
24175 len -= 2;
24176 lead = 2;
24177 }
24178 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024179 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024180 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024181 /* skip over "s:" and "g:" */
24182 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024183 lv.ll_name += 2;
24184 len = (int)(end - lv.ll_name);
24185 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024186
24187 /*
24188 * Copy the function name to allocated memory.
24189 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24190 * Accept <SNR>123_name() outside a script.
24191 */
24192 if (skip)
24193 lead = 0; /* do nothing */
24194 else if (lead > 0)
24195 {
24196 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024197 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24198 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024199 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024200 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024201 if (current_SID <= 0)
24202 {
24203 EMSG(_(e_usingsid));
24204 goto theend;
24205 }
24206 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24207 lead += (int)STRLEN(sid_buf);
24208 }
24209 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024210 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024211 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024212 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024213 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024214 goto theend;
24215 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024216 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024217 {
24218 char_u *cp = vim_strchr(lv.ll_name, ':');
24219
24220 if (cp != NULL && cp < end)
24221 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024222 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024223 goto theend;
24224 }
24225 }
24226
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024227 name = alloc((unsigned)(len + lead + 1));
24228 if (name != NULL)
24229 {
24230 if (lead > 0)
24231 {
24232 name[0] = K_SPECIAL;
24233 name[1] = KS_EXTRA;
24234 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024235 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024236 STRCPY(name + 3, sid_buf);
24237 }
24238 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024239 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024240 }
24241 *pp = end;
24242
24243theend:
24244 clear_lval(&lv);
24245 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246}
24247
24248/*
24249 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24250 * Return 2 if "p" starts with "s:".
24251 * Return 0 otherwise.
24252 */
24253 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024254eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024256 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24257 * the standard library function. */
24258 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24259 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260 return 5;
24261 if (p[0] == 's' && p[1] == ':')
24262 return 2;
24263 return 0;
24264}
24265
24266/*
24267 * Return TRUE if "p" starts with "<SID>" or "s:".
24268 * Only works if eval_fname_script() returned non-zero for "p"!
24269 */
24270 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024271eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024272{
24273 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24274}
24275
24276/*
24277 * List the head of the function: "name(arg1, arg2)".
24278 */
24279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024280list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281{
24282 int j;
24283
24284 msg_start();
24285 if (indent)
24286 MSG_PUTS(" ");
24287 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024288 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289 {
24290 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024291 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024292 }
24293 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024294 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024296 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 {
24298 if (j)
24299 MSG_PUTS(", ");
24300 msg_puts(FUNCARG(fp, j));
24301 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024302 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 {
24304 if (j)
24305 MSG_PUTS(", ");
24306 MSG_PUTS("...");
24307 }
24308 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024309 if (fp->uf_flags & FC_ABORT)
24310 MSG_PUTS(" abort");
24311 if (fp->uf_flags & FC_RANGE)
24312 MSG_PUTS(" range");
24313 if (fp->uf_flags & FC_DICT)
24314 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024315 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024316 if (p_verbose > 0)
24317 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024318}
24319
24320/*
24321 * Find a function by name, return pointer to it in ufuncs.
24322 * Return NULL for unknown function.
24323 */
24324 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024325find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024326{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024327 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024329 hi = hash_find(&func_hashtab, name);
24330 if (!HASHITEM_EMPTY(hi))
24331 return HI2UF(hi);
24332 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024333}
24334
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024335#if defined(EXITFREE) || defined(PROTO)
24336 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024337free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024338{
24339 hashitem_T *hi;
24340
24341 /* Need to start all over every time, because func_free() may change the
24342 * hash table. */
24343 while (func_hashtab.ht_used > 0)
24344 for (hi = func_hashtab.ht_array; ; ++hi)
24345 if (!HASHITEM_EMPTY(hi))
24346 {
24347 func_free(HI2UF(hi));
24348 break;
24349 }
24350}
24351#endif
24352
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024354translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024355{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024356 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024357 return find_internal_func(name) >= 0;
24358 return find_func(name) != NULL;
24359}
24360
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024361/*
24362 * Return TRUE if a function "name" exists.
24363 */
24364 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024365function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024366{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024367 char_u *nm = name;
24368 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024369 int n = FALSE;
24370
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024371 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24372 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024373 nm = skipwhite(nm);
24374
24375 /* Only accept "funcname", "funcname ", "funcname (..." and
24376 * "funcname(...", not "funcname!...". */
24377 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024378 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024379 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024380 return n;
24381}
24382
Bram Moolenaara1544c02013-05-30 12:35:52 +020024383 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024384get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024385{
24386 char_u *nm = name;
24387 char_u *p;
24388
24389 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24390
24391 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024392 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024393 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024394
Bram Moolenaara1544c02013-05-30 12:35:52 +020024395 vim_free(p);
24396 return NULL;
24397}
24398
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024399/*
24400 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024401 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24402 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024403 */
24404 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024405builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024406{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024407 char_u *p;
24408
24409 if (!ASCII_ISLOWER(name[0]))
24410 return FALSE;
24411 p = vim_strchr(name, AUTOLOAD_CHAR);
24412 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024413}
24414
Bram Moolenaar05159a02005-02-26 23:04:13 +000024415#if defined(FEAT_PROFILE) || defined(PROTO)
24416/*
24417 * Start profiling function "fp".
24418 */
24419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024420func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024421{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024422 int len = fp->uf_lines.ga_len;
24423
24424 if (len == 0)
24425 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024426 fp->uf_tm_count = 0;
24427 profile_zero(&fp->uf_tm_self);
24428 profile_zero(&fp->uf_tm_total);
24429 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024430 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024431 if (fp->uf_tml_total == NULL)
24432 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024433 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024434 if (fp->uf_tml_self == NULL)
24435 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024436 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024437 fp->uf_tml_idx = -1;
24438 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24439 || fp->uf_tml_self == NULL)
24440 return; /* out of memory */
24441
24442 fp->uf_profiling = TRUE;
24443}
24444
24445/*
24446 * Dump the profiling results for all functions in file "fd".
24447 */
24448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024449func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024450{
24451 hashitem_T *hi;
24452 int todo;
24453 ufunc_T *fp;
24454 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024455 ufunc_T **sorttab;
24456 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024457
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024458 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024459 if (todo == 0)
24460 return; /* nothing to dump */
24461
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024462 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024463
Bram Moolenaar05159a02005-02-26 23:04:13 +000024464 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24465 {
24466 if (!HASHITEM_EMPTY(hi))
24467 {
24468 --todo;
24469 fp = HI2UF(hi);
24470 if (fp->uf_profiling)
24471 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024472 if (sorttab != NULL)
24473 sorttab[st_len++] = fp;
24474
Bram Moolenaar05159a02005-02-26 23:04:13 +000024475 if (fp->uf_name[0] == K_SPECIAL)
24476 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24477 else
24478 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24479 if (fp->uf_tm_count == 1)
24480 fprintf(fd, "Called 1 time\n");
24481 else
24482 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24483 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24484 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24485 fprintf(fd, "\n");
24486 fprintf(fd, "count total (s) self (s)\n");
24487
24488 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24489 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024490 if (FUNCLINE(fp, i) == NULL)
24491 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024492 prof_func_line(fd, fp->uf_tml_count[i],
24493 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024494 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24495 }
24496 fprintf(fd, "\n");
24497 }
24498 }
24499 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024500
24501 if (sorttab != NULL && st_len > 0)
24502 {
24503 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24504 prof_total_cmp);
24505 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24506 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24507 prof_self_cmp);
24508 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24509 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024510
24511 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024512}
Bram Moolenaar73830342005-02-28 22:48:19 +000024513
24514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024515prof_sort_list(
24516 FILE *fd,
24517 ufunc_T **sorttab,
24518 int st_len,
24519 char *title,
24520 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024521{
24522 int i;
24523 ufunc_T *fp;
24524
24525 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24526 fprintf(fd, "count total (s) self (s) function\n");
24527 for (i = 0; i < 20 && i < st_len; ++i)
24528 {
24529 fp = sorttab[i];
24530 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24531 prefer_self);
24532 if (fp->uf_name[0] == K_SPECIAL)
24533 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24534 else
24535 fprintf(fd, " %s()\n", fp->uf_name);
24536 }
24537 fprintf(fd, "\n");
24538}
24539
24540/*
24541 * Print the count and times for one function or function line.
24542 */
24543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024544prof_func_line(
24545 FILE *fd,
24546 int count,
24547 proftime_T *total,
24548 proftime_T *self,
24549 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024550{
24551 if (count > 0)
24552 {
24553 fprintf(fd, "%5d ", count);
24554 if (prefer_self && profile_equal(total, self))
24555 fprintf(fd, " ");
24556 else
24557 fprintf(fd, "%s ", profile_msg(total));
24558 if (!prefer_self && profile_equal(total, self))
24559 fprintf(fd, " ");
24560 else
24561 fprintf(fd, "%s ", profile_msg(self));
24562 }
24563 else
24564 fprintf(fd, " ");
24565}
24566
24567/*
24568 * Compare function for total time sorting.
24569 */
24570 static int
24571#ifdef __BORLANDC__
24572_RTLENTRYF
24573#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024574prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024575{
24576 ufunc_T *p1, *p2;
24577
24578 p1 = *(ufunc_T **)s1;
24579 p2 = *(ufunc_T **)s2;
24580 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24581}
24582
24583/*
24584 * Compare function for self time sorting.
24585 */
24586 static int
24587#ifdef __BORLANDC__
24588_RTLENTRYF
24589#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024590prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024591{
24592 ufunc_T *p1, *p2;
24593
24594 p1 = *(ufunc_T **)s1;
24595 p2 = *(ufunc_T **)s2;
24596 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24597}
24598
Bram Moolenaar05159a02005-02-26 23:04:13 +000024599#endif
24600
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024601/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024602 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024603 * Return TRUE if a package was loaded.
24604 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024605 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024606script_autoload(
24607 char_u *name,
24608 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024609{
24610 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024611 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024612 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024613 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024614
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024615 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024616 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024617 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024618 return FALSE;
24619
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024620 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024621
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024622 /* Find the name in the list of previously loaded package names. Skip
24623 * "autoload/", it's always the same. */
24624 for (i = 0; i < ga_loaded.ga_len; ++i)
24625 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24626 break;
24627 if (!reload && i < ga_loaded.ga_len)
24628 ret = FALSE; /* was loaded already */
24629 else
24630 {
24631 /* Remember the name if it wasn't loaded already. */
24632 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24633 {
24634 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24635 tofree = NULL;
24636 }
24637
24638 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024639 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024640 ret = TRUE;
24641 }
24642
24643 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024644 return ret;
24645}
24646
24647/*
24648 * Return the autoload script name for a function or variable name.
24649 * Returns NULL when out of memory.
24650 */
24651 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024652autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024653{
24654 char_u *p;
24655 char_u *scriptname;
24656
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024657 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024658 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24659 if (scriptname == NULL)
24660 return FALSE;
24661 STRCPY(scriptname, "autoload/");
24662 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024663 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024664 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024665 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024666 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024667 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024668}
24669
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24671
24672/*
24673 * Function given to ExpandGeneric() to obtain the list of user defined
24674 * function names.
24675 */
24676 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024677get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024678{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024679 static long_u done;
24680 static hashitem_T *hi;
24681 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682
24683 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024684 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024685 done = 0;
24686 hi = func_hashtab.ht_array;
24687 }
24688 if (done < func_hashtab.ht_used)
24689 {
24690 if (done++ > 0)
24691 ++hi;
24692 while (HASHITEM_EMPTY(hi))
24693 ++hi;
24694 fp = HI2UF(hi);
24695
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024696 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024697 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024698
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024699 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24700 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024701
24702 cat_func_name(IObuff, fp);
24703 if (xp->xp_context != EXPAND_USER_FUNC)
24704 {
24705 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024706 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024707 STRCAT(IObuff, ")");
24708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024709 return IObuff;
24710 }
24711 return NULL;
24712}
24713
24714#endif /* FEAT_CMDL_COMPL */
24715
24716/*
24717 * Copy the function name of "fp" to buffer "buf".
24718 * "buf" must be able to hold the function name plus three bytes.
24719 * Takes care of script-local function names.
24720 */
24721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024722cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024724 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024725 {
24726 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024727 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024728 }
24729 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024730 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024731}
24732
24733/*
24734 * ":delfunction {name}"
24735 */
24736 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024737ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024738{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024739 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024740 char_u *p;
24741 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024742 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024743
24744 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024745 name = trans_function_name(&p, eap->skip, 0, &fudi);
24746 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024748 {
24749 if (fudi.fd_dict != NULL && !eap->skip)
24750 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024753 if (!ends_excmd(*skipwhite(p)))
24754 {
24755 vim_free(name);
24756 EMSG(_(e_trailing));
24757 return;
24758 }
24759 eap->nextcmd = check_nextcmd(p);
24760 if (eap->nextcmd != NULL)
24761 *p = NUL;
24762
24763 if (!eap->skip)
24764 fp = find_func(name);
24765 vim_free(name);
24766
24767 if (!eap->skip)
24768 {
24769 if (fp == NULL)
24770 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024771 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024772 return;
24773 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024774 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024775 {
24776 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24777 return;
24778 }
24779
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024780 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024782 /* Delete the dict item that refers to the function, it will
24783 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024784 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024786 else
24787 func_free(fp);
24788 }
24789}
24790
24791/*
24792 * Free a function and remove it from the list of functions.
24793 */
24794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024795func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024796{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024797 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024798
24799 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024800 ga_clear_strings(&(fp->uf_args));
24801 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024802#ifdef FEAT_PROFILE
24803 vim_free(fp->uf_tml_count);
24804 vim_free(fp->uf_tml_total);
24805 vim_free(fp->uf_tml_self);
24806#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024807
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024808 /* remove the function from the function hashtable */
24809 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24810 if (HASHITEM_EMPTY(hi))
24811 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024812 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024813 hash_remove(&func_hashtab, hi);
24814
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024815 vim_free(fp);
24816}
24817
24818/*
24819 * Unreference a Function: decrement the reference count and free it when it
24820 * becomes zero. Only for numbered functions.
24821 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024823func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024824{
24825 ufunc_T *fp;
24826
24827 if (name != NULL && isdigit(*name))
24828 {
24829 fp = find_func(name);
24830 if (fp == NULL)
24831 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024832 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024833 {
24834 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024835 * when "uf_calls" becomes zero. */
24836 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024837 func_free(fp);
24838 }
24839 }
24840}
24841
24842/*
24843 * Count a reference to a Function.
24844 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024845 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024846func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024847{
24848 ufunc_T *fp;
24849
24850 if (name != NULL && isdigit(*name))
24851 {
24852 fp = find_func(name);
24853 if (fp == NULL)
24854 EMSG2(_(e_intern2), "func_ref()");
24855 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024856 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857 }
24858}
24859
24860/*
24861 * Call a user function.
24862 */
24863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024864call_user_func(
24865 ufunc_T *fp, /* pointer to function */
24866 int argcount, /* nr of args */
24867 typval_T *argvars, /* arguments */
24868 typval_T *rettv, /* return value */
24869 linenr_T firstline, /* first line of range */
24870 linenr_T lastline, /* last line of range */
24871 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024872{
Bram Moolenaar33570922005-01-25 22:26:29 +000024873 char_u *save_sourcing_name;
24874 linenr_T save_sourcing_lnum;
24875 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024876 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024877 int save_did_emsg;
24878 static int depth = 0;
24879 dictitem_T *v;
24880 int fixvar_idx = 0; /* index in fixvar[] */
24881 int i;
24882 int ai;
24883 char_u numbuf[NUMBUFLEN];
24884 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024885 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024886#ifdef FEAT_PROFILE
24887 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024888 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024890
24891 /* If depth of calling is getting too high, don't execute the function */
24892 if (depth >= p_mfd)
24893 {
24894 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024895 rettv->v_type = VAR_NUMBER;
24896 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024897 return;
24898 }
24899 ++depth;
24900
24901 line_breakcheck(); /* check for CTRL-C hit */
24902
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024903 fc = (funccall_T *)alloc(sizeof(funccall_T));
24904 fc->caller = current_funccal;
24905 current_funccal = fc;
24906 fc->func = fp;
24907 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024908 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024909 fc->linenr = 0;
24910 fc->returned = FALSE;
24911 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024913 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24914 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024915
Bram Moolenaar33570922005-01-25 22:26:29 +000024916 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024917 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024918 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24919 * each argument variable and saves a lot of time.
24920 */
24921 /*
24922 * Init l: variables.
24923 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024924 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024925 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024926 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024927 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24928 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024929 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024930 name = v->di_key;
24931 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024932 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024933 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024934 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024935 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024936 v->di_tv.vval.v_dict = selfdict;
24937 ++selfdict->dv_refcount;
24938 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024939
Bram Moolenaar33570922005-01-25 22:26:29 +000024940 /*
24941 * Init a: variables.
24942 * Set a:0 to "argcount".
24943 * Set a:000 to a list with room for the "..." arguments.
24944 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024945 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024946 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024947 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024948 /* Use "name" to avoid a warning from some compiler that checks the
24949 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024950 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024951 name = v->di_key;
24952 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024953 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024954 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024955 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024956 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024957 v->di_tv.vval.v_list = &fc->l_varlist;
24958 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24959 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24960 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024961
24962 /*
24963 * Set a:firstline to "firstline" and a:lastline to "lastline".
24964 * Set a:name to named arguments.
24965 * Set a:N to the "..." arguments.
24966 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024967 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024968 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024969 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024970 (varnumber_T)lastline);
24971 for (i = 0; i < argcount; ++i)
24972 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024973 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024974 if (ai < 0)
24975 /* named argument a:name */
24976 name = FUNCARG(fp, i);
24977 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024978 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024979 /* "..." argument a:1, a:2, etc. */
24980 sprintf((char *)numbuf, "%d", ai + 1);
24981 name = numbuf;
24982 }
24983 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24984 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024985 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024986 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24987 }
24988 else
24989 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024990 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24991 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024992 if (v == NULL)
24993 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024994 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024995 }
24996 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024997 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024998
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024999 /* Note: the values are copied directly to avoid alloc/free.
25000 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025001 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025002 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025003
25004 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25005 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025006 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25007 fc->l_listitems[ai].li_tv = argvars[i];
25008 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025009 }
25010 }
25011
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012 /* Don't redraw while executing the function. */
25013 ++RedrawingDisabled;
25014 save_sourcing_name = sourcing_name;
25015 save_sourcing_lnum = sourcing_lnum;
25016 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025017 /* need space for function name + ("function " + 3) or "[number]" */
25018 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25019 + STRLEN(fp->uf_name) + 20;
25020 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025021 if (sourcing_name != NULL)
25022 {
25023 if (save_sourcing_name != NULL
25024 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025025 sprintf((char *)sourcing_name, "%s[%d]..",
25026 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025027 else
25028 STRCPY(sourcing_name, "function ");
25029 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25030
25031 if (p_verbose >= 12)
25032 {
25033 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025034 verbose_enter_scroll();
25035
Bram Moolenaar555b2802005-05-19 21:08:39 +000025036 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025037 if (p_verbose >= 14)
25038 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025040 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025041 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025042 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043
25044 msg_puts((char_u *)"(");
25045 for (i = 0; i < argcount; ++i)
25046 {
25047 if (i > 0)
25048 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025049 if (argvars[i].v_type == VAR_NUMBER)
25050 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025051 else
25052 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025053 /* Do not want errors such as E724 here. */
25054 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025055 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025056 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025057 if (s != NULL)
25058 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025059 if (vim_strsize(s) > MSG_BUF_CLEN)
25060 {
25061 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25062 s = buf;
25063 }
25064 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025065 vim_free(tofree);
25066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025067 }
25068 }
25069 msg_puts((char_u *)")");
25070 }
25071 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025072
25073 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025074 --no_wait_return;
25075 }
25076 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025077#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025078 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025079 {
25080 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25081 func_do_profile(fp);
25082 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025083 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025084 {
25085 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025086 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025087 profile_zero(&fp->uf_tm_children);
25088 }
25089 script_prof_save(&wait_start);
25090 }
25091#endif
25092
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025094 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025095 save_did_emsg = did_emsg;
25096 did_emsg = FALSE;
25097
25098 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025099 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025100 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25101
25102 --RedrawingDisabled;
25103
25104 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025105 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025107 clear_tv(rettv);
25108 rettv->v_type = VAR_NUMBER;
25109 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110 }
25111
Bram Moolenaar05159a02005-02-26 23:04:13 +000025112#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025113 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025114 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025115 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025116 profile_end(&call_start);
25117 profile_sub_wait(&wait_start, &call_start);
25118 profile_add(&fp->uf_tm_total, &call_start);
25119 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025120 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025121 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025122 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25123 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025124 }
25125 }
25126#endif
25127
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128 /* when being verbose, mention the return value */
25129 if (p_verbose >= 12)
25130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025131 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025132 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025133
Bram Moolenaar071d4272004-06-13 20:20:40 +000025134 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025135 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025136 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025137 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025138 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025139 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025140 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025141 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025142 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025143 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025144 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025145
Bram Moolenaar555b2802005-05-19 21:08:39 +000025146 /* The value may be very long. Skip the middle part, so that we
25147 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025148 * truncate it at the end. Don't want errors such as E724 here. */
25149 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025150 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025151 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025152 if (s != NULL)
25153 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025154 if (vim_strsize(s) > MSG_BUF_CLEN)
25155 {
25156 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25157 s = buf;
25158 }
25159 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025160 vim_free(tofree);
25161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025162 }
25163 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025164
25165 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025166 --no_wait_return;
25167 }
25168
25169 vim_free(sourcing_name);
25170 sourcing_name = save_sourcing_name;
25171 sourcing_lnum = save_sourcing_lnum;
25172 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025173#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025174 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025175 script_prof_restore(&wait_start);
25176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025177
25178 if (p_verbose >= 12 && sourcing_name != NULL)
25179 {
25180 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025181 verbose_enter_scroll();
25182
Bram Moolenaar555b2802005-05-19 21:08:39 +000025183 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025184 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025185
25186 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025187 --no_wait_return;
25188 }
25189
25190 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025191 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025192 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025193
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025194 /* If the a:000 list and the l: and a: dicts are not referenced we can
25195 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025196 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25197 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25198 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25199 {
25200 free_funccal(fc, FALSE);
25201 }
25202 else
25203 {
25204 hashitem_T *hi;
25205 listitem_T *li;
25206 int todo;
25207
25208 /* "fc" is still in use. This can happen when returning "a:000" or
25209 * assigning "l:" to a global variable.
25210 * Link "fc" in the list for garbage collection later. */
25211 fc->caller = previous_funccal;
25212 previous_funccal = fc;
25213
25214 /* Make a copy of the a: variables, since we didn't do that above. */
25215 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25216 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25217 {
25218 if (!HASHITEM_EMPTY(hi))
25219 {
25220 --todo;
25221 v = HI2DI(hi);
25222 copy_tv(&v->di_tv, &v->di_tv);
25223 }
25224 }
25225
25226 /* Make a copy of the a:000 items, since we didn't do that above. */
25227 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25228 copy_tv(&li->li_tv, &li->li_tv);
25229 }
25230}
25231
25232/*
25233 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025234 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025235 */
25236 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025237can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025238{
25239 return (fc->l_varlist.lv_copyID != copyID
25240 && fc->l_vars.dv_copyID != copyID
25241 && fc->l_avars.dv_copyID != copyID);
25242}
25243
25244/*
25245 * Free "fc" and what it contains.
25246 */
25247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025248free_funccal(
25249 funccall_T *fc,
25250 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025251{
25252 listitem_T *li;
25253
25254 /* The a: variables typevals may not have been allocated, only free the
25255 * allocated variables. */
25256 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25257
25258 /* free all l: variables */
25259 vars_clear(&fc->l_vars.dv_hashtab);
25260
25261 /* Free the a:000 variables if they were allocated. */
25262 if (free_val)
25263 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25264 clear_tv(&li->li_tv);
25265
25266 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025267}
25268
25269/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025270 * Add a number variable "name" to dict "dp" with value "nr".
25271 */
25272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025273add_nr_var(
25274 dict_T *dp,
25275 dictitem_T *v,
25276 char *name,
25277 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025278{
25279 STRCPY(v->di_key, name);
25280 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25281 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25282 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025283 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025284 v->di_tv.vval.v_number = nr;
25285}
25286
25287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025288 * ":return [expr]"
25289 */
25290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025291ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292{
25293 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025294 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025295 int returning = FALSE;
25296
25297 if (current_funccal == NULL)
25298 {
25299 EMSG(_("E133: :return not inside a function"));
25300 return;
25301 }
25302
25303 if (eap->skip)
25304 ++emsg_skip;
25305
25306 eap->nextcmd = NULL;
25307 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025308 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025309 {
25310 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025311 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025313 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025314 }
25315 /* It's safer to return also on error. */
25316 else if (!eap->skip)
25317 {
25318 /*
25319 * Return unless the expression evaluation has been cancelled due to an
25320 * aborting error, an interrupt, or an exception.
25321 */
25322 if (!aborting())
25323 returning = do_return(eap, FALSE, TRUE, NULL);
25324 }
25325
25326 /* When skipping or the return gets pending, advance to the next command
25327 * in this line (!returning). Otherwise, ignore the rest of the line.
25328 * Following lines will be ignored by get_func_line(). */
25329 if (returning)
25330 eap->nextcmd = NULL;
25331 else if (eap->nextcmd == NULL) /* no argument */
25332 eap->nextcmd = check_nextcmd(arg);
25333
25334 if (eap->skip)
25335 --emsg_skip;
25336}
25337
25338/*
25339 * Return from a function. Possibly makes the return pending. Also called
25340 * for a pending return at the ":endtry" or after returning from an extra
25341 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025342 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025343 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025344 * FALSE when the return gets pending.
25345 */
25346 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025347do_return(
25348 exarg_T *eap,
25349 int reanimate,
25350 int is_cmd,
25351 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025352{
25353 int idx;
25354 struct condstack *cstack = eap->cstack;
25355
25356 if (reanimate)
25357 /* Undo the return. */
25358 current_funccal->returned = FALSE;
25359
25360 /*
25361 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25362 * not in its finally clause (which then is to be executed next) is found.
25363 * In this case, make the ":return" pending for execution at the ":endtry".
25364 * Otherwise, return normally.
25365 */
25366 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25367 if (idx >= 0)
25368 {
25369 cstack->cs_pending[idx] = CSTP_RETURN;
25370
25371 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025372 /* A pending return again gets pending. "rettv" points to an
25373 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025374 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025375 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025376 else
25377 {
25378 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025379 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025381 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025382
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025383 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025384 {
25385 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025386 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025387 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025388 else
25389 EMSG(_(e_outofmem));
25390 }
25391 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025392 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025393
25394 if (reanimate)
25395 {
25396 /* The pending return value could be overwritten by a ":return"
25397 * without argument in a finally clause; reset the default
25398 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025399 current_funccal->rettv->v_type = VAR_NUMBER;
25400 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025401 }
25402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025403 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404 }
25405 else
25406 {
25407 current_funccal->returned = TRUE;
25408
25409 /* If the return is carried out now, store the return value. For
25410 * a return immediately after reanimation, the value is already
25411 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025412 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025413 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025414 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025415 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025417 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025418 }
25419 }
25420
25421 return idx < 0;
25422}
25423
25424/*
25425 * Free the variable with a pending return value.
25426 */
25427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025428discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025429{
Bram Moolenaar33570922005-01-25 22:26:29 +000025430 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025431}
25432
25433/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025434 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435 * is an allocated string. Used by report_pending() for verbose messages.
25436 */
25437 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025438get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025439{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025440 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025441 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025442 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025444 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025445 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025446 if (s == NULL)
25447 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025448
25449 STRCPY(IObuff, ":return ");
25450 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25451 if (STRLEN(s) + 8 >= IOSIZE)
25452 STRCPY(IObuff + IOSIZE - 4, "...");
25453 vim_free(tofree);
25454 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455}
25456
25457/*
25458 * Get next function line.
25459 * Called by do_cmdline() to get the next line.
25460 * Returns allocated string, or NULL for end of function.
25461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025462 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025463get_func_line(
25464 int c UNUSED,
25465 void *cookie,
25466 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467{
Bram Moolenaar33570922005-01-25 22:26:29 +000025468 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025469 ufunc_T *fp = fcp->func;
25470 char_u *retval;
25471 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025472
25473 /* If breakpoints have been added/deleted need to check for it. */
25474 if (fcp->dbg_tick != debug_tick)
25475 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025476 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025477 sourcing_lnum);
25478 fcp->dbg_tick = debug_tick;
25479 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025480#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025481 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025482 func_line_end(cookie);
25483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484
Bram Moolenaar05159a02005-02-26 23:04:13 +000025485 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025486 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25487 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025488 retval = NULL;
25489 else
25490 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025491 /* Skip NULL lines (continuation lines). */
25492 while (fcp->linenr < gap->ga_len
25493 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25494 ++fcp->linenr;
25495 if (fcp->linenr >= gap->ga_len)
25496 retval = NULL;
25497 else
25498 {
25499 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25500 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025501#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025502 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025503 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025504#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025506 }
25507
25508 /* Did we encounter a breakpoint? */
25509 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25510 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025511 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025512 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025513 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025514 sourcing_lnum);
25515 fcp->dbg_tick = debug_tick;
25516 }
25517
25518 return retval;
25519}
25520
Bram Moolenaar05159a02005-02-26 23:04:13 +000025521#if defined(FEAT_PROFILE) || defined(PROTO)
25522/*
25523 * Called when starting to read a function line.
25524 * "sourcing_lnum" must be correct!
25525 * When skipping lines it may not actually be executed, but we won't find out
25526 * until later and we need to store the time now.
25527 */
25528 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025529func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025530{
25531 funccall_T *fcp = (funccall_T *)cookie;
25532 ufunc_T *fp = fcp->func;
25533
25534 if (fp->uf_profiling && sourcing_lnum >= 1
25535 && sourcing_lnum <= fp->uf_lines.ga_len)
25536 {
25537 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025538 /* Skip continuation lines. */
25539 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25540 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025541 fp->uf_tml_execed = FALSE;
25542 profile_start(&fp->uf_tml_start);
25543 profile_zero(&fp->uf_tml_children);
25544 profile_get_wait(&fp->uf_tml_wait);
25545 }
25546}
25547
25548/*
25549 * Called when actually executing a function line.
25550 */
25551 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025552func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025553{
25554 funccall_T *fcp = (funccall_T *)cookie;
25555 ufunc_T *fp = fcp->func;
25556
25557 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25558 fp->uf_tml_execed = TRUE;
25559}
25560
25561/*
25562 * Called when done with a function line.
25563 */
25564 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025565func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025566{
25567 funccall_T *fcp = (funccall_T *)cookie;
25568 ufunc_T *fp = fcp->func;
25569
25570 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25571 {
25572 if (fp->uf_tml_execed)
25573 {
25574 ++fp->uf_tml_count[fp->uf_tml_idx];
25575 profile_end(&fp->uf_tml_start);
25576 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025577 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025578 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25579 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025580 }
25581 fp->uf_tml_idx = -1;
25582 }
25583}
25584#endif
25585
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586/*
25587 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025588 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025589 */
25590 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025591func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025592{
Bram Moolenaar33570922005-01-25 22:26:29 +000025593 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594
25595 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25596 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025597 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598 || fcp->returned);
25599}
25600
25601/*
25602 * return TRUE if cookie indicates a function which "abort"s on errors.
25603 */
25604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025605func_has_abort(
25606 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025608 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025609}
25610
25611#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25612typedef enum
25613{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025614 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25615 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25616 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617} var_flavour_T;
25618
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025619static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025620
25621 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025622var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623{
25624 char_u *p = varname;
25625
25626 if (ASCII_ISUPPER(*p))
25627 {
25628 while (*(++p))
25629 if (ASCII_ISLOWER(*p))
25630 return VAR_FLAVOUR_SESSION;
25631 return VAR_FLAVOUR_VIMINFO;
25632 }
25633 else
25634 return VAR_FLAVOUR_DEFAULT;
25635}
25636#endif
25637
25638#if defined(FEAT_VIMINFO) || defined(PROTO)
25639/*
25640 * Restore global vars that start with a capital from the viminfo file
25641 */
25642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025643read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025644{
25645 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025646 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025647 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025648 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025649
25650 if (!writing && (find_viminfo_parameter('!') != NULL))
25651 {
25652 tab = vim_strchr(virp->vir_line + 1, '\t');
25653 if (tab != NULL)
25654 {
25655 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025656 switch (*tab)
25657 {
25658 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025659#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025660 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025661#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025662 case 'D': type = VAR_DICT; break;
25663 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025664 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666
25667 tab = vim_strchr(tab, '\t');
25668 if (tab != NULL)
25669 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025670 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025671 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025672 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025673 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025674#ifdef FEAT_FLOAT
25675 else if (type == VAR_FLOAT)
25676 (void)string2float(tab + 1, &tv.vval.v_float);
25677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025678 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025679 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025680 if (type == VAR_DICT || type == VAR_LIST)
25681 {
25682 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25683
25684 if (etv == NULL)
25685 /* Failed to parse back the dict or list, use it as a
25686 * string. */
25687 tv.v_type = VAR_STRING;
25688 else
25689 {
25690 vim_free(tv.vval.v_string);
25691 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025692 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025693 }
25694 }
25695
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025696 /* when in a function use global variables */
25697 save_funccal = current_funccal;
25698 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025699 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025700 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025701
25702 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025703 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025704 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25705 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025706 }
25707 }
25708 }
25709
25710 return viminfo_readline(virp);
25711}
25712
25713/*
25714 * Write global vars that start with a capital to the viminfo file
25715 */
25716 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025717write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025718{
Bram Moolenaar33570922005-01-25 22:26:29 +000025719 hashitem_T *hi;
25720 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025721 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025722 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025723 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025724 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025725 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025726
25727 if (find_viminfo_parameter('!') == NULL)
25728 return;
25729
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025730 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025731
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025732 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025733 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025734 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025735 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025736 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025737 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025738 this_var = HI2DI(hi);
25739 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025740 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025741 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025742 {
25743 case VAR_STRING: s = "STR"; break;
25744 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025745 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025746 case VAR_DICT: s = "DIC"; break;
25747 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025748 case VAR_SPECIAL: s = "XPL"; break;
25749
25750 case VAR_UNKNOWN:
25751 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025752 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025753 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025754 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025755 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025756 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025757 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025758 if (p != NULL)
25759 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025760 vim_free(tofree);
25761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025762 }
25763 }
25764}
25765#endif
25766
25767#if defined(FEAT_SESSION) || defined(PROTO)
25768 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025769store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025770{
Bram Moolenaar33570922005-01-25 22:26:29 +000025771 hashitem_T *hi;
25772 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025773 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774 char_u *p, *t;
25775
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025776 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025777 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025779 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025780 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025781 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025782 this_var = HI2DI(hi);
25783 if ((this_var->di_tv.v_type == VAR_NUMBER
25784 || this_var->di_tv.v_type == VAR_STRING)
25785 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025786 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025787 /* Escape special characters with a backslash. Turn a LF and
25788 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025789 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025790 (char_u *)"\\\"\n\r");
25791 if (p == NULL) /* out of memory */
25792 break;
25793 for (t = p; *t != NUL; ++t)
25794 if (*t == '\n')
25795 *t = 'n';
25796 else if (*t == '\r')
25797 *t = 'r';
25798 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025799 this_var->di_key,
25800 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25801 : ' ',
25802 p,
25803 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25804 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025805 || put_eol(fd) == FAIL)
25806 {
25807 vim_free(p);
25808 return FAIL;
25809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025810 vim_free(p);
25811 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025812#ifdef FEAT_FLOAT
25813 else if (this_var->di_tv.v_type == VAR_FLOAT
25814 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25815 {
25816 float_T f = this_var->di_tv.vval.v_float;
25817 int sign = ' ';
25818
25819 if (f < 0)
25820 {
25821 f = -f;
25822 sign = '-';
25823 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025824 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025825 this_var->di_key, sign, f) < 0)
25826 || put_eol(fd) == FAIL)
25827 return FAIL;
25828 }
25829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830 }
25831 }
25832 return OK;
25833}
25834#endif
25835
Bram Moolenaar661b1822005-07-28 22:36:45 +000025836/*
25837 * Display script name where an item was last set.
25838 * Should only be invoked when 'verbose' is non-zero.
25839 */
25840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025841last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025842{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025843 char_u *p;
25844
Bram Moolenaar661b1822005-07-28 22:36:45 +000025845 if (scriptID != 0)
25846 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025847 p = home_replace_save(NULL, get_scriptname(scriptID));
25848 if (p != NULL)
25849 {
25850 verbose_enter();
25851 MSG_PUTS(_("\n\tLast set from "));
25852 MSG_PUTS(p);
25853 vim_free(p);
25854 verbose_leave();
25855 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025856 }
25857}
25858
Bram Moolenaard812df62008-11-09 12:46:09 +000025859/*
25860 * List v:oldfiles in a nice way.
25861 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025862 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025863ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025864{
25865 list_T *l = vimvars[VV_OLDFILES].vv_list;
25866 listitem_T *li;
25867 int nr = 0;
25868
25869 if (l == NULL)
25870 msg((char_u *)_("No old files"));
25871 else
25872 {
25873 msg_start();
25874 msg_scroll = TRUE;
25875 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25876 {
25877 msg_outnum((long)++nr);
25878 MSG_PUTS(": ");
25879 msg_outtrans(get_tv_string(&li->li_tv));
25880 msg_putchar('\n');
25881 out_flush(); /* output one line at a time */
25882 ui_breakcheck();
25883 }
25884 /* Assume "got_int" was set to truncate the listing. */
25885 got_int = FALSE;
25886
25887#ifdef FEAT_BROWSE_CMD
25888 if (cmdmod.browse)
25889 {
25890 quit_more = FALSE;
25891 nr = prompt_for_number(FALSE);
25892 msg_starthere();
25893 if (nr > 0)
25894 {
25895 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25896 (long)nr);
25897
25898 if (p != NULL)
25899 {
25900 p = expand_env_save(p);
25901 eap->arg = p;
25902 eap->cmdidx = CMD_edit;
25903 cmdmod.browse = FALSE;
25904 do_exedit(eap, NULL);
25905 vim_free(p);
25906 }
25907 }
25908 }
25909#endif
25910 }
25911}
25912
Bram Moolenaar53744302015-07-17 17:38:22 +020025913/* reset v:option_new, v:option_old and v:option_type */
25914 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025915reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025916{
25917 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25918 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25919 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25920}
25921
25922
Bram Moolenaar071d4272004-06-13 20:20:40 +000025923#endif /* FEAT_EVAL */
25924
Bram Moolenaar071d4272004-06-13 20:20:40 +000025925
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025926#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025927
25928#ifdef WIN3264
25929/*
25930 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25931 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025932static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25933static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25934static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025935
25936/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025937 * Get the short path (8.3) for the filename in "fnamep".
25938 * Only works for a valid file name.
25939 * When the path gets longer "fnamep" is changed and the allocated buffer
25940 * is put in "bufp".
25941 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25942 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025943 */
25944 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025945get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025946{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025947 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025948 char_u *newbuf;
25949
25950 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025951 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025952 if (l > len - 1)
25953 {
25954 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025955 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025956 newbuf = vim_strnsave(*fnamep, l);
25957 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025958 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959
25960 vim_free(*bufp);
25961 *fnamep = *bufp = newbuf;
25962
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025963 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025964 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025965 }
25966
25967 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025968 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025969}
25970
25971/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025972 * Get the short path (8.3) for the filename in "fname". The converted
25973 * path is returned in "bufp".
25974 *
25975 * Some of the directories specified in "fname" may not exist. This function
25976 * will shorten the existing directories at the beginning of the path and then
25977 * append the remaining non-existing path.
25978 *
25979 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025980 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025981 * bufp - Pointer to an allocated buffer for the filename.
25982 * fnamelen - Length of the filename pointed to by fname
25983 *
25984 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025985 */
25986 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025987shortpath_for_invalid_fname(
25988 char_u **fname,
25989 char_u **bufp,
25990 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025991{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025992 char_u *short_fname, *save_fname, *pbuf_unused;
25993 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025994 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025995 int old_len, len;
25996 int new_len, sfx_len;
25997 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025998
25999 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026000 old_len = *fnamelen;
26001 save_fname = vim_strnsave(*fname, old_len);
26002 pbuf_unused = NULL;
26003 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026004
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026005 endp = save_fname + old_len - 1; /* Find the end of the copy */
26006 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026007
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026008 /*
26009 * Try shortening the supplied path till it succeeds by removing one
26010 * directory at a time from the tail of the path.
26011 */
26012 len = 0;
26013 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026014 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026015 /* go back one path-separator */
26016 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26017 --endp;
26018 if (endp <= save_fname)
26019 break; /* processed the complete path */
26020
26021 /*
26022 * Replace the path separator with a NUL and try to shorten the
26023 * resulting path.
26024 */
26025 ch = *endp;
26026 *endp = 0;
26027 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026028 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026029 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26030 {
26031 retval = FAIL;
26032 goto theend;
26033 }
26034 *endp = ch; /* preserve the string */
26035
26036 if (len > 0)
26037 break; /* successfully shortened the path */
26038
26039 /* failed to shorten the path. Skip the path separator */
26040 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026041 }
26042
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026043 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026044 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026045 /*
26046 * Succeeded in shortening the path. Now concatenate the shortened
26047 * path with the remaining path at the tail.
26048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026049
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026050 /* Compute the length of the new path. */
26051 sfx_len = (int)(save_endp - endp) + 1;
26052 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026053
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026054 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026055 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026056 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026057 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026058 /* There is not enough space in the currently allocated string,
26059 * copy it to a buffer big enough. */
26060 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026061 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026062 {
26063 retval = FAIL;
26064 goto theend;
26065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026066 }
26067 else
26068 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026069 /* Transfer short_fname to the main buffer (it's big enough),
26070 * unless get_short_pathname() did its work in-place. */
26071 *fname = *bufp = save_fname;
26072 if (short_fname != save_fname)
26073 vim_strncpy(save_fname, short_fname, len);
26074 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026075 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026076
26077 /* concat the not-shortened part of the path */
26078 vim_strncpy(*fname + len, endp, sfx_len);
26079 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026080 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026081
26082theend:
26083 vim_free(pbuf_unused);
26084 vim_free(save_fname);
26085
26086 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026087}
26088
26089/*
26090 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026091 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026092 */
26093 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026094shortpath_for_partial(
26095 char_u **fnamep,
26096 char_u **bufp,
26097 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026098{
26099 int sepcount, len, tflen;
26100 char_u *p;
26101 char_u *pbuf, *tfname;
26102 int hasTilde;
26103
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026104 /* Count up the path separators from the RHS.. so we know which part
26105 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026106 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026107 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026108 if (vim_ispathsep(*p))
26109 ++sepcount;
26110
26111 /* Need full path first (use expand_env() to remove a "~/") */
26112 hasTilde = (**fnamep == '~');
26113 if (hasTilde)
26114 pbuf = tfname = expand_env_save(*fnamep);
26115 else
26116 pbuf = tfname = FullName_save(*fnamep, FALSE);
26117
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026118 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026119
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026120 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26121 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026122
26123 if (len == 0)
26124 {
26125 /* Don't have a valid filename, so shorten the rest of the
26126 * path if we can. This CAN give us invalid 8.3 filenames, but
26127 * there's not a lot of point in guessing what it might be.
26128 */
26129 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026130 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26131 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026132 }
26133
26134 /* Count the paths backward to find the beginning of the desired string. */
26135 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026136 {
26137#ifdef FEAT_MBYTE
26138 if (has_mbyte)
26139 p -= mb_head_off(tfname, p);
26140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026141 if (vim_ispathsep(*p))
26142 {
26143 if (sepcount == 0 || (hasTilde && sepcount == 1))
26144 break;
26145 else
26146 sepcount --;
26147 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026149 if (hasTilde)
26150 {
26151 --p;
26152 if (p >= tfname)
26153 *p = '~';
26154 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026155 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026156 }
26157 else
26158 ++p;
26159
26160 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26161 vim_free(*bufp);
26162 *fnamelen = (int)STRLEN(p);
26163 *bufp = pbuf;
26164 *fnamep = p;
26165
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026166 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026167}
26168#endif /* WIN3264 */
26169
26170/*
26171 * Adjust a filename, according to a string of modifiers.
26172 * *fnamep must be NUL terminated when called. When returning, the length is
26173 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026174 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026175 * When there is an error, *fnamep is set to NULL.
26176 */
26177 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026178modify_fname(
26179 char_u *src, /* string with modifiers */
26180 int *usedlen, /* characters after src that are used */
26181 char_u **fnamep, /* file name so far */
26182 char_u **bufp, /* buffer for allocated file name or NULL */
26183 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026184{
26185 int valid = 0;
26186 char_u *tail;
26187 char_u *s, *p, *pbuf;
26188 char_u dirname[MAXPATHL];
26189 int c;
26190 int has_fullname = 0;
26191#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026192 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026193 int has_shortname = 0;
26194#endif
26195
26196repeat:
26197 /* ":p" - full path/file_name */
26198 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26199 {
26200 has_fullname = 1;
26201
26202 valid |= VALID_PATH;
26203 *usedlen += 2;
26204
26205 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26206 if ((*fnamep)[0] == '~'
26207#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26208 && ((*fnamep)[1] == '/'
26209# ifdef BACKSLASH_IN_FILENAME
26210 || (*fnamep)[1] == '\\'
26211# endif
26212 || (*fnamep)[1] == NUL)
26213
26214#endif
26215 )
26216 {
26217 *fnamep = expand_env_save(*fnamep);
26218 vim_free(*bufp); /* free any allocated file name */
26219 *bufp = *fnamep;
26220 if (*fnamep == NULL)
26221 return -1;
26222 }
26223
26224 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026225 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026226 {
26227 if (vim_ispathsep(*p)
26228 && p[1] == '.'
26229 && (p[2] == NUL
26230 || vim_ispathsep(p[2])
26231 || (p[2] == '.'
26232 && (p[3] == NUL || vim_ispathsep(p[3])))))
26233 break;
26234 }
26235
26236 /* FullName_save() is slow, don't use it when not needed. */
26237 if (*p != NUL || !vim_isAbsName(*fnamep))
26238 {
26239 *fnamep = FullName_save(*fnamep, *p != NUL);
26240 vim_free(*bufp); /* free any allocated file name */
26241 *bufp = *fnamep;
26242 if (*fnamep == NULL)
26243 return -1;
26244 }
26245
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026246#ifdef WIN3264
26247# if _WIN32_WINNT >= 0x0500
26248 if (vim_strchr(*fnamep, '~') != NULL)
26249 {
26250 /* Expand 8.3 filename to full path. Needed to make sure the same
26251 * file does not have two different names.
26252 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26253 p = alloc(_MAX_PATH + 1);
26254 if (p != NULL)
26255 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026256 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026257 {
26258 vim_free(*bufp);
26259 *bufp = *fnamep = p;
26260 }
26261 else
26262 vim_free(p);
26263 }
26264 }
26265# endif
26266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026267 /* Append a path separator to a directory. */
26268 if (mch_isdir(*fnamep))
26269 {
26270 /* Make room for one or two extra characters. */
26271 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26272 vim_free(*bufp); /* free any allocated file name */
26273 *bufp = *fnamep;
26274 if (*fnamep == NULL)
26275 return -1;
26276 add_pathsep(*fnamep);
26277 }
26278 }
26279
26280 /* ":." - path relative to the current directory */
26281 /* ":~" - path relative to the home directory */
26282 /* ":8" - shortname path - postponed till after */
26283 while (src[*usedlen] == ':'
26284 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26285 {
26286 *usedlen += 2;
26287 if (c == '8')
26288 {
26289#ifdef WIN3264
26290 has_shortname = 1; /* Postpone this. */
26291#endif
26292 continue;
26293 }
26294 pbuf = NULL;
26295 /* Need full path first (use expand_env() to remove a "~/") */
26296 if (!has_fullname)
26297 {
26298 if (c == '.' && **fnamep == '~')
26299 p = pbuf = expand_env_save(*fnamep);
26300 else
26301 p = pbuf = FullName_save(*fnamep, FALSE);
26302 }
26303 else
26304 p = *fnamep;
26305
26306 has_fullname = 0;
26307
26308 if (p != NULL)
26309 {
26310 if (c == '.')
26311 {
26312 mch_dirname(dirname, MAXPATHL);
26313 s = shorten_fname(p, dirname);
26314 if (s != NULL)
26315 {
26316 *fnamep = s;
26317 if (pbuf != NULL)
26318 {
26319 vim_free(*bufp); /* free any allocated file name */
26320 *bufp = pbuf;
26321 pbuf = NULL;
26322 }
26323 }
26324 }
26325 else
26326 {
26327 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26328 /* Only replace it when it starts with '~' */
26329 if (*dirname == '~')
26330 {
26331 s = vim_strsave(dirname);
26332 if (s != NULL)
26333 {
26334 *fnamep = s;
26335 vim_free(*bufp);
26336 *bufp = s;
26337 }
26338 }
26339 }
26340 vim_free(pbuf);
26341 }
26342 }
26343
26344 tail = gettail(*fnamep);
26345 *fnamelen = (int)STRLEN(*fnamep);
26346
26347 /* ":h" - head, remove "/file_name", can be repeated */
26348 /* Don't remove the first "/" or "c:\" */
26349 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26350 {
26351 valid |= VALID_HEAD;
26352 *usedlen += 2;
26353 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026354 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026355 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026356 *fnamelen = (int)(tail - *fnamep);
26357#ifdef VMS
26358 if (*fnamelen > 0)
26359 *fnamelen += 1; /* the path separator is part of the path */
26360#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026361 if (*fnamelen == 0)
26362 {
26363 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26364 p = vim_strsave((char_u *)".");
26365 if (p == NULL)
26366 return -1;
26367 vim_free(*bufp);
26368 *bufp = *fnamep = tail = p;
26369 *fnamelen = 1;
26370 }
26371 else
26372 {
26373 while (tail > s && !after_pathsep(s, tail))
26374 mb_ptr_back(*fnamep, tail);
26375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026376 }
26377
26378 /* ":8" - shortname */
26379 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26380 {
26381 *usedlen += 2;
26382#ifdef WIN3264
26383 has_shortname = 1;
26384#endif
26385 }
26386
26387#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026388 /*
26389 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026390 */
26391 if (has_shortname)
26392 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026393 /* Copy the string if it is shortened by :h and when it wasn't copied
26394 * yet, because we are going to change it in place. Avoids changing
26395 * the buffer name for "%:8". */
26396 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026397 {
26398 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026399 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026400 return -1;
26401 vim_free(*bufp);
26402 *bufp = *fnamep = p;
26403 }
26404
26405 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026406 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026407 if (!has_fullname && !vim_isAbsName(*fnamep))
26408 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026409 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026410 return -1;
26411 }
26412 else
26413 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026414 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026415
Bram Moolenaardc935552011-08-17 15:23:23 +020026416 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026417 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026418 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026419 return -1;
26420
26421 if (l == 0)
26422 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026423 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026424 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026425 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026426 return -1;
26427 }
26428 *fnamelen = l;
26429 }
26430 }
26431#endif /* WIN3264 */
26432
26433 /* ":t" - tail, just the basename */
26434 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26435 {
26436 *usedlen += 2;
26437 *fnamelen -= (int)(tail - *fnamep);
26438 *fnamep = tail;
26439 }
26440
26441 /* ":e" - extension, can be repeated */
26442 /* ":r" - root, without extension, can be repeated */
26443 while (src[*usedlen] == ':'
26444 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26445 {
26446 /* find a '.' in the tail:
26447 * - for second :e: before the current fname
26448 * - otherwise: The last '.'
26449 */
26450 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26451 s = *fnamep - 2;
26452 else
26453 s = *fnamep + *fnamelen - 1;
26454 for ( ; s > tail; --s)
26455 if (s[0] == '.')
26456 break;
26457 if (src[*usedlen + 1] == 'e') /* :e */
26458 {
26459 if (s > tail)
26460 {
26461 *fnamelen += (int)(*fnamep - (s + 1));
26462 *fnamep = s + 1;
26463#ifdef VMS
26464 /* cut version from the extension */
26465 s = *fnamep + *fnamelen - 1;
26466 for ( ; s > *fnamep; --s)
26467 if (s[0] == ';')
26468 break;
26469 if (s > *fnamep)
26470 *fnamelen = s - *fnamep;
26471#endif
26472 }
26473 else if (*fnamep <= tail)
26474 *fnamelen = 0;
26475 }
26476 else /* :r */
26477 {
26478 if (s > tail) /* remove one extension */
26479 *fnamelen = (int)(s - *fnamep);
26480 }
26481 *usedlen += 2;
26482 }
26483
26484 /* ":s?pat?foo?" - substitute */
26485 /* ":gs?pat?foo?" - global substitute */
26486 if (src[*usedlen] == ':'
26487 && (src[*usedlen + 1] == 's'
26488 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26489 {
26490 char_u *str;
26491 char_u *pat;
26492 char_u *sub;
26493 int sep;
26494 char_u *flags;
26495 int didit = FALSE;
26496
26497 flags = (char_u *)"";
26498 s = src + *usedlen + 2;
26499 if (src[*usedlen + 1] == 'g')
26500 {
26501 flags = (char_u *)"g";
26502 ++s;
26503 }
26504
26505 sep = *s++;
26506 if (sep)
26507 {
26508 /* find end of pattern */
26509 p = vim_strchr(s, sep);
26510 if (p != NULL)
26511 {
26512 pat = vim_strnsave(s, (int)(p - s));
26513 if (pat != NULL)
26514 {
26515 s = p + 1;
26516 /* find end of substitution */
26517 p = vim_strchr(s, sep);
26518 if (p != NULL)
26519 {
26520 sub = vim_strnsave(s, (int)(p - s));
26521 str = vim_strnsave(*fnamep, *fnamelen);
26522 if (sub != NULL && str != NULL)
26523 {
26524 *usedlen = (int)(p + 1 - src);
26525 s = do_string_sub(str, pat, sub, flags);
26526 if (s != NULL)
26527 {
26528 *fnamep = s;
26529 *fnamelen = (int)STRLEN(s);
26530 vim_free(*bufp);
26531 *bufp = s;
26532 didit = TRUE;
26533 }
26534 }
26535 vim_free(sub);
26536 vim_free(str);
26537 }
26538 vim_free(pat);
26539 }
26540 }
26541 /* after using ":s", repeat all the modifiers */
26542 if (didit)
26543 goto repeat;
26544 }
26545 }
26546
Bram Moolenaar26df0922014-02-23 23:39:13 +010026547 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26548 {
26549 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26550 if (p == NULL)
26551 return -1;
26552 vim_free(*bufp);
26553 *bufp = *fnamep = p;
26554 *fnamelen = (int)STRLEN(p);
26555 *usedlen += 2;
26556 }
26557
Bram Moolenaar071d4272004-06-13 20:20:40 +000026558 return valid;
26559}
26560
26561/*
26562 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26563 * "flags" can be "g" to do a global substitute.
26564 * Returns an allocated string, NULL for error.
26565 */
26566 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026567do_string_sub(
26568 char_u *str,
26569 char_u *pat,
26570 char_u *sub,
26571 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026572{
26573 int sublen;
26574 regmatch_T regmatch;
26575 int i;
26576 int do_all;
26577 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026578 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026579 garray_T ga;
26580 char_u *ret;
26581 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026582 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026583
26584 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26585 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026586 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026587
26588 ga_init2(&ga, 1, 200);
26589
26590 do_all = (flags[0] == 'g');
26591
26592 regmatch.rm_ic = p_ic;
26593 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26594 if (regmatch.regprog != NULL)
26595 {
26596 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026597 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026598 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26599 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026600 /* Skip empty match except for first match. */
26601 if (regmatch.startp[0] == regmatch.endp[0])
26602 {
26603 if (zero_width == regmatch.startp[0])
26604 {
26605 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026606 i = MB_PTR2LEN(tail);
26607 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26608 (size_t)i);
26609 ga.ga_len += i;
26610 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026611 continue;
26612 }
26613 zero_width = regmatch.startp[0];
26614 }
26615
Bram Moolenaar071d4272004-06-13 20:20:40 +000026616 /*
26617 * Get some space for a temporary buffer to do the substitution
26618 * into. It will contain:
26619 * - The text up to where the match is.
26620 * - The substituted text.
26621 * - The text after the match.
26622 */
26623 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026624 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026625 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26626 {
26627 ga_clear(&ga);
26628 break;
26629 }
26630
26631 /* copy the text up to where the match is */
26632 i = (int)(regmatch.startp[0] - tail);
26633 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26634 /* add the substituted text */
26635 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26636 + ga.ga_len + i, TRUE, TRUE, FALSE);
26637 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026638 tail = regmatch.endp[0];
26639 if (*tail == NUL)
26640 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026641 if (!do_all)
26642 break;
26643 }
26644
26645 if (ga.ga_data != NULL)
26646 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26647
Bram Moolenaar473de612013-06-08 18:19:48 +020026648 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026649 }
26650
26651 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26652 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026653 if (p_cpo == empty_option)
26654 p_cpo = save_cpo;
26655 else
26656 /* Darn, evaluating {sub} expression changed the value. */
26657 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026658
26659 return ret;
26660}
26661
26662#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */