blob: 1471568184cb3f5e5901da257d5c680796c480b9 [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 Moolenaar98921892016-02-23 17:14:37 +010013674# ifdef USE_GTK3
13675 "gui_gtk3",
13676# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013680#ifdef FEAT_GUI_GNOME
13681 "gui_gnome",
13682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683#ifdef FEAT_GUI_MAC
13684 "gui_mac",
13685#endif
13686#ifdef FEAT_GUI_MOTIF
13687 "gui_motif",
13688#endif
13689#ifdef FEAT_GUI_PHOTON
13690 "gui_photon",
13691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692#ifdef FEAT_GUI_W32
13693 "gui_win32",
13694#endif
13695#ifdef FEAT_HANGULIN
13696 "hangul_input",
13697#endif
13698#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13699 "iconv",
13700#endif
13701#ifdef FEAT_INS_EXPAND
13702 "insert_expand",
13703#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013704#ifdef FEAT_JOB
13705 "job",
13706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707#ifdef FEAT_JUMPLIST
13708 "jumplist",
13709#endif
13710#ifdef FEAT_KEYMAP
13711 "keymap",
13712#endif
13713#ifdef FEAT_LANGMAP
13714 "langmap",
13715#endif
13716#ifdef FEAT_LIBCALL
13717 "libcall",
13718#endif
13719#ifdef FEAT_LINEBREAK
13720 "linebreak",
13721#endif
13722#ifdef FEAT_LISP
13723 "lispindent",
13724#endif
13725#ifdef FEAT_LISTCMDS
13726 "listcmds",
13727#endif
13728#ifdef FEAT_LOCALMAP
13729 "localmap",
13730#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013731#ifdef FEAT_LUA
13732# ifndef DYNAMIC_LUA
13733 "lua",
13734# endif
13735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736#ifdef FEAT_MENU
13737 "menu",
13738#endif
13739#ifdef FEAT_SESSION
13740 "mksession",
13741#endif
13742#ifdef FEAT_MODIFY_FNAME
13743 "modify_fname",
13744#endif
13745#ifdef FEAT_MOUSE
13746 "mouse",
13747#endif
13748#ifdef FEAT_MOUSESHAPE
13749 "mouseshape",
13750#endif
13751#if defined(UNIX) || defined(VMS)
13752# ifdef FEAT_MOUSE_DEC
13753 "mouse_dec",
13754# endif
13755# ifdef FEAT_MOUSE_GPM
13756 "mouse_gpm",
13757# endif
13758# ifdef FEAT_MOUSE_JSB
13759 "mouse_jsbterm",
13760# endif
13761# ifdef FEAT_MOUSE_NET
13762 "mouse_netterm",
13763# endif
13764# ifdef FEAT_MOUSE_PTERM
13765 "mouse_pterm",
13766# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013767# ifdef FEAT_MOUSE_SGR
13768 "mouse_sgr",
13769# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013770# ifdef FEAT_SYSMOUSE
13771 "mouse_sysmouse",
13772# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013773# ifdef FEAT_MOUSE_URXVT
13774 "mouse_urxvt",
13775# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776# ifdef FEAT_MOUSE_XTERM
13777 "mouse_xterm",
13778# endif
13779#endif
13780#ifdef FEAT_MBYTE
13781 "multi_byte",
13782#endif
13783#ifdef FEAT_MBYTE_IME
13784 "multi_byte_ime",
13785#endif
13786#ifdef FEAT_MULTI_LANG
13787 "multi_lang",
13788#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013789#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013790#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013791 "mzscheme",
13792#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794#ifdef FEAT_OLE
13795 "ole",
13796#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013797 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798#ifdef FEAT_PATH_EXTRA
13799 "path_extra",
13800#endif
13801#ifdef FEAT_PERL
13802#ifndef DYNAMIC_PERL
13803 "perl",
13804#endif
13805#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013806#ifdef FEAT_PERSISTENT_UNDO
13807 "persistent_undo",
13808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809#ifdef FEAT_PYTHON
13810#ifndef DYNAMIC_PYTHON
13811 "python",
13812#endif
13813#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013814#ifdef FEAT_PYTHON3
13815#ifndef DYNAMIC_PYTHON3
13816 "python3",
13817#endif
13818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819#ifdef FEAT_POSTSCRIPT
13820 "postscript",
13821#endif
13822#ifdef FEAT_PRINTER
13823 "printer",
13824#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013825#ifdef FEAT_PROFILE
13826 "profile",
13827#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013828#ifdef FEAT_RELTIME
13829 "reltime",
13830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831#ifdef FEAT_QUICKFIX
13832 "quickfix",
13833#endif
13834#ifdef FEAT_RIGHTLEFT
13835 "rightleft",
13836#endif
13837#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13838 "ruby",
13839#endif
13840#ifdef FEAT_SCROLLBIND
13841 "scrollbind",
13842#endif
13843#ifdef FEAT_CMDL_INFO
13844 "showcmd",
13845 "cmdline_info",
13846#endif
13847#ifdef FEAT_SIGNS
13848 "signs",
13849#endif
13850#ifdef FEAT_SMARTINDENT
13851 "smartindent",
13852#endif
13853#ifdef FEAT_SNIFF
13854 "sniff",
13855#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013856#ifdef STARTUPTIME
13857 "startuptime",
13858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859#ifdef FEAT_STL_OPT
13860 "statusline",
13861#endif
13862#ifdef FEAT_SUN_WORKSHOP
13863 "sun_workshop",
13864#endif
13865#ifdef FEAT_NETBEANS_INTG
13866 "netbeans_intg",
13867#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013868#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013869 "spell",
13870#endif
13871#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 "syntax",
13873#endif
13874#if defined(USE_SYSTEM) || !defined(UNIX)
13875 "system",
13876#endif
13877#ifdef FEAT_TAG_BINS
13878 "tag_binary",
13879#endif
13880#ifdef FEAT_TAG_OLDSTATIC
13881 "tag_old_static",
13882#endif
13883#ifdef FEAT_TAG_ANYWHITE
13884 "tag_any_white",
13885#endif
13886#ifdef FEAT_TCL
13887# ifndef DYNAMIC_TCL
13888 "tcl",
13889# endif
13890#endif
13891#ifdef TERMINFO
13892 "terminfo",
13893#endif
13894#ifdef FEAT_TERMRESPONSE
13895 "termresponse",
13896#endif
13897#ifdef FEAT_TEXTOBJ
13898 "textobjects",
13899#endif
13900#ifdef HAVE_TGETENT
13901 "tgetent",
13902#endif
13903#ifdef FEAT_TITLE
13904 "title",
13905#endif
13906#ifdef FEAT_TOOLBAR
13907 "toolbar",
13908#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013909#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13910 "unnamedplus",
13911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912#ifdef FEAT_USR_CMDS
13913 "user-commands", /* was accidentally included in 5.4 */
13914 "user_commands",
13915#endif
13916#ifdef FEAT_VIMINFO
13917 "viminfo",
13918#endif
13919#ifdef FEAT_VERTSPLIT
13920 "vertsplit",
13921#endif
13922#ifdef FEAT_VIRTUALEDIT
13923 "virtualedit",
13924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926#ifdef FEAT_VISUALEXTRA
13927 "visualextra",
13928#endif
13929#ifdef FEAT_VREPLACE
13930 "vreplace",
13931#endif
13932#ifdef FEAT_WILDIGN
13933 "wildignore",
13934#endif
13935#ifdef FEAT_WILDMENU
13936 "wildmenu",
13937#endif
13938#ifdef FEAT_WINDOWS
13939 "windows",
13940#endif
13941#ifdef FEAT_WAK
13942 "winaltkeys",
13943#endif
13944#ifdef FEAT_WRITEBACKUP
13945 "writebackup",
13946#endif
13947#ifdef FEAT_XIM
13948 "xim",
13949#endif
13950#ifdef FEAT_XFONTSET
13951 "xfontset",
13952#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013953#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013954 "xpm",
13955 "xpm_w32", /* for backward compatibility */
13956#else
13957# if defined(HAVE_XPM)
13958 "xpm",
13959# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961#ifdef USE_XSMP
13962 "xsmp",
13963#endif
13964#ifdef USE_XSMP_INTERACT
13965 "xsmp_interact",
13966#endif
13967#ifdef FEAT_XCLIPBOARD
13968 "xterm_clipboard",
13969#endif
13970#ifdef FEAT_XTERM_SAVE
13971 "xterm_save",
13972#endif
13973#if defined(UNIX) && defined(FEAT_X11)
13974 "X11",
13975#endif
13976 NULL
13977 };
13978
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013979 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 for (i = 0; has_list[i] != NULL; ++i)
13981 if (STRICMP(name, has_list[i]) == 0)
13982 {
13983 n = TRUE;
13984 break;
13985 }
13986
13987 if (n == FALSE)
13988 {
13989 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013990 {
13991 if (name[5] == '-'
13992 && STRLEN(name) > 11
13993 && vim_isdigit(name[6])
13994 && vim_isdigit(name[8])
13995 && vim_isdigit(name[10]))
13996 {
13997 int major = atoi((char *)name + 6);
13998 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013999
14000 /* Expect "patch-9.9.01234". */
14001 n = (major < VIM_VERSION_MAJOR
14002 || (major == VIM_VERSION_MAJOR
14003 && (minor < VIM_VERSION_MINOR
14004 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014005 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014006 }
14007 else
14008 n = has_patch(atoi((char *)name + 5));
14009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 else if (STRICMP(name, "vim_starting") == 0)
14011 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014012#ifdef FEAT_MBYTE
14013 else if (STRICMP(name, "multi_byte_encoding") == 0)
14014 n = has_mbyte;
14015#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014016#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14017 else if (STRICMP(name, "balloon_multiline") == 0)
14018 n = multiline_balloon_available();
14019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020#ifdef DYNAMIC_TCL
14021 else if (STRICMP(name, "tcl") == 0)
14022 n = tcl_enabled(FALSE);
14023#endif
14024#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14025 else if (STRICMP(name, "iconv") == 0)
14026 n = iconv_enabled(FALSE);
14027#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014028#ifdef DYNAMIC_LUA
14029 else if (STRICMP(name, "lua") == 0)
14030 n = lua_enabled(FALSE);
14031#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014032#ifdef DYNAMIC_MZSCHEME
14033 else if (STRICMP(name, "mzscheme") == 0)
14034 n = mzscheme_enabled(FALSE);
14035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036#ifdef DYNAMIC_RUBY
14037 else if (STRICMP(name, "ruby") == 0)
14038 n = ruby_enabled(FALSE);
14039#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014040#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041#ifdef DYNAMIC_PYTHON
14042 else if (STRICMP(name, "python") == 0)
14043 n = python_enabled(FALSE);
14044#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014045#endif
14046#ifdef FEAT_PYTHON3
14047#ifdef DYNAMIC_PYTHON3
14048 else if (STRICMP(name, "python3") == 0)
14049 n = python3_enabled(FALSE);
14050#endif
14051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052#ifdef DYNAMIC_PERL
14053 else if (STRICMP(name, "perl") == 0)
14054 n = perl_enabled(FALSE);
14055#endif
14056#ifdef FEAT_GUI
14057 else if (STRICMP(name, "gui_running") == 0)
14058 n = (gui.in_use || gui.starting);
14059# ifdef FEAT_GUI_W32
14060 else if (STRICMP(name, "gui_win32s") == 0)
14061 n = gui_is_win32s();
14062# endif
14063# ifdef FEAT_BROWSE
14064 else if (STRICMP(name, "browse") == 0)
14065 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14066# endif
14067#endif
14068#ifdef FEAT_SYN_HL
14069 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014070 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071#endif
14072#if defined(WIN3264)
14073 else if (STRICMP(name, "win95") == 0)
14074 n = mch_windows95();
14075#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014076#ifdef FEAT_NETBEANS_INTG
14077 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014078 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 }
14081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083}
14084
14085/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014086 * "has_key()" function
14087 */
14088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014089f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014090{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014091 if (argvars[0].v_type != VAR_DICT)
14092 {
14093 EMSG(_(e_dictreq));
14094 return;
14095 }
14096 if (argvars[0].vval.v_dict == NULL)
14097 return;
14098
14099 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014100 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014101}
14102
14103/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014104 * "haslocaldir()" function
14105 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014107f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014108{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014109 win_T *wp = NULL;
14110
14111 wp = find_tabwin(&argvars[0], &argvars[1]);
14112 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014113}
14114
14115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 * "hasmapto()" function
14117 */
14118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014119f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120{
14121 char_u *name;
14122 char_u *mode;
14123 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014124 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014126 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014127 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 mode = (char_u *)"nvo";
14129 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014131 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014132 if (argvars[2].v_type != VAR_UNKNOWN)
14133 abbr = get_tv_number(&argvars[2]);
14134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135
Bram Moolenaar2c932302006-03-18 21:42:09 +000014136 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014137 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014139 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140}
14141
14142/*
14143 * "histadd()" function
14144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014146f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147{
14148#ifdef FEAT_CMDHIST
14149 int histype;
14150 char_u *str;
14151 char_u buf[NUMBUFLEN];
14152#endif
14153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014154 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 if (check_restricted() || check_secure())
14156 return;
14157#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14159 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160 if (histype >= 0)
14161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014162 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 if (*str != NUL)
14164 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014165 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014167 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168 return;
14169 }
14170 }
14171#endif
14172}
14173
14174/*
14175 * "histdel()" function
14176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014178f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179{
14180#ifdef FEAT_CMDHIST
14181 int n;
14182 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014183 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014185 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14186 if (str == NULL)
14187 n = 0;
14188 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014190 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014191 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014194 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195 else
14196 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014198 get_tv_string_buf(&argvars[1], buf));
14199 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200#endif
14201}
14202
14203/*
14204 * "histget()" function
14205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014207f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208{
14209#ifdef FEAT_CMDHIST
14210 int type;
14211 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014214 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14215 if (str == NULL)
14216 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014218 {
14219 type = get_histtype(str);
14220 if (argvars[1].v_type == VAR_UNKNOWN)
14221 idx = get_history_idx(type);
14222 else
14223 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14224 /* -1 on type error */
14225 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014228 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014230 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231}
14232
14233/*
14234 * "histnr()" function
14235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014237f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238{
14239 int i;
14240
14241#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014242 char_u *history = get_tv_string_chk(&argvars[0]);
14243
14244 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245 if (i >= HIST_CMD && i < HIST_COUNT)
14246 i = get_history_idx(i);
14247 else
14248#endif
14249 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014250 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251}
14252
14253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254 * "highlightID(name)" function
14255 */
14256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014257f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014259 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260}
14261
14262/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014263 * "highlight_exists()" function
14264 */
14265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014266f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014267{
14268 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14269}
14270
14271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272 * "hostname()" function
14273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014275f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276{
14277 char_u hostname[256];
14278
14279 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014280 rettv->v_type = VAR_STRING;
14281 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282}
14283
14284/*
14285 * iconv() function
14286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014288f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289{
14290#ifdef FEAT_MBYTE
14291 char_u buf1[NUMBUFLEN];
14292 char_u buf2[NUMBUFLEN];
14293 char_u *from, *to, *str;
14294 vimconv_T vimconv;
14295#endif
14296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014297 rettv->v_type = VAR_STRING;
14298 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299
14300#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014301 str = get_tv_string(&argvars[0]);
14302 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14303 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 vimconv.vc_type = CONV_NONE;
14305 convert_setup(&vimconv, from, to);
14306
14307 /* If the encodings are equal, no conversion needed. */
14308 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014311 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312
14313 convert_setup(&vimconv, NULL, NULL);
14314 vim_free(from);
14315 vim_free(to);
14316#endif
14317}
14318
14319/*
14320 * "indent()" function
14321 */
14322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014323f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324{
14325 linenr_T lnum;
14326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014329 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014331 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332}
14333
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014334/*
14335 * "index()" function
14336 */
14337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014338f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014339{
Bram Moolenaar33570922005-01-25 22:26:29 +000014340 list_T *l;
14341 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014342 long idx = 0;
14343 int ic = FALSE;
14344
14345 rettv->vval.v_number = -1;
14346 if (argvars[0].v_type != VAR_LIST)
14347 {
14348 EMSG(_(e_listreq));
14349 return;
14350 }
14351 l = argvars[0].vval.v_list;
14352 if (l != NULL)
14353 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014354 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014355 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357 int error = FALSE;
14358
Bram Moolenaar758711c2005-02-02 23:11:38 +000014359 /* Start at specified item. Use the cached index that list_find()
14360 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014361 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014362 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014363 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014364 ic = get_tv_number_chk(&argvars[3], &error);
14365 if (error)
14366 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014367 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014368
Bram Moolenaar758711c2005-02-02 23:11:38 +000014369 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014370 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014371 {
14372 rettv->vval.v_number = idx;
14373 break;
14374 }
14375 }
14376}
14377
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378static int inputsecret_flag = 0;
14379
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014380static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014381
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014383 * This function is used by f_input() and f_inputdialog() functions. The third
14384 * argument to f_input() specifies the type of completion to use at the
14385 * prompt. The third argument to f_inputdialog() specifies the value to return
14386 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 */
14388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014389get_user_input(
14390 typval_T *argvars,
14391 typval_T *rettv,
14392 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014394 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 char_u *p = NULL;
14396 int c;
14397 char_u buf[NUMBUFLEN];
14398 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014399 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014400 int xp_type = EXPAND_NOTHING;
14401 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014403 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014404 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405
14406#ifdef NO_CONSOLE_INPUT
14407 /* While starting up, there is no place to enter text. */
14408 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410#endif
14411
14412 cmd_silent = FALSE; /* Want to see the prompt. */
14413 if (prompt != NULL)
14414 {
14415 /* Only the part of the message after the last NL is considered as
14416 * prompt for the command line */
14417 p = vim_strrchr(prompt, '\n');
14418 if (p == NULL)
14419 p = prompt;
14420 else
14421 {
14422 ++p;
14423 c = *p;
14424 *p = NUL;
14425 msg_start();
14426 msg_clr_eos();
14427 msg_puts_attr(prompt, echo_attr);
14428 msg_didout = FALSE;
14429 msg_starthere();
14430 *p = c;
14431 }
14432 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014434 if (argvars[1].v_type != VAR_UNKNOWN)
14435 {
14436 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14437 if (defstr != NULL)
14438 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014440 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014441 {
14442 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014443 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014444 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014445
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014446 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014447 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014448
Bram Moolenaar4463f292005-09-25 22:20:24 +000014449 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14450 if (xp_name == NULL)
14451 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014452
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014453 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014454
Bram Moolenaar4463f292005-09-25 22:20:24 +000014455 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14456 &xp_arg) == FAIL)
14457 return;
14458 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014459 }
14460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014461 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014462 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014463 int save_ex_normal_busy = ex_normal_busy;
14464 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014465 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014466 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14467 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014468 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014469 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014470 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014471 && argvars[1].v_type != VAR_UNKNOWN
14472 && argvars[2].v_type != VAR_UNKNOWN)
14473 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14474 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014475
14476 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014478 /* since the user typed this, no need to wait for return */
14479 need_wait_return = FALSE;
14480 msg_didout = FALSE;
14481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 cmd_silent = cmd_silent_save;
14483}
14484
14485/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014486 * "input()" function
14487 * Also handles inputsecret() when inputsecret is set.
14488 */
14489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014490f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014491{
14492 get_user_input(argvars, rettv, FALSE);
14493}
14494
14495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496 * "inputdialog()" function
14497 */
14498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014499f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500{
14501#if defined(FEAT_GUI_TEXTDIALOG)
14502 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14503 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14504 {
14505 char_u *message;
14506 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014507 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014509 message = get_tv_string_chk(&argvars[0]);
14510 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014511 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014512 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 else
14514 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014515 if (message != NULL && defstr != NULL
14516 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014517 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519 else
14520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014521 if (message != NULL && defstr != NULL
14522 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014523 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014524 rettv->vval.v_string = vim_strsave(
14525 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014527 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014529 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530 }
14531 else
14532#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014533 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534}
14535
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014536/*
14537 * "inputlist()" function
14538 */
14539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014540f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014541{
14542 listitem_T *li;
14543 int selected;
14544 int mouse_used;
14545
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014546#ifdef NO_CONSOLE_INPUT
14547 /* While starting up, there is no place to enter text. */
14548 if (no_console_input())
14549 return;
14550#endif
14551 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14552 {
14553 EMSG2(_(e_listarg), "inputlist()");
14554 return;
14555 }
14556
14557 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014558 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014559 lines_left = Rows; /* avoid more prompt */
14560 msg_scroll = TRUE;
14561 msg_clr_eos();
14562
14563 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14564 {
14565 msg_puts(get_tv_string(&li->li_tv));
14566 msg_putchar('\n');
14567 }
14568
14569 /* Ask for choice. */
14570 selected = prompt_for_number(&mouse_used);
14571 if (mouse_used)
14572 selected -= lines_left;
14573
14574 rettv->vval.v_number = selected;
14575}
14576
14577
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14579
14580/*
14581 * "inputrestore()" function
14582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014584f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585{
14586 if (ga_userinput.ga_len > 0)
14587 {
14588 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14590 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014591 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592 }
14593 else if (p_verbose > 1)
14594 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014595 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014596 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597 }
14598}
14599
14600/*
14601 * "inputsave()" function
14602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014604f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014606 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607 if (ga_grow(&ga_userinput, 1) == OK)
14608 {
14609 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14610 + ga_userinput.ga_len);
14611 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014612 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 }
14614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014615 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616}
14617
14618/*
14619 * "inputsecret()" function
14620 */
14621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014622f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623{
14624 ++cmdline_star;
14625 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014626 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 --cmdline_star;
14628 --inputsecret_flag;
14629}
14630
14631/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014632 * "insert()" function
14633 */
14634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014635f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014636{
14637 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014638 listitem_T *item;
14639 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014640 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014641
14642 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014643 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014644 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014645 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014646 {
14647 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014648 before = get_tv_number_chk(&argvars[2], &error);
14649 if (error)
14650 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014651
Bram Moolenaar758711c2005-02-02 23:11:38 +000014652 if (before == l->lv_len)
14653 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014654 else
14655 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014656 item = list_find(l, before);
14657 if (item == NULL)
14658 {
14659 EMSGN(_(e_listidx), before);
14660 l = NULL;
14661 }
14662 }
14663 if (l != NULL)
14664 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014665 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014666 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014667 }
14668 }
14669}
14670
14671/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014672 * "invert(expr)" function
14673 */
14674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014675f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014676{
14677 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14678}
14679
14680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681 * "isdirectory()" function
14682 */
14683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014684f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014686 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687}
14688
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014689/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014690 * "islocked()" function
14691 */
14692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014693f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014694{
14695 lval_T lv;
14696 char_u *end;
14697 dictitem_T *di;
14698
14699 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014700 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14701 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014702 if (end != NULL && lv.ll_name != NULL)
14703 {
14704 if (*end != NUL)
14705 EMSG(_(e_trailing));
14706 else
14707 {
14708 if (lv.ll_tv == NULL)
14709 {
14710 if (check_changedtick(lv.ll_name))
14711 rettv->vval.v_number = 1; /* always locked */
14712 else
14713 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014714 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014715 if (di != NULL)
14716 {
14717 /* Consider a variable locked when:
14718 * 1. the variable itself is locked
14719 * 2. the value of the variable is locked.
14720 * 3. the List or Dict value is locked.
14721 */
14722 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14723 || tv_islocked(&di->di_tv));
14724 }
14725 }
14726 }
14727 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014728 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014729 else if (lv.ll_newkey != NULL)
14730 EMSG2(_(e_dictkey), lv.ll_newkey);
14731 else if (lv.ll_list != NULL)
14732 /* List item. */
14733 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14734 else
14735 /* Dictionary item. */
14736 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14737 }
14738 }
14739
14740 clear_lval(&lv);
14741}
14742
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014743static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014744
14745/*
14746 * Turn a dict into a list:
14747 * "what" == 0: list of keys
14748 * "what" == 1: list of values
14749 * "what" == 2: list of items
14750 */
14751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014752dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014753{
Bram Moolenaar33570922005-01-25 22:26:29 +000014754 list_T *l2;
14755 dictitem_T *di;
14756 hashitem_T *hi;
14757 listitem_T *li;
14758 listitem_T *li2;
14759 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014760 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014761
Bram Moolenaar8c711452005-01-14 21:53:12 +000014762 if (argvars[0].v_type != VAR_DICT)
14763 {
14764 EMSG(_(e_dictreq));
14765 return;
14766 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014767 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014768 return;
14769
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014770 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014771 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014772
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014773 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014774 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014775 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014776 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014777 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014778 --todo;
14779 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014780
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014781 li = listitem_alloc();
14782 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014783 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014784 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014785
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014786 if (what == 0)
14787 {
14788 /* keys() */
14789 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014790 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014791 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14792 }
14793 else if (what == 1)
14794 {
14795 /* values() */
14796 copy_tv(&di->di_tv, &li->li_tv);
14797 }
14798 else
14799 {
14800 /* items() */
14801 l2 = list_alloc();
14802 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014803 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014804 li->li_tv.vval.v_list = l2;
14805 if (l2 == NULL)
14806 break;
14807 ++l2->lv_refcount;
14808
14809 li2 = listitem_alloc();
14810 if (li2 == NULL)
14811 break;
14812 list_append(l2, li2);
14813 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014814 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014815 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14816
14817 li2 = listitem_alloc();
14818 if (li2 == NULL)
14819 break;
14820 list_append(l2, li2);
14821 copy_tv(&di->di_tv, &li2->li_tv);
14822 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014823 }
14824 }
14825}
14826
14827/*
14828 * "items(dict)" function
14829 */
14830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014831f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014832{
14833 dict_list(argvars, rettv, 2);
14834}
14835
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014836#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014837/*
14838 * Get the job from the argument.
14839 * Returns NULL if the job is invalid.
14840 */
14841 static job_T *
14842get_job_arg(typval_T *tv)
14843{
14844 job_T *job;
14845
14846 if (tv->v_type != VAR_JOB)
14847 {
14848 EMSG2(_(e_invarg2), get_tv_string(tv));
14849 return NULL;
14850 }
14851 job = tv->vval.v_job;
14852
14853 if (job == NULL)
14854 EMSG(_("E916: not a valid job"));
14855 return job;
14856}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014857
14858# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014859/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014860 * "job_getchannel()" function
14861 */
14862 static void
14863f_job_getchannel(typval_T *argvars, typval_T *rettv)
14864{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014865 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014866
Bram Moolenaar65edff82016-02-21 16:40:11 +010014867 if (job != NULL)
14868 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014869 rettv->v_type = VAR_CHANNEL;
14870 rettv->vval.v_channel = job->jv_channel;
14871 if (job->jv_channel != NULL)
14872 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014873 }
14874}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014875# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014876
14877/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014878 * "job_setoptions()" function
14879 */
14880 static void
14881f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14882{
14883 job_T *job = get_job_arg(&argvars[0]);
14884 jobopt_T opt;
14885
14886 if (job == NULL)
14887 return;
14888 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014889 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014890 return;
14891 job_set_options(job, &opt);
14892}
14893
14894/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014895 * "job_start()" function
14896 */
14897 static void
14898f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14899{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014900 job_T *job;
14901 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014902#if defined(UNIX)
14903# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014904 char **argv = NULL;
14905 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014906#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014907 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014908#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014909 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014910
14911 rettv->v_type = VAR_JOB;
14912 job = job_alloc();
14913 rettv->vval.v_job = job;
14914 if (job == NULL)
14915 return;
14916
14917 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014918
14919 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010014920 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014921 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010014922 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014923 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
14924 + JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014925 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010014926 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014927
Bram Moolenaar835dc632016-02-07 14:27:38 +010014928#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014929 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014930#endif
14931
14932 if (argvars[0].v_type == VAR_STRING)
14933 {
14934 /* Command is a string. */
14935 cmd = argvars[0].vval.v_string;
14936#ifdef USE_ARGV
14937 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14938 return;
14939 argv[argc] = NULL;
14940#endif
14941 }
14942 else if (argvars[0].v_type != VAR_LIST
14943 || argvars[0].vval.v_list == NULL
14944 || argvars[0].vval.v_list->lv_len < 1)
14945 {
14946 EMSG(_(e_invarg));
14947 return;
14948 }
14949 else
14950 {
14951 list_T *l = argvars[0].vval.v_list;
14952 listitem_T *li;
14953 char_u *s;
14954
14955#ifdef USE_ARGV
14956 /* Pass argv[] to mch_call_shell(). */
14957 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14958 if (argv == NULL)
14959 return;
14960#endif
14961 for (li = l->lv_first; li != NULL; li = li->li_next)
14962 {
14963 s = get_tv_string_chk(&li->li_tv);
14964 if (s == NULL)
14965 goto theend;
14966#ifdef USE_ARGV
14967 argv[argc++] = (char *)s;
14968#else
14969 if (li != l->lv_first)
14970 {
14971 s = vim_strsave_shellescape(s, FALSE, TRUE);
14972 if (s == NULL)
14973 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014974 ga_concat(&ga, s);
14975 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014976 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014977 else
14978 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014979 if (li->li_next != NULL)
14980 ga_append(&ga, ' ');
14981#endif
14982 }
14983#ifdef USE_ARGV
14984 argv[argc] = NULL;
14985#else
14986 cmd = ga.ga_data;
14987#endif
14988 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014989
Bram Moolenaar835dc632016-02-07 14:27:38 +010014990#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014991# ifdef FEAT_CHANNEL
14992 if (ch_log_active())
14993 {
14994 garray_T ga;
14995 int i;
14996
14997 ga_init2(&ga, (int)sizeof(char), 200);
14998 for (i = 0; i < argc; ++i)
14999 {
15000 if (i > 0)
15001 ga_concat(&ga, (char_u *)" ");
15002 ga_concat(&ga, (char_u *)argv[i]);
15003 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015004 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015005 ga_clear(&ga);
15006 }
15007# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015008 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015009#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015010# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015011 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015012# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015013 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015014#endif
15015
15016theend:
15017#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015018 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015019#else
15020 vim_free(ga.ga_data);
15021#endif
15022}
15023
15024/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015025 * Get the status of "job" and invoke the exit callback when needed.
15026 * The returned string is not allocated.
15027 */
15028 static char *
15029job_status(job_T *job)
15030{
15031 char *result;
15032
15033 if (job->jv_status == JOB_ENDED)
15034 /* No need to check, dead is dead. */
15035 result = "dead";
15036 else if (job->jv_status == JOB_FAILED)
15037 result = "fail";
15038 else
15039 {
15040 result = mch_job_status(job);
15041# ifdef FEAT_CHANNEL
15042 if (job->jv_status == JOB_ENDED)
15043 ch_log(job->jv_channel, "Job ended");
15044# endif
15045 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15046 {
15047 typval_T argv[3];
15048 typval_T rettv;
15049 int dummy;
15050
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015051 /* invoke the exit callback; make sure the refcount is > 0 */
15052 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015053 argv[0].v_type = VAR_JOB;
15054 argv[0].vval.v_job = job;
15055 argv[1].v_type = VAR_NUMBER;
15056 argv[1].vval.v_number = job->jv_exitval;
15057 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15058 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15059 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015060 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015061 }
15062 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15063 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015064 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015065 * freed. Careful: caller must not use "job" after this! */
15066 job_free(job);
15067 }
15068 }
15069 return result;
15070}
15071
15072/*
15073 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15074 */
15075 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015076job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015077{
15078 static time_t last_check = 0;
15079 time_t now;
15080 job_T *job;
15081 job_T *next;
15082
15083 /* Only do this once in 10 seconds. */
15084 now = time(NULL);
15085 if (last_check + 10 < now)
15086 {
15087 last_check = now;
15088 for (job = first_job; job != NULL; job = next)
15089 {
15090 next = job->jv_next;
15091 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15092 job_status(job); /* may free "job" */
15093 }
15094 }
15095}
15096
15097/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015098 * "job_status()" function
15099 */
15100 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015101f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015102{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015103 job_T *job = get_job_arg(&argvars[0]);
15104 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015105
Bram Moolenaar65edff82016-02-21 16:40:11 +010015106 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015107 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015108 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015109 rettv->v_type = VAR_STRING;
15110 rettv->vval.v_string = vim_strsave((char_u *)result);
15111 }
15112}
15113
15114/*
15115 * "job_stop()" function
15116 */
15117 static void
15118f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15119{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015120 job_T *job = get_job_arg(&argvars[0]);
15121
15122 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015123 {
15124 char_u *arg;
15125
15126 if (argvars[1].v_type == VAR_UNKNOWN)
15127 arg = (char_u *)"";
15128 else
15129 {
15130 arg = get_tv_string_chk(&argvars[1]);
15131 if (arg == NULL)
15132 {
15133 EMSG(_(e_invarg));
15134 return;
15135 }
15136 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010015137 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015138 rettv->vval.v_number = 0;
15139 else
15140 rettv->vval.v_number = 1;
15141 }
15142}
15143#endif
15144
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015146 * "join()" function
15147 */
15148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015149f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015150{
15151 garray_T ga;
15152 char_u *sep;
15153
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015154 if (argvars[0].v_type != VAR_LIST)
15155 {
15156 EMSG(_(e_listreq));
15157 return;
15158 }
15159 if (argvars[0].vval.v_list == NULL)
15160 return;
15161 if (argvars[1].v_type == VAR_UNKNOWN)
15162 sep = (char_u *)" ";
15163 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015164 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015165
15166 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015167
15168 if (sep != NULL)
15169 {
15170 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015171 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015172 ga_append(&ga, NUL);
15173 rettv->vval.v_string = (char_u *)ga.ga_data;
15174 }
15175 else
15176 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015177}
15178
15179/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015180 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015181 */
15182 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015183f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015184{
15185 js_read_T reader;
15186
15187 reader.js_buf = get_tv_string(&argvars[0]);
15188 reader.js_fill = NULL;
15189 reader.js_used = 0;
15190 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15191 EMSG(_(e_invarg));
15192}
15193
15194/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015195 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015196 */
15197 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015198f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015199{
15200 rettv->v_type = VAR_STRING;
15201 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15202}
15203
15204/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015205 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015206 */
15207 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015208f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015209{
15210 js_read_T reader;
15211
15212 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015213 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015214 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015215 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015216 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015217}
15218
15219/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015220 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015221 */
15222 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015223f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015224{
15225 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015226 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015227}
15228
15229/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015230 * "keys()" function
15231 */
15232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015233f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015234{
15235 dict_list(argvars, rettv, 0);
15236}
15237
15238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015239 * "last_buffer_nr()" function.
15240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015242f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015243{
15244 int n = 0;
15245 buf_T *buf;
15246
15247 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15248 if (n < buf->b_fnum)
15249 n = buf->b_fnum;
15250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015251 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015252}
15253
15254/*
15255 * "len()" function
15256 */
15257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015258f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015259{
15260 switch (argvars[0].v_type)
15261 {
15262 case VAR_STRING:
15263 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015264 rettv->vval.v_number = (varnumber_T)STRLEN(
15265 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015266 break;
15267 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015268 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015269 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015270 case VAR_DICT:
15271 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15272 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015273 case VAR_UNKNOWN:
15274 case VAR_SPECIAL:
15275 case VAR_FLOAT:
15276 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015277 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015278 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015279 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015280 break;
15281 }
15282}
15283
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015284static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015285
15286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015287libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015288{
15289#ifdef FEAT_LIBCALL
15290 char_u *string_in;
15291 char_u **string_result;
15292 int nr_result;
15293#endif
15294
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015295 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015296 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015297 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015298
15299 if (check_restricted() || check_secure())
15300 return;
15301
15302#ifdef FEAT_LIBCALL
15303 /* The first two args must be strings, otherwise its meaningless */
15304 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15305 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015306 string_in = NULL;
15307 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015308 string_in = argvars[2].vval.v_string;
15309 if (type == VAR_NUMBER)
15310 string_result = NULL;
15311 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015312 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015313 if (mch_libcall(argvars[0].vval.v_string,
15314 argvars[1].vval.v_string,
15315 string_in,
15316 argvars[2].vval.v_number,
15317 string_result,
15318 &nr_result) == OK
15319 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015320 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015321 }
15322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323}
15324
15325/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015326 * "libcall()" function
15327 */
15328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015329f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015330{
15331 libcall_common(argvars, rettv, VAR_STRING);
15332}
15333
15334/*
15335 * "libcallnr()" function
15336 */
15337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015338f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339{
15340 libcall_common(argvars, rettv, VAR_NUMBER);
15341}
15342
15343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 * "line(string)" function
15345 */
15346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015347f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348{
15349 linenr_T lnum = 0;
15350 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015351 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015353 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354 if (fp != NULL)
15355 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015356 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357}
15358
15359/*
15360 * "line2byte(lnum)" function
15361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015363f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364{
15365#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367#else
15368 linenr_T lnum;
15369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015370 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015372 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015374 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15375 if (rettv->vval.v_number >= 0)
15376 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377#endif
15378}
15379
15380/*
15381 * "lispindent(lnum)" function
15382 */
15383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015384f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385{
15386#ifdef FEAT_LISP
15387 pos_T pos;
15388 linenr_T lnum;
15389
15390 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015391 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15393 {
15394 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015395 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 curwin->w_cursor = pos;
15397 }
15398 else
15399#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401}
15402
15403/*
15404 * "localtime()" function
15405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015407f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410}
15411
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015412static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413
15414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015415get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416{
15417 char_u *keys;
15418 char_u *which;
15419 char_u buf[NUMBUFLEN];
15420 char_u *keys_buf = NULL;
15421 char_u *rhs;
15422 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015423 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015424 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015425 mapblock_T *mp;
15426 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427
15428 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015429 rettv->v_type = VAR_STRING;
15430 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015432 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015433 if (*keys == NUL)
15434 return;
15435
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015436 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015438 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015439 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015440 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015441 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015442 if (argvars[3].v_type != VAR_UNKNOWN)
15443 get_dict = get_tv_number(&argvars[3]);
15444 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446 else
15447 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015448 if (which == NULL)
15449 return;
15450
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 mode = get_map_mode(&which, 0);
15452
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015453 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015454 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015456
15457 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015459 /* Return a string. */
15460 if (rhs != NULL)
15461 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462
Bram Moolenaarbd743252010-10-20 21:23:33 +020015463 }
15464 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15465 {
15466 /* Return a dictionary. */
15467 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15468 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15469 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470
Bram Moolenaarbd743252010-10-20 21:23:33 +020015471 dict_add_nr_str(dict, "lhs", 0L, lhs);
15472 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15473 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15474 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15475 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15476 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15477 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015478 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015479 dict_add_nr_str(dict, "mode", 0L, mapmode);
15480
15481 vim_free(lhs);
15482 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483 }
15484}
15485
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015486#ifdef FEAT_FLOAT
15487/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015488 * "log()" function
15489 */
15490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015491f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015492{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015493 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015494
15495 rettv->v_type = VAR_FLOAT;
15496 if (get_float_arg(argvars, &f) == OK)
15497 rettv->vval.v_float = log(f);
15498 else
15499 rettv->vval.v_float = 0.0;
15500}
15501
15502/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015503 * "log10()" function
15504 */
15505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015506f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015507{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015508 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015509
15510 rettv->v_type = VAR_FLOAT;
15511 if (get_float_arg(argvars, &f) == OK)
15512 rettv->vval.v_float = log10(f);
15513 else
15514 rettv->vval.v_float = 0.0;
15515}
15516#endif
15517
Bram Moolenaar1dced572012-04-05 16:54:08 +020015518#ifdef FEAT_LUA
15519/*
15520 * "luaeval()" function
15521 */
15522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015523f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015524{
15525 char_u *str;
15526 char_u buf[NUMBUFLEN];
15527
15528 str = get_tv_string_buf(&argvars[0], buf);
15529 do_luaeval(str, argvars + 1, rettv);
15530}
15531#endif
15532
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015534 * "map()" function
15535 */
15536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015537f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015538{
15539 filter_map(argvars, rettv, TRUE);
15540}
15541
15542/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015543 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 */
15545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015546f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015548 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549}
15550
15551/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015552 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 */
15554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015555f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015557 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558}
15559
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015560static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561
15562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015563find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015565 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015566 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015567 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 char_u *pat;
15569 regmatch_T regmatch;
15570 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015571 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572 char_u *save_cpo;
15573 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015574 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015575 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015576 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015577 list_T *l = NULL;
15578 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015579 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015580 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581
15582 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15583 save_cpo = p_cpo;
15584 p_cpo = (char_u *)"";
15585
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015586 rettv->vval.v_number = -1;
15587 if (type == 3)
15588 {
15589 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015590 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015591 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015592 }
15593 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015595 rettv->v_type = VAR_STRING;
15596 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015599 if (argvars[0].v_type == VAR_LIST)
15600 {
15601 if ((l = argvars[0].vval.v_list) == NULL)
15602 goto theend;
15603 li = l->lv_first;
15604 }
15605 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015606 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015607 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015608 len = (long)STRLEN(str);
15609 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015611 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15612 if (pat == NULL)
15613 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015614
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015615 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015617 int error = FALSE;
15618
15619 start = get_tv_number_chk(&argvars[2], &error);
15620 if (error)
15621 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015622 if (l != NULL)
15623 {
15624 li = list_find(l, start);
15625 if (li == NULL)
15626 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015627 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015628 }
15629 else
15630 {
15631 if (start < 0)
15632 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015633 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015634 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015635 /* When "count" argument is there ignore matches before "start",
15636 * otherwise skip part of the string. Differs when pattern is "^"
15637 * or "\<". */
15638 if (argvars[3].v_type != VAR_UNKNOWN)
15639 startcol = start;
15640 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015641 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015642 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015643 len -= start;
15644 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015645 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015646
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015647 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015648 nth = get_tv_number_chk(&argvars[3], &error);
15649 if (error)
15650 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651 }
15652
15653 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15654 if (regmatch.regprog != NULL)
15655 {
15656 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015657
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015658 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015659 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015660 if (l != NULL)
15661 {
15662 if (li == NULL)
15663 {
15664 match = FALSE;
15665 break;
15666 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015667 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015668 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015669 if (str == NULL)
15670 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015671 }
15672
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015673 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015674
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015675 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015676 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015677 if (l == NULL && !match)
15678 break;
15679
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015680 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015681 if (l != NULL)
15682 {
15683 li = li->li_next;
15684 ++idx;
15685 }
15686 else
15687 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015688#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015689 startcol = (colnr_T)(regmatch.startp[0]
15690 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015691#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015692 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015693#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015694 if (startcol > (colnr_T)len
15695 || str + startcol <= regmatch.startp[0])
15696 {
15697 match = FALSE;
15698 break;
15699 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015700 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015701 }
15702
15703 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015705 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015706 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015707 int i;
15708
15709 /* return list with matched string and submatches */
15710 for (i = 0; i < NSUBEXP; ++i)
15711 {
15712 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015713 {
15714 if (list_append_string(rettv->vval.v_list,
15715 (char_u *)"", 0) == FAIL)
15716 break;
15717 }
15718 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015719 regmatch.startp[i],
15720 (int)(regmatch.endp[i] - regmatch.startp[i]))
15721 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015722 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015723 }
15724 }
15725 else if (type == 2)
15726 {
15727 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015728 if (l != NULL)
15729 copy_tv(&li->li_tv, rettv);
15730 else
15731 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015733 }
15734 else if (l != NULL)
15735 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 else
15737 {
15738 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015739 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740 (varnumber_T)(regmatch.startp[0] - str);
15741 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015742 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015744 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 }
15746 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015747 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 }
15749
15750theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015751 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 p_cpo = save_cpo;
15753}
15754
15755/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015756 * "match()" function
15757 */
15758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015759f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015760{
15761 find_some_match(argvars, rettv, 1);
15762}
15763
15764/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015765 * "matchadd()" function
15766 */
15767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015768f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015769{
15770#ifdef FEAT_SEARCH_EXTRA
15771 char_u buf[NUMBUFLEN];
15772 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15773 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15774 int prio = 10; /* default priority */
15775 int id = -1;
15776 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015777 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015778
15779 rettv->vval.v_number = -1;
15780
15781 if (grp == NULL || pat == NULL)
15782 return;
15783 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015784 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015785 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015786 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015787 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015788 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015789 if (argvars[4].v_type != VAR_UNKNOWN)
15790 {
15791 if (argvars[4].v_type != VAR_DICT)
15792 {
15793 EMSG(_(e_dictreq));
15794 return;
15795 }
15796 if (dict_find(argvars[4].vval.v_dict,
15797 (char_u *)"conceal", -1) != NULL)
15798 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15799 (char_u *)"conceal", FALSE);
15800 }
15801 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015802 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015803 if (error == TRUE)
15804 return;
15805 if (id >= 1 && id <= 3)
15806 {
15807 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15808 return;
15809 }
15810
Bram Moolenaar6561d522015-07-21 15:48:27 +020015811 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15812 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015813#endif
15814}
15815
15816/*
15817 * "matchaddpos()" function
15818 */
15819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015820f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015821{
15822#ifdef FEAT_SEARCH_EXTRA
15823 char_u buf[NUMBUFLEN];
15824 char_u *group;
15825 int prio = 10;
15826 int id = -1;
15827 int error = FALSE;
15828 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015829 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015830
15831 rettv->vval.v_number = -1;
15832
15833 group = get_tv_string_buf_chk(&argvars[0], buf);
15834 if (group == NULL)
15835 return;
15836
15837 if (argvars[1].v_type != VAR_LIST)
15838 {
15839 EMSG2(_(e_listarg), "matchaddpos()");
15840 return;
15841 }
15842 l = argvars[1].vval.v_list;
15843 if (l == NULL)
15844 return;
15845
15846 if (argvars[2].v_type != VAR_UNKNOWN)
15847 {
15848 prio = get_tv_number_chk(&argvars[2], &error);
15849 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015850 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015851 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015852 if (argvars[4].v_type != VAR_UNKNOWN)
15853 {
15854 if (argvars[4].v_type != VAR_DICT)
15855 {
15856 EMSG(_(e_dictreq));
15857 return;
15858 }
15859 if (dict_find(argvars[4].vval.v_dict,
15860 (char_u *)"conceal", -1) != NULL)
15861 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15862 (char_u *)"conceal", FALSE);
15863 }
15864 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015865 }
15866 if (error == TRUE)
15867 return;
15868
15869 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15870 if (id == 1 || id == 2)
15871 {
15872 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15873 return;
15874 }
15875
Bram Moolenaar6561d522015-07-21 15:48:27 +020015876 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15877 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015878#endif
15879}
15880
15881/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015882 * "matcharg()" function
15883 */
15884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015885f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015886{
15887 if (rettv_list_alloc(rettv) == OK)
15888 {
15889#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015890 int id = get_tv_number(&argvars[0]);
15891 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015892
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015893 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015894 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015895 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15896 {
15897 list_append_string(rettv->vval.v_list,
15898 syn_id2name(m->hlg_id), -1);
15899 list_append_string(rettv->vval.v_list, m->pattern, -1);
15900 }
15901 else
15902 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015903 list_append_string(rettv->vval.v_list, NULL, -1);
15904 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015905 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015906 }
15907#endif
15908 }
15909}
15910
15911/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015912 * "matchdelete()" function
15913 */
15914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015915f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015916{
15917#ifdef FEAT_SEARCH_EXTRA
15918 rettv->vval.v_number = match_delete(curwin,
15919 (int)get_tv_number(&argvars[0]), TRUE);
15920#endif
15921}
15922
15923/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015924 * "matchend()" function
15925 */
15926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015927f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015928{
15929 find_some_match(argvars, rettv, 0);
15930}
15931
15932/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015933 * "matchlist()" function
15934 */
15935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015936f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015937{
15938 find_some_match(argvars, rettv, 3);
15939}
15940
15941/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015942 * "matchstr()" function
15943 */
15944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015945f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015946{
15947 find_some_match(argvars, rettv, 2);
15948}
15949
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015950static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015951
15952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015953max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015954{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015955 long n = 0;
15956 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015957 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015958
15959 if (argvars[0].v_type == VAR_LIST)
15960 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015961 list_T *l;
15962 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015963
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015964 l = argvars[0].vval.v_list;
15965 if (l != NULL)
15966 {
15967 li = l->lv_first;
15968 if (li != NULL)
15969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015970 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015971 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015972 {
15973 li = li->li_next;
15974 if (li == NULL)
15975 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015976 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015977 if (domax ? i > n : i < n)
15978 n = i;
15979 }
15980 }
15981 }
15982 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015983 else if (argvars[0].v_type == VAR_DICT)
15984 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015985 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015986 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015987 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015988 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015989
15990 d = argvars[0].vval.v_dict;
15991 if (d != NULL)
15992 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015993 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015994 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015995 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015996 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015997 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015998 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015999 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016000 if (first)
16001 {
16002 n = i;
16003 first = FALSE;
16004 }
16005 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016006 n = i;
16007 }
16008 }
16009 }
16010 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016011 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016012 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016013 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016014}
16015
16016/*
16017 * "max()" function
16018 */
16019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016020f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016021{
16022 max_min(argvars, rettv, TRUE);
16023}
16024
16025/*
16026 * "min()" function
16027 */
16028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016029f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016030{
16031 max_min(argvars, rettv, FALSE);
16032}
16033
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016034static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016035
16036/*
16037 * Create the directory in which "dir" is located, and higher levels when
16038 * needed.
16039 */
16040 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016041mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016042{
16043 char_u *p;
16044 char_u *updir;
16045 int r = FAIL;
16046
16047 /* Get end of directory name in "dir".
16048 * We're done when it's "/" or "c:/". */
16049 p = gettail_sep(dir);
16050 if (p <= get_past_head(dir))
16051 return OK;
16052
16053 /* If the directory exists we're done. Otherwise: create it.*/
16054 updir = vim_strnsave(dir, (int)(p - dir));
16055 if (updir == NULL)
16056 return FAIL;
16057 if (mch_isdir(updir))
16058 r = OK;
16059 else if (mkdir_recurse(updir, prot) == OK)
16060 r = vim_mkdir_emsg(updir, prot);
16061 vim_free(updir);
16062 return r;
16063}
16064
16065#ifdef vim_mkdir
16066/*
16067 * "mkdir()" function
16068 */
16069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016070f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016071{
16072 char_u *dir;
16073 char_u buf[NUMBUFLEN];
16074 int prot = 0755;
16075
16076 rettv->vval.v_number = FAIL;
16077 if (check_restricted() || check_secure())
16078 return;
16079
16080 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016081 if (*dir == NUL)
16082 rettv->vval.v_number = FAIL;
16083 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016084 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016085 if (*gettail(dir) == NUL)
16086 /* remove trailing slashes */
16087 *gettail_sep(dir) = NUL;
16088
16089 if (argvars[1].v_type != VAR_UNKNOWN)
16090 {
16091 if (argvars[2].v_type != VAR_UNKNOWN)
16092 prot = get_tv_number_chk(&argvars[2], NULL);
16093 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16094 mkdir_recurse(dir, prot);
16095 }
16096 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016097 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016098}
16099#endif
16100
Bram Moolenaar0d660222005-01-07 21:51:51 +000016101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102 * "mode()" function
16103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016105f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016107 char_u buf[3];
16108
16109 buf[1] = NUL;
16110 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112 if (VIsual_active)
16113 {
16114 if (VIsual_select)
16115 buf[0] = VIsual_mode + 's' - 'v';
16116 else
16117 buf[0] = VIsual_mode;
16118 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016119 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016120 || State == CONFIRM)
16121 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016123 if (State == ASKMORE)
16124 buf[1] = 'm';
16125 else if (State == CONFIRM)
16126 buf[1] = '?';
16127 }
16128 else if (State == EXTERNCMD)
16129 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130 else if (State & INSERT)
16131 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016132#ifdef FEAT_VREPLACE
16133 if (State & VREPLACE_FLAG)
16134 {
16135 buf[0] = 'R';
16136 buf[1] = 'v';
16137 }
16138 else
16139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 if (State & REPLACE_FLAG)
16141 buf[0] = 'R';
16142 else
16143 buf[0] = 'i';
16144 }
16145 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016146 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016148 if (exmode_active)
16149 buf[1] = 'v';
16150 }
16151 else if (exmode_active)
16152 {
16153 buf[0] = 'c';
16154 buf[1] = 'e';
16155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016159 if (finish_op)
16160 buf[1] = 'o';
16161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016163 /* Clear out the minor mode when the argument is not a non-zero number or
16164 * non-empty string. */
16165 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016166 buf[1] = NUL;
16167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016168 rettv->vval.v_string = vim_strsave(buf);
16169 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170}
16171
Bram Moolenaar429fa852013-04-15 12:27:36 +020016172#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016173/*
16174 * "mzeval()" function
16175 */
16176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016177f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016178{
16179 char_u *str;
16180 char_u buf[NUMBUFLEN];
16181
16182 str = get_tv_string_buf(&argvars[0], buf);
16183 do_mzeval(str, rettv);
16184}
Bram Moolenaar75676462013-01-30 14:55:42 +010016185
16186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016187mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016188{
16189 typval_T argvars[3];
16190
16191 argvars[0].v_type = VAR_STRING;
16192 argvars[0].vval.v_string = name;
16193 copy_tv(args, &argvars[1]);
16194 argvars[2].v_type = VAR_UNKNOWN;
16195 f_call(argvars, rettv);
16196 clear_tv(&argvars[1]);
16197}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016198#endif
16199
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016201 * "nextnonblank()" function
16202 */
16203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016204f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016205{
16206 linenr_T lnum;
16207
16208 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016210 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016211 {
16212 lnum = 0;
16213 break;
16214 }
16215 if (*skipwhite(ml_get(lnum)) != NUL)
16216 break;
16217 }
16218 rettv->vval.v_number = lnum;
16219}
16220
16221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 * "nr2char()" function
16223 */
16224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016225f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226{
16227 char_u buf[NUMBUFLEN];
16228
16229#ifdef FEAT_MBYTE
16230 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016231 {
16232 int utf8 = 0;
16233
16234 if (argvars[1].v_type != VAR_UNKNOWN)
16235 utf8 = get_tv_number_chk(&argvars[1], NULL);
16236 if (utf8)
16237 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16238 else
16239 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 else
16242#endif
16243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016244 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245 buf[1] = NUL;
16246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016247 rettv->v_type = VAR_STRING;
16248 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016249}
16250
16251/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016252 * "or(expr, expr)" function
16253 */
16254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016255f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016256{
16257 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16258 | get_tv_number_chk(&argvars[1], NULL);
16259}
16260
16261/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016262 * "pathshorten()" function
16263 */
16264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016265f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016266{
16267 char_u *p;
16268
16269 rettv->v_type = VAR_STRING;
16270 p = get_tv_string_chk(&argvars[0]);
16271 if (p == NULL)
16272 rettv->vval.v_string = NULL;
16273 else
16274 {
16275 p = vim_strsave(p);
16276 rettv->vval.v_string = p;
16277 if (p != NULL)
16278 shorten_dir(p);
16279 }
16280}
16281
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016282#ifdef FEAT_PERL
16283/*
16284 * "perleval()" function
16285 */
16286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016287f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016288{
16289 char_u *str;
16290 char_u buf[NUMBUFLEN];
16291
16292 str = get_tv_string_buf(&argvars[0], buf);
16293 do_perleval(str, rettv);
16294}
16295#endif
16296
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016297#ifdef FEAT_FLOAT
16298/*
16299 * "pow()" function
16300 */
16301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016302f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016303{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016304 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016305
16306 rettv->v_type = VAR_FLOAT;
16307 if (get_float_arg(argvars, &fx) == OK
16308 && get_float_arg(&argvars[1], &fy) == OK)
16309 rettv->vval.v_float = pow(fx, fy);
16310 else
16311 rettv->vval.v_float = 0.0;
16312}
16313#endif
16314
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016315/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316 * "prevnonblank()" function
16317 */
16318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016319f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016320{
16321 linenr_T lnum;
16322
16323 lnum = get_tv_lnum(argvars);
16324 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16325 lnum = 0;
16326 else
16327 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16328 --lnum;
16329 rettv->vval.v_number = lnum;
16330}
16331
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016332/* This dummy va_list is here because:
16333 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16334 * - locally in the function results in a "used before set" warning
16335 * - using va_start() to initialize it gives "function with fixed args" error */
16336static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016337
Bram Moolenaar8c711452005-01-14 21:53:12 +000016338/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016339 * "printf()" function
16340 */
16341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016342f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016343{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016344 char_u buf[NUMBUFLEN];
16345 int len;
16346 char_u *s;
16347 int saved_did_emsg = did_emsg;
16348 char *fmt;
16349
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016350 rettv->v_type = VAR_STRING;
16351 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016352
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016353 /* Get the required length, allocate the buffer and do it for real. */
16354 did_emsg = FALSE;
16355 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16356 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16357 if (!did_emsg)
16358 {
16359 s = alloc(len + 1);
16360 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016361 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016362 rettv->vval.v_string = s;
16363 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016364 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016365 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016366 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016367}
16368
16369/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016370 * "pumvisible()" function
16371 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016373f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016374{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016375#ifdef FEAT_INS_EXPAND
16376 if (pum_visible())
16377 rettv->vval.v_number = 1;
16378#endif
16379}
16380
Bram Moolenaardb913952012-06-29 12:54:53 +020016381#ifdef FEAT_PYTHON3
16382/*
16383 * "py3eval()" function
16384 */
16385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016386f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016387{
16388 char_u *str;
16389 char_u buf[NUMBUFLEN];
16390
16391 str = get_tv_string_buf(&argvars[0], buf);
16392 do_py3eval(str, rettv);
16393}
16394#endif
16395
16396#ifdef FEAT_PYTHON
16397/*
16398 * "pyeval()" function
16399 */
16400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016401f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016402{
16403 char_u *str;
16404 char_u buf[NUMBUFLEN];
16405
16406 str = get_tv_string_buf(&argvars[0], buf);
16407 do_pyeval(str, rettv);
16408}
16409#endif
16410
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016411/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016412 * "range()" function
16413 */
16414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016415f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016416{
16417 long start;
16418 long end;
16419 long stride = 1;
16420 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016421 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016422
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016423 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016424 if (argvars[1].v_type == VAR_UNKNOWN)
16425 {
16426 end = start - 1;
16427 start = 0;
16428 }
16429 else
16430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016431 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016432 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016433 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016434 }
16435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016436 if (error)
16437 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016438 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016439 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016440 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016441 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016442 else
16443 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016444 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016445 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016446 if (list_append_number(rettv->vval.v_list,
16447 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016448 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016449 }
16450}
16451
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016452/*
16453 * "readfile()" function
16454 */
16455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016456f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016457{
16458 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016459 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016460 char_u *fname;
16461 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016462 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16463 int io_size = sizeof(buf);
16464 int readlen; /* size of last fread() */
16465 char_u *prev = NULL; /* previously read bytes, if any */
16466 long prevlen = 0; /* length of data in prev */
16467 long prevsize = 0; /* size of prev buffer */
16468 long maxline = MAXLNUM;
16469 long cnt = 0;
16470 char_u *p; /* position in buf */
16471 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016472
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016473 if (argvars[1].v_type != VAR_UNKNOWN)
16474 {
16475 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16476 binary = TRUE;
16477 if (argvars[2].v_type != VAR_UNKNOWN)
16478 maxline = get_tv_number(&argvars[2]);
16479 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016480
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016481 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016482 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016483
16484 /* Always open the file in binary mode, library functions have a mind of
16485 * their own about CR-LF conversion. */
16486 fname = get_tv_string(&argvars[0]);
16487 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16488 {
16489 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16490 return;
16491 }
16492
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016493 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016494 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016495 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016496
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016497 /* This for loop processes what was read, but is also entered at end
16498 * of file so that either:
16499 * - an incomplete line gets written
16500 * - a "binary" file gets an empty line at the end if it ends in a
16501 * newline. */
16502 for (p = buf, start = buf;
16503 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16504 ++p)
16505 {
16506 if (*p == '\n' || readlen <= 0)
16507 {
16508 listitem_T *li;
16509 char_u *s = NULL;
16510 long_u len = p - start;
16511
16512 /* Finished a line. Remove CRs before NL. */
16513 if (readlen > 0 && !binary)
16514 {
16515 while (len > 0 && start[len - 1] == '\r')
16516 --len;
16517 /* removal may cross back to the "prev" string */
16518 if (len == 0)
16519 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16520 --prevlen;
16521 }
16522 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016523 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016524 else
16525 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016526 /* Change "prev" buffer to be the right size. This way
16527 * the bytes are only copied once, and very long lines are
16528 * allocated only once. */
16529 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016530 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016531 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016532 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016533 prev = NULL; /* the list will own the string */
16534 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016535 }
16536 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016537 if (s == NULL)
16538 {
16539 do_outofmem_msg((long_u) prevlen + len + 1);
16540 failed = TRUE;
16541 break;
16542 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016543
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016544 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016545 {
16546 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016547 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016548 break;
16549 }
16550 li->li_tv.v_type = VAR_STRING;
16551 li->li_tv.v_lock = 0;
16552 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016553 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016554
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016555 start = p + 1; /* step over newline */
16556 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016557 break;
16558 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016559 else if (*p == NUL)
16560 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016561#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016562 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16563 * when finding the BF and check the previous two bytes. */
16564 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016565 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016566 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16567 * + 1, these may be in the "prev" string. */
16568 char_u back1 = p >= buf + 1 ? p[-1]
16569 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16570 char_u back2 = p >= buf + 2 ? p[-2]
16571 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16572 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016573
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016574 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016575 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016576 char_u *dest = p - 2;
16577
16578 /* Usually a BOM is at the beginning of a file, and so at
16579 * the beginning of a line; then we can just step over it.
16580 */
16581 if (start == dest)
16582 start = p + 1;
16583 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016584 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016585 /* have to shuffle buf to close gap */
16586 int adjust_prevlen = 0;
16587
16588 if (dest < buf)
16589 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016590 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016591 dest = buf;
16592 }
16593 if (readlen > p - buf + 1)
16594 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16595 readlen -= 3 - adjust_prevlen;
16596 prevlen -= adjust_prevlen;
16597 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016598 }
16599 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016600 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016601#endif
16602 } /* for */
16603
16604 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16605 break;
16606 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016607 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016608 /* There's part of a line in buf, store it in "prev". */
16609 if (p - start + prevlen >= prevsize)
16610 {
16611 /* need bigger "prev" buffer */
16612 char_u *newprev;
16613
16614 /* A common use case is ordinary text files and "prev" gets a
16615 * fragment of a line, so the first allocation is made
16616 * small, to avoid repeatedly 'allocing' large and
16617 * 'reallocing' small. */
16618 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016619 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016620 else
16621 {
16622 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016623 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016624 prevsize = grow50pc > growmin ? grow50pc : growmin;
16625 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016626 newprev = prev == NULL ? alloc(prevsize)
16627 : vim_realloc(prev, prevsize);
16628 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016629 {
16630 do_outofmem_msg((long_u)prevsize);
16631 failed = TRUE;
16632 break;
16633 }
16634 prev = newprev;
16635 }
16636 /* Add the line part to end of "prev". */
16637 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016638 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016639 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016640 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016641
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016642 /*
16643 * For a negative line count use only the lines at the end of the file,
16644 * free the rest.
16645 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016646 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016647 while (cnt > -maxline)
16648 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016649 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016650 --cnt;
16651 }
16652
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016653 if (failed)
16654 {
16655 list_free(rettv->vval.v_list, TRUE);
16656 /* readfile doc says an empty list is returned on error */
16657 rettv->vval.v_list = list_alloc();
16658 }
16659
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016660 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016661 fclose(fd);
16662}
16663
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016664#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016665static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016666
16667/*
16668 * Convert a List to proftime_T.
16669 * Return FAIL when there is something wrong.
16670 */
16671 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016672list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016673{
16674 long n1, n2;
16675 int error = FALSE;
16676
16677 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16678 || arg->vval.v_list->lv_len != 2)
16679 return FAIL;
16680 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16681 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16682# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016683 tm->HighPart = n1;
16684 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016685# else
16686 tm->tv_sec = n1;
16687 tm->tv_usec = n2;
16688# endif
16689 return error ? FAIL : OK;
16690}
16691#endif /* FEAT_RELTIME */
16692
16693/*
16694 * "reltime()" function
16695 */
16696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016697f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016698{
16699#ifdef FEAT_RELTIME
16700 proftime_T res;
16701 proftime_T start;
16702
16703 if (argvars[0].v_type == VAR_UNKNOWN)
16704 {
16705 /* No arguments: get current time. */
16706 profile_start(&res);
16707 }
16708 else if (argvars[1].v_type == VAR_UNKNOWN)
16709 {
16710 if (list2proftime(&argvars[0], &res) == FAIL)
16711 return;
16712 profile_end(&res);
16713 }
16714 else
16715 {
16716 /* Two arguments: compute the difference. */
16717 if (list2proftime(&argvars[0], &start) == FAIL
16718 || list2proftime(&argvars[1], &res) == FAIL)
16719 return;
16720 profile_sub(&res, &start);
16721 }
16722
16723 if (rettv_list_alloc(rettv) == OK)
16724 {
16725 long n1, n2;
16726
16727# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016728 n1 = res.HighPart;
16729 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016730# else
16731 n1 = res.tv_sec;
16732 n2 = res.tv_usec;
16733# endif
16734 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16735 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16736 }
16737#endif
16738}
16739
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016740#ifdef FEAT_FLOAT
16741/*
16742 * "reltimefloat()" function
16743 */
16744 static void
16745f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16746{
16747# ifdef FEAT_RELTIME
16748 proftime_T tm;
16749# endif
16750
16751 rettv->v_type = VAR_FLOAT;
16752 rettv->vval.v_float = 0;
16753# ifdef FEAT_RELTIME
16754 if (list2proftime(&argvars[0], &tm) == OK)
16755 rettv->vval.v_float = profile_float(&tm);
16756# endif
16757}
16758#endif
16759
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016760/*
16761 * "reltimestr()" function
16762 */
16763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016764f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016765{
16766#ifdef FEAT_RELTIME
16767 proftime_T tm;
16768#endif
16769
16770 rettv->v_type = VAR_STRING;
16771 rettv->vval.v_string = NULL;
16772#ifdef FEAT_RELTIME
16773 if (list2proftime(&argvars[0], &tm) == OK)
16774 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16775#endif
16776}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016777
Bram Moolenaar0d660222005-01-07 21:51:51 +000016778#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016779static void make_connection(void);
16780static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016781
16782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016783make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016784{
16785 if (X_DISPLAY == NULL
16786# ifdef FEAT_GUI
16787 && !gui.in_use
16788# endif
16789 )
16790 {
16791 x_force_connect = TRUE;
16792 setup_term_clip();
16793 x_force_connect = FALSE;
16794 }
16795}
16796
16797 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016798check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016799{
16800 make_connection();
16801 if (X_DISPLAY == NULL)
16802 {
16803 EMSG(_("E240: No connection to Vim server"));
16804 return FAIL;
16805 }
16806 return OK;
16807}
16808#endif
16809
16810#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016811static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016812
16813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016814remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016815{
16816 char_u *server_name;
16817 char_u *keys;
16818 char_u *r = NULL;
16819 char_u buf[NUMBUFLEN];
16820# ifdef WIN32
16821 HWND w;
16822# else
16823 Window w;
16824# endif
16825
16826 if (check_restricted() || check_secure())
16827 return;
16828
16829# ifdef FEAT_X11
16830 if (check_connection() == FAIL)
16831 return;
16832# endif
16833
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016834 server_name = get_tv_string_chk(&argvars[0]);
16835 if (server_name == NULL)
16836 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016837 keys = get_tv_string_buf(&argvars[1], buf);
16838# ifdef WIN32
16839 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16840# else
16841 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16842 < 0)
16843# endif
16844 {
16845 if (r != NULL)
16846 EMSG(r); /* sending worked but evaluation failed */
16847 else
16848 EMSG2(_("E241: Unable to send to %s"), server_name);
16849 return;
16850 }
16851
16852 rettv->vval.v_string = r;
16853
16854 if (argvars[2].v_type != VAR_UNKNOWN)
16855 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016856 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016857 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016858 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016859
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016860 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016861 v.di_tv.v_type = VAR_STRING;
16862 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016863 idvar = get_tv_string_chk(&argvars[2]);
16864 if (idvar != NULL)
16865 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016866 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016867 }
16868}
16869#endif
16870
16871/*
16872 * "remote_expr()" function
16873 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016875f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016876{
16877 rettv->v_type = VAR_STRING;
16878 rettv->vval.v_string = NULL;
16879#ifdef FEAT_CLIENTSERVER
16880 remote_common(argvars, rettv, TRUE);
16881#endif
16882}
16883
16884/*
16885 * "remote_foreground()" function
16886 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016888f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016889{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016890#ifdef FEAT_CLIENTSERVER
16891# ifdef WIN32
16892 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016893 {
16894 char_u *server_name = get_tv_string_chk(&argvars[0]);
16895
16896 if (server_name != NULL)
16897 serverForeground(server_name);
16898 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016899# else
16900 /* Send a foreground() expression to the server. */
16901 argvars[1].v_type = VAR_STRING;
16902 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16903 argvars[2].v_type = VAR_UNKNOWN;
16904 remote_common(argvars, rettv, TRUE);
16905 vim_free(argvars[1].vval.v_string);
16906# endif
16907#endif
16908}
16909
Bram Moolenaar0d660222005-01-07 21:51:51 +000016910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016911f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016912{
16913#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016914 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016915 char_u *s = NULL;
16916# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016917 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016918# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016919 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016920
16921 if (check_restricted() || check_secure())
16922 {
16923 rettv->vval.v_number = -1;
16924 return;
16925 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016926 serverid = get_tv_string_chk(&argvars[0]);
16927 if (serverid == NULL)
16928 {
16929 rettv->vval.v_number = -1;
16930 return; /* type error; errmsg already given */
16931 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016932# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016933 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016934 if (n == 0)
16935 rettv->vval.v_number = -1;
16936 else
16937 {
16938 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16939 rettv->vval.v_number = (s != NULL);
16940 }
16941# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942 if (check_connection() == FAIL)
16943 return;
16944
16945 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016946 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016947# endif
16948
16949 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016951 char_u *retvar;
16952
Bram Moolenaar33570922005-01-25 22:26:29 +000016953 v.di_tv.v_type = VAR_STRING;
16954 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016955 retvar = get_tv_string_chk(&argvars[1]);
16956 if (retvar != NULL)
16957 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016958 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016959 }
16960#else
16961 rettv->vval.v_number = -1;
16962#endif
16963}
16964
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016966f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967{
16968 char_u *r = NULL;
16969
16970#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016971 char_u *serverid = get_tv_string_chk(&argvars[0]);
16972
16973 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974 {
16975# ifdef WIN32
16976 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016977 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016979 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980 if (n != 0)
16981 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16982 if (r == NULL)
16983# else
16984 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016985 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986# endif
16987 EMSG(_("E277: Unable to read a server reply"));
16988 }
16989#endif
16990 rettv->v_type = VAR_STRING;
16991 rettv->vval.v_string = r;
16992}
16993
16994/*
16995 * "remote_send()" function
16996 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016998f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016999{
17000 rettv->v_type = VAR_STRING;
17001 rettv->vval.v_string = NULL;
17002#ifdef FEAT_CLIENTSERVER
17003 remote_common(argvars, rettv, FALSE);
17004#endif
17005}
17006
17007/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017008 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017009 */
17010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017011f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017012{
Bram Moolenaar33570922005-01-25 22:26:29 +000017013 list_T *l;
17014 listitem_T *item, *item2;
17015 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017016 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017017 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017018 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017019 dict_T *d;
17020 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017021 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017022
Bram Moolenaar8c711452005-01-14 21:53:12 +000017023 if (argvars[0].v_type == VAR_DICT)
17024 {
17025 if (argvars[2].v_type != VAR_UNKNOWN)
17026 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017027 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017028 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017029 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017030 key = get_tv_string_chk(&argvars[1]);
17031 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017033 di = dict_find(d, key, -1);
17034 if (di == NULL)
17035 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017036 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17037 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017038 {
17039 *rettv = di->di_tv;
17040 init_tv(&di->di_tv);
17041 dictitem_remove(d, di);
17042 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017044 }
17045 }
17046 else if (argvars[0].v_type != VAR_LIST)
17047 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017048 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017049 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017051 int error = FALSE;
17052
17053 idx = get_tv_number_chk(&argvars[1], &error);
17054 if (error)
17055 ; /* type error: do nothing, errmsg already given */
17056 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017057 EMSGN(_(e_listidx), idx);
17058 else
17059 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017060 if (argvars[2].v_type == VAR_UNKNOWN)
17061 {
17062 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017063 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017064 *rettv = item->li_tv;
17065 vim_free(item);
17066 }
17067 else
17068 {
17069 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017070 end = get_tv_number_chk(&argvars[2], &error);
17071 if (error)
17072 ; /* type error: do nothing */
17073 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017074 EMSGN(_(e_listidx), end);
17075 else
17076 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017077 int cnt = 0;
17078
17079 for (li = item; li != NULL; li = li->li_next)
17080 {
17081 ++cnt;
17082 if (li == item2)
17083 break;
17084 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017085 if (li == NULL) /* didn't find "item2" after "item" */
17086 EMSG(_(e_invrange));
17087 else
17088 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017089 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017090 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017091 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017092 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017093 l->lv_first = item;
17094 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017095 item->li_prev = NULL;
17096 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017097 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017098 }
17099 }
17100 }
17101 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017102 }
17103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104}
17105
17106/*
17107 * "rename({from}, {to})" function
17108 */
17109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017110f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111{
17112 char_u buf[NUMBUFLEN];
17113
17114 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017115 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017117 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17118 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119}
17120
17121/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017122 * "repeat()" function
17123 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017125f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017126{
17127 char_u *p;
17128 int n;
17129 int slen;
17130 int len;
17131 char_u *r;
17132 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017133
17134 n = get_tv_number(&argvars[1]);
17135 if (argvars[0].v_type == VAR_LIST)
17136 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017137 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017138 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017139 if (list_extend(rettv->vval.v_list,
17140 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017141 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017142 }
17143 else
17144 {
17145 p = get_tv_string(&argvars[0]);
17146 rettv->v_type = VAR_STRING;
17147 rettv->vval.v_string = NULL;
17148
17149 slen = (int)STRLEN(p);
17150 len = slen * n;
17151 if (len <= 0)
17152 return;
17153
17154 r = alloc(len + 1);
17155 if (r != NULL)
17156 {
17157 for (i = 0; i < n; i++)
17158 mch_memmove(r + i * slen, p, (size_t)slen);
17159 r[len] = NUL;
17160 }
17161
17162 rettv->vval.v_string = r;
17163 }
17164}
17165
17166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 * "resolve()" function
17168 */
17169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017170f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171{
17172 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017173#ifdef HAVE_READLINK
17174 char_u *buf = NULL;
17175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017177 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178#ifdef FEAT_SHORTCUT
17179 {
17180 char_u *v = NULL;
17181
17182 v = mch_resolve_shortcut(p);
17183 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017184 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017186 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 }
17188#else
17189# ifdef HAVE_READLINK
17190 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 char_u *cpy;
17192 int len;
17193 char_u *remain = NULL;
17194 char_u *q;
17195 int is_relative_to_current = FALSE;
17196 int has_trailing_pathsep = FALSE;
17197 int limit = 100;
17198
17199 p = vim_strsave(p);
17200
17201 if (p[0] == '.' && (vim_ispathsep(p[1])
17202 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17203 is_relative_to_current = TRUE;
17204
17205 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017206 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017209 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211
17212 q = getnextcomp(p);
17213 if (*q != NUL)
17214 {
17215 /* Separate the first path component in "p", and keep the
17216 * remainder (beginning with the path separator). */
17217 remain = vim_strsave(q - 1);
17218 q[-1] = NUL;
17219 }
17220
Bram Moolenaard9462e32011-04-11 21:35:11 +020017221 buf = alloc(MAXPATHL + 1);
17222 if (buf == NULL)
17223 goto fail;
17224
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225 for (;;)
17226 {
17227 for (;;)
17228 {
17229 len = readlink((char *)p, (char *)buf, MAXPATHL);
17230 if (len <= 0)
17231 break;
17232 buf[len] = NUL;
17233
17234 if (limit-- == 0)
17235 {
17236 vim_free(p);
17237 vim_free(remain);
17238 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017239 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 goto fail;
17241 }
17242
17243 /* Ensure that the result will have a trailing path separator
17244 * if the argument has one. */
17245 if (remain == NULL && has_trailing_pathsep)
17246 add_pathsep(buf);
17247
17248 /* Separate the first path component in the link value and
17249 * concatenate the remainders. */
17250 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17251 if (*q != NUL)
17252 {
17253 if (remain == NULL)
17254 remain = vim_strsave(q - 1);
17255 else
17256 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017257 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 if (cpy != NULL)
17259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260 vim_free(remain);
17261 remain = cpy;
17262 }
17263 }
17264 q[-1] = NUL;
17265 }
17266
17267 q = gettail(p);
17268 if (q > p && *q == NUL)
17269 {
17270 /* Ignore trailing path separator. */
17271 q[-1] = NUL;
17272 q = gettail(p);
17273 }
17274 if (q > p && !mch_isFullName(buf))
17275 {
17276 /* symlink is relative to directory of argument */
17277 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17278 if (cpy != NULL)
17279 {
17280 STRCPY(cpy, p);
17281 STRCPY(gettail(cpy), buf);
17282 vim_free(p);
17283 p = cpy;
17284 }
17285 }
17286 else
17287 {
17288 vim_free(p);
17289 p = vim_strsave(buf);
17290 }
17291 }
17292
17293 if (remain == NULL)
17294 break;
17295
17296 /* Append the first path component of "remain" to "p". */
17297 q = getnextcomp(remain + 1);
17298 len = q - remain - (*q != NUL);
17299 cpy = vim_strnsave(p, STRLEN(p) + len);
17300 if (cpy != NULL)
17301 {
17302 STRNCAT(cpy, remain, len);
17303 vim_free(p);
17304 p = cpy;
17305 }
17306 /* Shorten "remain". */
17307 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017308 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 else
17310 {
17311 vim_free(remain);
17312 remain = NULL;
17313 }
17314 }
17315
17316 /* If the result is a relative path name, make it explicitly relative to
17317 * the current directory if and only if the argument had this form. */
17318 if (!vim_ispathsep(*p))
17319 {
17320 if (is_relative_to_current
17321 && *p != NUL
17322 && !(p[0] == '.'
17323 && (p[1] == NUL
17324 || vim_ispathsep(p[1])
17325 || (p[1] == '.'
17326 && (p[2] == NUL
17327 || vim_ispathsep(p[2]))))))
17328 {
17329 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017330 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331 if (cpy != NULL)
17332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 vim_free(p);
17334 p = cpy;
17335 }
17336 }
17337 else if (!is_relative_to_current)
17338 {
17339 /* Strip leading "./". */
17340 q = p;
17341 while (q[0] == '.' && vim_ispathsep(q[1]))
17342 q += 2;
17343 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017344 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345 }
17346 }
17347
17348 /* Ensure that the result will have no trailing path separator
17349 * if the argument had none. But keep "/" or "//". */
17350 if (!has_trailing_pathsep)
17351 {
17352 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017353 if (after_pathsep(p, q))
17354 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355 }
17356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017357 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358 }
17359# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017360 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361# endif
17362#endif
17363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017364 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365
17366#ifdef HAVE_READLINK
17367fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017368 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017370 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371}
17372
17373/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017374 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375 */
17376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017377f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378{
Bram Moolenaar33570922005-01-25 22:26:29 +000017379 list_T *l;
17380 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381
Bram Moolenaar0d660222005-01-07 21:51:51 +000017382 if (argvars[0].v_type != VAR_LIST)
17383 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017384 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017385 && !tv_check_lock(l->lv_lock,
17386 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017387 {
17388 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017389 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017390 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017391 while (li != NULL)
17392 {
17393 ni = li->li_prev;
17394 list_append(l, li);
17395 li = ni;
17396 }
17397 rettv->vval.v_list = l;
17398 rettv->v_type = VAR_LIST;
17399 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017400 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402}
17403
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017404#define SP_NOMOVE 0x01 /* don't move cursor */
17405#define SP_REPEAT 0x02 /* repeat to find outer pair */
17406#define SP_RETCOUNT 0x04 /* return matchcount */
17407#define SP_SETPCMARK 0x08 /* set previous context mark */
17408#define SP_START 0x10 /* accept match at start position */
17409#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17410#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017411#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017412
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017413static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017414
17415/*
17416 * Get flags for a search function.
17417 * Possibly sets "p_ws".
17418 * Returns BACKWARD, FORWARD or zero (for an error).
17419 */
17420 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017421get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017422{
17423 int dir = FORWARD;
17424 char_u *flags;
17425 char_u nbuf[NUMBUFLEN];
17426 int mask;
17427
17428 if (varp->v_type != VAR_UNKNOWN)
17429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017430 flags = get_tv_string_buf_chk(varp, nbuf);
17431 if (flags == NULL)
17432 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017433 while (*flags != NUL)
17434 {
17435 switch (*flags)
17436 {
17437 case 'b': dir = BACKWARD; break;
17438 case 'w': p_ws = TRUE; break;
17439 case 'W': p_ws = FALSE; break;
17440 default: mask = 0;
17441 if (flagsp != NULL)
17442 switch (*flags)
17443 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017444 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017445 case 'e': mask = SP_END; break;
17446 case 'm': mask = SP_RETCOUNT; break;
17447 case 'n': mask = SP_NOMOVE; break;
17448 case 'p': mask = SP_SUBPAT; break;
17449 case 'r': mask = SP_REPEAT; break;
17450 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017451 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017452 }
17453 if (mask == 0)
17454 {
17455 EMSG2(_(e_invarg2), flags);
17456 dir = 0;
17457 }
17458 else
17459 *flagsp |= mask;
17460 }
17461 if (dir == 0)
17462 break;
17463 ++flags;
17464 }
17465 }
17466 return dir;
17467}
17468
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017470 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017472 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017473search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017475 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 char_u *pat;
17477 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017478 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 int save_p_ws = p_ws;
17480 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017481 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017482 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017483 proftime_T tm;
17484#ifdef FEAT_RELTIME
17485 long time_limit = 0;
17486#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017487 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017488 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017490 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017491 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017492 if (dir == 0)
17493 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017494 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017495 if (flags & SP_START)
17496 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017497 if (flags & SP_END)
17498 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017499 if (flags & SP_COLUMN)
17500 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017501
Bram Moolenaar76929292008-01-06 19:07:36 +000017502 /* Optional arguments: line number to stop searching and timeout. */
17503 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017504 {
17505 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17506 if (lnum_stop < 0)
17507 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017508#ifdef FEAT_RELTIME
17509 if (argvars[3].v_type != VAR_UNKNOWN)
17510 {
17511 time_limit = get_tv_number_chk(&argvars[3], NULL);
17512 if (time_limit < 0)
17513 goto theend;
17514 }
17515#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017516 }
17517
Bram Moolenaar76929292008-01-06 19:07:36 +000017518#ifdef FEAT_RELTIME
17519 /* Set the time limit, if there is one. */
17520 profile_setlimit(time_limit, &tm);
17521#endif
17522
Bram Moolenaar231334e2005-07-25 20:46:57 +000017523 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017524 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017525 * Check to make sure only those flags are set.
17526 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17527 * flags cannot be set. Check for that condition also.
17528 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017529 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017530 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017532 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017533 goto theend;
17534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017536 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017537 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017538 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017539 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017541 if (flags & SP_SUBPAT)
17542 retval = subpatnum;
17543 else
17544 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017545 if (flags & SP_SETPCMARK)
17546 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017548 if (match_pos != NULL)
17549 {
17550 /* Store the match cursor position */
17551 match_pos->lnum = pos.lnum;
17552 match_pos->col = pos.col + 1;
17553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554 /* "/$" will put the cursor after the end of the line, may need to
17555 * correct that here */
17556 check_cursor();
17557 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017558
17559 /* If 'n' flag is used: restore cursor position. */
17560 if (flags & SP_NOMOVE)
17561 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017562 else
17563 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017564theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017566
17567 return retval;
17568}
17569
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017570#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017571
17572/*
17573 * round() is not in C90, use ceil() or floor() instead.
17574 */
17575 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017576vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017577{
17578 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17579}
17580
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017581/*
17582 * "round({float})" function
17583 */
17584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017585f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017586{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017587 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017588
17589 rettv->v_type = VAR_FLOAT;
17590 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017591 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017592 else
17593 rettv->vval.v_float = 0.0;
17594}
17595#endif
17596
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017597/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017598 * "screenattr()" function
17599 */
17600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017601f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017602{
17603 int row;
17604 int col;
17605 int c;
17606
17607 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17608 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17609 if (row < 0 || row >= screen_Rows
17610 || col < 0 || col >= screen_Columns)
17611 c = -1;
17612 else
17613 c = ScreenAttrs[LineOffset[row] + col];
17614 rettv->vval.v_number = c;
17615}
17616
17617/*
17618 * "screenchar()" function
17619 */
17620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017621f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017622{
17623 int row;
17624 int col;
17625 int off;
17626 int c;
17627
17628 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17629 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17630 if (row < 0 || row >= screen_Rows
17631 || col < 0 || col >= screen_Columns)
17632 c = -1;
17633 else
17634 {
17635 off = LineOffset[row] + col;
17636#ifdef FEAT_MBYTE
17637 if (enc_utf8 && ScreenLinesUC[off] != 0)
17638 c = ScreenLinesUC[off];
17639 else
17640#endif
17641 c = ScreenLines[off];
17642 }
17643 rettv->vval.v_number = c;
17644}
17645
17646/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017647 * "screencol()" function
17648 *
17649 * First column is 1 to be consistent with virtcol().
17650 */
17651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017652f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017653{
17654 rettv->vval.v_number = screen_screencol() + 1;
17655}
17656
17657/*
17658 * "screenrow()" function
17659 */
17660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017661f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017662{
17663 rettv->vval.v_number = screen_screenrow() + 1;
17664}
17665
17666/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017667 * "search()" function
17668 */
17669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017670f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017671{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017672 int flags = 0;
17673
17674 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675}
17676
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017678 * "searchdecl()" function
17679 */
17680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017681f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017682{
17683 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017684 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017685 int error = FALSE;
17686 char_u *name;
17687
17688 rettv->vval.v_number = 1; /* default: FAIL */
17689
17690 name = get_tv_string_chk(&argvars[0]);
17691 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017692 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017693 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017694 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17695 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17696 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017697 if (!error && name != NULL)
17698 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017699 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017700}
17701
17702/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017703 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017706searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707{
17708 char_u *spat, *mpat, *epat;
17709 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 int dir;
17712 int flags = 0;
17713 char_u nbuf1[NUMBUFLEN];
17714 char_u nbuf2[NUMBUFLEN];
17715 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017716 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017717 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017718 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017721 spat = get_tv_string_chk(&argvars[0]);
17722 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17723 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17724 if (spat == NULL || mpat == NULL || epat == NULL)
17725 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727 /* Handle the optional fourth argument: flags */
17728 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017729 if (dir == 0)
17730 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017731
17732 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017733 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17734 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017735 if ((flags & (SP_END | SP_SUBPAT)) != 0
17736 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017737 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017738 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017739 goto theend;
17740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017742 /* Using 'r' implies 'W', otherwise it doesn't work. */
17743 if (flags & SP_REPEAT)
17744 p_ws = FALSE;
17745
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017746 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017747 if (argvars[3].v_type == VAR_UNKNOWN
17748 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749 skip = (char_u *)"";
17750 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017752 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017753 if (argvars[5].v_type != VAR_UNKNOWN)
17754 {
17755 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17756 if (lnum_stop < 0)
17757 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017758#ifdef FEAT_RELTIME
17759 if (argvars[6].v_type != VAR_UNKNOWN)
17760 {
17761 time_limit = get_tv_number_chk(&argvars[6], NULL);
17762 if (time_limit < 0)
17763 goto theend;
17764 }
17765#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017766 }
17767 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017768 if (skip == NULL)
17769 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017771 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017772 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017773
17774theend:
17775 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017776
17777 return retval;
17778}
17779
17780/*
17781 * "searchpair()" function
17782 */
17783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017784f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017785{
17786 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17787}
17788
17789/*
17790 * "searchpairpos()" function
17791 */
17792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017793f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017794{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017795 pos_T match_pos;
17796 int lnum = 0;
17797 int col = 0;
17798
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017799 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017800 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017801
17802 if (searchpair_cmn(argvars, &match_pos) > 0)
17803 {
17804 lnum = match_pos.lnum;
17805 col = match_pos.col;
17806 }
17807
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017808 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17809 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017810}
17811
17812/*
17813 * Search for a start/middle/end thing.
17814 * Used by searchpair(), see its documentation for the details.
17815 * Returns 0 or -1 for no match,
17816 */
17817 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017818do_searchpair(
17819 char_u *spat, /* start pattern */
17820 char_u *mpat, /* middle pattern */
17821 char_u *epat, /* end pattern */
17822 int dir, /* BACKWARD or FORWARD */
17823 char_u *skip, /* skip expression */
17824 int flags, /* SP_SETPCMARK and other SP_ values */
17825 pos_T *match_pos,
17826 linenr_T lnum_stop, /* stop at this line if not zero */
17827 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017828{
17829 char_u *save_cpo;
17830 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17831 long retval = 0;
17832 pos_T pos;
17833 pos_T firstpos;
17834 pos_T foundpos;
17835 pos_T save_cursor;
17836 pos_T save_pos;
17837 int n;
17838 int r;
17839 int nest = 1;
17840 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017841 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017842 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017843
17844 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17845 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017846 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017847
Bram Moolenaar76929292008-01-06 19:07:36 +000017848#ifdef FEAT_RELTIME
17849 /* Set the time limit, if there is one. */
17850 profile_setlimit(time_limit, &tm);
17851#endif
17852
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017853 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17854 * start/middle/end (pat3, for the top pair). */
17855 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17856 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17857 if (pat2 == NULL || pat3 == NULL)
17858 goto theend;
17859 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17860 if (*mpat == NUL)
17861 STRCPY(pat3, pat2);
17862 else
17863 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17864 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017865 if (flags & SP_START)
17866 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017867
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 save_cursor = curwin->w_cursor;
17869 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017870 clearpos(&firstpos);
17871 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 pat = pat3;
17873 for (;;)
17874 {
17875 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017876 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17878 /* didn't find it or found the first match again: FAIL */
17879 break;
17880
17881 if (firstpos.lnum == 0)
17882 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017883 if (equalpos(pos, foundpos))
17884 {
17885 /* Found the same position again. Can happen with a pattern that
17886 * has "\zs" at the end and searching backwards. Advance one
17887 * character and try again. */
17888 if (dir == BACKWARD)
17889 decl(&pos);
17890 else
17891 incl(&pos);
17892 }
17893 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017895 /* clear the start flag to avoid getting stuck here */
17896 options &= ~SEARCH_START;
17897
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 /* If the skip pattern matches, ignore this match. */
17899 if (*skip != NUL)
17900 {
17901 save_pos = curwin->w_cursor;
17902 curwin->w_cursor = pos;
17903 r = eval_to_bool(skip, &err, NULL, FALSE);
17904 curwin->w_cursor = save_pos;
17905 if (err)
17906 {
17907 /* Evaluating {skip} caused an error, break here. */
17908 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017909 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910 break;
17911 }
17912 if (r)
17913 continue;
17914 }
17915
17916 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17917 {
17918 /* Found end when searching backwards or start when searching
17919 * forward: nested pair. */
17920 ++nest;
17921 pat = pat2; /* nested, don't search for middle */
17922 }
17923 else
17924 {
17925 /* Found end when searching forward or start when searching
17926 * backward: end of (nested) pair; or found middle in outer pair. */
17927 if (--nest == 1)
17928 pat = pat3; /* outer level, search for middle */
17929 }
17930
17931 if (nest == 0)
17932 {
17933 /* Found the match: return matchcount or line number. */
17934 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017935 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017937 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017938 if (flags & SP_SETPCMARK)
17939 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 curwin->w_cursor = pos;
17941 if (!(flags & SP_REPEAT))
17942 break;
17943 nest = 1; /* search for next unmatched */
17944 }
17945 }
17946
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017947 if (match_pos != NULL)
17948 {
17949 /* Store the match cursor position */
17950 match_pos->lnum = curwin->w_cursor.lnum;
17951 match_pos->col = curwin->w_cursor.col + 1;
17952 }
17953
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017955 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956 curwin->w_cursor = save_cursor;
17957
17958theend:
17959 vim_free(pat2);
17960 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017961 if (p_cpo == empty_option)
17962 p_cpo = save_cpo;
17963 else
17964 /* Darn, evaluating the {skip} expression changed the value. */
17965 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017966
17967 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968}
17969
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017970/*
17971 * "searchpos()" function
17972 */
17973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017974f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017975{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017976 pos_T match_pos;
17977 int lnum = 0;
17978 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017979 int n;
17980 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017981
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017982 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017983 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017984
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017985 n = search_cmn(argvars, &match_pos, &flags);
17986 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017987 {
17988 lnum = match_pos.lnum;
17989 col = match_pos.col;
17990 }
17991
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017992 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17993 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017994 if (flags & SP_SUBPAT)
17995 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017996}
17997
Bram Moolenaar0d660222005-01-07 21:51:51 +000017998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017999f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018001#ifdef FEAT_CLIENTSERVER
18002 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018003 char_u *server = get_tv_string_chk(&argvars[0]);
18004 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005
Bram Moolenaar0d660222005-01-07 21:51:51 +000018006 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018007 if (server == NULL || reply == NULL)
18008 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018009 if (check_restricted() || check_secure())
18010 return;
18011# ifdef FEAT_X11
18012 if (check_connection() == FAIL)
18013 return;
18014# endif
18015
18016 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018018 EMSG(_("E258: Unable to send to client"));
18019 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018021 rettv->vval.v_number = 0;
18022#else
18023 rettv->vval.v_number = -1;
18024#endif
18025}
18026
Bram Moolenaar0d660222005-01-07 21:51:51 +000018027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018028f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018029{
18030 char_u *r = NULL;
18031
18032#ifdef FEAT_CLIENTSERVER
18033# ifdef WIN32
18034 r = serverGetVimNames();
18035# else
18036 make_connection();
18037 if (X_DISPLAY != NULL)
18038 r = serverGetVimNames(X_DISPLAY);
18039# endif
18040#endif
18041 rettv->v_type = VAR_STRING;
18042 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043}
18044
18045/*
18046 * "setbufvar()" function
18047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018049f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050{
18051 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018054 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 char_u nbuf[NUMBUFLEN];
18056
18057 if (check_restricted() || check_secure())
18058 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018059 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18060 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018061 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 varp = &argvars[2];
18063
18064 if (buf != NULL && varname != NULL && varp != NULL)
18065 {
18066 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068
18069 if (*varname == '&')
18070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018071 long numval;
18072 char_u *strval;
18073 int error = FALSE;
18074
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018076 numval = get_tv_number_chk(varp, &error);
18077 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018078 if (!error && strval != NULL)
18079 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 }
18081 else
18082 {
18083 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18084 if (bufvarname != NULL)
18085 {
18086 STRCPY(bufvarname, "b:");
18087 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018088 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 vim_free(bufvarname);
18090 }
18091 }
18092
18093 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096}
18097
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018099f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018100{
18101 dict_T *d;
18102 dictitem_T *di;
18103 char_u *csearch;
18104
18105 if (argvars[0].v_type != VAR_DICT)
18106 {
18107 EMSG(_(e_dictreq));
18108 return;
18109 }
18110
18111 if ((d = argvars[0].vval.v_dict) != NULL)
18112 {
18113 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18114 if (csearch != NULL)
18115 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018116#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018117 if (enc_utf8)
18118 {
18119 int pcc[MAX_MCO];
18120 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018121
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018122 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18123 }
18124 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018125#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018126 set_last_csearch(PTR2CHAR(csearch),
18127 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018128 }
18129
18130 di = dict_find(d, (char_u *)"forward", -1);
18131 if (di != NULL)
18132 set_csearch_direction(get_tv_number(&di->di_tv)
18133 ? FORWARD : BACKWARD);
18134
18135 di = dict_find(d, (char_u *)"until", -1);
18136 if (di != NULL)
18137 set_csearch_until(!!get_tv_number(&di->di_tv));
18138 }
18139}
18140
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141/*
18142 * "setcmdpos()" function
18143 */
18144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018145f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018147 int pos = (int)get_tv_number(&argvars[0]) - 1;
18148
18149 if (pos >= 0)
18150 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151}
18152
18153/*
18154 * "setline()" function
18155 */
18156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018157f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158{
18159 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018160 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018161 list_T *l = NULL;
18162 listitem_T *li = NULL;
18163 long added = 0;
18164 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018166 lnum = get_tv_lnum(&argvars[0]);
18167 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018169 l = argvars[1].vval.v_list;
18170 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018172 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018173 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018174
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018175 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018176 for (;;)
18177 {
18178 if (l != NULL)
18179 {
18180 /* list argument, get next string */
18181 if (li == NULL)
18182 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018183 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018184 li = li->li_next;
18185 }
18186
18187 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018188 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018189 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018190
18191 /* When coming here from Insert mode, sync undo, so that this can be
18192 * undone separately from what was previously inserted. */
18193 if (u_sync_once == 2)
18194 {
18195 u_sync_once = 1; /* notify that u_sync() was called */
18196 u_sync(TRUE);
18197 }
18198
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018199 if (lnum <= curbuf->b_ml.ml_line_count)
18200 {
18201 /* existing line, replace it */
18202 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18203 {
18204 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018205 if (lnum == curwin->w_cursor.lnum)
18206 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018207 rettv->vval.v_number = 0; /* OK */
18208 }
18209 }
18210 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18211 {
18212 /* lnum is one past the last line, append the line */
18213 ++added;
18214 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18215 rettv->vval.v_number = 0; /* OK */
18216 }
18217
18218 if (l == NULL) /* only one string argument */
18219 break;
18220 ++lnum;
18221 }
18222
18223 if (added > 0)
18224 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225}
18226
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018227static 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 +000018228
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018230 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018231 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018233set_qf_ll_list(
18234 win_T *wp UNUSED,
18235 typval_T *list_arg UNUSED,
18236 typval_T *action_arg UNUSED,
18237 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018238{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018239#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018240 char_u *act;
18241 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018242#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018243
Bram Moolenaar2641f772005-03-25 21:58:17 +000018244 rettv->vval.v_number = -1;
18245
18246#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018247 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018248 EMSG(_(e_listreq));
18249 else
18250 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018251 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018252
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018253 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018254 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018255 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018256 if (act == NULL)
18257 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018258 if (*act == 'a' || *act == 'r')
18259 action = *act;
18260 }
18261
Bram Moolenaar81484f42012-12-05 15:16:47 +010018262 if (l != NULL && set_errorlist(wp, l, action,
18263 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018264 rettv->vval.v_number = 0;
18265 }
18266#endif
18267}
18268
18269/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018270 * "setloclist()" function
18271 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018273f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018274{
18275 win_T *win;
18276
18277 rettv->vval.v_number = -1;
18278
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018279 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018280 if (win != NULL)
18281 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18282}
18283
18284/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018285 * "setmatches()" function
18286 */
18287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018288f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018289{
18290#ifdef FEAT_SEARCH_EXTRA
18291 list_T *l;
18292 listitem_T *li;
18293 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018294 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018295
18296 rettv->vval.v_number = -1;
18297 if (argvars[0].v_type != VAR_LIST)
18298 {
18299 EMSG(_(e_listreq));
18300 return;
18301 }
18302 if ((l = argvars[0].vval.v_list) != NULL)
18303 {
18304
18305 /* To some extent make sure that we are dealing with a list from
18306 * "getmatches()". */
18307 li = l->lv_first;
18308 while (li != NULL)
18309 {
18310 if (li->li_tv.v_type != VAR_DICT
18311 || (d = li->li_tv.vval.v_dict) == NULL)
18312 {
18313 EMSG(_(e_invarg));
18314 return;
18315 }
18316 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018317 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18318 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018319 && dict_find(d, (char_u *)"priority", -1) != NULL
18320 && dict_find(d, (char_u *)"id", -1) != NULL))
18321 {
18322 EMSG(_(e_invarg));
18323 return;
18324 }
18325 li = li->li_next;
18326 }
18327
18328 clear_matches(curwin);
18329 li = l->lv_first;
18330 while (li != NULL)
18331 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018332 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018333 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018334 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018335 char_u *group;
18336 int priority;
18337 int id;
18338 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018339
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018340 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018341 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18342 {
18343 if (s == NULL)
18344 {
18345 s = list_alloc();
18346 if (s == NULL)
18347 return;
18348 }
18349
18350 /* match from matchaddpos() */
18351 for (i = 1; i < 9; i++)
18352 {
18353 sprintf((char *)buf, (char *)"pos%d", i);
18354 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18355 {
18356 if (di->di_tv.v_type != VAR_LIST)
18357 return;
18358
18359 list_append_tv(s, &di->di_tv);
18360 s->lv_refcount++;
18361 }
18362 else
18363 break;
18364 }
18365 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018366
18367 group = get_dict_string(d, (char_u *)"group", FALSE);
18368 priority = (int)get_dict_number(d, (char_u *)"priority");
18369 id = (int)get_dict_number(d, (char_u *)"id");
18370 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18371 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18372 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018373 if (i == 0)
18374 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018375 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018376 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018377 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018378 }
18379 else
18380 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018381 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018382 list_unref(s);
18383 s = NULL;
18384 }
18385
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018386 li = li->li_next;
18387 }
18388 rettv->vval.v_number = 0;
18389 }
18390#endif
18391}
18392
18393/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018394 * "setpos()" function
18395 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018397f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018398{
18399 pos_T pos;
18400 int fnum;
18401 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018402 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018403
Bram Moolenaar08250432008-02-13 11:42:46 +000018404 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018405 name = get_tv_string_chk(argvars);
18406 if (name != NULL)
18407 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018408 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018409 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018410 if (--pos.col < 0)
18411 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018412 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018413 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018414 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018415 if (fnum == curbuf->b_fnum)
18416 {
18417 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018418 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018419 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018420 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018421 curwin->w_set_curswant = FALSE;
18422 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018423 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018424 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018425 }
18426 else
18427 EMSG(_(e_invarg));
18428 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018429 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18430 {
18431 /* set mark */
18432 if (setmark_pos(name[1], &pos, fnum) == OK)
18433 rettv->vval.v_number = 0;
18434 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018435 else
18436 EMSG(_(e_invarg));
18437 }
18438 }
18439}
18440
18441/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018442 * "setqflist()" function
18443 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018445f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018446{
18447 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18448}
18449
18450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451 * "setreg()" function
18452 */
18453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018454f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455{
18456 int regname;
18457 char_u *strregname;
18458 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018459 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460 int append;
18461 char_u yank_type;
18462 long block_len;
18463
18464 block_len = -1;
18465 yank_type = MAUTO;
18466 append = FALSE;
18467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018468 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018469 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018471 if (strregname == NULL)
18472 return; /* type error; errmsg already given */
18473 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474 if (regname == 0 || regname == '@')
18475 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018477 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018479 stropt = get_tv_string_chk(&argvars[2]);
18480 if (stropt == NULL)
18481 return; /* type error */
18482 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 switch (*stropt)
18484 {
18485 case 'a': case 'A': /* append */
18486 append = TRUE;
18487 break;
18488 case 'v': case 'c': /* character-wise selection */
18489 yank_type = MCHAR;
18490 break;
18491 case 'V': case 'l': /* line-wise selection */
18492 yank_type = MLINE;
18493 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494 case 'b': case Ctrl_V: /* block-wise selection */
18495 yank_type = MBLOCK;
18496 if (VIM_ISDIGIT(stropt[1]))
18497 {
18498 ++stropt;
18499 block_len = getdigits(&stropt) - 1;
18500 --stropt;
18501 }
18502 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503 }
18504 }
18505
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018506 if (argvars[1].v_type == VAR_LIST)
18507 {
18508 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018509 char_u **allocval;
18510 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018511 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018512 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018513 int len = argvars[1].vval.v_list->lv_len;
18514 listitem_T *li;
18515
Bram Moolenaar7d647822014-04-05 21:28:56 +020018516 /* First half: use for pointers to result lines; second half: use for
18517 * pointers to allocated copies. */
18518 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018519 if (lstval == NULL)
18520 return;
18521 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018522 allocval = lstval + len + 2;
18523 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018524
18525 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18526 li = li->li_next)
18527 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018528 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018529 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018530 goto free_lstval;
18531 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018532 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018533 /* Need to make a copy, next get_tv_string_buf_chk() will
18534 * overwrite the string. */
18535 strval = vim_strsave(buf);
18536 if (strval == NULL)
18537 goto free_lstval;
18538 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018539 }
18540 *curval++ = strval;
18541 }
18542 *curval++ = NULL;
18543
18544 write_reg_contents_lst(regname, lstval, -1,
18545 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018546free_lstval:
18547 while (curallocval > allocval)
18548 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018549 vim_free(lstval);
18550 }
18551 else
18552 {
18553 strval = get_tv_string_chk(&argvars[1]);
18554 if (strval == NULL)
18555 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018556 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018558 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018559 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560}
18561
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018562/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018563 * "settabvar()" function
18564 */
18565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018566f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018567{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018568#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018569 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018570 tabpage_T *tp;
18571#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018572 char_u *varname, *tabvarname;
18573 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018574
18575 rettv->vval.v_number = 0;
18576
18577 if (check_restricted() || check_secure())
18578 return;
18579
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018580#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018581 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018582#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018583 varname = get_tv_string_chk(&argvars[1]);
18584 varp = &argvars[2];
18585
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018586 if (varname != NULL && varp != NULL
18587#ifdef FEAT_WINDOWS
18588 && tp != NULL
18589#endif
18590 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018591 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018592#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018593 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018594 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018595#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018596
18597 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18598 if (tabvarname != NULL)
18599 {
18600 STRCPY(tabvarname, "t:");
18601 STRCPY(tabvarname + 2, varname);
18602 set_var(tabvarname, varp, TRUE);
18603 vim_free(tabvarname);
18604 }
18605
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018606#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018607 /* Restore current tabpage */
18608 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018609 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018610#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018611 }
18612}
18613
18614/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018615 * "settabwinvar()" function
18616 */
18617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018618f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018619{
18620 setwinvar(argvars, rettv, 1);
18621}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622
18623/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018624 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018627f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018629 setwinvar(argvars, rettv, 0);
18630}
18631
18632/*
18633 * "setwinvar()" and "settabwinvar()" functions
18634 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018635
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018637setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018638{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639 win_T *win;
18640#ifdef FEAT_WINDOWS
18641 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018642 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018643 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644#endif
18645 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018646 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018648 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649
18650 if (check_restricted() || check_secure())
18651 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018652
18653#ifdef FEAT_WINDOWS
18654 if (off == 1)
18655 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18656 else
18657 tp = curtab;
18658#endif
18659 win = find_win_by_nr(&argvars[off], tp);
18660 varname = get_tv_string_chk(&argvars[off + 1]);
18661 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662
18663 if (win != NULL && varname != NULL && varp != NULL)
18664 {
18665#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018666 need_switch_win = !(tp == curtab && win == curwin);
18667 if (!need_switch_win
18668 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018671 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018673 long numval;
18674 char_u *strval;
18675 int error = FALSE;
18676
18677 ++varname;
18678 numval = get_tv_number_chk(varp, &error);
18679 strval = get_tv_string_buf_chk(varp, nbuf);
18680 if (!error && strval != NULL)
18681 set_option_value(varname, numval, strval, OPT_LOCAL);
18682 }
18683 else
18684 {
18685 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18686 if (winvarname != NULL)
18687 {
18688 STRCPY(winvarname, "w:");
18689 STRCPY(winvarname + 2, varname);
18690 set_var(winvarname, varp, TRUE);
18691 vim_free(winvarname);
18692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693 }
18694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018696 if (need_switch_win)
18697 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698#endif
18699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700}
18701
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018702#ifdef FEAT_CRYPT
18703/*
18704 * "sha256({string})" function
18705 */
18706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018707f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018708{
18709 char_u *p;
18710
18711 p = get_tv_string(&argvars[0]);
18712 rettv->vval.v_string = vim_strsave(
18713 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18714 rettv->v_type = VAR_STRING;
18715}
18716#endif /* FEAT_CRYPT */
18717
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018719 * "shellescape({string})" function
18720 */
18721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018722f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018723{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018724 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018725 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018726 rettv->v_type = VAR_STRING;
18727}
18728
18729/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018730 * shiftwidth() function
18731 */
18732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018733f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018734{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018735 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018736}
18737
18738/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018739 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740 */
18741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018742f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018744 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745
Bram Moolenaar0d660222005-01-07 21:51:51 +000018746 p = get_tv_string(&argvars[0]);
18747 rettv->vval.v_string = vim_strsave(p);
18748 simplify_filename(rettv->vval.v_string); /* simplify in place */
18749 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750}
18751
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018752#ifdef FEAT_FLOAT
18753/*
18754 * "sin()" function
18755 */
18756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018757f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018758{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018759 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018760
18761 rettv->v_type = VAR_FLOAT;
18762 if (get_float_arg(argvars, &f) == OK)
18763 rettv->vval.v_float = sin(f);
18764 else
18765 rettv->vval.v_float = 0.0;
18766}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018767
18768/*
18769 * "sinh()" function
18770 */
18771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018772f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018773{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018774 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018775
18776 rettv->v_type = VAR_FLOAT;
18777 if (get_float_arg(argvars, &f) == OK)
18778 rettv->vval.v_float = sinh(f);
18779 else
18780 rettv->vval.v_float = 0.0;
18781}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018782#endif
18783
Bram Moolenaar0d660222005-01-07 21:51:51 +000018784static int
18785#ifdef __BORLANDC__
18786 _RTLENTRYF
18787#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018788 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018789static int
18790#ifdef __BORLANDC__
18791 _RTLENTRYF
18792#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018793 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018794
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018795/* struct used in the array that's given to qsort() */
18796typedef struct
18797{
18798 listitem_T *item;
18799 int idx;
18800} sortItem_T;
18801
Bram Moolenaar0b962472016-02-22 22:51:33 +010018802/* struct storing information about current sort */
18803typedef struct
18804{
18805 int item_compare_ic;
18806 int item_compare_numeric;
18807 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018808#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018809 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018810#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018811 char_u *item_compare_func;
18812 dict_T *item_compare_selfdict;
18813 int item_compare_func_err;
18814 int item_compare_keep_zero;
18815} sortinfo_T;
18816static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018817static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018818#define ITEM_COMPARE_FAIL 999
18819
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018821 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018823 static int
18824#ifdef __BORLANDC__
18825_RTLENTRYF
18826#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018827item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018829 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018830 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018831 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018832 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018833 int res;
18834 char_u numbuf1[NUMBUFLEN];
18835 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018837 si1 = (sortItem_T *)s1;
18838 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018839 tv1 = &si1->item->li_tv;
18840 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018841
Bram Moolenaar0b962472016-02-22 22:51:33 +010018842 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018843 {
18844 long v1 = get_tv_number(tv1);
18845 long v2 = get_tv_number(tv2);
18846
18847 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18848 }
18849
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018850#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018851 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018852 {
18853 float_T v1 = get_tv_float(tv1);
18854 float_T v2 = get_tv_float(tv2);
18855
18856 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18857 }
18858#endif
18859
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018860 /* tv2string() puts quotes around a string and allocates memory. Don't do
18861 * that for string variables. Use a single quote when comparing with a
18862 * non-string to do what the docs promise. */
18863 if (tv1->v_type == VAR_STRING)
18864 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018865 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018866 p1 = (char_u *)"'";
18867 else
18868 p1 = tv1->vval.v_string;
18869 }
18870 else
18871 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18872 if (tv2->v_type == VAR_STRING)
18873 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018874 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018875 p2 = (char_u *)"'";
18876 else
18877 p2 = tv2->vval.v_string;
18878 }
18879 else
18880 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018881 if (p1 == NULL)
18882 p1 = (char_u *)"";
18883 if (p2 == NULL)
18884 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018885 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018886 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018887 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018888 res = STRICMP(p1, p2);
18889 else
18890 res = STRCMP(p1, p2);
18891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018893 {
18894 double n1, n2;
18895 n1 = strtod((char *)p1, (char **)&p1);
18896 n2 = strtod((char *)p2, (char **)&p2);
18897 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18898 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018899
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018900 /* When the result would be zero, compare the item indexes. Makes the
18901 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018902 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018903 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018904
Bram Moolenaar0d660222005-01-07 21:51:51 +000018905 vim_free(tofree1);
18906 vim_free(tofree2);
18907 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908}
18909
18910 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018911#ifdef __BORLANDC__
18912_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018914item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018915{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018916 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018917 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018918 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018919 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018920 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018922 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018923 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018924 return 0;
18925
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018926 si1 = (sortItem_T *)s1;
18927 si2 = (sortItem_T *)s2;
18928
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018929 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018930 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018931 copy_tv(&si1->item->li_tv, &argv[0]);
18932 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018933
18934 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018935 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010018936 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018937 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010018938 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018939 clear_tv(&argv[0]);
18940 clear_tv(&argv[1]);
18941
18942 if (res == FAIL)
18943 res = ITEM_COMPARE_FAIL;
18944 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018945 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18946 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018947 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018948 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018949
18950 /* When the result would be zero, compare the pointers themselves. Makes
18951 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018952 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018953 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018954
Bram Moolenaar0d660222005-01-07 21:51:51 +000018955 return res;
18956}
18957
18958/*
18959 * "sort({list})" function
18960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018962do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963{
Bram Moolenaar33570922005-01-25 22:26:29 +000018964 list_T *l;
18965 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018966 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018967 sortinfo_T *old_sortinfo;
18968 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018969 long len;
18970 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971
Bram Moolenaar0b962472016-02-22 22:51:33 +010018972 /* Pointer to current info struct used in compare function. Save and
18973 * restore the current one for nested calls. */
18974 old_sortinfo = sortinfo;
18975 sortinfo = &info;
18976
Bram Moolenaar0d660222005-01-07 21:51:51 +000018977 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018978 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 else
18980 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018981 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018982 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018983 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18984 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018985 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018986 rettv->vval.v_list = l;
18987 rettv->v_type = VAR_LIST;
18988 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989
Bram Moolenaar0d660222005-01-07 21:51:51 +000018990 len = list_len(l);
18991 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018992 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993
Bram Moolenaar0b962472016-02-22 22:51:33 +010018994 info.item_compare_ic = FALSE;
18995 info.item_compare_numeric = FALSE;
18996 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018997#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018998 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018999#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019000 info.item_compare_func = NULL;
19001 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019002 if (argvars[1].v_type != VAR_UNKNOWN)
19003 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019004 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019005 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019006 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019007 else
19008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019009 int error = FALSE;
19010
19011 i = get_tv_number_chk(&argvars[1], &error);
19012 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019013 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019014 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019015 info.item_compare_ic = TRUE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019016 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019017 info.item_compare_func = get_tv_string(&argvars[1]);
19018 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019019 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019020 if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019021 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019022 info.item_compare_func = NULL;
19023 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019024 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019025 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019026 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019027 info.item_compare_func = NULL;
19028 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019029 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019030#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019031 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019032 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019033 info.item_compare_func = NULL;
19034 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019035 }
19036#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019037 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019038 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019039 info.item_compare_func = NULL;
19040 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019041 }
19042 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019043 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019044
19045 if (argvars[2].v_type != VAR_UNKNOWN)
19046 {
19047 /* optional third argument: {dict} */
19048 if (argvars[2].v_type != VAR_DICT)
19049 {
19050 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019051 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019052 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019053 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019054 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056
Bram Moolenaar0d660222005-01-07 21:51:51 +000019057 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019058 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019059 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019060 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061
Bram Moolenaar327aa022014-03-25 18:24:23 +010019062 i = 0;
19063 if (sort)
19064 {
19065 /* sort(): ptrs will be the list to sort */
19066 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019067 {
19068 ptrs[i].item = li;
19069 ptrs[i].idx = i;
19070 ++i;
19071 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019072
Bram Moolenaar0b962472016-02-22 22:51:33 +010019073 info.item_compare_func_err = FALSE;
19074 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019075 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019076 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019077 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019078 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019079 EMSG(_("E702: Sort compare function failed"));
19080 else
19081 {
19082 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019083 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019084 info.item_compare_func == NULL
19085 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019086
Bram Moolenaar0b962472016-02-22 22:51:33 +010019087 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019088 {
19089 /* Clear the List and append the items in sorted order. */
19090 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19091 l->lv_len = 0;
19092 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019093 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019094 }
19095 }
19096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019098 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019099 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019100
19101 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019102 info.item_compare_func_err = FALSE;
19103 info.item_compare_keep_zero = TRUE;
19104 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019105 ? item_compare2 : item_compare;
19106
19107 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19108 li = li->li_next)
19109 {
19110 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19111 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019112 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019113 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019114 {
19115 EMSG(_("E882: Uniq compare function failed"));
19116 break;
19117 }
19118 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019119
Bram Moolenaar0b962472016-02-22 22:51:33 +010019120 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019121 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019122 while (--i >= 0)
19123 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019124 li = ptrs[i].item->li_next;
19125 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019126 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019127 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019128 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019129 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019130 list_fix_watch(l, li);
19131 listitem_free(li);
19132 l->lv_len--;
19133 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019134 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019135 }
19136
19137 vim_free(ptrs);
19138 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019139theend:
19140 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019141}
19142
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019143/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019144 * "sort({list})" function
19145 */
19146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019147f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019148{
19149 do_sort_uniq(argvars, rettv, TRUE);
19150}
19151
19152/*
19153 * "uniq({list})" function
19154 */
19155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019156f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019157{
19158 do_sort_uniq(argvars, rettv, FALSE);
19159}
19160
19161/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019162 * "soundfold({word})" function
19163 */
19164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019165f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019166{
19167 char_u *s;
19168
19169 rettv->v_type = VAR_STRING;
19170 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019171#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019172 rettv->vval.v_string = eval_soundfold(s);
19173#else
19174 rettv->vval.v_string = vim_strsave(s);
19175#endif
19176}
19177
19178/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019179 * "spellbadword()" function
19180 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019182f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019183{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019184 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019185 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019186 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019187
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019188 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019189 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019190
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019191#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019192 if (argvars[0].v_type == VAR_UNKNOWN)
19193 {
19194 /* Find the start and length of the badly spelled word. */
19195 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19196 if (len != 0)
19197 word = ml_get_cursor();
19198 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019199 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019200 {
19201 char_u *str = get_tv_string_chk(&argvars[0]);
19202 int capcol = -1;
19203
19204 if (str != NULL)
19205 {
19206 /* Check the argument for spelling. */
19207 while (*str != NUL)
19208 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019209 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019210 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019211 {
19212 word = str;
19213 break;
19214 }
19215 str += len;
19216 }
19217 }
19218 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019219#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019220
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019221 list_append_string(rettv->vval.v_list, word, len);
19222 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019223 attr == HLF_SPB ? "bad" :
19224 attr == HLF_SPR ? "rare" :
19225 attr == HLF_SPL ? "local" :
19226 attr == HLF_SPC ? "caps" :
19227 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019228}
19229
19230/*
19231 * "spellsuggest()" function
19232 */
19233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019234f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019235{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019236#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019237 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019238 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019239 int maxcount;
19240 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019241 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019242 listitem_T *li;
19243 int need_capital = FALSE;
19244#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019245
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019246 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019247 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019248
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019249#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019250 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019251 {
19252 str = get_tv_string(&argvars[0]);
19253 if (argvars[1].v_type != VAR_UNKNOWN)
19254 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019255 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019256 if (maxcount <= 0)
19257 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019258 if (argvars[2].v_type != VAR_UNKNOWN)
19259 {
19260 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19261 if (typeerr)
19262 return;
19263 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019264 }
19265 else
19266 maxcount = 25;
19267
Bram Moolenaar4770d092006-01-12 23:22:24 +000019268 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019269
19270 for (i = 0; i < ga.ga_len; ++i)
19271 {
19272 str = ((char_u **)ga.ga_data)[i];
19273
19274 li = listitem_alloc();
19275 if (li == NULL)
19276 vim_free(str);
19277 else
19278 {
19279 li->li_tv.v_type = VAR_STRING;
19280 li->li_tv.v_lock = 0;
19281 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019282 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019283 }
19284 }
19285 ga_clear(&ga);
19286 }
19287#endif
19288}
19289
Bram Moolenaar0d660222005-01-07 21:51:51 +000019290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019291f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019292{
19293 char_u *str;
19294 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019295 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019296 regmatch_T regmatch;
19297 char_u patbuf[NUMBUFLEN];
19298 char_u *save_cpo;
19299 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019300 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019301 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019302 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019303
19304 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19305 save_cpo = p_cpo;
19306 p_cpo = (char_u *)"";
19307
19308 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019309 if (argvars[1].v_type != VAR_UNKNOWN)
19310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019311 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19312 if (pat == NULL)
19313 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019314 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019315 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019316 }
19317 if (pat == NULL || *pat == NUL)
19318 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019319
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019320 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019322 if (typeerr)
19323 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324
Bram Moolenaar0d660222005-01-07 21:51:51 +000019325 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19326 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019328 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019329 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019330 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019331 if (*str == NUL)
19332 match = FALSE; /* empty item at the end */
19333 else
19334 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019335 if (match)
19336 end = regmatch.startp[0];
19337 else
19338 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019339 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19340 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019341 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019342 if (list_append_string(rettv->vval.v_list, str,
19343 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019344 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019345 }
19346 if (!match)
19347 break;
19348 /* Advance to just after the match. */
19349 if (regmatch.endp[0] > str)
19350 col = 0;
19351 else
19352 {
19353 /* Don't get stuck at the same match. */
19354#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019355 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019356#else
19357 col = 1;
19358#endif
19359 }
19360 str = regmatch.endp[0];
19361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362
Bram Moolenaar473de612013-06-08 18:19:48 +020019363 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365
Bram Moolenaar0d660222005-01-07 21:51:51 +000019366 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367}
19368
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019369#ifdef FEAT_FLOAT
19370/*
19371 * "sqrt()" function
19372 */
19373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019374f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019375{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019376 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019377
19378 rettv->v_type = VAR_FLOAT;
19379 if (get_float_arg(argvars, &f) == OK)
19380 rettv->vval.v_float = sqrt(f);
19381 else
19382 rettv->vval.v_float = 0.0;
19383}
19384
19385/*
19386 * "str2float()" function
19387 */
19388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019389f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019390{
19391 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19392
19393 if (*p == '+')
19394 p = skipwhite(p + 1);
19395 (void)string2float(p, &rettv->vval.v_float);
19396 rettv->v_type = VAR_FLOAT;
19397}
19398#endif
19399
Bram Moolenaar2c932302006-03-18 21:42:09 +000019400/*
19401 * "str2nr()" function
19402 */
19403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019404f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019405{
19406 int base = 10;
19407 char_u *p;
19408 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019409 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019410
19411 if (argvars[1].v_type != VAR_UNKNOWN)
19412 {
19413 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019414 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019415 {
19416 EMSG(_(e_invarg));
19417 return;
19418 }
19419 }
19420
19421 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019422 if (*p == '+')
19423 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019424 switch (base)
19425 {
19426 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19427 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19428 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19429 default: what = 0;
19430 }
19431 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019432 rettv->vval.v_number = n;
19433}
19434
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435#ifdef HAVE_STRFTIME
19436/*
19437 * "strftime({format}[, {time}])" function
19438 */
19439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019440f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441{
19442 char_u result_buf[256];
19443 struct tm *curtime;
19444 time_t seconds;
19445 char_u *p;
19446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019447 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019449 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019450 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 seconds = time(NULL);
19452 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019453 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 curtime = localtime(&seconds);
19455 /* MSVC returns NULL for an invalid value of seconds. */
19456 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019457 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 else
19459 {
19460# ifdef FEAT_MBYTE
19461 vimconv_T conv;
19462 char_u *enc;
19463
19464 conv.vc_type = CONV_NONE;
19465 enc = enc_locale();
19466 convert_setup(&conv, p_enc, enc);
19467 if (conv.vc_type != CONV_NONE)
19468 p = string_convert(&conv, p, NULL);
19469# endif
19470 if (p != NULL)
19471 (void)strftime((char *)result_buf, sizeof(result_buf),
19472 (char *)p, curtime);
19473 else
19474 result_buf[0] = NUL;
19475
19476# ifdef FEAT_MBYTE
19477 if (conv.vc_type != CONV_NONE)
19478 vim_free(p);
19479 convert_setup(&conv, enc, p_enc);
19480 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019481 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 else
19483# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019484 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485
19486# ifdef FEAT_MBYTE
19487 /* Release conversion descriptors */
19488 convert_setup(&conv, NULL, NULL);
19489 vim_free(enc);
19490# endif
19491 }
19492}
19493#endif
19494
19495/*
19496 * "stridx()" function
19497 */
19498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019499f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500{
19501 char_u buf[NUMBUFLEN];
19502 char_u *needle;
19503 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019504 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019506 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019508 needle = get_tv_string_chk(&argvars[1]);
19509 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019510 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019511 if (needle == NULL || haystack == NULL)
19512 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513
Bram Moolenaar33570922005-01-25 22:26:29 +000019514 if (argvars[2].v_type != VAR_UNKNOWN)
19515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019516 int error = FALSE;
19517
19518 start_idx = get_tv_number_chk(&argvars[2], &error);
19519 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019520 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019521 if (start_idx >= 0)
19522 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019523 }
19524
19525 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19526 if (pos != NULL)
19527 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528}
19529
19530/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019531 * "string()" function
19532 */
19533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019534f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019535{
19536 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019537 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019539 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019540 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019541 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019542 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019543 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544}
19545
19546/*
19547 * "strlen()" function
19548 */
19549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019550f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019552 rettv->vval.v_number = (varnumber_T)(STRLEN(
19553 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554}
19555
19556/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019557 * "strchars()" function
19558 */
19559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019560f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019561{
19562 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019563 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019564#ifdef FEAT_MBYTE
19565 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019566 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019567#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019568
19569 if (argvars[1].v_type != VAR_UNKNOWN)
19570 skipcc = get_tv_number_chk(&argvars[1], NULL);
19571 if (skipcc < 0 || skipcc > 1)
19572 EMSG(_(e_invarg));
19573 else
19574 {
19575#ifdef FEAT_MBYTE
19576 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19577 while (*s != NUL)
19578 {
19579 func_mb_ptr2char_adv(&s);
19580 ++len;
19581 }
19582 rettv->vval.v_number = len;
19583#else
19584 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19585#endif
19586 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019587}
19588
19589/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019590 * "strdisplaywidth()" function
19591 */
19592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019593f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019594{
19595 char_u *s = get_tv_string(&argvars[0]);
19596 int col = 0;
19597
19598 if (argvars[1].v_type != VAR_UNKNOWN)
19599 col = get_tv_number(&argvars[1]);
19600
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019601 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019602}
19603
19604/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019605 * "strwidth()" function
19606 */
19607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019608f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019609{
19610 char_u *s = get_tv_string(&argvars[0]);
19611
19612 rettv->vval.v_number = (varnumber_T)(
19613#ifdef FEAT_MBYTE
19614 mb_string2cells(s, -1)
19615#else
19616 STRLEN(s)
19617#endif
19618 );
19619}
19620
19621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 * "strpart()" function
19623 */
19624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019625f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626{
19627 char_u *p;
19628 int n;
19629 int len;
19630 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019631 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019633 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 slen = (int)STRLEN(p);
19635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019636 n = get_tv_number_chk(&argvars[1], &error);
19637 if (error)
19638 len = 0;
19639 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019640 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641 else
19642 len = slen - n; /* default len: all bytes that are available. */
19643
19644 /*
19645 * Only return the overlap between the specified part and the actual
19646 * string.
19647 */
19648 if (n < 0)
19649 {
19650 len += n;
19651 n = 0;
19652 }
19653 else if (n > slen)
19654 n = slen;
19655 if (len < 0)
19656 len = 0;
19657 else if (n + len > slen)
19658 len = slen - n;
19659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019660 rettv->v_type = VAR_STRING;
19661 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662}
19663
19664/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019665 * "strridx()" function
19666 */
19667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019668f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019669{
19670 char_u buf[NUMBUFLEN];
19671 char_u *needle;
19672 char_u *haystack;
19673 char_u *rest;
19674 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019675 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019677 needle = get_tv_string_chk(&argvars[1]);
19678 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019679
19680 rettv->vval.v_number = -1;
19681 if (needle == NULL || haystack == NULL)
19682 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019683
19684 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019685 if (argvars[2].v_type != VAR_UNKNOWN)
19686 {
19687 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019688 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019689 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019690 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019691 }
19692 else
19693 end_idx = haystack_len;
19694
Bram Moolenaar0d660222005-01-07 21:51:51 +000019695 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019696 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019697 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019698 lastmatch = haystack + end_idx;
19699 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019700 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019701 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019702 for (rest = haystack; *rest != '\0'; ++rest)
19703 {
19704 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019705 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019706 break;
19707 lastmatch = rest;
19708 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019709 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019710
19711 if (lastmatch == NULL)
19712 rettv->vval.v_number = -1;
19713 else
19714 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19715}
19716
19717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 * "strtrans()" function
19719 */
19720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019721f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019723 rettv->v_type = VAR_STRING;
19724 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725}
19726
19727/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019728 * "submatch()" function
19729 */
19730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019731f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019732{
Bram Moolenaar41571762014-04-02 19:00:58 +020019733 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019734 int no;
19735 int retList = 0;
19736
19737 no = (int)get_tv_number_chk(&argvars[0], &error);
19738 if (error)
19739 return;
19740 error = FALSE;
19741 if (argvars[1].v_type != VAR_UNKNOWN)
19742 retList = get_tv_number_chk(&argvars[1], &error);
19743 if (error)
19744 return;
19745
19746 if (retList == 0)
19747 {
19748 rettv->v_type = VAR_STRING;
19749 rettv->vval.v_string = reg_submatch(no);
19750 }
19751 else
19752 {
19753 rettv->v_type = VAR_LIST;
19754 rettv->vval.v_list = reg_submatch_list(no);
19755 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019756}
19757
19758/*
19759 * "substitute()" function
19760 */
19761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019762f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019763{
19764 char_u patbuf[NUMBUFLEN];
19765 char_u subbuf[NUMBUFLEN];
19766 char_u flagsbuf[NUMBUFLEN];
19767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019768 char_u *str = get_tv_string_chk(&argvars[0]);
19769 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19770 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19771 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19772
Bram Moolenaar0d660222005-01-07 21:51:51 +000019773 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019774 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19775 rettv->vval.v_string = NULL;
19776 else
19777 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019778}
19779
19780/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019781 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019784f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785{
19786 int id = 0;
19787#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019788 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 long col;
19790 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019791 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019793 lnum = get_tv_lnum(argvars); /* -1 on type error */
19794 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19795 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019797 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019798 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019799 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800#endif
19801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019802 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803}
19804
19805/*
19806 * "synIDattr(id, what [, mode])" function
19807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019809f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810{
19811 char_u *p = NULL;
19812#ifdef FEAT_SYN_HL
19813 int id;
19814 char_u *what;
19815 char_u *mode;
19816 char_u modebuf[NUMBUFLEN];
19817 int modec;
19818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019819 id = get_tv_number(&argvars[0]);
19820 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019821 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019823 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019825 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826 modec = 0; /* replace invalid with current */
19827 }
19828 else
19829 {
19830#ifdef FEAT_GUI
19831 if (gui.in_use)
19832 modec = 'g';
19833 else
19834#endif
19835 if (t_colors > 1)
19836 modec = 'c';
19837 else
19838 modec = 't';
19839 }
19840
19841
19842 switch (TOLOWER_ASC(what[0]))
19843 {
19844 case 'b':
19845 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19846 p = highlight_color(id, what, modec);
19847 else /* bold */
19848 p = highlight_has_attr(id, HL_BOLD, modec);
19849 break;
19850
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019851 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852 p = highlight_color(id, what, modec);
19853 break;
19854
19855 case 'i':
19856 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19857 p = highlight_has_attr(id, HL_INVERSE, modec);
19858 else /* italic */
19859 p = highlight_has_attr(id, HL_ITALIC, modec);
19860 break;
19861
19862 case 'n': /* name */
19863 p = get_highlight_name(NULL, id - 1);
19864 break;
19865
19866 case 'r': /* reverse */
19867 p = highlight_has_attr(id, HL_INVERSE, modec);
19868 break;
19869
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019870 case 's':
19871 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19872 p = highlight_color(id, what, modec);
19873 else /* standout */
19874 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 break;
19876
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019877 case 'u':
19878 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19879 /* underline */
19880 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19881 else
19882 /* undercurl */
19883 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884 break;
19885 }
19886
19887 if (p != NULL)
19888 p = vim_strsave(p);
19889#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019890 rettv->v_type = VAR_STRING;
19891 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892}
19893
19894/*
19895 * "synIDtrans(id)" function
19896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019898f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899{
19900 int id;
19901
19902#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019903 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904
19905 if (id > 0)
19906 id = syn_get_final_id(id);
19907 else
19908#endif
19909 id = 0;
19910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019911 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912}
19913
19914/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019915 * "synconcealed(lnum, col)" function
19916 */
19917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019918f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019919{
19920#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19921 long lnum;
19922 long col;
19923 int syntax_flags = 0;
19924 int cchar;
19925 int matchid = 0;
19926 char_u str[NUMBUFLEN];
19927#endif
19928
19929 rettv->v_type = VAR_LIST;
19930 rettv->vval.v_list = NULL;
19931
19932#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19933 lnum = get_tv_lnum(argvars); /* -1 on type error */
19934 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19935
19936 vim_memset(str, NUL, sizeof(str));
19937
19938 if (rettv_list_alloc(rettv) != FAIL)
19939 {
19940 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19941 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19942 && curwin->w_p_cole > 0)
19943 {
19944 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19945 syntax_flags = get_syntax_info(&matchid);
19946
19947 /* get the conceal character */
19948 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19949 {
19950 cchar = syn_get_sub_char();
19951 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19952 cchar = lcs_conceal;
19953 if (cchar != NUL)
19954 {
19955# ifdef FEAT_MBYTE
19956 if (has_mbyte)
19957 (*mb_char2bytes)(cchar, str);
19958 else
19959# endif
19960 str[0] = cchar;
19961 }
19962 }
19963 }
19964
19965 list_append_number(rettv->vval.v_list,
19966 (syntax_flags & HL_CONCEAL) != 0);
19967 /* -1 to auto-determine strlen */
19968 list_append_string(rettv->vval.v_list, str, -1);
19969 list_append_number(rettv->vval.v_list, matchid);
19970 }
19971#endif
19972}
19973
19974/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019975 * "synstack(lnum, col)" function
19976 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019978f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019979{
19980#ifdef FEAT_SYN_HL
19981 long lnum;
19982 long col;
19983 int i;
19984 int id;
19985#endif
19986
19987 rettv->v_type = VAR_LIST;
19988 rettv->vval.v_list = NULL;
19989
19990#ifdef FEAT_SYN_HL
19991 lnum = get_tv_lnum(argvars); /* -1 on type error */
19992 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19993
19994 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019995 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019996 && rettv_list_alloc(rettv) != FAIL)
19997 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019998 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019999 for (i = 0; ; ++i)
20000 {
20001 id = syn_get_stack_item(i);
20002 if (id < 0)
20003 break;
20004 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20005 break;
20006 }
20007 }
20008#endif
20009}
20010
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020012get_cmd_output_as_rettv(
20013 typval_T *argvars,
20014 typval_T *rettv,
20015 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020017 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020019 char_u *infile = NULL;
20020 char_u buf[NUMBUFLEN];
20021 int err = FALSE;
20022 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020023 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020024 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020026 rettv->v_type = VAR_STRING;
20027 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020028 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020029 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020030
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020031 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020032 {
20033 /*
20034 * Write the string to a temp file, to be used for input of the shell
20035 * command.
20036 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020037 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020038 {
20039 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020040 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020041 }
20042
20043 fd = mch_fopen((char *)infile, WRITEBIN);
20044 if (fd == NULL)
20045 {
20046 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020047 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020048 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020049 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020050 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020051 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20052 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020053 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020054 else
20055 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020056 size_t len;
20057
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020058 p = get_tv_string_buf_chk(&argvars[1], buf);
20059 if (p == NULL)
20060 {
20061 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020062 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020063 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020064 len = STRLEN(p);
20065 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020066 err = TRUE;
20067 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020068 if (fclose(fd) != 0)
20069 err = TRUE;
20070 if (err)
20071 {
20072 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020073 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020074 }
20075 }
20076
Bram Moolenaar52a72462014-08-29 15:53:52 +020020077 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20078 * echoes typeahead, that messes up the display. */
20079 if (!msg_silent)
20080 flags += SHELL_COOKED;
20081
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020082 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020084 int len;
20085 listitem_T *li;
20086 char_u *s = NULL;
20087 char_u *start;
20088 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020089 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090
Bram Moolenaar52a72462014-08-29 15:53:52 +020020091 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020092 if (res == NULL)
20093 goto errret;
20094
20095 list = list_alloc();
20096 if (list == NULL)
20097 goto errret;
20098
20099 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020101 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020102 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020103 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020104 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020105
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020106 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020107 if (s == NULL)
20108 goto errret;
20109
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020110 for (p = s; start < end; ++p, ++start)
20111 *p = *start == NUL ? NL : *start;
20112 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020113
20114 li = listitem_alloc();
20115 if (li == NULL)
20116 {
20117 vim_free(s);
20118 goto errret;
20119 }
20120 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020121 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020122 li->li_tv.vval.v_string = s;
20123 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020125
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020126 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020127 rettv->v_type = VAR_LIST;
20128 rettv->vval.v_list = list;
20129 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020131 else
20132 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020133 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020134#ifdef USE_CR
20135 /* translate <CR> into <NL> */
20136 if (res != NULL)
20137 {
20138 char_u *s;
20139
20140 for (s = res; *s; ++s)
20141 {
20142 if (*s == CAR)
20143 *s = NL;
20144 }
20145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146#else
20147# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020148 /* translate <CR><NL> into <NL> */
20149 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020151 char_u *s, *d;
20152
20153 d = res;
20154 for (s = res; *s; ++s)
20155 {
20156 if (s[0] == CAR && s[1] == NL)
20157 ++s;
20158 *d++ = *s;
20159 }
20160 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162# endif
20163#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020164 rettv->vval.v_string = res;
20165 res = NULL;
20166 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020167
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020168errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020169 if (infile != NULL)
20170 {
20171 mch_remove(infile);
20172 vim_free(infile);
20173 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020174 if (res != NULL)
20175 vim_free(res);
20176 if (list != NULL)
20177 list_free(list, TRUE);
20178}
20179
20180/*
20181 * "system()" function
20182 */
20183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020184f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020185{
20186 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20187}
20188
20189/*
20190 * "systemlist()" function
20191 */
20192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020193f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020194{
20195 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196}
20197
20198/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020199 * "tabpagebuflist()" function
20200 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020202f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020203{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020204#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020205 tabpage_T *tp;
20206 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020207
20208 if (argvars[0].v_type == VAR_UNKNOWN)
20209 wp = firstwin;
20210 else
20211 {
20212 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20213 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020214 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020215 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020216 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020217 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020218 for (; wp != NULL; wp = wp->w_next)
20219 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020220 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020221 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020222 }
20223#endif
20224}
20225
20226
20227/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020228 * "tabpagenr()" function
20229 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020231f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020232{
20233 int nr = 1;
20234#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020235 char_u *arg;
20236
20237 if (argvars[0].v_type != VAR_UNKNOWN)
20238 {
20239 arg = get_tv_string_chk(&argvars[0]);
20240 nr = 0;
20241 if (arg != NULL)
20242 {
20243 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020244 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020245 else
20246 EMSG2(_(e_invexpr2), arg);
20247 }
20248 }
20249 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020250 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020251#endif
20252 rettv->vval.v_number = nr;
20253}
20254
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020255
20256#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020257static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020258
20259/*
20260 * Common code for tabpagewinnr() and winnr().
20261 */
20262 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020263get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020264{
20265 win_T *twin;
20266 int nr = 1;
20267 win_T *wp;
20268 char_u *arg;
20269
20270 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20271 if (argvar->v_type != VAR_UNKNOWN)
20272 {
20273 arg = get_tv_string_chk(argvar);
20274 if (arg == NULL)
20275 nr = 0; /* type error; errmsg already given */
20276 else if (STRCMP(arg, "$") == 0)
20277 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20278 else if (STRCMP(arg, "#") == 0)
20279 {
20280 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20281 if (twin == NULL)
20282 nr = 0;
20283 }
20284 else
20285 {
20286 EMSG2(_(e_invexpr2), arg);
20287 nr = 0;
20288 }
20289 }
20290
20291 if (nr > 0)
20292 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20293 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020294 {
20295 if (wp == NULL)
20296 {
20297 /* didn't find it in this tabpage */
20298 nr = 0;
20299 break;
20300 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020301 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020302 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020303 return nr;
20304}
20305#endif
20306
20307/*
20308 * "tabpagewinnr()" function
20309 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020311f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020312{
20313 int nr = 1;
20314#ifdef FEAT_WINDOWS
20315 tabpage_T *tp;
20316
20317 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20318 if (tp == NULL)
20319 nr = 0;
20320 else
20321 nr = get_winnr(tp, &argvars[1]);
20322#endif
20323 rettv->vval.v_number = nr;
20324}
20325
20326
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020327/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020328 * "tagfiles()" function
20329 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020331f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020332{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020333 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020334 tagname_T tn;
20335 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020336
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020337 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020338 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020339 fname = alloc(MAXPATHL);
20340 if (fname == NULL)
20341 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020342
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020343 for (first = TRUE; ; first = FALSE)
20344 if (get_tagfname(&tn, first, fname) == FAIL
20345 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020346 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020347 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020348 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020349}
20350
20351/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020352 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020353 */
20354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020355f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020356{
20357 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020358
20359 tag_pattern = get_tv_string(&argvars[0]);
20360
20361 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020362 if (*tag_pattern == NUL)
20363 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020364
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020365 if (rettv_list_alloc(rettv) == OK)
20366 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020367}
20368
20369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 * "tempname()" function
20371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020373f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374{
20375 static int x = 'A';
20376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020377 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020378 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379
20380 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20381 * names. Skip 'I' and 'O', they are used for shell redirection. */
20382 do
20383 {
20384 if (x == 'Z')
20385 x = '0';
20386 else if (x == '9')
20387 x = 'A';
20388 else
20389 {
20390#ifdef EBCDIC
20391 if (x == 'I')
20392 x = 'J';
20393 else if (x == 'R')
20394 x = 'S';
20395 else
20396#endif
20397 ++x;
20398 }
20399 } while (x == 'I' || x == 'O');
20400}
20401
20402/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020403 * "test(list)" function: Just checking the walls...
20404 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020406f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020407{
20408 /* Used for unit testing. Change the code below to your liking. */
20409#if 0
20410 listitem_T *li;
20411 list_T *l;
20412 char_u *bad, *good;
20413
20414 if (argvars[0].v_type != VAR_LIST)
20415 return;
20416 l = argvars[0].vval.v_list;
20417 if (l == NULL)
20418 return;
20419 li = l->lv_first;
20420 if (li == NULL)
20421 return;
20422 bad = get_tv_string(&li->li_tv);
20423 li = li->li_next;
20424 if (li == NULL)
20425 return;
20426 good = get_tv_string(&li->li_tv);
20427 rettv->vval.v_number = test_edit_score(bad, good);
20428#endif
20429}
20430
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020431#ifdef FEAT_FLOAT
20432/*
20433 * "tan()" function
20434 */
20435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020436f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020437{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020438 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020439
20440 rettv->v_type = VAR_FLOAT;
20441 if (get_float_arg(argvars, &f) == OK)
20442 rettv->vval.v_float = tan(f);
20443 else
20444 rettv->vval.v_float = 0.0;
20445}
20446
20447/*
20448 * "tanh()" function
20449 */
20450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020451f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020452{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020453 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020454
20455 rettv->v_type = VAR_FLOAT;
20456 if (get_float_arg(argvars, &f) == OK)
20457 rettv->vval.v_float = tanh(f);
20458 else
20459 rettv->vval.v_float = 0.0;
20460}
20461#endif
20462
Bram Moolenaard52d9742005-08-21 22:20:28 +000020463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 * "tolower(string)" function
20465 */
20466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020467f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468{
20469 char_u *p;
20470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020471 p = vim_strsave(get_tv_string(&argvars[0]));
20472 rettv->v_type = VAR_STRING;
20473 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474
20475 if (p != NULL)
20476 while (*p != NUL)
20477 {
20478#ifdef FEAT_MBYTE
20479 int l;
20480
20481 if (enc_utf8)
20482 {
20483 int c, lc;
20484
20485 c = utf_ptr2char(p);
20486 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020487 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488 /* TODO: reallocate string when byte count changes. */
20489 if (utf_char2len(lc) == l)
20490 utf_char2bytes(lc, p);
20491 p += l;
20492 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020493 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 p += l; /* skip multi-byte character */
20495 else
20496#endif
20497 {
20498 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20499 ++p;
20500 }
20501 }
20502}
20503
20504/*
20505 * "toupper(string)" function
20506 */
20507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020508f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020510 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020511 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512}
20513
20514/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020515 * "tr(string, fromstr, tostr)" function
20516 */
20517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020518f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020519{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020520 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020521 char_u *fromstr;
20522 char_u *tostr;
20523 char_u *p;
20524#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020525 int inlen;
20526 int fromlen;
20527 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020528 int idx;
20529 char_u *cpstr;
20530 int cplen;
20531 int first = TRUE;
20532#endif
20533 char_u buf[NUMBUFLEN];
20534 char_u buf2[NUMBUFLEN];
20535 garray_T ga;
20536
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020537 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020538 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20539 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020540
20541 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020542 rettv->v_type = VAR_STRING;
20543 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020544 if (fromstr == NULL || tostr == NULL)
20545 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020546 ga_init2(&ga, (int)sizeof(char), 80);
20547
20548#ifdef FEAT_MBYTE
20549 if (!has_mbyte)
20550#endif
20551 /* not multi-byte: fromstr and tostr must be the same length */
20552 if (STRLEN(fromstr) != STRLEN(tostr))
20553 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020554#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020555error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020556#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020557 EMSG2(_(e_invarg2), fromstr);
20558 ga_clear(&ga);
20559 return;
20560 }
20561
20562 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020563 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020564 {
20565#ifdef FEAT_MBYTE
20566 if (has_mbyte)
20567 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020568 inlen = (*mb_ptr2len)(in_str);
20569 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020570 cplen = inlen;
20571 idx = 0;
20572 for (p = fromstr; *p != NUL; p += fromlen)
20573 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020574 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020575 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020576 {
20577 for (p = tostr; *p != NUL; p += tolen)
20578 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020579 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020580 if (idx-- == 0)
20581 {
20582 cplen = tolen;
20583 cpstr = p;
20584 break;
20585 }
20586 }
20587 if (*p == NUL) /* tostr is shorter than fromstr */
20588 goto error;
20589 break;
20590 }
20591 ++idx;
20592 }
20593
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020594 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020595 {
20596 /* Check that fromstr and tostr have the same number of
20597 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020598 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020599 first = FALSE;
20600 for (p = tostr; *p != NUL; p += tolen)
20601 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020602 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020603 --idx;
20604 }
20605 if (idx != 0)
20606 goto error;
20607 }
20608
Bram Moolenaarcde88542015-08-11 19:14:00 +020020609 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020610 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020611 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020612
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020613 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020614 }
20615 else
20616#endif
20617 {
20618 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020619 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020620 if (p != NULL)
20621 ga_append(&ga, tostr[p - fromstr]);
20622 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020623 ga_append(&ga, *in_str);
20624 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020625 }
20626 }
20627
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020628 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020629 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020630 ga_append(&ga, NUL);
20631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020632 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020633}
20634
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020635#ifdef FEAT_FLOAT
20636/*
20637 * "trunc({float})" function
20638 */
20639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020640f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020641{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020642 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020643
20644 rettv->v_type = VAR_FLOAT;
20645 if (get_float_arg(argvars, &f) == OK)
20646 /* trunc() is not in C90, use floor() or ceil() instead. */
20647 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20648 else
20649 rettv->vval.v_float = 0.0;
20650}
20651#endif
20652
Bram Moolenaar8299df92004-07-10 09:47:34 +000020653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654 * "type(expr)" function
20655 */
20656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020657f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020659 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020660
20661 switch (argvars[0].v_type)
20662 {
20663 case VAR_NUMBER: n = 0; break;
20664 case VAR_STRING: n = 1; break;
20665 case VAR_FUNC: n = 2; break;
20666 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020667 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020668 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020669 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020670 if (argvars[0].vval.v_number == VVAL_FALSE
20671 || argvars[0].vval.v_number == VVAL_TRUE)
20672 n = 6;
20673 else
20674 n = 7;
20675 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020676 case VAR_JOB: n = 8; break;
20677 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020678 case VAR_UNKNOWN:
20679 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20680 n = -1;
20681 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020682 }
20683 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684}
20685
20686/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020687 * "undofile(name)" function
20688 */
20689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020690f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020691{
20692 rettv->v_type = VAR_STRING;
20693#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020694 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020695 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020696
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020697 if (*fname == NUL)
20698 {
20699 /* If there is no file name there will be no undo file. */
20700 rettv->vval.v_string = NULL;
20701 }
20702 else
20703 {
20704 char_u *ffname = FullName_save(fname, FALSE);
20705
20706 if (ffname != NULL)
20707 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20708 vim_free(ffname);
20709 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020710 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020711#else
20712 rettv->vval.v_string = NULL;
20713#endif
20714}
20715
20716/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020717 * "undotree()" function
20718 */
20719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020720f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020721{
20722 if (rettv_dict_alloc(rettv) == OK)
20723 {
20724 dict_T *dict = rettv->vval.v_dict;
20725 list_T *list;
20726
Bram Moolenaar730cde92010-06-27 05:18:54 +020020727 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020728 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020729 dict_add_nr_str(dict, "save_last",
20730 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020731 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20732 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020733 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020734
20735 list = list_alloc();
20736 if (list != NULL)
20737 {
20738 u_eval_tree(curbuf->b_u_oldhead, list);
20739 dict_add_list(dict, "entries", list);
20740 }
20741 }
20742}
20743
20744/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020745 * "values(dict)" function
20746 */
20747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020748f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020749{
20750 dict_list(argvars, rettv, 1);
20751}
20752
20753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754 * "virtcol(string)" function
20755 */
20756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020757f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758{
20759 colnr_T vcol = 0;
20760 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020761 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020763 fp = var2fpos(&argvars[0], FALSE, &fnum);
20764 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20765 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020766 {
20767 getvvcol(curwin, fp, NULL, NULL, &vcol);
20768 ++vcol;
20769 }
20770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020771 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772}
20773
20774/*
20775 * "visualmode()" function
20776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020778f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 char_u str[2];
20781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020782 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783 str[0] = curbuf->b_visual_mode_eval;
20784 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020785 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786
20787 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020788 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790}
20791
20792/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020793 * "wildmenumode()" function
20794 */
20795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020796f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020797{
20798#ifdef FEAT_WILDMENU
20799 if (wild_menu_showing)
20800 rettv->vval.v_number = 1;
20801#endif
20802}
20803
20804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 * "winbufnr(nr)" function
20806 */
20807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020808f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809{
20810 win_T *wp;
20811
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020812 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020814 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020816 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817}
20818
20819/*
20820 * "wincol()" function
20821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020823f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824{
20825 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020826 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827}
20828
20829/*
20830 * "winheight(nr)" function
20831 */
20832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020833f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834{
20835 win_T *wp;
20836
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020837 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020839 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020841 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842}
20843
20844/*
20845 * "winline()" function
20846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020848f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849{
20850 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020851 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852}
20853
20854/*
20855 * "winnr()" function
20856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020858f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859{
20860 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020861
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020863 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020865 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866}
20867
20868/*
20869 * "winrestcmd()" function
20870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020872f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873{
20874#ifdef FEAT_WINDOWS
20875 win_T *wp;
20876 int winnr = 1;
20877 garray_T ga;
20878 char_u buf[50];
20879
20880 ga_init2(&ga, (int)sizeof(char), 70);
20881 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20882 {
20883 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20884 ga_concat(&ga, buf);
20885# ifdef FEAT_VERTSPLIT
20886 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20887 ga_concat(&ga, buf);
20888# endif
20889 ++winnr;
20890 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020891 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020893 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020895 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020897 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898}
20899
20900/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020901 * "winrestview()" function
20902 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020904f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020905{
20906 dict_T *dict;
20907
20908 if (argvars[0].v_type != VAR_DICT
20909 || (dict = argvars[0].vval.v_dict) == NULL)
20910 EMSG(_(e_invarg));
20911 else
20912 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020913 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20914 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20915 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20916 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020917#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020918 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20919 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020920#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020921 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20922 {
20923 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20924 curwin->w_set_curswant = FALSE;
20925 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020926
Bram Moolenaar82c25852014-05-28 16:47:16 +020020927 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20928 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020929#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020930 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20931 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020932#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020933 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20934 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20935 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20936 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020937
20938 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020939 win_new_height(curwin, curwin->w_height);
20940# ifdef FEAT_VERTSPLIT
20941 win_new_width(curwin, W_WIDTH(curwin));
20942# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020943 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020944
Bram Moolenaarb851a962014-10-31 15:45:52 +010020945 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020946 curwin->w_topline = 1;
20947 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20948 curwin->w_topline = curbuf->b_ml.ml_line_count;
20949#ifdef FEAT_DIFF
20950 check_topfill(curwin, TRUE);
20951#endif
20952 }
20953}
20954
20955/*
20956 * "winsaveview()" function
20957 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020959f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020960{
20961 dict_T *dict;
20962
Bram Moolenaara800b422010-06-27 01:15:55 +020020963 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020964 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020965 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020966
20967 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20968 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20969#ifdef FEAT_VIRTUALEDIT
20970 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20971#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020972 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020973 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20974
20975 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20976#ifdef FEAT_DIFF
20977 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20978#endif
20979 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20980 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20981}
20982
20983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 * "winwidth(nr)" function
20985 */
20986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020987f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988{
20989 win_T *wp;
20990
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020991 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 else
20995#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020996 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020998 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999#endif
21000}
21001
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021003 * "wordcount()" function
21004 */
21005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021006f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021007{
21008 if (rettv_dict_alloc(rettv) == FAIL)
21009 return;
21010 cursor_pos_info(rettv->vval.v_dict);
21011}
21012
21013/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021014 * Write list of strings to file
21015 */
21016 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021017write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021018{
21019 listitem_T *li;
21020 int c;
21021 int ret = OK;
21022 char_u *s;
21023
21024 for (li = list->lv_first; li != NULL; li = li->li_next)
21025 {
21026 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21027 {
21028 if (*s == '\n')
21029 c = putc(NUL, fd);
21030 else
21031 c = putc(*s, fd);
21032 if (c == EOF)
21033 {
21034 ret = FAIL;
21035 break;
21036 }
21037 }
21038 if (!binary || li->li_next != NULL)
21039 if (putc('\n', fd) == EOF)
21040 {
21041 ret = FAIL;
21042 break;
21043 }
21044 if (ret == FAIL)
21045 {
21046 EMSG(_(e_write));
21047 break;
21048 }
21049 }
21050 return ret;
21051}
21052
21053/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021054 * "writefile()" function
21055 */
21056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021057f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021058{
21059 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021060 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021061 char_u *fname;
21062 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021063 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021064
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021065 if (check_restricted() || check_secure())
21066 return;
21067
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021068 if (argvars[0].v_type != VAR_LIST)
21069 {
21070 EMSG2(_(e_listarg), "writefile()");
21071 return;
21072 }
21073 if (argvars[0].vval.v_list == NULL)
21074 return;
21075
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021076 if (argvars[2].v_type != VAR_UNKNOWN)
21077 {
21078 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21079 binary = TRUE;
21080 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21081 append = TRUE;
21082 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021083
21084 /* Always open the file in binary mode, library functions have a mind of
21085 * their own about CR-LF conversion. */
21086 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021087 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21088 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021089 {
21090 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21091 ret = -1;
21092 }
21093 else
21094 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021095 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21096 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021097 fclose(fd);
21098 }
21099
21100 rettv->vval.v_number = ret;
21101}
21102
21103/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021104 * "xor(expr, expr)" function
21105 */
21106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021107f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021108{
21109 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21110 ^ get_tv_number_chk(&argvars[1], NULL);
21111}
21112
21113
21114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021116 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 */
21118 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021119var2fpos(
21120 typval_T *varp,
21121 int dollar_lnum, /* TRUE when $ is last line */
21122 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021124 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021126 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127
Bram Moolenaara5525202006-03-02 22:52:09 +000021128 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021129 if (varp->v_type == VAR_LIST)
21130 {
21131 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021132 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021133 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021134 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021135
21136 l = varp->vval.v_list;
21137 if (l == NULL)
21138 return NULL;
21139
21140 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021141 pos.lnum = list_find_nr(l, 0L, &error);
21142 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021143 return NULL; /* invalid line number */
21144
21145 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021146 pos.col = list_find_nr(l, 1L, &error);
21147 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021148 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021149 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021150
21151 /* We accept "$" for the column number: last column. */
21152 li = list_find(l, 1L);
21153 if (li != NULL && li->li_tv.v_type == VAR_STRING
21154 && li->li_tv.vval.v_string != NULL
21155 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21156 pos.col = len + 1;
21157
Bram Moolenaara5525202006-03-02 22:52:09 +000021158 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021159 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021160 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021161 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021162
Bram Moolenaara5525202006-03-02 22:52:09 +000021163#ifdef FEAT_VIRTUALEDIT
21164 /* Get the virtual offset. Defaults to zero. */
21165 pos.coladd = list_find_nr(l, 2L, &error);
21166 if (error)
21167 pos.coladd = 0;
21168#endif
21169
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021170 return &pos;
21171 }
21172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021173 name = get_tv_string_chk(varp);
21174 if (name == NULL)
21175 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021176 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021178 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21179 {
21180 if (VIsual_active)
21181 return &VIsual;
21182 return &curwin->w_cursor;
21183 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021184 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021186 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21188 return NULL;
21189 return pp;
21190 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021191
21192#ifdef FEAT_VIRTUALEDIT
21193 pos.coladd = 0;
21194#endif
21195
Bram Moolenaar477933c2007-07-17 14:32:23 +000021196 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021197 {
21198 pos.col = 0;
21199 if (name[1] == '0') /* "w0": first visible line */
21200 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021201 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021202 pos.lnum = curwin->w_topline;
21203 return &pos;
21204 }
21205 else if (name[1] == '$') /* "w$": last visible line */
21206 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021207 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021208 pos.lnum = curwin->w_botline - 1;
21209 return &pos;
21210 }
21211 }
21212 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021214 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215 {
21216 pos.lnum = curbuf->b_ml.ml_line_count;
21217 pos.col = 0;
21218 }
21219 else
21220 {
21221 pos.lnum = curwin->w_cursor.lnum;
21222 pos.col = (colnr_T)STRLEN(ml_get_curline());
21223 }
21224 return &pos;
21225 }
21226 return NULL;
21227}
21228
21229/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021230 * Convert list in "arg" into a position and optional file number.
21231 * When "fnump" is NULL there is no file number, only 3 items.
21232 * Note that the column is passed on as-is, the caller may want to decrement
21233 * it to use 1 for the first column.
21234 * Return FAIL when conversion is not possible, doesn't check the position for
21235 * validity.
21236 */
21237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021238list2fpos(
21239 typval_T *arg,
21240 pos_T *posp,
21241 int *fnump,
21242 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021243{
21244 list_T *l = arg->vval.v_list;
21245 long i = 0;
21246 long n;
21247
Bram Moolenaar493c1782014-05-28 14:34:46 +020021248 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21249 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021250 if (arg->v_type != VAR_LIST
21251 || l == NULL
21252 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021253 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021254 return FAIL;
21255
21256 if (fnump != NULL)
21257 {
21258 n = list_find_nr(l, i++, NULL); /* fnum */
21259 if (n < 0)
21260 return FAIL;
21261 if (n == 0)
21262 n = curbuf->b_fnum; /* current buffer */
21263 *fnump = n;
21264 }
21265
21266 n = list_find_nr(l, i++, NULL); /* lnum */
21267 if (n < 0)
21268 return FAIL;
21269 posp->lnum = n;
21270
21271 n = list_find_nr(l, i++, NULL); /* col */
21272 if (n < 0)
21273 return FAIL;
21274 posp->col = n;
21275
21276#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021277 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021278 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021279 posp->coladd = 0;
21280 else
21281 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021282#endif
21283
Bram Moolenaar493c1782014-05-28 14:34:46 +020021284 if (curswantp != NULL)
21285 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21286
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021287 return OK;
21288}
21289
21290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 * Get the length of an environment variable name.
21292 * Advance "arg" to the first character after the name.
21293 * Return 0 for error.
21294 */
21295 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021296get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297{
21298 char_u *p;
21299 int len;
21300
21301 for (p = *arg; vim_isIDc(*p); ++p)
21302 ;
21303 if (p == *arg) /* no name found */
21304 return 0;
21305
21306 len = (int)(p - *arg);
21307 *arg = p;
21308 return len;
21309}
21310
21311/*
21312 * Get the length of the name of a function or internal variable.
21313 * "arg" is advanced to the first non-white character after the name.
21314 * Return 0 if something is wrong.
21315 */
21316 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021317get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318{
21319 char_u *p;
21320 int len;
21321
21322 /* Find the end of the name. */
21323 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021324 {
21325 if (*p == ':')
21326 {
21327 /* "s:" is start of "s:var", but "n:" is not and can be used in
21328 * slice "[n:]". Also "xx:" is not a namespace. */
21329 len = (int)(p - *arg);
21330 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21331 || len > 1)
21332 break;
21333 }
21334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021335 if (p == *arg) /* no name found */
21336 return 0;
21337
21338 len = (int)(p - *arg);
21339 *arg = skipwhite(p);
21340
21341 return len;
21342}
21343
21344/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021345 * Get the length of the name of a variable or function.
21346 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021348 * Return -1 if curly braces expansion failed.
21349 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 * If the name contains 'magic' {}'s, expand them and return the
21351 * expanded name in an allocated string via 'alias' - caller must free.
21352 */
21353 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021354get_name_len(
21355 char_u **arg,
21356 char_u **alias,
21357 int evaluate,
21358 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359{
21360 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 char_u *p;
21362 char_u *expr_start;
21363 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364
21365 *alias = NULL; /* default to no alias */
21366
21367 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21368 && (*arg)[2] == (int)KE_SNR)
21369 {
21370 /* hard coded <SNR>, already translated */
21371 *arg += 3;
21372 return get_id_len(arg) + 3;
21373 }
21374 len = eval_fname_script(*arg);
21375 if (len > 0)
21376 {
21377 /* literal "<SID>", "s:" or "<SNR>" */
21378 *arg += len;
21379 }
21380
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021382 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021384 p = find_name_end(*arg, &expr_start, &expr_end,
21385 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 if (expr_start != NULL)
21387 {
21388 char_u *temp_string;
21389
21390 if (!evaluate)
21391 {
21392 len += (int)(p - *arg);
21393 *arg = skipwhite(p);
21394 return len;
21395 }
21396
21397 /*
21398 * Include any <SID> etc in the expanded string:
21399 * Thus the -len here.
21400 */
21401 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21402 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021403 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 *alias = temp_string;
21405 *arg = skipwhite(p);
21406 return (int)STRLEN(temp_string);
21407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408
21409 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021410 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411 EMSG2(_(e_invexpr2), *arg);
21412
21413 return len;
21414}
21415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021416/*
21417 * Find the end of a variable or function name, taking care of magic braces.
21418 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21419 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021420 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021421 * Return a pointer to just after the name. Equal to "arg" if there is no
21422 * valid name.
21423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021425find_name_end(
21426 char_u *arg,
21427 char_u **expr_start,
21428 char_u **expr_end,
21429 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021431 int mb_nest = 0;
21432 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021434 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021436 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021438 *expr_start = NULL;
21439 *expr_end = NULL;
21440 }
21441
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021442 /* Quick check for valid starting character. */
21443 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21444 return arg;
21445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021446 for (p = arg; *p != NUL
21447 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021448 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021449 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021450 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021451 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021452 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021453 if (*p == '\'')
21454 {
21455 /* skip over 'string' to avoid counting [ and ] inside it. */
21456 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21457 ;
21458 if (*p == NUL)
21459 break;
21460 }
21461 else if (*p == '"')
21462 {
21463 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21464 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21465 if (*p == '\\' && p[1] != NUL)
21466 ++p;
21467 if (*p == NUL)
21468 break;
21469 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021470 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21471 {
21472 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021473 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021474 len = (int)(p - arg);
21475 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021476 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021477 break;
21478 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021480 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021482 if (*p == '[')
21483 ++br_nest;
21484 else if (*p == ']')
21485 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021488 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021490 if (*p == '{')
21491 {
21492 mb_nest++;
21493 if (expr_start != NULL && *expr_start == NULL)
21494 *expr_start = p;
21495 }
21496 else if (*p == '}')
21497 {
21498 mb_nest--;
21499 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21500 *expr_end = p;
21501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503 }
21504
21505 return p;
21506}
21507
21508/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021509 * Expands out the 'magic' {}'s in a variable/function name.
21510 * Note that this can call itself recursively, to deal with
21511 * constructs like foo{bar}{baz}{bam}
21512 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21513 * "in_start" ^
21514 * "expr_start" ^
21515 * "expr_end" ^
21516 * "in_end" ^
21517 *
21518 * Returns a new allocated string, which the caller must free.
21519 * Returns NULL for failure.
21520 */
21521 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021522make_expanded_name(
21523 char_u *in_start,
21524 char_u *expr_start,
21525 char_u *expr_end,
21526 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021527{
21528 char_u c1;
21529 char_u *retval = NULL;
21530 char_u *temp_result;
21531 char_u *nextcmd = NULL;
21532
21533 if (expr_end == NULL || in_end == NULL)
21534 return NULL;
21535 *expr_start = NUL;
21536 *expr_end = NUL;
21537 c1 = *in_end;
21538 *in_end = NUL;
21539
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021540 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021541 if (temp_result != NULL && nextcmd == NULL)
21542 {
21543 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21544 + (in_end - expr_end) + 1));
21545 if (retval != NULL)
21546 {
21547 STRCPY(retval, in_start);
21548 STRCAT(retval, temp_result);
21549 STRCAT(retval, expr_end + 1);
21550 }
21551 }
21552 vim_free(temp_result);
21553
21554 *in_end = c1; /* put char back for error messages */
21555 *expr_start = '{';
21556 *expr_end = '}';
21557
21558 if (retval != NULL)
21559 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021560 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021561 if (expr_start != NULL)
21562 {
21563 /* Further expansion! */
21564 temp_result = make_expanded_name(retval, expr_start,
21565 expr_end, temp_result);
21566 vim_free(retval);
21567 retval = temp_result;
21568 }
21569 }
21570
21571 return retval;
21572}
21573
21574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021576 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 */
21578 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021579eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021581 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21582}
21583
21584/*
21585 * Return TRUE if character "c" can be used as the first character in a
21586 * variable or function name (excluding '{' and '}').
21587 */
21588 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021589eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021590{
21591 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592}
21593
21594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 * Set number v: variable to "val".
21596 */
21597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021598set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021600 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601}
21602
21603/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021604 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605 */
21606 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021607get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021609 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610}
21611
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021612/*
21613 * Get string v: variable value. Uses a static buffer, can only be used once.
21614 */
21615 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021616get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021617{
21618 return get_tv_string(&vimvars[idx].vv_tv);
21619}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021620
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021622 * Get List v: variable value. Caller must take care of reference count when
21623 * needed.
21624 */
21625 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021626get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021627{
21628 return vimvars[idx].vv_list;
21629}
21630
21631/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021632 * Set v:char to character "c".
21633 */
21634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021635set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021636{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021637 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021638
21639#ifdef FEAT_MBYTE
21640 if (has_mbyte)
21641 buf[(*mb_char2bytes)(c, buf)] = NUL;
21642 else
21643#endif
21644 {
21645 buf[0] = c;
21646 buf[1] = NUL;
21647 }
21648 set_vim_var_string(VV_CHAR, buf, -1);
21649}
21650
21651/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021652 * Set v:count to "count" and v:count1 to "count1".
21653 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 */
21655 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021656set_vcount(
21657 long count,
21658 long count1,
21659 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021661 if (set_prevcount)
21662 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021663 vimvars[VV_COUNT].vv_nr = count;
21664 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665}
21666
21667/*
21668 * Set string v: variable to a copy of "val".
21669 */
21670 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021671set_vim_var_string(
21672 int idx,
21673 char_u *val,
21674 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675{
Bram Moolenaara542c682016-01-31 16:28:04 +010021676 clear_tv(&vimvars[idx].vv_di.di_tv);
21677 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021679 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021681 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021682 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021683 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684}
21685
21686/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021687 * Set List v: variable to "val".
21688 */
21689 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021690set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021691{
Bram Moolenaara542c682016-01-31 16:28:04 +010021692 clear_tv(&vimvars[idx].vv_di.di_tv);
21693 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021694 vimvars[idx].vv_list = val;
21695 if (val != NULL)
21696 ++val->lv_refcount;
21697}
21698
21699/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021700 * Set Dictionary v: variable to "val".
21701 */
21702 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021703set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021704{
21705 int todo;
21706 hashitem_T *hi;
21707
Bram Moolenaara542c682016-01-31 16:28:04 +010021708 clear_tv(&vimvars[idx].vv_di.di_tv);
21709 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021710 vimvars[idx].vv_dict = val;
21711 if (val != NULL)
21712 {
21713 ++val->dv_refcount;
21714
21715 /* Set readonly */
21716 todo = (int)val->dv_hashtab.ht_used;
21717 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21718 {
21719 if (HASHITEM_EMPTY(hi))
21720 continue;
21721 --todo;
21722 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21723 }
21724 }
21725}
21726
21727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728 * Set v:register if needed.
21729 */
21730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021731set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732{
21733 char_u regname;
21734
21735 if (c == 0 || c == ' ')
21736 regname = '"';
21737 else
21738 regname = c;
21739 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021740 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741 set_vim_var_string(VV_REG, &regname, 1);
21742}
21743
21744/*
21745 * Get or set v:exception. If "oldval" == NULL, return the current value.
21746 * Otherwise, restore the value to "oldval" and return NULL.
21747 * Must always be called in pairs to save and restore v:exception! Does not
21748 * take care of memory allocations.
21749 */
21750 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021751v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752{
21753 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021754 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755
Bram Moolenaare9a41262005-01-15 22:18:47 +000021756 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 return NULL;
21758}
21759
21760/*
21761 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21762 * Otherwise, restore the value to "oldval" and return NULL.
21763 * Must always be called in pairs to save and restore v:throwpoint! Does not
21764 * take care of memory allocations.
21765 */
21766 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021767v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768{
21769 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021770 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771
Bram Moolenaare9a41262005-01-15 22:18:47 +000021772 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773 return NULL;
21774}
21775
21776#if defined(FEAT_AUTOCMD) || defined(PROTO)
21777/*
21778 * Set v:cmdarg.
21779 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21780 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21781 * Must always be called in pairs!
21782 */
21783 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021784set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785{
21786 char_u *oldval;
21787 char_u *newval;
21788 unsigned len;
21789
Bram Moolenaare9a41262005-01-15 22:18:47 +000021790 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021791 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021793 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021794 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021795 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796 }
21797
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021798 if (eap->force_bin == FORCE_BIN)
21799 len = 6;
21800 else if (eap->force_bin == FORCE_NOBIN)
21801 len = 8;
21802 else
21803 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021804
21805 if (eap->read_edit)
21806 len += 7;
21807
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021808 if (eap->force_ff != 0)
21809 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21810# ifdef FEAT_MBYTE
21811 if (eap->force_enc != 0)
21812 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021813 if (eap->bad_char != 0)
21814 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021815# endif
21816
21817 newval = alloc(len + 1);
21818 if (newval == NULL)
21819 return NULL;
21820
21821 if (eap->force_bin == FORCE_BIN)
21822 sprintf((char *)newval, " ++bin");
21823 else if (eap->force_bin == FORCE_NOBIN)
21824 sprintf((char *)newval, " ++nobin");
21825 else
21826 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021827
21828 if (eap->read_edit)
21829 STRCAT(newval, " ++edit");
21830
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021831 if (eap->force_ff != 0)
21832 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21833 eap->cmd + eap->force_ff);
21834# ifdef FEAT_MBYTE
21835 if (eap->force_enc != 0)
21836 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21837 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021838 if (eap->bad_char == BAD_KEEP)
21839 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21840 else if (eap->bad_char == BAD_DROP)
21841 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21842 else if (eap->bad_char != 0)
21843 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021844# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021845 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021846 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847}
21848#endif
21849
21850/*
21851 * Get the value of internal variable "name".
21852 * Return OK or FAIL.
21853 */
21854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021855get_var_tv(
21856 char_u *name,
21857 int len, /* length of "name" */
21858 typval_T *rettv, /* NULL when only checking existence */
21859 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21860 int verbose, /* may give error message */
21861 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862{
21863 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021864 typval_T *tv = NULL;
21865 typval_T atv;
21866 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868
21869 /* truncate the name, so that we can use strcmp() */
21870 cc = name[len];
21871 name[len] = NUL;
21872
21873 /*
21874 * Check for "b:changedtick".
21875 */
21876 if (STRCMP(name, "b:changedtick") == 0)
21877 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021878 atv.v_type = VAR_NUMBER;
21879 atv.vval.v_number = curbuf->b_changedtick;
21880 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881 }
21882
21883 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 * Check for user-defined variables.
21885 */
21886 else
21887 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021888 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021890 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021891 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021892 if (dip != NULL)
21893 *dip = v;
21894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895 }
21896
Bram Moolenaare9a41262005-01-15 22:18:47 +000021897 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021899 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021900 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 ret = FAIL;
21902 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021903 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021904 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905
21906 name[len] = cc;
21907
21908 return ret;
21909}
21910
21911/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021912 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21913 * Also handle function call with Funcref variable: func(expr)
21914 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21915 */
21916 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021917handle_subscript(
21918 char_u **arg,
21919 typval_T *rettv,
21920 int evaluate, /* do more than finding the end */
21921 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021922{
21923 int ret = OK;
21924 dict_T *selfdict = NULL;
21925 char_u *s;
21926 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021927 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021928
21929 while (ret == OK
21930 && (**arg == '['
21931 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021932 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021933 && !vim_iswhite(*(*arg - 1)))
21934 {
21935 if (**arg == '(')
21936 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021937 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021938 if (evaluate)
21939 {
21940 functv = *rettv;
21941 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021942
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021943 /* Invoke the function. Recursive! */
21944 s = functv.vval.v_string;
21945 }
21946 else
21947 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021948 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021949 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21950 &len, evaluate, selfdict);
21951
21952 /* Clear the funcref afterwards, so that deleting it while
21953 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021954 if (evaluate)
21955 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021956
21957 /* Stop the expression evaluation when immediately aborting on
21958 * error, or when an interrupt occurred or an exception was thrown
21959 * but not caught. */
21960 if (aborting())
21961 {
21962 if (ret == OK)
21963 clear_tv(rettv);
21964 ret = FAIL;
21965 }
21966 dict_unref(selfdict);
21967 selfdict = NULL;
21968 }
21969 else /* **arg == '[' || **arg == '.' */
21970 {
21971 dict_unref(selfdict);
21972 if (rettv->v_type == VAR_DICT)
21973 {
21974 selfdict = rettv->vval.v_dict;
21975 if (selfdict != NULL)
21976 ++selfdict->dv_refcount;
21977 }
21978 else
21979 selfdict = NULL;
21980 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21981 {
21982 clear_tv(rettv);
21983 ret = FAIL;
21984 }
21985 }
21986 }
21987 dict_unref(selfdict);
21988 return ret;
21989}
21990
21991/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021992 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021993 * value).
21994 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021995 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021996alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021997{
Bram Moolenaar33570922005-01-25 22:26:29 +000021998 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021999}
22000
22001/*
22002 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 * The string "s" must have been allocated, it is consumed.
22004 * Return NULL for out of memory, the variable otherwise.
22005 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022006 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022007alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008{
Bram Moolenaar33570922005-01-25 22:26:29 +000022009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022011 rettv = alloc_tv();
22012 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022014 rettv->v_type = VAR_STRING;
22015 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016 }
22017 else
22018 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022019 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020}
22021
22022/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022023 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022025 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022026free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027{
22028 if (varp != NULL)
22029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022030 switch (varp->v_type)
22031 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022032 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022033 func_unref(varp->vval.v_string);
22034 /*FALLTHROUGH*/
22035 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022036 vim_free(varp->vval.v_string);
22037 break;
22038 case VAR_LIST:
22039 list_unref(varp->vval.v_list);
22040 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022041 case VAR_DICT:
22042 dict_unref(varp->vval.v_dict);
22043 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022044 case VAR_JOB:
22045#ifdef FEAT_JOB
22046 job_unref(varp->vval.v_job);
22047 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022048#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022049 case VAR_CHANNEL:
22050#ifdef FEAT_CHANNEL
22051 channel_unref(varp->vval.v_channel);
22052 break;
22053#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022054 case VAR_NUMBER:
22055 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022056 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022057 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022058 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 vim_free(varp);
22061 }
22062}
22063
22064/*
22065 * Free the memory for a variable value and set the value to NULL or 0.
22066 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022067 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022068clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069{
22070 if (varp != NULL)
22071 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022072 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022074 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022075 func_unref(varp->vval.v_string);
22076 /*FALLTHROUGH*/
22077 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022078 vim_free(varp->vval.v_string);
22079 varp->vval.v_string = NULL;
22080 break;
22081 case VAR_LIST:
22082 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022083 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022084 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022085 case VAR_DICT:
22086 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022087 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022088 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022089 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022090 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022091 varp->vval.v_number = 0;
22092 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022093 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022094#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022095 varp->vval.v_float = 0.0;
22096 break;
22097#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022098 case VAR_JOB:
22099#ifdef FEAT_JOB
22100 job_unref(varp->vval.v_job);
22101 varp->vval.v_job = NULL;
22102#endif
22103 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022104 case VAR_CHANNEL:
22105#ifdef FEAT_CHANNEL
22106 channel_unref(varp->vval.v_channel);
22107 varp->vval.v_channel = NULL;
22108#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022109 case VAR_UNKNOWN:
22110 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022112 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113 }
22114}
22115
22116/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022117 * Set the value of a variable to NULL without freeing items.
22118 */
22119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022120init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022121{
22122 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022123 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022124}
22125
22126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 * Get the number value of a variable.
22128 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022129 * For incompatible types, return 0.
22130 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22131 * caller of incompatible types: it sets *denote to TRUE if "denote"
22132 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022133 */
22134 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022135get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022137 int error = FALSE;
22138
22139 return get_tv_number_chk(varp, &error); /* return 0L on error */
22140}
22141
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022142 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022143get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022144{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022145 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022147 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022149 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022150 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022151 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022152#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022153 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022154 break;
22155#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022156 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022157 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022158 break;
22159 case VAR_STRING:
22160 if (varp->vval.v_string != NULL)
22161 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022162 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022163 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022164 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022165 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022166 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022167 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022168 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022169 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022170 case VAR_SPECIAL:
22171 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22172 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022173 case VAR_JOB:
22174#ifdef FEAT_JOB
22175 EMSG(_("E910: Using a Job as a Number"));
22176 break;
22177#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022178 case VAR_CHANNEL:
22179#ifdef FEAT_CHANNEL
22180 EMSG(_("E913: Using a Channel as a Number"));
22181 break;
22182#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022183 case VAR_UNKNOWN:
22184 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022185 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022187 if (denote == NULL) /* useful for values that must be unsigned */
22188 n = -1;
22189 else
22190 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022191 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192}
22193
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022194#ifdef FEAT_FLOAT
22195 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022196get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022197{
22198 switch (varp->v_type)
22199 {
22200 case VAR_NUMBER:
22201 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022202 case VAR_FLOAT:
22203 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022204 case VAR_FUNC:
22205 EMSG(_("E891: Using a Funcref as a Float"));
22206 break;
22207 case VAR_STRING:
22208 EMSG(_("E892: Using a String as a Float"));
22209 break;
22210 case VAR_LIST:
22211 EMSG(_("E893: Using a List as a Float"));
22212 break;
22213 case VAR_DICT:
22214 EMSG(_("E894: Using a Dictionary as a Float"));
22215 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022216 case VAR_SPECIAL:
22217 EMSG(_("E907: Using a special value as a Float"));
22218 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022219 case VAR_JOB:
22220# ifdef FEAT_JOB
22221 EMSG(_("E911: Using a Job as a Float"));
22222 break;
22223# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022224 case VAR_CHANNEL:
22225# ifdef FEAT_CHANNEL
22226 EMSG(_("E914: Using a Channel as a Float"));
22227 break;
22228# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022229 case VAR_UNKNOWN:
22230 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022231 break;
22232 }
22233 return 0;
22234}
22235#endif
22236
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022238 * Get the lnum from the first argument.
22239 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022240 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241 */
22242 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022243get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244{
Bram Moolenaar33570922005-01-25 22:26:29 +000022245 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 linenr_T lnum;
22247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022248 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249 if (lnum == 0) /* no valid number, try using line() */
22250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022251 rettv.v_type = VAR_NUMBER;
22252 f_line(argvars, &rettv);
22253 lnum = rettv.vval.v_number;
22254 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 }
22256 return lnum;
22257}
22258
22259/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022260 * Get the lnum from the first argument.
22261 * Also accepts "$", then "buf" is used.
22262 * Returns 0 on error.
22263 */
22264 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022265get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022266{
22267 if (argvars[0].v_type == VAR_STRING
22268 && argvars[0].vval.v_string != NULL
22269 && argvars[0].vval.v_string[0] == '$'
22270 && buf != NULL)
22271 return buf->b_ml.ml_line_count;
22272 return get_tv_number_chk(&argvars[0], NULL);
22273}
22274
22275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 * Get the string value of a variable.
22277 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022278 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22279 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 * If the String variable has never been set, return an empty string.
22281 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022282 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22283 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 */
22285 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022286get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287{
22288 static char_u mybuf[NUMBUFLEN];
22289
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022290 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291}
22292
22293 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022294get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022296 char_u *res = get_tv_string_buf_chk(varp, buf);
22297
22298 return res != NULL ? res : (char_u *)"";
22299}
22300
Bram Moolenaar7d647822014-04-05 21:28:56 +020022301/*
22302 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22303 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022304 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022305get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022306{
22307 static char_u mybuf[NUMBUFLEN];
22308
22309 return get_tv_string_buf_chk(varp, mybuf);
22310}
22311
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022312 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022313get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022314{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022315 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022317 case VAR_NUMBER:
22318 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22319 return buf;
22320 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022321 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022322 break;
22323 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022324 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022325 break;
22326 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022327 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022328 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022329 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022330#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022331 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022332 break;
22333#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022334 case VAR_STRING:
22335 if (varp->vval.v_string != NULL)
22336 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022337 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022338 case VAR_SPECIAL:
22339 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22340 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022341 case VAR_JOB:
22342#ifdef FEAT_JOB
22343 {
22344 job_T *job = varp->vval.v_job;
22345 char *status = job->jv_status == JOB_FAILED ? "fail"
22346 : job->jv_status == JOB_ENDED ? "dead"
22347 : "run";
22348# ifdef UNIX
22349 vim_snprintf((char *)buf, NUMBUFLEN,
22350 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022351# elif defined(WIN32)
22352 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022353 "process %ld %s",
22354 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022355 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022356# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022357 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022358 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22359# endif
22360 return buf;
22361 }
22362#endif
22363 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022364 case VAR_CHANNEL:
22365#ifdef FEAT_CHANNEL
22366 {
22367 channel_T *channel = varp->vval.v_channel;
22368 char *status = channel_status(channel);
22369
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022370 if (channel == NULL)
22371 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22372 else
22373 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022374 "channel %d %s", channel->ch_id, status);
22375 return buf;
22376 }
22377#endif
22378 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022379 case VAR_UNKNOWN:
22380 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022381 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022383 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384}
22385
22386/*
22387 * Find variable "name" in the list of variables.
22388 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022389 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022390 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022391 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022393 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022394find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022396 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022397 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398
Bram Moolenaara7043832005-01-21 11:56:39 +000022399 ht = find_var_ht(name, &varname);
22400 if (htp != NULL)
22401 *htp = ht;
22402 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022404 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405}
22406
22407/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022408 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022409 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022411 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022412find_var_in_ht(
22413 hashtab_T *ht,
22414 int htname,
22415 char_u *varname,
22416 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022417{
Bram Moolenaar33570922005-01-25 22:26:29 +000022418 hashitem_T *hi;
22419
22420 if (*varname == NUL)
22421 {
22422 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022423 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022424 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022425 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022426 case 'g': return &globvars_var;
22427 case 'v': return &vimvars_var;
22428 case 'b': return &curbuf->b_bufvar;
22429 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022430#ifdef FEAT_WINDOWS
22431 case 't': return &curtab->tp_winvar;
22432#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022433 case 'l': return current_funccal == NULL
22434 ? NULL : &current_funccal->l_vars_var;
22435 case 'a': return current_funccal == NULL
22436 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022437 }
22438 return NULL;
22439 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022440
22441 hi = hash_find(ht, varname);
22442 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022443 {
22444 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022445 * worked find the variable again. Don't auto-load a script if it was
22446 * loaded already, otherwise it would be loaded every time when
22447 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022448 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022449 {
22450 /* Note: script_autoload() may make "hi" invalid. It must either
22451 * be obtained again or not used. */
22452 if (!script_autoload(varname, FALSE) || aborting())
22453 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022454 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022455 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022456 if (HASHITEM_EMPTY(hi))
22457 return NULL;
22458 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022459 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022460}
22461
22462/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022463 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022464 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022465 * Set "varname" to the start of name without ':'.
22466 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022467 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022468find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022470 hashitem_T *hi;
22471
Bram Moolenaar73627d02015-08-11 15:46:09 +020022472 if (name[0] == NUL)
22473 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 if (name[1] != ':')
22475 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022476 /* The name must not start with a colon or #. */
22477 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 return NULL;
22479 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022480
22481 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022482 hi = hash_find(&compat_hashtab, name);
22483 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022484 return &compat_hashtab;
22485
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022487 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022488 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 }
22490 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022491 if (*name == 'g') /* global variable */
22492 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022493 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22494 */
22495 if (vim_strchr(name + 2, ':') != NULL
22496 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022497 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022499 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022501 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022502#ifdef FEAT_WINDOWS
22503 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022504 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022505#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022506 if (*name == 'v') /* v: variable */
22507 return &vimvarht;
22508 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022509 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022510 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022511 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 if (*name == 's' /* script variable */
22513 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22514 return &SCRIPT_VARS(current_SID);
22515 return NULL;
22516}
22517
22518/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022519 * Get function call environment based on bactrace debug level
22520 */
22521 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022522get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022523{
22524 int i;
22525 funccall_T *funccal;
22526 funccall_T *temp_funccal;
22527
22528 funccal = current_funccal;
22529 if (debug_backtrace_level > 0)
22530 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022531 for (i = 0; i < debug_backtrace_level; i++)
22532 {
22533 temp_funccal = funccal->caller;
22534 if (temp_funccal)
22535 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022536 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022537 /* backtrace level overflow. reset to max */
22538 debug_backtrace_level = i;
22539 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022540 }
22541 return funccal;
22542}
22543
22544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022546 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 * Returns NULL when it doesn't exist.
22548 */
22549 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022550get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551{
Bram Moolenaar33570922005-01-25 22:26:29 +000022552 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022554 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555 if (v == NULL)
22556 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022557 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558}
22559
22560/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022561 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 * sourcing this script and when executing functions defined in the script.
22563 */
22564 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022565new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566{
Bram Moolenaara7043832005-01-21 11:56:39 +000022567 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022568 hashtab_T *ht;
22569 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022570
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22572 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022573 /* Re-allocating ga_data means that an ht_array pointing to
22574 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022575 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022576 for (i = 1; i <= ga_scripts.ga_len; ++i)
22577 {
22578 ht = &SCRIPT_VARS(i);
22579 if (ht->ht_mask == HT_INIT_SIZE - 1)
22580 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022581 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022582 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022583 }
22584
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 while (ga_scripts.ga_len < id)
22586 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022587 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022588 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022589 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591 }
22592 }
22593}
22594
22595/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022596 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22597 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 */
22599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022600init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601{
Bram Moolenaar33570922005-01-25 22:26:29 +000022602 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022603 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022604 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022605 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022606 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022607 dict_var->di_tv.vval.v_dict = dict;
22608 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022609 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022610 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22611 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612}
22613
22614/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022615 * Unreference a dictionary initialized by init_var_dict().
22616 */
22617 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022618unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022619{
22620 /* Now the dict needs to be freed if no one else is using it, go back to
22621 * normal reference counting. */
22622 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22623 dict_unref(dict);
22624}
22625
22626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022628 * Frees all allocated variables and the value they contain.
22629 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 */
22631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022632vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022633{
22634 vars_clear_ext(ht, TRUE);
22635}
22636
22637/*
22638 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22639 */
22640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022641vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022642{
Bram Moolenaara7043832005-01-21 11:56:39 +000022643 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022644 hashitem_T *hi;
22645 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646
Bram Moolenaar33570922005-01-25 22:26:29 +000022647 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022648 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022649 for (hi = ht->ht_array; todo > 0; ++hi)
22650 {
22651 if (!HASHITEM_EMPTY(hi))
22652 {
22653 --todo;
22654
Bram Moolenaar33570922005-01-25 22:26:29 +000022655 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022656 * ht_array might change then. hash_clear() takes care of it
22657 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022658 v = HI2DI(hi);
22659 if (free_val)
22660 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022661 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022662 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022663 }
22664 }
22665 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022666 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667}
22668
Bram Moolenaara7043832005-01-21 11:56:39 +000022669/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022670 * Delete a variable from hashtab "ht" at item "hi".
22671 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022674delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675{
Bram Moolenaar33570922005-01-25 22:26:29 +000022676 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022677
22678 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022679 clear_tv(&di->di_tv);
22680 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681}
22682
22683/*
22684 * List the value of one internal variable.
22685 */
22686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022687list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022688{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022689 char_u *tofree;
22690 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022691 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022692
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022693 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022694 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022695 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022696 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697}
22698
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022700list_one_var_a(
22701 char_u *prefix,
22702 char_u *name,
22703 int type,
22704 char_u *string,
22705 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706{
Bram Moolenaar31859182007-08-14 20:41:13 +000022707 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22708 msg_start();
22709 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 if (name != NULL) /* "a:" vars don't have a name stored */
22711 msg_puts(name);
22712 msg_putchar(' ');
22713 msg_advance(22);
22714 if (type == VAR_NUMBER)
22715 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022716 else if (type == VAR_FUNC)
22717 msg_putchar('*');
22718 else if (type == VAR_LIST)
22719 {
22720 msg_putchar('[');
22721 if (*string == '[')
22722 ++string;
22723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022724 else if (type == VAR_DICT)
22725 {
22726 msg_putchar('{');
22727 if (*string == '{')
22728 ++string;
22729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 else
22731 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022732
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022734
22735 if (type == VAR_FUNC)
22736 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022737 if (*first)
22738 {
22739 msg_clr_eos();
22740 *first = FALSE;
22741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742}
22743
22744/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022745 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746 * If the variable already exists, the value is updated.
22747 * Otherwise the variable is created.
22748 */
22749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022750set_var(
22751 char_u *name,
22752 typval_T *tv,
22753 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754{
Bram Moolenaar33570922005-01-25 22:26:29 +000022755 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022757 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022759 ht = find_var_ht(name, &varname);
22760 if (ht == NULL || *varname == NUL)
22761 {
22762 EMSG2(_(e_illvar), name);
22763 return;
22764 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022765 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022766
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022767 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22768 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022769
Bram Moolenaar33570922005-01-25 22:26:29 +000022770 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022772 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022773 if (var_check_ro(v->di_flags, name, FALSE)
22774 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022775 return;
22776 if (v->di_tv.v_type != tv->v_type
22777 && !((v->di_tv.v_type == VAR_STRING
22778 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022779 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022780 || tv->v_type == VAR_NUMBER))
22781#ifdef FEAT_FLOAT
22782 && !((v->di_tv.v_type == VAR_NUMBER
22783 || v->di_tv.v_type == VAR_FLOAT)
22784 && (tv->v_type == VAR_NUMBER
22785 || tv->v_type == VAR_FLOAT))
22786#endif
22787 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022788 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022789 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022790 return;
22791 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022792
22793 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022794 * Handle setting internal v: variables separately where needed to
22795 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022796 */
22797 if (ht == &vimvarht)
22798 {
22799 if (v->di_tv.v_type == VAR_STRING)
22800 {
22801 vim_free(v->di_tv.vval.v_string);
22802 if (copy || tv->v_type != VAR_STRING)
22803 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22804 else
22805 {
22806 /* Take over the string to avoid an extra alloc/free. */
22807 v->di_tv.vval.v_string = tv->vval.v_string;
22808 tv->vval.v_string = NULL;
22809 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022810 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022811 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022812 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022813 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022814 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022815 if (STRCMP(varname, "searchforward") == 0)
22816 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022817#ifdef FEAT_SEARCH_EXTRA
22818 else if (STRCMP(varname, "hlsearch") == 0)
22819 {
22820 no_hlsearch = !v->di_tv.vval.v_number;
22821 redraw_all_later(SOME_VALID);
22822 }
22823#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022824 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022825 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022826 else if (v->di_tv.v_type != tv->v_type)
22827 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022828 }
22829
22830 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 }
22832 else /* add a new variable */
22833 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022834 /* Can't add "v:" variable. */
22835 if (ht == &vimvarht)
22836 {
22837 EMSG2(_(e_illvar), name);
22838 return;
22839 }
22840
Bram Moolenaar92124a32005-06-17 22:03:40 +000022841 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022842 if (!valid_varname(varname))
22843 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022844
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022845 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22846 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022847 if (v == NULL)
22848 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022849 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022850 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022852 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853 return;
22854 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022855 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022857
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022858 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022859 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022860 else
22861 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022862 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022863 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022864 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866}
22867
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022868/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022869 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022870 * Also give an error message.
22871 */
22872 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022873var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022874{
22875 if (flags & DI_FLAGS_RO)
22876 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022877 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022878 return TRUE;
22879 }
22880 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22881 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022882 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022883 return TRUE;
22884 }
22885 return FALSE;
22886}
22887
22888/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022889 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22890 * Also give an error message.
22891 */
22892 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022893var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022894{
22895 if (flags & DI_FLAGS_FIX)
22896 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022897 EMSG2(_("E795: Cannot delete variable %s"),
22898 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022899 return TRUE;
22900 }
22901 return FALSE;
22902}
22903
22904/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022905 * Check if a funcref is assigned to a valid variable name.
22906 * Return TRUE and give an error if not.
22907 */
22908 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022909var_check_func_name(
22910 char_u *name, /* points to start of variable name */
22911 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022912{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022913 /* Allow for w: b: s: and t:. */
22914 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022915 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22916 ? name[2] : name[0]))
22917 {
22918 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22919 name);
22920 return TRUE;
22921 }
22922 /* Don't allow hiding a function. When "v" is not NULL we might be
22923 * assigning another function to the same var, the type is checked
22924 * below. */
22925 if (new_var && function_exists(name))
22926 {
22927 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22928 name);
22929 return TRUE;
22930 }
22931 return FALSE;
22932}
22933
22934/*
22935 * Check if a variable name is valid.
22936 * Return FALSE and give an error if not.
22937 */
22938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022939valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022940{
22941 char_u *p;
22942
22943 for (p = varname; *p != NUL; ++p)
22944 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22945 && *p != AUTOLOAD_CHAR)
22946 {
22947 EMSG2(_(e_illvar), varname);
22948 return FALSE;
22949 }
22950 return TRUE;
22951}
22952
22953/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022954 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022955 * Also give an error message, using "name" or _("name") when use_gettext is
22956 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022957 */
22958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022959tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022960{
22961 if (lock & VAR_LOCKED)
22962 {
22963 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022964 name == NULL ? (char_u *)_("Unknown")
22965 : use_gettext ? (char_u *)_(name)
22966 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022967 return TRUE;
22968 }
22969 if (lock & VAR_FIXED)
22970 {
22971 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022972 name == NULL ? (char_u *)_("Unknown")
22973 : use_gettext ? (char_u *)_(name)
22974 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022975 return TRUE;
22976 }
22977 return FALSE;
22978}
22979
22980/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022981 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022982 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022983 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022984 * It is OK for "from" and "to" to point to the same item. This is used to
22985 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022986 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022987 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022988copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022990 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022991 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022992 switch (from->v_type)
22993 {
22994 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022995 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022996 to->vval.v_number = from->vval.v_number;
22997 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022998 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022999#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023000 to->vval.v_float = from->vval.v_float;
23001 break;
23002#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023003 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023004#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023005 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023006 if (to->vval.v_job != NULL)
23007 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023008 break;
23009#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023010 case VAR_CHANNEL:
23011#ifdef FEAT_CHANNEL
23012 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023013 if (to->vval.v_channel != NULL)
23014 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023015 break;
23016#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023017 case VAR_STRING:
23018 case VAR_FUNC:
23019 if (from->vval.v_string == NULL)
23020 to->vval.v_string = NULL;
23021 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023022 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023023 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023024 if (from->v_type == VAR_FUNC)
23025 func_ref(to->vval.v_string);
23026 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023027 break;
23028 case VAR_LIST:
23029 if (from->vval.v_list == NULL)
23030 to->vval.v_list = NULL;
23031 else
23032 {
23033 to->vval.v_list = from->vval.v_list;
23034 ++to->vval.v_list->lv_refcount;
23035 }
23036 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023037 case VAR_DICT:
23038 if (from->vval.v_dict == NULL)
23039 to->vval.v_dict = NULL;
23040 else
23041 {
23042 to->vval.v_dict = from->vval.v_dict;
23043 ++to->vval.v_dict->dv_refcount;
23044 }
23045 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023046 case VAR_UNKNOWN:
23047 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023048 break;
23049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050}
23051
23052/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023053 * Make a copy of an item.
23054 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023055 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23056 * reference to an already copied list/dict can be used.
23057 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023058 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023060item_copy(
23061 typval_T *from,
23062 typval_T *to,
23063 int deep,
23064 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023065{
23066 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023067 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023068
Bram Moolenaar33570922005-01-25 22:26:29 +000023069 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023070 {
23071 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023072 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023073 }
23074 ++recurse;
23075
23076 switch (from->v_type)
23077 {
23078 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023079 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023080 case VAR_STRING:
23081 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023082 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023083 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023084 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023085 copy_tv(from, to);
23086 break;
23087 case VAR_LIST:
23088 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023089 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023090 if (from->vval.v_list == NULL)
23091 to->vval.v_list = NULL;
23092 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23093 {
23094 /* use the copy made earlier */
23095 to->vval.v_list = from->vval.v_list->lv_copylist;
23096 ++to->vval.v_list->lv_refcount;
23097 }
23098 else
23099 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23100 if (to->vval.v_list == NULL)
23101 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023102 break;
23103 case VAR_DICT:
23104 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023105 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023106 if (from->vval.v_dict == NULL)
23107 to->vval.v_dict = NULL;
23108 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23109 {
23110 /* use the copy made earlier */
23111 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23112 ++to->vval.v_dict->dv_refcount;
23113 }
23114 else
23115 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23116 if (to->vval.v_dict == NULL)
23117 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023118 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023119 case VAR_UNKNOWN:
23120 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023121 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023122 }
23123 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023124 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023125}
23126
23127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 * ":echo expr1 ..." print each argument separated with a space, add a
23129 * newline at the end.
23130 * ":echon expr1 ..." print each argument plain.
23131 */
23132 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023133ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134{
23135 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023136 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023137 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138 char_u *p;
23139 int needclr = TRUE;
23140 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023141 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142
23143 if (eap->skip)
23144 ++emsg_skip;
23145 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23146 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023147 /* If eval1() causes an error message the text from the command may
23148 * still need to be cleared. E.g., "echo 22,44". */
23149 need_clr_eos = needclr;
23150
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023152 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153 {
23154 /*
23155 * Report the invalid expression unless the expression evaluation
23156 * has been cancelled due to an aborting error, an interrupt, or an
23157 * exception.
23158 */
23159 if (!aborting())
23160 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023161 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 break;
23163 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023164 need_clr_eos = FALSE;
23165
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 if (!eap->skip)
23167 {
23168 if (atstart)
23169 {
23170 atstart = FALSE;
23171 /* Call msg_start() after eval1(), evaluating the expression
23172 * may cause a message to appear. */
23173 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023174 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023175 /* Mark the saved text as finishing the line, so that what
23176 * follows is displayed on a new line when scrolling back
23177 * at the more prompt. */
23178 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181 }
23182 else if (eap->cmdidx == CMD_echo)
23183 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023184 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023185 if (p != NULL)
23186 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023188 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023190 if (*p != TAB && needclr)
23191 {
23192 /* remove any text still there from the command */
23193 msg_clr_eos();
23194 needclr = FALSE;
23195 }
23196 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 }
23198 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023199 {
23200#ifdef FEAT_MBYTE
23201 if (has_mbyte)
23202 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023203 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023204
23205 (void)msg_outtrans_len_attr(p, i, echo_attr);
23206 p += i - 1;
23207 }
23208 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023210 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023213 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023215 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 arg = skipwhite(arg);
23217 }
23218 eap->nextcmd = check_nextcmd(arg);
23219
23220 if (eap->skip)
23221 --emsg_skip;
23222 else
23223 {
23224 /* remove text that may still be there from the command */
23225 if (needclr)
23226 msg_clr_eos();
23227 if (eap->cmdidx == CMD_echo)
23228 msg_end();
23229 }
23230}
23231
23232/*
23233 * ":echohl {name}".
23234 */
23235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023236ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237{
23238 int id;
23239
23240 id = syn_name2id(eap->arg);
23241 if (id == 0)
23242 echo_attr = 0;
23243 else
23244 echo_attr = syn_id2attr(id);
23245}
23246
23247/*
23248 * ":execute expr1 ..." execute the result of an expression.
23249 * ":echomsg expr1 ..." Print a message
23250 * ":echoerr expr1 ..." Print an error
23251 * Each gets spaces around each argument and a newline at the end for
23252 * echo commands
23253 */
23254 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023255ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256{
23257 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023258 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023259 int ret = OK;
23260 char_u *p;
23261 garray_T ga;
23262 int len;
23263 int save_did_emsg;
23264
23265 ga_init2(&ga, 1, 80);
23266
23267 if (eap->skip)
23268 ++emsg_skip;
23269 while (*arg != NUL && *arg != '|' && *arg != '\n')
23270 {
23271 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023272 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 {
23274 /*
23275 * Report the invalid expression unless the expression evaluation
23276 * has been cancelled due to an aborting error, an interrupt, or an
23277 * exception.
23278 */
23279 if (!aborting())
23280 EMSG2(_(e_invexpr2), p);
23281 ret = FAIL;
23282 break;
23283 }
23284
23285 if (!eap->skip)
23286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023287 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023288 len = (int)STRLEN(p);
23289 if (ga_grow(&ga, len + 2) == FAIL)
23290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023291 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 ret = FAIL;
23293 break;
23294 }
23295 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298 ga.ga_len += len;
23299 }
23300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023301 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 arg = skipwhite(arg);
23303 }
23304
23305 if (ret != FAIL && ga.ga_data != NULL)
23306 {
23307 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023308 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023310 out_flush();
23311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312 else if (eap->cmdidx == CMD_echoerr)
23313 {
23314 /* We don't want to abort following commands, restore did_emsg. */
23315 save_did_emsg = did_emsg;
23316 EMSG((char_u *)ga.ga_data);
23317 if (!force_abort)
23318 did_emsg = save_did_emsg;
23319 }
23320 else if (eap->cmdidx == CMD_execute)
23321 do_cmdline((char_u *)ga.ga_data,
23322 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23323 }
23324
23325 ga_clear(&ga);
23326
23327 if (eap->skip)
23328 --emsg_skip;
23329
23330 eap->nextcmd = check_nextcmd(arg);
23331}
23332
23333/*
23334 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23335 * "arg" points to the "&" or '+' when called, to "option" when returning.
23336 * Returns NULL when no option name found. Otherwise pointer to the char
23337 * after the option name.
23338 */
23339 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023340find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341{
23342 char_u *p = *arg;
23343
23344 ++p;
23345 if (*p == 'g' && p[1] == ':')
23346 {
23347 *opt_flags = OPT_GLOBAL;
23348 p += 2;
23349 }
23350 else if (*p == 'l' && p[1] == ':')
23351 {
23352 *opt_flags = OPT_LOCAL;
23353 p += 2;
23354 }
23355 else
23356 *opt_flags = 0;
23357
23358 if (!ASCII_ISALPHA(*p))
23359 return NULL;
23360 *arg = p;
23361
23362 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23363 p += 4; /* termcap option */
23364 else
23365 while (ASCII_ISALPHA(*p))
23366 ++p;
23367 return p;
23368}
23369
23370/*
23371 * ":function"
23372 */
23373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023374ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023375{
23376 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023377 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378 int j;
23379 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023381 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 char_u *name = NULL;
23383 char_u *p;
23384 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023385 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023386 garray_T newargs;
23387 garray_T newlines;
23388 int varargs = FALSE;
23389 int mustend = FALSE;
23390 int flags = 0;
23391 ufunc_T *fp;
23392 int indent;
23393 int nesting;
23394 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023395 dictitem_T *v;
23396 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023397 static int func_nr = 0; /* number for nameless function */
23398 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023399 hashtab_T *ht;
23400 int todo;
23401 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023402 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023403
23404 /*
23405 * ":function" without argument: list functions.
23406 */
23407 if (ends_excmd(*eap->arg))
23408 {
23409 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023410 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023411 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023412 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023413 {
23414 if (!HASHITEM_EMPTY(hi))
23415 {
23416 --todo;
23417 fp = HI2UF(hi);
23418 if (!isdigit(*fp->uf_name))
23419 list_func_head(fp, FALSE);
23420 }
23421 }
23422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423 eap->nextcmd = check_nextcmd(eap->arg);
23424 return;
23425 }
23426
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023427 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023428 * ":function /pat": list functions matching pattern.
23429 */
23430 if (*eap->arg == '/')
23431 {
23432 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23433 if (!eap->skip)
23434 {
23435 regmatch_T regmatch;
23436
23437 c = *p;
23438 *p = NUL;
23439 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23440 *p = c;
23441 if (regmatch.regprog != NULL)
23442 {
23443 regmatch.rm_ic = p_ic;
23444
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023445 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023446 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23447 {
23448 if (!HASHITEM_EMPTY(hi))
23449 {
23450 --todo;
23451 fp = HI2UF(hi);
23452 if (!isdigit(*fp->uf_name)
23453 && vim_regexec(&regmatch, fp->uf_name, 0))
23454 list_func_head(fp, FALSE);
23455 }
23456 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023457 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023458 }
23459 }
23460 if (*p == '/')
23461 ++p;
23462 eap->nextcmd = check_nextcmd(p);
23463 return;
23464 }
23465
23466 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023467 * Get the function name. There are these situations:
23468 * func normal function name
23469 * "name" == func, "fudi.fd_dict" == NULL
23470 * dict.func new dictionary entry
23471 * "name" == NULL, "fudi.fd_dict" set,
23472 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23473 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023474 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023475 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23476 * dict.func existing dict entry that's not a Funcref
23477 * "name" == NULL, "fudi.fd_dict" set,
23478 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023479 * s:func script-local function name
23480 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023483 name = trans_function_name(&p, eap->skip, 0, &fudi);
23484 paren = (vim_strchr(p, '(') != NULL);
23485 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486 {
23487 /*
23488 * Return on an invalid expression in braces, unless the expression
23489 * evaluation has been cancelled due to an aborting error, an
23490 * interrupt, or an exception.
23491 */
23492 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023493 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023494 if (!eap->skip && fudi.fd_newkey != NULL)
23495 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023496 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 else
23500 eap->skip = TRUE;
23501 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023502
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503 /* An error in a function call during evaluation of an expression in magic
23504 * braces should not cause the function not to be defined. */
23505 saved_did_emsg = did_emsg;
23506 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023507
23508 /*
23509 * ":function func" with only function name: list function.
23510 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023511 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512 {
23513 if (!ends_excmd(*skipwhite(p)))
23514 {
23515 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023516 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 }
23518 eap->nextcmd = check_nextcmd(p);
23519 if (eap->nextcmd != NULL)
23520 *p = NUL;
23521 if (!eap->skip && !got_int)
23522 {
23523 fp = find_func(name);
23524 if (fp != NULL)
23525 {
23526 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023527 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023529 if (FUNCLINE(fp, j) == NULL)
23530 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023531 msg_putchar('\n');
23532 msg_outnum((long)(j + 1));
23533 if (j < 9)
23534 msg_putchar(' ');
23535 if (j < 99)
23536 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023537 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538 out_flush(); /* show a line at a time */
23539 ui_breakcheck();
23540 }
23541 if (!got_int)
23542 {
23543 msg_putchar('\n');
23544 msg_puts((char_u *)" endfunction");
23545 }
23546 }
23547 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023548 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023550 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 }
23552
23553 /*
23554 * ":function name(arg1, arg2)" Define function.
23555 */
23556 p = skipwhite(p);
23557 if (*p != '(')
23558 {
23559 if (!eap->skip)
23560 {
23561 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023562 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023563 }
23564 /* attempt to continue by skipping some text */
23565 if (vim_strchr(p, '(') != NULL)
23566 p = vim_strchr(p, '(');
23567 }
23568 p = skipwhite(p + 1);
23569
23570 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23571 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23572
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023573 if (!eap->skip)
23574 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023575 /* Check the name of the function. Unless it's a dictionary function
23576 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023577 if (name != NULL)
23578 arg = name;
23579 else
23580 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023581 if (arg != NULL && (fudi.fd_di == NULL
23582 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023583 {
23584 if (*arg == K_SPECIAL)
23585 j = 3;
23586 else
23587 j = 0;
23588 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23589 : eval_isnamec(arg[j])))
23590 ++j;
23591 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023592 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023593 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023594 /* Disallow using the g: dict. */
23595 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23596 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023597 }
23598
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 /*
23600 * Isolate the arguments: "arg1, arg2, ...)"
23601 */
23602 while (*p != ')')
23603 {
23604 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23605 {
23606 varargs = TRUE;
23607 p += 3;
23608 mustend = TRUE;
23609 }
23610 else
23611 {
23612 arg = p;
23613 while (ASCII_ISALNUM(*p) || *p == '_')
23614 ++p;
23615 if (arg == p || isdigit(*arg)
23616 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23617 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23618 {
23619 if (!eap->skip)
23620 EMSG2(_("E125: Illegal argument: %s"), arg);
23621 break;
23622 }
23623 if (ga_grow(&newargs, 1) == FAIL)
23624 goto erret;
23625 c = *p;
23626 *p = NUL;
23627 arg = vim_strsave(arg);
23628 if (arg == NULL)
23629 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023630
23631 /* Check for duplicate argument name. */
23632 for (i = 0; i < newargs.ga_len; ++i)
23633 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23634 {
23635 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023636 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023637 goto erret;
23638 }
23639
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23641 *p = c;
23642 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023643 if (*p == ',')
23644 ++p;
23645 else
23646 mustend = TRUE;
23647 }
23648 p = skipwhite(p);
23649 if (mustend && *p != ')')
23650 {
23651 if (!eap->skip)
23652 EMSG2(_(e_invarg2), eap->arg);
23653 break;
23654 }
23655 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023656 if (*p != ')')
23657 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 ++p; /* skip the ')' */
23659
Bram Moolenaare9a41262005-01-15 22:18:47 +000023660 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023661 for (;;)
23662 {
23663 p = skipwhite(p);
23664 if (STRNCMP(p, "range", 5) == 0)
23665 {
23666 flags |= FC_RANGE;
23667 p += 5;
23668 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023669 else if (STRNCMP(p, "dict", 4) == 0)
23670 {
23671 flags |= FC_DICT;
23672 p += 4;
23673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023674 else if (STRNCMP(p, "abort", 5) == 0)
23675 {
23676 flags |= FC_ABORT;
23677 p += 5;
23678 }
23679 else
23680 break;
23681 }
23682
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023683 /* When there is a line break use what follows for the function body.
23684 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23685 if (*p == '\n')
23686 line_arg = p + 1;
23687 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023688 EMSG(_(e_trailing));
23689
23690 /*
23691 * Read the body of the function, until ":endfunction" is found.
23692 */
23693 if (KeyTyped)
23694 {
23695 /* Check if the function already exists, don't let the user type the
23696 * whole function before telling him it doesn't work! For a script we
23697 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023698 if (!eap->skip && !eap->forceit)
23699 {
23700 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23701 EMSG(_(e_funcdict));
23702 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023703 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023706 if (!eap->skip && did_emsg)
23707 goto erret;
23708
Bram Moolenaar071d4272004-06-13 20:20:40 +000023709 msg_putchar('\n'); /* don't overwrite the function name */
23710 cmdline_row = msg_row;
23711 }
23712
23713 indent = 2;
23714 nesting = 0;
23715 for (;;)
23716 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023717 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023718 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023719 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023720 saved_wait_return = FALSE;
23721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023723 sourcing_lnum_off = sourcing_lnum;
23724
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023725 if (line_arg != NULL)
23726 {
23727 /* Use eap->arg, split up in parts by line breaks. */
23728 theline = line_arg;
23729 p = vim_strchr(theline, '\n');
23730 if (p == NULL)
23731 line_arg += STRLEN(line_arg);
23732 else
23733 {
23734 *p = NUL;
23735 line_arg = p + 1;
23736 }
23737 }
23738 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023739 theline = getcmdline(':', 0L, indent);
23740 else
23741 theline = eap->getline(':', eap->cookie, indent);
23742 if (KeyTyped)
23743 lines_left = Rows - 1;
23744 if (theline == NULL)
23745 {
23746 EMSG(_("E126: Missing :endfunction"));
23747 goto erret;
23748 }
23749
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023750 /* Detect line continuation: sourcing_lnum increased more than one. */
23751 if (sourcing_lnum > sourcing_lnum_off + 1)
23752 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23753 else
23754 sourcing_lnum_off = 0;
23755
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756 if (skip_until != NULL)
23757 {
23758 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23759 * don't check for ":endfunc". */
23760 if (STRCMP(theline, skip_until) == 0)
23761 {
23762 vim_free(skip_until);
23763 skip_until = NULL;
23764 }
23765 }
23766 else
23767 {
23768 /* skip ':' and blanks*/
23769 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23770 ;
23771
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023772 /* Check for "endfunction". */
23773 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023774 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023775 if (line_arg == NULL)
23776 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023777 break;
23778 }
23779
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023780 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023781 * at "end". */
23782 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23783 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023784 else if (STRNCMP(p, "if", 2) == 0
23785 || STRNCMP(p, "wh", 2) == 0
23786 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023787 || STRNCMP(p, "try", 3) == 0)
23788 indent += 2;
23789
23790 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023791 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023793 if (*p == '!')
23794 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023796 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23797 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023799 ++nesting;
23800 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023801 }
23802 }
23803
23804 /* Check for ":append" or ":insert". */
23805 p = skip_range(p, NULL);
23806 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23807 || (p[0] == 'i'
23808 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23809 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23810 skip_until = vim_strsave((char_u *)".");
23811
23812 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23813 arg = skipwhite(skiptowhite(p));
23814 if (arg[0] == '<' && arg[1] =='<'
23815 && ((p[0] == 'p' && p[1] == 'y'
23816 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23817 || (p[0] == 'p' && p[1] == 'e'
23818 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23819 || (p[0] == 't' && p[1] == 'c'
23820 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023821 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23822 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023823 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23824 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023825 || (p[0] == 'm' && p[1] == 'z'
23826 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 ))
23828 {
23829 /* ":python <<" continues until a dot, like ":append" */
23830 p = skipwhite(arg + 2);
23831 if (*p == NUL)
23832 skip_until = vim_strsave((char_u *)".");
23833 else
23834 skip_until = vim_strsave(p);
23835 }
23836 }
23837
23838 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023839 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023840 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023841 if (line_arg == NULL)
23842 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023844 }
23845
23846 /* Copy the line to newly allocated memory. get_one_sourceline()
23847 * allocates 250 bytes per line, this saves 80% on average. The cost
23848 * is an extra alloc/free. */
23849 p = vim_strsave(theline);
23850 if (p != NULL)
23851 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023852 if (line_arg == NULL)
23853 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023854 theline = p;
23855 }
23856
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023857 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23858
23859 /* Add NULL lines for continuation lines, so that the line count is
23860 * equal to the index in the growarray. */
23861 while (sourcing_lnum_off-- > 0)
23862 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023863
23864 /* Check for end of eap->arg. */
23865 if (line_arg != NULL && *line_arg == NUL)
23866 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023867 }
23868
23869 /* Don't define the function when skipping commands or when an error was
23870 * detected. */
23871 if (eap->skip || did_emsg)
23872 goto erret;
23873
23874 /*
23875 * If there are no errors, add the function
23876 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023877 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023878 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023879 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023880 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023881 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023882 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023883 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023884 goto erret;
23885 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023886
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023887 fp = find_func(name);
23888 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023890 if (!eap->forceit)
23891 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023892 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023893 goto erret;
23894 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023895 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023896 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023897 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023898 name);
23899 goto erret;
23900 }
23901 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023902 ga_clear_strings(&(fp->uf_args));
23903 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023904 vim_free(name);
23905 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 }
23908 else
23909 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023910 char numbuf[20];
23911
23912 fp = NULL;
23913 if (fudi.fd_newkey == NULL && !eap->forceit)
23914 {
23915 EMSG(_(e_funcdict));
23916 goto erret;
23917 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023918 if (fudi.fd_di == NULL)
23919 {
23920 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023921 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023922 goto erret;
23923 }
23924 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023925 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023926 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023927
23928 /* Give the function a sequential number. Can only be used with a
23929 * Funcref! */
23930 vim_free(name);
23931 sprintf(numbuf, "%d", ++func_nr);
23932 name = vim_strsave((char_u *)numbuf);
23933 if (name == NULL)
23934 goto erret;
23935 }
23936
23937 if (fp == NULL)
23938 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023939 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023940 {
23941 int slen, plen;
23942 char_u *scriptname;
23943
23944 /* Check that the autoload name matches the script name. */
23945 j = FAIL;
23946 if (sourcing_name != NULL)
23947 {
23948 scriptname = autoload_name(name);
23949 if (scriptname != NULL)
23950 {
23951 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023952 plen = (int)STRLEN(p);
23953 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023954 if (slen > plen && fnamecmp(p,
23955 sourcing_name + slen - plen) == 0)
23956 j = OK;
23957 vim_free(scriptname);
23958 }
23959 }
23960 if (j == FAIL)
23961 {
23962 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23963 goto erret;
23964 }
23965 }
23966
23967 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 if (fp == NULL)
23969 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023970
23971 if (fudi.fd_dict != NULL)
23972 {
23973 if (fudi.fd_di == NULL)
23974 {
23975 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023976 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023977 if (fudi.fd_di == NULL)
23978 {
23979 vim_free(fp);
23980 goto erret;
23981 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023982 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23983 {
23984 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023985 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023986 goto erret;
23987 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023988 }
23989 else
23990 /* overwrite existing dict entry */
23991 clear_tv(&fudi.fd_di->di_tv);
23992 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023993 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023994 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023995 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023996
23997 /* behave like "dict" was used */
23998 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023999 }
24000
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024002 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024003 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24004 {
24005 vim_free(fp);
24006 goto erret;
24007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024009 fp->uf_args = newargs;
24010 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024011#ifdef FEAT_PROFILE
24012 fp->uf_tml_count = NULL;
24013 fp->uf_tml_total = NULL;
24014 fp->uf_tml_self = NULL;
24015 fp->uf_profiling = FALSE;
24016 if (prof_def_func())
24017 func_do_profile(fp);
24018#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024019 fp->uf_varargs = varargs;
24020 fp->uf_flags = flags;
24021 fp->uf_calls = 0;
24022 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024023 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024
24025erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024026 ga_clear_strings(&newargs);
24027 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024028ret_free:
24029 vim_free(skip_until);
24030 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024033 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034}
24035
24036/*
24037 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024038 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024040 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024041 * TFN_INT: internal function name OK
24042 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024043 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024044 * Advances "pp" to just after the function name (if no error).
24045 */
24046 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024047trans_function_name(
24048 char_u **pp,
24049 int skip, /* only find the end, don't evaluate */
24050 int flags,
24051 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024052{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024053 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054 char_u *start;
24055 char_u *end;
24056 int lead;
24057 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024059 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024060
24061 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024062 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024064
24065 /* Check for hard coded <SNR>: already translated function ID (from a user
24066 * command). */
24067 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24068 && (*pp)[2] == (int)KE_SNR)
24069 {
24070 *pp += 3;
24071 len = get_id_len(pp) + 3;
24072 return vim_strnsave(start, len);
24073 }
24074
24075 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24076 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024077 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024078 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024079 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024080
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024081 /* Note that TFN_ flags use the same values as GLV_ flags. */
24082 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024083 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024084 if (end == start)
24085 {
24086 if (!skip)
24087 EMSG(_("E129: Function name required"));
24088 goto theend;
24089 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024090 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024091 {
24092 /*
24093 * Report an invalid expression in braces, unless the expression
24094 * evaluation has been cancelled due to an aborting error, an
24095 * interrupt, or an exception.
24096 */
24097 if (!aborting())
24098 {
24099 if (end != NULL)
24100 EMSG2(_(e_invarg2), start);
24101 }
24102 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024103 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024104 goto theend;
24105 }
24106
24107 if (lv.ll_tv != NULL)
24108 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024109 if (fdp != NULL)
24110 {
24111 fdp->fd_dict = lv.ll_dict;
24112 fdp->fd_newkey = lv.ll_newkey;
24113 lv.ll_newkey = NULL;
24114 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024115 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024116 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24117 {
24118 name = vim_strsave(lv.ll_tv->vval.v_string);
24119 *pp = end;
24120 }
24121 else
24122 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024123 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24124 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024125 EMSG(_(e_funcref));
24126 else
24127 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024128 name = NULL;
24129 }
24130 goto theend;
24131 }
24132
24133 if (lv.ll_name == NULL)
24134 {
24135 /* Error found, but continue after the function name. */
24136 *pp = end;
24137 goto theend;
24138 }
24139
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024140 /* Check if the name is a Funcref. If so, use the value. */
24141 if (lv.ll_exp_name != NULL)
24142 {
24143 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024144 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024145 if (name == lv.ll_exp_name)
24146 name = NULL;
24147 }
24148 else
24149 {
24150 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024151 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024152 if (name == *pp)
24153 name = NULL;
24154 }
24155 if (name != NULL)
24156 {
24157 name = vim_strsave(name);
24158 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024159 if (STRNCMP(name, "<SNR>", 5) == 0)
24160 {
24161 /* Change "<SNR>" to the byte sequence. */
24162 name[0] = K_SPECIAL;
24163 name[1] = KS_EXTRA;
24164 name[2] = (int)KE_SNR;
24165 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24166 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024167 goto theend;
24168 }
24169
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024170 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024171 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024172 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024173 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24174 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24175 {
24176 /* When there was "s:" already or the name expanded to get a
24177 * leading "s:" then remove it. */
24178 lv.ll_name += 2;
24179 len -= 2;
24180 lead = 2;
24181 }
24182 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024183 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024184 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024185 /* skip over "s:" and "g:" */
24186 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024187 lv.ll_name += 2;
24188 len = (int)(end - lv.ll_name);
24189 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024190
24191 /*
24192 * Copy the function name to allocated memory.
24193 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24194 * Accept <SNR>123_name() outside a script.
24195 */
24196 if (skip)
24197 lead = 0; /* do nothing */
24198 else if (lead > 0)
24199 {
24200 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024201 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24202 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024203 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024204 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024205 if (current_SID <= 0)
24206 {
24207 EMSG(_(e_usingsid));
24208 goto theend;
24209 }
24210 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24211 lead += (int)STRLEN(sid_buf);
24212 }
24213 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024214 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024215 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024216 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024217 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024218 goto theend;
24219 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024220 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024221 {
24222 char_u *cp = vim_strchr(lv.ll_name, ':');
24223
24224 if (cp != NULL && cp < end)
24225 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024226 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024227 goto theend;
24228 }
24229 }
24230
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024231 name = alloc((unsigned)(len + lead + 1));
24232 if (name != NULL)
24233 {
24234 if (lead > 0)
24235 {
24236 name[0] = K_SPECIAL;
24237 name[1] = KS_EXTRA;
24238 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024239 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024240 STRCPY(name + 3, sid_buf);
24241 }
24242 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024243 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024244 }
24245 *pp = end;
24246
24247theend:
24248 clear_lval(&lv);
24249 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024250}
24251
24252/*
24253 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24254 * Return 2 if "p" starts with "s:".
24255 * Return 0 otherwise.
24256 */
24257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024258eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024260 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24261 * the standard library function. */
24262 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24263 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024264 return 5;
24265 if (p[0] == 's' && p[1] == ':')
24266 return 2;
24267 return 0;
24268}
24269
24270/*
24271 * Return TRUE if "p" starts with "<SID>" or "s:".
24272 * Only works if eval_fname_script() returned non-zero for "p"!
24273 */
24274 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024275eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024276{
24277 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24278}
24279
24280/*
24281 * List the head of the function: "name(arg1, arg2)".
24282 */
24283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024284list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024285{
24286 int j;
24287
24288 msg_start();
24289 if (indent)
24290 MSG_PUTS(" ");
24291 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024292 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293 {
24294 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024295 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024296 }
24297 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024298 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024299 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024300 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024301 {
24302 if (j)
24303 MSG_PUTS(", ");
24304 msg_puts(FUNCARG(fp, j));
24305 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024306 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307 {
24308 if (j)
24309 MSG_PUTS(", ");
24310 MSG_PUTS("...");
24311 }
24312 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024313 if (fp->uf_flags & FC_ABORT)
24314 MSG_PUTS(" abort");
24315 if (fp->uf_flags & FC_RANGE)
24316 MSG_PUTS(" range");
24317 if (fp->uf_flags & FC_DICT)
24318 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024319 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024320 if (p_verbose > 0)
24321 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322}
24323
24324/*
24325 * Find a function by name, return pointer to it in ufuncs.
24326 * Return NULL for unknown function.
24327 */
24328 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024329find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024330{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024331 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024333 hi = hash_find(&func_hashtab, name);
24334 if (!HASHITEM_EMPTY(hi))
24335 return HI2UF(hi);
24336 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024337}
24338
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024339#if defined(EXITFREE) || defined(PROTO)
24340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024341free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024342{
24343 hashitem_T *hi;
24344
24345 /* Need to start all over every time, because func_free() may change the
24346 * hash table. */
24347 while (func_hashtab.ht_used > 0)
24348 for (hi = func_hashtab.ht_array; ; ++hi)
24349 if (!HASHITEM_EMPTY(hi))
24350 {
24351 func_free(HI2UF(hi));
24352 break;
24353 }
24354}
24355#endif
24356
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024357 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024358translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024359{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024360 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024361 return find_internal_func(name) >= 0;
24362 return find_func(name) != NULL;
24363}
24364
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024365/*
24366 * Return TRUE if a function "name" exists.
24367 */
24368 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024369function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024370{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024371 char_u *nm = name;
24372 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024373 int n = FALSE;
24374
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024375 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24376 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024377 nm = skipwhite(nm);
24378
24379 /* Only accept "funcname", "funcname ", "funcname (..." and
24380 * "funcname(...", not "funcname!...". */
24381 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024382 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024383 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024384 return n;
24385}
24386
Bram Moolenaara1544c02013-05-30 12:35:52 +020024387 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024388get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024389{
24390 char_u *nm = name;
24391 char_u *p;
24392
24393 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24394
24395 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024396 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024397 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024398
Bram Moolenaara1544c02013-05-30 12:35:52 +020024399 vim_free(p);
24400 return NULL;
24401}
24402
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024403/*
24404 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024405 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24406 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024407 */
24408 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024409builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024410{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024411 char_u *p;
24412
24413 if (!ASCII_ISLOWER(name[0]))
24414 return FALSE;
24415 p = vim_strchr(name, AUTOLOAD_CHAR);
24416 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024417}
24418
Bram Moolenaar05159a02005-02-26 23:04:13 +000024419#if defined(FEAT_PROFILE) || defined(PROTO)
24420/*
24421 * Start profiling function "fp".
24422 */
24423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024424func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024425{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024426 int len = fp->uf_lines.ga_len;
24427
24428 if (len == 0)
24429 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024430 fp->uf_tm_count = 0;
24431 profile_zero(&fp->uf_tm_self);
24432 profile_zero(&fp->uf_tm_total);
24433 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024434 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024435 if (fp->uf_tml_total == NULL)
24436 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024437 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024438 if (fp->uf_tml_self == NULL)
24439 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024440 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024441 fp->uf_tml_idx = -1;
24442 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24443 || fp->uf_tml_self == NULL)
24444 return; /* out of memory */
24445
24446 fp->uf_profiling = TRUE;
24447}
24448
24449/*
24450 * Dump the profiling results for all functions in file "fd".
24451 */
24452 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024453func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024454{
24455 hashitem_T *hi;
24456 int todo;
24457 ufunc_T *fp;
24458 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024459 ufunc_T **sorttab;
24460 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024461
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024462 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024463 if (todo == 0)
24464 return; /* nothing to dump */
24465
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024466 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024467
Bram Moolenaar05159a02005-02-26 23:04:13 +000024468 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24469 {
24470 if (!HASHITEM_EMPTY(hi))
24471 {
24472 --todo;
24473 fp = HI2UF(hi);
24474 if (fp->uf_profiling)
24475 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024476 if (sorttab != NULL)
24477 sorttab[st_len++] = fp;
24478
Bram Moolenaar05159a02005-02-26 23:04:13 +000024479 if (fp->uf_name[0] == K_SPECIAL)
24480 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24481 else
24482 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24483 if (fp->uf_tm_count == 1)
24484 fprintf(fd, "Called 1 time\n");
24485 else
24486 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24487 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24488 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24489 fprintf(fd, "\n");
24490 fprintf(fd, "count total (s) self (s)\n");
24491
24492 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24493 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024494 if (FUNCLINE(fp, i) == NULL)
24495 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024496 prof_func_line(fd, fp->uf_tml_count[i],
24497 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024498 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24499 }
24500 fprintf(fd, "\n");
24501 }
24502 }
24503 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024504
24505 if (sorttab != NULL && st_len > 0)
24506 {
24507 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24508 prof_total_cmp);
24509 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24510 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24511 prof_self_cmp);
24512 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24513 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024514
24515 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024516}
Bram Moolenaar73830342005-02-28 22:48:19 +000024517
24518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024519prof_sort_list(
24520 FILE *fd,
24521 ufunc_T **sorttab,
24522 int st_len,
24523 char *title,
24524 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024525{
24526 int i;
24527 ufunc_T *fp;
24528
24529 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24530 fprintf(fd, "count total (s) self (s) function\n");
24531 for (i = 0; i < 20 && i < st_len; ++i)
24532 {
24533 fp = sorttab[i];
24534 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24535 prefer_self);
24536 if (fp->uf_name[0] == K_SPECIAL)
24537 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24538 else
24539 fprintf(fd, " %s()\n", fp->uf_name);
24540 }
24541 fprintf(fd, "\n");
24542}
24543
24544/*
24545 * Print the count and times for one function or function line.
24546 */
24547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024548prof_func_line(
24549 FILE *fd,
24550 int count,
24551 proftime_T *total,
24552 proftime_T *self,
24553 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024554{
24555 if (count > 0)
24556 {
24557 fprintf(fd, "%5d ", count);
24558 if (prefer_self && profile_equal(total, self))
24559 fprintf(fd, " ");
24560 else
24561 fprintf(fd, "%s ", profile_msg(total));
24562 if (!prefer_self && profile_equal(total, self))
24563 fprintf(fd, " ");
24564 else
24565 fprintf(fd, "%s ", profile_msg(self));
24566 }
24567 else
24568 fprintf(fd, " ");
24569}
24570
24571/*
24572 * Compare function for total time sorting.
24573 */
24574 static int
24575#ifdef __BORLANDC__
24576_RTLENTRYF
24577#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024578prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024579{
24580 ufunc_T *p1, *p2;
24581
24582 p1 = *(ufunc_T **)s1;
24583 p2 = *(ufunc_T **)s2;
24584 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24585}
24586
24587/*
24588 * Compare function for self time sorting.
24589 */
24590 static int
24591#ifdef __BORLANDC__
24592_RTLENTRYF
24593#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024594prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024595{
24596 ufunc_T *p1, *p2;
24597
24598 p1 = *(ufunc_T **)s1;
24599 p2 = *(ufunc_T **)s2;
24600 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24601}
24602
Bram Moolenaar05159a02005-02-26 23:04:13 +000024603#endif
24604
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024605/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024606 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024607 * Return TRUE if a package was loaded.
24608 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024609 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024610script_autoload(
24611 char_u *name,
24612 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024613{
24614 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024615 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024616 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024617 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024618
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024619 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024620 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024621 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024622 return FALSE;
24623
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024624 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024625
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024626 /* Find the name in the list of previously loaded package names. Skip
24627 * "autoload/", it's always the same. */
24628 for (i = 0; i < ga_loaded.ga_len; ++i)
24629 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24630 break;
24631 if (!reload && i < ga_loaded.ga_len)
24632 ret = FALSE; /* was loaded already */
24633 else
24634 {
24635 /* Remember the name if it wasn't loaded already. */
24636 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24637 {
24638 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24639 tofree = NULL;
24640 }
24641
24642 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024643 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024644 ret = TRUE;
24645 }
24646
24647 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024648 return ret;
24649}
24650
24651/*
24652 * Return the autoload script name for a function or variable name.
24653 * Returns NULL when out of memory.
24654 */
24655 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024656autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024657{
24658 char_u *p;
24659 char_u *scriptname;
24660
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024661 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024662 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24663 if (scriptname == NULL)
24664 return FALSE;
24665 STRCPY(scriptname, "autoload/");
24666 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024667 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024668 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024669 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024670 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024671 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024672}
24673
Bram Moolenaar071d4272004-06-13 20:20:40 +000024674#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24675
24676/*
24677 * Function given to ExpandGeneric() to obtain the list of user defined
24678 * function names.
24679 */
24680 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024681get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024683 static long_u done;
24684 static hashitem_T *hi;
24685 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024686
24687 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024688 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024689 done = 0;
24690 hi = func_hashtab.ht_array;
24691 }
24692 if (done < func_hashtab.ht_used)
24693 {
24694 if (done++ > 0)
24695 ++hi;
24696 while (HASHITEM_EMPTY(hi))
24697 ++hi;
24698 fp = HI2UF(hi);
24699
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024700 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024701 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024702
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024703 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24704 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024705
24706 cat_func_name(IObuff, fp);
24707 if (xp->xp_context != EXPAND_USER_FUNC)
24708 {
24709 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024710 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024711 STRCAT(IObuff, ")");
24712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024713 return IObuff;
24714 }
24715 return NULL;
24716}
24717
24718#endif /* FEAT_CMDL_COMPL */
24719
24720/*
24721 * Copy the function name of "fp" to buffer "buf".
24722 * "buf" must be able to hold the function name plus three bytes.
24723 * Takes care of script-local function names.
24724 */
24725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024726cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024728 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729 {
24730 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024731 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024732 }
24733 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024734 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024735}
24736
24737/*
24738 * ":delfunction {name}"
24739 */
24740 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024741ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024742{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024743 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024744 char_u *p;
24745 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024746 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747
24748 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024749 name = trans_function_name(&p, eap->skip, 0, &fudi);
24750 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024752 {
24753 if (fudi.fd_dict != NULL && !eap->skip)
24754 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024755 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024757 if (!ends_excmd(*skipwhite(p)))
24758 {
24759 vim_free(name);
24760 EMSG(_(e_trailing));
24761 return;
24762 }
24763 eap->nextcmd = check_nextcmd(p);
24764 if (eap->nextcmd != NULL)
24765 *p = NUL;
24766
24767 if (!eap->skip)
24768 fp = find_func(name);
24769 vim_free(name);
24770
24771 if (!eap->skip)
24772 {
24773 if (fp == NULL)
24774 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024775 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024776 return;
24777 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024778 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779 {
24780 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24781 return;
24782 }
24783
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024784 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024786 /* Delete the dict item that refers to the function, it will
24787 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024788 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024790 else
24791 func_free(fp);
24792 }
24793}
24794
24795/*
24796 * Free a function and remove it from the list of functions.
24797 */
24798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024799func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024800{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024801 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024802
24803 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024804 ga_clear_strings(&(fp->uf_args));
24805 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024806#ifdef FEAT_PROFILE
24807 vim_free(fp->uf_tml_count);
24808 vim_free(fp->uf_tml_total);
24809 vim_free(fp->uf_tml_self);
24810#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024811
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024812 /* remove the function from the function hashtable */
24813 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24814 if (HASHITEM_EMPTY(hi))
24815 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024816 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024817 hash_remove(&func_hashtab, hi);
24818
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024819 vim_free(fp);
24820}
24821
24822/*
24823 * Unreference a Function: decrement the reference count and free it when it
24824 * becomes zero. Only for numbered functions.
24825 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024826 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024827func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024828{
24829 ufunc_T *fp;
24830
24831 if (name != NULL && isdigit(*name))
24832 {
24833 fp = find_func(name);
24834 if (fp == NULL)
24835 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024836 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024837 {
24838 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024839 * when "uf_calls" becomes zero. */
24840 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024841 func_free(fp);
24842 }
24843 }
24844}
24845
24846/*
24847 * Count a reference to a Function.
24848 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024850func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024851{
24852 ufunc_T *fp;
24853
24854 if (name != NULL && isdigit(*name))
24855 {
24856 fp = find_func(name);
24857 if (fp == NULL)
24858 EMSG2(_(e_intern2), "func_ref()");
24859 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024860 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024861 }
24862}
24863
24864/*
24865 * Call a user function.
24866 */
24867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024868call_user_func(
24869 ufunc_T *fp, /* pointer to function */
24870 int argcount, /* nr of args */
24871 typval_T *argvars, /* arguments */
24872 typval_T *rettv, /* return value */
24873 linenr_T firstline, /* first line of range */
24874 linenr_T lastline, /* last line of range */
24875 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024876{
Bram Moolenaar33570922005-01-25 22:26:29 +000024877 char_u *save_sourcing_name;
24878 linenr_T save_sourcing_lnum;
24879 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024880 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024881 int save_did_emsg;
24882 static int depth = 0;
24883 dictitem_T *v;
24884 int fixvar_idx = 0; /* index in fixvar[] */
24885 int i;
24886 int ai;
24887 char_u numbuf[NUMBUFLEN];
24888 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024889 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024890#ifdef FEAT_PROFILE
24891 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024892 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894
24895 /* If depth of calling is getting too high, don't execute the function */
24896 if (depth >= p_mfd)
24897 {
24898 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024899 rettv->v_type = VAR_NUMBER;
24900 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024901 return;
24902 }
24903 ++depth;
24904
24905 line_breakcheck(); /* check for CTRL-C hit */
24906
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024907 fc = (funccall_T *)alloc(sizeof(funccall_T));
24908 fc->caller = current_funccal;
24909 current_funccal = fc;
24910 fc->func = fp;
24911 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024912 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024913 fc->linenr = 0;
24914 fc->returned = FALSE;
24915 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024917 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24918 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919
Bram Moolenaar33570922005-01-25 22:26:29 +000024920 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024921 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024922 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24923 * each argument variable and saves a lot of time.
24924 */
24925 /*
24926 * Init l: variables.
24927 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024928 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024929 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024930 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024931 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24932 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024933 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024934 name = v->di_key;
24935 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024936 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024937 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024938 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024939 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024940 v->di_tv.vval.v_dict = selfdict;
24941 ++selfdict->dv_refcount;
24942 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024943
Bram Moolenaar33570922005-01-25 22:26:29 +000024944 /*
24945 * Init a: variables.
24946 * Set a:0 to "argcount".
24947 * Set a:000 to a list with room for the "..." arguments.
24948 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024949 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024950 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024951 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024952 /* Use "name" to avoid a warning from some compiler that checks the
24953 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024954 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024955 name = v->di_key;
24956 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024957 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024958 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024959 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024960 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024961 v->di_tv.vval.v_list = &fc->l_varlist;
24962 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24963 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24964 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024965
24966 /*
24967 * Set a:firstline to "firstline" and a:lastline to "lastline".
24968 * Set a:name to named arguments.
24969 * Set a:N to the "..." arguments.
24970 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024971 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024972 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024973 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024974 (varnumber_T)lastline);
24975 for (i = 0; i < argcount; ++i)
24976 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024977 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024978 if (ai < 0)
24979 /* named argument a:name */
24980 name = FUNCARG(fp, i);
24981 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024982 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024983 /* "..." argument a:1, a:2, etc. */
24984 sprintf((char *)numbuf, "%d", ai + 1);
24985 name = numbuf;
24986 }
24987 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24988 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024989 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024990 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24991 }
24992 else
24993 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024994 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24995 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024996 if (v == NULL)
24997 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024998 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024999 }
25000 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025001 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025002
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025003 /* Note: the values are copied directly to avoid alloc/free.
25004 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025005 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025006 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025007
25008 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25009 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025010 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25011 fc->l_listitems[ai].li_tv = argvars[i];
25012 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025013 }
25014 }
25015
Bram Moolenaar071d4272004-06-13 20:20:40 +000025016 /* Don't redraw while executing the function. */
25017 ++RedrawingDisabled;
25018 save_sourcing_name = sourcing_name;
25019 save_sourcing_lnum = sourcing_lnum;
25020 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025021 /* need space for function name + ("function " + 3) or "[number]" */
25022 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25023 + STRLEN(fp->uf_name) + 20;
25024 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025025 if (sourcing_name != NULL)
25026 {
25027 if (save_sourcing_name != NULL
25028 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025029 sprintf((char *)sourcing_name, "%s[%d]..",
25030 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031 else
25032 STRCPY(sourcing_name, "function ");
25033 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25034
25035 if (p_verbose >= 12)
25036 {
25037 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025038 verbose_enter_scroll();
25039
Bram Moolenaar555b2802005-05-19 21:08:39 +000025040 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025041 if (p_verbose >= 14)
25042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025044 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025045 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025046 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025047
25048 msg_puts((char_u *)"(");
25049 for (i = 0; i < argcount; ++i)
25050 {
25051 if (i > 0)
25052 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025053 if (argvars[i].v_type == VAR_NUMBER)
25054 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025055 else
25056 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025057 /* Do not want errors such as E724 here. */
25058 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025059 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025060 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025061 if (s != NULL)
25062 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025063 if (vim_strsize(s) > MSG_BUF_CLEN)
25064 {
25065 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25066 s = buf;
25067 }
25068 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025069 vim_free(tofree);
25070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071 }
25072 }
25073 msg_puts((char_u *)")");
25074 }
25075 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025076
25077 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025078 --no_wait_return;
25079 }
25080 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025081#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025082 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025083 {
25084 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25085 func_do_profile(fp);
25086 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025087 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025088 {
25089 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025090 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025091 profile_zero(&fp->uf_tm_children);
25092 }
25093 script_prof_save(&wait_start);
25094 }
25095#endif
25096
Bram Moolenaar071d4272004-06-13 20:20:40 +000025097 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025098 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025099 save_did_emsg = did_emsg;
25100 did_emsg = FALSE;
25101
25102 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025103 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25105
25106 --RedrawingDisabled;
25107
25108 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025109 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025111 clear_tv(rettv);
25112 rettv->v_type = VAR_NUMBER;
25113 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025114 }
25115
Bram Moolenaar05159a02005-02-26 23:04:13 +000025116#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025117 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025118 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025119 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025120 profile_end(&call_start);
25121 profile_sub_wait(&wait_start, &call_start);
25122 profile_add(&fp->uf_tm_total, &call_start);
25123 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025124 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025125 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025126 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25127 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025128 }
25129 }
25130#endif
25131
Bram Moolenaar071d4272004-06-13 20:20:40 +000025132 /* when being verbose, mention the return value */
25133 if (p_verbose >= 12)
25134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025135 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025136 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025137
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025139 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025140 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025141 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025142 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025143 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025144 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025145 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025146 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025147 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025148 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025149
Bram Moolenaar555b2802005-05-19 21:08:39 +000025150 /* The value may be very long. Skip the middle part, so that we
25151 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025152 * truncate it at the end. Don't want errors such as E724 here. */
25153 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025154 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025155 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025156 if (s != NULL)
25157 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025158 if (vim_strsize(s) > MSG_BUF_CLEN)
25159 {
25160 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25161 s = buf;
25162 }
25163 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025164 vim_free(tofree);
25165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025166 }
25167 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025168
25169 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025170 --no_wait_return;
25171 }
25172
25173 vim_free(sourcing_name);
25174 sourcing_name = save_sourcing_name;
25175 sourcing_lnum = save_sourcing_lnum;
25176 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025177#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025178 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025179 script_prof_restore(&wait_start);
25180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181
25182 if (p_verbose >= 12 && sourcing_name != NULL)
25183 {
25184 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025185 verbose_enter_scroll();
25186
Bram Moolenaar555b2802005-05-19 21:08:39 +000025187 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025188 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025189
25190 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025191 --no_wait_return;
25192 }
25193
25194 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025195 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025196 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025197
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025198 /* If the a:000 list and the l: and a: dicts are not referenced we can
25199 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025200 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25201 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25202 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25203 {
25204 free_funccal(fc, FALSE);
25205 }
25206 else
25207 {
25208 hashitem_T *hi;
25209 listitem_T *li;
25210 int todo;
25211
25212 /* "fc" is still in use. This can happen when returning "a:000" or
25213 * assigning "l:" to a global variable.
25214 * Link "fc" in the list for garbage collection later. */
25215 fc->caller = previous_funccal;
25216 previous_funccal = fc;
25217
25218 /* Make a copy of the a: variables, since we didn't do that above. */
25219 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25220 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25221 {
25222 if (!HASHITEM_EMPTY(hi))
25223 {
25224 --todo;
25225 v = HI2DI(hi);
25226 copy_tv(&v->di_tv, &v->di_tv);
25227 }
25228 }
25229
25230 /* Make a copy of the a:000 items, since we didn't do that above. */
25231 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25232 copy_tv(&li->li_tv, &li->li_tv);
25233 }
25234}
25235
25236/*
25237 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025238 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025239 */
25240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025241can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025242{
25243 return (fc->l_varlist.lv_copyID != copyID
25244 && fc->l_vars.dv_copyID != copyID
25245 && fc->l_avars.dv_copyID != copyID);
25246}
25247
25248/*
25249 * Free "fc" and what it contains.
25250 */
25251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025252free_funccal(
25253 funccall_T *fc,
25254 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025255{
25256 listitem_T *li;
25257
25258 /* The a: variables typevals may not have been allocated, only free the
25259 * allocated variables. */
25260 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25261
25262 /* free all l: variables */
25263 vars_clear(&fc->l_vars.dv_hashtab);
25264
25265 /* Free the a:000 variables if they were allocated. */
25266 if (free_val)
25267 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25268 clear_tv(&li->li_tv);
25269
25270 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271}
25272
25273/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025274 * Add a number variable "name" to dict "dp" with value "nr".
25275 */
25276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025277add_nr_var(
25278 dict_T *dp,
25279 dictitem_T *v,
25280 char *name,
25281 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025282{
25283 STRCPY(v->di_key, name);
25284 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25285 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25286 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025287 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025288 v->di_tv.vval.v_number = nr;
25289}
25290
25291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 * ":return [expr]"
25293 */
25294 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025295ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296{
25297 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025298 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025299 int returning = FALSE;
25300
25301 if (current_funccal == NULL)
25302 {
25303 EMSG(_("E133: :return not inside a function"));
25304 return;
25305 }
25306
25307 if (eap->skip)
25308 ++emsg_skip;
25309
25310 eap->nextcmd = NULL;
25311 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025312 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025313 {
25314 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025315 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025316 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025317 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025318 }
25319 /* It's safer to return also on error. */
25320 else if (!eap->skip)
25321 {
25322 /*
25323 * Return unless the expression evaluation has been cancelled due to an
25324 * aborting error, an interrupt, or an exception.
25325 */
25326 if (!aborting())
25327 returning = do_return(eap, FALSE, TRUE, NULL);
25328 }
25329
25330 /* When skipping or the return gets pending, advance to the next command
25331 * in this line (!returning). Otherwise, ignore the rest of the line.
25332 * Following lines will be ignored by get_func_line(). */
25333 if (returning)
25334 eap->nextcmd = NULL;
25335 else if (eap->nextcmd == NULL) /* no argument */
25336 eap->nextcmd = check_nextcmd(arg);
25337
25338 if (eap->skip)
25339 --emsg_skip;
25340}
25341
25342/*
25343 * Return from a function. Possibly makes the return pending. Also called
25344 * for a pending return at the ":endtry" or after returning from an extra
25345 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025346 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025347 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025348 * FALSE when the return gets pending.
25349 */
25350 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025351do_return(
25352 exarg_T *eap,
25353 int reanimate,
25354 int is_cmd,
25355 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025356{
25357 int idx;
25358 struct condstack *cstack = eap->cstack;
25359
25360 if (reanimate)
25361 /* Undo the return. */
25362 current_funccal->returned = FALSE;
25363
25364 /*
25365 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25366 * not in its finally clause (which then is to be executed next) is found.
25367 * In this case, make the ":return" pending for execution at the ":endtry".
25368 * Otherwise, return normally.
25369 */
25370 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25371 if (idx >= 0)
25372 {
25373 cstack->cs_pending[idx] = CSTP_RETURN;
25374
25375 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025376 /* A pending return again gets pending. "rettv" points to an
25377 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025378 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025379 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380 else
25381 {
25382 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025383 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025384 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025385 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025387 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025388 {
25389 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025390 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025391 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025392 else
25393 EMSG(_(e_outofmem));
25394 }
25395 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025396 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025397
25398 if (reanimate)
25399 {
25400 /* The pending return value could be overwritten by a ":return"
25401 * without argument in a finally clause; reset the default
25402 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025403 current_funccal->rettv->v_type = VAR_NUMBER;
25404 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025405 }
25406 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025407 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025408 }
25409 else
25410 {
25411 current_funccal->returned = TRUE;
25412
25413 /* If the return is carried out now, store the return value. For
25414 * a return immediately after reanimation, the value is already
25415 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025416 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025418 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025419 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025420 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025421 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025422 }
25423 }
25424
25425 return idx < 0;
25426}
25427
25428/*
25429 * Free the variable with a pending return value.
25430 */
25431 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025432discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025433{
Bram Moolenaar33570922005-01-25 22:26:29 +000025434 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435}
25436
25437/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025438 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025439 * is an allocated string. Used by report_pending() for verbose messages.
25440 */
25441 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025442get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025444 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025445 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025446 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025448 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025449 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025450 if (s == NULL)
25451 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025452
25453 STRCPY(IObuff, ":return ");
25454 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25455 if (STRLEN(s) + 8 >= IOSIZE)
25456 STRCPY(IObuff + IOSIZE - 4, "...");
25457 vim_free(tofree);
25458 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025459}
25460
25461/*
25462 * Get next function line.
25463 * Called by do_cmdline() to get the next line.
25464 * Returns allocated string, or NULL for end of function.
25465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025466 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025467get_func_line(
25468 int c UNUSED,
25469 void *cookie,
25470 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471{
Bram Moolenaar33570922005-01-25 22:26:29 +000025472 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025473 ufunc_T *fp = fcp->func;
25474 char_u *retval;
25475 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476
25477 /* If breakpoints have been added/deleted need to check for it. */
25478 if (fcp->dbg_tick != debug_tick)
25479 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025480 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481 sourcing_lnum);
25482 fcp->dbg_tick = debug_tick;
25483 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025484#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025485 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025486 func_line_end(cookie);
25487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025488
Bram Moolenaar05159a02005-02-26 23:04:13 +000025489 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025490 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25491 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025492 retval = NULL;
25493 else
25494 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025495 /* Skip NULL lines (continuation lines). */
25496 while (fcp->linenr < gap->ga_len
25497 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25498 ++fcp->linenr;
25499 if (fcp->linenr >= gap->ga_len)
25500 retval = NULL;
25501 else
25502 {
25503 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25504 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025505#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025506 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025507 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025508#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025510 }
25511
25512 /* Did we encounter a breakpoint? */
25513 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25514 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025515 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025517 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025518 sourcing_lnum);
25519 fcp->dbg_tick = debug_tick;
25520 }
25521
25522 return retval;
25523}
25524
Bram Moolenaar05159a02005-02-26 23:04:13 +000025525#if defined(FEAT_PROFILE) || defined(PROTO)
25526/*
25527 * Called when starting to read a function line.
25528 * "sourcing_lnum" must be correct!
25529 * When skipping lines it may not actually be executed, but we won't find out
25530 * until later and we need to store the time now.
25531 */
25532 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025533func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025534{
25535 funccall_T *fcp = (funccall_T *)cookie;
25536 ufunc_T *fp = fcp->func;
25537
25538 if (fp->uf_profiling && sourcing_lnum >= 1
25539 && sourcing_lnum <= fp->uf_lines.ga_len)
25540 {
25541 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025542 /* Skip continuation lines. */
25543 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25544 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025545 fp->uf_tml_execed = FALSE;
25546 profile_start(&fp->uf_tml_start);
25547 profile_zero(&fp->uf_tml_children);
25548 profile_get_wait(&fp->uf_tml_wait);
25549 }
25550}
25551
25552/*
25553 * Called when actually executing a function line.
25554 */
25555 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025556func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025557{
25558 funccall_T *fcp = (funccall_T *)cookie;
25559 ufunc_T *fp = fcp->func;
25560
25561 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25562 fp->uf_tml_execed = TRUE;
25563}
25564
25565/*
25566 * Called when done with a function line.
25567 */
25568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025569func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025570{
25571 funccall_T *fcp = (funccall_T *)cookie;
25572 ufunc_T *fp = fcp->func;
25573
25574 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25575 {
25576 if (fp->uf_tml_execed)
25577 {
25578 ++fp->uf_tml_count[fp->uf_tml_idx];
25579 profile_end(&fp->uf_tml_start);
25580 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025581 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025582 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25583 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025584 }
25585 fp->uf_tml_idx = -1;
25586 }
25587}
25588#endif
25589
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590/*
25591 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025592 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025593 */
25594 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025595func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025596{
Bram Moolenaar33570922005-01-25 22:26:29 +000025597 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598
25599 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25600 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025601 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025602 || fcp->returned);
25603}
25604
25605/*
25606 * return TRUE if cookie indicates a function which "abort"s on errors.
25607 */
25608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025609func_has_abort(
25610 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025612 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025613}
25614
25615#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25616typedef enum
25617{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025618 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25619 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25620 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025621} var_flavour_T;
25622
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025623static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025624
25625 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025626var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025627{
25628 char_u *p = varname;
25629
25630 if (ASCII_ISUPPER(*p))
25631 {
25632 while (*(++p))
25633 if (ASCII_ISLOWER(*p))
25634 return VAR_FLAVOUR_SESSION;
25635 return VAR_FLAVOUR_VIMINFO;
25636 }
25637 else
25638 return VAR_FLAVOUR_DEFAULT;
25639}
25640#endif
25641
25642#if defined(FEAT_VIMINFO) || defined(PROTO)
25643/*
25644 * Restore global vars that start with a capital from the viminfo file
25645 */
25646 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025647read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648{
25649 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025650 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025651 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025652 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653
25654 if (!writing && (find_viminfo_parameter('!') != NULL))
25655 {
25656 tab = vim_strchr(virp->vir_line + 1, '\t');
25657 if (tab != NULL)
25658 {
25659 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025660 switch (*tab)
25661 {
25662 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025663#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025664 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025665#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025666 case 'D': type = VAR_DICT; break;
25667 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025668 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670
25671 tab = vim_strchr(tab, '\t');
25672 if (tab != NULL)
25673 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025674 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025675 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025676 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025677 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025678#ifdef FEAT_FLOAT
25679 else if (type == VAR_FLOAT)
25680 (void)string2float(tab + 1, &tv.vval.v_float);
25681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025682 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025683 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025684 if (type == VAR_DICT || type == VAR_LIST)
25685 {
25686 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25687
25688 if (etv == NULL)
25689 /* Failed to parse back the dict or list, use it as a
25690 * string. */
25691 tv.v_type = VAR_STRING;
25692 else
25693 {
25694 vim_free(tv.vval.v_string);
25695 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025696 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025697 }
25698 }
25699
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025700 /* when in a function use global variables */
25701 save_funccal = current_funccal;
25702 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025703 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025704 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025705
25706 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025707 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025708 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25709 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025710 }
25711 }
25712 }
25713
25714 return viminfo_readline(virp);
25715}
25716
25717/*
25718 * Write global vars that start with a capital to the viminfo file
25719 */
25720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025721write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025722{
Bram Moolenaar33570922005-01-25 22:26:29 +000025723 hashitem_T *hi;
25724 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025725 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025726 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025727 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025728 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025729 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025730
25731 if (find_viminfo_parameter('!') == NULL)
25732 return;
25733
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025734 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025735
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025736 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025737 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025739 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025740 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025741 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025742 this_var = HI2DI(hi);
25743 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025744 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025745 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025746 {
25747 case VAR_STRING: s = "STR"; break;
25748 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025749 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025750 case VAR_DICT: s = "DIC"; break;
25751 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025752 case VAR_SPECIAL: s = "XPL"; break;
25753
25754 case VAR_UNKNOWN:
25755 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025756 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025757 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025758 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025759 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025760 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025761 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025762 if (p != NULL)
25763 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025764 vim_free(tofree);
25765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766 }
25767 }
25768}
25769#endif
25770
25771#if defined(FEAT_SESSION) || defined(PROTO)
25772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025773store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774{
Bram Moolenaar33570922005-01-25 22:26:29 +000025775 hashitem_T *hi;
25776 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025777 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778 char_u *p, *t;
25779
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025780 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025781 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025782 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025783 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025784 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025785 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025786 this_var = HI2DI(hi);
25787 if ((this_var->di_tv.v_type == VAR_NUMBER
25788 || this_var->di_tv.v_type == VAR_STRING)
25789 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025790 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025791 /* Escape special characters with a backslash. Turn a LF and
25792 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025793 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025794 (char_u *)"\\\"\n\r");
25795 if (p == NULL) /* out of memory */
25796 break;
25797 for (t = p; *t != NUL; ++t)
25798 if (*t == '\n')
25799 *t = 'n';
25800 else if (*t == '\r')
25801 *t = 'r';
25802 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025803 this_var->di_key,
25804 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25805 : ' ',
25806 p,
25807 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25808 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025809 || put_eol(fd) == FAIL)
25810 {
25811 vim_free(p);
25812 return FAIL;
25813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025814 vim_free(p);
25815 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025816#ifdef FEAT_FLOAT
25817 else if (this_var->di_tv.v_type == VAR_FLOAT
25818 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25819 {
25820 float_T f = this_var->di_tv.vval.v_float;
25821 int sign = ' ';
25822
25823 if (f < 0)
25824 {
25825 f = -f;
25826 sign = '-';
25827 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025828 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025829 this_var->di_key, sign, f) < 0)
25830 || put_eol(fd) == FAIL)
25831 return FAIL;
25832 }
25833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834 }
25835 }
25836 return OK;
25837}
25838#endif
25839
Bram Moolenaar661b1822005-07-28 22:36:45 +000025840/*
25841 * Display script name where an item was last set.
25842 * Should only be invoked when 'verbose' is non-zero.
25843 */
25844 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025845last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025846{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025847 char_u *p;
25848
Bram Moolenaar661b1822005-07-28 22:36:45 +000025849 if (scriptID != 0)
25850 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025851 p = home_replace_save(NULL, get_scriptname(scriptID));
25852 if (p != NULL)
25853 {
25854 verbose_enter();
25855 MSG_PUTS(_("\n\tLast set from "));
25856 MSG_PUTS(p);
25857 vim_free(p);
25858 verbose_leave();
25859 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025860 }
25861}
25862
Bram Moolenaard812df62008-11-09 12:46:09 +000025863/*
25864 * List v:oldfiles in a nice way.
25865 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025866 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025867ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025868{
25869 list_T *l = vimvars[VV_OLDFILES].vv_list;
25870 listitem_T *li;
25871 int nr = 0;
25872
25873 if (l == NULL)
25874 msg((char_u *)_("No old files"));
25875 else
25876 {
25877 msg_start();
25878 msg_scroll = TRUE;
25879 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25880 {
25881 msg_outnum((long)++nr);
25882 MSG_PUTS(": ");
25883 msg_outtrans(get_tv_string(&li->li_tv));
25884 msg_putchar('\n');
25885 out_flush(); /* output one line at a time */
25886 ui_breakcheck();
25887 }
25888 /* Assume "got_int" was set to truncate the listing. */
25889 got_int = FALSE;
25890
25891#ifdef FEAT_BROWSE_CMD
25892 if (cmdmod.browse)
25893 {
25894 quit_more = FALSE;
25895 nr = prompt_for_number(FALSE);
25896 msg_starthere();
25897 if (nr > 0)
25898 {
25899 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25900 (long)nr);
25901
25902 if (p != NULL)
25903 {
25904 p = expand_env_save(p);
25905 eap->arg = p;
25906 eap->cmdidx = CMD_edit;
25907 cmdmod.browse = FALSE;
25908 do_exedit(eap, NULL);
25909 vim_free(p);
25910 }
25911 }
25912 }
25913#endif
25914 }
25915}
25916
Bram Moolenaar53744302015-07-17 17:38:22 +020025917/* reset v:option_new, v:option_old and v:option_type */
25918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025919reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025920{
25921 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25922 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25923 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25924}
25925
25926
Bram Moolenaar071d4272004-06-13 20:20:40 +000025927#endif /* FEAT_EVAL */
25928
Bram Moolenaar071d4272004-06-13 20:20:40 +000025929
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025930#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025931
25932#ifdef WIN3264
25933/*
25934 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25935 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025936static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25937static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25938static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025939
25940/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025941 * Get the short path (8.3) for the filename in "fnamep".
25942 * Only works for a valid file name.
25943 * When the path gets longer "fnamep" is changed and the allocated buffer
25944 * is put in "bufp".
25945 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25946 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947 */
25948 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025949get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025950{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025951 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025952 char_u *newbuf;
25953
25954 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025955 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025956 if (l > len - 1)
25957 {
25958 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025959 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025960 newbuf = vim_strnsave(*fnamep, l);
25961 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025962 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025963
25964 vim_free(*bufp);
25965 *fnamep = *bufp = newbuf;
25966
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025967 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025968 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025969 }
25970
25971 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025972 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025973}
25974
25975/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025976 * Get the short path (8.3) for the filename in "fname". The converted
25977 * path is returned in "bufp".
25978 *
25979 * Some of the directories specified in "fname" may not exist. This function
25980 * will shorten the existing directories at the beginning of the path and then
25981 * append the remaining non-existing path.
25982 *
25983 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025984 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025985 * bufp - Pointer to an allocated buffer for the filename.
25986 * fnamelen - Length of the filename pointed to by fname
25987 *
25988 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025989 */
25990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025991shortpath_for_invalid_fname(
25992 char_u **fname,
25993 char_u **bufp,
25994 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025995{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025996 char_u *short_fname, *save_fname, *pbuf_unused;
25997 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025998 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025999 int old_len, len;
26000 int new_len, sfx_len;
26001 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026002
26003 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026004 old_len = *fnamelen;
26005 save_fname = vim_strnsave(*fname, old_len);
26006 pbuf_unused = NULL;
26007 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026008
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026009 endp = save_fname + old_len - 1; /* Find the end of the copy */
26010 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026011
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026012 /*
26013 * Try shortening the supplied path till it succeeds by removing one
26014 * directory at a time from the tail of the path.
26015 */
26016 len = 0;
26017 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026018 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026019 /* go back one path-separator */
26020 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26021 --endp;
26022 if (endp <= save_fname)
26023 break; /* processed the complete path */
26024
26025 /*
26026 * Replace the path separator with a NUL and try to shorten the
26027 * resulting path.
26028 */
26029 ch = *endp;
26030 *endp = 0;
26031 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026032 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026033 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26034 {
26035 retval = FAIL;
26036 goto theend;
26037 }
26038 *endp = ch; /* preserve the string */
26039
26040 if (len > 0)
26041 break; /* successfully shortened the path */
26042
26043 /* failed to shorten the path. Skip the path separator */
26044 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026045 }
26046
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026047 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026048 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026049 /*
26050 * Succeeded in shortening the path. Now concatenate the shortened
26051 * path with the remaining path at the tail.
26052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026053
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026054 /* Compute the length of the new path. */
26055 sfx_len = (int)(save_endp - endp) + 1;
26056 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026057
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026058 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026059 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026060 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026061 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026062 /* There is not enough space in the currently allocated string,
26063 * copy it to a buffer big enough. */
26064 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026065 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026066 {
26067 retval = FAIL;
26068 goto theend;
26069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026070 }
26071 else
26072 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026073 /* Transfer short_fname to the main buffer (it's big enough),
26074 * unless get_short_pathname() did its work in-place. */
26075 *fname = *bufp = save_fname;
26076 if (short_fname != save_fname)
26077 vim_strncpy(save_fname, short_fname, len);
26078 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026079 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026080
26081 /* concat the not-shortened part of the path */
26082 vim_strncpy(*fname + len, endp, sfx_len);
26083 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026084 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026085
26086theend:
26087 vim_free(pbuf_unused);
26088 vim_free(save_fname);
26089
26090 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026091}
26092
26093/*
26094 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026095 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026096 */
26097 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026098shortpath_for_partial(
26099 char_u **fnamep,
26100 char_u **bufp,
26101 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026102{
26103 int sepcount, len, tflen;
26104 char_u *p;
26105 char_u *pbuf, *tfname;
26106 int hasTilde;
26107
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026108 /* Count up the path separators from the RHS.. so we know which part
26109 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026110 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026111 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026112 if (vim_ispathsep(*p))
26113 ++sepcount;
26114
26115 /* Need full path first (use expand_env() to remove a "~/") */
26116 hasTilde = (**fnamep == '~');
26117 if (hasTilde)
26118 pbuf = tfname = expand_env_save(*fnamep);
26119 else
26120 pbuf = tfname = FullName_save(*fnamep, FALSE);
26121
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026122 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026123
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026124 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26125 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026126
26127 if (len == 0)
26128 {
26129 /* Don't have a valid filename, so shorten the rest of the
26130 * path if we can. This CAN give us invalid 8.3 filenames, but
26131 * there's not a lot of point in guessing what it might be.
26132 */
26133 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026134 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26135 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026136 }
26137
26138 /* Count the paths backward to find the beginning of the desired string. */
26139 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026140 {
26141#ifdef FEAT_MBYTE
26142 if (has_mbyte)
26143 p -= mb_head_off(tfname, p);
26144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026145 if (vim_ispathsep(*p))
26146 {
26147 if (sepcount == 0 || (hasTilde && sepcount == 1))
26148 break;
26149 else
26150 sepcount --;
26151 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026153 if (hasTilde)
26154 {
26155 --p;
26156 if (p >= tfname)
26157 *p = '~';
26158 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026159 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160 }
26161 else
26162 ++p;
26163
26164 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26165 vim_free(*bufp);
26166 *fnamelen = (int)STRLEN(p);
26167 *bufp = pbuf;
26168 *fnamep = p;
26169
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026170 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026171}
26172#endif /* WIN3264 */
26173
26174/*
26175 * Adjust a filename, according to a string of modifiers.
26176 * *fnamep must be NUL terminated when called. When returning, the length is
26177 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026178 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026179 * When there is an error, *fnamep is set to NULL.
26180 */
26181 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026182modify_fname(
26183 char_u *src, /* string with modifiers */
26184 int *usedlen, /* characters after src that are used */
26185 char_u **fnamep, /* file name so far */
26186 char_u **bufp, /* buffer for allocated file name or NULL */
26187 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026188{
26189 int valid = 0;
26190 char_u *tail;
26191 char_u *s, *p, *pbuf;
26192 char_u dirname[MAXPATHL];
26193 int c;
26194 int has_fullname = 0;
26195#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026196 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026197 int has_shortname = 0;
26198#endif
26199
26200repeat:
26201 /* ":p" - full path/file_name */
26202 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26203 {
26204 has_fullname = 1;
26205
26206 valid |= VALID_PATH;
26207 *usedlen += 2;
26208
26209 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26210 if ((*fnamep)[0] == '~'
26211#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26212 && ((*fnamep)[1] == '/'
26213# ifdef BACKSLASH_IN_FILENAME
26214 || (*fnamep)[1] == '\\'
26215# endif
26216 || (*fnamep)[1] == NUL)
26217
26218#endif
26219 )
26220 {
26221 *fnamep = expand_env_save(*fnamep);
26222 vim_free(*bufp); /* free any allocated file name */
26223 *bufp = *fnamep;
26224 if (*fnamep == NULL)
26225 return -1;
26226 }
26227
26228 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026229 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026230 {
26231 if (vim_ispathsep(*p)
26232 && p[1] == '.'
26233 && (p[2] == NUL
26234 || vim_ispathsep(p[2])
26235 || (p[2] == '.'
26236 && (p[3] == NUL || vim_ispathsep(p[3])))))
26237 break;
26238 }
26239
26240 /* FullName_save() is slow, don't use it when not needed. */
26241 if (*p != NUL || !vim_isAbsName(*fnamep))
26242 {
26243 *fnamep = FullName_save(*fnamep, *p != NUL);
26244 vim_free(*bufp); /* free any allocated file name */
26245 *bufp = *fnamep;
26246 if (*fnamep == NULL)
26247 return -1;
26248 }
26249
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026250#ifdef WIN3264
26251# if _WIN32_WINNT >= 0x0500
26252 if (vim_strchr(*fnamep, '~') != NULL)
26253 {
26254 /* Expand 8.3 filename to full path. Needed to make sure the same
26255 * file does not have two different names.
26256 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26257 p = alloc(_MAX_PATH + 1);
26258 if (p != NULL)
26259 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026260 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026261 {
26262 vim_free(*bufp);
26263 *bufp = *fnamep = p;
26264 }
26265 else
26266 vim_free(p);
26267 }
26268 }
26269# endif
26270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026271 /* Append a path separator to a directory. */
26272 if (mch_isdir(*fnamep))
26273 {
26274 /* Make room for one or two extra characters. */
26275 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26276 vim_free(*bufp); /* free any allocated file name */
26277 *bufp = *fnamep;
26278 if (*fnamep == NULL)
26279 return -1;
26280 add_pathsep(*fnamep);
26281 }
26282 }
26283
26284 /* ":." - path relative to the current directory */
26285 /* ":~" - path relative to the home directory */
26286 /* ":8" - shortname path - postponed till after */
26287 while (src[*usedlen] == ':'
26288 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26289 {
26290 *usedlen += 2;
26291 if (c == '8')
26292 {
26293#ifdef WIN3264
26294 has_shortname = 1; /* Postpone this. */
26295#endif
26296 continue;
26297 }
26298 pbuf = NULL;
26299 /* Need full path first (use expand_env() to remove a "~/") */
26300 if (!has_fullname)
26301 {
26302 if (c == '.' && **fnamep == '~')
26303 p = pbuf = expand_env_save(*fnamep);
26304 else
26305 p = pbuf = FullName_save(*fnamep, FALSE);
26306 }
26307 else
26308 p = *fnamep;
26309
26310 has_fullname = 0;
26311
26312 if (p != NULL)
26313 {
26314 if (c == '.')
26315 {
26316 mch_dirname(dirname, MAXPATHL);
26317 s = shorten_fname(p, dirname);
26318 if (s != NULL)
26319 {
26320 *fnamep = s;
26321 if (pbuf != NULL)
26322 {
26323 vim_free(*bufp); /* free any allocated file name */
26324 *bufp = pbuf;
26325 pbuf = NULL;
26326 }
26327 }
26328 }
26329 else
26330 {
26331 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26332 /* Only replace it when it starts with '~' */
26333 if (*dirname == '~')
26334 {
26335 s = vim_strsave(dirname);
26336 if (s != NULL)
26337 {
26338 *fnamep = s;
26339 vim_free(*bufp);
26340 *bufp = s;
26341 }
26342 }
26343 }
26344 vim_free(pbuf);
26345 }
26346 }
26347
26348 tail = gettail(*fnamep);
26349 *fnamelen = (int)STRLEN(*fnamep);
26350
26351 /* ":h" - head, remove "/file_name", can be repeated */
26352 /* Don't remove the first "/" or "c:\" */
26353 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26354 {
26355 valid |= VALID_HEAD;
26356 *usedlen += 2;
26357 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026358 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026359 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026360 *fnamelen = (int)(tail - *fnamep);
26361#ifdef VMS
26362 if (*fnamelen > 0)
26363 *fnamelen += 1; /* the path separator is part of the path */
26364#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026365 if (*fnamelen == 0)
26366 {
26367 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26368 p = vim_strsave((char_u *)".");
26369 if (p == NULL)
26370 return -1;
26371 vim_free(*bufp);
26372 *bufp = *fnamep = tail = p;
26373 *fnamelen = 1;
26374 }
26375 else
26376 {
26377 while (tail > s && !after_pathsep(s, tail))
26378 mb_ptr_back(*fnamep, tail);
26379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026380 }
26381
26382 /* ":8" - shortname */
26383 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26384 {
26385 *usedlen += 2;
26386#ifdef WIN3264
26387 has_shortname = 1;
26388#endif
26389 }
26390
26391#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026392 /*
26393 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026394 */
26395 if (has_shortname)
26396 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026397 /* Copy the string if it is shortened by :h and when it wasn't copied
26398 * yet, because we are going to change it in place. Avoids changing
26399 * the buffer name for "%:8". */
26400 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026401 {
26402 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026403 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026404 return -1;
26405 vim_free(*bufp);
26406 *bufp = *fnamep = p;
26407 }
26408
26409 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026410 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026411 if (!has_fullname && !vim_isAbsName(*fnamep))
26412 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026413 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026414 return -1;
26415 }
26416 else
26417 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026418 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026419
Bram Moolenaardc935552011-08-17 15:23:23 +020026420 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026421 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026422 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026423 return -1;
26424
26425 if (l == 0)
26426 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026427 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026428 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026429 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026430 return -1;
26431 }
26432 *fnamelen = l;
26433 }
26434 }
26435#endif /* WIN3264 */
26436
26437 /* ":t" - tail, just the basename */
26438 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26439 {
26440 *usedlen += 2;
26441 *fnamelen -= (int)(tail - *fnamep);
26442 *fnamep = tail;
26443 }
26444
26445 /* ":e" - extension, can be repeated */
26446 /* ":r" - root, without extension, can be repeated */
26447 while (src[*usedlen] == ':'
26448 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26449 {
26450 /* find a '.' in the tail:
26451 * - for second :e: before the current fname
26452 * - otherwise: The last '.'
26453 */
26454 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26455 s = *fnamep - 2;
26456 else
26457 s = *fnamep + *fnamelen - 1;
26458 for ( ; s > tail; --s)
26459 if (s[0] == '.')
26460 break;
26461 if (src[*usedlen + 1] == 'e') /* :e */
26462 {
26463 if (s > tail)
26464 {
26465 *fnamelen += (int)(*fnamep - (s + 1));
26466 *fnamep = s + 1;
26467#ifdef VMS
26468 /* cut version from the extension */
26469 s = *fnamep + *fnamelen - 1;
26470 for ( ; s > *fnamep; --s)
26471 if (s[0] == ';')
26472 break;
26473 if (s > *fnamep)
26474 *fnamelen = s - *fnamep;
26475#endif
26476 }
26477 else if (*fnamep <= tail)
26478 *fnamelen = 0;
26479 }
26480 else /* :r */
26481 {
26482 if (s > tail) /* remove one extension */
26483 *fnamelen = (int)(s - *fnamep);
26484 }
26485 *usedlen += 2;
26486 }
26487
26488 /* ":s?pat?foo?" - substitute */
26489 /* ":gs?pat?foo?" - global substitute */
26490 if (src[*usedlen] == ':'
26491 && (src[*usedlen + 1] == 's'
26492 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26493 {
26494 char_u *str;
26495 char_u *pat;
26496 char_u *sub;
26497 int sep;
26498 char_u *flags;
26499 int didit = FALSE;
26500
26501 flags = (char_u *)"";
26502 s = src + *usedlen + 2;
26503 if (src[*usedlen + 1] == 'g')
26504 {
26505 flags = (char_u *)"g";
26506 ++s;
26507 }
26508
26509 sep = *s++;
26510 if (sep)
26511 {
26512 /* find end of pattern */
26513 p = vim_strchr(s, sep);
26514 if (p != NULL)
26515 {
26516 pat = vim_strnsave(s, (int)(p - s));
26517 if (pat != NULL)
26518 {
26519 s = p + 1;
26520 /* find end of substitution */
26521 p = vim_strchr(s, sep);
26522 if (p != NULL)
26523 {
26524 sub = vim_strnsave(s, (int)(p - s));
26525 str = vim_strnsave(*fnamep, *fnamelen);
26526 if (sub != NULL && str != NULL)
26527 {
26528 *usedlen = (int)(p + 1 - src);
26529 s = do_string_sub(str, pat, sub, flags);
26530 if (s != NULL)
26531 {
26532 *fnamep = s;
26533 *fnamelen = (int)STRLEN(s);
26534 vim_free(*bufp);
26535 *bufp = s;
26536 didit = TRUE;
26537 }
26538 }
26539 vim_free(sub);
26540 vim_free(str);
26541 }
26542 vim_free(pat);
26543 }
26544 }
26545 /* after using ":s", repeat all the modifiers */
26546 if (didit)
26547 goto repeat;
26548 }
26549 }
26550
Bram Moolenaar26df0922014-02-23 23:39:13 +010026551 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26552 {
26553 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26554 if (p == NULL)
26555 return -1;
26556 vim_free(*bufp);
26557 *bufp = *fnamep = p;
26558 *fnamelen = (int)STRLEN(p);
26559 *usedlen += 2;
26560 }
26561
Bram Moolenaar071d4272004-06-13 20:20:40 +000026562 return valid;
26563}
26564
26565/*
26566 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26567 * "flags" can be "g" to do a global substitute.
26568 * Returns an allocated string, NULL for error.
26569 */
26570 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026571do_string_sub(
26572 char_u *str,
26573 char_u *pat,
26574 char_u *sub,
26575 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026576{
26577 int sublen;
26578 regmatch_T regmatch;
26579 int i;
26580 int do_all;
26581 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026582 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026583 garray_T ga;
26584 char_u *ret;
26585 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026586 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026587
26588 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26589 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026590 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026591
26592 ga_init2(&ga, 1, 200);
26593
26594 do_all = (flags[0] == 'g');
26595
26596 regmatch.rm_ic = p_ic;
26597 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26598 if (regmatch.regprog != NULL)
26599 {
26600 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026601 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026602 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26603 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026604 /* Skip empty match except for first match. */
26605 if (regmatch.startp[0] == regmatch.endp[0])
26606 {
26607 if (zero_width == regmatch.startp[0])
26608 {
26609 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026610 i = MB_PTR2LEN(tail);
26611 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26612 (size_t)i);
26613 ga.ga_len += i;
26614 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026615 continue;
26616 }
26617 zero_width = regmatch.startp[0];
26618 }
26619
Bram Moolenaar071d4272004-06-13 20:20:40 +000026620 /*
26621 * Get some space for a temporary buffer to do the substitution
26622 * into. It will contain:
26623 * - The text up to where the match is.
26624 * - The substituted text.
26625 * - The text after the match.
26626 */
26627 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026628 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026629 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26630 {
26631 ga_clear(&ga);
26632 break;
26633 }
26634
26635 /* copy the text up to where the match is */
26636 i = (int)(regmatch.startp[0] - tail);
26637 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26638 /* add the substituted text */
26639 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26640 + ga.ga_len + i, TRUE, TRUE, FALSE);
26641 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026642 tail = regmatch.endp[0];
26643 if (*tail == NUL)
26644 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026645 if (!do_all)
26646 break;
26647 }
26648
26649 if (ga.ga_data != NULL)
26650 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26651
Bram Moolenaar473de612013-06-08 18:19:48 +020026652 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026653 }
26654
26655 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26656 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026657 if (p_cpo == empty_option)
26658 p_cpo = save_cpo;
26659 else
26660 /* Darn, evaluating {sub} expression changed the value. */
26661 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026662
26663 return ret;
26664}
26665
26666#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */