blob: 8afd52e0b4e22f325a0ed91e5d153e0c0fab502c [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);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100454#ifdef FEAT_JOB
455static void job_free(job_T *job);
456#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
458static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
462static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
463static 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 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_abs(typval_T *argvars, typval_T *rettv);
469static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_add(typval_T *argvars, typval_T *rettv);
472static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
473static void f_and(typval_T *argvars, typval_T *rettv);
474static void f_append(typval_T *argvars, typval_T *rettv);
475static void f_argc(typval_T *argvars, typval_T *rettv);
476static void f_argidx(typval_T *argvars, typval_T *rettv);
477static void f_arglistid(typval_T *argvars, typval_T *rettv);
478static void f_argv(typval_T *argvars, typval_T *rettv);
479static void f_assert_equal(typval_T *argvars, typval_T *rettv);
480static void f_assert_exception(typval_T *argvars, typval_T *rettv);
481static void f_assert_fails(typval_T *argvars, typval_T *rettv);
482static void f_assert_false(typval_T *argvars, typval_T *rettv);
483static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100485static void f_asin(typval_T *argvars, typval_T *rettv);
486static void f_atan(typval_T *argvars, typval_T *rettv);
487static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_browse(typval_T *argvars, typval_T *rettv);
490static void f_browsedir(typval_T *argvars, typval_T *rettv);
491static void f_bufexists(typval_T *argvars, typval_T *rettv);
492static void f_buflisted(typval_T *argvars, typval_T *rettv);
493static void f_bufloaded(typval_T *argvars, typval_T *rettv);
494static void f_bufname(typval_T *argvars, typval_T *rettv);
495static void f_bufnr(typval_T *argvars, typval_T *rettv);
496static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
497static void f_byte2line(typval_T *argvars, typval_T *rettv);
498static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
499static void f_byteidx(typval_T *argvars, typval_T *rettv);
500static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
501static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100503static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100505#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100506static void f_ch_close(typval_T *argvars, typval_T *rettv);
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);
510static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100511static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
512static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100513static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100514static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100516static void f_changenr(typval_T *argvars, typval_T *rettv);
517static void f_char2nr(typval_T *argvars, typval_T *rettv);
518static void f_cindent(typval_T *argvars, typval_T *rettv);
519static void f_clearmatches(typval_T *argvars, typval_T *rettv);
520static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000521#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100522static void f_complete(typval_T *argvars, typval_T *rettv);
523static void f_complete_add(typval_T *argvars, typval_T *rettv);
524static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000525#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100526static void f_confirm(typval_T *argvars, typval_T *rettv);
527static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100529static void f_cos(typval_T *argvars, typval_T *rettv);
530static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000531#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100532static void f_count(typval_T *argvars, typval_T *rettv);
533static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
534static void f_cursor(typval_T *argsvars, typval_T *rettv);
535static void f_deepcopy(typval_T *argvars, typval_T *rettv);
536static void f_delete(typval_T *argvars, typval_T *rettv);
537static void f_did_filetype(typval_T *argvars, typval_T *rettv);
538static void f_diff_filler(typval_T *argvars, typval_T *rettv);
539static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100540static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_empty(typval_T *argvars, typval_T *rettv);
542static void f_escape(typval_T *argvars, typval_T *rettv);
543static void f_eval(typval_T *argvars, typval_T *rettv);
544static void f_eventhandler(typval_T *argvars, typval_T *rettv);
545static void f_executable(typval_T *argvars, typval_T *rettv);
546static void f_exepath(typval_T *argvars, typval_T *rettv);
547static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100549static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200550#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_expand(typval_T *argvars, typval_T *rettv);
552static void f_extend(typval_T *argvars, typval_T *rettv);
553static void f_feedkeys(typval_T *argvars, typval_T *rettv);
554static void f_filereadable(typval_T *argvars, typval_T *rettv);
555static void f_filewritable(typval_T *argvars, typval_T *rettv);
556static void f_filter(typval_T *argvars, typval_T *rettv);
557static void f_finddir(typval_T *argvars, typval_T *rettv);
558static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000559#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100560static void f_float2nr(typval_T *argvars, typval_T *rettv);
561static void f_floor(typval_T *argvars, typval_T *rettv);
562static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000563#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_fnameescape(typval_T *argvars, typval_T *rettv);
565static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
566static void f_foldclosed(typval_T *argvars, typval_T *rettv);
567static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
568static void f_foldlevel(typval_T *argvars, typval_T *rettv);
569static void f_foldtext(typval_T *argvars, typval_T *rettv);
570static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
571static void f_foreground(typval_T *argvars, typval_T *rettv);
572static void f_function(typval_T *argvars, typval_T *rettv);
573static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
574static void f_get(typval_T *argvars, typval_T *rettv);
575static void f_getbufline(typval_T *argvars, typval_T *rettv);
576static void f_getbufvar(typval_T *argvars, typval_T *rettv);
577static void f_getchar(typval_T *argvars, typval_T *rettv);
578static void f_getcharmod(typval_T *argvars, typval_T *rettv);
579static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
580static void f_getcmdline(typval_T *argvars, typval_T *rettv);
581static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
582static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
583static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
584static void f_getcwd(typval_T *argvars, typval_T *rettv);
585static void f_getfontname(typval_T *argvars, typval_T *rettv);
586static void f_getfperm(typval_T *argvars, typval_T *rettv);
587static void f_getfsize(typval_T *argvars, typval_T *rettv);
588static void f_getftime(typval_T *argvars, typval_T *rettv);
589static void f_getftype(typval_T *argvars, typval_T *rettv);
590static void f_getline(typval_T *argvars, typval_T *rettv);
591static void f_getmatches(typval_T *argvars, typval_T *rettv);
592static void f_getpid(typval_T *argvars, typval_T *rettv);
593static void f_getcurpos(typval_T *argvars, typval_T *rettv);
594static void f_getpos(typval_T *argvars, typval_T *rettv);
595static void f_getqflist(typval_T *argvars, typval_T *rettv);
596static void f_getreg(typval_T *argvars, typval_T *rettv);
597static void f_getregtype(typval_T *argvars, typval_T *rettv);
598static void f_gettabvar(typval_T *argvars, typval_T *rettv);
599static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
600static void f_getwinposx(typval_T *argvars, typval_T *rettv);
601static void f_getwinposy(typval_T *argvars, typval_T *rettv);
602static void f_getwinvar(typval_T *argvars, typval_T *rettv);
603static void f_glob(typval_T *argvars, typval_T *rettv);
604static void f_globpath(typval_T *argvars, typval_T *rettv);
605static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
606static void f_has(typval_T *argvars, typval_T *rettv);
607static void f_has_key(typval_T *argvars, typval_T *rettv);
608static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
609static void f_hasmapto(typval_T *argvars, typval_T *rettv);
610static void f_histadd(typval_T *argvars, typval_T *rettv);
611static void f_histdel(typval_T *argvars, typval_T *rettv);
612static void f_histget(typval_T *argvars, typval_T *rettv);
613static void f_histnr(typval_T *argvars, typval_T *rettv);
614static void f_hlID(typval_T *argvars, typval_T *rettv);
615static void f_hlexists(typval_T *argvars, typval_T *rettv);
616static void f_hostname(typval_T *argvars, typval_T *rettv);
617static void f_iconv(typval_T *argvars, typval_T *rettv);
618static void f_indent(typval_T *argvars, typval_T *rettv);
619static void f_index(typval_T *argvars, typval_T *rettv);
620static void f_input(typval_T *argvars, typval_T *rettv);
621static void f_inputdialog(typval_T *argvars, typval_T *rettv);
622static void f_inputlist(typval_T *argvars, typval_T *rettv);
623static void f_inputrestore(typval_T *argvars, typval_T *rettv);
624static void f_inputsave(typval_T *argvars, typval_T *rettv);
625static void f_inputsecret(typval_T *argvars, typval_T *rettv);
626static void f_insert(typval_T *argvars, typval_T *rettv);
627static void f_invert(typval_T *argvars, typval_T *rettv);
628static void f_isdirectory(typval_T *argvars, typval_T *rettv);
629static void f_islocked(typval_T *argvars, typval_T *rettv);
630static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100631#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100632# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100633static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100634# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +0100635static void f_job_start(typval_T *argvars, typval_T *rettv);
636static void f_job_stop(typval_T *argvars, typval_T *rettv);
637static void f_job_status(typval_T *argvars, typval_T *rettv);
638#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100639static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100640static void f_js_decode(typval_T *argvars, typval_T *rettv);
641static void f_js_encode(typval_T *argvars, typval_T *rettv);
642static void f_json_decode(typval_T *argvars, typval_T *rettv);
643static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_keys(typval_T *argvars, typval_T *rettv);
645static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
646static void f_len(typval_T *argvars, typval_T *rettv);
647static void f_libcall(typval_T *argvars, typval_T *rettv);
648static void f_libcallnr(typval_T *argvars, typval_T *rettv);
649static void f_line(typval_T *argvars, typval_T *rettv);
650static void f_line2byte(typval_T *argvars, typval_T *rettv);
651static void f_lispindent(typval_T *argvars, typval_T *rettv);
652static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000653#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100654static void f_log(typval_T *argvars, typval_T *rettv);
655static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000656#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200657#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200659#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_map(typval_T *argvars, typval_T *rettv);
661static void f_maparg(typval_T *argvars, typval_T *rettv);
662static void f_mapcheck(typval_T *argvars, typval_T *rettv);
663static void f_match(typval_T *argvars, typval_T *rettv);
664static void f_matchadd(typval_T *argvars, typval_T *rettv);
665static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
666static void f_matcharg(typval_T *argvars, typval_T *rettv);
667static void f_matchdelete(typval_T *argvars, typval_T *rettv);
668static void f_matchend(typval_T *argvars, typval_T *rettv);
669static void f_matchlist(typval_T *argvars, typval_T *rettv);
670static void f_matchstr(typval_T *argvars, typval_T *rettv);
671static void f_max(typval_T *argvars, typval_T *rettv);
672static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000673#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000675#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100677#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100679#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
681static void f_nr2char(typval_T *argvars, typval_T *rettv);
682static void f_or(typval_T *argvars, typval_T *rettv);
683static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100684#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100686#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000687#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
691static void f_printf(typval_T *argvars, typval_T *rettv);
692static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200693#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200695#endif
696#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200698#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_range(typval_T *argvars, typval_T *rettv);
700static void f_readfile(typval_T *argvars, typval_T *rettv);
701static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100702#ifdef FEAT_FLOAT
703static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
704#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_reltimestr(typval_T *argvars, typval_T *rettv);
706static void f_remote_expr(typval_T *argvars, typval_T *rettv);
707static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
708static void f_remote_peek(typval_T *argvars, typval_T *rettv);
709static void f_remote_read(typval_T *argvars, typval_T *rettv);
710static void f_remote_send(typval_T *argvars, typval_T *rettv);
711static void f_remove(typval_T *argvars, typval_T *rettv);
712static void f_rename(typval_T *argvars, typval_T *rettv);
713static void f_repeat(typval_T *argvars, typval_T *rettv);
714static void f_resolve(typval_T *argvars, typval_T *rettv);
715static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100717static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_screenattr(typval_T *argvars, typval_T *rettv);
720static void f_screenchar(typval_T *argvars, typval_T *rettv);
721static void f_screencol(typval_T *argvars, typval_T *rettv);
722static void f_screenrow(typval_T *argvars, typval_T *rettv);
723static void f_search(typval_T *argvars, typval_T *rettv);
724static void f_searchdecl(typval_T *argvars, typval_T *rettv);
725static void f_searchpair(typval_T *argvars, typval_T *rettv);
726static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
727static void f_searchpos(typval_T *argvars, typval_T *rettv);
728static void f_server2client(typval_T *argvars, typval_T *rettv);
729static void f_serverlist(typval_T *argvars, typval_T *rettv);
730static void f_setbufvar(typval_T *argvars, typval_T *rettv);
731static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
732static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
733static void f_setline(typval_T *argvars, typval_T *rettv);
734static void f_setloclist(typval_T *argvars, typval_T *rettv);
735static void f_setmatches(typval_T *argvars, typval_T *rettv);
736static void f_setpos(typval_T *argvars, typval_T *rettv);
737static void f_setqflist(typval_T *argvars, typval_T *rettv);
738static void f_setreg(typval_T *argvars, typval_T *rettv);
739static void f_settabvar(typval_T *argvars, typval_T *rettv);
740static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
741static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100742#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100743static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100744#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100745static void f_shellescape(typval_T *argvars, typval_T *rettv);
746static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
747static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000748#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_sin(typval_T *argvars, typval_T *rettv);
750static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_sort(typval_T *argvars, typval_T *rettv);
753static void f_soundfold(typval_T *argvars, typval_T *rettv);
754static void f_spellbadword(typval_T *argvars, typval_T *rettv);
755static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
756static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000757#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_sqrt(typval_T *argvars, typval_T *rettv);
759static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000760#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_str2nr(typval_T *argvars, typval_T *rettv);
762static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000763#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000765#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_stridx(typval_T *argvars, typval_T *rettv);
767static void f_string(typval_T *argvars, typval_T *rettv);
768static void f_strlen(typval_T *argvars, typval_T *rettv);
769static void f_strpart(typval_T *argvars, typval_T *rettv);
770static void f_strridx(typval_T *argvars, typval_T *rettv);
771static void f_strtrans(typval_T *argvars, typval_T *rettv);
772static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
773static void f_strwidth(typval_T *argvars, typval_T *rettv);
774static void f_submatch(typval_T *argvars, typval_T *rettv);
775static void f_substitute(typval_T *argvars, typval_T *rettv);
776static void f_synID(typval_T *argvars, typval_T *rettv);
777static void f_synIDattr(typval_T *argvars, typval_T *rettv);
778static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
779static void f_synstack(typval_T *argvars, typval_T *rettv);
780static void f_synconcealed(typval_T *argvars, typval_T *rettv);
781static void f_system(typval_T *argvars, typval_T *rettv);
782static void f_systemlist(typval_T *argvars, typval_T *rettv);
783static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
784static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
785static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
786static void f_taglist(typval_T *argvars, typval_T *rettv);
787static void f_tagfiles(typval_T *argvars, typval_T *rettv);
788static void f_tempname(typval_T *argvars, typval_T *rettv);
789static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200790#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100791static void f_tan(typval_T *argvars, typval_T *rettv);
792static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200793#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100794static void f_tolower(typval_T *argvars, typval_T *rettv);
795static void f_toupper(typval_T *argvars, typval_T *rettv);
796static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000797#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100798static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000799#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_type(typval_T *argvars, typval_T *rettv);
801static void f_undofile(typval_T *argvars, typval_T *rettv);
802static void f_undotree(typval_T *argvars, typval_T *rettv);
803static void f_uniq(typval_T *argvars, typval_T *rettv);
804static void f_values(typval_T *argvars, typval_T *rettv);
805static void f_virtcol(typval_T *argvars, typval_T *rettv);
806static void f_visualmode(typval_T *argvars, typval_T *rettv);
807static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
808static void f_winbufnr(typval_T *argvars, typval_T *rettv);
809static void f_wincol(typval_T *argvars, typval_T *rettv);
810static void f_winheight(typval_T *argvars, typval_T *rettv);
811static void f_winline(typval_T *argvars, typval_T *rettv);
812static void f_winnr(typval_T *argvars, typval_T *rettv);
813static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
814static void f_winrestview(typval_T *argvars, typval_T *rettv);
815static void f_winsaveview(typval_T *argvars, typval_T *rettv);
816static void f_winwidth(typval_T *argvars, typval_T *rettv);
817static void f_writefile(typval_T *argvars, typval_T *rettv);
818static void f_wordcount(typval_T *argvars, typval_T *rettv);
819static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000820
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100821static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
822static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
823static int get_env_len(char_u **arg);
824static int get_id_len(char_u **arg);
825static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
826static 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 +0000827#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
828#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
829 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100830static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
831static int eval_isnamec(int c);
832static int eval_isnamec1(int c);
833static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
834static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100835static typval_T *alloc_string_tv(char_u *string);
836static void init_tv(typval_T *varp);
837static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100838#ifdef FEAT_FLOAT
839static float_T get_tv_float(typval_T *varp);
840#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static linenr_T get_tv_lnum(typval_T *argvars);
842static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
843static char_u *get_tv_string(typval_T *varp);
844static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
845static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
846static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
847static hashtab_T *find_var_ht(char_u *name, char_u **varname);
848static funccall_T *get_funccal(void);
849static void vars_clear_ext(hashtab_T *ht, int free_val);
850static void delete_var(hashtab_T *ht, hashitem_T *hi);
851static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
852static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
853static void set_var(char_u *name, typval_T *varp, int copy);
854static int var_check_ro(int flags, char_u *name, int use_gettext);
855static int var_check_fixed(int flags, char_u *name, int use_gettext);
856static int var_check_func_name(char_u *name, int new_var);
857static int valid_varname(char_u *varname);
858static int tv_check_lock(int lock, char_u *name, int use_gettext);
859static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
860static char_u *find_option_end(char_u **arg, int *opt_flags);
861static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
862static int eval_fname_script(char_u *p);
863static int eval_fname_sid(char_u *p);
864static void list_func_head(ufunc_T *fp, int indent);
865static ufunc_T *find_func(char_u *name);
866static int function_exists(char_u *name);
867static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000868#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static void func_do_profile(ufunc_T *fp);
870static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
871static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000872static int
873# ifdef __BORLANDC__
874 _RTLENTRYF
875# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100876 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000877static int
878# ifdef __BORLANDC__
879 _RTLENTRYF
880# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000882#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100883static int script_autoload(char_u *name, int reload);
884static char_u *autoload_name(char_u *name);
885static void cat_func_name(char_u *buf, ufunc_T *fp);
886static void func_free(ufunc_T *fp);
887static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
888static int can_free_funccal(funccall_T *fc, int copyID) ;
889static void free_funccal(funccall_T *fc, int free_val);
890static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
891static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
892static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
893static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
894static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
895static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
896static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
897static int write_list(FILE *fd, list_T *list, int binary);
898static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000899
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200900
901#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100902static int compare_func_name(const void *s1, const void *s2);
903static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200904#endif
905
Bram Moolenaar33570922005-01-25 22:26:29 +0000906/*
907 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000908 */
909 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100910eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000911{
Bram Moolenaar33570922005-01-25 22:26:29 +0000912 int i;
913 struct vimvar *p;
914
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200915 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
916 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200917 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000918 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000919 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000920
921 for (i = 0; i < VV_LEN; ++i)
922 {
923 p = &vimvars[i];
924 STRCPY(p->vv_di.di_key, p->vv_name);
925 if (p->vv_flags & VV_RO)
926 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
927 else if (p->vv_flags & VV_RO_SBX)
928 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
929 else
930 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000931
932 /* add to v: scope dict, unless the value is not always available */
933 if (p->vv_type != VAR_UNKNOWN)
934 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000935 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000936 /* add to compat scope dict */
937 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100939 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
940
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000941 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100942 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200943 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100944 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100945
946 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
947 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
948 set_vim_var_nr(VV_NONE, VVAL_NONE);
949 set_vim_var_nr(VV_NULL, VVAL_NULL);
950
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200951 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200952
953#ifdef EBCDIC
954 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100955 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200956 */
957 sortFunctions();
958#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000959}
960
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000961#if defined(EXITFREE) || defined(PROTO)
962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100963eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000964{
965 int i;
966 struct vimvar *p;
967
968 for (i = 0; i < VV_LEN; ++i)
969 {
970 p = &vimvars[i];
971 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000972 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000973 vim_free(p->vv_str);
974 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000975 }
976 else if (p->vv_di.di_tv.v_type == VAR_LIST)
977 {
978 list_unref(p->vv_list);
979 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000980 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000981 }
982 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000983 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000984 hash_clear(&compat_hashtab);
985
Bram Moolenaard9fba312005-06-26 22:34:35 +0000986 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100987# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200988 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100989# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000990
991 /* global variables */
992 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000993
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000994 /* autoloaded script names */
995 ga_clear_strings(&ga_loaded);
996
Bram Moolenaarcca74132013-09-25 21:00:28 +0200997 /* Script-local variables. First clear all the variables and in a second
998 * loop free the scriptvar_T, because a variable in one script might hold
999 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001000 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001001 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001002 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001003 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001004 ga_clear(&ga_scripts);
1005
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001006 /* unreferenced lists and dicts */
1007 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001008
1009 /* functions */
1010 free_all_functions();
1011 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001012}
1013#endif
1014
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 * Return the name of the executed function.
1017 */
1018 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001019func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022}
1023
1024/*
1025 * Return the address holding the next breakpoint line for a funccall cookie.
1026 */
1027 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001028func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar33570922005-01-25 22:26:29 +00001030 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031}
1032
1033/*
1034 * Return the address holding the debug tick for a funccall cookie.
1035 */
1036 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001037func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
Bram Moolenaar33570922005-01-25 22:26:29 +00001039 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040}
1041
1042/*
1043 * Return the nesting level for a funccall cookie.
1044 */
1045 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001046func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
Bram Moolenaar33570922005-01-25 22:26:29 +00001048 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049}
1050
1051/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001052funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001054/* pointer to list of previously used funccal, still around because some
1055 * item in it is still being used. */
1056funccall_T *previous_funccal = NULL;
1057
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058/*
1059 * Return TRUE when a function was ended by a ":return" command.
1060 */
1061 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001062current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063{
1064 return current_funccal->returned;
1065}
1066
1067
1068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 * Set an internal variable to a string value. Creates the variable if it does
1070 * not already exist.
1071 */
1072 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001073set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001075 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001076 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
1078 val = vim_strsave(value);
1079 if (val != NULL)
1080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001081 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001082 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001084 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001085 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 }
1088}
1089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001091static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092static char_u *redir_endp = NULL;
1093static char_u *redir_varname = NULL;
1094
1095/*
1096 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001097 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 * Returns OK if successfully completed the setup. FAIL otherwise.
1099 */
1100 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001101var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102{
1103 int save_emsg;
1104 int err;
1105 typval_T tv;
1106
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001107 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001108 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 {
1110 EMSG(_(e_invarg));
1111 return FAIL;
1112 }
1113
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001114 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115 redir_varname = vim_strsave(name);
1116 if (redir_varname == NULL)
1117 return FAIL;
1118
1119 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1120 if (redir_lval == NULL)
1121 {
1122 var_redir_stop();
1123 return FAIL;
1124 }
1125
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126 /* The output is stored in growarray "redir_ga" until redirection ends. */
1127 ga_init2(&redir_ga, (int)sizeof(char), 500);
1128
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001130 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001131 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1133 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001134 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 if (redir_endp != NULL && *redir_endp != NUL)
1136 /* Trailing characters are present after the variable name */
1137 EMSG(_(e_trailing));
1138 else
1139 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001140 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 var_redir_stop();
1142 return FAIL;
1143 }
1144
1145 /* check if we can write to the variable: set it to or append an empty
1146 * string */
1147 save_emsg = did_emsg;
1148 did_emsg = FALSE;
1149 tv.v_type = VAR_STRING;
1150 tv.vval.v_string = (char_u *)"";
1151 if (append)
1152 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1153 else
1154 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001155 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001157 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 if (err)
1159 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001160 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 var_redir_stop();
1162 return FAIL;
1163 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164
1165 return OK;
1166}
1167
1168/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001169 * Append "value[value_len]" to the variable set by var_redir_start().
1170 * The actual appending is postponed until redirection ends, because the value
1171 * appended may in fact be the string we write to, changing it may cause freed
1172 * memory to be used:
1173 * :redir => foo
1174 * :let foo
1175 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 */
1177 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001178var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001180 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181
1182 if (redir_lval == NULL)
1183 return;
1184
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001185 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001186 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001190 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001191 {
1192 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001193 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001194 }
1195 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197}
1198
1199/*
1200 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001201 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 */
1203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001204var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001206 typval_T tv;
1207
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208 if (redir_lval != NULL)
1209 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001210 /* If there was no error: assign the text to the variable. */
1211 if (redir_endp != NULL)
1212 {
1213 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1214 tv.v_type = VAR_STRING;
1215 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001216 /* Call get_lval() again, if it's inside a Dict or List it may
1217 * have changed. */
1218 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001219 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001220 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1221 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1222 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001223 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001224
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001225 /* free the collected output */
1226 vim_free(redir_ga.ga_data);
1227 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001228
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001229 vim_free(redir_lval);
1230 redir_lval = NULL;
1231 }
1232 vim_free(redir_varname);
1233 redir_varname = NULL;
1234}
1235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236# if defined(FEAT_MBYTE) || defined(PROTO)
1237 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001238eval_charconvert(
1239 char_u *enc_from,
1240 char_u *enc_to,
1241 char_u *fname_from,
1242 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243{
1244 int err = FALSE;
1245
1246 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1247 set_vim_var_string(VV_CC_TO, enc_to, -1);
1248 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1249 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1250 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1251 err = TRUE;
1252 set_vim_var_string(VV_CC_FROM, NULL, -1);
1253 set_vim_var_string(VV_CC_TO, NULL, -1);
1254 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1255 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1256
1257 if (err)
1258 return FAIL;
1259 return OK;
1260}
1261# endif
1262
1263# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1264 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001265eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266{
1267 int err = FALSE;
1268
1269 set_vim_var_string(VV_FNAME_IN, fname, -1);
1270 set_vim_var_string(VV_CMDARG, args, -1);
1271 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1272 err = TRUE;
1273 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1274 set_vim_var_string(VV_CMDARG, NULL, -1);
1275
1276 if (err)
1277 {
1278 mch_remove(fname);
1279 return FAIL;
1280 }
1281 return OK;
1282}
1283# endif
1284
1285# if defined(FEAT_DIFF) || defined(PROTO)
1286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001287eval_diff(
1288 char_u *origfile,
1289 char_u *newfile,
1290 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
1292 int err = FALSE;
1293
1294 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1295 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1296 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1297 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1298 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1299 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1300 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1301}
1302
1303 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001304eval_patch(
1305 char_u *origfile,
1306 char_u *difffile,
1307 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308{
1309 int err;
1310
1311 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1312 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1313 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1314 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1315 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1316 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1317 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1318}
1319# endif
1320
1321/*
1322 * Top level evaluation function, returning a boolean.
1323 * Sets "error" to TRUE if there was an error.
1324 * Return TRUE or FALSE.
1325 */
1326 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001327eval_to_bool(
1328 char_u *arg,
1329 int *error,
1330 char_u **nextcmd,
1331 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 int retval = FALSE;
1335
1336 if (skip)
1337 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001338 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 else
1341 {
1342 *error = FALSE;
1343 if (!skip)
1344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001345 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348 }
1349 if (skip)
1350 --emsg_skip;
1351
1352 return retval;
1353}
1354
1355/*
1356 * Top level evaluation function, returning a string. If "skip" is TRUE,
1357 * only parsing to "nextcmd" is done, without reporting errors. Return
1358 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1359 */
1360 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001361eval_to_string_skip(
1362 char_u *arg,
1363 char_u **nextcmd,
1364 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
Bram Moolenaar33570922005-01-25 22:26:29 +00001366 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 char_u *retval;
1368
1369 if (skip)
1370 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 retval = NULL;
1373 else
1374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001375 retval = vim_strsave(get_tv_string(&tv));
1376 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
1378 if (skip)
1379 --emsg_skip;
1380
1381 return retval;
1382}
1383
1384/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001385 * Skip over an expression at "*pp".
1386 * Return FAIL for an error, OK otherwise.
1387 */
1388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001389skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001390{
Bram Moolenaar33570922005-01-25 22:26:29 +00001391 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001392
1393 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001394 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001395}
1396
1397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001399 * When "convert" is TRUE convert a List into a sequence of lines and convert
1400 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 * Return pointer to allocated memory, or NULL for failure.
1402 */
1403 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001404eval_to_string(
1405 char_u *arg,
1406 char_u **nextcmd,
1407 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001411 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001412#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001413 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001416 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 retval = NULL;
1418 else
1419 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001420 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001421 {
1422 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001423 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001424 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001425 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001426 if (tv.vval.v_list->lv_len > 0)
1427 ga_append(&ga, NL);
1428 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001429 ga_append(&ga, NUL);
1430 retval = (char_u *)ga.ga_data;
1431 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001432#ifdef FEAT_FLOAT
1433 else if (convert && tv.v_type == VAR_FLOAT)
1434 {
1435 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1436 retval = vim_strsave(numbuf);
1437 }
1438#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001439 else
1440 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001441 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443
1444 return retval;
1445}
1446
1447/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001448 * Call eval_to_string() without using current local variables and using
1449 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 */
1451 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001452eval_to_string_safe(
1453 char_u *arg,
1454 char_u **nextcmd,
1455 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 char_u *retval;
1458 void *save_funccalp;
1459
1460 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001461 if (use_sandbox)
1462 ++sandbox;
1463 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001464 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001465 if (use_sandbox)
1466 --sandbox;
1467 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 restore_funccal(save_funccalp);
1469 return retval;
1470}
1471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472/*
1473 * Top level evaluation function, returning a number.
1474 * Evaluates "expr" silently.
1475 * Returns -1 for an error.
1476 */
1477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001478eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
Bram Moolenaar33570922005-01-25 22:26:29 +00001480 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001482 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 ++emsg_off;
1485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001486 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 retval = -1;
1488 else
1489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001490 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001491 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
1493 --emsg_off;
1494
1495 return retval;
1496}
1497
Bram Moolenaara40058a2005-07-11 22:42:07 +00001498/*
1499 * Prepare v: variable "idx" to be used.
1500 * Save the current typeval in "save_tv".
1501 * When not used yet add the variable to the v: hashtable.
1502 */
1503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001504prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001505{
1506 *save_tv = vimvars[idx].vv_tv;
1507 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1508 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1509}
1510
1511/*
1512 * Restore v: variable "idx" to typeval "save_tv".
1513 * When no longer defined, remove the variable from the v: hashtable.
1514 */
1515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001516restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001517{
1518 hashitem_T *hi;
1519
Bram Moolenaara40058a2005-07-11 22:42:07 +00001520 vimvars[idx].vv_tv = *save_tv;
1521 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1522 {
1523 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1524 if (HASHITEM_EMPTY(hi))
1525 EMSG2(_(e_intern2), "restore_vimvar()");
1526 else
1527 hash_remove(&vimvarht, hi);
1528 }
1529}
1530
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001531#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001532/*
1533 * Evaluate an expression to a list with suggestions.
1534 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001535 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001536 */
1537 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001538eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001539{
1540 typval_T save_val;
1541 typval_T rettv;
1542 list_T *list = NULL;
1543 char_u *p = skipwhite(expr);
1544
1545 /* Set "v:val" to the bad word. */
1546 prepare_vimvar(VV_VAL, &save_val);
1547 vimvars[VV_VAL].vv_type = VAR_STRING;
1548 vimvars[VV_VAL].vv_str = badword;
1549 if (p_verbose == 0)
1550 ++emsg_off;
1551
1552 if (eval1(&p, &rettv, TRUE) == OK)
1553 {
1554 if (rettv.v_type != VAR_LIST)
1555 clear_tv(&rettv);
1556 else
1557 list = rettv.vval.v_list;
1558 }
1559
1560 if (p_verbose == 0)
1561 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001562 restore_vimvar(VV_VAL, &save_val);
1563
1564 return list;
1565}
1566
1567/*
1568 * "list" is supposed to contain two items: a word and a number. Return the
1569 * word in "pp" and the number as the return value.
1570 * Return -1 if anything isn't right.
1571 * Used to get the good word and score from the eval_spell_expr() result.
1572 */
1573 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001574get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001575{
1576 listitem_T *li;
1577
1578 li = list->lv_first;
1579 if (li == NULL)
1580 return -1;
1581 *pp = get_tv_string(&li->li_tv);
1582
1583 li = li->li_next;
1584 if (li == NULL)
1585 return -1;
1586 return get_tv_number(&li->li_tv);
1587}
1588#endif
1589
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001590/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001591 * Top level evaluation function.
1592 * Returns an allocated typval_T with the result.
1593 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001594 */
1595 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001596eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001597{
1598 typval_T *tv;
1599
1600 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001601 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001602 {
1603 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001604 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001605 }
1606
1607 return tv;
1608}
1609
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001613 * Uses argv[argc] for the function arguments. Only Number and String
1614 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001617 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001618call_vim_function(
1619 char_u *func,
1620 int argc,
1621 char_u **argv,
1622 int safe, /* use the sandbox */
1623 int str_arg_only, /* all arguments are strings */
1624 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
Bram Moolenaar33570922005-01-25 22:26:29 +00001626 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 long n;
1628 int len;
1629 int i;
1630 int doesrange;
1631 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001634 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 for (i = 0; i < argc; i++)
1639 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001640 /* Pass a NULL or empty argument as an empty string */
1641 if (argv[i] == NULL || *argv[i] == NUL)
1642 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001643 argvars[i].v_type = VAR_STRING;
1644 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001645 continue;
1646 }
1647
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001648 if (str_arg_only)
1649 len = 0;
1650 else
1651 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001652 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 if (len != 0 && len == (int)STRLEN(argv[i]))
1654 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001655 argvars[i].v_type = VAR_NUMBER;
1656 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 }
1658 else
1659 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001660 argvars[i].v_type = VAR_STRING;
1661 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 }
1664
1665 if (safe)
1666 {
1667 save_funccalp = save_funccal();
1668 ++sandbox;
1669 }
1670
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001671 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1672 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (safe)
1676 {
1677 --sandbox;
1678 restore_funccal(save_funccalp);
1679 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 vim_free(argvars);
1681
1682 if (ret == FAIL)
1683 clear_tv(rettv);
1684
1685 return ret;
1686}
1687
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001688/*
1689 * Call vimL function "func" and return the result as a number.
1690 * Returns -1 when calling the function fails.
1691 * Uses argv[argc] for the function arguments.
1692 */
1693 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001694call_func_retnr(
1695 char_u *func,
1696 int argc,
1697 char_u **argv,
1698 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001699{
1700 typval_T rettv;
1701 long retval;
1702
1703 /* All arguments are passed as strings, no conversion to number. */
1704 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1705 return -1;
1706
1707 retval = get_tv_number_chk(&rettv, NULL);
1708 clear_tv(&rettv);
1709 return retval;
1710}
1711
1712#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1713 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1714
Bram Moolenaar4f688582007-07-24 12:34:30 +00001715# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001717 * Call vimL function "func" and return the result as a string.
1718 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719 * Uses argv[argc] for the function arguments.
1720 */
1721 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001722call_func_retstr(
1723 char_u *func,
1724 int argc,
1725 char_u **argv,
1726 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727{
1728 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001729 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001731 /* All arguments are passed as strings, no conversion to number. */
1732 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733 return NULL;
1734
1735 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 return retval;
1738}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001739# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001740
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001741/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001742 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001744 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745 */
1746 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001747call_func_retlist(
1748 char_u *func,
1749 int argc,
1750 char_u **argv,
1751 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001752{
1753 typval_T rettv;
1754
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001755 /* All arguments are passed as strings, no conversion to number. */
1756 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757 return NULL;
1758
1759 if (rettv.v_type != VAR_LIST)
1760 {
1761 clear_tv(&rettv);
1762 return NULL;
1763 }
1764
1765 return rettv.vval.v_list;
1766}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#endif
1768
1769/*
1770 * Save the current function call pointer, and set it to NULL.
1771 * Used when executing autocommands and for ":source".
1772 */
1773 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001774save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001776 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 current_funccal = NULL;
1779 return (void *)fc;
1780}
1781
1782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001783restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001785 funccall_T *fc = (funccall_T *)vfc;
1786
1787 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788}
1789
Bram Moolenaar05159a02005-02-26 23:04:13 +00001790#if defined(FEAT_PROFILE) || defined(PROTO)
1791/*
1792 * Prepare profiling for entering a child or something else that is not
1793 * counted for the script/function itself.
1794 * Should always be called in pair with prof_child_exit().
1795 */
1796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001797prof_child_enter(
1798 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001799{
1800 funccall_T *fc = current_funccal;
1801
1802 if (fc != NULL && fc->func->uf_profiling)
1803 profile_start(&fc->prof_child);
1804 script_prof_save(tm);
1805}
1806
1807/*
1808 * Take care of time spent in a child.
1809 * Should always be called after prof_child_enter().
1810 */
1811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001812prof_child_exit(
1813 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001814{
1815 funccall_T *fc = current_funccal;
1816
1817 if (fc != NULL && fc->func->uf_profiling)
1818 {
1819 profile_end(&fc->prof_child);
1820 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1821 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1822 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1823 }
1824 script_prof_restore(tm);
1825}
1826#endif
1827
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829#ifdef FEAT_FOLDING
1830/*
1831 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1832 * it in "*cp". Doesn't give error messages.
1833 */
1834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001835eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836{
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 int retval;
1839 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001840 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1841 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
1843 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001844 if (use_sandbox)
1845 ++sandbox;
1846 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 retval = 0;
1850 else
1851 {
1852 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 if (tv.v_type == VAR_NUMBER)
1854 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001855 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 retval = 0;
1857 else
1858 {
1859 /* If the result is a string, check if there is a non-digit before
1860 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 if (!VIM_ISDIGIT(*s) && *s != '-')
1863 *cp = *s++;
1864 retval = atol((char *)s);
1865 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001869 if (use_sandbox)
1870 --sandbox;
1871 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
1873 return retval;
1874}
1875#endif
1876
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 * ":let" list all variable values
1879 * ":let var1 var2" list variable values
1880 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 * ":let var += expr" assignment command.
1882 * ":let var -= expr" assignment command.
1883 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 */
1886 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001887ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888{
1889 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 int var_count = 0;
1894 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001896 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001897 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
Bram Moolenaardb552d602006-03-23 22:59:57 +00001899 argend = skip_var_list(arg, &var_count, &semicolon);
1900 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001902 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1903 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001904 expr = skipwhite(argend);
1905 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1906 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001908 /*
1909 * ":let" without "=": list variables
1910 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 if (*arg == '[')
1912 EMSG(_(e_invarg));
1913 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001915 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001919 list_glob_vars(&first);
1920 list_buf_vars(&first);
1921 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001922#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001923 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001924#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001925 list_script_vars(&first);
1926 list_func_vars(&first);
1927 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 eap->nextcmd = check_nextcmd(arg);
1930 }
1931 else
1932 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 op[0] = '=';
1934 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001935 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001937 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1938 op[0] = *expr; /* +=, -= or .= */
1939 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001941 else
1942 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if (eap->skip)
1945 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (eap->skip)
1948 {
1949 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 --emsg_skip;
1952 }
1953 else if (i != FAIL)
1954 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
1959 }
1960}
1961
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962/*
1963 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1964 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 * When "nextchars" is not NULL it points to a string with characters that
1966 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1967 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 * Returns OK or FAIL;
1969 */
1970 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001971ex_let_vars(
1972 char_u *arg_start,
1973 typval_T *tv,
1974 int copy, /* copy values from "tv", don't move */
1975 int semicolon, /* from skip_var_list() */
1976 int var_count, /* from skip_var_list() */
1977 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978{
1979 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001980 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 listitem_T *item;
1983 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984
1985 if (*arg != '[')
1986 {
1987 /*
1988 * ":let var = expr" or ":for var in list"
1989 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001990 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return FAIL;
1992 return OK;
1993 }
1994
1995 /*
1996 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1997 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001998 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 {
2000 EMSG(_(e_listreq));
2001 return FAIL;
2002 }
2003
2004 i = list_len(l);
2005 if (semicolon == 0 && var_count < i)
2006 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002007 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 return FAIL;
2009 }
2010 if (var_count - semicolon > i)
2011 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002012 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 return FAIL;
2014 }
2015
2016 item = l->lv_first;
2017 while (*arg != ']')
2018 {
2019 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002020 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 item = item->li_next;
2022 if (arg == NULL)
2023 return FAIL;
2024
2025 arg = skipwhite(arg);
2026 if (*arg == ';')
2027 {
2028 /* Put the rest of the list (may be empty) in the var after ';'.
2029 * Create a new list for this. */
2030 l = list_alloc();
2031 if (l == NULL)
2032 return FAIL;
2033 while (item != NULL)
2034 {
2035 list_append_tv(l, &item->li_tv);
2036 item = item->li_next;
2037 }
2038
2039 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002040 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 ltv.vval.v_list = l;
2042 l->lv_refcount = 1;
2043
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002044 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2045 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046 clear_tv(&ltv);
2047 if (arg == NULL)
2048 return FAIL;
2049 break;
2050 }
2051 else if (*arg != ',' && *arg != ']')
2052 {
2053 EMSG2(_(e_intern2), "ex_let_vars()");
2054 return FAIL;
2055 }
2056 }
2057
2058 return OK;
2059}
2060
2061/*
2062 * Skip over assignable variable "var" or list of variables "[var, var]".
2063 * Used for ":let varvar = expr" and ":for varvar in expr".
2064 * For "[var, var]" increment "*var_count" for each variable.
2065 * for "[var, var; var]" set "semicolon".
2066 * Return NULL for an error.
2067 */
2068 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002069skip_var_list(
2070 char_u *arg,
2071 int *var_count,
2072 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002073{
2074 char_u *p, *s;
2075
2076 if (*arg == '[')
2077 {
2078 /* "[var, var]": find the matching ']'. */
2079 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002080 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081 {
2082 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2083 s = skip_var_one(p);
2084 if (s == p)
2085 {
2086 EMSG2(_(e_invarg2), p);
2087 return NULL;
2088 }
2089 ++*var_count;
2090
2091 p = skipwhite(s);
2092 if (*p == ']')
2093 break;
2094 else if (*p == ';')
2095 {
2096 if (*semicolon == 1)
2097 {
2098 EMSG(_("Double ; in list of variables"));
2099 return NULL;
2100 }
2101 *semicolon = 1;
2102 }
2103 else if (*p != ',')
2104 {
2105 EMSG2(_(e_invarg2), p);
2106 return NULL;
2107 }
2108 }
2109 return p + 1;
2110 }
2111 else
2112 return skip_var_one(arg);
2113}
2114
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002115/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002116 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002117 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002119 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002120skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002122 if (*arg == '@' && arg[1] != NUL)
2123 return arg + 2;
2124 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2125 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002126}
2127
Bram Moolenaara7043832005-01-21 11:56:39 +00002128/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002129 * List variables for hashtab "ht" with prefix "prefix".
2130 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002131 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002133list_hashtable_vars(
2134 hashtab_T *ht,
2135 char_u *prefix,
2136 int empty,
2137 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002138{
Bram Moolenaar33570922005-01-25 22:26:29 +00002139 hashitem_T *hi;
2140 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002141 int todo;
2142
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002143 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002144 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2145 {
2146 if (!HASHITEM_EMPTY(hi))
2147 {
2148 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002149 di = HI2DI(hi);
2150 if (empty || di->di_tv.v_type != VAR_STRING
2151 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002153 }
2154 }
2155}
2156
2157/*
2158 * List global variables.
2159 */
2160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002161list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002162{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002164}
2165
2166/*
2167 * List buffer variables.
2168 */
2169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002170list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002171{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002172 char_u numbuf[NUMBUFLEN];
2173
Bram Moolenaar429fa852013-04-15 12:27:36 +02002174 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002176
2177 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2179 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002180}
2181
2182/*
2183 * List window variables.
2184 */
2185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002186list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002187{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002188 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002190}
2191
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002192#ifdef FEAT_WINDOWS
2193/*
2194 * List tab page variables.
2195 */
2196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002197list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002198{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002199 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002201}
2202#endif
2203
Bram Moolenaara7043832005-01-21 11:56:39 +00002204/*
2205 * List Vim variables.
2206 */
2207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002208list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211}
2212
2213/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002214 * List script-local variables, if there is a script.
2215 */
2216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002218{
2219 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2221 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002222}
2223
2224/*
2225 * List function variables, if there is a function.
2226 */
2227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002229{
2230 if (current_funccal != NULL)
2231 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002232 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002233}
2234
2235/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 * List variables in "arg".
2237 */
2238 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240{
2241 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 char_u *name_start;
2245 char_u *arg_subsc;
2246 char_u *tofree;
2247 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248
2249 while (!ends_excmd(*arg) && !got_int)
2250 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002253 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2255 {
2256 emsg_severe = TRUE;
2257 EMSG(_(e_trailing));
2258 break;
2259 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 /* get_name_len() takes care of expanding curly braces */
2264 name_start = name = arg;
2265 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2266 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 /* This is mainly to keep test 49 working: when expanding
2269 * curly braces fails overrule the exception error message. */
2270 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 emsg_severe = TRUE;
2273 EMSG2(_(e_invarg2), arg);
2274 break;
2275 }
2276 error = TRUE;
2277 }
2278 else
2279 {
2280 if (tofree != NULL)
2281 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002282 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 else
2285 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 /* handle d.key, l[idx], f(expr) */
2287 arg_subsc = arg;
2288 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002296 case 'g': list_glob_vars(first); break;
2297 case 'b': list_buf_vars(first); break;
2298 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002299#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002300 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002301#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 case 'v': list_vim_vars(first); break;
2303 case 's': list_script_vars(first); break;
2304 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 default:
2306 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002307 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002308 }
2309 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 {
2311 char_u numbuf[NUMBUFLEN];
2312 char_u *tf;
2313 int c;
2314 char_u *s;
2315
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002316 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317 c = *arg;
2318 *arg = NUL;
2319 list_one_var_a((char_u *)"",
2320 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002321 tv.v_type,
2322 s == NULL ? (char_u *)"" : s,
2323 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002324 *arg = c;
2325 vim_free(tf);
2326 }
2327 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 }
2330 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002331
2332 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002334
2335 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 }
2337
2338 return arg;
2339}
2340
2341/*
2342 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2343 * Returns a pointer to the char just after the var name.
2344 * Returns NULL if there is an error.
2345 */
2346 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002347ex_let_one(
2348 char_u *arg, /* points to variable name */
2349 typval_T *tv, /* value to assign to variable */
2350 int copy, /* copy value from "tv" */
2351 char_u *endchars, /* valid chars after variable name or NULL */
2352 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353{
2354 int c1;
2355 char_u *name;
2356 char_u *p;
2357 char_u *arg_end = NULL;
2358 int len;
2359 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361
2362 /*
2363 * ":let $VAR = expr": Set environment variable.
2364 */
2365 if (*arg == '$')
2366 {
2367 /* Find the end of the name. */
2368 ++arg;
2369 name = arg;
2370 len = get_env_len(&arg);
2371 if (len == 0)
2372 EMSG2(_(e_invarg2), name - 1);
2373 else
2374 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002375 if (op != NULL && (*op == '+' || *op == '-'))
2376 EMSG2(_(e_letwrong), op);
2377 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002378 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002380 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 {
2382 c1 = name[len];
2383 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 p = get_tv_string_chk(tv);
2385 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 {
2387 int mustfree = FALSE;
2388 char_u *s = vim_getenv(name, &mustfree);
2389
2390 if (s != NULL)
2391 {
2392 p = tofree = concat_str(s, p);
2393 if (mustfree)
2394 vim_free(s);
2395 }
2396 }
2397 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002398 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002400 if (STRICMP(name, "HOME") == 0)
2401 init_homedir();
2402 else if (didset_vim && STRICMP(name, "VIM") == 0)
2403 didset_vim = FALSE;
2404 else if (didset_vimruntime
2405 && STRICMP(name, "VIMRUNTIME") == 0)
2406 didset_vimruntime = FALSE;
2407 arg_end = arg;
2408 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002409 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002411 }
2412 }
2413 }
2414
2415 /*
2416 * ":let &option = expr": Set option value.
2417 * ":let &l:option = expr": Set local option value.
2418 * ":let &g:option = expr": Set global option value.
2419 */
2420 else if (*arg == '&')
2421 {
2422 /* Find the end of the name. */
2423 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002424 if (p == NULL || (endchars != NULL
2425 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 EMSG(_(e_letunexp));
2427 else
2428 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 long n;
2430 int opt_type;
2431 long numval;
2432 char_u *stringval = NULL;
2433 char_u *s;
2434
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002435 c1 = *p;
2436 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437
2438 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 s = get_tv_string_chk(tv); /* != NULL if number or string */
2440 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 {
2442 opt_type = get_option_value(arg, &numval,
2443 &stringval, opt_flags);
2444 if ((opt_type == 1 && *op == '.')
2445 || (opt_type == 0 && *op != '.'))
2446 EMSG2(_(e_letwrong), op);
2447 else
2448 {
2449 if (opt_type == 1) /* number */
2450 {
2451 if (*op == '+')
2452 n = numval + n;
2453 else
2454 n = numval - n;
2455 }
2456 else if (opt_type == 0 && stringval != NULL) /* string */
2457 {
2458 s = concat_str(stringval, s);
2459 vim_free(stringval);
2460 stringval = s;
2461 }
2462 }
2463 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464 if (s != NULL)
2465 {
2466 set_option_value(arg, n, s, opt_flags);
2467 arg_end = p;
2468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 }
2472 }
2473
2474 /*
2475 * ":let @r = expr": Set register contents.
2476 */
2477 else if (*arg == '@')
2478 {
2479 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 if (op != NULL && (*op == '+' || *op == '-'))
2481 EMSG2(_(e_letwrong), op);
2482 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002483 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 EMSG(_(e_letunexp));
2485 else
2486 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002487 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 char_u *s;
2489
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002490 p = get_tv_string_chk(tv);
2491 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002493 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 if (s != NULL)
2495 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002496 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 vim_free(s);
2498 }
2499 }
2500 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 arg_end = arg + 1;
2504 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002505 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 }
2507 }
2508
2509 /*
2510 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002513 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002515 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002517 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2521 EMSG(_(e_letunexp));
2522 else
2523 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002524 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 arg_end = p;
2526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 }
2530
2531 else
2532 EMSG2(_(e_invarg2), arg);
2533
2534 return arg_end;
2535}
2536
2537/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2539 */
2540 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002541check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542{
2543 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2544 {
2545 EMSG2(_(e_readonlyvar), arg);
2546 return TRUE;
2547 }
2548 return FALSE;
2549}
2550
2551/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 * Get an lval: variable, Dict item or List item that can be assigned a value
2553 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2554 * "name.key", "name.key[expr]" etc.
2555 * Indexing only works if "name" is an existing List or Dictionary.
2556 * "name" points to the start of the name.
2557 * If "rettv" is not NULL it points to the value to be assigned.
2558 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2559 * wrong; must end in space or cmd separator.
2560 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002561 * flags:
2562 * GLV_QUIET: do not give error messages
2563 * GLV_NO_AUTOLOAD: do not use script autoloading
2564 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002566 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002568 */
2569 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002570get_lval(
2571 char_u *name,
2572 typval_T *rettv,
2573 lval_T *lp,
2574 int unlet,
2575 int skip,
2576 int flags, /* GLV_ values */
2577 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002578{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002579 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 char_u *expr_start, *expr_end;
2581 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002582 dictitem_T *v;
2583 typval_T var1;
2584 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002586 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002588 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002590 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002591
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594
2595 if (skip)
2596 {
2597 /* When skipping just find the end of the name. */
2598 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002599 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 }
2601
2602 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002603 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (expr_start != NULL)
2605 {
2606 /* Don't expand the name when we already know there is an error. */
2607 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2608 && *p != '[' && *p != '.')
2609 {
2610 EMSG(_(e_trailing));
2611 return NULL;
2612 }
2613
2614 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2615 if (lp->ll_exp_name == NULL)
2616 {
2617 /* Report an invalid expression in braces, unless the
2618 * expression evaluation has been cancelled due to an
2619 * aborting error, an interrupt, or an exception. */
2620 if (!aborting() && !quiet)
2621 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002622 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 EMSG2(_(e_invarg2), name);
2624 return NULL;
2625 }
2626 }
2627 lp->ll_name = lp->ll_exp_name;
2628 }
2629 else
2630 lp->ll_name = name;
2631
2632 /* Without [idx] or .key we are done. */
2633 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2634 return p;
2635
2636 cc = *p;
2637 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002638 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (v == NULL && !quiet)
2640 EMSG2(_(e_undefvar), lp->ll_name);
2641 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 if (v == NULL)
2643 return NULL;
2644
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 /*
2646 * Loop until no more [idx] or .key is following.
2647 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002648 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2652 && !(lp->ll_tv->v_type == VAR_DICT
2653 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
2656 EMSG(_("E689: Can only index a List or Dictionary"));
2657 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!quiet)
2662 EMSG(_("E708: [:] must come last"));
2663 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 len = -1;
2667 if (*p == '.')
2668 {
2669 key = p + 1;
2670 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2671 ;
2672 if (len == 0)
2673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (!quiet)
2675 EMSG(_(e_emptykey));
2676 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 }
2678 p = key + len;
2679 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002680 else
2681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 if (*p == ':')
2685 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 else
2687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 empty1 = FALSE;
2689 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002691 if (get_tv_string_chk(&var1) == NULL)
2692 {
2693 /* not a number or string */
2694 clear_tv(&var1);
2695 return NULL;
2696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
2698
2699 /* Optionally get the second index [ :expr]. */
2700 if (*p == ':')
2701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002705 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (rettv != NULL && (rettv->v_type != VAR_LIST
2711 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (!quiet)
2714 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 if (!empty1)
2716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 p = skipwhite(p + 1);
2720 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 else
2723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2726 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (!empty1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002731 if (get_tv_string_chk(&var2) == NULL)
2732 {
2733 /* not a number or string */
2734 if (!empty1)
2735 clear_tv(&var1);
2736 clear_tv(&var2);
2737 return NULL;
2738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
2748 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 if (!empty1)
2750 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 }
2755
2756 /* Skip to past ']'. */
2757 ++p;
2758 }
2759
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 {
2762 if (len == -1)
2763 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002765 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 if (*key == NUL)
2767 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (!quiet)
2769 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 }
2773 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002774 lp->ll_list = NULL;
2775 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002776 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002778 /* When assigning to a scope dictionary check that a function and
2779 * variable name is valid (only variable name unless it is l: or
2780 * g: dictionary). Disallow overwriting a builtin function. */
2781 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002783 int prevval;
2784 int wrong;
2785
2786 if (len != -1)
2787 {
2788 prevval = key[len];
2789 key[len] = NUL;
2790 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002791 else
2792 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002793 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2794 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002795 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002796 || !valid_varname(key);
2797 if (len != -1)
2798 key[len] = prevval;
2799 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 return NULL;
2801 }
2802
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002805 /* Can't add "v:" variable. */
2806 if (lp->ll_dict == &vimvardict)
2807 {
2808 EMSG2(_(e_illvar), name);
2809 return NULL;
2810 }
2811
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002812 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002816 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 if (len == -1)
2818 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 }
2821 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 if (len == -1)
2826 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 p = NULL;
2829 break;
2830 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002831 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002832 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 return NULL;
2834
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 if (len == -1)
2836 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 }
2839 else
2840 {
2841 /*
2842 * Get the number and item for the only or first index of the List.
2843 */
2844 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 else
2847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002848 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 clear_tv(&var1);
2850 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002851 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 lp->ll_list = lp->ll_tv->vval.v_list;
2853 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2854 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002856 if (lp->ll_n1 < 0)
2857 {
2858 lp->ll_n1 = 0;
2859 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2860 }
2861 }
2862 if (lp->ll_li == NULL)
2863 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002866 if (!quiet)
2867 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 }
2870
2871 /*
2872 * May need to find the item or absolute index for the second
2873 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 * When no index given: "lp->ll_empty2" is TRUE.
2875 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002879 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002885 {
2886 if (!quiet)
2887 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002889 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002891 }
2892
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2894 if (lp->ll_n1 < 0)
2895 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2896 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002897 {
2898 if (!quiet)
2899 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002901 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002902 }
2903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002906 }
2907
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 return p;
2909}
2910
2911/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002912 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 */
2914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002915clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916{
2917 vim_free(lp->ll_exp_name);
2918 vim_free(lp->ll_newkey);
2919}
2920
2921/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002922 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 */
2926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002927set_var_lval(
2928 lval_T *lp,
2929 char_u *endp,
2930 typval_T *rettv,
2931 int copy,
2932 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933{
2934 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002935 listitem_T *ri;
2936 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937
2938 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002941 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 cc = *endp;
2943 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 if (op != NULL && *op != '=')
2945 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002946 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002948 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002949 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002950 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002951 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002953 if ((di == NULL
2954 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2955 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2956 FALSE)))
2957 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 set_var(lp->ll_name, &tv, FALSE);
2959 clear_tv(&tv);
2960 }
2961 }
2962 else
2963 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002965 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002967 else if (tv_check_lock(lp->ll_newkey == NULL
2968 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002969 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002970 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 else if (lp->ll_range)
2972 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002973 listitem_T *ll_li = lp->ll_li;
2974 int ll_n1 = lp->ll_n1;
2975
2976 /*
2977 * Check whether any of the list items is locked
2978 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002979 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002980 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002981 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002982 return;
2983 ri = ri->li_next;
2984 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2985 break;
2986 ll_li = ll_li->li_next;
2987 ++ll_n1;
2988 }
2989
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 /*
2991 * Assign the List values to the list items.
2992 */
2993 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002994 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002995 if (op != NULL && *op != '=')
2996 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2997 else
2998 {
2999 clear_tv(&lp->ll_li->li_tv);
3000 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3001 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 ri = ri->li_next;
3003 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3004 break;
3005 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003008 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003009 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 ri = NULL;
3011 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003012 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003013 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 lp->ll_li = lp->ll_li->li_next;
3015 ++lp->ll_n1;
3016 }
3017 if (ri != NULL)
3018 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 else if (lp->ll_empty2
3020 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 : lp->ll_n1 != lp->ll_n2)
3022 EMSG(_("E711: List value has not enough items"));
3023 }
3024 else
3025 {
3026 /*
3027 * Assign to a List or Dictionary item.
3028 */
3029 if (lp->ll_newkey != NULL)
3030 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003031 if (op != NULL && *op != '=')
3032 {
3033 EMSG2(_(e_letwrong), op);
3034 return;
3035 }
3036
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003038 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 if (di == NULL)
3040 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003041 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3042 {
3043 vim_free(di);
3044 return;
3045 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003047 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 else if (op != NULL && *op != '=')
3049 {
3050 tv_op(lp->ll_tv, rettv, op);
3051 return;
3052 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003053 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003054 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003055
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003056 /*
3057 * Assign the value to the variable or list item.
3058 */
3059 if (copy)
3060 copy_tv(rettv, lp->ll_tv);
3061 else
3062 {
3063 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003064 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003065 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066 }
3067 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003068}
3069
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003070/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3072 * Returns OK or FAIL.
3073 */
3074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003075tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076{
3077 long n;
3078 char_u numbuf[NUMBUFLEN];
3079 char_u *s;
3080
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003081 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3082 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3083 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 {
3085 switch (tv1->v_type)
3086 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003087 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003088 case VAR_DICT:
3089 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003090 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003091 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003092 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003093 break;
3094
3095 case VAR_LIST:
3096 if (*op != '+' || tv2->v_type != VAR_LIST)
3097 break;
3098 /* List += List */
3099 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3100 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3101 return OK;
3102
3103 case VAR_NUMBER:
3104 case VAR_STRING:
3105 if (tv2->v_type == VAR_LIST)
3106 break;
3107 if (*op == '+' || *op == '-')
3108 {
3109 /* nr += nr or nr -= nr*/
3110 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111#ifdef FEAT_FLOAT
3112 if (tv2->v_type == VAR_FLOAT)
3113 {
3114 float_T f = n;
3115
3116 if (*op == '+')
3117 f += tv2->vval.v_float;
3118 else
3119 f -= tv2->vval.v_float;
3120 clear_tv(tv1);
3121 tv1->v_type = VAR_FLOAT;
3122 tv1->vval.v_float = f;
3123 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003124 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125#endif
3126 {
3127 if (*op == '+')
3128 n += get_tv_number(tv2);
3129 else
3130 n -= get_tv_number(tv2);
3131 clear_tv(tv1);
3132 tv1->v_type = VAR_NUMBER;
3133 tv1->vval.v_number = n;
3134 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 }
3136 else
3137 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003138 if (tv2->v_type == VAR_FLOAT)
3139 break;
3140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 /* str .= str */
3142 s = get_tv_string(tv1);
3143 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3144 clear_tv(tv1);
3145 tv1->v_type = VAR_STRING;
3146 tv1->vval.v_string = s;
3147 }
3148 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003149
3150#ifdef FEAT_FLOAT
3151 case VAR_FLOAT:
3152 {
3153 float_T f;
3154
3155 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3156 && tv2->v_type != VAR_NUMBER
3157 && tv2->v_type != VAR_STRING))
3158 break;
3159 if (tv2->v_type == VAR_FLOAT)
3160 f = tv2->vval.v_float;
3161 else
3162 f = get_tv_number(tv2);
3163 if (*op == '+')
3164 tv1->vval.v_float += f;
3165 else
3166 tv1->vval.v_float -= f;
3167 }
3168 return OK;
3169#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003170 }
3171 }
3172
3173 EMSG2(_(e_letwrong), op);
3174 return FAIL;
3175}
3176
3177/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178 * Add a watcher to a list.
3179 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003181list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182{
3183 lw->lw_next = l->lv_watch;
3184 l->lv_watch = lw;
3185}
3186
3187/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003188 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 * No warning when it isn't found...
3190 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003191 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003192list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193{
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195
3196 lwp = &l->lv_watch;
3197 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3198 {
3199 if (lw == lwrem)
3200 {
3201 *lwp = lw->lw_next;
3202 break;
3203 }
3204 lwp = &lw->lw_next;
3205 }
3206}
3207
3208/*
3209 * Just before removing an item from a list: advance watchers to the next
3210 * item.
3211 */
3212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003213list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214{
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216
3217 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3218 if (lw->lw_item == item)
3219 lw->lw_item = item->li_next;
3220}
3221
3222/*
3223 * Evaluate the expression used in a ":for var in expr" command.
3224 * "arg" points to "var".
3225 * Set "*errp" to TRUE for an error, FALSE otherwise;
3226 * Return a pointer that holds the info. Null when there is an error.
3227 */
3228 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003229eval_for_line(
3230 char_u *arg,
3231 int *errp,
3232 char_u **nextcmdp,
3233 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234{
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 typval_T tv;
3238 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239
3240 *errp = TRUE; /* default: there is an error */
3241
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 if (fi == NULL)
3244 return NULL;
3245
3246 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3247 if (expr == NULL)
3248 return fi;
3249
3250 expr = skipwhite(expr);
3251 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3252 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003253 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 return fi;
3255 }
3256
3257 if (skip)
3258 ++emsg_skip;
3259 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3260 {
3261 *errp = FALSE;
3262 if (!skip)
3263 {
3264 l = tv.vval.v_list;
3265 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 clear_tv(&tv);
3269 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 else
3271 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003272 /* No need to increment the refcount, it's already set for the
3273 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 fi->fi_list = l;
3275 list_add_watch(l, &fi->fi_lw);
3276 fi->fi_lw.lw_item = l->lv_first;
3277 }
3278 }
3279 }
3280 if (skip)
3281 --emsg_skip;
3282
3283 return fi;
3284}
3285
3286/*
3287 * Use the first item in a ":for" list. Advance to the next.
3288 * Assign the values to the variable (list). "arg" points to the first one.
3289 * Return TRUE when a valid item was found, FALSE when at end of list or
3290 * something wrong.
3291 */
3292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003293next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003295 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298
3299 item = fi->fi_lw.lw_item;
3300 if (item == NULL)
3301 result = FALSE;
3302 else
3303 {
3304 fi->fi_lw.lw_item = item->li_next;
3305 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3306 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3307 }
3308 return result;
3309}
3310
3311/*
3312 * Free the structure used to store info used by ":for".
3313 */
3314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003315free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003316{
Bram Moolenaar33570922005-01-25 22:26:29 +00003317 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003319 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003320 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003322 list_unref(fi->fi_list);
3323 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 vim_free(fi);
3325}
3326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3328
3329 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003330set_context_for_expression(
3331 expand_T *xp,
3332 char_u *arg,
3333 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335 int got_eq = FALSE;
3336 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 if (cmdidx == CMD_let)
3340 {
3341 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003342 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 {
3344 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003345 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 {
3347 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003348 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 if (vim_iswhite(*p))
3350 break;
3351 }
3352 return;
3353 }
3354 }
3355 else
3356 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3357 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 while ((xp->xp_pattern = vim_strpbrk(arg,
3359 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3360 {
3361 c = *xp->xp_pattern;
3362 if (c == '&')
3363 {
3364 c = xp->xp_pattern[1];
3365 if (c == '&')
3366 {
3367 ++xp->xp_pattern;
3368 xp->xp_context = cmdidx != CMD_let || got_eq
3369 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3370 }
3371 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003374 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3375 xp->xp_pattern += 2;
3376
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379 else if (c == '$')
3380 {
3381 /* environment variable */
3382 xp->xp_context = EXPAND_ENV_VARS;
3383 }
3384 else if (c == '=')
3385 {
3386 got_eq = TRUE;
3387 xp->xp_context = EXPAND_EXPRESSION;
3388 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003389 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 && xp->xp_context == EXPAND_FUNCTIONS
3391 && vim_strchr(xp->xp_pattern, '(') == NULL)
3392 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003393 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 break;
3395 }
3396 else if (cmdidx != CMD_let || got_eq)
3397 {
3398 if (c == '"') /* string */
3399 {
3400 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3401 if (c == '\\' && xp->xp_pattern[1] != NUL)
3402 ++xp->xp_pattern;
3403 xp->xp_context = EXPAND_NOTHING;
3404 }
3405 else if (c == '\'') /* literal string */
3406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003407 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3409 /* skip */ ;
3410 xp->xp_context = EXPAND_NOTHING;
3411 }
3412 else if (c == '|')
3413 {
3414 if (xp->xp_pattern[1] == '|')
3415 {
3416 ++xp->xp_pattern;
3417 xp->xp_context = EXPAND_EXPRESSION;
3418 }
3419 else
3420 xp->xp_context = EXPAND_COMMANDS;
3421 }
3422 else
3423 xp->xp_context = EXPAND_EXPRESSION;
3424 }
3425 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003426 /* Doesn't look like something valid, expand as an expression
3427 * anyway. */
3428 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 arg = xp->xp_pattern;
3430 if (*arg != NUL)
3431 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3432 /* skip */ ;
3433 }
3434 xp->xp_pattern = arg;
3435}
3436
3437#endif /* FEAT_CMDL_COMPL */
3438
3439/*
3440 * ":1,25call func(arg1, arg2)" function call.
3441 */
3442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003443ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
3445 char_u *arg = eap->arg;
3446 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003448 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003450 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 linenr_T lnum;
3452 int doesrange;
3453 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003454 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003456 if (eap->skip)
3457 {
3458 /* trans_function_name() doesn't work well when skipping, use eval0()
3459 * instead to skip to any following command, e.g. for:
3460 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003461 ++emsg_skip;
3462 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3463 clear_tv(&rettv);
3464 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003465 return;
3466 }
3467
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003468 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003469 if (fudi.fd_newkey != NULL)
3470 {
3471 /* Still need to give an error message for missing key. */
3472 EMSG2(_(e_dictkey), fudi.fd_newkey);
3473 vim_free(fudi.fd_newkey);
3474 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003475 if (tofree == NULL)
3476 return;
3477
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003478 /* Increase refcount on dictionary, it could get deleted when evaluating
3479 * the arguments. */
3480 if (fudi.fd_dict != NULL)
3481 ++fudi.fd_dict->dv_refcount;
3482
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003483 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003484 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003485 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar532c7802005-01-27 14:44:31 +00003487 /* Skip white space to allow ":call func ()". Not good, but required for
3488 * backward compatibility. */
3489 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003490 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
3492 if (*startarg != '(')
3493 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003494 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 goto end;
3496 }
3497
3498 /*
3499 * When skipping, evaluate the function once, to find the end of the
3500 * arguments.
3501 * When the function takes a range, this is discovered after the first
3502 * call, and the loop is broken.
3503 */
3504 if (eap->skip)
3505 {
3506 ++emsg_skip;
3507 lnum = eap->line2; /* do it once, also with an invalid range */
3508 }
3509 else
3510 lnum = eap->line1;
3511 for ( ; lnum <= eap->line2; ++lnum)
3512 {
3513 if (!eap->skip && eap->addr_count > 0)
3514 {
3515 curwin->w_cursor.lnum = lnum;
3516 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003517#ifdef FEAT_VIRTUALEDIT
3518 curwin->w_cursor.coladd = 0;
3519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003522 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003523 eap->line1, eap->line2, &doesrange,
3524 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 failed = TRUE;
3527 break;
3528 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003529
3530 /* Handle a function returning a Funcref, Dictionary or List. */
3531 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3532 {
3533 failed = TRUE;
3534 break;
3535 }
3536
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (doesrange || eap->skip)
3539 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003542 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003543 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (aborting())
3546 break;
3547 }
3548 if (eap->skip)
3549 --emsg_skip;
3550
3551 if (!failed)
3552 {
3553 /* Check for trailing illegal characters and a following command. */
3554 if (!ends_excmd(*arg))
3555 {
3556 emsg_severe = TRUE;
3557 EMSG(_(e_trailing));
3558 }
3559 else
3560 eap->nextcmd = check_nextcmd(arg);
3561 }
3562
3563end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003564 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003565 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566}
3567
3568/*
3569 * ":unlet[!] var1 ... " command.
3570 */
3571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003572ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003574 ex_unletlock(eap, eap->arg, 0);
3575}
3576
3577/*
3578 * ":lockvar" and ":unlockvar" commands
3579 */
3580 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003581ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 int deep = 2;
3585
3586 if (eap->forceit)
3587 deep = -1;
3588 else if (vim_isdigit(*arg))
3589 {
3590 deep = getdigits(&arg);
3591 arg = skipwhite(arg);
3592 }
3593
3594 ex_unletlock(eap, arg, deep);
3595}
3596
3597/*
3598 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3599 */
3600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003601ex_unletlock(
3602 exarg_T *eap,
3603 char_u *argstart,
3604 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605{
3606 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003609 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
3611 do
3612 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003614 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003615 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 if (lv.ll_name == NULL)
3617 error = TRUE; /* error but continue parsing */
3618 if (name_end == NULL || (!vim_iswhite(*name_end)
3619 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 if (name_end != NULL)
3622 {
3623 emsg_severe = TRUE;
3624 EMSG(_(e_trailing));
3625 }
3626 if (!(eap->skip || error))
3627 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 break;
3629 }
3630
3631 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003632 {
3633 if (eap->cmdidx == CMD_unlet)
3634 {
3635 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3636 error = TRUE;
3637 }
3638 else
3639 {
3640 if (do_lock_var(&lv, name_end, deep,
3641 eap->cmdidx == CMD_lockvar) == FAIL)
3642 error = TRUE;
3643 }
3644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 if (!eap->skip)
3647 clear_lval(&lv);
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 arg = skipwhite(name_end);
3650 } while (!ends_excmd(*arg));
3651
3652 eap->nextcmd = check_nextcmd(arg);
3653}
3654
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003656do_unlet_var(
3657 lval_T *lp,
3658 char_u *name_end,
3659 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 int ret = OK;
3662 int cc;
3663
3664 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 cc = *name_end;
3667 *name_end = NUL;
3668
3669 /* Normal name or expanded name. */
3670 if (check_changedtick(lp->ll_name))
3671 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003673 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003674 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003675 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003676 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003677 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003678 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003679 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003680 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003681 else if (lp->ll_range)
3682 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003683 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003684 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003685 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003686
3687 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3688 {
3689 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003690 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003691 return FAIL;
3692 ll_li = li;
3693 ++ll_n1;
3694 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695
3696 /* Delete a range of List items. */
3697 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3698 {
3699 li = lp->ll_li->li_next;
3700 listitem_remove(lp->ll_list, lp->ll_li);
3701 lp->ll_li = li;
3702 ++lp->ll_n1;
3703 }
3704 }
3705 else
3706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003707 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708 /* unlet a List item. */
3709 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003712 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 }
3714
3715 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003716}
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718/*
3719 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003720 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 */
3722 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003723do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
Bram Moolenaar33570922005-01-25 22:26:29 +00003725 hashtab_T *ht;
3726 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003727 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003728 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003729 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 ht = find_var_ht(name, &varname);
3732 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003734 if (ht == &globvarht)
3735 d = &globvardict;
3736 else if (current_funccal != NULL
3737 && ht == &current_funccal->l_vars.dv_hashtab)
3738 d = &current_funccal->l_vars;
3739 else if (ht == &compat_hashtab)
3740 d = &vimvardict;
3741 else
3742 {
3743 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3744 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3745 }
3746 if (d == NULL)
3747 {
3748 EMSG2(_(e_intern2), "do_unlet()");
3749 return FAIL;
3750 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003751 hi = hash_find(ht, varname);
3752 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003753 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003754 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003755 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003756 || var_check_ro(di->di_flags, name, FALSE)
3757 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003758 return FAIL;
3759
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003760 delete_var(ht, hi);
3761 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003764 if (forceit)
3765 return OK;
3766 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 return FAIL;
3768}
3769
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003770/*
3771 * Lock or unlock variable indicated by "lp".
3772 * "deep" is the levels to go (-1 for unlimited);
3773 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3774 */
3775 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003776do_lock_var(
3777 lval_T *lp,
3778 char_u *name_end,
3779 int deep,
3780 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003781{
3782 int ret = OK;
3783 int cc;
3784 dictitem_T *di;
3785
3786 if (deep == 0) /* nothing to do */
3787 return OK;
3788
3789 if (lp->ll_tv == NULL)
3790 {
3791 cc = *name_end;
3792 *name_end = NUL;
3793
3794 /* Normal name or expanded name. */
3795 if (check_changedtick(lp->ll_name))
3796 ret = FAIL;
3797 else
3798 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003799 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003800 if (di == NULL)
3801 ret = FAIL;
3802 else
3803 {
3804 if (lock)
3805 di->di_flags |= DI_FLAGS_LOCK;
3806 else
3807 di->di_flags &= ~DI_FLAGS_LOCK;
3808 item_lock(&di->di_tv, deep, lock);
3809 }
3810 }
3811 *name_end = cc;
3812 }
3813 else if (lp->ll_range)
3814 {
3815 listitem_T *li = lp->ll_li;
3816
3817 /* (un)lock a range of List items. */
3818 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3819 {
3820 item_lock(&li->li_tv, deep, lock);
3821 li = li->li_next;
3822 ++lp->ll_n1;
3823 }
3824 }
3825 else if (lp->ll_list != NULL)
3826 /* (un)lock a List item. */
3827 item_lock(&lp->ll_li->li_tv, deep, lock);
3828 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003829 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003830 item_lock(&lp->ll_di->di_tv, deep, lock);
3831
3832 return ret;
3833}
3834
3835/*
3836 * Lock or unlock an item. "deep" is nr of levels to go.
3837 */
3838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003839item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003840{
3841 static int recurse = 0;
3842 list_T *l;
3843 listitem_T *li;
3844 dict_T *d;
3845 hashitem_T *hi;
3846 int todo;
3847
3848 if (recurse >= DICT_MAXNEST)
3849 {
3850 EMSG(_("E743: variable nested too deep for (un)lock"));
3851 return;
3852 }
3853 if (deep == 0)
3854 return;
3855 ++recurse;
3856
3857 /* lock/unlock the item itself */
3858 if (lock)
3859 tv->v_lock |= VAR_LOCKED;
3860 else
3861 tv->v_lock &= ~VAR_LOCKED;
3862
3863 switch (tv->v_type)
3864 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003865 case VAR_UNKNOWN:
3866 case VAR_NUMBER:
3867 case VAR_STRING:
3868 case VAR_FUNC:
3869 case VAR_FLOAT:
3870 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003871 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003872 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003873 break;
3874
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003875 case VAR_LIST:
3876 if ((l = tv->vval.v_list) != NULL)
3877 {
3878 if (lock)
3879 l->lv_lock |= VAR_LOCKED;
3880 else
3881 l->lv_lock &= ~VAR_LOCKED;
3882 if (deep < 0 || deep > 1)
3883 /* recursive: lock/unlock the items the List contains */
3884 for (li = l->lv_first; li != NULL; li = li->li_next)
3885 item_lock(&li->li_tv, deep - 1, lock);
3886 }
3887 break;
3888 case VAR_DICT:
3889 if ((d = tv->vval.v_dict) != NULL)
3890 {
3891 if (lock)
3892 d->dv_lock |= VAR_LOCKED;
3893 else
3894 d->dv_lock &= ~VAR_LOCKED;
3895 if (deep < 0 || deep > 1)
3896 {
3897 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003898 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003899 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3900 {
3901 if (!HASHITEM_EMPTY(hi))
3902 {
3903 --todo;
3904 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3905 }
3906 }
3907 }
3908 }
3909 }
3910 --recurse;
3911}
3912
Bram Moolenaara40058a2005-07-11 22:42:07 +00003913/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003914 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3915 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003916 */
3917 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003918tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003919{
3920 return (tv->v_lock & VAR_LOCKED)
3921 || (tv->v_type == VAR_LIST
3922 && tv->vval.v_list != NULL
3923 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3924 || (tv->v_type == VAR_DICT
3925 && tv->vval.v_dict != NULL
3926 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3927}
3928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3930/*
3931 * Delete all "menutrans_" variables.
3932 */
3933 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003934del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003937 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003940 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003942 {
3943 if (!HASHITEM_EMPTY(hi))
3944 {
3945 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3947 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 }
3949 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951}
3952#endif
3953
3954#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3955
3956/*
3957 * Local string buffer for the next two functions to store a variable name
3958 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3959 * get_user_var_name().
3960 */
3961
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003962static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
3964static char_u *varnamebuf = NULL;
3965static int varnamebuflen = 0;
3966
3967/*
3968 * Function to concatenate a prefix and a variable name.
3969 */
3970 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003971cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
3973 int len;
3974
3975 len = (int)STRLEN(name) + 3;
3976 if (len > varnamebuflen)
3977 {
3978 vim_free(varnamebuf);
3979 len += 10; /* some additional space */
3980 varnamebuf = alloc(len);
3981 if (varnamebuf == NULL)
3982 {
3983 varnamebuflen = 0;
3984 return NULL;
3985 }
3986 varnamebuflen = len;
3987 }
3988 *varnamebuf = prefix;
3989 varnamebuf[1] = ':';
3990 STRCPY(varnamebuf + 2, name);
3991 return varnamebuf;
3992}
3993
3994/*
3995 * Function given to ExpandGeneric() to obtain the list of user defined
3996 * (global/buffer/window/built-in) variable names.
3997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003999get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004001 static long_u gdone;
4002 static long_u bdone;
4003 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004004#ifdef FEAT_WINDOWS
4005 static long_u tdone;
4006#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004007 static int vidx;
4008 static hashitem_T *hi;
4009 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
4011 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004012 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004013 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004014#ifdef FEAT_WINDOWS
4015 tdone = 0;
4016#endif
4017 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004018
4019 /* Global variables */
4020 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004022 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004023 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004024 else
4025 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004026 while (HASHITEM_EMPTY(hi))
4027 ++hi;
4028 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4029 return cat_prefix_varname('g', hi->hi_key);
4030 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004032
4033 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004034 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004035 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004038 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004039 else
4040 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004041 while (HASHITEM_EMPTY(hi))
4042 ++hi;
4043 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004045 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 return (char_u *)"b:changedtick";
4049 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004050
4051 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004052 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004055 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004057 else
4058 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004059 while (HASHITEM_EMPTY(hi))
4060 ++hi;
4061 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004063
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004064#ifdef FEAT_WINDOWS
4065 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004066 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004067 if (tdone < ht->ht_used)
4068 {
4069 if (tdone++ == 0)
4070 hi = ht->ht_array;
4071 else
4072 ++hi;
4073 while (HASHITEM_EMPTY(hi))
4074 ++hi;
4075 return cat_prefix_varname('t', hi->hi_key);
4076 }
4077#endif
4078
Bram Moolenaar33570922005-01-25 22:26:29 +00004079 /* v: variables */
4080 if (vidx < VV_LEN)
4081 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
4083 vim_free(varnamebuf);
4084 varnamebuf = NULL;
4085 varnamebuflen = 0;
4086 return NULL;
4087}
4088
4089#endif /* FEAT_CMDL_COMPL */
4090
4091/*
4092 * types for expressions.
4093 */
4094typedef enum
4095{
4096 TYPE_UNKNOWN = 0
4097 , TYPE_EQUAL /* == */
4098 , TYPE_NEQUAL /* != */
4099 , TYPE_GREATER /* > */
4100 , TYPE_GEQUAL /* >= */
4101 , TYPE_SMALLER /* < */
4102 , TYPE_SEQUAL /* <= */
4103 , TYPE_MATCH /* =~ */
4104 , TYPE_NOMATCH /* !~ */
4105} exptype_T;
4106
4107/*
4108 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4111 */
4112
4113/*
4114 * Handle zero level expression.
4115 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004117 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 * Return OK or FAIL.
4119 */
4120 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004121eval0(
4122 char_u *arg,
4123 typval_T *rettv,
4124 char_u **nextcmd,
4125 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126{
4127 int ret;
4128 char_u *p;
4129
4130 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (ret == FAIL || !ends_excmd(*p))
4133 {
4134 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 /*
4137 * Report the invalid expression unless the expression evaluation has
4138 * been cancelled due to an aborting error, an interrupt, or an
4139 * exception.
4140 */
4141 if (!aborting())
4142 EMSG2(_(e_invexpr2), arg);
4143 ret = FAIL;
4144 }
4145 if (nextcmd != NULL)
4146 *nextcmd = check_nextcmd(p);
4147
4148 return ret;
4149}
4150
4151/*
4152 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004153 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 *
4155 * "arg" must point to the first non-white of the expression.
4156 * "arg" is advanced to the next non-white after the recognized expression.
4157 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004158 * Note: "rettv.v_lock" is not set.
4159 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 * Return OK or FAIL.
4161 */
4162 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004163eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164{
4165 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004166 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
4168 /*
4169 * Get the first variable.
4170 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 return FAIL;
4173
4174 if ((*arg)[0] == '?')
4175 {
4176 result = FALSE;
4177 if (evaluate)
4178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 int error = FALSE;
4180
4181 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004183 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 if (error)
4185 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187
4188 /*
4189 * Get the second variable.
4190 */
4191 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 return FAIL;
4194
4195 /*
4196 * Check for the ":".
4197 */
4198 if ((*arg)[0] != ':')
4199 {
4200 EMSG(_("E109: Missing ':' after '?'"));
4201 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 return FAIL;
4204 }
4205
4206 /*
4207 * Get the third variable.
4208 */
4209 *arg = skipwhite(*arg + 1);
4210 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4211 {
4212 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return FAIL;
4215 }
4216 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219
4220 return OK;
4221}
4222
4223/*
4224 * Handle first level expression:
4225 * expr2 || expr2 || expr2 logical OR
4226 *
4227 * "arg" must point to the first non-white of the expression.
4228 * "arg" is advanced to the next non-white after the recognized expression.
4229 *
4230 * Return OK or FAIL.
4231 */
4232 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004233eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
Bram Moolenaar33570922005-01-25 22:26:29 +00004235 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 long result;
4237 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
4240 /*
4241 * Get the first variable.
4242 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 return FAIL;
4245
4246 /*
4247 * Repeat until there is no following "||".
4248 */
4249 first = TRUE;
4250 result = FALSE;
4251 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4252 {
4253 if (evaluate && first)
4254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (error)
4259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 first = FALSE;
4261 }
4262
4263 /*
4264 * Get the second variable.
4265 */
4266 *arg = skipwhite(*arg + 2);
4267 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4268 return FAIL;
4269
4270 /*
4271 * Compute the result.
4272 */
4273 if (evaluate && !result)
4274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004275 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 if (error)
4279 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 if (evaluate)
4282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 rettv->v_type = VAR_NUMBER;
4284 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 }
4287
4288 return OK;
4289}
4290
4291/*
4292 * Handle second level expression:
4293 * expr3 && expr3 && expr3 logical AND
4294 *
4295 * "arg" must point to the first non-white of the expression.
4296 * "arg" is advanced to the next non-white after the recognized expression.
4297 *
4298 * Return OK or FAIL.
4299 */
4300 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004301eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302{
Bram Moolenaar33570922005-01-25 22:26:29 +00004303 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 long result;
4305 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004306 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 /*
4309 * Get the first variable.
4310 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 return FAIL;
4313
4314 /*
4315 * Repeat until there is no following "&&".
4316 */
4317 first = TRUE;
4318 result = TRUE;
4319 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4320 {
4321 if (evaluate && first)
4322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004326 if (error)
4327 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 first = FALSE;
4329 }
4330
4331 /*
4332 * Get the second variable.
4333 */
4334 *arg = skipwhite(*arg + 2);
4335 if (eval4(arg, &var2, evaluate && result) == FAIL)
4336 return FAIL;
4337
4338 /*
4339 * Compute the result.
4340 */
4341 if (evaluate && result)
4342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004346 if (error)
4347 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 if (evaluate)
4350 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004351 rettv->v_type = VAR_NUMBER;
4352 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 }
4355
4356 return OK;
4357}
4358
4359/*
4360 * Handle third level expression:
4361 * var1 == var2
4362 * var1 =~ var2
4363 * var1 != var2
4364 * var1 !~ var2
4365 * var1 > var2
4366 * var1 >= var2
4367 * var1 < var2
4368 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004369 * var1 is var2
4370 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 *
4372 * "arg" must point to the first non-white of the expression.
4373 * "arg" is advanced to the next non-white after the recognized expression.
4374 *
4375 * Return OK or FAIL.
4376 */
4377 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004378eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379{
Bram Moolenaar33570922005-01-25 22:26:29 +00004380 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 char_u *p;
4382 int i;
4383 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004384 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 int len = 2;
4386 long n1, n2;
4387 char_u *s1, *s2;
4388 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4389 regmatch_T regmatch;
4390 int ic;
4391 char_u *save_cpo;
4392
4393 /*
4394 * Get the first variable.
4395 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 return FAIL;
4398
4399 p = *arg;
4400 switch (p[0])
4401 {
4402 case '=': if (p[1] == '=')
4403 type = TYPE_EQUAL;
4404 else if (p[1] == '~')
4405 type = TYPE_MATCH;
4406 break;
4407 case '!': if (p[1] == '=')
4408 type = TYPE_NEQUAL;
4409 else if (p[1] == '~')
4410 type = TYPE_NOMATCH;
4411 break;
4412 case '>': if (p[1] != '=')
4413 {
4414 type = TYPE_GREATER;
4415 len = 1;
4416 }
4417 else
4418 type = TYPE_GEQUAL;
4419 break;
4420 case '<': if (p[1] != '=')
4421 {
4422 type = TYPE_SMALLER;
4423 len = 1;
4424 }
4425 else
4426 type = TYPE_SEQUAL;
4427 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004428 case 'i': if (p[1] == 's')
4429 {
4430 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4431 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004432 i = p[len];
4433 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004434 {
4435 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4436 type_is = TRUE;
4437 }
4438 }
4439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441
4442 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004443 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 */
4445 if (type != TYPE_UNKNOWN)
4446 {
4447 /* extra question mark appended: ignore case */
4448 if (p[len] == '?')
4449 {
4450 ic = TRUE;
4451 ++len;
4452 }
4453 /* extra '#' appended: match case */
4454 else if (p[len] == '#')
4455 {
4456 ic = FALSE;
4457 ++len;
4458 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004459 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 else
4461 ic = p_ic;
4462
4463 /*
4464 * Get the second variable.
4465 */
4466 *arg = skipwhite(p + len);
4467 if (eval5(arg, &var2, evaluate) == FAIL)
4468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004469 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 return FAIL;
4471 }
4472
4473 if (evaluate)
4474 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004475 if (type_is && rettv->v_type != var2.v_type)
4476 {
4477 /* For "is" a different type always means FALSE, for "notis"
4478 * it means TRUE. */
4479 n1 = (type == TYPE_NEQUAL);
4480 }
4481 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4482 {
4483 if (type_is)
4484 {
4485 n1 = (rettv->v_type == var2.v_type
4486 && rettv->vval.v_list == var2.vval.v_list);
4487 if (type == TYPE_NEQUAL)
4488 n1 = !n1;
4489 }
4490 else if (rettv->v_type != var2.v_type
4491 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4492 {
4493 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004494 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004496 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004497 clear_tv(rettv);
4498 clear_tv(&var2);
4499 return FAIL;
4500 }
4501 else
4502 {
4503 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004504 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4505 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 if (type == TYPE_NEQUAL)
4507 n1 = !n1;
4508 }
4509 }
4510
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004511 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4512 {
4513 if (type_is)
4514 {
4515 n1 = (rettv->v_type == var2.v_type
4516 && rettv->vval.v_dict == var2.vval.v_dict);
4517 if (type == TYPE_NEQUAL)
4518 n1 = !n1;
4519 }
4520 else if (rettv->v_type != var2.v_type
4521 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4522 {
4523 if (rettv->v_type != var2.v_type)
4524 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4525 else
4526 EMSG(_("E736: Invalid operation for Dictionary"));
4527 clear_tv(rettv);
4528 clear_tv(&var2);
4529 return FAIL;
4530 }
4531 else
4532 {
4533 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004534 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4535 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004536 if (type == TYPE_NEQUAL)
4537 n1 = !n1;
4538 }
4539 }
4540
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004541 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4542 {
4543 if (rettv->v_type != var2.v_type
4544 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4545 {
4546 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004547 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004548 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004549 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 clear_tv(rettv);
4551 clear_tv(&var2);
4552 return FAIL;
4553 }
4554 else
4555 {
4556 /* Compare two Funcrefs for being equal or unequal. */
4557 if (rettv->vval.v_string == NULL
4558 || var2.vval.v_string == NULL)
4559 n1 = FALSE;
4560 else
4561 n1 = STRCMP(rettv->vval.v_string,
4562 var2.vval.v_string) == 0;
4563 if (type == TYPE_NEQUAL)
4564 n1 = !n1;
4565 }
4566 }
4567
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004568#ifdef FEAT_FLOAT
4569 /*
4570 * If one of the two variables is a float, compare as a float.
4571 * When using "=~" or "!~", always compare as string.
4572 */
4573 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4574 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4575 {
4576 float_T f1, f2;
4577
4578 if (rettv->v_type == VAR_FLOAT)
4579 f1 = rettv->vval.v_float;
4580 else
4581 f1 = get_tv_number(rettv);
4582 if (var2.v_type == VAR_FLOAT)
4583 f2 = var2.vval.v_float;
4584 else
4585 f2 = get_tv_number(&var2);
4586 n1 = FALSE;
4587 switch (type)
4588 {
4589 case TYPE_EQUAL: n1 = (f1 == f2); break;
4590 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4591 case TYPE_GREATER: n1 = (f1 > f2); break;
4592 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4593 case TYPE_SMALLER: n1 = (f1 < f2); break;
4594 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4595 case TYPE_UNKNOWN:
4596 case TYPE_MATCH:
4597 case TYPE_NOMATCH: break; /* avoid gcc warning */
4598 }
4599 }
4600#endif
4601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 /*
4603 * If one of the two variables is a number, compare as a number.
4604 * When using "=~" or "!~", always compare as string.
4605 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004606 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609 n1 = get_tv_number(rettv);
4610 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 switch (type)
4612 {
4613 case TYPE_EQUAL: n1 = (n1 == n2); break;
4614 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4615 case TYPE_GREATER: n1 = (n1 > n2); break;
4616 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4617 case TYPE_SMALLER: n1 = (n1 < n2); break;
4618 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4619 case TYPE_UNKNOWN:
4620 case TYPE_MATCH:
4621 case TYPE_NOMATCH: break; /* avoid gcc warning */
4622 }
4623 }
4624 else
4625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 s1 = get_tv_string_buf(rettv, buf1);
4627 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4629 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4630 else
4631 i = 0;
4632 n1 = FALSE;
4633 switch (type)
4634 {
4635 case TYPE_EQUAL: n1 = (i == 0); break;
4636 case TYPE_NEQUAL: n1 = (i != 0); break;
4637 case TYPE_GREATER: n1 = (i > 0); break;
4638 case TYPE_GEQUAL: n1 = (i >= 0); break;
4639 case TYPE_SMALLER: n1 = (i < 0); break;
4640 case TYPE_SEQUAL: n1 = (i <= 0); break;
4641
4642 case TYPE_MATCH:
4643 case TYPE_NOMATCH:
4644 /* avoid 'l' flag in 'cpoptions' */
4645 save_cpo = p_cpo;
4646 p_cpo = (char_u *)"";
4647 regmatch.regprog = vim_regcomp(s2,
4648 RE_MAGIC + RE_STRING);
4649 regmatch.rm_ic = ic;
4650 if (regmatch.regprog != NULL)
4651 {
4652 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004653 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 if (type == TYPE_NOMATCH)
4655 n1 = !n1;
4656 }
4657 p_cpo = save_cpo;
4658 break;
4659
4660 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4661 }
4662 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004663 clear_tv(rettv);
4664 clear_tv(&var2);
4665 rettv->v_type = VAR_NUMBER;
4666 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 }
4668 }
4669
4670 return OK;
4671}
4672
4673/*
4674 * Handle fourth level expression:
4675 * + number addition
4676 * - number subtraction
4677 * . string concatenation
4678 *
4679 * "arg" must point to the first non-white of the expression.
4680 * "arg" is advanced to the next non-white after the recognized expression.
4681 *
4682 * Return OK or FAIL.
4683 */
4684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004685eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
Bram Moolenaar33570922005-01-25 22:26:29 +00004687 typval_T var2;
4688 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 int op;
4690 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004691#ifdef FEAT_FLOAT
4692 float_T f1 = 0, f2 = 0;
4693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 char_u *s1, *s2;
4695 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4696 char_u *p;
4697
4698 /*
4699 * Get the first variable.
4700 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004701 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 return FAIL;
4703
4704 /*
4705 * Repeat computing, until no '+', '-' or '.' is following.
4706 */
4707 for (;;)
4708 {
4709 op = **arg;
4710 if (op != '+' && op != '-' && op != '.')
4711 break;
4712
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004713 if ((op != '+' || rettv->v_type != VAR_LIST)
4714#ifdef FEAT_FLOAT
4715 && (op == '.' || rettv->v_type != VAR_FLOAT)
4716#endif
4717 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 {
4719 /* For "list + ...", an illegal use of the first operand as
4720 * a number cannot be determined before evaluating the 2nd
4721 * operand: if this is also a list, all is ok.
4722 * For "something . ...", "something - ..." or "non-list + ...",
4723 * we know that the first operand needs to be a string or number
4724 * without evaluating the 2nd operand. So check before to avoid
4725 * side effects after an error. */
4726 if (evaluate && get_tv_string_chk(rettv) == NULL)
4727 {
4728 clear_tv(rettv);
4729 return FAIL;
4730 }
4731 }
4732
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 /*
4734 * Get the second variable.
4735 */
4736 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004737 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004739 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 return FAIL;
4741 }
4742
4743 if (evaluate)
4744 {
4745 /*
4746 * Compute the result.
4747 */
4748 if (op == '.')
4749 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004750 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4751 s2 = get_tv_string_buf_chk(&var2, buf2);
4752 if (s2 == NULL) /* type error ? */
4753 {
4754 clear_tv(rettv);
4755 clear_tv(&var2);
4756 return FAIL;
4757 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004758 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004759 clear_tv(rettv);
4760 rettv->v_type = VAR_STRING;
4761 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004763 else if (op == '+' && rettv->v_type == VAR_LIST
4764 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004765 {
4766 /* concatenate Lists */
4767 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4768 &var3) == FAIL)
4769 {
4770 clear_tv(rettv);
4771 clear_tv(&var2);
4772 return FAIL;
4773 }
4774 clear_tv(rettv);
4775 *rettv = var3;
4776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 else
4778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004779 int error = FALSE;
4780
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004781#ifdef FEAT_FLOAT
4782 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004783 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004784 f1 = rettv->vval.v_float;
4785 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004786 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787 else
4788#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004790 n1 = get_tv_number_chk(rettv, &error);
4791 if (error)
4792 {
4793 /* This can only happen for "list + non-list". For
4794 * "non-list + ..." or "something - ...", we returned
4795 * before evaluating the 2nd operand. */
4796 clear_tv(rettv);
4797 return FAIL;
4798 }
4799#ifdef FEAT_FLOAT
4800 if (var2.v_type == VAR_FLOAT)
4801 f1 = n1;
4802#endif
4803 }
4804#ifdef FEAT_FLOAT
4805 if (var2.v_type == VAR_FLOAT)
4806 {
4807 f2 = var2.vval.v_float;
4808 n2 = 0;
4809 }
4810 else
4811#endif
4812 {
4813 n2 = get_tv_number_chk(&var2, &error);
4814 if (error)
4815 {
4816 clear_tv(rettv);
4817 clear_tv(&var2);
4818 return FAIL;
4819 }
4820#ifdef FEAT_FLOAT
4821 if (rettv->v_type == VAR_FLOAT)
4822 f2 = n2;
4823#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004825 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004826
4827#ifdef FEAT_FLOAT
4828 /* If there is a float on either side the result is a float. */
4829 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4830 {
4831 if (op == '+')
4832 f1 = f1 + f2;
4833 else
4834 f1 = f1 - f2;
4835 rettv->v_type = VAR_FLOAT;
4836 rettv->vval.v_float = f1;
4837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004839#endif
4840 {
4841 if (op == '+')
4842 n1 = n1 + n2;
4843 else
4844 n1 = n1 - n2;
4845 rettv->v_type = VAR_NUMBER;
4846 rettv->vval.v_number = n1;
4847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
4851 }
4852 return OK;
4853}
4854
4855/*
4856 * Handle fifth level expression:
4857 * * number multiplication
4858 * / number division
4859 * % number modulo
4860 *
4861 * "arg" must point to the first non-white of the expression.
4862 * "arg" is advanced to the next non-white after the recognized expression.
4863 *
4864 * Return OK or FAIL.
4865 */
4866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004867eval6(
4868 char_u **arg,
4869 typval_T *rettv,
4870 int evaluate,
4871 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872{
Bram Moolenaar33570922005-01-25 22:26:29 +00004873 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 int op;
4875 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876#ifdef FEAT_FLOAT
4877 int use_float = FALSE;
4878 float_T f1 = 0, f2;
4879#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004880 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
4882 /*
4883 * Get the first variable.
4884 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004885 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 return FAIL;
4887
4888 /*
4889 * Repeat computing, until no '*', '/' or '%' is following.
4890 */
4891 for (;;)
4892 {
4893 op = **arg;
4894 if (op != '*' && op != '/' && op != '%')
4895 break;
4896
4897 if (evaluate)
4898 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899#ifdef FEAT_FLOAT
4900 if (rettv->v_type == VAR_FLOAT)
4901 {
4902 f1 = rettv->vval.v_float;
4903 use_float = TRUE;
4904 n1 = 0;
4905 }
4906 else
4907#endif
4908 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004910 if (error)
4911 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913 else
4914 n1 = 0;
4915
4916 /*
4917 * Get the second variable.
4918 */
4919 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004920 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 return FAIL;
4922
4923 if (evaluate)
4924 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004925#ifdef FEAT_FLOAT
4926 if (var2.v_type == VAR_FLOAT)
4927 {
4928 if (!use_float)
4929 {
4930 f1 = n1;
4931 use_float = TRUE;
4932 }
4933 f2 = var2.vval.v_float;
4934 n2 = 0;
4935 }
4936 else
4937#endif
4938 {
4939 n2 = get_tv_number_chk(&var2, &error);
4940 clear_tv(&var2);
4941 if (error)
4942 return FAIL;
4943#ifdef FEAT_FLOAT
4944 if (use_float)
4945 f2 = n2;
4946#endif
4947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
4949 /*
4950 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004951 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953#ifdef FEAT_FLOAT
4954 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 if (op == '*')
4957 f1 = f1 * f2;
4958 else if (op == '/')
4959 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004960# ifdef VMS
4961 /* VMS crashes on divide by zero, work around it */
4962 if (f2 == 0.0)
4963 {
4964 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004965 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004967 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004969 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970 }
4971 else
4972 f1 = f1 / f2;
4973# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974 /* We rely on the floating point library to handle divide
4975 * by zero to result in "inf" and not a crash. */
4976 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004977# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004981 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 return FAIL;
4983 }
4984 rettv->v_type = VAR_FLOAT;
4985 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990 if (op == '*')
4991 n1 = n1 * n2;
4992 else if (op == '/')
4993 {
4994 if (n2 == 0) /* give an error message? */
4995 {
4996 if (n1 == 0)
4997 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4998 else if (n1 < 0)
4999 n1 = -0x7fffffffL;
5000 else
5001 n1 = 0x7fffffffL;
5002 }
5003 else
5004 n1 = n1 / n2;
5005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005007 {
5008 if (n2 == 0) /* give an error message? */
5009 n1 = 0;
5010 else
5011 n1 = n1 % n2;
5012 }
5013 rettv->v_type = VAR_NUMBER;
5014 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 }
5018
5019 return OK;
5020}
5021
5022/*
5023 * Handle sixth level expression:
5024 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005025 * "string" string constant
5026 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 * &option-name option value
5028 * @r register contents
5029 * identifier variable value
5030 * function() function call
5031 * $VAR environment variable
5032 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005033 * [expr, expr] List
5034 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 *
5036 * Also handle:
5037 * ! in front logical NOT
5038 * - in front unary minus
5039 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 * trailing [] subscript in String or List
5041 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 *
5043 * "arg" must point to the first non-white of the expression.
5044 * "arg" is advanced to the next non-white after the recognized expression.
5045 *
5046 * Return OK or FAIL.
5047 */
5048 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005049eval7(
5050 char_u **arg,
5051 typval_T *rettv,
5052 int evaluate,
5053 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 long n;
5056 int len;
5057 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 char_u *start_leader, *end_leader;
5059 int ret = OK;
5060 char_u *alias;
5061
5062 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005063 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005064 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
5068 /*
5069 * Skip '!' and '-' characters. They are handled later.
5070 */
5071 start_leader = *arg;
5072 while (**arg == '!' || **arg == '-' || **arg == '+')
5073 *arg = skipwhite(*arg + 1);
5074 end_leader = *arg;
5075
5076 switch (**arg)
5077 {
5078 /*
5079 * Number constant.
5080 */
5081 case '0':
5082 case '1':
5083 case '2':
5084 case '3':
5085 case '4':
5086 case '5':
5087 case '6':
5088 case '7':
5089 case '8':
5090 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005091 {
5092#ifdef FEAT_FLOAT
5093 char_u *p = skipdigits(*arg + 1);
5094 int get_float = FALSE;
5095
5096 /* We accept a float when the format matches
5097 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005098 * strict to avoid backwards compatibility problems.
5099 * Don't look for a float after the "." operator, so that
5100 * ":let vers = 1.2.3" doesn't fail. */
5101 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005103 get_float = TRUE;
5104 p = skipdigits(p + 2);
5105 if (*p == 'e' || *p == 'E')
5106 {
5107 ++p;
5108 if (*p == '-' || *p == '+')
5109 ++p;
5110 if (!vim_isdigit(*p))
5111 get_float = FALSE;
5112 else
5113 p = skipdigits(p + 1);
5114 }
5115 if (ASCII_ISALPHA(*p) || *p == '.')
5116 get_float = FALSE;
5117 }
5118 if (get_float)
5119 {
5120 float_T f;
5121
5122 *arg += string2float(*arg, &f);
5123 if (evaluate)
5124 {
5125 rettv->v_type = VAR_FLOAT;
5126 rettv->vval.v_float = f;
5127 }
5128 }
5129 else
5130#endif
5131 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005132 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005133 *arg += len;
5134 if (evaluate)
5135 {
5136 rettv->v_type = VAR_NUMBER;
5137 rettv->vval.v_number = n;
5138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142
5143 /*
5144 * String constant: "string".
5145 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005146 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 break;
5148
5149 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005152 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005153 break;
5154
5155 /*
5156 * List: [expr, expr]
5157 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005158 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 break;
5160
5161 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 * Dictionary: {key: val, key: val}
5163 */
5164 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5165 break;
5166
5167 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005168 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005170 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 break;
5172
5173 /*
5174 * Environment variable: $VAR.
5175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 break;
5178
5179 /*
5180 * Register contents: @r.
5181 */
5182 case '@': ++*arg;
5183 if (evaluate)
5184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005185 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005186 rettv->vval.v_string = get_reg_contents(**arg,
5187 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189 if (**arg != NUL)
5190 ++*arg;
5191 break;
5192
5193 /*
5194 * nested expression: (expression).
5195 */
5196 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005197 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (**arg == ')')
5199 ++*arg;
5200 else if (ret == OK)
5201 {
5202 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005203 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 ret = FAIL;
5205 }
5206 break;
5207
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 break;
5210 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211
5212 if (ret == NOTDONE)
5213 {
5214 /*
5215 * Must be a variable or function name.
5216 * Can also be a curly-braces kind of name: {expr}.
5217 */
5218 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005219 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 if (alias != NULL)
5221 s = alias;
5222
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005223 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 ret = FAIL;
5225 else
5226 {
5227 if (**arg == '(') /* recursive! */
5228 {
5229 /* If "s" is the name of a variable of type VAR_FUNC
5230 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005231 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232
5233 /* Invoke the function. */
5234 ret = get_func_tv(s, len, rettv, arg,
5235 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005236 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005237
5238 /* If evaluate is FALSE rettv->v_type was not set in
5239 * get_func_tv, but it's needed in handle_subscript() to parse
5240 * what follows. So set it here. */
5241 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5242 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005243 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005244 rettv->v_type = VAR_FUNC;
5245 }
5246
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 /* Stop the expression evaluation when immediately
5248 * aborting on error, or when an interrupt occurred or
5249 * an exception was thrown but not caught. */
5250 if (aborting())
5251 {
5252 if (ret == OK)
5253 clear_tv(rettv);
5254 ret = FAIL;
5255 }
5256 }
5257 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005258 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005259 else
5260 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005262 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 }
5264
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 *arg = skipwhite(*arg);
5266
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005267 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5268 * expr(expr). */
5269 if (ret == OK)
5270 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
5272 /*
5273 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5274 */
5275 if (ret == OK && evaluate && end_leader > start_leader)
5276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005277 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005278 int val = 0;
5279#ifdef FEAT_FLOAT
5280 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005282 if (rettv->v_type == VAR_FLOAT)
5283 f = rettv->vval.v_float;
5284 else
5285#endif
5286 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005289 clear_tv(rettv);
5290 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 else
5293 {
5294 while (end_leader > start_leader)
5295 {
5296 --end_leader;
5297 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005298 {
5299#ifdef FEAT_FLOAT
5300 if (rettv->v_type == VAR_FLOAT)
5301 f = !f;
5302 else
5303#endif
5304 val = !val;
5305 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005307 {
5308#ifdef FEAT_FLOAT
5309 if (rettv->v_type == VAR_FLOAT)
5310 f = -f;
5311 else
5312#endif
5313 val = -val;
5314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005315 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005316#ifdef FEAT_FLOAT
5317 if (rettv->v_type == VAR_FLOAT)
5318 {
5319 clear_tv(rettv);
5320 rettv->vval.v_float = f;
5321 }
5322 else
5323#endif
5324 {
5325 clear_tv(rettv);
5326 rettv->v_type = VAR_NUMBER;
5327 rettv->vval.v_number = val;
5328 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
5331
5332 return ret;
5333}
5334
5335/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005336 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5337 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5339 */
5340 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005341eval_index(
5342 char_u **arg,
5343 typval_T *rettv,
5344 int evaluate,
5345 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346{
5347 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005348 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 long len = -1;
5351 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354
Bram Moolenaara03f2332016-02-06 18:09:59 +01005355 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005357 case VAR_FUNC:
5358 if (verbose)
5359 EMSG(_("E695: Cannot index a Funcref"));
5360 return FAIL;
5361 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005362#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005363 if (verbose)
5364 EMSG(_(e_float_as_string));
5365 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005366#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005367 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005368 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005369 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005370 if (verbose)
5371 EMSG(_("E909: Cannot index a special variable"));
5372 return FAIL;
5373 case VAR_UNKNOWN:
5374 if (evaluate)
5375 return FAIL;
5376 /* FALLTHROUGH */
5377
5378 case VAR_STRING:
5379 case VAR_NUMBER:
5380 case VAR_LIST:
5381 case VAR_DICT:
5382 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005383 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005385 init_tv(&var1);
5386 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 /*
5390 * dict.name
5391 */
5392 key = *arg + 1;
5393 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5394 ;
5395 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005397 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 }
5399 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005401 /*
5402 * something[idx]
5403 *
5404 * Get the (first) variable from inside the [].
5405 */
5406 *arg = skipwhite(*arg + 1);
5407 if (**arg == ':')
5408 empty1 = TRUE;
5409 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5410 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005411 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5412 {
5413 /* not a number or string */
5414 clear_tv(&var1);
5415 return FAIL;
5416 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005417
5418 /*
5419 * Get the second variable from inside the [:].
5420 */
5421 if (**arg == ':')
5422 {
5423 range = TRUE;
5424 *arg = skipwhite(*arg + 1);
5425 if (**arg == ']')
5426 empty2 = TRUE;
5427 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005429 if (!empty1)
5430 clear_tv(&var1);
5431 return FAIL;
5432 }
5433 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5434 {
5435 /* not a number or string */
5436 if (!empty1)
5437 clear_tv(&var1);
5438 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005439 return FAIL;
5440 }
5441 }
5442
5443 /* Check for the ']'. */
5444 if (**arg != ']')
5445 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005446 if (verbose)
5447 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 clear_tv(&var1);
5449 if (range)
5450 clear_tv(&var2);
5451 return FAIL;
5452 }
5453 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455
5456 if (evaluate)
5457 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458 n1 = 0;
5459 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005461 n1 = get_tv_number(&var1);
5462 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 }
5464 if (range)
5465 {
5466 if (empty2)
5467 n2 = -1;
5468 else
5469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 n2 = get_tv_number(&var2);
5471 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 }
5473 }
5474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005477 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005478 case VAR_FUNC:
5479 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005480 case VAR_SPECIAL:
5481 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005482 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005483 break; /* not evaluating, skipping over subscript */
5484
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 case VAR_NUMBER:
5486 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005487 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488 len = (long)STRLEN(s);
5489 if (range)
5490 {
5491 /* The resulting variable is a substring. If the indexes
5492 * are out of range the result is empty. */
5493 if (n1 < 0)
5494 {
5495 n1 = len + n1;
5496 if (n1 < 0)
5497 n1 = 0;
5498 }
5499 if (n2 < 0)
5500 n2 = len + n2;
5501 else if (n2 >= len)
5502 n2 = len;
5503 if (n1 >= len || n2 < 0 || n1 > n2)
5504 s = NULL;
5505 else
5506 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5507 }
5508 else
5509 {
5510 /* The resulting variable is a string of a single
5511 * character. If the index is too big or negative the
5512 * result is empty. */
5513 if (n1 >= len || n1 < 0)
5514 s = NULL;
5515 else
5516 s = vim_strnsave(s + n1, 1);
5517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518 clear_tv(rettv);
5519 rettv->v_type = VAR_STRING;
5520 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 break;
5522
5523 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 if (n1 < 0)
5526 n1 = len + n1;
5527 if (!empty1 && (n1 < 0 || n1 >= len))
5528 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005529 /* For a range we allow invalid values and return an empty
5530 * list. A list index out of range is an error. */
5531 if (!range)
5532 {
5533 if (verbose)
5534 EMSGN(_(e_listidx), n1);
5535 return FAIL;
5536 }
5537 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 }
5539 if (range)
5540 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005541 list_T *l;
5542 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543
5544 if (n2 < 0)
5545 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005546 else if (n2 >= len)
5547 n2 = len - 1;
5548 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005549 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 l = list_alloc();
5551 if (l == NULL)
5552 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 n1 <= n2; ++n1)
5555 {
5556 if (list_append_tv(l, &item->li_tv) == FAIL)
5557 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005558 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 return FAIL;
5560 }
5561 item = item->li_next;
5562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 clear_tv(rettv);
5564 rettv->v_type = VAR_LIST;
5565 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005566 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 }
5568 else
5569 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005570 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 clear_tv(rettv);
5572 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005573 }
5574 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575
5576 case VAR_DICT:
5577 if (range)
5578 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005579 if (verbose)
5580 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581 if (len == -1)
5582 clear_tv(&var1);
5583 return FAIL;
5584 }
5585 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005586 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587
5588 if (len == -1)
5589 {
5590 key = get_tv_string(&var1);
5591 if (*key == NUL)
5592 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005593 if (verbose)
5594 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 clear_tv(&var1);
5596 return FAIL;
5597 }
5598 }
5599
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005600 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005602 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005603 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 if (len == -1)
5605 clear_tv(&var1);
5606 if (item == NULL)
5607 return FAIL;
5608
5609 copy_tv(&item->di_tv, &var1);
5610 clear_tv(rettv);
5611 *rettv = var1;
5612 }
5613 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614 }
5615 }
5616
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005617 return OK;
5618}
5619
5620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 * Get an option value.
5622 * "arg" points to the '&' or '+' before the option name.
5623 * "arg" is advanced to character after the option name.
5624 * Return OK or FAIL.
5625 */
5626 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005627get_option_tv(
5628 char_u **arg,
5629 typval_T *rettv, /* when NULL, only check if option exists */
5630 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 char_u *option_end;
5633 long numval;
5634 char_u *stringval;
5635 int opt_type;
5636 int c;
5637 int working = (**arg == '+'); /* has("+option") */
5638 int ret = OK;
5639 int opt_flags;
5640
5641 /*
5642 * Isolate the option name and find its value.
5643 */
5644 option_end = find_option_end(arg, &opt_flags);
5645 if (option_end == NULL)
5646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 EMSG2(_("E112: Option name missing: %s"), *arg);
5649 return FAIL;
5650 }
5651
5652 if (!evaluate)
5653 {
5654 *arg = option_end;
5655 return OK;
5656 }
5657
5658 c = *option_end;
5659 *option_end = NUL;
5660 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005661 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662
5663 if (opt_type == -3) /* invalid name */
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 EMSG2(_("E113: Unknown option: %s"), *arg);
5667 ret = FAIL;
5668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 if (opt_type == -2) /* hidden string option */
5672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 rettv->v_type = VAR_STRING;
5674 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 }
5676 else if (opt_type == -1) /* hidden number option */
5677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 rettv->v_type = VAR_NUMBER;
5679 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
5681 else if (opt_type == 1) /* number option */
5682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683 rettv->v_type = VAR_NUMBER;
5684 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 else /* string option */
5687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005688 rettv->v_type = VAR_STRING;
5689 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691 }
5692 else if (working && (opt_type == -2 || opt_type == -1))
5693 ret = FAIL;
5694
5695 *option_end = c; /* put back for error messages */
5696 *arg = option_end;
5697
5698 return ret;
5699}
5700
5701/*
5702 * Allocate a variable for a string constant.
5703 * Return OK or FAIL.
5704 */
5705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005706get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708 char_u *p;
5709 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 int extra = 0;
5711
5712 /*
5713 * Find the end of the string, skipping backslashed characters.
5714 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005715 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
5717 if (*p == '\\' && p[1] != NUL)
5718 {
5719 ++p;
5720 /* A "\<x>" form occupies at least 4 characters, and produces up
5721 * to 6 characters: reserve space for 2 extra */
5722 if (*p == '<')
5723 extra += 2;
5724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
5726
5727 if (*p != '"')
5728 {
5729 EMSG2(_("E114: Missing quote: %s"), *arg);
5730 return FAIL;
5731 }
5732
5733 /* If only parsing, set *arg and return here */
5734 if (!evaluate)
5735 {
5736 *arg = p + 1;
5737 return OK;
5738 }
5739
5740 /*
5741 * Copy the string into allocated memory, handling backslashed
5742 * characters.
5743 */
5744 name = alloc((unsigned)(p - *arg + extra));
5745 if (name == NULL)
5746 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 rettv->v_type = VAR_STRING;
5748 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
5752 if (*p == '\\')
5753 {
5754 switch (*++p)
5755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 case 'b': *name++ = BS; ++p; break;
5757 case 'e': *name++ = ESC; ++p; break;
5758 case 'f': *name++ = FF; ++p; break;
5759 case 'n': *name++ = NL; ++p; break;
5760 case 'r': *name++ = CAR; ++p; break;
5761 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
5763 case 'X': /* hex: "\x1", "\x12" */
5764 case 'x':
5765 case 'u': /* Unicode: "\u0023" */
5766 case 'U':
5767 if (vim_isxdigit(p[1]))
5768 {
5769 int n, nr;
5770 int c = toupper(*p);
5771
5772 if (c == 'X')
5773 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005774 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005776 else
5777 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 nr = 0;
5779 while (--n >= 0 && vim_isxdigit(p[1]))
5780 {
5781 ++p;
5782 nr = (nr << 4) + hex2nr(*p);
5783 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785#ifdef FEAT_MBYTE
5786 /* For "\u" store the number according to
5787 * 'encoding'. */
5788 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 else
5791#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 break;
5795
5796 /* octal: "\1", "\12", "\123" */
5797 case '0':
5798 case '1':
5799 case '2':
5800 case '3':
5801 case '4':
5802 case '5':
5803 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 case '7': *name = *p++ - '0';
5805 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 *name = (*name << 3) + *p++ - '0';
5808 if (*p >= '0' && *p <= '7')
5809 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 break;
5813
5814 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 if (extra != 0)
5817 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 break;
5820 }
5821 /* FALLTHROUGH */
5822
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 break;
5825 }
5826 }
5827 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 *arg = p + 1;
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 return OK;
5835}
5836
5837/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 * Return OK or FAIL.
5840 */
5841 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005842get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843{
5844 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005845 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005846 int reduce = 0;
5847
5848 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 */
5851 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5852 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 break;
5857 ++reduce;
5858 ++p;
5859 }
5860 }
5861
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 return FAIL;
5866 }
5867
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 if (!evaluate)
5870 {
5871 *arg = p + 1;
5872 return OK;
5873 }
5874
5875 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 */
5878 str = alloc((unsigned)((p - *arg) - reduce));
5879 if (str == NULL)
5880 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 rettv->v_type = VAR_STRING;
5882 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 break;
5890 ++p;
5891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 *arg = p + 1;
5896
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 return OK;
5898}
5899
5900/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 * Allocate a variable for a List and fill it from "*arg".
5902 * Return OK or FAIL.
5903 */
5904 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005905get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906{
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 list_T *l = NULL;
5908 typval_T tv;
5909 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910
5911 if (evaluate)
5912 {
5913 l = list_alloc();
5914 if (l == NULL)
5915 return FAIL;
5916 }
5917
5918 *arg = skipwhite(*arg + 1);
5919 while (**arg != ']' && **arg != NUL)
5920 {
5921 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5922 goto failret;
5923 if (evaluate)
5924 {
5925 item = listitem_alloc();
5926 if (item != NULL)
5927 {
5928 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005929 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 list_append(l, item);
5931 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005932 else
5933 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 }
5935
5936 if (**arg == ']')
5937 break;
5938 if (**arg != ',')
5939 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005940 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 goto failret;
5942 }
5943 *arg = skipwhite(*arg + 1);
5944 }
5945
5946 if (**arg != ']')
5947 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005948 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949failret:
5950 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005951 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 return FAIL;
5953 }
5954
5955 *arg = skipwhite(*arg + 1);
5956 if (evaluate)
5957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005958 rettv->v_type = VAR_LIST;
5959 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 ++l->lv_refcount;
5961 }
5962
5963 return OK;
5964}
5965
5966/*
5967 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005968 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005970 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005973 list_T *l;
5974
5975 l = (list_T *)alloc_clear(sizeof(list_T));
5976 if (l != NULL)
5977 {
5978 /* Prepend the list to the list of lists for garbage collection. */
5979 if (first_list != NULL)
5980 first_list->lv_used_prev = l;
5981 l->lv_used_prev = NULL;
5982 l->lv_used_next = first_list;
5983 first_list = l;
5984 }
5985 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986}
5987
5988/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005989 * Allocate an empty list for a return value.
5990 * Returns OK or FAIL.
5991 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005992 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005993rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005994{
5995 list_T *l = list_alloc();
5996
5997 if (l == NULL)
5998 return FAIL;
5999
6000 rettv->vval.v_list = l;
6001 rettv->v_type = VAR_LIST;
6002 ++l->lv_refcount;
6003 return OK;
6004}
6005
6006/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 * Unreference a list: decrement the reference count and free it when it
6008 * becomes zero.
6009 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006010 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006011list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006013 if (l != NULL && --l->lv_refcount <= 0)
6014 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015}
6016
6017/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006018 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 * Ignores the reference count.
6020 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006021 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006022list_free(
6023 list_T *l,
6024 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025{
Bram Moolenaar33570922005-01-25 22:26:29 +00006026 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006028 /* Remove the list from the list of lists for garbage collection. */
6029 if (l->lv_used_prev == NULL)
6030 first_list = l->lv_used_next;
6031 else
6032 l->lv_used_prev->lv_used_next = l->lv_used_next;
6033 if (l->lv_used_next != NULL)
6034 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6035
Bram Moolenaard9fba312005-06-26 22:34:35 +00006036 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006038 /* Remove the item before deleting it. */
6039 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006040 if (recurse || (item->li_tv.v_type != VAR_LIST
6041 && item->li_tv.v_type != VAR_DICT))
6042 clear_tv(&item->li_tv);
6043 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 }
6045 vim_free(l);
6046}
6047
6048/*
6049 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006050 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006052 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006053listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054{
Bram Moolenaar33570922005-01-25 22:26:29 +00006055 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056}
6057
6058/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006059 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006062listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006064 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 vim_free(item);
6066}
6067
6068/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006069 * Remove a list item from a List and free it. Also clears the value.
6070 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006071 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006072listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006073{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006074 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075 listitem_free(item);
6076}
6077
6078/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079 * Get the number of items in a list.
6080 */
6081 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006082list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 if (l == NULL)
6085 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006086 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087}
6088
6089/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090 * Return TRUE when two lists have exactly the same values.
6091 */
6092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006093list_equal(
6094 list_T *l1,
6095 list_T *l2,
6096 int ic, /* ignore case for strings */
6097 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098{
Bram Moolenaar33570922005-01-25 22:26:29 +00006099 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006101 if (l1 == NULL || l2 == NULL)
6102 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006103 if (l1 == l2)
6104 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006105 if (list_len(l1) != list_len(l2))
6106 return FALSE;
6107
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006108 for (item1 = l1->lv_first, item2 = l2->lv_first;
6109 item1 != NULL && item2 != NULL;
6110 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112 return FALSE;
6113 return item1 == NULL && item2 == NULL;
6114}
6115
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006116/*
6117 * Return the dictitem that an entry in a hashtable points to.
6118 */
6119 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006121{
6122 return HI2DI(hi);
6123}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006124
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006126 * Return TRUE when two dictionaries have exactly the same key/values.
6127 */
6128 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006129dict_equal(
6130 dict_T *d1,
6131 dict_T *d2,
6132 int ic, /* ignore case for strings */
6133 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006134{
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 hashitem_T *hi;
6136 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006137 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006138
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006139 if (d1 == NULL || d2 == NULL)
6140 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006141 if (d1 == d2)
6142 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143 if (dict_len(d1) != dict_len(d2))
6144 return FALSE;
6145
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006146 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006148 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006149 if (!HASHITEM_EMPTY(hi))
6150 {
6151 item2 = dict_find(d2, hi->hi_key, -1);
6152 if (item2 == NULL)
6153 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006154 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 return FALSE;
6156 --todo;
6157 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006158 }
6159 return TRUE;
6160}
6161
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006162static int tv_equal_recurse_limit;
6163
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006164/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006165 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006166 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006167 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006168 */
6169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006170tv_equal(
6171 typval_T *tv1,
6172 typval_T *tv2,
6173 int ic, /* ignore case */
6174 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175{
6176 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006177 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006178 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006179 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006180
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006181 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006182 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006184 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185 * recursiveness to a limit. We guess they are equal then.
6186 * A fixed limit has the problem of still taking an awful long time.
6187 * Reduce the limit every time running into it. That should work fine for
6188 * deeply linked structures that are not recursively linked and catch
6189 * recursiveness quickly. */
6190 if (!recursive)
6191 tv_equal_recurse_limit = 1000;
6192 if (recursive_cnt >= tv_equal_recurse_limit)
6193 {
6194 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006195 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006196 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006197
6198 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006199 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006200 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201 ++recursive_cnt;
6202 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6203 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006204 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006205
6206 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 ++recursive_cnt;
6208 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6209 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006210 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006211
6212 case VAR_FUNC:
6213 return (tv1->vval.v_string != NULL
6214 && tv2->vval.v_string != NULL
6215 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6216
6217 case VAR_NUMBER:
6218 return tv1->vval.v_number == tv2->vval.v_number;
6219
6220 case VAR_STRING:
6221 s1 = get_tv_string_buf(tv1, buf1);
6222 s2 = get_tv_string_buf(tv2, buf2);
6223 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006224
6225 case VAR_SPECIAL:
6226 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006227
6228 case VAR_FLOAT:
6229#ifdef FEAT_FLOAT
6230 return tv1->vval.v_float == tv2->vval.v_float;
6231#endif
6232 case VAR_JOB:
6233#ifdef FEAT_JOB
6234 return tv1->vval.v_job == tv2->vval.v_job;
6235#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006236 case VAR_CHANNEL:
6237#ifdef FEAT_CHANNEL
6238 return tv1->vval.v_channel == tv2->vval.v_channel;
6239#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006240 case VAR_UNKNOWN:
6241 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006243
Bram Moolenaara03f2332016-02-06 18:09:59 +01006244 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6245 * does not equal anything, not even itself. */
6246 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006247}
6248
6249/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006250 * Locate item with index "n" in list "l" and return it.
6251 * A negative index is counted from the end; -1 is the last item.
6252 * Returns NULL when "n" is out of range.
6253 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006254 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006255list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256{
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258 long idx;
6259
6260 if (l == NULL)
6261 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006262
6263 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006265 n = l->lv_len + n;
6266
6267 /* Check for index out of range. */
6268 if (n < 0 || n >= l->lv_len)
6269 return NULL;
6270
6271 /* When there is a cached index may start search from there. */
6272 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006273 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006274 if (n < l->lv_idx / 2)
6275 {
6276 /* closest to the start of the list */
6277 item = l->lv_first;
6278 idx = 0;
6279 }
6280 else if (n > (l->lv_idx + l->lv_len) / 2)
6281 {
6282 /* closest to the end of the list */
6283 item = l->lv_last;
6284 idx = l->lv_len - 1;
6285 }
6286 else
6287 {
6288 /* closest to the cached index */
6289 item = l->lv_idx_item;
6290 idx = l->lv_idx;
6291 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292 }
6293 else
6294 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006295 if (n < l->lv_len / 2)
6296 {
6297 /* closest to the start of the list */
6298 item = l->lv_first;
6299 idx = 0;
6300 }
6301 else
6302 {
6303 /* closest to the end of the list */
6304 item = l->lv_last;
6305 idx = l->lv_len - 1;
6306 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006307 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006308
6309 while (n > idx)
6310 {
6311 /* search forward */
6312 item = item->li_next;
6313 ++idx;
6314 }
6315 while (n < idx)
6316 {
6317 /* search backward */
6318 item = item->li_prev;
6319 --idx;
6320 }
6321
6322 /* cache the used index */
6323 l->lv_idx = idx;
6324 l->lv_idx_item = item;
6325
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 return item;
6327}
6328
6329/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006330 * Get list item "l[idx]" as a number.
6331 */
6332 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006333list_find_nr(
6334 list_T *l,
6335 long idx,
6336 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006337{
6338 listitem_T *li;
6339
6340 li = list_find(l, idx);
6341 if (li == NULL)
6342 {
6343 if (errorp != NULL)
6344 *errorp = TRUE;
6345 return -1L;
6346 }
6347 return get_tv_number_chk(&li->li_tv, errorp);
6348}
6349
6350/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006351 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6352 */
6353 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006354list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006355{
6356 listitem_T *li;
6357
6358 li = list_find(l, idx - 1);
6359 if (li == NULL)
6360 {
6361 EMSGN(_(e_listidx), idx);
6362 return NULL;
6363 }
6364 return get_tv_string(&li->li_tv);
6365}
6366
6367/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006368 * Locate "item" list "l" and return its index.
6369 * Returns -1 when "item" is not in the list.
6370 */
6371 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006372list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006373{
6374 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006375 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006376
6377 if (l == NULL)
6378 return -1;
6379 idx = 0;
6380 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6381 ++idx;
6382 if (li == NULL)
6383 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006384 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006385}
6386
6387/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006388 * Append item "item" to the end of list "l".
6389 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006390 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006391list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392{
6393 if (l->lv_last == NULL)
6394 {
6395 /* empty list */
6396 l->lv_first = item;
6397 l->lv_last = item;
6398 item->li_prev = NULL;
6399 }
6400 else
6401 {
6402 l->lv_last->li_next = item;
6403 item->li_prev = l->lv_last;
6404 l->lv_last = item;
6405 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006406 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 item->li_next = NULL;
6408}
6409
6410/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006412 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006414 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006415list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006417 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418
Bram Moolenaar05159a02005-02-26 23:04:13 +00006419 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006421 copy_tv(tv, &li->li_tv);
6422 list_append(l, li);
6423 return OK;
6424}
6425
6426/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006427 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006428 * Return FAIL when out of memory.
6429 */
6430 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006431list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006432{
6433 listitem_T *li = listitem_alloc();
6434
6435 if (li == NULL)
6436 return FAIL;
6437 li->li_tv.v_type = VAR_DICT;
6438 li->li_tv.v_lock = 0;
6439 li->li_tv.vval.v_dict = dict;
6440 list_append(list, li);
6441 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442 return OK;
6443}
6444
6445/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006446 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006447 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006448 * Returns FAIL when out of memory.
6449 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006450 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006451list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006452{
6453 listitem_T *li = listitem_alloc();
6454
6455 if (li == NULL)
6456 return FAIL;
6457 list_append(l, li);
6458 li->li_tv.v_type = VAR_STRING;
6459 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006460 if (str == NULL)
6461 li->li_tv.vval.v_string = NULL;
6462 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006463 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006464 return FAIL;
6465 return OK;
6466}
6467
6468/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006469 * Append "n" to list "l".
6470 * Returns FAIL when out of memory.
6471 */
6472 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006473list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006474{
6475 listitem_T *li;
6476
6477 li = listitem_alloc();
6478 if (li == NULL)
6479 return FAIL;
6480 li->li_tv.v_type = VAR_NUMBER;
6481 li->li_tv.v_lock = 0;
6482 li->li_tv.vval.v_number = n;
6483 list_append(l, li);
6484 return OK;
6485}
6486
6487/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006488 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006489 * If "item" is NULL append at the end.
6490 * Return FAIL when out of memory.
6491 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006493list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494{
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496
6497 if (ni == NULL)
6498 return FAIL;
6499 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006500 list_insert(l, ni, item);
6501 return OK;
6502}
6503
6504 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006505list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006506{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 if (item == NULL)
6508 /* Append new item at end of list. */
6509 list_append(l, ni);
6510 else
6511 {
6512 /* Insert new item before existing item. */
6513 ni->li_prev = item->li_prev;
6514 ni->li_next = item;
6515 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006516 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006518 ++l->lv_idx;
6519 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 l->lv_idx_item = NULL;
6524 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006526 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006528}
6529
6530/*
6531 * Extend "l1" with "l2".
6532 * If "bef" is NULL append at the end, otherwise insert before this item.
6533 * Returns FAIL when out of memory.
6534 */
6535 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006536list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537{
Bram Moolenaar33570922005-01-25 22:26:29 +00006538 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006539 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006541 /* We also quit the loop when we have inserted the original item count of
6542 * the list, avoid a hang when we extend a list with itself. */
6543 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6545 return FAIL;
6546 return OK;
6547}
6548
6549/*
6550 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6551 * Return FAIL when out of memory.
6552 */
6553 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006554list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555{
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006558 if (l1 == NULL || l2 == NULL)
6559 return FAIL;
6560
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 if (l == NULL)
6564 return FAIL;
6565 tv->v_type = VAR_LIST;
6566 tv->vval.v_list = l;
6567
6568 /* append all items from the second list */
6569 return list_extend(l, l2, NULL);
6570}
6571
6572/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006573 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006575 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576 * Returns NULL when out of memory.
6577 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006579list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580{
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 list_T *copy;
6582 listitem_T *item;
6583 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584
6585 if (orig == NULL)
6586 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587
6588 copy = list_alloc();
6589 if (copy != NULL)
6590 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006591 if (copyID != 0)
6592 {
6593 /* Do this before adding the items, because one of the items may
6594 * refer back to this list. */
6595 orig->lv_copyID = copyID;
6596 orig->lv_copylist = copy;
6597 }
6598 for (item = orig->lv_first; item != NULL && !got_int;
6599 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 {
6601 ni = listitem_alloc();
6602 if (ni == NULL)
6603 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006604 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006605 {
6606 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6607 {
6608 vim_free(ni);
6609 break;
6610 }
6611 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006613 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 list_append(copy, ni);
6615 }
6616 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006617 if (item != NULL)
6618 {
6619 list_unref(copy);
6620 copy = NULL;
6621 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622 }
6623
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006624 return copy;
6625}
6626
6627/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006628 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006629 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006630 * This used to be called list_remove, but that conflicts with a Sun header
6631 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006632 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006633 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006634vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006635{
Bram Moolenaar33570922005-01-25 22:26:29 +00006636 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006638 /* notify watchers */
6639 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006641 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006642 list_fix_watch(l, ip);
6643 if (ip == item2)
6644 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006646
6647 if (item2->li_next == NULL)
6648 l->lv_last = item->li_prev;
6649 else
6650 item2->li_next->li_prev = item->li_prev;
6651 if (item->li_prev == NULL)
6652 l->lv_first = item2->li_next;
6653 else
6654 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006655 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656}
6657
6658/*
6659 * Return an allocated string with the string representation of a list.
6660 * May return NULL.
6661 */
6662 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006663list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664{
6665 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666
6667 if (tv->vval.v_list == NULL)
6668 return NULL;
6669 ga_init2(&ga, (int)sizeof(char), 80);
6670 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006671 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006672 {
6673 vim_free(ga.ga_data);
6674 return NULL;
6675 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006676 ga_append(&ga, ']');
6677 ga_append(&ga, NUL);
6678 return (char_u *)ga.ga_data;
6679}
6680
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006681typedef struct join_S {
6682 char_u *s;
6683 char_u *tofree;
6684} join_T;
6685
6686 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006687list_join_inner(
6688 garray_T *gap, /* to store the result in */
6689 list_T *l,
6690 char_u *sep,
6691 int echo_style,
6692 int copyID,
6693 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006694{
6695 int i;
6696 join_T *p;
6697 int len;
6698 int sumlen = 0;
6699 int first = TRUE;
6700 char_u *tofree;
6701 char_u numbuf[NUMBUFLEN];
6702 listitem_T *item;
6703 char_u *s;
6704
6705 /* Stringify each item in the list. */
6706 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6707 {
6708 if (echo_style)
6709 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6710 else
6711 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6712 if (s == NULL)
6713 return FAIL;
6714
6715 len = (int)STRLEN(s);
6716 sumlen += len;
6717
Bram Moolenaarcde88542015-08-11 19:14:00 +02006718 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006719 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6720 if (tofree != NULL || s != numbuf)
6721 {
6722 p->s = s;
6723 p->tofree = tofree;
6724 }
6725 else
6726 {
6727 p->s = vim_strnsave(s, len);
6728 p->tofree = p->s;
6729 }
6730
6731 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006732 if (did_echo_string_emsg) /* recursion error, bail out */
6733 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006734 }
6735
6736 /* Allocate result buffer with its total size, avoid re-allocation and
6737 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6738 if (join_gap->ga_len >= 2)
6739 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6740 if (ga_grow(gap, sumlen + 2) == FAIL)
6741 return FAIL;
6742
6743 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6744 {
6745 if (first)
6746 first = FALSE;
6747 else
6748 ga_concat(gap, sep);
6749 p = ((join_T *)join_gap->ga_data) + i;
6750
6751 if (p->s != NULL)
6752 ga_concat(gap, p->s);
6753 line_breakcheck();
6754 }
6755
6756 return OK;
6757}
6758
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006759/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006760 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006761 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006762 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006763 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006764 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006765list_join(
6766 garray_T *gap,
6767 list_T *l,
6768 char_u *sep,
6769 int echo_style,
6770 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006771{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006772 garray_T join_ga;
6773 int retval;
6774 join_T *p;
6775 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776
Bram Moolenaard39a7512015-04-16 22:51:22 +02006777 if (l->lv_len < 1)
6778 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006779 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6780 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6781
6782 /* Dispose each item in join_ga. */
6783 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006784 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006785 p = (join_T *)join_ga.ga_data;
6786 for (i = 0; i < join_ga.ga_len; ++i)
6787 {
6788 vim_free(p->tofree);
6789 ++p;
6790 }
6791 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006792 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006793
6794 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795}
6796
6797/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006798 * Return the next (unique) copy ID.
6799 * Used for serializing nested structures.
6800 */
6801 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006802get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006803{
6804 current_copyID += COPYID_INC;
6805 return current_copyID;
6806}
6807
6808/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006809 * Garbage collection for lists and dictionaries.
6810 *
6811 * We use reference counts to be able to free most items right away when they
6812 * are no longer used. But for composite items it's possible that it becomes
6813 * unused while the reference count is > 0: When there is a recursive
6814 * reference. Example:
6815 * :let l = [1, 2, 3]
6816 * :let d = {9: l}
6817 * :let l[1] = d
6818 *
6819 * Since this is quite unusual we handle this with garbage collection: every
6820 * once in a while find out which lists and dicts are not referenced from any
6821 * variable.
6822 *
6823 * Here is a good reference text about garbage collection (refers to Python
6824 * but it applies to all reference-counting mechanisms):
6825 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006826 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006827
6828/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006829 * Do garbage collection for lists and dicts.
6830 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006831 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006833garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006834{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006835 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006836 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 buf_T *buf;
6838 win_T *wp;
6839 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006840 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006841 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006842 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006843#ifdef FEAT_WINDOWS
6844 tabpage_T *tp;
6845#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006846
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006847 /* Only do this once. */
6848 want_garbage_collect = FALSE;
6849 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006850 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006851
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006852 /* We advance by two because we add one for items referenced through
6853 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006854 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006855
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 /*
6857 * 1. Go through all accessible variables and mark all lists and dicts
6858 * with copyID.
6859 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860
6861 /* Don't free variables in the previous_funccal list unless they are only
6862 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006863 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6865 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006866 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6867 NULL);
6868 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6869 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 }
6871
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872 /* script-local variables */
6873 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006874 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875
6876 /* buffer-local variables */
6877 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006878 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6879 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006880
6881 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006882 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6884 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006885#ifdef FEAT_AUTOCMD
6886 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006887 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6888 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006889#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006891#ifdef FEAT_WINDOWS
6892 /* tabpage-local variables */
6893 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006894 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6895 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006896#endif
6897
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900
6901 /* function-local variables */
6902 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6903 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6905 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906 }
6907
Bram Moolenaard812df62008-11-09 12:46:09 +00006908 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006910
Bram Moolenaar1dced572012-04-05 16:54:08 +02006911#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006913#endif
6914
Bram Moolenaardb913952012-06-29 12:54:53 +02006915#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006917#endif
6918
6919#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006920 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006921#endif
6922
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006923#ifdef FEAT_CHANNEL
6924 abort = abort || set_ref_in_channel(copyID);
6925#endif
6926
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006928 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 /*
6930 * 2. Free lists and dictionaries that are not referenced.
6931 */
6932 did_free = free_unref_items(copyID);
6933
6934 /*
6935 * 3. Check if any funccal can be freed now.
6936 */
6937 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006938 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 if (can_free_funccal(*pfc, copyID))
6940 {
6941 fc = *pfc;
6942 *pfc = fc->caller;
6943 free_funccal(fc, TRUE);
6944 did_free = TRUE;
6945 did_free_funccal = TRUE;
6946 }
6947 else
6948 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006949 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 if (did_free_funccal)
6951 /* When a funccal was freed some more items might be garbage
6952 * collected, so run again. */
6953 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 else if (p_verbose > 0)
6956 {
6957 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6958 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959
6960 return did_free;
6961}
6962
6963/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006964 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 */
6966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006967free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006969 dict_T *dd, *dd_next;
6970 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971 int did_free = FALSE;
6972
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975 */
6976 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006977 {
6978 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006981 /* Free the Dictionary and ordinary items it contains, but don't
6982 * recurse into Lists and Dictionaries, they will be in the list
6983 * of dicts or list of lists. */
6984 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006987 dd = dd_next;
6988 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989
6990 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006991 * Go through the list of lists and free items without the copyID.
6992 * But don't free a list that has a watcher (used in a for loop), these
6993 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 */
6995 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006996 {
6997 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006998 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6999 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007001 /* Free the List and ordinary items it contains, but don't recurse
7002 * into Lists and Dictionaries, they will be in the list of dicts
7003 * or list of lists. */
7004 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007007 ll = ll_next;
7008 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007009
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 return did_free;
7011}
7012
7013/*
7014 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007015 * "list_stack" is used to add lists to be marked. Can be NULL.
7016 *
7017 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007018 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007019 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007020set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021{
7022 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007023 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 hashtab_T *cur_ht;
7026 ht_stack_T *ht_stack = NULL;
7027 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 cur_ht = ht;
7030 for (;;)
7031 {
7032 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 /* Mark each item in the hashtab. If the item contains a hashtab
7035 * it is added to ht_stack, if it contains a list it is added to
7036 * list_stack. */
7037 todo = (int)cur_ht->ht_used;
7038 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7039 if (!HASHITEM_EMPTY(hi))
7040 {
7041 --todo;
7042 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7043 &ht_stack, list_stack);
7044 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046
7047 if (ht_stack == NULL)
7048 break;
7049
7050 /* take an item from the stack */
7051 cur_ht = ht_stack->ht;
7052 tempitem = ht_stack;
7053 ht_stack = ht_stack->prev;
7054 free(tempitem);
7055 }
7056
7057 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058}
7059
7060/*
7061 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007062 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7063 *
7064 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007065 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007066 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007067set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007069 listitem_T *li;
7070 int abort = FALSE;
7071 list_T *cur_l;
7072 list_stack_T *list_stack = NULL;
7073 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 cur_l = l;
7076 for (;;)
7077 {
7078 if (!abort)
7079 /* Mark each item in the list. If the item contains a hashtab
7080 * it is added to ht_stack, if it contains a list it is added to
7081 * list_stack. */
7082 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7083 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7084 ht_stack, &list_stack);
7085 if (list_stack == NULL)
7086 break;
7087
7088 /* take an item from the stack */
7089 cur_l = list_stack->list;
7090 tempitem = list_stack;
7091 list_stack = list_stack->prev;
7092 free(tempitem);
7093 }
7094
7095 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007096}
7097
7098/*
7099 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007100 * "list_stack" is used to add lists to be marked. Can be NULL.
7101 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7102 *
7103 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007104 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007106set_ref_in_item(
7107 typval_T *tv,
7108 int copyID,
7109 ht_stack_T **ht_stack,
7110 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007111{
7112 dict_T *dd;
7113 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007114 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007115
Bram Moolenaara03f2332016-02-06 18:09:59 +01007116 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007117 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007118 dd = tv->vval.v_dict;
7119 if (dd != NULL && dd->dv_copyID != copyID)
7120 {
7121 /* Didn't see this dict yet. */
7122 dd->dv_copyID = copyID;
7123 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007124 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007125 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7126 }
7127 else
7128 {
7129 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7130 if (newitem == NULL)
7131 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007132 else
7133 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007134 newitem->ht = &dd->dv_hashtab;
7135 newitem->prev = *ht_stack;
7136 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007138 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007139 }
7140 }
7141 else if (tv->v_type == VAR_LIST)
7142 {
7143 ll = tv->vval.v_list;
7144 if (ll != NULL && ll->lv_copyID != copyID)
7145 {
7146 /* Didn't see this list yet. */
7147 ll->lv_copyID = copyID;
7148 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007149 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007150 abort = set_ref_in_list(ll, copyID, ht_stack);
7151 }
7152 else
7153 {
7154 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007155 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007156 if (newitem == NULL)
7157 abort = TRUE;
7158 else
7159 {
7160 newitem->list = ll;
7161 newitem->prev = *list_stack;
7162 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007163 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007164 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007165 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007166 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007167 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007168}
7169
7170/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171 * Allocate an empty header for a dictionary.
7172 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007173 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007174dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175{
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007177
Bram Moolenaar33570922005-01-25 22:26:29 +00007178 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007179 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007180 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007181 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007182 if (first_dict != NULL)
7183 first_dict->dv_used_prev = d;
7184 d->dv_used_next = first_dict;
7185 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007186 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007187
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007189 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007190 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007191 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007192 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007193 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007194 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007195}
7196
7197/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007198 * Allocate an empty dict for a return value.
7199 * Returns OK or FAIL.
7200 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007202rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007203{
7204 dict_T *d = dict_alloc();
7205
7206 if (d == NULL)
7207 return FAIL;
7208
7209 rettv->vval.v_dict = d;
7210 rettv->v_type = VAR_DICT;
7211 ++d->dv_refcount;
7212 return OK;
7213}
7214
7215
7216/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217 * Unreference a Dictionary: decrement the reference count and free it when it
7218 * becomes zero.
7219 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007221dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007223 if (d != NULL && --d->dv_refcount <= 0)
7224 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225}
7226
7227/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007228 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229 * Ignores the reference count.
7230 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007232dict_free(
7233 dict_T *d,
7234 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007236 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007237 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007238 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007240 /* Remove the dict from the list of dicts for garbage collection. */
7241 if (d->dv_used_prev == NULL)
7242 first_dict = d->dv_used_next;
7243 else
7244 d->dv_used_prev->dv_used_next = d->dv_used_next;
7245 if (d->dv_used_next != NULL)
7246 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7247
7248 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007249 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007250 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007253 if (!HASHITEM_EMPTY(hi))
7254 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007255 /* Remove the item before deleting it, just in case there is
7256 * something recursive causing trouble. */
7257 di = HI2DI(hi);
7258 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007259 if (recurse || (di->di_tv.v_type != VAR_LIST
7260 && di->di_tv.v_type != VAR_DICT))
7261 clear_tv(&di->di_tv);
7262 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007263 --todo;
7264 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 vim_free(d);
7268}
7269
7270/*
7271 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007272 * The "key" is copied to the new item.
7273 * Note that the value of the item "di_tv" still needs to be initialized!
7274 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007276 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007277dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278{
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007281 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007283 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007285 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007286 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288}
7289
7290/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007291 * Make a copy of a Dictionary item.
7292 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007293 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007294dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295{
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007298 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7299 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300 if (di != NULL)
7301 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007303 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304 copy_tv(&org->di_tv, &di->di_tv);
7305 }
7306 return di;
7307}
7308
7309/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310 * Remove item "item" from Dictionary "dict" and free it.
7311 */
7312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007313dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314{
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 if (HASHITEM_EMPTY(hi))
7319 EMSG2(_(e_intern2), "dictitem_remove()");
7320 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 dictitem_free(item);
7323}
7324
7325/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326 * Free a dict item. Also clears the value.
7327 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007329dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007330{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007332 if (item->di_flags & DI_FLAGS_ALLOC)
7333 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007334}
7335
7336/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007337 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7338 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007339 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 * Returns NULL when out of memory.
7341 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007343dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007344{
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 dict_T *copy;
7346 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349
7350 if (orig == NULL)
7351 return NULL;
7352
7353 copy = dict_alloc();
7354 if (copy != NULL)
7355 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007356 if (copyID != 0)
7357 {
7358 orig->dv_copyID = copyID;
7359 orig->dv_copydict = copy;
7360 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007361 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007362 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 --todo;
7367
7368 di = dictitem_alloc(hi->hi_key);
7369 if (di == NULL)
7370 break;
7371 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007372 {
7373 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7374 copyID) == FAIL)
7375 {
7376 vim_free(di);
7377 break;
7378 }
7379 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007380 else
7381 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7382 if (dict_add(copy, di) == FAIL)
7383 {
7384 dictitem_free(di);
7385 break;
7386 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007389
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391 if (todo > 0)
7392 {
7393 dict_unref(copy);
7394 copy = NULL;
7395 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 }
7397
7398 return copy;
7399}
7400
7401/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007403 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007405 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007406dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407{
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409}
7410
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007412 * Add a number or string entry to dictionary "d".
7413 * When "str" is NULL use number "nr", otherwise use "str".
7414 * Returns FAIL when out of memory and when key already exists.
7415 */
7416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007417dict_add_nr_str(
7418 dict_T *d,
7419 char *key,
7420 long nr,
7421 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007422{
7423 dictitem_T *item;
7424
7425 item = dictitem_alloc((char_u *)key);
7426 if (item == NULL)
7427 return FAIL;
7428 item->di_tv.v_lock = 0;
7429 if (str == NULL)
7430 {
7431 item->di_tv.v_type = VAR_NUMBER;
7432 item->di_tv.vval.v_number = nr;
7433 }
7434 else
7435 {
7436 item->di_tv.v_type = VAR_STRING;
7437 item->di_tv.vval.v_string = vim_strsave(str);
7438 }
7439 if (dict_add(d, item) == FAIL)
7440 {
7441 dictitem_free(item);
7442 return FAIL;
7443 }
7444 return OK;
7445}
7446
7447/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007448 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007449 * Returns FAIL when out of memory and when key already exists.
7450 */
7451 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007452dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007453{
7454 dictitem_T *item;
7455
7456 item = dictitem_alloc((char_u *)key);
7457 if (item == NULL)
7458 return FAIL;
7459 item->di_tv.v_lock = 0;
7460 item->di_tv.v_type = VAR_LIST;
7461 item->di_tv.vval.v_list = list;
7462 if (dict_add(d, item) == FAIL)
7463 {
7464 dictitem_free(item);
7465 return FAIL;
7466 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007467 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007468 return OK;
7469}
7470
7471/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007472 * Get the number of items in a Dictionary.
7473 */
7474 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007475dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007476{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 if (d == NULL)
7478 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007479 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480}
7481
7482/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007483 * Find item "key[len]" in Dictionary "d".
7484 * If "len" is negative use strlen(key).
7485 * Returns NULL when not found.
7486 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007487 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007488dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007490#define AKEYLEN 200
7491 char_u buf[AKEYLEN];
7492 char_u *akey;
7493 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007494 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496 if (len < 0)
7497 akey = key;
7498 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007499 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007500 tofree = akey = vim_strnsave(key, len);
7501 if (akey == NULL)
7502 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007503 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007504 else
7505 {
7506 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007507 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007508 akey = buf;
7509 }
7510
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007512 vim_free(tofree);
7513 if (HASHITEM_EMPTY(hi))
7514 return NULL;
7515 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516}
7517
7518/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007519 * Get a string item from a dictionary.
7520 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007521 * Returns NULL if the entry doesn't exist or out of memory.
7522 */
7523 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007524get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007525{
7526 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007527 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007528
7529 di = dict_find(d, key, -1);
7530 if (di == NULL)
7531 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007532 s = get_tv_string(&di->di_tv);
7533 if (save && s != NULL)
7534 s = vim_strsave(s);
7535 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007536}
7537
7538/*
7539 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007540 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007541 */
7542 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007543get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007544{
7545 dictitem_T *di;
7546
7547 di = dict_find(d, key, -1);
7548 if (di == NULL)
7549 return 0;
7550 return get_tv_number(&di->di_tv);
7551}
7552
7553/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 * Return an allocated string with the string representation of a Dictionary.
7555 * May return NULL.
7556 */
7557 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007558dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559{
7560 garray_T ga;
7561 int first = TRUE;
7562 char_u *tofree;
7563 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007567 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007569 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570 return NULL;
7571 ga_init2(&ga, (int)sizeof(char), 80);
7572 ga_append(&ga, '{');
7573
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007574 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007575 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007579 --todo;
7580
7581 if (first)
7582 first = FALSE;
7583 else
7584 ga_concat(&ga, (char_u *)", ");
7585
7586 tofree = string_quote(hi->hi_key, FALSE);
7587 if (tofree != NULL)
7588 {
7589 ga_concat(&ga, tofree);
7590 vim_free(tofree);
7591 }
7592 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007593 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 if (s != NULL)
7595 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007597 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007598 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007599 line_breakcheck();
7600
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007603 if (todo > 0)
7604 {
7605 vim_free(ga.ga_data);
7606 return NULL;
7607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608
7609 ga_append(&ga, '}');
7610 ga_append(&ga, NUL);
7611 return (char_u *)ga.ga_data;
7612}
7613
7614/*
7615 * Allocate a variable for a Dictionary and fill it from "*arg".
7616 * Return OK or FAIL. Returns NOTDONE for {expr}.
7617 */
7618 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007619get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620{
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 dict_T *d = NULL;
7622 typval_T tvkey;
7623 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007624 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007627 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628
7629 /*
7630 * First check if it's not a curly-braces thing: {expr}.
7631 * Must do this without evaluating, otherwise a function may be called
7632 * twice. Unfortunately this means we need to call eval1() twice for the
7633 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007634 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007636 if (*start != '}')
7637 {
7638 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7639 return FAIL;
7640 if (*start == '}')
7641 return NOTDONE;
7642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643
7644 if (evaluate)
7645 {
7646 d = dict_alloc();
7647 if (d == NULL)
7648 return FAIL;
7649 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007650 tvkey.v_type = VAR_UNKNOWN;
7651 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652
7653 *arg = skipwhite(*arg + 1);
7654 while (**arg != '}' && **arg != NUL)
7655 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007656 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 goto failret;
7658 if (**arg != ':')
7659 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007660 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 goto failret;
7663 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007664 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007666 key = get_tv_string_buf_chk(&tvkey, buf);
7667 if (key == NULL || *key == NUL)
7668 {
7669 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7670 if (key != NULL)
7671 EMSG(_(e_emptykey));
7672 clear_tv(&tvkey);
7673 goto failret;
7674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676
7677 *arg = skipwhite(*arg + 1);
7678 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7679 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007680 if (evaluate)
7681 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 goto failret;
7683 }
7684 if (evaluate)
7685 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007686 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 if (item != NULL)
7688 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007689 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007690 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 clear_tv(&tv);
7692 goto failret;
7693 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007694 item = dictitem_alloc(key);
7695 clear_tv(&tvkey);
7696 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007699 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007700 if (dict_add(d, item) == FAIL)
7701 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 }
7703 }
7704
7705 if (**arg == '}')
7706 break;
7707 if (**arg != ',')
7708 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007709 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 goto failret;
7711 }
7712 *arg = skipwhite(*arg + 1);
7713 }
7714
7715 if (**arg != '}')
7716 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007717 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718failret:
7719 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007720 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 return FAIL;
7722 }
7723
7724 *arg = skipwhite(*arg + 1);
7725 if (evaluate)
7726 {
7727 rettv->v_type = VAR_DICT;
7728 rettv->vval.v_dict = d;
7729 ++d->dv_refcount;
7730 }
7731
7732 return OK;
7733}
7734
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007735#if defined(FEAT_CHANNEL) || defined(PROTO)
7736/*
7737 * Decrement the reference count on "channel" and free it when it goes down to
7738 * zero.
7739 * Returns TRUE when the channel was freed.
7740 */
7741 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007742channel_unref(channel_T *channel)
7743{
7744 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007745 {
Bram Moolenaar77073442016-02-13 23:23:53 +01007746 channel_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007747 return TRUE;
7748 }
7749 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007750}
7751#endif
7752
Bram Moolenaar835dc632016-02-07 14:27:38 +01007753#ifdef FEAT_JOB
7754 static void
7755job_free(job_T *job)
7756{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007757# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007758 if (job->jv_channel != NULL)
7759 {
7760 /* The channel doesn't count as a references for the job, we need to
7761 * NULL the reference when the job is freed. */
7762 job->jv_channel->ch_job = NULL;
7763 channel_unref(job->jv_channel);
7764 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007765# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007766 mch_clear_job(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007767 vim_free(job);
7768}
7769
7770 static void
7771job_unref(job_T *job)
7772{
7773 if (job != NULL && --job->jv_refcount <= 0)
7774 job_free(job);
7775}
7776
7777/*
7778 * Allocate a job. Sets the refcount to one.
7779 */
7780 static job_T *
7781job_alloc(void)
7782{
7783 job_T *job;
7784
7785 job = (job_T *)alloc_clear(sizeof(job_T));
7786 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007787 job->jv_refcount = 1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007788 return job;
7789}
7790
7791#endif
7792
Bram Moolenaar17a13432016-01-24 14:22:10 +01007793 static char *
7794get_var_special_name(int nr)
7795{
7796 switch (nr)
7797 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007798 case VVAL_FALSE: return "v:false";
7799 case VVAL_TRUE: return "v:true";
7800 case VVAL_NONE: return "v:none";
7801 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007802 }
7803 EMSG2(_(e_intern2), "get_var_special_name()");
7804 return "42";
7805}
7806
Bram Moolenaar8c711452005-01-14 21:53:12 +00007807/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007808 * Return a string with the string representation of a variable.
7809 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007810 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007811 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007812 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007813 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007814 */
7815 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007816echo_string(
7817 typval_T *tv,
7818 char_u **tofree,
7819 char_u *numbuf,
7820 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007821{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822 static int recurse = 0;
7823 char_u *r = NULL;
7824
Bram Moolenaar33570922005-01-25 22:26:29 +00007825 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007826 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007827 if (!did_echo_string_emsg)
7828 {
7829 /* Only give this message once for a recursive call to avoid
7830 * flooding the user with errors. And stop iterating over lists
7831 * and dicts. */
7832 did_echo_string_emsg = TRUE;
7833 EMSG(_("E724: variable nested too deep for displaying"));
7834 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007835 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007836 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 }
7838 ++recurse;
7839
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007840 switch (tv->v_type)
7841 {
7842 case VAR_FUNC:
7843 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007844 r = tv->vval.v_string;
7845 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007846
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007847 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007848 if (tv->vval.v_list == NULL)
7849 {
7850 *tofree = NULL;
7851 r = NULL;
7852 }
7853 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7854 {
7855 *tofree = NULL;
7856 r = (char_u *)"[...]";
7857 }
7858 else
7859 {
7860 tv->vval.v_list->lv_copyID = copyID;
7861 *tofree = list2string(tv, copyID);
7862 r = *tofree;
7863 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007865
Bram Moolenaar8c711452005-01-14 21:53:12 +00007866 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007867 if (tv->vval.v_dict == NULL)
7868 {
7869 *tofree = NULL;
7870 r = NULL;
7871 }
7872 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7873 {
7874 *tofree = NULL;
7875 r = (char_u *)"{...}";
7876 }
7877 else
7878 {
7879 tv->vval.v_dict->dv_copyID = copyID;
7880 *tofree = dict2string(tv, copyID);
7881 r = *tofree;
7882 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007883 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007884
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007885 case VAR_STRING:
7886 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007887 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007888 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007889 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007890 *tofree = NULL;
7891 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007892 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007893
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007894 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007895#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007896 *tofree = NULL;
7897 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7898 r = numbuf;
7899 break;
7900#endif
7901
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007902 case VAR_SPECIAL:
7903 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007904 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007905 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007907
Bram Moolenaar8502c702014-06-17 12:51:16 +02007908 if (--recurse == 0)
7909 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007910 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911}
7912
7913/*
7914 * Return a string with the string representation of a variable.
7915 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7916 * "numbuf" is used for a number.
7917 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007918 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007919 */
7920 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007921tv2string(
7922 typval_T *tv,
7923 char_u **tofree,
7924 char_u *numbuf,
7925 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926{
7927 switch (tv->v_type)
7928 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007929 case VAR_FUNC:
7930 *tofree = string_quote(tv->vval.v_string, TRUE);
7931 return *tofree;
7932 case VAR_STRING:
7933 *tofree = string_quote(tv->vval.v_string, FALSE);
7934 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007935#ifdef FEAT_FLOAT
7936 case VAR_FLOAT:
7937 *tofree = NULL;
7938 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7939 return numbuf;
7940#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007941 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007944 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007945 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007946 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007947 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007948 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007950 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007951}
7952
7953/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 * Return string "str" in ' quotes, doubling ' characters.
7955 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007956 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007957 */
7958 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007959string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960{
Bram Moolenaar33570922005-01-25 22:26:29 +00007961 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007962 char_u *p, *r, *s;
7963
Bram Moolenaar33570922005-01-25 22:26:29 +00007964 len = (function ? 13 : 3);
7965 if (str != NULL)
7966 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007967 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007968 for (p = str; *p != NUL; mb_ptr_adv(p))
7969 if (*p == '\'')
7970 ++len;
7971 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007972 s = r = alloc(len);
7973 if (r != NULL)
7974 {
7975 if (function)
7976 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007978 r += 10;
7979 }
7980 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007982 if (str != NULL)
7983 for (p = str; *p != NUL; )
7984 {
7985 if (*p == '\'')
7986 *r++ = '\'';
7987 MB_COPY_CHAR(p, r);
7988 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007989 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 if (function)
7991 *r++ = ')';
7992 *r++ = NUL;
7993 }
7994 return s;
7995}
7996
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007997#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007998/*
7999 * Convert the string "text" to a floating point number.
8000 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8001 * this always uses a decimal point.
8002 * Returns the length of the text that was consumed.
8003 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008004 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008005string2float(
8006 char_u *text,
8007 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008008{
8009 char *s = (char *)text;
8010 float_T f;
8011
8012 f = strtod(s, &s);
8013 *value = f;
8014 return (int)((char_u *)s - text);
8015}
8016#endif
8017
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 * Get the value of an environment variable.
8020 * "arg" is pointing to the '$'. It is advanced to after the name.
8021 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008022 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 */
8024 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008025get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026{
8027 char_u *string = NULL;
8028 int len;
8029 int cc;
8030 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008031 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032
8033 ++*arg;
8034 name = *arg;
8035 len = get_env_len(arg);
8036 if (evaluate)
8037 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008038 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008039 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008040
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008041 cc = name[len];
8042 name[len] = NUL;
8043 /* first try vim_getenv(), fast for normal environment vars */
8044 string = vim_getenv(name, &mustfree);
8045 if (string != NULL && *string != NUL)
8046 {
8047 if (!mustfree)
8048 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008050 else
8051 {
8052 if (mustfree)
8053 vim_free(string);
8054
8055 /* next try expanding things like $VIM and ${HOME} */
8056 string = expand_env_save(name - 1);
8057 if (string != NULL && *string == '$')
8058 {
8059 vim_free(string);
8060 string = NULL;
8061 }
8062 }
8063 name[len] = cc;
8064
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008065 rettv->v_type = VAR_STRING;
8066 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 }
8068
8069 return OK;
8070}
8071
8072/*
8073 * Array with names and number of arguments of all internal functions
8074 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8075 */
8076static struct fst
8077{
8078 char *f_name; /* function name */
8079 char f_min_argc; /* minimal number of arguments */
8080 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008081 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008082 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083} functions[] =
8084{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008085#ifdef FEAT_FLOAT
8086 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008087 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008088#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008089 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008090 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008091 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"append", 2, 2, f_append},
8093 {"argc", 0, 0, f_argc},
8094 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008095 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008096 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008097#ifdef FEAT_FLOAT
8098 {"asin", 1, 1, f_asin}, /* WJMc */
8099#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008100 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008101 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008102 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008103 {"assert_false", 1, 2, f_assert_false},
8104 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008105#ifdef FEAT_FLOAT
8106 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008107 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008110 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"bufexists", 1, 1, f_bufexists},
8112 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8113 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8114 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8115 {"buflisted", 1, 1, f_buflisted},
8116 {"bufloaded", 1, 1, f_bufloaded},
8117 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008118 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"bufwinnr", 1, 1, f_bufwinnr},
8120 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008121 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008122 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008123 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008124#ifdef FEAT_FLOAT
8125 {"ceil", 1, 1, f_ceil},
8126#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008127#ifdef FEAT_CHANNEL
8128 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008129 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008130 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008131 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008132 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008133 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8134 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008135 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008136 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008137#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008138 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008139 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008141 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008143#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008144 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008145 {"complete_add", 1, 1, f_complete_add},
8146 {"complete_check", 0, 0, f_complete_check},
8147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008149 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008150#ifdef FEAT_FLOAT
8151 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008152 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008153#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008154 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008156 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008157 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008158 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008160 {"diff_filler", 1, 1, f_diff_filler},
8161 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008162 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008163 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008165 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"eventhandler", 0, 0, f_eventhandler},
8167 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008168 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008170#ifdef FEAT_FLOAT
8171 {"exp", 1, 1, f_exp},
8172#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008173 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008174 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008175 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8177 {"filereadable", 1, 1, f_filereadable},
8178 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008179 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008180 {"finddir", 1, 3, f_finddir},
8181 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008182#ifdef FEAT_FLOAT
8183 {"float2nr", 1, 1, f_float2nr},
8184 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008185 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008186#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008187 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"fnamemodify", 2, 2, f_fnamemodify},
8189 {"foldclosed", 1, 1, f_foldclosed},
8190 {"foldclosedend", 1, 1, f_foldclosedend},
8191 {"foldlevel", 1, 1, f_foldlevel},
8192 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008193 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008195 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008196 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008197 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008198 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008199 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"getchar", 0, 1, f_getchar},
8201 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008202 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {"getcmdline", 0, 0, f_getcmdline},
8204 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008205 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008206 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008207 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008208 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008209 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008210 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 {"getfsize", 1, 1, f_getfsize},
8212 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008213 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008214 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008215 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008216 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008217 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008218 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008219 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008220 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008222 {"gettabvar", 2, 3, f_gettabvar},
8223 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"getwinposx", 0, 0, f_getwinposx},
8225 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008226 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008227 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008228 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008229 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008231 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008232 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008233 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8235 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8236 {"histadd", 2, 2, f_histadd},
8237 {"histdel", 1, 2, f_histdel},
8238 {"histget", 1, 2, f_histget},
8239 {"histnr", 1, 1, f_histnr},
8240 {"hlID", 1, 1, f_hlID},
8241 {"hlexists", 1, 1, f_hlexists},
8242 {"hostname", 0, 0, f_hostname},
8243 {"iconv", 3, 3, f_iconv},
8244 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008245 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008246 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008248 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {"inputrestore", 0, 0, f_inputrestore},
8250 {"inputsave", 0, 0, f_inputsave},
8251 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008252 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008253 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008255 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008256 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008257#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008258# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008259 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008260# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008261 {"job_start", 1, 2, f_job_start},
8262 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008263 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008264#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008265 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008266 {"js_decode", 1, 1, f_js_decode},
8267 {"js_encode", 1, 1, f_js_encode},
8268 {"json_decode", 1, 1, f_json_decode},
8269 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008270 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008272 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"libcall", 3, 3, f_libcall},
8274 {"libcallnr", 3, 3, f_libcallnr},
8275 {"line", 1, 1, f_line},
8276 {"line2byte", 1, 1, f_line2byte},
8277 {"lispindent", 1, 1, f_lispindent},
8278 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008279#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008280 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008281 {"log10", 1, 1, f_log10},
8282#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008283#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008284 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008285#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008286 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008287 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008288 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008289 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008290 {"matchadd", 2, 5, f_matchadd},
8291 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008292 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008293 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008294 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008295 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008296 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008297 {"max", 1, 1, f_max},
8298 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008299#ifdef vim_mkdir
8300 {"mkdir", 1, 3, f_mkdir},
8301#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008302 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008303#ifdef FEAT_MZSCHEME
8304 {"mzeval", 1, 1, f_mzeval},
8305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008307 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008308 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008309 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008310#ifdef FEAT_PERL
8311 {"perleval", 1, 1, f_perleval},
8312#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008313#ifdef FEAT_FLOAT
8314 {"pow", 2, 2, f_pow},
8315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008317 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008318 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008319#ifdef FEAT_PYTHON3
8320 {"py3eval", 1, 1, f_py3eval},
8321#endif
8322#ifdef FEAT_PYTHON
8323 {"pyeval", 1, 1, f_pyeval},
8324#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008325 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008326 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008327 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008328 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008329 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"remote_expr", 2, 3, f_remote_expr},
8331 {"remote_foreground", 1, 1, f_remote_foreground},
8332 {"remote_peek", 1, 2, f_remote_peek},
8333 {"remote_read", 1, 1, f_remote_read},
8334 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008335 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008337 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008339 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008340#ifdef FEAT_FLOAT
8341 {"round", 1, 1, f_round},
8342#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008343 {"screenattr", 2, 2, f_screenattr},
8344 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008345 {"screencol", 0, 0, f_screencol},
8346 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008347 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008348 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008349 {"searchpair", 3, 7, f_searchpair},
8350 {"searchpairpos", 3, 7, f_searchpairpos},
8351 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"server2client", 2, 2, f_server2client},
8353 {"serverlist", 0, 0, f_serverlist},
8354 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008355 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"setcmdpos", 1, 1, f_setcmdpos},
8357 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008358 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008359 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008360 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008361 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008363 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008364 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008366#ifdef FEAT_CRYPT
8367 {"sha256", 1, 1, f_sha256},
8368#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008369 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008370 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008372#ifdef FEAT_FLOAT
8373 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008374 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008375#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008376 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008377 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008378 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008379 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008380 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008381#ifdef FEAT_FLOAT
8382 {"sqrt", 1, 1, f_sqrt},
8383 {"str2float", 1, 1, f_str2float},
8384#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008385 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008386 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008387 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388#ifdef HAVE_STRFTIME
8389 {"strftime", 1, 2, f_strftime},
8390#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008391 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008392 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"strlen", 1, 1, f_strlen},
8394 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008395 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008397 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008398 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"substitute", 4, 4, f_substitute},
8400 {"synID", 3, 3, f_synID},
8401 {"synIDattr", 2, 3, f_synIDattr},
8402 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008403 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008404 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008405 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008406 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008407 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008408 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008409 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008410 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008411 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008412#ifdef FEAT_FLOAT
8413 {"tan", 1, 1, f_tan},
8414 {"tanh", 1, 1, f_tanh},
8415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008417 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {"tolower", 1, 1, f_tolower},
8419 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008420 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008421#ifdef FEAT_FLOAT
8422 {"trunc", 1, 1, f_trunc},
8423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008425 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008426 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008427 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008428 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {"virtcol", 1, 1, f_virtcol},
8430 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008431 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {"winbufnr", 1, 1, f_winbufnr},
8433 {"wincol", 0, 0, f_wincol},
8434 {"winheight", 1, 1, f_winheight},
8435 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008436 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008438 {"winrestview", 1, 1, f_winrestview},
8439 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008441 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008442 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008443 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444};
8445
8446#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8447
8448/*
8449 * Function given to ExpandGeneric() to obtain the list of internal
8450 * or user defined function names.
8451 */
8452 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008453get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 static int intidx = -1;
8456 char_u *name;
8457
8458 if (idx == 0)
8459 intidx = -1;
8460 if (intidx < 0)
8461 {
8462 name = get_user_func_name(xp, idx);
8463 if (name != NULL)
8464 return name;
8465 }
8466 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8467 {
8468 STRCPY(IObuff, functions[intidx].f_name);
8469 STRCAT(IObuff, "(");
8470 if (functions[intidx].f_max_argc == 0)
8471 STRCAT(IObuff, ")");
8472 return IObuff;
8473 }
8474
8475 return NULL;
8476}
8477
8478/*
8479 * Function given to ExpandGeneric() to obtain the list of internal or
8480 * user defined variable or function names.
8481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008483get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 static int intidx = -1;
8486 char_u *name;
8487
8488 if (idx == 0)
8489 intidx = -1;
8490 if (intidx < 0)
8491 {
8492 name = get_function_name(xp, idx);
8493 if (name != NULL)
8494 return name;
8495 }
8496 return get_user_var_name(xp, ++intidx);
8497}
8498
8499#endif /* FEAT_CMDL_COMPL */
8500
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008501#if defined(EBCDIC) || defined(PROTO)
8502/*
8503 * Compare struct fst by function name.
8504 */
8505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008506compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008507{
8508 struct fst *p1 = (struct fst *)s1;
8509 struct fst *p2 = (struct fst *)s2;
8510
8511 return STRCMP(p1->f_name, p2->f_name);
8512}
8513
8514/*
8515 * Sort the function table by function name.
8516 * The sorting of the table above is ASCII dependant.
8517 * On machines using EBCDIC we have to sort it.
8518 */
8519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008520sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008521{
8522 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8523
8524 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8525}
8526#endif
8527
8528
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529/*
8530 * Find internal function in table above.
8531 * Return index, or -1 if not found
8532 */
8533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008534find_internal_func(
8535 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536{
8537 int first = 0;
8538 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8539 int cmp;
8540 int x;
8541
8542 /*
8543 * Find the function name in the table. Binary search.
8544 */
8545 while (first <= last)
8546 {
8547 x = first + ((unsigned)(last - first) >> 1);
8548 cmp = STRCMP(name, functions[x].f_name);
8549 if (cmp < 0)
8550 last = x - 1;
8551 else if (cmp > 0)
8552 first = x + 1;
8553 else
8554 return x;
8555 }
8556 return -1;
8557}
8558
8559/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008560 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8561 * name it contains, otherwise return "name".
8562 */
8563 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008564deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008565{
Bram Moolenaar33570922005-01-25 22:26:29 +00008566 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008567 int cc;
8568
8569 cc = name[*lenp];
8570 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008571 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008572 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008574 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008575 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008576 {
8577 *lenp = 0;
8578 return (char_u *)""; /* just in case */
8579 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008580 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008581 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008582 }
8583
8584 return name;
8585}
8586
8587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 * Allocate a variable for the result of a function.
8589 * Return OK or FAIL.
8590 */
8591 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008592get_func_tv(
8593 char_u *name, /* name of the function */
8594 int len, /* length of "name" */
8595 typval_T *rettv,
8596 char_u **arg, /* argument, pointing to the '(' */
8597 linenr_T firstline, /* first line of range */
8598 linenr_T lastline, /* last line of range */
8599 int *doesrange, /* return: function handled range */
8600 int evaluate,
8601 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
8603 char_u *argp;
8604 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008605 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 int argcount = 0; /* number of arguments found */
8607
8608 /*
8609 * Get the arguments.
8610 */
8611 argp = *arg;
8612 while (argcount < MAX_FUNC_ARGS)
8613 {
8614 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8615 if (*argp == ')' || *argp == ',' || *argp == NUL)
8616 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8618 {
8619 ret = FAIL;
8620 break;
8621 }
8622 ++argcount;
8623 if (*argp != ',')
8624 break;
8625 }
8626 if (*argp == ')')
8627 ++argp;
8628 else
8629 ret = FAIL;
8630
8631 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008635 {
8636 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008637 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008638 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008639 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641
8642 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008643 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644
8645 *arg = skipwhite(argp);
8646 return ret;
8647}
8648
8649
8650/*
8651 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008652 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008653 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008655 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008656call_func(
8657 char_u *funcname, /* name of the function */
8658 int len, /* length of "name" */
8659 typval_T *rettv, /* return value goes here */
8660 int argcount, /* number of "argvars" */
8661 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008662 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008663 linenr_T firstline, /* first line of range */
8664 linenr_T lastline, /* last line of range */
8665 int *doesrange, /* return: function handled range */
8666 int evaluate,
8667 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668{
8669 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670#define ERROR_UNKNOWN 0
8671#define ERROR_TOOMANY 1
8672#define ERROR_TOOFEW 2
8673#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008674#define ERROR_DICT 4
8675#define ERROR_NONE 5
8676#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 int error = ERROR_NONE;
8678 int i;
8679 int llen;
8680 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681#define FLEN_FIXED 40
8682 char_u fname_buf[FLEN_FIXED + 1];
8683 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008684 char_u *name;
8685
8686 /* Make a copy of the name, if it comes from a funcref variable it could
8687 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008688 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008689 if (name == NULL)
8690 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691
8692 /*
8693 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8694 * Change <SNR>123_name() to K_SNR 123_name().
8695 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 llen = eval_fname_script(name);
8698 if (llen > 0)
8699 {
8700 fname_buf[0] = K_SPECIAL;
8701 fname_buf[1] = KS_EXTRA;
8702 fname_buf[2] = (int)KE_SNR;
8703 i = 3;
8704 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8705 {
8706 if (current_SID <= 0)
8707 error = ERROR_SCRIPT;
8708 else
8709 {
8710 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8711 i = (int)STRLEN(fname_buf);
8712 }
8713 }
8714 if (i + STRLEN(name + llen) < FLEN_FIXED)
8715 {
8716 STRCPY(fname_buf + i, name + llen);
8717 fname = fname_buf;
8718 }
8719 else
8720 {
8721 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8722 if (fname == NULL)
8723 error = ERROR_OTHER;
8724 else
8725 {
8726 mch_memmove(fname, fname_buf, (size_t)i);
8727 STRCPY(fname + i, name + llen);
8728 }
8729 }
8730 }
8731 else
8732 fname = name;
8733
8734 *doesrange = FALSE;
8735
8736
8737 /* execute the function if no errors detected and executing */
8738 if (evaluate && error == ERROR_NONE)
8739 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008740 char_u *rfname = fname;
8741
8742 /* Ignore "g:" before a function name. */
8743 if (fname[0] == 'g' && fname[1] == ':')
8744 rfname = fname + 2;
8745
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008746 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8747 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 error = ERROR_UNKNOWN;
8749
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008750 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 {
8752 /*
8753 * User defined function.
8754 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008755 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008756
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008758 /* Trigger FuncUndefined event, may load the function. */
8759 if (fp == NULL
8760 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008761 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008762 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008764 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008765 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 }
8767#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008768 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008769 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008770 {
8771 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008772 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008773 }
8774
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 if (fp != NULL)
8776 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008777 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008779 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008781 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008783 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008784 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 else
8786 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008787 int did_save_redo = FALSE;
8788
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 /*
8790 * Call the user function.
8791 * Save and restore search patterns, script variables and
8792 * redo buffer.
8793 */
8794 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008795#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008796 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008797#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008798 {
8799 saveRedobuff();
8800 did_save_redo = TRUE;
8801 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008802 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008804 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008805 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8806 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8807 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008808 /* Function was unreferenced while being used, free it
8809 * now. */
8810 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008811 if (did_save_redo)
8812 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 restore_search_patterns();
8814 error = ERROR_NONE;
8815 }
8816 }
8817 }
8818 else
8819 {
8820 /*
8821 * Find the function name in the table, call its implementation.
8822 */
8823 i = find_internal_func(fname);
8824 if (i >= 0)
8825 {
8826 if (argcount < functions[i].f_min_argc)
8827 error = ERROR_TOOFEW;
8828 else if (argcount > functions[i].f_max_argc)
8829 error = ERROR_TOOMANY;
8830 else
8831 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008832 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 error = ERROR_NONE;
8835 }
8836 }
8837 }
8838 /*
8839 * The function call (or "FuncUndefined" autocommand sequence) might
8840 * have been aborted by an error, an interrupt, or an explicitly thrown
8841 * exception that has not been caught so far. This situation can be
8842 * tested for by calling aborting(). For an error in an internal
8843 * function or for the "E132" error in call_user_func(), however, the
8844 * throw point at which the "force_abort" flag (temporarily reset by
8845 * emsg()) is normally updated has not been reached yet. We need to
8846 * update that flag first to make aborting() reliable.
8847 */
8848 update_force_abort();
8849 }
8850 if (error == ERROR_NONE)
8851 ret = OK;
8852
8853 /*
8854 * Report an error unless the argument evaluation or function call has been
8855 * cancelled due to an aborting error, an interrupt, or an exception.
8856 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008857 if (!aborting())
8858 {
8859 switch (error)
8860 {
8861 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008862 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008863 break;
8864 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008865 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008866 break;
8867 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008868 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008869 name);
8870 break;
8871 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008872 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008873 name);
8874 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008875 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008876 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008877 name);
8878 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008879 }
8880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 if (fname != name && fname != fname_buf)
8883 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008884 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885
8886 return ret;
8887}
8888
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008889/*
8890 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008891 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008892 */
8893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008894emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008895{
8896 char_u *p;
8897
8898 if (*name == K_SPECIAL)
8899 p = concat_str((char_u *)"<SNR>", name + 3);
8900 else
8901 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008902 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008903 if (p != name)
8904 vim_free(p);
8905}
8906
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008907/*
8908 * Return TRUE for a non-zero Number and a non-empty String.
8909 */
8910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008911non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008912{
8913 return ((argvars[0].v_type == VAR_NUMBER
8914 && argvars[0].vval.v_number != 0)
8915 || (argvars[0].v_type == VAR_STRING
8916 && argvars[0].vval.v_string != NULL
8917 && *argvars[0].vval.v_string != NUL));
8918}
8919
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920/*********************************************
8921 * Implementation of the built-in functions
8922 */
8923
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008924#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008925static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008926
8927/*
8928 * Get the float value of "argvars[0]" into "f".
8929 * Returns FAIL when the argument is not a Number or Float.
8930 */
8931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008932get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008933{
8934 if (argvars[0].v_type == VAR_FLOAT)
8935 {
8936 *f = argvars[0].vval.v_float;
8937 return OK;
8938 }
8939 if (argvars[0].v_type == VAR_NUMBER)
8940 {
8941 *f = (float_T)argvars[0].vval.v_number;
8942 return OK;
8943 }
8944 EMSG(_("E808: Number or Float required"));
8945 return FAIL;
8946}
8947
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008948/*
8949 * "abs(expr)" function
8950 */
8951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008952f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008953{
8954 if (argvars[0].v_type == VAR_FLOAT)
8955 {
8956 rettv->v_type = VAR_FLOAT;
8957 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8958 }
8959 else
8960 {
8961 varnumber_T n;
8962 int error = FALSE;
8963
8964 n = get_tv_number_chk(&argvars[0], &error);
8965 if (error)
8966 rettv->vval.v_number = -1;
8967 else if (n > 0)
8968 rettv->vval.v_number = n;
8969 else
8970 rettv->vval.v_number = -n;
8971 }
8972}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008973
8974/*
8975 * "acos()" function
8976 */
8977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008978f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008979{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01008980 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008981
8982 rettv->v_type = VAR_FLOAT;
8983 if (get_float_arg(argvars, &f) == OK)
8984 rettv->vval.v_float = acos(f);
8985 else
8986 rettv->vval.v_float = 0.0;
8987}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008988#endif
8989
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008991 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 */
8993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008994f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995{
Bram Moolenaar33570922005-01-25 22:26:29 +00008996 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008999 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009001 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009002 && !tv_check_lock(l->lv_lock,
9003 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009004 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009006 }
9007 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009008 EMSG(_(e_listreq));
9009}
9010
9011/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009012 * "alloc_fail(id, countdown, repeat)" function
9013 */
9014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009015f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009016{
9017 if (argvars[0].v_type != VAR_NUMBER
9018 || argvars[0].vval.v_number <= 0
9019 || argvars[1].v_type != VAR_NUMBER
9020 || argvars[1].vval.v_number < 0
9021 || argvars[2].v_type != VAR_NUMBER)
9022 EMSG(_(e_invarg));
9023 else
9024 {
9025 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009026 if (alloc_fail_id >= aid_last)
9027 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009028 alloc_fail_countdown = argvars[1].vval.v_number;
9029 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009030 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009031 }
9032}
9033
9034/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009035 * "and(expr, expr)" function
9036 */
9037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009038f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009039{
9040 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9041 & get_tv_number_chk(&argvars[1], NULL);
9042}
9043
9044/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009045 * "append(lnum, string/list)" function
9046 */
9047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009048f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009049{
9050 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009052 list_T *l = NULL;
9053 listitem_T *li = NULL;
9054 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009055 long added = 0;
9056
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009057 /* When coming here from Insert mode, sync undo, so that this can be
9058 * undone separately from what was previously inserted. */
9059 if (u_sync_once == 2)
9060 {
9061 u_sync_once = 1; /* notify that u_sync() was called */
9062 u_sync(TRUE);
9063 }
9064
Bram Moolenaar0d660222005-01-07 21:51:51 +00009065 lnum = get_tv_lnum(argvars);
9066 if (lnum >= 0
9067 && lnum <= curbuf->b_ml.ml_line_count
9068 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009069 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009070 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009071 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009072 l = argvars[1].vval.v_list;
9073 if (l == NULL)
9074 return;
9075 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009076 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009077 for (;;)
9078 {
9079 if (l == NULL)
9080 tv = &argvars[1]; /* append a string */
9081 else if (li == NULL)
9082 break; /* end of list */
9083 else
9084 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009085 line = get_tv_string_chk(tv);
9086 if (line == NULL) /* type error */
9087 {
9088 rettv->vval.v_number = 1; /* Failed */
9089 break;
9090 }
9091 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009092 ++added;
9093 if (l == NULL)
9094 break;
9095 li = li->li_next;
9096 }
9097
9098 appended_lines_mark(lnum, added);
9099 if (curwin->w_cursor.lnum > lnum)
9100 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009102 else
9103 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104}
9105
9106/*
9107 * "argc()" function
9108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009110f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113}
9114
9115/*
9116 * "argidx()" function
9117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009119f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122}
9123
9124/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009125 * "arglistid()" function
9126 */
9127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009128f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009129{
9130 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009131
9132 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009133 wp = find_tabwin(&argvars[0], &argvars[1]);
9134 if (wp != NULL)
9135 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009136}
9137
9138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 * "argv(nr)" function
9140 */
9141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009142f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143{
9144 int idx;
9145
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009146 if (argvars[0].v_type != VAR_UNKNOWN)
9147 {
9148 idx = get_tv_number_chk(&argvars[0], NULL);
9149 if (idx >= 0 && idx < ARGCOUNT)
9150 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9151 else
9152 rettv->vval.v_string = NULL;
9153 rettv->v_type = VAR_STRING;
9154 }
9155 else if (rettv_list_alloc(rettv) == OK)
9156 for (idx = 0; idx < ARGCOUNT; ++idx)
9157 list_append_string(rettv->vval.v_list,
9158 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159}
9160
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009161static void prepare_assert_error(garray_T*gap);
9162static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9163static void assert_error(garray_T *gap);
9164static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009165
9166/*
9167 * Prepare "gap" for an assert error and add the sourcing position.
9168 */
9169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009170prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009171{
9172 char buf[NUMBUFLEN];
9173
9174 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009175 if (sourcing_name != NULL)
9176 {
9177 ga_concat(gap, sourcing_name);
9178 if (sourcing_lnum > 0)
9179 ga_concat(gap, (char_u *)" ");
9180 }
9181 if (sourcing_lnum > 0)
9182 {
9183 sprintf(buf, "line %ld", (long)sourcing_lnum);
9184 ga_concat(gap, (char_u *)buf);
9185 }
9186 if (sourcing_name != NULL || sourcing_lnum > 0)
9187 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009188}
9189
9190/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009191 * Append "str" to "gap", escaping unprintable characters.
9192 * Changes NL to \n, CR to \r, etc.
9193 */
9194 static void
9195ga_concat_esc(garray_T *gap, char_u *str)
9196{
9197 char_u *p;
9198 char_u buf[NUMBUFLEN];
9199
9200 for (p = str; *p != NUL; ++p)
9201 switch (*p)
9202 {
9203 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9204 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9205 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9206 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9207 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9208 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9209 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9210 default:
9211 if (*p < ' ')
9212 {
9213 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9214 ga_concat(gap, buf);
9215 }
9216 else
9217 ga_append(gap, *p);
9218 break;
9219 }
9220}
9221
9222/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009223 * Fill "gap" with information about an assert error.
9224 */
9225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009226fill_assert_error(
9227 garray_T *gap,
9228 typval_T *opt_msg_tv,
9229 char_u *exp_str,
9230 typval_T *exp_tv,
9231 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009232{
9233 char_u numbuf[NUMBUFLEN];
9234 char_u *tofree;
9235
9236 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9237 {
9238 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9239 vim_free(tofree);
9240 }
9241 else
9242 {
9243 ga_concat(gap, (char_u *)"Expected ");
9244 if (exp_str == NULL)
9245 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009246 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009247 vim_free(tofree);
9248 }
9249 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009250 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009251 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009252 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009253 vim_free(tofree);
9254 }
9255}
Bram Moolenaar43345542015-11-29 17:35:35 +01009256
9257/*
9258 * Add an assert error to v:errors.
9259 */
9260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009261assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009262{
9263 struct vimvar *vp = &vimvars[VV_ERRORS];
9264
9265 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9266 /* Make sure v:errors is a list. */
9267 set_vim_var_list(VV_ERRORS, list_alloc());
9268 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9269}
9270
Bram Moolenaar43345542015-11-29 17:35:35 +01009271/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009272 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009273 */
9274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009275f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009276{
9277 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009278
9279 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9280 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009281 prepare_assert_error(&ga);
9282 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9283 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009284 ga_clear(&ga);
9285 }
9286}
9287
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009288/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009289 * "assert_exception(string[, msg])" function
9290 */
9291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009292f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009293{
9294 garray_T ga;
9295 char *error;
9296
9297 error = (char *)get_tv_string_chk(&argvars[0]);
9298 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9299 {
9300 prepare_assert_error(&ga);
9301 ga_concat(&ga, (char_u *)"v:exception is not set");
9302 assert_error(&ga);
9303 ga_clear(&ga);
9304 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009305 else if (error != NULL
9306 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009307 {
9308 prepare_assert_error(&ga);
9309 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9310 &vimvars[VV_EXCEPTION].vv_tv);
9311 assert_error(&ga);
9312 ga_clear(&ga);
9313 }
9314}
9315
9316/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009317 * "assert_fails(cmd [, error])" function
9318 */
9319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009320f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009321{
9322 char_u *cmd = get_tv_string_chk(&argvars[0]);
9323 garray_T ga;
9324
9325 called_emsg = FALSE;
9326 suppress_errthrow = TRUE;
9327 emsg_silent = TRUE;
9328 do_cmdline_cmd(cmd);
9329 if (!called_emsg)
9330 {
9331 prepare_assert_error(&ga);
9332 ga_concat(&ga, (char_u *)"command did not fail: ");
9333 ga_concat(&ga, cmd);
9334 assert_error(&ga);
9335 ga_clear(&ga);
9336 }
9337 else if (argvars[1].v_type != VAR_UNKNOWN)
9338 {
9339 char_u buf[NUMBUFLEN];
9340 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9341
9342 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9343 {
9344 prepare_assert_error(&ga);
9345 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9346 &vimvars[VV_ERRMSG].vv_tv);
9347 assert_error(&ga);
9348 ga_clear(&ga);
9349 }
9350 }
9351
9352 called_emsg = FALSE;
9353 suppress_errthrow = FALSE;
9354 emsg_silent = FALSE;
9355 emsg_on_display = FALSE;
9356 set_vim_var_string(VV_ERRMSG, NULL, 0);
9357}
9358
9359/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009360 * Common for assert_true() and assert_false().
9361 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009363assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009364{
9365 int error = FALSE;
9366 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009367
Bram Moolenaar37127922016-02-06 20:29:28 +01009368 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009369 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009370 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009371 if (argvars[0].v_type != VAR_NUMBER
9372 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9373 || error)
9374 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009375 prepare_assert_error(&ga);
9376 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009377 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009378 NULL, &argvars[0]);
9379 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009380 ga_clear(&ga);
9381 }
9382}
9383
9384/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009385 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009386 */
9387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009388f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009389{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009390 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009391}
9392
9393/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009394 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009395 */
9396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009397f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009398{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009399 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009400}
9401
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009402#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009403/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009404 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009405 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009407f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009408{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009409 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009410
9411 rettv->v_type = VAR_FLOAT;
9412 if (get_float_arg(argvars, &f) == OK)
9413 rettv->vval.v_float = asin(f);
9414 else
9415 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009416}
9417
9418/*
9419 * "atan()" function
9420 */
9421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009422f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009423{
9424 float_T f;
9425
9426 rettv->v_type = VAR_FLOAT;
9427 if (get_float_arg(argvars, &f) == OK)
9428 rettv->vval.v_float = atan(f);
9429 else
9430 rettv->vval.v_float = 0.0;
9431}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009432
9433/*
9434 * "atan2()" function
9435 */
9436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009437f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009438{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009439 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009440
9441 rettv->v_type = VAR_FLOAT;
9442 if (get_float_arg(argvars, &fx) == OK
9443 && get_float_arg(&argvars[1], &fy) == OK)
9444 rettv->vval.v_float = atan2(fx, fy);
9445 else
9446 rettv->vval.v_float = 0.0;
9447}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009448#endif
9449
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450/*
9451 * "browse(save, title, initdir, default)" function
9452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009454f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455{
9456#ifdef FEAT_BROWSE
9457 int save;
9458 char_u *title;
9459 char_u *initdir;
9460 char_u *defname;
9461 char_u buf[NUMBUFLEN];
9462 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009463 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009465 save = get_tv_number_chk(&argvars[0], &error);
9466 title = get_tv_string_chk(&argvars[1]);
9467 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9468 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 if (error || title == NULL || initdir == NULL || defname == NULL)
9471 rettv->vval.v_string = NULL;
9472 else
9473 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009474 do_browse(save ? BROWSE_SAVE : 0,
9475 title, defname, NULL, initdir, NULL, curbuf);
9476#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009478#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009480}
9481
9482/*
9483 * "browsedir(title, initdir)" function
9484 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009486f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009487{
9488#ifdef FEAT_BROWSE
9489 char_u *title;
9490 char_u *initdir;
9491 char_u buf[NUMBUFLEN];
9492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009493 title = get_tv_string_chk(&argvars[0]);
9494 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009496 if (title == NULL || initdir == NULL)
9497 rettv->vval.v_string = NULL;
9498 else
9499 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009500 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505}
9506
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009507static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009508
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509/*
9510 * Find a buffer by number or exact name.
9511 */
9512 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009513find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
9515 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009517 if (avar->v_type == VAR_NUMBER)
9518 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009519 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009521 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009522 if (buf == NULL)
9523 {
9524 /* No full path name match, try a match with a URL or a "nofile"
9525 * buffer, these don't use the full path. */
9526 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9527 if (buf->b_fname != NULL
9528 && (path_with_url(buf->b_fname)
9529#ifdef FEAT_QUICKFIX
9530 || bt_nofile(buf)
9531#endif
9532 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009533 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009534 break;
9535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 }
9537 return buf;
9538}
9539
9540/*
9541 * "bufexists(expr)" function
9542 */
9543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009544f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009546 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547}
9548
9549/*
9550 * "buflisted(expr)" function
9551 */
9552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009553f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554{
9555 buf_T *buf;
9556
9557 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559}
9560
9561/*
9562 * "bufloaded(expr)" function
9563 */
9564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009565f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566{
9567 buf_T *buf;
9568
9569 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571}
9572
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009573static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009574
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575/*
9576 * Get buffer by number or pattern.
9577 */
9578 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009579get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 int save_magic;
9583 char_u *save_cpo;
9584 buf_T *buf;
9585
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 if (tv->v_type == VAR_NUMBER)
9587 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009588 if (tv->v_type != VAR_STRING)
9589 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 if (name == NULL || *name == NUL)
9591 return curbuf;
9592 if (name[0] == '$' && name[1] == NUL)
9593 return lastbuf;
9594
9595 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9596 save_magic = p_magic;
9597 p_magic = TRUE;
9598 save_cpo = p_cpo;
9599 p_cpo = (char_u *)"";
9600
9601 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009602 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603
9604 p_magic = save_magic;
9605 p_cpo = save_cpo;
9606
9607 /* If not found, try expanding the name, like done for bufexists(). */
9608 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610
9611 return buf;
9612}
9613
9614/*
9615 * "bufname(expr)" function
9616 */
9617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009618f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619{
9620 buf_T *buf;
9621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009624 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 --emsg_off;
9631}
9632
9633/*
9634 * "bufnr(expr)" function
9635 */
9636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009637f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638{
9639 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009640 int error = FALSE;
9641 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009645 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009646 --emsg_off;
9647
9648 /* If the buffer isn't found and the second argument is not zero create a
9649 * new buffer. */
9650 if (buf == NULL
9651 && argvars[1].v_type != VAR_UNKNOWN
9652 && get_tv_number_chk(&argvars[1], &error) != 0
9653 && !error
9654 && (name = get_tv_string_chk(&argvars[0])) != NULL
9655 && !error)
9656 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9657
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662}
9663
9664/*
9665 * "bufwinnr(nr)" function
9666 */
9667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009668f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
9670#ifdef FEAT_WINDOWS
9671 win_T *wp;
9672 int winnr = 0;
9673#endif
9674 buf_T *buf;
9675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009676 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009678 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679#ifdef FEAT_WINDOWS
9680 for (wp = firstwin; wp; wp = wp->w_next)
9681 {
9682 ++winnr;
9683 if (wp->w_buffer == buf)
9684 break;
9685 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689#endif
9690 --emsg_off;
9691}
9692
9693/*
9694 * "byte2line(byte)" function
9695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009697f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
9699#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701#else
9702 long boff = 0;
9703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009704 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 (linenr_T)0, &boff);
9710#endif
9711}
9712
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009714byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009715{
9716#ifdef FEAT_MBYTE
9717 char_u *t;
9718#endif
9719 char_u *str;
9720 long idx;
9721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009722 str = get_tv_string_chk(&argvars[0]);
9723 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009725 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009726 return;
9727
9728#ifdef FEAT_MBYTE
9729 t = str;
9730 for ( ; idx > 0; idx--)
9731 {
9732 if (*t == NUL) /* EOL reached */
9733 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009734 if (enc_utf8 && comp)
9735 t += utf_ptr2len(t);
9736 else
9737 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009738 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009739 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009740#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009741 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009743#endif
9744}
9745
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009746/*
9747 * "byteidx()" function
9748 */
9749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009750f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009751{
9752 byteidx(argvars, rettv, FALSE);
9753}
9754
9755/*
9756 * "byteidxcomp()" function
9757 */
9758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009759f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009760{
9761 byteidx(argvars, rettv, TRUE);
9762}
9763
Bram Moolenaardb913952012-06-29 12:54:53 +02009764 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009765func_call(
9766 char_u *name,
9767 typval_T *args,
9768 dict_T *selfdict,
9769 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009770{
9771 listitem_T *item;
9772 typval_T argv[MAX_FUNC_ARGS + 1];
9773 int argc = 0;
9774 int dummy;
9775 int r = 0;
9776
9777 for (item = args->vval.v_list->lv_first; item != NULL;
9778 item = item->li_next)
9779 {
9780 if (argc == MAX_FUNC_ARGS)
9781 {
9782 EMSG(_("E699: Too many arguments"));
9783 break;
9784 }
9785 /* Make a copy of each argument. This is needed to be able to set
9786 * v_lock to VAR_FIXED in the copy without changing the original list.
9787 */
9788 copy_tv(&item->li_tv, &argv[argc++]);
9789 }
9790
9791 if (item == NULL)
9792 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9793 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9794 &dummy, TRUE, selfdict);
9795
9796 /* Free the arguments. */
9797 while (argc > 0)
9798 clear_tv(&argv[--argc]);
9799
9800 return r;
9801}
9802
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009803/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009804 * "call(func, arglist)" function
9805 */
9806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009807f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009808{
9809 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009810 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009811
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009812 if (argvars[1].v_type != VAR_LIST)
9813 {
9814 EMSG(_(e_listreq));
9815 return;
9816 }
9817 if (argvars[1].vval.v_list == NULL)
9818 return;
9819
9820 if (argvars[0].v_type == VAR_FUNC)
9821 func = argvars[0].vval.v_string;
9822 else
9823 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009824 if (*func == NUL)
9825 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009826
Bram Moolenaare9a41262005-01-15 22:18:47 +00009827 if (argvars[2].v_type != VAR_UNKNOWN)
9828 {
9829 if (argvars[2].v_type != VAR_DICT)
9830 {
9831 EMSG(_(e_dictreq));
9832 return;
9833 }
9834 selfdict = argvars[2].vval.v_dict;
9835 }
9836
Bram Moolenaardb913952012-06-29 12:54:53 +02009837 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009838}
9839
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009840#ifdef FEAT_FLOAT
9841/*
9842 * "ceil({float})" function
9843 */
9844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009845f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009846{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009847 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009848
9849 rettv->v_type = VAR_FLOAT;
9850 if (get_float_arg(argvars, &f) == OK)
9851 rettv->vval.v_float = ceil(f);
9852 else
9853 rettv->vval.v_float = 0.0;
9854}
9855#endif
9856
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009857#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009858/*
9859 * Get a callback from "arg". It can be a Funcref or a function name.
9860 * When "arg" is zero return an empty string.
9861 * Return NULL for an invalid argument.
9862 */
9863 static char_u *
9864get_callback(typval_T *arg)
9865{
9866 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9867 return arg->vval.v_string;
9868 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9869 return (char_u *)"";
9870 EMSG(_("E999: Invalid callback argument"));
9871 return NULL;
9872}
9873
9874/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009875 * Get the option entries from the dict in "tv", parse them and put the result
9876 * in "opt".
9877 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009878 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009879 */
9880 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009881get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009882{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009883 typval_T *item;
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009884 char_u *mode;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009885 dict_T *dict;
9886 int todo;
9887 hashitem_T *hi;
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009888
Bram Moolenaar8600ace2016-02-19 23:31:40 +01009889 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009890 if (tv->v_type == VAR_UNKNOWN)
9891 return OK;
9892 if (tv->v_type != VAR_DICT)
9893 {
9894 EMSG(_(e_invarg));
9895 return FAIL;
9896 }
9897 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009898 if (dict == NULL)
9899 return OK;
9900
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009901 todo = (int)dict->dv_hashtab.ht_used;
9902 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
9903 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009904 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009905 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009906
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009907 if (STRCMP(hi->hi_key, "mode") == 0)
9908 {
9909 if (!(supported & JO_MODE))
9910 break;
9911 opt->jo_set |= JO_MODE;
9912 mode = get_tv_string(item);
9913 if (STRCMP(mode, "nl") == 0)
9914 opt->jo_mode = MODE_NL;
9915 else if (STRCMP(mode, "raw") == 0)
9916 opt->jo_mode = MODE_RAW;
9917 else if (STRCMP(mode, "js") == 0)
9918 opt->jo_mode = MODE_JS;
9919 else if (STRCMP(mode, "json") == 0)
9920 opt->jo_mode = MODE_JSON;
9921 else
9922 {
9923 EMSG2(_(e_invarg2), mode);
9924 return FAIL;
9925 }
9926 }
9927 else if (STRCMP(hi->hi_key, "callback") == 0)
9928 {
9929 if (!(supported & JO_CALLBACK))
9930 break;
9931 opt->jo_set |= JO_CALLBACK;
9932 opt->jo_callback = get_callback(item);
9933 if (opt->jo_callback == NULL)
9934 {
9935 EMSG2(_(e_invarg2), "callback");
9936 return FAIL;
9937 }
9938 }
9939 else if (STRCMP(hi->hi_key, "waittime") == 0)
9940 {
9941 if (!(supported & JO_WAITTIME))
9942 break;
9943 opt->jo_set |= JO_WAITTIME;
9944 opt->jo_waittime = get_tv_number(item);
9945 }
9946 else if (STRCMP(hi->hi_key, "timeout") == 0)
9947 {
9948 if (!(supported & JO_TIMEOUT))
9949 break;
9950 opt->jo_set |= JO_TIMEOUT;
9951 opt->jo_timeout = get_tv_number(item);
9952 }
9953 else
9954 break;
9955 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009956 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009957 if (todo > 0)
9958 {
9959 EMSG2(_(e_invarg2), hi->hi_key);
9960 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009961 }
9962
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009963 return OK;
9964}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009965#endif
9966
9967#ifdef FEAT_CHANNEL
9968/*
9969 * Get the channel from the argument.
9970 * Returns NULL if the handle is invalid.
9971 */
9972 static channel_T *
9973get_channel_arg(typval_T *tv)
9974{
9975 channel_T *channel;
9976
9977 if (tv->v_type != VAR_CHANNEL)
9978 {
9979 EMSG2(_(e_invarg2), get_tv_string(tv));
9980 return NULL;
9981 }
9982 channel = tv->vval.v_channel;
9983
9984 if (channel == NULL || !channel_is_open(channel))
9985 {
9986 EMSG(_("E906: not an open channel"));
9987 return NULL;
9988 }
9989 return channel;
9990}
9991
9992/*
9993 * "ch_close()" function
9994 */
9995 static void
9996f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9997{
9998 channel_T *channel = get_channel_arg(&argvars[0]);
9999
10000 if (channel != NULL)
10001 channel_close(channel);
10002}
10003
10004/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010005 * "ch_log()" function
10006 */
10007 static void
10008f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10009{
10010 char_u *msg = get_tv_string(&argvars[0]);
10011 channel_T *channel = NULL;
10012
10013 if (argvars[1].v_type != VAR_UNKNOWN)
10014 channel = get_channel_arg(&argvars[1]);
10015
10016 ch_log(channel, (char *)msg);
10017}
10018
10019/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010020 * "ch_logfile()" function
10021 */
10022 static void
10023f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10024{
10025 char_u *fname;
10026 char_u *opt = (char_u *)"";
10027 char_u buf[NUMBUFLEN];
10028 FILE *file = NULL;
10029
10030 fname = get_tv_string(&argvars[0]);
10031 if (argvars[1].v_type == VAR_STRING)
10032 opt = get_tv_string_buf(&argvars[1], buf);
10033 if (*fname != NUL)
10034 {
10035 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10036 if (file == NULL)
10037 {
10038 EMSG2(_(e_notopen), fname);
10039 return;
10040 }
10041 }
10042 ch_logfile(file);
10043}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010044
10045/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010046 * "ch_open()" function
10047 */
10048 static void
10049f_ch_open(typval_T *argvars, typval_T *rettv)
10050{
10051 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010052 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010053 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010054 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010055 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010056 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010057
10058 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010059 rettv->v_type = VAR_CHANNEL;
10060 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010061
10062 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010063 if (argvars[1].v_type != VAR_UNKNOWN
10064 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010065 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010066 EMSG(_(e_invarg));
10067 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010068 }
10069
10070 /* parse address */
10071 p = vim_strchr(address, ':');
10072 if (p == NULL)
10073 {
10074 EMSG2(_(e_invarg2), address);
10075 return;
10076 }
10077 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010078 port = strtol((char *)p, &rest, 10);
10079 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010080 {
10081 p[-1] = ':';
10082 EMSG2(_(e_invarg2), address);
10083 return;
10084 }
10085
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010086 /* parse options */
10087 opt.jo_mode = MODE_JSON;
10088 opt.jo_callback = NULL;
10089 opt.jo_waittime = 0;
10090 opt.jo_timeout = 2000;
10091 if (get_job_options(&argvars[1], &opt,
10092 JO_MODE + JO_CALLBACK + JO_WAITTIME + JO_TIMEOUT) == FAIL)
10093 return;
10094 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010095 {
10096 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010097 return;
10098 }
10099
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010100 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010101 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010102 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010103 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010104 opt.jo_set = JO_ALL;
10105 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010106 }
10107}
10108
10109/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010110 * "ch_readraw()" function
10111 */
10112 static void
10113f_ch_readraw(typval_T *argvars, typval_T *rettv)
10114{
Bram Moolenaar77073442016-02-13 23:23:53 +010010115 channel_T *channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010116
10117 /* return an empty string by default */
10118 rettv->v_type = VAR_STRING;
10119 rettv->vval.v_string = NULL;
10120
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010121 /* TODO: use timeout from the options */
10122
Bram Moolenaar77073442016-02-13 23:23:53 +010010123 channel = get_channel_arg(&argvars[0]);
10124 if (channel != NULL)
10125 rettv->vval.v_string = channel_read_block(channel);
10126}
10127
10128/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010129 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010130 * Returns the channel if the caller should read the response.
10131 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010132 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010133 static channel_T *
Bram Moolenaara07fec92016-02-05 21:04:08 +010010134send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010135{
Bram Moolenaar77073442016-02-13 23:23:53 +010010136 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010137 jobopt_T opt;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010138
Bram Moolenaar77073442016-02-13 23:23:53 +010010139 channel = get_channel_arg(&argvars[0]);
10140 if (channel == NULL)
10141 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010142
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010143 opt.jo_callback = NULL;
10144 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10145 return NULL;
10146
Bram Moolenaara07fec92016-02-05 21:04:08 +010010147 /* Set the callback. An empty callback means no callback and not reading
10148 * the response. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010149 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
10150 channel_set_req_callback(channel, opt.jo_callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010151
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010152 if (channel_send(channel, text, fun) == OK && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010153 return channel;
10154 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010155}
10156
10157/*
10158 * "ch_sendexpr()" function
10159 */
10160 static void
10161f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10162{
10163 char_u *text;
10164 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010165 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010166 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010167 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010168
10169 /* return an empty string by default */
10170 rettv->v_type = VAR_STRING;
10171 rettv->vval.v_string = NULL;
10172
Bram Moolenaar77073442016-02-13 23:23:53 +010010173 channel = get_channel_arg(&argvars[0]);
10174 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010175 return;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010176
Bram Moolenaar77073442016-02-13 23:23:53 +010010177 ch_mode = channel_get_mode(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010178 if (ch_mode == MODE_RAW)
10179 {
10180 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
10181 return;
10182 }
10183
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010184 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010185 text = json_encode_nr_expr(id, &argvars[1],
10186 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010187 if (text == NULL)
10188 return;
10189
Bram Moolenaar77073442016-02-13 23:23:53 +010010190 channel = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010191 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010192 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010193 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010194 if (channel_read_json_block(channel, id, &listtv) == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010195 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010196 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010197
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010198 /* Move the item from the list and then change the type to
10199 * avoid the value being freed. */
10200 *rettv = list->lv_last->li_tv;
10201 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010202 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010203 }
10204 }
10205}
10206
10207/*
10208 * "ch_sendraw()" function
10209 */
10210 static void
10211f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10212{
10213 char_u buf[NUMBUFLEN];
10214 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010215 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010216
10217 /* return an empty string by default */
10218 rettv->v_type = VAR_STRING;
10219 rettv->vval.v_string = NULL;
10220
10221 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar77073442016-02-13 23:23:53 +010010222 channel = send_common(argvars, text, 0, "sendraw");
10223 if (channel != NULL)
10224 rettv->vval.v_string = channel_read_block(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010225}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010226
10227/*
10228 * "ch_setoptions()" function
10229 */
10230 static void
10231f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10232{
10233 channel_T *channel;
10234 jobopt_T opt;
10235
10236 channel = get_channel_arg(&argvars[0]);
10237 if (channel == NULL)
10238 return;
10239 if (get_job_options(&argvars[1], &opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010240 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010241 channel_set_options(channel, &opt);
10242}
10243
10244/*
10245 * "ch_status()" function
10246 */
10247 static void
10248f_ch_status(typval_T *argvars, typval_T *rettv)
10249{
10250 /* return an empty string by default */
10251 rettv->v_type = VAR_STRING;
10252
10253 if (argvars[0].v_type != VAR_CHANNEL)
10254 {
10255 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10256 rettv->vval.v_string = NULL;
10257 }
10258 else
10259 rettv->vval.v_string = vim_strsave(
10260 (char_u *)channel_status(argvars[0].vval.v_channel));
10261}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010262#endif
10263
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010264/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010265 * "changenr()" function
10266 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010268f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010269{
10270 rettv->vval.v_number = curbuf->b_u_seq_cur;
10271}
10272
10273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 * "char2nr(string)" function
10275 */
10276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010277f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278{
10279#ifdef FEAT_MBYTE
10280 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010281 {
10282 int utf8 = 0;
10283
10284 if (argvars[1].v_type != VAR_UNKNOWN)
10285 utf8 = get_tv_number_chk(&argvars[1], NULL);
10286
10287 if (utf8)
10288 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10289 else
10290 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 else
10293#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010294 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295}
10296
10297/*
10298 * "cindent(lnum)" function
10299 */
10300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010301f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302{
10303#ifdef FEAT_CINDENT
10304 pos_T pos;
10305 linenr_T lnum;
10306
10307 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010308 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10310 {
10311 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010312 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 curwin->w_cursor = pos;
10314 }
10315 else
10316#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010317 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318}
10319
10320/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010321 * "clearmatches()" function
10322 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010324f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010325{
10326#ifdef FEAT_SEARCH_EXTRA
10327 clear_matches(curwin);
10328#endif
10329}
10330
10331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 * "col(string)" function
10333 */
10334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010335f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336{
10337 colnr_T col = 0;
10338 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010339 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010341 fp = var2fpos(&argvars[0], FALSE, &fnum);
10342 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 {
10344 if (fp->col == MAXCOL)
10345 {
10346 /* '> can be MAXCOL, get the length of the line then */
10347 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010348 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 else
10350 col = MAXCOL;
10351 }
10352 else
10353 {
10354 col = fp->col + 1;
10355#ifdef FEAT_VIRTUALEDIT
10356 /* col(".") when the cursor is on the NUL at the end of the line
10357 * because of "coladd" can be seen as an extra column. */
10358 if (virtual_active() && fp == &curwin->w_cursor)
10359 {
10360 char_u *p = ml_get_cursor();
10361
10362 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10363 curwin->w_virtcol - curwin->w_cursor.coladd))
10364 {
10365# ifdef FEAT_MBYTE
10366 int l;
10367
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010368 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 col += l;
10370# else
10371 if (*p != NUL && p[1] == NUL)
10372 ++col;
10373# endif
10374 }
10375 }
10376#endif
10377 }
10378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010379 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380}
10381
Bram Moolenaar572cb562005-08-05 21:35:02 +000010382#if defined(FEAT_INS_EXPAND)
10383/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010384 * "complete()" function
10385 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010387f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010388{
10389 int startcol;
10390
10391 if ((State & INSERT) == 0)
10392 {
10393 EMSG(_("E785: complete() can only be used in Insert mode"));
10394 return;
10395 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010396
10397 /* Check for undo allowed here, because if something was already inserted
10398 * the line was already saved for undo and this check isn't done. */
10399 if (!undo_allowed())
10400 return;
10401
Bram Moolenaarade00832006-03-10 21:46:58 +000010402 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10403 {
10404 EMSG(_(e_invarg));
10405 return;
10406 }
10407
10408 startcol = get_tv_number_chk(&argvars[0], NULL);
10409 if (startcol <= 0)
10410 return;
10411
10412 set_completion(startcol - 1, argvars[1].vval.v_list);
10413}
10414
10415/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010416 * "complete_add()" function
10417 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010419f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010420{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010421 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010422}
10423
10424/*
10425 * "complete_check()" function
10426 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010428f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010429{
10430 int saved = RedrawingDisabled;
10431
10432 RedrawingDisabled = 0;
10433 ins_compl_check_keys(0);
10434 rettv->vval.v_number = compl_interrupted;
10435 RedrawingDisabled = saved;
10436}
10437#endif
10438
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439/*
10440 * "confirm(message, buttons[, default [, type]])" function
10441 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010443f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444{
10445#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10446 char_u *message;
10447 char_u *buttons = NULL;
10448 char_u buf[NUMBUFLEN];
10449 char_u buf2[NUMBUFLEN];
10450 int def = 1;
10451 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010452 char_u *typestr;
10453 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010455 message = get_tv_string_chk(&argvars[0]);
10456 if (message == NULL)
10457 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010458 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010460 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10461 if (buttons == NULL)
10462 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010463 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010465 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010466 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010468 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10469 if (typestr == NULL)
10470 error = TRUE;
10471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473 switch (TOUPPER_ASC(*typestr))
10474 {
10475 case 'E': type = VIM_ERROR; break;
10476 case 'Q': type = VIM_QUESTION; break;
10477 case 'I': type = VIM_INFO; break;
10478 case 'W': type = VIM_WARNING; break;
10479 case 'G': type = VIM_GENERIC; break;
10480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 }
10482 }
10483 }
10484 }
10485
10486 if (buttons == NULL || *buttons == NUL)
10487 buttons = (char_u *)_("&Ok");
10488
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010489 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010490 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010491 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492#endif
10493}
10494
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010495/*
10496 * "copy()" function
10497 */
10498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010499f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010500{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010501 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010502}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010504#ifdef FEAT_FLOAT
10505/*
10506 * "cos()" function
10507 */
10508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010509f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010510{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010511 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010512
10513 rettv->v_type = VAR_FLOAT;
10514 if (get_float_arg(argvars, &f) == OK)
10515 rettv->vval.v_float = cos(f);
10516 else
10517 rettv->vval.v_float = 0.0;
10518}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010519
10520/*
10521 * "cosh()" function
10522 */
10523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010524f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010525{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010526 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010527
10528 rettv->v_type = VAR_FLOAT;
10529 if (get_float_arg(argvars, &f) == OK)
10530 rettv->vval.v_float = cosh(f);
10531 else
10532 rettv->vval.v_float = 0.0;
10533}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010534#endif
10535
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010537 * "count()" function
10538 */
10539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010540f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010541{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010542 long n = 0;
10543 int ic = FALSE;
10544
Bram Moolenaare9a41262005-01-15 22:18:47 +000010545 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010546 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010547 listitem_T *li;
10548 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010549 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010550
Bram Moolenaare9a41262005-01-15 22:18:47 +000010551 if ((l = argvars[0].vval.v_list) != NULL)
10552 {
10553 li = l->lv_first;
10554 if (argvars[2].v_type != VAR_UNKNOWN)
10555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010556 int error = FALSE;
10557
10558 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010559 if (argvars[3].v_type != VAR_UNKNOWN)
10560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010561 idx = get_tv_number_chk(&argvars[3], &error);
10562 if (!error)
10563 {
10564 li = list_find(l, idx);
10565 if (li == NULL)
10566 EMSGN(_(e_listidx), idx);
10567 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010569 if (error)
10570 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010571 }
10572
10573 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010574 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010575 ++n;
10576 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010577 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578 else if (argvars[0].v_type == VAR_DICT)
10579 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010580 int todo;
10581 dict_T *d;
10582 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010584 if ((d = argvars[0].vval.v_dict) != NULL)
10585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010586 int error = FALSE;
10587
Bram Moolenaare9a41262005-01-15 22:18:47 +000010588 if (argvars[2].v_type != VAR_UNKNOWN)
10589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010590 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010591 if (argvars[3].v_type != VAR_UNKNOWN)
10592 EMSG(_(e_invarg));
10593 }
10594
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010595 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010596 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010597 {
10598 if (!HASHITEM_EMPTY(hi))
10599 {
10600 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010601 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010602 ++n;
10603 }
10604 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605 }
10606 }
10607 else
10608 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010609 rettv->vval.v_number = n;
10610}
10611
10612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10614 *
10615 * Checks the existence of a cscope connection.
10616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010618f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619{
10620#ifdef FEAT_CSCOPE
10621 int num = 0;
10622 char_u *dbpath = NULL;
10623 char_u *prepend = NULL;
10624 char_u buf[NUMBUFLEN];
10625
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010626 if (argvars[0].v_type != VAR_UNKNOWN
10627 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629 num = (int)get_tv_number(&argvars[0]);
10630 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010631 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010632 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 }
10634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636#endif
10637}
10638
10639/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010640 * "cursor(lnum, col)" function, or
10641 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010643 * Moves the cursor to the specified line and column.
10644 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010647f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648{
10649 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010650#ifdef FEAT_VIRTUALEDIT
10651 long coladd = 0;
10652#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010653 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010655 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010656 if (argvars[1].v_type == VAR_UNKNOWN)
10657 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010658 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010659 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010660
Bram Moolenaar493c1782014-05-28 14:34:46 +020010661 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010662 {
10663 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010664 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010665 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010666 line = pos.lnum;
10667 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010668#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010669 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010670#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010671 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010672 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010673 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010674 set_curswant = FALSE;
10675 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010676 }
10677 else
10678 {
10679 line = get_tv_lnum(argvars);
10680 col = get_tv_number_chk(&argvars[1], NULL);
10681#ifdef FEAT_VIRTUALEDIT
10682 if (argvars[2].v_type != VAR_UNKNOWN)
10683 coladd = get_tv_number_chk(&argvars[2], NULL);
10684#endif
10685 }
10686 if (line < 0 || col < 0
10687#ifdef FEAT_VIRTUALEDIT
10688 || coladd < 0
10689#endif
10690 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010691 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 if (line > 0)
10693 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 if (col > 0)
10695 curwin->w_cursor.col = col - 1;
10696#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010697 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698#endif
10699
10700 /* Make sure the cursor is in a valid position. */
10701 check_cursor();
10702#ifdef FEAT_MBYTE
10703 /* Correct cursor for multi-byte character. */
10704 if (has_mbyte)
10705 mb_adjust_cursor();
10706#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010707
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010708 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010709 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710}
10711
10712/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010713 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 */
10715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010716f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010718 int noref = 0;
10719
10720 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010721 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010722 if (noref < 0 || noref > 1)
10723 EMSG(_(e_invarg));
10724 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010725 {
10726 current_copyID += COPYID_INC;
10727 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729}
10730
10731/*
10732 * "delete()" function
10733 */
10734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010735f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010737 char_u nbuf[NUMBUFLEN];
10738 char_u *name;
10739 char_u *flags;
10740
10741 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010743 return;
10744
10745 name = get_tv_string(&argvars[0]);
10746 if (name == NULL || *name == NUL)
10747 {
10748 EMSG(_(e_invarg));
10749 return;
10750 }
10751
10752 if (argvars[1].v_type != VAR_UNKNOWN)
10753 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010755 flags = (char_u *)"";
10756
10757 if (*flags == NUL)
10758 /* delete a file */
10759 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10760 else if (STRCMP(flags, "d") == 0)
10761 /* delete an empty directory */
10762 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10763 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010764 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010765 rettv->vval.v_number = delete_recursive(name);
10766 else
10767 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768}
10769
10770/*
10771 * "did_filetype()" function
10772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010774f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775{
10776#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778#endif
10779}
10780
10781/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010782 * "diff_filler()" function
10783 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010785f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010786{
10787#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010789#endif
10790}
10791
10792/*
10793 * "diff_hlID()" function
10794 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010796f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010797{
10798#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010799 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010800 static linenr_T prev_lnum = 0;
10801 static int changedtick = 0;
10802 static int fnum = 0;
10803 static int change_start = 0;
10804 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010805 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010806 int filler_lines;
10807 int col;
10808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010809 if (lnum < 0) /* ignore type error in {lnum} arg */
10810 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010811 if (lnum != prev_lnum
10812 || changedtick != curbuf->b_changedtick
10813 || fnum != curbuf->b_fnum)
10814 {
10815 /* New line, buffer, change: need to get the values. */
10816 filler_lines = diff_check(curwin, lnum);
10817 if (filler_lines < 0)
10818 {
10819 if (filler_lines == -1)
10820 {
10821 change_start = MAXCOL;
10822 change_end = -1;
10823 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10824 hlID = HLF_ADD; /* added line */
10825 else
10826 hlID = HLF_CHD; /* changed line */
10827 }
10828 else
10829 hlID = HLF_ADD; /* added line */
10830 }
10831 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010832 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010833 prev_lnum = lnum;
10834 changedtick = curbuf->b_changedtick;
10835 fnum = curbuf->b_fnum;
10836 }
10837
10838 if (hlID == HLF_CHD || hlID == HLF_TXD)
10839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010840 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010841 if (col >= change_start && col <= change_end)
10842 hlID = HLF_TXD; /* changed text */
10843 else
10844 hlID = HLF_CHD; /* changed line */
10845 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010846 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010847#endif
10848}
10849
10850/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010851 * "disable_char_avail_for_testing({expr})" function
10852 */
10853 static void
10854f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10855{
10856 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10857}
10858
10859/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010860 * "empty({expr})" function
10861 */
10862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010863f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010864{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010865 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010866
10867 switch (argvars[0].v_type)
10868 {
10869 case VAR_STRING:
10870 case VAR_FUNC:
10871 n = argvars[0].vval.v_string == NULL
10872 || *argvars[0].vval.v_string == NUL;
10873 break;
10874 case VAR_NUMBER:
10875 n = argvars[0].vval.v_number == 0;
10876 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010877 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010878#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010879 n = argvars[0].vval.v_float == 0.0;
10880 break;
10881#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010882 case VAR_LIST:
10883 n = argvars[0].vval.v_list == NULL
10884 || argvars[0].vval.v_list->lv_first == NULL;
10885 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010886 case VAR_DICT:
10887 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010888 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010889 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010890 case VAR_SPECIAL:
10891 n = argvars[0].vval.v_number != VVAL_TRUE;
10892 break;
10893
Bram Moolenaar835dc632016-02-07 14:27:38 +010010894 case VAR_JOB:
10895#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010010896 n = argvars[0].vval.v_job == NULL
10897 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10898 break;
10899#endif
10900 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010010901#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010902 n = argvars[0].vval.v_channel == NULL
10903 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010904 break;
10905#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010906 case VAR_UNKNOWN:
10907 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10908 n = TRUE;
10909 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010910 }
10911
10912 rettv->vval.v_number = n;
10913}
10914
10915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 * "escape({string}, {chars})" function
10917 */
10918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010919f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920{
10921 char_u buf[NUMBUFLEN];
10922
Bram Moolenaar758711c2005-02-02 23:11:38 +000010923 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10924 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010925 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926}
10927
10928/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010929 * "eval()" function
10930 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010932f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010933{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010934 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010935
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010936 s = get_tv_string_chk(&argvars[0]);
10937 if (s != NULL)
10938 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010939
Bram Moolenaar615b9972015-01-14 17:15:05 +010010940 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010941 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10942 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010943 if (p != NULL && !aborting())
10944 EMSG2(_(e_invexpr2), p);
10945 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010947 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010948 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010949 else if (*s != NUL)
10950 EMSG(_(e_trailing));
10951}
10952
10953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 * "eventhandler()" function
10955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010957f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010959 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960}
10961
10962/*
10963 * "executable()" function
10964 */
10965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010966f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010968 char_u *name = get_tv_string(&argvars[0]);
10969
10970 /* Check in $PATH and also check directly if there is a directory name. */
10971 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10972 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010973}
10974
10975/*
10976 * "exepath()" function
10977 */
10978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010979f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010980{
10981 char_u *p = NULL;
10982
Bram Moolenaarb5971142015-03-21 17:32:19 +010010983 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010984 rettv->v_type = VAR_STRING;
10985 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986}
10987
10988/*
10989 * "exists()" function
10990 */
10991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010992f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993{
10994 char_u *p;
10995 char_u *name;
10996 int n = FALSE;
10997 int len = 0;
10998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 if (*p == '$') /* environment variable */
11001 {
11002 /* first try "normal" environment variables (fast) */
11003 if (mch_getenv(p + 1) != NULL)
11004 n = TRUE;
11005 else
11006 {
11007 /* try expanding things like $VIM and ${HOME} */
11008 p = expand_env_save(p);
11009 if (p != NULL && *p != '$')
11010 n = TRUE;
11011 vim_free(p);
11012 }
11013 }
11014 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011017 if (*skipwhite(p) != NUL)
11018 n = FALSE; /* trailing garbage */
11019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 else if (*p == '*') /* internal or user defined function */
11021 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011022 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 }
11024 else if (*p == ':')
11025 {
11026 n = cmd_exists(p + 1);
11027 }
11028 else if (*p == '#')
11029 {
11030#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011031 if (p[1] == '#')
11032 n = autocmd_supported(p + 2);
11033 else
11034 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035#endif
11036 }
11037 else /* internal variable */
11038 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011039 char_u *tofree;
11040 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011042 /* get_name_len() takes care of expanding curly braces */
11043 name = p;
11044 len = get_name_len(&p, &tofree, TRUE, FALSE);
11045 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011047 if (tofree != NULL)
11048 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011049 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011050 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011052 /* handle d.key, l[idx], f(expr) */
11053 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11054 if (n)
11055 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 }
11057 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011058 if (*p != NUL)
11059 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011061 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 }
11063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065}
11066
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011067#ifdef FEAT_FLOAT
11068/*
11069 * "exp()" function
11070 */
11071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011072f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011073{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011074 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011075
11076 rettv->v_type = VAR_FLOAT;
11077 if (get_float_arg(argvars, &f) == OK)
11078 rettv->vval.v_float = exp(f);
11079 else
11080 rettv->vval.v_float = 0.0;
11081}
11082#endif
11083
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084/*
11085 * "expand()" function
11086 */
11087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011088f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089{
11090 char_u *s;
11091 int len;
11092 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011093 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011096 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011099 if (argvars[1].v_type != VAR_UNKNOWN
11100 && argvars[2].v_type != VAR_UNKNOWN
11101 && get_tv_number_chk(&argvars[2], &error)
11102 && !error)
11103 {
11104 rettv->v_type = VAR_LIST;
11105 rettv->vval.v_list = NULL;
11106 }
11107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 if (*s == '%' || *s == '#' || *s == '<')
11110 {
11111 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011112 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011114 if (rettv->v_type == VAR_LIST)
11115 {
11116 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11117 list_append_string(rettv->vval.v_list, result, -1);
11118 else
11119 vim_free(result);
11120 }
11121 else
11122 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 }
11124 else
11125 {
11126 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011127 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 if (argvars[1].v_type != VAR_UNKNOWN
11129 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011130 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011131 if (!error)
11132 {
11133 ExpandInit(&xpc);
11134 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011135 if (p_wic)
11136 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011137 if (rettv->v_type == VAR_STRING)
11138 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11139 options, WILD_ALL);
11140 else if (rettv_list_alloc(rettv) != FAIL)
11141 {
11142 int i;
11143
11144 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11145 for (i = 0; i < xpc.xp_numfiles; i++)
11146 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11147 ExpandCleanup(&xpc);
11148 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011149 }
11150 else
11151 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 }
11153}
11154
11155/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011156 * Go over all entries in "d2" and add them to "d1".
11157 * When "action" is "error" then a duplicate key is an error.
11158 * When "action" is "force" then a duplicate key is overwritten.
11159 * Otherwise duplicate keys are ignored ("action" is "keep").
11160 */
11161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011162dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011163{
11164 dictitem_T *di1;
11165 hashitem_T *hi2;
11166 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011167 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011168
11169 todo = (int)d2->dv_hashtab.ht_used;
11170 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11171 {
11172 if (!HASHITEM_EMPTY(hi2))
11173 {
11174 --todo;
11175 di1 = dict_find(d1, hi2->hi_key, -1);
11176 if (d1->dv_scope != 0)
11177 {
11178 /* Disallow replacing a builtin function in l: and g:.
11179 * Check the key to be valid when adding to any
11180 * scope. */
11181 if (d1->dv_scope == VAR_DEF_SCOPE
11182 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11183 && var_check_func_name(hi2->hi_key,
11184 di1 == NULL))
11185 break;
11186 if (!valid_varname(hi2->hi_key))
11187 break;
11188 }
11189 if (di1 == NULL)
11190 {
11191 di1 = dictitem_copy(HI2DI(hi2));
11192 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11193 dictitem_free(di1);
11194 }
11195 else if (*action == 'e')
11196 {
11197 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11198 break;
11199 }
11200 else if (*action == 'f' && HI2DI(hi2) != di1)
11201 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011202 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11203 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011204 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011205 clear_tv(&di1->di_tv);
11206 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11207 }
11208 }
11209 }
11210}
11211
11212/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011213 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011214 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011215 */
11216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011217f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011218{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011219 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011220
Bram Moolenaare9a41262005-01-15 22:18:47 +000011221 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011222 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011223 list_T *l1, *l2;
11224 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011225 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011226 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011227
Bram Moolenaare9a41262005-01-15 22:18:47 +000011228 l1 = argvars[0].vval.v_list;
11229 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011230 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011231 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011232 {
11233 if (argvars[2].v_type != VAR_UNKNOWN)
11234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011235 before = get_tv_number_chk(&argvars[2], &error);
11236 if (error)
11237 return; /* type error; errmsg already given */
11238
Bram Moolenaar758711c2005-02-02 23:11:38 +000011239 if (before == l1->lv_len)
11240 item = NULL;
11241 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011242 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011243 item = list_find(l1, before);
11244 if (item == NULL)
11245 {
11246 EMSGN(_(e_listidx), before);
11247 return;
11248 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011249 }
11250 }
11251 else
11252 item = NULL;
11253 list_extend(l1, l2, item);
11254
Bram Moolenaare9a41262005-01-15 22:18:47 +000011255 copy_tv(&argvars[0], rettv);
11256 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011257 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011258 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11259 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011260 dict_T *d1, *d2;
11261 char_u *action;
11262 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011263
11264 d1 = argvars[0].vval.v_dict;
11265 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011266 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011267 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011268 {
11269 /* Check the third argument. */
11270 if (argvars[2].v_type != VAR_UNKNOWN)
11271 {
11272 static char *(av[]) = {"keep", "force", "error"};
11273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011274 action = get_tv_string_chk(&argvars[2]);
11275 if (action == NULL)
11276 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011277 for (i = 0; i < 3; ++i)
11278 if (STRCMP(action, av[i]) == 0)
11279 break;
11280 if (i == 3)
11281 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011282 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011283 return;
11284 }
11285 }
11286 else
11287 action = (char_u *)"force";
11288
Bram Moolenaara9922d62013-05-30 13:01:18 +020011289 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011290
Bram Moolenaare9a41262005-01-15 22:18:47 +000011291 copy_tv(&argvars[0], rettv);
11292 }
11293 }
11294 else
11295 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011296}
11297
11298/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011299 * "feedkeys()" function
11300 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011302f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011303{
11304 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011305 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011306 char_u *keys, *flags;
11307 char_u nbuf[NUMBUFLEN];
11308 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011309 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011310 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011311
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011312 /* This is not allowed in the sandbox. If the commands would still be
11313 * executed in the sandbox it would be OK, but it probably happens later,
11314 * when "sandbox" is no longer set. */
11315 if (check_secure())
11316 return;
11317
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011318 keys = get_tv_string(&argvars[0]);
11319 if (*keys != NUL)
11320 {
11321 if (argvars[1].v_type != VAR_UNKNOWN)
11322 {
11323 flags = get_tv_string_buf(&argvars[1], nbuf);
11324 for ( ; *flags != NUL; ++flags)
11325 {
11326 switch (*flags)
11327 {
11328 case 'n': remap = FALSE; break;
11329 case 'm': remap = TRUE; break;
11330 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011331 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011332 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011333 }
11334 }
11335 }
11336
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011337 /* Need to escape K_SPECIAL and CSI before putting the string in the
11338 * typeahead buffer. */
11339 keys_esc = vim_strsave_escape_csi(keys);
11340 if (keys_esc != NULL)
11341 {
11342 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011343 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011344 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011345 if (vgetc_busy)
11346 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011347 if (execute)
11348 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011349 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011350 }
11351}
11352
11353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 * "filereadable()" function
11355 */
11356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011357f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011359 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 char_u *p;
11361 int n;
11362
Bram Moolenaarc236c162008-07-13 17:41:49 +000011363#ifndef O_NONBLOCK
11364# define O_NONBLOCK 0
11365#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011366 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011367 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11368 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 {
11370 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011371 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 }
11373 else
11374 n = FALSE;
11375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377}
11378
11379/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011380 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 * rights to write into.
11382 */
11383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011384f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011386 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387}
11388
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011389 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011390findfilendir(
11391 typval_T *argvars UNUSED,
11392 typval_T *rettv,
11393 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011394{
11395#ifdef FEAT_SEARCHPATH
11396 char_u *fname;
11397 char_u *fresult = NULL;
11398 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11399 char_u *p;
11400 char_u pathbuf[NUMBUFLEN];
11401 int count = 1;
11402 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011403 int error = FALSE;
11404#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011405
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011406 rettv->vval.v_string = NULL;
11407 rettv->v_type = VAR_STRING;
11408
11409#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011411
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011412 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11415 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011416 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011417 else
11418 {
11419 if (*p != NUL)
11420 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011422 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011423 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011424 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011425 }
11426
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011427 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11428 error = TRUE;
11429
11430 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011432 do
11433 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011434 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011435 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011436 fresult = find_file_in_path_option(first ? fname : NULL,
11437 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011438 0, first, path,
11439 find_what,
11440 curbuf->b_ffname,
11441 find_what == FINDFILE_DIR
11442 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011444
11445 if (fresult != NULL && rettv->v_type == VAR_LIST)
11446 list_append_string(rettv->vval.v_list, fresult, -1);
11447
11448 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011449 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011450
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011451 if (rettv->v_type == VAR_STRING)
11452 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011453#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011454}
11455
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011456static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11457static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011458
11459/*
11460 * Implementation of map() and filter().
11461 */
11462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011463filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011464{
11465 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011466 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011467 listitem_T *li, *nli;
11468 list_T *l = NULL;
11469 dictitem_T *di;
11470 hashtab_T *ht;
11471 hashitem_T *hi;
11472 dict_T *d = NULL;
11473 typval_T save_val;
11474 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011475 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011476 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011477 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011478 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011479 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011480 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011481 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011482
Bram Moolenaare9a41262005-01-15 22:18:47 +000011483 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011484 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011485 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011486 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011487 return;
11488 }
11489 else if (argvars[0].v_type == VAR_DICT)
11490 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011491 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011492 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011493 return;
11494 }
11495 else
11496 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011497 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011498 return;
11499 }
11500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 expr = get_tv_string_buf_chk(&argvars[1], buf);
11502 /* On type errors, the preceding call has already displayed an error
11503 * message. Avoid a misleading error message for an empty string that
11504 * was not passed as argument. */
11505 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011507 prepare_vimvar(VV_VAL, &save_val);
11508 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011509
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011510 /* We reset "did_emsg" to be able to detect whether an error
11511 * occurred during evaluation of the expression. */
11512 save_did_emsg = did_emsg;
11513 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011514
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011515 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011516 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011518 vimvars[VV_KEY].vv_type = VAR_STRING;
11519
11520 ht = &d->dv_hashtab;
11521 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011522 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011523 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011525 if (!HASHITEM_EMPTY(hi))
11526 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011527 int r;
11528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011529 --todo;
11530 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011531 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011532 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11533 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011534 break;
11535 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011536 r = filter_map_one(&di->di_tv, expr, map, &rem);
11537 clear_tv(&vimvars[VV_KEY].vv_tv);
11538 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011539 break;
11540 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011541 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011542 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11543 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011544 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011545 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011546 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011547 }
11548 }
11549 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011550 }
11551 else
11552 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011553 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11554
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011555 for (li = l->lv_first; li != NULL; li = nli)
11556 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011557 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011558 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011560 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011561 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011562 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011563 break;
11564 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011565 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011566 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011567 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011568 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011569
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011570 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011571 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011572
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011573 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011574 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011575
11576 copy_tv(&argvars[0], rettv);
11577}
11578
11579 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011580filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011581{
Bram Moolenaar33570922005-01-25 22:26:29 +000011582 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011583 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011584 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011585
Bram Moolenaar33570922005-01-25 22:26:29 +000011586 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011587 s = expr;
11588 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011589 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011590 if (*s != NUL) /* check for trailing chars after expr */
11591 {
11592 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011593 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011594 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011595 }
11596 if (map)
11597 {
11598 /* map(): replace the list item value */
11599 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011600 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011601 *tv = rettv;
11602 }
11603 else
11604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011605 int error = FALSE;
11606
Bram Moolenaare9a41262005-01-15 22:18:47 +000011607 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011608 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011609 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011610 /* On type error, nothing has been removed; return FAIL to stop the
11611 * loop. The error message was given by get_tv_number_chk(). */
11612 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011613 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011614 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011615 retval = OK;
11616theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011617 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011618 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011619}
11620
11621/*
11622 * "filter()" function
11623 */
11624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011625f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011626{
11627 filter_map(argvars, rettv, FALSE);
11628}
11629
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011630/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011631 * "finddir({fname}[, {path}[, {count}]])" function
11632 */
11633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011634f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011635{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011636 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011637}
11638
11639/*
11640 * "findfile({fname}[, {path}[, {count}]])" function
11641 */
11642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011643f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011644{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011645 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011646}
11647
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011648#ifdef FEAT_FLOAT
11649/*
11650 * "float2nr({float})" function
11651 */
11652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011653f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011654{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011655 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011656
11657 if (get_float_arg(argvars, &f) == OK)
11658 {
11659 if (f < -0x7fffffff)
11660 rettv->vval.v_number = -0x7fffffff;
11661 else if (f > 0x7fffffff)
11662 rettv->vval.v_number = 0x7fffffff;
11663 else
11664 rettv->vval.v_number = (varnumber_T)f;
11665 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011666}
11667
11668/*
11669 * "floor({float})" function
11670 */
11671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011672f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011673{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011674 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011675
11676 rettv->v_type = VAR_FLOAT;
11677 if (get_float_arg(argvars, &f) == OK)
11678 rettv->vval.v_float = floor(f);
11679 else
11680 rettv->vval.v_float = 0.0;
11681}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011682
11683/*
11684 * "fmod()" function
11685 */
11686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011687f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011688{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011689 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011690
11691 rettv->v_type = VAR_FLOAT;
11692 if (get_float_arg(argvars, &fx) == OK
11693 && get_float_arg(&argvars[1], &fy) == OK)
11694 rettv->vval.v_float = fmod(fx, fy);
11695 else
11696 rettv->vval.v_float = 0.0;
11697}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011698#endif
11699
Bram Moolenaar0d660222005-01-07 21:51:51 +000011700/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011701 * "fnameescape({string})" function
11702 */
11703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011704f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011705{
11706 rettv->vval.v_string = vim_strsave_fnameescape(
11707 get_tv_string(&argvars[0]), FALSE);
11708 rettv->v_type = VAR_STRING;
11709}
11710
11711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712 * "fnamemodify({fname}, {mods})" function
11713 */
11714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011715f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716{
11717 char_u *fname;
11718 char_u *mods;
11719 int usedlen = 0;
11720 int len;
11721 char_u *fbuf = NULL;
11722 char_u buf[NUMBUFLEN];
11723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011724 fname = get_tv_string_chk(&argvars[0]);
11725 mods = get_tv_string_buf_chk(&argvars[1], buf);
11726 if (fname == NULL || mods == NULL)
11727 fname = NULL;
11728 else
11729 {
11730 len = (int)STRLEN(fname);
11731 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011734 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011736 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011738 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739 vim_free(fbuf);
11740}
11741
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011742static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743
11744/*
11745 * "foldclosed()" function
11746 */
11747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011748foldclosed_both(
11749 typval_T *argvars UNUSED,
11750 typval_T *rettv,
11751 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752{
11753#ifdef FEAT_FOLDING
11754 linenr_T lnum;
11755 linenr_T first, last;
11756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11759 {
11760 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11761 {
11762 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011763 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011765 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 return;
11767 }
11768 }
11769#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011770 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771}
11772
11773/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011774 * "foldclosed()" function
11775 */
11776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011777f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011778{
11779 foldclosed_both(argvars, rettv, FALSE);
11780}
11781
11782/*
11783 * "foldclosedend()" function
11784 */
11785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011786f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011787{
11788 foldclosed_both(argvars, rettv, TRUE);
11789}
11790
11791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792 * "foldlevel()" function
11793 */
11794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011795f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796{
11797#ifdef FEAT_FOLDING
11798 linenr_T lnum;
11799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804}
11805
11806/*
11807 * "foldtext()" function
11808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011810f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811{
11812#ifdef FEAT_FOLDING
11813 linenr_T lnum;
11814 char_u *s;
11815 char_u *r;
11816 int len;
11817 char *txt;
11818#endif
11819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011820 rettv->v_type = VAR_STRING;
11821 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011823 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11824 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11825 <= curbuf->b_ml.ml_line_count
11826 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827 {
11828 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011829 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11830 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 {
11832 if (!linewhite(lnum))
11833 break;
11834 ++lnum;
11835 }
11836
11837 /* Find interesting text in this line. */
11838 s = skipwhite(ml_get(lnum));
11839 /* skip C comment-start */
11840 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011841 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011843 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011844 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011845 {
11846 s = skipwhite(ml_get(lnum + 1));
11847 if (*s == '*')
11848 s = skipwhite(s + 1);
11849 }
11850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 txt = _("+-%s%3ld lines: ");
11852 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011853 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 + 20 /* for %3ld */
11855 + STRLEN(s))); /* concatenated */
11856 if (r != NULL)
11857 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011858 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11859 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11860 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861 len = (int)STRLEN(r);
11862 STRCAT(r, s);
11863 /* remove 'foldmarker' and 'commentstring' */
11864 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866 }
11867 }
11868#endif
11869}
11870
11871/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011872 * "foldtextresult(lnum)" function
11873 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011875f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011876{
11877#ifdef FEAT_FOLDING
11878 linenr_T lnum;
11879 char_u *text;
11880 char_u buf[51];
11881 foldinfo_T foldinfo;
11882 int fold_count;
11883#endif
11884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885 rettv->v_type = VAR_STRING;
11886 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011887#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011889 /* treat illegal types and illegal string values for {lnum} the same */
11890 if (lnum < 0)
11891 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011892 fold_count = foldedCount(curwin, lnum, &foldinfo);
11893 if (fold_count > 0)
11894 {
11895 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11896 &foldinfo, buf);
11897 if (text == buf)
11898 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011900 }
11901#endif
11902}
11903
11904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 * "foreground()" function
11906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011908f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910#ifdef FEAT_GUI
11911 if (gui.in_use)
11912 gui_mch_set_foreground();
11913#else
11914# ifdef WIN32
11915 win32_set_foreground();
11916# endif
11917#endif
11918}
11919
11920/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011921 * "function()" function
11922 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011924f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011925{
11926 char_u *s;
11927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011928 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011929 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011930 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011931 /* Don't check an autoload name for existence here. */
11932 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011933 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011934 else
11935 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011936 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011937 {
11938 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011939 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011940
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011941 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11942 * also be called from another script. Using trans_function_name()
11943 * would also work, but some plugins depend on the name being
11944 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011945 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011946 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011947 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011948 if (rettv->vval.v_string != NULL)
11949 {
11950 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011951 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011952 }
11953 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011954 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011955 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011957 }
11958}
11959
11960/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011961 * "garbagecollect()" function
11962 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011964f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011965{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011966 /* This is postponed until we are back at the toplevel, because we may be
11967 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11968 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011969
11970 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11971 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011972}
11973
11974/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011975 * "get()" function
11976 */
11977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011978f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011979{
Bram Moolenaar33570922005-01-25 22:26:29 +000011980 listitem_T *li;
11981 list_T *l;
11982 dictitem_T *di;
11983 dict_T *d;
11984 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011985
Bram Moolenaare9a41262005-01-15 22:18:47 +000011986 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011987 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011988 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011990 int error = FALSE;
11991
11992 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11993 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011994 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011995 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011996 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011997 else if (argvars[0].v_type == VAR_DICT)
11998 {
11999 if ((d = argvars[0].vval.v_dict) != NULL)
12000 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012001 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012002 if (di != NULL)
12003 tv = &di->di_tv;
12004 }
12005 }
12006 else
12007 EMSG2(_(e_listdictarg), "get()");
12008
12009 if (tv == NULL)
12010 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012011 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012012 copy_tv(&argvars[2], rettv);
12013 }
12014 else
12015 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012016}
12017
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012018static 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 +000012019
12020/*
12021 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012022 * Return a range (from start to end) of lines in rettv from the specified
12023 * buffer.
12024 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012025 */
12026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012027get_buffer_lines(
12028 buf_T *buf,
12029 linenr_T start,
12030 linenr_T end,
12031 int retlist,
12032 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012033{
12034 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012035
Bram Moolenaar959a1432013-12-14 12:17:38 +010012036 rettv->v_type = VAR_STRING;
12037 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012038 if (retlist && rettv_list_alloc(rettv) == FAIL)
12039 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012040
12041 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12042 return;
12043
12044 if (!retlist)
12045 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012046 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12047 p = ml_get_buf(buf, start, FALSE);
12048 else
12049 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012050 rettv->vval.v_string = vim_strsave(p);
12051 }
12052 else
12053 {
12054 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012055 return;
12056
12057 if (start < 1)
12058 start = 1;
12059 if (end > buf->b_ml.ml_line_count)
12060 end = buf->b_ml.ml_line_count;
12061 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012062 if (list_append_string(rettv->vval.v_list,
12063 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012064 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012065 }
12066}
12067
12068/*
12069 * "getbufline()" function
12070 */
12071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012072f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012073{
12074 linenr_T lnum;
12075 linenr_T end;
12076 buf_T *buf;
12077
12078 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12079 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012080 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012081 --emsg_off;
12082
Bram Moolenaar661b1822005-07-28 22:36:45 +000012083 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012084 if (argvars[2].v_type == VAR_UNKNOWN)
12085 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012086 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012087 end = get_tv_lnum_buf(&argvars[2], buf);
12088
Bram Moolenaar342337a2005-07-21 21:11:17 +000012089 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012090}
12091
Bram Moolenaar0d660222005-01-07 21:51:51 +000012092/*
12093 * "getbufvar()" function
12094 */
12095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012096f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012097{
12098 buf_T *buf;
12099 buf_T *save_curbuf;
12100 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012101 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012102 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012104 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12105 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012106 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012107 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012108
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012109 rettv->v_type = VAR_STRING;
12110 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012111
12112 if (buf != NULL && varname != NULL)
12113 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012114 /* set curbuf to be our buf, temporarily */
12115 save_curbuf = curbuf;
12116 curbuf = buf;
12117
Bram Moolenaar0d660222005-01-07 21:51:51 +000012118 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012119 {
12120 if (get_option_tv(&varname, rettv, TRUE) == OK)
12121 done = TRUE;
12122 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012123 else if (STRCMP(varname, "changedtick") == 0)
12124 {
12125 rettv->v_type = VAR_NUMBER;
12126 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012127 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012128 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012129 else
12130 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012131 /* Look up the variable. */
12132 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12133 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12134 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012135 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012136 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012137 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012138 done = TRUE;
12139 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012140 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012141
12142 /* restore previous notion of curbuf */
12143 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012144 }
12145
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012146 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12147 /* use the default value */
12148 copy_tv(&argvars[2], rettv);
12149
Bram Moolenaar0d660222005-01-07 21:51:51 +000012150 --emsg_off;
12151}
12152
12153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154 * "getchar()" function
12155 */
12156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012157f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158{
12159 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012160 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012162 /* Position the cursor. Needed after a message that ends in a space. */
12163 windgoto(msg_row, msg_col);
12164
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 ++no_mapping;
12166 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012167 for (;;)
12168 {
12169 if (argvars[0].v_type == VAR_UNKNOWN)
12170 /* getchar(): blocking wait. */
12171 n = safe_vgetc();
12172 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12173 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012174 n = vpeekc_any();
12175 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012176 /* illegal argument or getchar(0) and no char avail: return zero */
12177 n = 0;
12178 else
12179 /* getchar(0) and char avail: return char */
12180 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012181
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012182 if (n == K_IGNORE)
12183 continue;
12184 break;
12185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 --no_mapping;
12187 --allow_keys;
12188
Bram Moolenaar219b8702006-11-01 14:32:36 +000012189 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12190 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12191 vimvars[VV_MOUSE_COL].vv_nr = 0;
12192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012193 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194 if (IS_SPECIAL(n) || mod_mask != 0)
12195 {
12196 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12197 int i = 0;
12198
12199 /* Turn a special key into three bytes, plus modifier. */
12200 if (mod_mask != 0)
12201 {
12202 temp[i++] = K_SPECIAL;
12203 temp[i++] = KS_MODIFIER;
12204 temp[i++] = mod_mask;
12205 }
12206 if (IS_SPECIAL(n))
12207 {
12208 temp[i++] = K_SPECIAL;
12209 temp[i++] = K_SECOND(n);
12210 temp[i++] = K_THIRD(n);
12211 }
12212#ifdef FEAT_MBYTE
12213 else if (has_mbyte)
12214 i += (*mb_char2bytes)(n, temp + i);
12215#endif
12216 else
12217 temp[i++] = n;
12218 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012219 rettv->v_type = VAR_STRING;
12220 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012221
12222#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012223 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012224 {
12225 int row = mouse_row;
12226 int col = mouse_col;
12227 win_T *win;
12228 linenr_T lnum;
12229# ifdef FEAT_WINDOWS
12230 win_T *wp;
12231# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012232 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012233
12234 if (row >= 0 && col >= 0)
12235 {
12236 /* Find the window at the mouse coordinates and compute the
12237 * text position. */
12238 win = mouse_find_win(&row, &col);
12239 (void)mouse_comp_pos(win, &row, &col, &lnum);
12240# ifdef FEAT_WINDOWS
12241 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012242 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012243# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012244 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012245 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12246 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12247 }
12248 }
12249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250 }
12251}
12252
12253/*
12254 * "getcharmod()" function
12255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012257f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012259 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260}
12261
12262/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012263 * "getcharsearch()" function
12264 */
12265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012266f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012267{
12268 if (rettv_dict_alloc(rettv) != FAIL)
12269 {
12270 dict_T *dict = rettv->vval.v_dict;
12271
12272 dict_add_nr_str(dict, "char", 0L, last_csearch());
12273 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12274 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12275 }
12276}
12277
12278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279 * "getcmdline()" function
12280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012282f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012284 rettv->v_type = VAR_STRING;
12285 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286}
12287
12288/*
12289 * "getcmdpos()" function
12290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012292f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012294 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295}
12296
12297/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012298 * "getcmdtype()" function
12299 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012301f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012302{
12303 rettv->v_type = VAR_STRING;
12304 rettv->vval.v_string = alloc(2);
12305 if (rettv->vval.v_string != NULL)
12306 {
12307 rettv->vval.v_string[0] = get_cmdline_type();
12308 rettv->vval.v_string[1] = NUL;
12309 }
12310}
12311
12312/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012313 * "getcmdwintype()" function
12314 */
12315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012316f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012317{
12318 rettv->v_type = VAR_STRING;
12319 rettv->vval.v_string = NULL;
12320#ifdef FEAT_CMDWIN
12321 rettv->vval.v_string = alloc(2);
12322 if (rettv->vval.v_string != NULL)
12323 {
12324 rettv->vval.v_string[0] = cmdwin_type;
12325 rettv->vval.v_string[1] = NUL;
12326 }
12327#endif
12328}
12329
12330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 * "getcwd()" function
12332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012334f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012336 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012337 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012339 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012340 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012341
12342 wp = find_tabwin(&argvars[0], &argvars[1]);
12343 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012345 if (wp->w_localdir != NULL)
12346 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12347 else if(globaldir != NULL)
12348 rettv->vval.v_string = vim_strsave(globaldir);
12349 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012350 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012351 cwd = alloc(MAXPATHL);
12352 if (cwd != NULL)
12353 {
12354 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12355 rettv->vval.v_string = vim_strsave(cwd);
12356 vim_free(cwd);
12357 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012358 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012359#ifdef BACKSLASH_IN_FILENAME
12360 if (rettv->vval.v_string != NULL)
12361 slash_adjust(rettv->vval.v_string);
12362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363 }
12364}
12365
12366/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012367 * "getfontname()" function
12368 */
12369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012370f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012371{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012372 rettv->v_type = VAR_STRING;
12373 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012374#ifdef FEAT_GUI
12375 if (gui.in_use)
12376 {
12377 GuiFont font;
12378 char_u *name = NULL;
12379
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012380 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012381 {
12382 /* Get the "Normal" font. Either the name saved by
12383 * hl_set_font_name() or from the font ID. */
12384 font = gui.norm_font;
12385 name = hl_get_font_name();
12386 }
12387 else
12388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012389 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012390 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12391 return;
12392 font = gui_mch_get_font(name, FALSE);
12393 if (font == NOFONT)
12394 return; /* Invalid font name, return empty string. */
12395 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012396 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012397 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012398 gui_mch_free_font(font);
12399 }
12400#endif
12401}
12402
12403/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012404 * "getfperm({fname})" function
12405 */
12406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012407f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012408{
12409 char_u *fname;
12410 struct stat st;
12411 char_u *perm = NULL;
12412 char_u flags[] = "rwx";
12413 int i;
12414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012417 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012418 if (mch_stat((char *)fname, &st) >= 0)
12419 {
12420 perm = vim_strsave((char_u *)"---------");
12421 if (perm != NULL)
12422 {
12423 for (i = 0; i < 9; i++)
12424 {
12425 if (st.st_mode & (1 << (8 - i)))
12426 perm[i] = flags[i % 3];
12427 }
12428 }
12429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012430 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012431}
12432
12433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434 * "getfsize({fname})" function
12435 */
12436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012437f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438{
12439 char_u *fname;
12440 struct stat st;
12441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012442 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012444 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445
12446 if (mch_stat((char *)fname, &st) >= 0)
12447 {
12448 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012449 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012452 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012453
12454 /* non-perfect check for overflow */
12455 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12456 rettv->vval.v_number = -2;
12457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458 }
12459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012460 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461}
12462
12463/*
12464 * "getftime({fname})" function
12465 */
12466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012467f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468{
12469 char_u *fname;
12470 struct stat st;
12471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012472 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473
12474 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012477 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478}
12479
12480/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012481 * "getftype({fname})" function
12482 */
12483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012484f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012485{
12486 char_u *fname;
12487 struct stat st;
12488 char_u *type = NULL;
12489 char *t;
12490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012493 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012494 if (mch_lstat((char *)fname, &st) >= 0)
12495 {
12496#ifdef S_ISREG
12497 if (S_ISREG(st.st_mode))
12498 t = "file";
12499 else if (S_ISDIR(st.st_mode))
12500 t = "dir";
12501# ifdef S_ISLNK
12502 else if (S_ISLNK(st.st_mode))
12503 t = "link";
12504# endif
12505# ifdef S_ISBLK
12506 else if (S_ISBLK(st.st_mode))
12507 t = "bdev";
12508# endif
12509# ifdef S_ISCHR
12510 else if (S_ISCHR(st.st_mode))
12511 t = "cdev";
12512# endif
12513# ifdef S_ISFIFO
12514 else if (S_ISFIFO(st.st_mode))
12515 t = "fifo";
12516# endif
12517# ifdef S_ISSOCK
12518 else if (S_ISSOCK(st.st_mode))
12519 t = "fifo";
12520# endif
12521 else
12522 t = "other";
12523#else
12524# ifdef S_IFMT
12525 switch (st.st_mode & S_IFMT)
12526 {
12527 case S_IFREG: t = "file"; break;
12528 case S_IFDIR: t = "dir"; break;
12529# ifdef S_IFLNK
12530 case S_IFLNK: t = "link"; break;
12531# endif
12532# ifdef S_IFBLK
12533 case S_IFBLK: t = "bdev"; break;
12534# endif
12535# ifdef S_IFCHR
12536 case S_IFCHR: t = "cdev"; break;
12537# endif
12538# ifdef S_IFIFO
12539 case S_IFIFO: t = "fifo"; break;
12540# endif
12541# ifdef S_IFSOCK
12542 case S_IFSOCK: t = "socket"; break;
12543# endif
12544 default: t = "other";
12545 }
12546# else
12547 if (mch_isdir(fname))
12548 t = "dir";
12549 else
12550 t = "file";
12551# endif
12552#endif
12553 type = vim_strsave((char_u *)t);
12554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012555 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012556}
12557
12558/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012559 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012560 */
12561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012562f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012563{
12564 linenr_T lnum;
12565 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012566 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012567
12568 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012569 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012570 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012571 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012572 retlist = FALSE;
12573 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012574 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012575 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012576 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012577 retlist = TRUE;
12578 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012579
Bram Moolenaar342337a2005-07-21 21:11:17 +000012580 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012581}
12582
12583/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012584 * "getmatches()" function
12585 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012587f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012588{
12589#ifdef FEAT_SEARCH_EXTRA
12590 dict_T *dict;
12591 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012592 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012593
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012594 if (rettv_list_alloc(rettv) == OK)
12595 {
12596 while (cur != NULL)
12597 {
12598 dict = dict_alloc();
12599 if (dict == NULL)
12600 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012601 if (cur->match.regprog == NULL)
12602 {
12603 /* match added with matchaddpos() */
12604 for (i = 0; i < MAXPOSMATCH; ++i)
12605 {
12606 llpos_T *llpos;
12607 char buf[6];
12608 list_T *l;
12609
12610 llpos = &cur->pos.pos[i];
12611 if (llpos->lnum == 0)
12612 break;
12613 l = list_alloc();
12614 if (l == NULL)
12615 break;
12616 list_append_number(l, (varnumber_T)llpos->lnum);
12617 if (llpos->col > 0)
12618 {
12619 list_append_number(l, (varnumber_T)llpos->col);
12620 list_append_number(l, (varnumber_T)llpos->len);
12621 }
12622 sprintf(buf, "pos%d", i + 1);
12623 dict_add_list(dict, buf, l);
12624 }
12625 }
12626 else
12627 {
12628 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12629 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012630 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012631 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12632 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012633# ifdef FEAT_CONCEAL
12634 if (cur->conceal_char)
12635 {
12636 char_u buf[MB_MAXBYTES + 1];
12637
12638 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12639 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12640 }
12641# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012642 list_append_dict(rettv->vval.v_list, dict);
12643 cur = cur->next;
12644 }
12645 }
12646#endif
12647}
12648
12649/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012650 * "getpid()" function
12651 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012653f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012654{
12655 rettv->vval.v_number = mch_get_pid();
12656}
12657
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012658static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012659
12660/*
12661 * "getcurpos()" function
12662 */
12663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012664f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012665{
12666 getpos_both(argvars, rettv, TRUE);
12667}
12668
Bram Moolenaar18081e32008-02-20 19:11:07 +000012669/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012670 * "getpos(string)" function
12671 */
12672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012673f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012674{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012675 getpos_both(argvars, rettv, FALSE);
12676}
12677
12678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012679getpos_both(
12680 typval_T *argvars,
12681 typval_T *rettv,
12682 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012683{
Bram Moolenaara5525202006-03-02 22:52:09 +000012684 pos_T *fp;
12685 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012686 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012687
12688 if (rettv_list_alloc(rettv) == OK)
12689 {
12690 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012691 if (getcurpos)
12692 fp = &curwin->w_cursor;
12693 else
12694 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012695 if (fnum != -1)
12696 list_append_number(l, (varnumber_T)fnum);
12697 else
12698 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012699 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12700 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012701 list_append_number(l, (fp != NULL)
12702 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012703 : (varnumber_T)0);
12704 list_append_number(l,
12705#ifdef FEAT_VIRTUALEDIT
12706 (fp != NULL) ? (varnumber_T)fp->coladd :
12707#endif
12708 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012709 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012710 {
12711 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012712 list_append_number(l, curwin->w_curswant == MAXCOL ?
12713 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012714 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012715 }
12716 else
12717 rettv->vval.v_number = FALSE;
12718}
12719
12720/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012721 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012722 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012724f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012725{
12726#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012727 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012728#endif
12729
Bram Moolenaar2641f772005-03-25 21:58:17 +000012730#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012731 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012732 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012733 wp = NULL;
12734 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12735 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012736 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012737 if (wp == NULL)
12738 return;
12739 }
12740
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012741 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012742 }
12743#endif
12744}
12745
12746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 * "getreg()" function
12748 */
12749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012750f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751{
12752 char_u *strregname;
12753 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012754 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012755 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012756 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012758 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012760 strregname = get_tv_string_chk(&argvars[0]);
12761 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012762 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012764 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012765 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12766 return_list = get_tv_number_chk(&argvars[2], &error);
12767 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012770 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012771
12772 if (error)
12773 return;
12774
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 regname = (strregname == NULL ? '"' : *strregname);
12776 if (regname == 0)
12777 regname = '"';
12778
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012779 if (return_list)
12780 {
12781 rettv->v_type = VAR_LIST;
12782 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12783 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012784 if (rettv->vval.v_list != NULL)
12785 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012786 }
12787 else
12788 {
12789 rettv->v_type = VAR_STRING;
12790 rettv->vval.v_string = get_reg_contents(regname,
12791 arg2 ? GREG_EXPR_SRC : 0);
12792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793}
12794
12795/*
12796 * "getregtype()" function
12797 */
12798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012799f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800{
12801 char_u *strregname;
12802 int regname;
12803 char_u buf[NUMBUFLEN + 2];
12804 long reglen = 0;
12805
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012806 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012807 {
12808 strregname = get_tv_string_chk(&argvars[0]);
12809 if (strregname == NULL) /* type error; errmsg already given */
12810 {
12811 rettv->v_type = VAR_STRING;
12812 rettv->vval.v_string = NULL;
12813 return;
12814 }
12815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 else
12817 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012818 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819
12820 regname = (strregname == NULL ? '"' : *strregname);
12821 if (regname == 0)
12822 regname = '"';
12823
12824 buf[0] = NUL;
12825 buf[1] = NUL;
12826 switch (get_reg_type(regname, &reglen))
12827 {
12828 case MLINE: buf[0] = 'V'; break;
12829 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830 case MBLOCK:
12831 buf[0] = Ctrl_V;
12832 sprintf((char *)buf + 1, "%ld", reglen + 1);
12833 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 rettv->v_type = VAR_STRING;
12836 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837}
12838
12839/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012840 * "gettabvar()" function
12841 */
12842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012843f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012844{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012845 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012846 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012847 dictitem_T *v;
12848 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012849 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012850
12851 rettv->v_type = VAR_STRING;
12852 rettv->vval.v_string = NULL;
12853
12854 varname = get_tv_string_chk(&argvars[1]);
12855 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12856 if (tp != NULL && varname != NULL)
12857 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012858 /* Set tp to be our tabpage, temporarily. Also set the window to the
12859 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012860 if (switch_win(&oldcurwin, &oldtabpage,
12861 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012862 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012863 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012864 /* look up the variable */
12865 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12866 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12867 if (v != NULL)
12868 {
12869 copy_tv(&v->di_tv, rettv);
12870 done = TRUE;
12871 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012872 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012873
12874 /* restore previous notion of curwin */
12875 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012876 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012877
12878 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012879 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012880}
12881
12882/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012883 * "gettabwinvar()" function
12884 */
12885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012886f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012887{
12888 getwinvar(argvars, rettv, 1);
12889}
12890
12891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 * "getwinposx()" function
12893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012895f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898#ifdef FEAT_GUI
12899 if (gui.in_use)
12900 {
12901 int x, y;
12902
12903 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012904 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905 }
12906#endif
12907}
12908
12909/*
12910 * "getwinposy()" function
12911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012913f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916#ifdef FEAT_GUI
12917 if (gui.in_use)
12918 {
12919 int x, y;
12920
12921 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923 }
12924#endif
12925}
12926
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012927/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012928 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012929 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012930 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012931find_win_by_nr(
12932 typval_T *vp,
12933 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012934{
12935#ifdef FEAT_WINDOWS
12936 win_T *wp;
12937#endif
12938 int nr;
12939
12940 nr = get_tv_number_chk(vp, NULL);
12941
12942#ifdef FEAT_WINDOWS
12943 if (nr < 0)
12944 return NULL;
12945 if (nr == 0)
12946 return curwin;
12947
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012948 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12949 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012950 if (--nr <= 0)
12951 break;
12952 return wp;
12953#else
12954 if (nr == 0 || nr == 1)
12955 return curwin;
12956 return NULL;
12957#endif
12958}
12959
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012961 * Find window specified by "wvp" in tabpage "tvp".
12962 */
12963 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012964find_tabwin(
12965 typval_T *wvp, /* VAR_UNKNOWN for current window */
12966 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012967{
12968 win_T *wp = NULL;
12969 tabpage_T *tp = NULL;
12970 long n;
12971
12972 if (wvp->v_type != VAR_UNKNOWN)
12973 {
12974 if (tvp->v_type != VAR_UNKNOWN)
12975 {
12976 n = get_tv_number(tvp);
12977 if (n >= 0)
12978 tp = find_tabpage(n);
12979 }
12980 else
12981 tp = curtab;
12982
12983 if (tp != NULL)
12984 wp = find_win_by_nr(wvp, tp);
12985 }
12986 else
12987 wp = curwin;
12988
12989 return wp;
12990}
12991
12992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993 * "getwinvar()" function
12994 */
12995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012996f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012998 getwinvar(argvars, rettv, 0);
12999}
13000
13001/*
13002 * getwinvar() and gettabwinvar()
13003 */
13004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013005getwinvar(
13006 typval_T *argvars,
13007 typval_T *rettv,
13008 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013009{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013010 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013012 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013013 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013014 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013015#ifdef FEAT_WINDOWS
13016 win_T *oldcurwin;
13017 tabpage_T *oldtabpage;
13018 int need_switch_win;
13019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013021#ifdef FEAT_WINDOWS
13022 if (off == 1)
13023 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13024 else
13025 tp = curtab;
13026#endif
13027 win = find_win_by_nr(&argvars[off], tp);
13028 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013029 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013031 rettv->v_type = VAR_STRING;
13032 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033
13034 if (win != NULL && varname != NULL)
13035 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013036#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013037 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013038 * otherwise the window is not valid. Only do this when needed,
13039 * autocommands get blocked. */
13040 need_switch_win = !(tp == curtab && win == curwin);
13041 if (!need_switch_win
13042 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13043#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013044 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013045 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013046 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013047 if (get_option_tv(&varname, rettv, 1) == OK)
13048 done = TRUE;
13049 }
13050 else
13051 {
13052 /* Look up the variable. */
13053 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13054 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13055 varname, FALSE);
13056 if (v != NULL)
13057 {
13058 copy_tv(&v->di_tv, rettv);
13059 done = TRUE;
13060 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013063
Bram Moolenaarba117c22015-09-29 16:53:22 +020013064#ifdef FEAT_WINDOWS
13065 if (need_switch_win)
13066 /* restore previous notion of curwin */
13067 restore_win(oldcurwin, oldtabpage, TRUE);
13068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069 }
13070
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013071 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13072 /* use the default return value */
13073 copy_tv(&argvars[off + 2], rettv);
13074
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075 --emsg_off;
13076}
13077
13078/*
13079 * "glob()" function
13080 */
13081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013082f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013084 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013086 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013088 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013089 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013090 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013091 if (argvars[1].v_type != VAR_UNKNOWN)
13092 {
13093 if (get_tv_number_chk(&argvars[1], &error))
13094 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013095 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013096 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013097 if (get_tv_number_chk(&argvars[2], &error))
13098 {
13099 rettv->v_type = VAR_LIST;
13100 rettv->vval.v_list = NULL;
13101 }
13102 if (argvars[3].v_type != VAR_UNKNOWN
13103 && get_tv_number_chk(&argvars[3], &error))
13104 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013105 }
13106 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013107 if (!error)
13108 {
13109 ExpandInit(&xpc);
13110 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013111 if (p_wic)
13112 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013113 if (rettv->v_type == VAR_STRING)
13114 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013115 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013116 else if (rettv_list_alloc(rettv) != FAIL)
13117 {
13118 int i;
13119
13120 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13121 NULL, options, WILD_ALL_KEEP);
13122 for (i = 0; i < xpc.xp_numfiles; i++)
13123 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13124
13125 ExpandCleanup(&xpc);
13126 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013127 }
13128 else
13129 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130}
13131
13132/*
13133 * "globpath()" function
13134 */
13135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013136f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013138 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013140 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013141 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013142 garray_T ga;
13143 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013145 /* When the optional second argument is non-zero, don't remove matches
13146 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013147 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013148 if (argvars[2].v_type != VAR_UNKNOWN)
13149 {
13150 if (get_tv_number_chk(&argvars[2], &error))
13151 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013152 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013153 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013154 if (get_tv_number_chk(&argvars[3], &error))
13155 {
13156 rettv->v_type = VAR_LIST;
13157 rettv->vval.v_list = NULL;
13158 }
13159 if (argvars[4].v_type != VAR_UNKNOWN
13160 && get_tv_number_chk(&argvars[4], &error))
13161 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013162 }
13163 }
13164 if (file != NULL && !error)
13165 {
13166 ga_init2(&ga, (int)sizeof(char_u *), 10);
13167 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13168 if (rettv->v_type == VAR_STRING)
13169 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13170 else if (rettv_list_alloc(rettv) != FAIL)
13171 for (i = 0; i < ga.ga_len; ++i)
13172 list_append_string(rettv->vval.v_list,
13173 ((char_u **)(ga.ga_data))[i], -1);
13174 ga_clear_strings(&ga);
13175 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013176 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013177 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178}
13179
13180/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013181 * "glob2regpat()" function
13182 */
13183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013184f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013185{
13186 char_u *pat = get_tv_string_chk(&argvars[0]);
13187
13188 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013189 rettv->vval.v_string = (pat == NULL)
13190 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013191}
13192
13193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194 * "has()" function
13195 */
13196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013197f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198{
13199 int i;
13200 char_u *name;
13201 int n = FALSE;
13202 static char *(has_list[]) =
13203 {
13204#ifdef AMIGA
13205 "amiga",
13206# ifdef FEAT_ARP
13207 "arp",
13208# endif
13209#endif
13210#ifdef __BEOS__
13211 "beos",
13212#endif
13213#ifdef MSDOS
13214# ifdef DJGPP
13215 "dos32",
13216# else
13217 "dos16",
13218# endif
13219#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013220#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221 "mac",
13222#endif
13223#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013224 "macunix", /* built with 'darwin' enabled */
13225#endif
13226#if defined(__APPLE__) && __APPLE__ == 1
13227 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229#ifdef __QNX__
13230 "qnx",
13231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232#ifdef UNIX
13233 "unix",
13234#endif
13235#ifdef VMS
13236 "vms",
13237#endif
13238#ifdef WIN16
13239 "win16",
13240#endif
13241#ifdef WIN32
13242 "win32",
13243#endif
13244#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13245 "win32unix",
13246#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013247#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 "win64",
13249#endif
13250#ifdef EBCDIC
13251 "ebcdic",
13252#endif
13253#ifndef CASE_INSENSITIVE_FILENAME
13254 "fname_case",
13255#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013256#ifdef HAVE_ACL
13257 "acl",
13258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259#ifdef FEAT_ARABIC
13260 "arabic",
13261#endif
13262#ifdef FEAT_AUTOCMD
13263 "autocmd",
13264#endif
13265#ifdef FEAT_BEVAL
13266 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013267# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13268 "balloon_multiline",
13269# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270#endif
13271#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13272 "builtin_terms",
13273# ifdef ALL_BUILTIN_TCAPS
13274 "all_builtin_terms",
13275# endif
13276#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013277#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13278 || defined(FEAT_GUI_W32) \
13279 || defined(FEAT_GUI_MOTIF))
13280 "browsefilter",
13281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282#ifdef FEAT_BYTEOFF
13283 "byte_offset",
13284#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013285#ifdef FEAT_CHANNEL
13286 "channel",
13287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288#ifdef FEAT_CINDENT
13289 "cindent",
13290#endif
13291#ifdef FEAT_CLIENTSERVER
13292 "clientserver",
13293#endif
13294#ifdef FEAT_CLIPBOARD
13295 "clipboard",
13296#endif
13297#ifdef FEAT_CMDL_COMPL
13298 "cmdline_compl",
13299#endif
13300#ifdef FEAT_CMDHIST
13301 "cmdline_hist",
13302#endif
13303#ifdef FEAT_COMMENTS
13304 "comments",
13305#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013306#ifdef FEAT_CONCEAL
13307 "conceal",
13308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309#ifdef FEAT_CRYPT
13310 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013311 "crypt-blowfish",
13312 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313#endif
13314#ifdef FEAT_CSCOPE
13315 "cscope",
13316#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013317#ifdef FEAT_CURSORBIND
13318 "cursorbind",
13319#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013320#ifdef CURSOR_SHAPE
13321 "cursorshape",
13322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323#ifdef DEBUG
13324 "debug",
13325#endif
13326#ifdef FEAT_CON_DIALOG
13327 "dialog_con",
13328#endif
13329#ifdef FEAT_GUI_DIALOG
13330 "dialog_gui",
13331#endif
13332#ifdef FEAT_DIFF
13333 "diff",
13334#endif
13335#ifdef FEAT_DIGRAPHS
13336 "digraphs",
13337#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013338#ifdef FEAT_DIRECTX
13339 "directx",
13340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341#ifdef FEAT_DND
13342 "dnd",
13343#endif
13344#ifdef FEAT_EMACS_TAGS
13345 "emacs_tags",
13346#endif
13347 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013348 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349#ifdef FEAT_SEARCH_EXTRA
13350 "extra_search",
13351#endif
13352#ifdef FEAT_FKMAP
13353 "farsi",
13354#endif
13355#ifdef FEAT_SEARCHPATH
13356 "file_in_path",
13357#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013358#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013359 "filterpipe",
13360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361#ifdef FEAT_FIND_ID
13362 "find_in_path",
13363#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013364#ifdef FEAT_FLOAT
13365 "float",
13366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367#ifdef FEAT_FOLDING
13368 "folding",
13369#endif
13370#ifdef FEAT_FOOTER
13371 "footer",
13372#endif
13373#if !defined(USE_SYSTEM) && defined(UNIX)
13374 "fork",
13375#endif
13376#ifdef FEAT_GETTEXT
13377 "gettext",
13378#endif
13379#ifdef FEAT_GUI
13380 "gui",
13381#endif
13382#ifdef FEAT_GUI_ATHENA
13383# ifdef FEAT_GUI_NEXTAW
13384 "gui_neXtaw",
13385# else
13386 "gui_athena",
13387# endif
13388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389#ifdef FEAT_GUI_GTK
13390 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013393#ifdef FEAT_GUI_GNOME
13394 "gui_gnome",
13395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396#ifdef FEAT_GUI_MAC
13397 "gui_mac",
13398#endif
13399#ifdef FEAT_GUI_MOTIF
13400 "gui_motif",
13401#endif
13402#ifdef FEAT_GUI_PHOTON
13403 "gui_photon",
13404#endif
13405#ifdef FEAT_GUI_W16
13406 "gui_win16",
13407#endif
13408#ifdef FEAT_GUI_W32
13409 "gui_win32",
13410#endif
13411#ifdef FEAT_HANGULIN
13412 "hangul_input",
13413#endif
13414#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13415 "iconv",
13416#endif
13417#ifdef FEAT_INS_EXPAND
13418 "insert_expand",
13419#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013420#ifdef FEAT_JOB
13421 "job",
13422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423#ifdef FEAT_JUMPLIST
13424 "jumplist",
13425#endif
13426#ifdef FEAT_KEYMAP
13427 "keymap",
13428#endif
13429#ifdef FEAT_LANGMAP
13430 "langmap",
13431#endif
13432#ifdef FEAT_LIBCALL
13433 "libcall",
13434#endif
13435#ifdef FEAT_LINEBREAK
13436 "linebreak",
13437#endif
13438#ifdef FEAT_LISP
13439 "lispindent",
13440#endif
13441#ifdef FEAT_LISTCMDS
13442 "listcmds",
13443#endif
13444#ifdef FEAT_LOCALMAP
13445 "localmap",
13446#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013447#ifdef FEAT_LUA
13448# ifndef DYNAMIC_LUA
13449 "lua",
13450# endif
13451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452#ifdef FEAT_MENU
13453 "menu",
13454#endif
13455#ifdef FEAT_SESSION
13456 "mksession",
13457#endif
13458#ifdef FEAT_MODIFY_FNAME
13459 "modify_fname",
13460#endif
13461#ifdef FEAT_MOUSE
13462 "mouse",
13463#endif
13464#ifdef FEAT_MOUSESHAPE
13465 "mouseshape",
13466#endif
13467#if defined(UNIX) || defined(VMS)
13468# ifdef FEAT_MOUSE_DEC
13469 "mouse_dec",
13470# endif
13471# ifdef FEAT_MOUSE_GPM
13472 "mouse_gpm",
13473# endif
13474# ifdef FEAT_MOUSE_JSB
13475 "mouse_jsbterm",
13476# endif
13477# ifdef FEAT_MOUSE_NET
13478 "mouse_netterm",
13479# endif
13480# ifdef FEAT_MOUSE_PTERM
13481 "mouse_pterm",
13482# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013483# ifdef FEAT_MOUSE_SGR
13484 "mouse_sgr",
13485# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013486# ifdef FEAT_SYSMOUSE
13487 "mouse_sysmouse",
13488# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013489# ifdef FEAT_MOUSE_URXVT
13490 "mouse_urxvt",
13491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492# ifdef FEAT_MOUSE_XTERM
13493 "mouse_xterm",
13494# endif
13495#endif
13496#ifdef FEAT_MBYTE
13497 "multi_byte",
13498#endif
13499#ifdef FEAT_MBYTE_IME
13500 "multi_byte_ime",
13501#endif
13502#ifdef FEAT_MULTI_LANG
13503 "multi_lang",
13504#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013505#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013506#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013507 "mzscheme",
13508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510#ifdef FEAT_OLE
13511 "ole",
13512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513#ifdef FEAT_PATH_EXTRA
13514 "path_extra",
13515#endif
13516#ifdef FEAT_PERL
13517#ifndef DYNAMIC_PERL
13518 "perl",
13519#endif
13520#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013521#ifdef FEAT_PERSISTENT_UNDO
13522 "persistent_undo",
13523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524#ifdef FEAT_PYTHON
13525#ifndef DYNAMIC_PYTHON
13526 "python",
13527#endif
13528#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013529#ifdef FEAT_PYTHON3
13530#ifndef DYNAMIC_PYTHON3
13531 "python3",
13532#endif
13533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534#ifdef FEAT_POSTSCRIPT
13535 "postscript",
13536#endif
13537#ifdef FEAT_PRINTER
13538 "printer",
13539#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013540#ifdef FEAT_PROFILE
13541 "profile",
13542#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013543#ifdef FEAT_RELTIME
13544 "reltime",
13545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546#ifdef FEAT_QUICKFIX
13547 "quickfix",
13548#endif
13549#ifdef FEAT_RIGHTLEFT
13550 "rightleft",
13551#endif
13552#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13553 "ruby",
13554#endif
13555#ifdef FEAT_SCROLLBIND
13556 "scrollbind",
13557#endif
13558#ifdef FEAT_CMDL_INFO
13559 "showcmd",
13560 "cmdline_info",
13561#endif
13562#ifdef FEAT_SIGNS
13563 "signs",
13564#endif
13565#ifdef FEAT_SMARTINDENT
13566 "smartindent",
13567#endif
13568#ifdef FEAT_SNIFF
13569 "sniff",
13570#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013571#ifdef STARTUPTIME
13572 "startuptime",
13573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574#ifdef FEAT_STL_OPT
13575 "statusline",
13576#endif
13577#ifdef FEAT_SUN_WORKSHOP
13578 "sun_workshop",
13579#endif
13580#ifdef FEAT_NETBEANS_INTG
13581 "netbeans_intg",
13582#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013583#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013584 "spell",
13585#endif
13586#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 "syntax",
13588#endif
13589#if defined(USE_SYSTEM) || !defined(UNIX)
13590 "system",
13591#endif
13592#ifdef FEAT_TAG_BINS
13593 "tag_binary",
13594#endif
13595#ifdef FEAT_TAG_OLDSTATIC
13596 "tag_old_static",
13597#endif
13598#ifdef FEAT_TAG_ANYWHITE
13599 "tag_any_white",
13600#endif
13601#ifdef FEAT_TCL
13602# ifndef DYNAMIC_TCL
13603 "tcl",
13604# endif
13605#endif
13606#ifdef TERMINFO
13607 "terminfo",
13608#endif
13609#ifdef FEAT_TERMRESPONSE
13610 "termresponse",
13611#endif
13612#ifdef FEAT_TEXTOBJ
13613 "textobjects",
13614#endif
13615#ifdef HAVE_TGETENT
13616 "tgetent",
13617#endif
13618#ifdef FEAT_TITLE
13619 "title",
13620#endif
13621#ifdef FEAT_TOOLBAR
13622 "toolbar",
13623#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013624#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13625 "unnamedplus",
13626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627#ifdef FEAT_USR_CMDS
13628 "user-commands", /* was accidentally included in 5.4 */
13629 "user_commands",
13630#endif
13631#ifdef FEAT_VIMINFO
13632 "viminfo",
13633#endif
13634#ifdef FEAT_VERTSPLIT
13635 "vertsplit",
13636#endif
13637#ifdef FEAT_VIRTUALEDIT
13638 "virtualedit",
13639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641#ifdef FEAT_VISUALEXTRA
13642 "visualextra",
13643#endif
13644#ifdef FEAT_VREPLACE
13645 "vreplace",
13646#endif
13647#ifdef FEAT_WILDIGN
13648 "wildignore",
13649#endif
13650#ifdef FEAT_WILDMENU
13651 "wildmenu",
13652#endif
13653#ifdef FEAT_WINDOWS
13654 "windows",
13655#endif
13656#ifdef FEAT_WAK
13657 "winaltkeys",
13658#endif
13659#ifdef FEAT_WRITEBACKUP
13660 "writebackup",
13661#endif
13662#ifdef FEAT_XIM
13663 "xim",
13664#endif
13665#ifdef FEAT_XFONTSET
13666 "xfontset",
13667#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013668#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013669 "xpm",
13670 "xpm_w32", /* for backward compatibility */
13671#else
13672# if defined(HAVE_XPM)
13673 "xpm",
13674# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676#ifdef USE_XSMP
13677 "xsmp",
13678#endif
13679#ifdef USE_XSMP_INTERACT
13680 "xsmp_interact",
13681#endif
13682#ifdef FEAT_XCLIPBOARD
13683 "xterm_clipboard",
13684#endif
13685#ifdef FEAT_XTERM_SAVE
13686 "xterm_save",
13687#endif
13688#if defined(UNIX) && defined(FEAT_X11)
13689 "X11",
13690#endif
13691 NULL
13692 };
13693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013694 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 for (i = 0; has_list[i] != NULL; ++i)
13696 if (STRICMP(name, has_list[i]) == 0)
13697 {
13698 n = TRUE;
13699 break;
13700 }
13701
13702 if (n == FALSE)
13703 {
13704 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013705 {
13706 if (name[5] == '-'
13707 && STRLEN(name) > 11
13708 && vim_isdigit(name[6])
13709 && vim_isdigit(name[8])
13710 && vim_isdigit(name[10]))
13711 {
13712 int major = atoi((char *)name + 6);
13713 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013714
13715 /* Expect "patch-9.9.01234". */
13716 n = (major < VIM_VERSION_MAJOR
13717 || (major == VIM_VERSION_MAJOR
13718 && (minor < VIM_VERSION_MINOR
13719 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013720 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013721 }
13722 else
13723 n = has_patch(atoi((char *)name + 5));
13724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725 else if (STRICMP(name, "vim_starting") == 0)
13726 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013727#ifdef FEAT_MBYTE
13728 else if (STRICMP(name, "multi_byte_encoding") == 0)
13729 n = has_mbyte;
13730#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013731#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13732 else if (STRICMP(name, "balloon_multiline") == 0)
13733 n = multiline_balloon_available();
13734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735#ifdef DYNAMIC_TCL
13736 else if (STRICMP(name, "tcl") == 0)
13737 n = tcl_enabled(FALSE);
13738#endif
13739#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13740 else if (STRICMP(name, "iconv") == 0)
13741 n = iconv_enabled(FALSE);
13742#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013743#ifdef DYNAMIC_LUA
13744 else if (STRICMP(name, "lua") == 0)
13745 n = lua_enabled(FALSE);
13746#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013747#ifdef DYNAMIC_MZSCHEME
13748 else if (STRICMP(name, "mzscheme") == 0)
13749 n = mzscheme_enabled(FALSE);
13750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751#ifdef DYNAMIC_RUBY
13752 else if (STRICMP(name, "ruby") == 0)
13753 n = ruby_enabled(FALSE);
13754#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013755#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756#ifdef DYNAMIC_PYTHON
13757 else if (STRICMP(name, "python") == 0)
13758 n = python_enabled(FALSE);
13759#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013760#endif
13761#ifdef FEAT_PYTHON3
13762#ifdef DYNAMIC_PYTHON3
13763 else if (STRICMP(name, "python3") == 0)
13764 n = python3_enabled(FALSE);
13765#endif
13766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767#ifdef DYNAMIC_PERL
13768 else if (STRICMP(name, "perl") == 0)
13769 n = perl_enabled(FALSE);
13770#endif
13771#ifdef FEAT_GUI
13772 else if (STRICMP(name, "gui_running") == 0)
13773 n = (gui.in_use || gui.starting);
13774# ifdef FEAT_GUI_W32
13775 else if (STRICMP(name, "gui_win32s") == 0)
13776 n = gui_is_win32s();
13777# endif
13778# ifdef FEAT_BROWSE
13779 else if (STRICMP(name, "browse") == 0)
13780 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13781# endif
13782#endif
13783#ifdef FEAT_SYN_HL
13784 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013785 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786#endif
13787#if defined(WIN3264)
13788 else if (STRICMP(name, "win95") == 0)
13789 n = mch_windows95();
13790#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013791#ifdef FEAT_NETBEANS_INTG
13792 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013793 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 }
13796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013797 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798}
13799
13800/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013801 * "has_key()" function
13802 */
13803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013804f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013805{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013806 if (argvars[0].v_type != VAR_DICT)
13807 {
13808 EMSG(_(e_dictreq));
13809 return;
13810 }
13811 if (argvars[0].vval.v_dict == NULL)
13812 return;
13813
13814 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013815 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013816}
13817
13818/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013819 * "haslocaldir()" function
13820 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013822f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013823{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013824 win_T *wp = NULL;
13825
13826 wp = find_tabwin(&argvars[0], &argvars[1]);
13827 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013828}
13829
13830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 * "hasmapto()" function
13832 */
13833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013834f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835{
13836 char_u *name;
13837 char_u *mode;
13838 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013839 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013841 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013842 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 mode = (char_u *)"nvo";
13844 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013846 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013847 if (argvars[2].v_type != VAR_UNKNOWN)
13848 abbr = get_tv_number(&argvars[2]);
13849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850
Bram Moolenaar2c932302006-03-18 21:42:09 +000013851 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013852 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855}
13856
13857/*
13858 * "histadd()" function
13859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013861f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862{
13863#ifdef FEAT_CMDHIST
13864 int histype;
13865 char_u *str;
13866 char_u buf[NUMBUFLEN];
13867#endif
13868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013869 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 if (check_restricted() || check_secure())
13871 return;
13872#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13874 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 if (histype >= 0)
13876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013877 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878 if (*str != NUL)
13879 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013880 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013882 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 return;
13884 }
13885 }
13886#endif
13887}
13888
13889/*
13890 * "histdel()" function
13891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013893f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894{
13895#ifdef FEAT_CMDHIST
13896 int n;
13897 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013898 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013900 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13901 if (str == NULL)
13902 n = 0;
13903 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013905 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013906 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013908 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013909 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 else
13911 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013912 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013913 get_tv_string_buf(&argvars[1], buf));
13914 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915#endif
13916}
13917
13918/*
13919 * "histget()" function
13920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013922f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923{
13924#ifdef FEAT_CMDHIST
13925 int type;
13926 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013927 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013929 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13930 if (str == NULL)
13931 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013933 {
13934 type = get_histtype(str);
13935 if (argvars[1].v_type == VAR_UNKNOWN)
13936 idx = get_history_idx(type);
13937 else
13938 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13939 /* -1 on type error */
13940 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013943 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013945 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946}
13947
13948/*
13949 * "histnr()" function
13950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013952f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953{
13954 int i;
13955
13956#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013957 char_u *history = get_tv_string_chk(&argvars[0]);
13958
13959 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960 if (i >= HIST_CMD && i < HIST_COUNT)
13961 i = get_history_idx(i);
13962 else
13963#endif
13964 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013965 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966}
13967
13968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 * "highlightID(name)" function
13970 */
13971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013972f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013974 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975}
13976
13977/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013978 * "highlight_exists()" function
13979 */
13980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013981f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013982{
13983 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13984}
13985
13986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 * "hostname()" function
13988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013990f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991{
13992 char_u hostname[256];
13993
13994 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013995 rettv->v_type = VAR_STRING;
13996 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997}
13998
13999/*
14000 * iconv() function
14001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014003f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004{
14005#ifdef FEAT_MBYTE
14006 char_u buf1[NUMBUFLEN];
14007 char_u buf2[NUMBUFLEN];
14008 char_u *from, *to, *str;
14009 vimconv_T vimconv;
14010#endif
14011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014012 rettv->v_type = VAR_STRING;
14013 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014
14015#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014016 str = get_tv_string(&argvars[0]);
14017 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14018 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019 vimconv.vc_type = CONV_NONE;
14020 convert_setup(&vimconv, from, to);
14021
14022 /* If the encodings are equal, no conversion needed. */
14023 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014024 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014026 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027
14028 convert_setup(&vimconv, NULL, NULL);
14029 vim_free(from);
14030 vim_free(to);
14031#endif
14032}
14033
14034/*
14035 * "indent()" function
14036 */
14037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014038f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039{
14040 linenr_T lnum;
14041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014042 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014044 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014046 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047}
14048
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014049/*
14050 * "index()" function
14051 */
14052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014053f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014054{
Bram Moolenaar33570922005-01-25 22:26:29 +000014055 list_T *l;
14056 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014057 long idx = 0;
14058 int ic = FALSE;
14059
14060 rettv->vval.v_number = -1;
14061 if (argvars[0].v_type != VAR_LIST)
14062 {
14063 EMSG(_(e_listreq));
14064 return;
14065 }
14066 l = argvars[0].vval.v_list;
14067 if (l != NULL)
14068 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014069 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014070 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014071 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014072 int error = FALSE;
14073
Bram Moolenaar758711c2005-02-02 23:11:38 +000014074 /* Start at specified item. Use the cached index that list_find()
14075 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014076 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014077 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014078 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014079 ic = get_tv_number_chk(&argvars[3], &error);
14080 if (error)
14081 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014082 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014083
Bram Moolenaar758711c2005-02-02 23:11:38 +000014084 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014085 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014086 {
14087 rettv->vval.v_number = idx;
14088 break;
14089 }
14090 }
14091}
14092
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093static int inputsecret_flag = 0;
14094
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014095static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014096
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014098 * This function is used by f_input() and f_inputdialog() functions. The third
14099 * argument to f_input() specifies the type of completion to use at the
14100 * prompt. The third argument to f_inputdialog() specifies the value to return
14101 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102 */
14103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014104get_user_input(
14105 typval_T *argvars,
14106 typval_T *rettv,
14107 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014109 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110 char_u *p = NULL;
14111 int c;
14112 char_u buf[NUMBUFLEN];
14113 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014114 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014115 int xp_type = EXPAND_NOTHING;
14116 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014118 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014119 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120
14121#ifdef NO_CONSOLE_INPUT
14122 /* While starting up, there is no place to enter text. */
14123 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125#endif
14126
14127 cmd_silent = FALSE; /* Want to see the prompt. */
14128 if (prompt != NULL)
14129 {
14130 /* Only the part of the message after the last NL is considered as
14131 * prompt for the command line */
14132 p = vim_strrchr(prompt, '\n');
14133 if (p == NULL)
14134 p = prompt;
14135 else
14136 {
14137 ++p;
14138 c = *p;
14139 *p = NUL;
14140 msg_start();
14141 msg_clr_eos();
14142 msg_puts_attr(prompt, echo_attr);
14143 msg_didout = FALSE;
14144 msg_starthere();
14145 *p = c;
14146 }
14147 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 if (argvars[1].v_type != VAR_UNKNOWN)
14150 {
14151 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14152 if (defstr != NULL)
14153 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014155 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014156 {
14157 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014158 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014159 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014160
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014161 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014162 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014163
Bram Moolenaar4463f292005-09-25 22:20:24 +000014164 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14165 if (xp_name == NULL)
14166 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014167
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014168 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014169
Bram Moolenaar4463f292005-09-25 22:20:24 +000014170 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14171 &xp_arg) == FAIL)
14172 return;
14173 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014174 }
14175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014176 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014177 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014178 int save_ex_normal_busy = ex_normal_busy;
14179 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014181 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14182 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014183 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014184 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014185 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014186 && argvars[1].v_type != VAR_UNKNOWN
14187 && argvars[2].v_type != VAR_UNKNOWN)
14188 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14189 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014190
14191 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 /* since the user typed this, no need to wait for return */
14194 need_wait_return = FALSE;
14195 msg_didout = FALSE;
14196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197 cmd_silent = cmd_silent_save;
14198}
14199
14200/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014201 * "input()" function
14202 * Also handles inputsecret() when inputsecret is set.
14203 */
14204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014205f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014206{
14207 get_user_input(argvars, rettv, FALSE);
14208}
14209
14210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211 * "inputdialog()" function
14212 */
14213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014214f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215{
14216#if defined(FEAT_GUI_TEXTDIALOG)
14217 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14218 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14219 {
14220 char_u *message;
14221 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014222 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014224 message = get_tv_string_chk(&argvars[0]);
14225 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014226 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014227 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228 else
14229 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230 if (message != NULL && defstr != NULL
14231 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014232 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014233 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 else
14235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 if (message != NULL && defstr != NULL
14237 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014238 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239 rettv->vval.v_string = vim_strsave(
14240 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014244 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245 }
14246 else
14247#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014248 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249}
14250
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014251/*
14252 * "inputlist()" function
14253 */
14254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014255f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014256{
14257 listitem_T *li;
14258 int selected;
14259 int mouse_used;
14260
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014261#ifdef NO_CONSOLE_INPUT
14262 /* While starting up, there is no place to enter text. */
14263 if (no_console_input())
14264 return;
14265#endif
14266 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14267 {
14268 EMSG2(_(e_listarg), "inputlist()");
14269 return;
14270 }
14271
14272 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014273 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014274 lines_left = Rows; /* avoid more prompt */
14275 msg_scroll = TRUE;
14276 msg_clr_eos();
14277
14278 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14279 {
14280 msg_puts(get_tv_string(&li->li_tv));
14281 msg_putchar('\n');
14282 }
14283
14284 /* Ask for choice. */
14285 selected = prompt_for_number(&mouse_used);
14286 if (mouse_used)
14287 selected -= lines_left;
14288
14289 rettv->vval.v_number = selected;
14290}
14291
14292
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14294
14295/*
14296 * "inputrestore()" function
14297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014299f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300{
14301 if (ga_userinput.ga_len > 0)
14302 {
14303 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14305 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014306 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 }
14308 else if (p_verbose > 1)
14309 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014310 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014311 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 }
14313}
14314
14315/*
14316 * "inputsave()" function
14317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014319f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014321 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 if (ga_grow(&ga_userinput, 1) == OK)
14323 {
14324 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14325 + ga_userinput.ga_len);
14326 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014327 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 }
14329 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014330 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331}
14332
14333/*
14334 * "inputsecret()" function
14335 */
14336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014337f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338{
14339 ++cmdline_star;
14340 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014341 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 --cmdline_star;
14343 --inputsecret_flag;
14344}
14345
14346/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014347 * "insert()" function
14348 */
14349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014350f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014351{
14352 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014353 listitem_T *item;
14354 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014355 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014356
14357 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014358 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014359 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014360 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014361 {
14362 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 before = get_tv_number_chk(&argvars[2], &error);
14364 if (error)
14365 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014366
Bram Moolenaar758711c2005-02-02 23:11:38 +000014367 if (before == l->lv_len)
14368 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014369 else
14370 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014371 item = list_find(l, before);
14372 if (item == NULL)
14373 {
14374 EMSGN(_(e_listidx), before);
14375 l = NULL;
14376 }
14377 }
14378 if (l != NULL)
14379 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014380 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014381 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014382 }
14383 }
14384}
14385
14386/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014387 * "invert(expr)" function
14388 */
14389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014390f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014391{
14392 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14393}
14394
14395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396 * "isdirectory()" function
14397 */
14398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014399f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014401 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402}
14403
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014404/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014405 * "islocked()" function
14406 */
14407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014408f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014409{
14410 lval_T lv;
14411 char_u *end;
14412 dictitem_T *di;
14413
14414 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014415 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14416 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014417 if (end != NULL && lv.ll_name != NULL)
14418 {
14419 if (*end != NUL)
14420 EMSG(_(e_trailing));
14421 else
14422 {
14423 if (lv.ll_tv == NULL)
14424 {
14425 if (check_changedtick(lv.ll_name))
14426 rettv->vval.v_number = 1; /* always locked */
14427 else
14428 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014429 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014430 if (di != NULL)
14431 {
14432 /* Consider a variable locked when:
14433 * 1. the variable itself is locked
14434 * 2. the value of the variable is locked.
14435 * 3. the List or Dict value is locked.
14436 */
14437 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14438 || tv_islocked(&di->di_tv));
14439 }
14440 }
14441 }
14442 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014443 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014444 else if (lv.ll_newkey != NULL)
14445 EMSG2(_(e_dictkey), lv.ll_newkey);
14446 else if (lv.ll_list != NULL)
14447 /* List item. */
14448 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14449 else
14450 /* Dictionary item. */
14451 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14452 }
14453 }
14454
14455 clear_lval(&lv);
14456}
14457
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014458static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014459
14460/*
14461 * Turn a dict into a list:
14462 * "what" == 0: list of keys
14463 * "what" == 1: list of values
14464 * "what" == 2: list of items
14465 */
14466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014467dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014468{
Bram Moolenaar33570922005-01-25 22:26:29 +000014469 list_T *l2;
14470 dictitem_T *di;
14471 hashitem_T *hi;
14472 listitem_T *li;
14473 listitem_T *li2;
14474 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014475 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014476
Bram Moolenaar8c711452005-01-14 21:53:12 +000014477 if (argvars[0].v_type != VAR_DICT)
14478 {
14479 EMSG(_(e_dictreq));
14480 return;
14481 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014482 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014483 return;
14484
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014485 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014486 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014487
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014488 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014489 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014490 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014491 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014492 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014493 --todo;
14494 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014495
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014496 li = listitem_alloc();
14497 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014498 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014499 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014500
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014501 if (what == 0)
14502 {
14503 /* keys() */
14504 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014505 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014506 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14507 }
14508 else if (what == 1)
14509 {
14510 /* values() */
14511 copy_tv(&di->di_tv, &li->li_tv);
14512 }
14513 else
14514 {
14515 /* items() */
14516 l2 = list_alloc();
14517 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014518 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014519 li->li_tv.vval.v_list = l2;
14520 if (l2 == NULL)
14521 break;
14522 ++l2->lv_refcount;
14523
14524 li2 = listitem_alloc();
14525 if (li2 == NULL)
14526 break;
14527 list_append(l2, li2);
14528 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014529 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014530 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14531
14532 li2 = listitem_alloc();
14533 if (li2 == NULL)
14534 break;
14535 list_append(l2, li2);
14536 copy_tv(&di->di_tv, &li2->li_tv);
14537 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014538 }
14539 }
14540}
14541
14542/*
14543 * "items(dict)" function
14544 */
14545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014546f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014547{
14548 dict_list(argvars, rettv, 2);
14549}
14550
Bram Moolenaar835dc632016-02-07 14:27:38 +010014551#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014552
14553# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014554/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014555 * "job_getchannel()" function
14556 */
14557 static void
14558f_job_getchannel(typval_T *argvars, typval_T *rettv)
14559{
14560 if (argvars[0].v_type != VAR_JOB)
14561 EMSG(_(e_invarg));
14562 else
14563 {
14564 job_T *job = argvars[0].vval.v_job;
14565
Bram Moolenaar77073442016-02-13 23:23:53 +010014566 rettv->v_type = VAR_CHANNEL;
14567 rettv->vval.v_channel = job->jv_channel;
14568 if (job->jv_channel != NULL)
14569 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014570 }
14571}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014572# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014573
14574/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014575 * "job_start()" function
14576 */
14577 static void
14578f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14579{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014580 job_T *job;
14581 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014582#if defined(UNIX)
14583# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014584 char **argv = NULL;
14585 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014586#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014587 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014588#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014589 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014590
14591 rettv->v_type = VAR_JOB;
14592 job = job_alloc();
14593 rettv->vval.v_job = job;
14594 if (job == NULL)
14595 return;
14596
14597 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014598
14599 /* Default mode is NL. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014600 opt.jo_mode = MODE_NL;
14601 opt.jo_callback = NULL;
14602 if (get_job_options(&argvars[1], &opt, JO_MODE + JO_CALLBACK) == FAIL)
14603 return;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014604
Bram Moolenaar835dc632016-02-07 14:27:38 +010014605#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014606 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014607#endif
14608
14609 if (argvars[0].v_type == VAR_STRING)
14610 {
14611 /* Command is a string. */
14612 cmd = argvars[0].vval.v_string;
14613#ifdef USE_ARGV
14614 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14615 return;
14616 argv[argc] = NULL;
14617#endif
14618 }
14619 else if (argvars[0].v_type != VAR_LIST
14620 || argvars[0].vval.v_list == NULL
14621 || argvars[0].vval.v_list->lv_len < 1)
14622 {
14623 EMSG(_(e_invarg));
14624 return;
14625 }
14626 else
14627 {
14628 list_T *l = argvars[0].vval.v_list;
14629 listitem_T *li;
14630 char_u *s;
14631
14632#ifdef USE_ARGV
14633 /* Pass argv[] to mch_call_shell(). */
14634 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14635 if (argv == NULL)
14636 return;
14637#endif
14638 for (li = l->lv_first; li != NULL; li = li->li_next)
14639 {
14640 s = get_tv_string_chk(&li->li_tv);
14641 if (s == NULL)
14642 goto theend;
14643#ifdef USE_ARGV
14644 argv[argc++] = (char *)s;
14645#else
14646 if (li != l->lv_first)
14647 {
14648 s = vim_strsave_shellescape(s, FALSE, TRUE);
14649 if (s == NULL)
14650 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014651 ga_concat(&ga, s);
14652 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014653 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014654 else
14655 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014656 if (li->li_next != NULL)
14657 ga_append(&ga, ' ');
14658#endif
14659 }
14660#ifdef USE_ARGV
14661 argv[argc] = NULL;
14662#else
14663 cmd = ga.ga_data;
14664#endif
14665 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014666
Bram Moolenaar835dc632016-02-07 14:27:38 +010014667#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014668# ifdef FEAT_CHANNEL
14669 if (ch_log_active())
14670 {
14671 garray_T ga;
14672 int i;
14673
14674 ga_init2(&ga, (int)sizeof(char), 200);
14675 for (i = 0; i < argc; ++i)
14676 {
14677 if (i > 0)
14678 ga_concat(&ga, (char_u *)" ");
14679 ga_concat(&ga, (char_u *)argv[i]);
14680 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010014681 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014682 ga_clear(&ga);
14683 }
14684# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014685 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014686#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014687# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010014688 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014689# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014690 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014691#endif
14692
14693theend:
14694#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014695 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014696#else
14697 vim_free(ga.ga_data);
14698#endif
14699}
14700
14701/*
14702 * "job_status()" function
14703 */
14704 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014705f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014706{
14707 char *result;
14708
14709 if (argvars[0].v_type != VAR_JOB)
14710 EMSG(_(e_invarg));
14711 else
14712 {
14713 job_T *job = argvars[0].vval.v_job;
14714
14715 if (job->jv_status == JOB_ENDED)
14716 /* No need to check, dead is dead. */
14717 result = "dead";
14718 else if (job->jv_status == JOB_FAILED)
14719 result = "fail";
14720 else
14721 result = mch_job_status(job);
14722 rettv->v_type = VAR_STRING;
14723 rettv->vval.v_string = vim_strsave((char_u *)result);
14724 }
14725}
14726
14727/*
14728 * "job_stop()" function
14729 */
14730 static void
14731f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14732{
14733 if (argvars[0].v_type != VAR_JOB)
14734 EMSG(_(e_invarg));
14735 else
14736 {
14737 char_u *arg;
14738
14739 if (argvars[1].v_type == VAR_UNKNOWN)
14740 arg = (char_u *)"";
14741 else
14742 {
14743 arg = get_tv_string_chk(&argvars[1]);
14744 if (arg == NULL)
14745 {
14746 EMSG(_(e_invarg));
14747 return;
14748 }
14749 }
14750 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14751 rettv->vval.v_number = 0;
14752 else
14753 rettv->vval.v_number = 1;
14754 }
14755}
14756#endif
14757
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014759 * "join()" function
14760 */
14761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014762f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014763{
14764 garray_T ga;
14765 char_u *sep;
14766
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014767 if (argvars[0].v_type != VAR_LIST)
14768 {
14769 EMSG(_(e_listreq));
14770 return;
14771 }
14772 if (argvars[0].vval.v_list == NULL)
14773 return;
14774 if (argvars[1].v_type == VAR_UNKNOWN)
14775 sep = (char_u *)" ";
14776 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014777 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014778
14779 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014780
14781 if (sep != NULL)
14782 {
14783 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014784 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014785 ga_append(&ga, NUL);
14786 rettv->vval.v_string = (char_u *)ga.ga_data;
14787 }
14788 else
14789 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014790}
14791
14792/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014793 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014794 */
14795 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014796f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014797{
14798 js_read_T reader;
14799
14800 reader.js_buf = get_tv_string(&argvars[0]);
14801 reader.js_fill = NULL;
14802 reader.js_used = 0;
14803 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14804 EMSG(_(e_invarg));
14805}
14806
14807/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014808 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014809 */
14810 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014811f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014812{
14813 rettv->v_type = VAR_STRING;
14814 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14815}
14816
14817/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014818 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014819 */
14820 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014821f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014822{
14823 js_read_T reader;
14824
14825 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014826 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014827 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014828 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014829 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014830}
14831
14832/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014833 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014834 */
14835 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014836f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014837{
14838 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014839 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014840}
14841
14842/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014843 * "keys()" function
14844 */
14845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014846f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014847{
14848 dict_list(argvars, rettv, 0);
14849}
14850
14851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 * "last_buffer_nr()" function.
14853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014855f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856{
14857 int n = 0;
14858 buf_T *buf;
14859
14860 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14861 if (n < buf->b_fnum)
14862 n = buf->b_fnum;
14863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014864 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014865}
14866
14867/*
14868 * "len()" function
14869 */
14870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014871f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014872{
14873 switch (argvars[0].v_type)
14874 {
14875 case VAR_STRING:
14876 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014877 rettv->vval.v_number = (varnumber_T)STRLEN(
14878 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014879 break;
14880 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014881 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014882 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014883 case VAR_DICT:
14884 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14885 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014886 case VAR_UNKNOWN:
14887 case VAR_SPECIAL:
14888 case VAR_FLOAT:
14889 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014890 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014891 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014892 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014893 break;
14894 }
14895}
14896
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014897static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014898
14899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014900libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014901{
14902#ifdef FEAT_LIBCALL
14903 char_u *string_in;
14904 char_u **string_result;
14905 int nr_result;
14906#endif
14907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014908 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014909 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014910 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014911
14912 if (check_restricted() || check_secure())
14913 return;
14914
14915#ifdef FEAT_LIBCALL
14916 /* The first two args must be strings, otherwise its meaningless */
14917 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14918 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014919 string_in = NULL;
14920 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014921 string_in = argvars[2].vval.v_string;
14922 if (type == VAR_NUMBER)
14923 string_result = NULL;
14924 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014925 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014926 if (mch_libcall(argvars[0].vval.v_string,
14927 argvars[1].vval.v_string,
14928 string_in,
14929 argvars[2].vval.v_number,
14930 string_result,
14931 &nr_result) == OK
14932 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014933 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014934 }
14935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936}
14937
14938/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014939 * "libcall()" function
14940 */
14941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014942f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014943{
14944 libcall_common(argvars, rettv, VAR_STRING);
14945}
14946
14947/*
14948 * "libcallnr()" function
14949 */
14950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014951f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014952{
14953 libcall_common(argvars, rettv, VAR_NUMBER);
14954}
14955
14956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957 * "line(string)" function
14958 */
14959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014960f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961{
14962 linenr_T lnum = 0;
14963 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014964 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014966 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967 if (fp != NULL)
14968 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014969 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970}
14971
14972/*
14973 * "line2byte(lnum)" function
14974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014976f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977{
14978#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014979 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980#else
14981 linenr_T lnum;
14982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014983 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014985 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014987 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14988 if (rettv->vval.v_number >= 0)
14989 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990#endif
14991}
14992
14993/*
14994 * "lispindent(lnum)" function
14995 */
14996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014997f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998{
14999#ifdef FEAT_LISP
15000 pos_T pos;
15001 linenr_T lnum;
15002
15003 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015004 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15006 {
15007 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015008 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 curwin->w_cursor = pos;
15010 }
15011 else
15012#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015013 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014}
15015
15016/*
15017 * "localtime()" function
15018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015020f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015022 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023}
15024
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015025static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026
15027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015028get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029{
15030 char_u *keys;
15031 char_u *which;
15032 char_u buf[NUMBUFLEN];
15033 char_u *keys_buf = NULL;
15034 char_u *rhs;
15035 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015036 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015037 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015038 mapblock_T *mp;
15039 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040
15041 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015042 rettv->v_type = VAR_STRING;
15043 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015045 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015046 if (*keys == NUL)
15047 return;
15048
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015049 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015051 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015052 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015053 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015054 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015055 if (argvars[3].v_type != VAR_UNKNOWN)
15056 get_dict = get_tv_number(&argvars[3]);
15057 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059 else
15060 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015061 if (which == NULL)
15062 return;
15063
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 mode = get_map_mode(&which, 0);
15065
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015066 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015067 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015069
15070 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015072 /* Return a string. */
15073 if (rhs != NULL)
15074 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075
Bram Moolenaarbd743252010-10-20 21:23:33 +020015076 }
15077 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15078 {
15079 /* Return a dictionary. */
15080 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15081 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15082 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083
Bram Moolenaarbd743252010-10-20 21:23:33 +020015084 dict_add_nr_str(dict, "lhs", 0L, lhs);
15085 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15086 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15087 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15088 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15089 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15090 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015091 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015092 dict_add_nr_str(dict, "mode", 0L, mapmode);
15093
15094 vim_free(lhs);
15095 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015096 }
15097}
15098
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015099#ifdef FEAT_FLOAT
15100/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015101 * "log()" function
15102 */
15103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015104f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015105{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015106 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015107
15108 rettv->v_type = VAR_FLOAT;
15109 if (get_float_arg(argvars, &f) == OK)
15110 rettv->vval.v_float = log(f);
15111 else
15112 rettv->vval.v_float = 0.0;
15113}
15114
15115/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015116 * "log10()" function
15117 */
15118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015119f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015120{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015121 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015122
15123 rettv->v_type = VAR_FLOAT;
15124 if (get_float_arg(argvars, &f) == OK)
15125 rettv->vval.v_float = log10(f);
15126 else
15127 rettv->vval.v_float = 0.0;
15128}
15129#endif
15130
Bram Moolenaar1dced572012-04-05 16:54:08 +020015131#ifdef FEAT_LUA
15132/*
15133 * "luaeval()" function
15134 */
15135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015136f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015137{
15138 char_u *str;
15139 char_u buf[NUMBUFLEN];
15140
15141 str = get_tv_string_buf(&argvars[0], buf);
15142 do_luaeval(str, argvars + 1, rettv);
15143}
15144#endif
15145
Bram Moolenaar071d4272004-06-13 20:20:40 +000015146/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015147 * "map()" function
15148 */
15149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015150f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015151{
15152 filter_map(argvars, rettv, TRUE);
15153}
15154
15155/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 */
15158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015159f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015161 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162}
15163
15164/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015165 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166 */
15167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015168f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015170 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171}
15172
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015173static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015174
15175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015176find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015178 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015179 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015180 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181 char_u *pat;
15182 regmatch_T regmatch;
15183 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015184 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 char_u *save_cpo;
15186 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015187 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015188 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015189 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015190 list_T *l = NULL;
15191 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015192 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015193 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194
15195 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15196 save_cpo = p_cpo;
15197 p_cpo = (char_u *)"";
15198
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015199 rettv->vval.v_number = -1;
15200 if (type == 3)
15201 {
15202 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015203 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015204 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015205 }
15206 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015208 rettv->v_type = VAR_STRING;
15209 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015212 if (argvars[0].v_type == VAR_LIST)
15213 {
15214 if ((l = argvars[0].vval.v_list) == NULL)
15215 goto theend;
15216 li = l->lv_first;
15217 }
15218 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015219 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015220 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015221 len = (long)STRLEN(str);
15222 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015224 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15225 if (pat == NULL)
15226 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015227
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015228 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015230 int error = FALSE;
15231
15232 start = get_tv_number_chk(&argvars[2], &error);
15233 if (error)
15234 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015235 if (l != NULL)
15236 {
15237 li = list_find(l, start);
15238 if (li == NULL)
15239 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015240 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015241 }
15242 else
15243 {
15244 if (start < 0)
15245 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015246 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015247 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015248 /* When "count" argument is there ignore matches before "start",
15249 * otherwise skip part of the string. Differs when pattern is "^"
15250 * or "\<". */
15251 if (argvars[3].v_type != VAR_UNKNOWN)
15252 startcol = start;
15253 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015254 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015255 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015256 len -= start;
15257 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015258 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015259
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015260 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015261 nth = get_tv_number_chk(&argvars[3], &error);
15262 if (error)
15263 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 }
15265
15266 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15267 if (regmatch.regprog != NULL)
15268 {
15269 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015270
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015271 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015272 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015273 if (l != NULL)
15274 {
15275 if (li == NULL)
15276 {
15277 match = FALSE;
15278 break;
15279 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015280 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015281 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015282 if (str == NULL)
15283 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015284 }
15285
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015286 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015287
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015288 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015289 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015290 if (l == NULL && !match)
15291 break;
15292
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015293 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015294 if (l != NULL)
15295 {
15296 li = li->li_next;
15297 ++idx;
15298 }
15299 else
15300 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015301#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015302 startcol = (colnr_T)(regmatch.startp[0]
15303 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015304#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015305 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015306#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015307 if (startcol > (colnr_T)len
15308 || str + startcol <= regmatch.startp[0])
15309 {
15310 match = FALSE;
15311 break;
15312 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015313 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015314 }
15315
15316 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015318 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015319 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015320 int i;
15321
15322 /* return list with matched string and submatches */
15323 for (i = 0; i < NSUBEXP; ++i)
15324 {
15325 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015326 {
15327 if (list_append_string(rettv->vval.v_list,
15328 (char_u *)"", 0) == FAIL)
15329 break;
15330 }
15331 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015332 regmatch.startp[i],
15333 (int)(regmatch.endp[i] - regmatch.startp[i]))
15334 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015335 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015336 }
15337 }
15338 else if (type == 2)
15339 {
15340 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015341 if (l != NULL)
15342 copy_tv(&li->li_tv, rettv);
15343 else
15344 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015346 }
15347 else if (l != NULL)
15348 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349 else
15350 {
15351 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015352 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 (varnumber_T)(regmatch.startp[0] - str);
15354 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015355 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015357 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358 }
15359 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015360 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 }
15362
15363theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015364 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 p_cpo = save_cpo;
15366}
15367
15368/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015369 * "match()" function
15370 */
15371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015372f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015373{
15374 find_some_match(argvars, rettv, 1);
15375}
15376
15377/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015378 * "matchadd()" function
15379 */
15380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015381f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015382{
15383#ifdef FEAT_SEARCH_EXTRA
15384 char_u buf[NUMBUFLEN];
15385 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15386 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15387 int prio = 10; /* default priority */
15388 int id = -1;
15389 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015390 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015391
15392 rettv->vval.v_number = -1;
15393
15394 if (grp == NULL || pat == NULL)
15395 return;
15396 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015397 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015398 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015399 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015400 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015401 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015402 if (argvars[4].v_type != VAR_UNKNOWN)
15403 {
15404 if (argvars[4].v_type != VAR_DICT)
15405 {
15406 EMSG(_(e_dictreq));
15407 return;
15408 }
15409 if (dict_find(argvars[4].vval.v_dict,
15410 (char_u *)"conceal", -1) != NULL)
15411 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15412 (char_u *)"conceal", FALSE);
15413 }
15414 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015415 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015416 if (error == TRUE)
15417 return;
15418 if (id >= 1 && id <= 3)
15419 {
15420 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15421 return;
15422 }
15423
Bram Moolenaar6561d522015-07-21 15:48:27 +020015424 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15425 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015426#endif
15427}
15428
15429/*
15430 * "matchaddpos()" function
15431 */
15432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015433f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015434{
15435#ifdef FEAT_SEARCH_EXTRA
15436 char_u buf[NUMBUFLEN];
15437 char_u *group;
15438 int prio = 10;
15439 int id = -1;
15440 int error = FALSE;
15441 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015442 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015443
15444 rettv->vval.v_number = -1;
15445
15446 group = get_tv_string_buf_chk(&argvars[0], buf);
15447 if (group == NULL)
15448 return;
15449
15450 if (argvars[1].v_type != VAR_LIST)
15451 {
15452 EMSG2(_(e_listarg), "matchaddpos()");
15453 return;
15454 }
15455 l = argvars[1].vval.v_list;
15456 if (l == NULL)
15457 return;
15458
15459 if (argvars[2].v_type != VAR_UNKNOWN)
15460 {
15461 prio = get_tv_number_chk(&argvars[2], &error);
15462 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015463 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015464 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015465 if (argvars[4].v_type != VAR_UNKNOWN)
15466 {
15467 if (argvars[4].v_type != VAR_DICT)
15468 {
15469 EMSG(_(e_dictreq));
15470 return;
15471 }
15472 if (dict_find(argvars[4].vval.v_dict,
15473 (char_u *)"conceal", -1) != NULL)
15474 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15475 (char_u *)"conceal", FALSE);
15476 }
15477 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015478 }
15479 if (error == TRUE)
15480 return;
15481
15482 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15483 if (id == 1 || id == 2)
15484 {
15485 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15486 return;
15487 }
15488
Bram Moolenaar6561d522015-07-21 15:48:27 +020015489 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15490 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015491#endif
15492}
15493
15494/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015495 * "matcharg()" function
15496 */
15497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015498f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015499{
15500 if (rettv_list_alloc(rettv) == OK)
15501 {
15502#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015503 int id = get_tv_number(&argvars[0]);
15504 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015505
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015506 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015507 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015508 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15509 {
15510 list_append_string(rettv->vval.v_list,
15511 syn_id2name(m->hlg_id), -1);
15512 list_append_string(rettv->vval.v_list, m->pattern, -1);
15513 }
15514 else
15515 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015516 list_append_string(rettv->vval.v_list, NULL, -1);
15517 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015518 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015519 }
15520#endif
15521 }
15522}
15523
15524/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015525 * "matchdelete()" function
15526 */
15527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015528f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015529{
15530#ifdef FEAT_SEARCH_EXTRA
15531 rettv->vval.v_number = match_delete(curwin,
15532 (int)get_tv_number(&argvars[0]), TRUE);
15533#endif
15534}
15535
15536/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015537 * "matchend()" function
15538 */
15539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015540f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015541{
15542 find_some_match(argvars, rettv, 0);
15543}
15544
15545/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015546 * "matchlist()" function
15547 */
15548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015549f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015550{
15551 find_some_match(argvars, rettv, 3);
15552}
15553
15554/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015555 * "matchstr()" function
15556 */
15557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015558f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015559{
15560 find_some_match(argvars, rettv, 2);
15561}
15562
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015563static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015564
15565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015566max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015567{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015568 long n = 0;
15569 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015570 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015571
15572 if (argvars[0].v_type == VAR_LIST)
15573 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015574 list_T *l;
15575 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015576
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015577 l = argvars[0].vval.v_list;
15578 if (l != NULL)
15579 {
15580 li = l->lv_first;
15581 if (li != NULL)
15582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015583 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015584 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015585 {
15586 li = li->li_next;
15587 if (li == NULL)
15588 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015589 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015590 if (domax ? i > n : i < n)
15591 n = i;
15592 }
15593 }
15594 }
15595 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015596 else if (argvars[0].v_type == VAR_DICT)
15597 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015598 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015599 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015600 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015601 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015602
15603 d = argvars[0].vval.v_dict;
15604 if (d != NULL)
15605 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015606 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015607 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015608 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015609 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015610 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015611 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015612 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015613 if (first)
15614 {
15615 n = i;
15616 first = FALSE;
15617 }
15618 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015619 n = i;
15620 }
15621 }
15622 }
15623 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015624 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015625 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015626 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015627}
15628
15629/*
15630 * "max()" function
15631 */
15632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015633f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015634{
15635 max_min(argvars, rettv, TRUE);
15636}
15637
15638/*
15639 * "min()" function
15640 */
15641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015642f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015643{
15644 max_min(argvars, rettv, FALSE);
15645}
15646
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015647static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015648
15649/*
15650 * Create the directory in which "dir" is located, and higher levels when
15651 * needed.
15652 */
15653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015654mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015655{
15656 char_u *p;
15657 char_u *updir;
15658 int r = FAIL;
15659
15660 /* Get end of directory name in "dir".
15661 * We're done when it's "/" or "c:/". */
15662 p = gettail_sep(dir);
15663 if (p <= get_past_head(dir))
15664 return OK;
15665
15666 /* If the directory exists we're done. Otherwise: create it.*/
15667 updir = vim_strnsave(dir, (int)(p - dir));
15668 if (updir == NULL)
15669 return FAIL;
15670 if (mch_isdir(updir))
15671 r = OK;
15672 else if (mkdir_recurse(updir, prot) == OK)
15673 r = vim_mkdir_emsg(updir, prot);
15674 vim_free(updir);
15675 return r;
15676}
15677
15678#ifdef vim_mkdir
15679/*
15680 * "mkdir()" function
15681 */
15682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015683f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015684{
15685 char_u *dir;
15686 char_u buf[NUMBUFLEN];
15687 int prot = 0755;
15688
15689 rettv->vval.v_number = FAIL;
15690 if (check_restricted() || check_secure())
15691 return;
15692
15693 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015694 if (*dir == NUL)
15695 rettv->vval.v_number = FAIL;
15696 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015697 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015698 if (*gettail(dir) == NUL)
15699 /* remove trailing slashes */
15700 *gettail_sep(dir) = NUL;
15701
15702 if (argvars[1].v_type != VAR_UNKNOWN)
15703 {
15704 if (argvars[2].v_type != VAR_UNKNOWN)
15705 prot = get_tv_number_chk(&argvars[2], NULL);
15706 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15707 mkdir_recurse(dir, prot);
15708 }
15709 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015710 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015711}
15712#endif
15713
Bram Moolenaar0d660222005-01-07 21:51:51 +000015714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 * "mode()" function
15716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015718f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015720 char_u buf[3];
15721
15722 buf[1] = NUL;
15723 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 if (VIsual_active)
15726 {
15727 if (VIsual_select)
15728 buf[0] = VIsual_mode + 's' - 'v';
15729 else
15730 buf[0] = VIsual_mode;
15731 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015732 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015733 || State == CONFIRM)
15734 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015736 if (State == ASKMORE)
15737 buf[1] = 'm';
15738 else if (State == CONFIRM)
15739 buf[1] = '?';
15740 }
15741 else if (State == EXTERNCMD)
15742 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743 else if (State & INSERT)
15744 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015745#ifdef FEAT_VREPLACE
15746 if (State & VREPLACE_FLAG)
15747 {
15748 buf[0] = 'R';
15749 buf[1] = 'v';
15750 }
15751 else
15752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 if (State & REPLACE_FLAG)
15754 buf[0] = 'R';
15755 else
15756 buf[0] = 'i';
15757 }
15758 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015761 if (exmode_active)
15762 buf[1] = 'v';
15763 }
15764 else if (exmode_active)
15765 {
15766 buf[0] = 'c';
15767 buf[1] = 'e';
15768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015770 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015772 if (finish_op)
15773 buf[1] = 'o';
15774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015776 /* Clear out the minor mode when the argument is not a non-zero number or
15777 * non-empty string. */
15778 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015779 buf[1] = NUL;
15780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015781 rettv->vval.v_string = vim_strsave(buf);
15782 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783}
15784
Bram Moolenaar429fa852013-04-15 12:27:36 +020015785#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015786/*
15787 * "mzeval()" function
15788 */
15789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015790f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015791{
15792 char_u *str;
15793 char_u buf[NUMBUFLEN];
15794
15795 str = get_tv_string_buf(&argvars[0], buf);
15796 do_mzeval(str, rettv);
15797}
Bram Moolenaar75676462013-01-30 14:55:42 +010015798
15799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015800mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015801{
15802 typval_T argvars[3];
15803
15804 argvars[0].v_type = VAR_STRING;
15805 argvars[0].vval.v_string = name;
15806 copy_tv(args, &argvars[1]);
15807 argvars[2].v_type = VAR_UNKNOWN;
15808 f_call(argvars, rettv);
15809 clear_tv(&argvars[1]);
15810}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015811#endif
15812
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015814 * "nextnonblank()" function
15815 */
15816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015817f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015818{
15819 linenr_T lnum;
15820
15821 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015823 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015824 {
15825 lnum = 0;
15826 break;
15827 }
15828 if (*skipwhite(ml_get(lnum)) != NUL)
15829 break;
15830 }
15831 rettv->vval.v_number = lnum;
15832}
15833
15834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835 * "nr2char()" function
15836 */
15837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015838f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839{
15840 char_u buf[NUMBUFLEN];
15841
15842#ifdef FEAT_MBYTE
15843 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015844 {
15845 int utf8 = 0;
15846
15847 if (argvars[1].v_type != VAR_UNKNOWN)
15848 utf8 = get_tv_number_chk(&argvars[1], NULL);
15849 if (utf8)
15850 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15851 else
15852 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854 else
15855#endif
15856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015857 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 buf[1] = NUL;
15859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015860 rettv->v_type = VAR_STRING;
15861 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015862}
15863
15864/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015865 * "or(expr, expr)" function
15866 */
15867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015868f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015869{
15870 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15871 | get_tv_number_chk(&argvars[1], NULL);
15872}
15873
15874/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015875 * "pathshorten()" function
15876 */
15877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015878f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015879{
15880 char_u *p;
15881
15882 rettv->v_type = VAR_STRING;
15883 p = get_tv_string_chk(&argvars[0]);
15884 if (p == NULL)
15885 rettv->vval.v_string = NULL;
15886 else
15887 {
15888 p = vim_strsave(p);
15889 rettv->vval.v_string = p;
15890 if (p != NULL)
15891 shorten_dir(p);
15892 }
15893}
15894
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015895#ifdef FEAT_PERL
15896/*
15897 * "perleval()" function
15898 */
15899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015900f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015901{
15902 char_u *str;
15903 char_u buf[NUMBUFLEN];
15904
15905 str = get_tv_string_buf(&argvars[0], buf);
15906 do_perleval(str, rettv);
15907}
15908#endif
15909
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015910#ifdef FEAT_FLOAT
15911/*
15912 * "pow()" function
15913 */
15914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015915f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015916{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015917 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015918
15919 rettv->v_type = VAR_FLOAT;
15920 if (get_float_arg(argvars, &fx) == OK
15921 && get_float_arg(&argvars[1], &fy) == OK)
15922 rettv->vval.v_float = pow(fx, fy);
15923 else
15924 rettv->vval.v_float = 0.0;
15925}
15926#endif
15927
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015928/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015929 * "prevnonblank()" function
15930 */
15931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015932f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015933{
15934 linenr_T lnum;
15935
15936 lnum = get_tv_lnum(argvars);
15937 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15938 lnum = 0;
15939 else
15940 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15941 --lnum;
15942 rettv->vval.v_number = lnum;
15943}
15944
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015945/* This dummy va_list is here because:
15946 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15947 * - locally in the function results in a "used before set" warning
15948 * - using va_start() to initialize it gives "function with fixed args" error */
15949static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015950
Bram Moolenaar8c711452005-01-14 21:53:12 +000015951/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015952 * "printf()" function
15953 */
15954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015955f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015956{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015957 char_u buf[NUMBUFLEN];
15958 int len;
15959 char_u *s;
15960 int saved_did_emsg = did_emsg;
15961 char *fmt;
15962
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015963 rettv->v_type = VAR_STRING;
15964 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015965
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015966 /* Get the required length, allocate the buffer and do it for real. */
15967 did_emsg = FALSE;
15968 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15969 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15970 if (!did_emsg)
15971 {
15972 s = alloc(len + 1);
15973 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015974 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015975 rettv->vval.v_string = s;
15976 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015977 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015978 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015979 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015980}
15981
15982/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015983 * "pumvisible()" function
15984 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015986f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015987{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015988#ifdef FEAT_INS_EXPAND
15989 if (pum_visible())
15990 rettv->vval.v_number = 1;
15991#endif
15992}
15993
Bram Moolenaardb913952012-06-29 12:54:53 +020015994#ifdef FEAT_PYTHON3
15995/*
15996 * "py3eval()" function
15997 */
15998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015999f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016000{
16001 char_u *str;
16002 char_u buf[NUMBUFLEN];
16003
16004 str = get_tv_string_buf(&argvars[0], buf);
16005 do_py3eval(str, rettv);
16006}
16007#endif
16008
16009#ifdef FEAT_PYTHON
16010/*
16011 * "pyeval()" function
16012 */
16013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016014f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016015{
16016 char_u *str;
16017 char_u buf[NUMBUFLEN];
16018
16019 str = get_tv_string_buf(&argvars[0], buf);
16020 do_pyeval(str, rettv);
16021}
16022#endif
16023
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016024/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016025 * "range()" function
16026 */
16027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016028f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016029{
16030 long start;
16031 long end;
16032 long stride = 1;
16033 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016034 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016035
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016036 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016037 if (argvars[1].v_type == VAR_UNKNOWN)
16038 {
16039 end = start - 1;
16040 start = 0;
16041 }
16042 else
16043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016044 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016045 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016046 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016047 }
16048
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016049 if (error)
16050 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016051 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016052 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016053 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016054 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016055 else
16056 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016057 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016058 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016059 if (list_append_number(rettv->vval.v_list,
16060 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016061 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016062 }
16063}
16064
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016065/*
16066 * "readfile()" function
16067 */
16068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016069f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016070{
16071 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016072 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016073 char_u *fname;
16074 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016075 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16076 int io_size = sizeof(buf);
16077 int readlen; /* size of last fread() */
16078 char_u *prev = NULL; /* previously read bytes, if any */
16079 long prevlen = 0; /* length of data in prev */
16080 long prevsize = 0; /* size of prev buffer */
16081 long maxline = MAXLNUM;
16082 long cnt = 0;
16083 char_u *p; /* position in buf */
16084 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016085
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016086 if (argvars[1].v_type != VAR_UNKNOWN)
16087 {
16088 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16089 binary = TRUE;
16090 if (argvars[2].v_type != VAR_UNKNOWN)
16091 maxline = get_tv_number(&argvars[2]);
16092 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016093
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016094 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016095 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016096
16097 /* Always open the file in binary mode, library functions have a mind of
16098 * their own about CR-LF conversion. */
16099 fname = get_tv_string(&argvars[0]);
16100 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16101 {
16102 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16103 return;
16104 }
16105
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016106 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016107 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016108 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016109
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016110 /* This for loop processes what was read, but is also entered at end
16111 * of file so that either:
16112 * - an incomplete line gets written
16113 * - a "binary" file gets an empty line at the end if it ends in a
16114 * newline. */
16115 for (p = buf, start = buf;
16116 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16117 ++p)
16118 {
16119 if (*p == '\n' || readlen <= 0)
16120 {
16121 listitem_T *li;
16122 char_u *s = NULL;
16123 long_u len = p - start;
16124
16125 /* Finished a line. Remove CRs before NL. */
16126 if (readlen > 0 && !binary)
16127 {
16128 while (len > 0 && start[len - 1] == '\r')
16129 --len;
16130 /* removal may cross back to the "prev" string */
16131 if (len == 0)
16132 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16133 --prevlen;
16134 }
16135 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016136 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016137 else
16138 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016139 /* Change "prev" buffer to be the right size. This way
16140 * the bytes are only copied once, and very long lines are
16141 * allocated only once. */
16142 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016143 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016144 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016145 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016146 prev = NULL; /* the list will own the string */
16147 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016148 }
16149 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016150 if (s == NULL)
16151 {
16152 do_outofmem_msg((long_u) prevlen + len + 1);
16153 failed = TRUE;
16154 break;
16155 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016156
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016157 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016158 {
16159 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016160 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016161 break;
16162 }
16163 li->li_tv.v_type = VAR_STRING;
16164 li->li_tv.v_lock = 0;
16165 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016166 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016167
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016168 start = p + 1; /* step over newline */
16169 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016170 break;
16171 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016172 else if (*p == NUL)
16173 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016174#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016175 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16176 * when finding the BF and check the previous two bytes. */
16177 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016178 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016179 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16180 * + 1, these may be in the "prev" string. */
16181 char_u back1 = p >= buf + 1 ? p[-1]
16182 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16183 char_u back2 = p >= buf + 2 ? p[-2]
16184 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16185 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016186
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016187 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016188 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016189 char_u *dest = p - 2;
16190
16191 /* Usually a BOM is at the beginning of a file, and so at
16192 * the beginning of a line; then we can just step over it.
16193 */
16194 if (start == dest)
16195 start = p + 1;
16196 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016197 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016198 /* have to shuffle buf to close gap */
16199 int adjust_prevlen = 0;
16200
16201 if (dest < buf)
16202 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016203 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016204 dest = buf;
16205 }
16206 if (readlen > p - buf + 1)
16207 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16208 readlen -= 3 - adjust_prevlen;
16209 prevlen -= adjust_prevlen;
16210 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016211 }
16212 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016213 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016214#endif
16215 } /* for */
16216
16217 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16218 break;
16219 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016220 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016221 /* There's part of a line in buf, store it in "prev". */
16222 if (p - start + prevlen >= prevsize)
16223 {
16224 /* need bigger "prev" buffer */
16225 char_u *newprev;
16226
16227 /* A common use case is ordinary text files and "prev" gets a
16228 * fragment of a line, so the first allocation is made
16229 * small, to avoid repeatedly 'allocing' large and
16230 * 'reallocing' small. */
16231 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016232 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016233 else
16234 {
16235 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016236 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016237 prevsize = grow50pc > growmin ? grow50pc : growmin;
16238 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016239 newprev = prev == NULL ? alloc(prevsize)
16240 : vim_realloc(prev, prevsize);
16241 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016242 {
16243 do_outofmem_msg((long_u)prevsize);
16244 failed = TRUE;
16245 break;
16246 }
16247 prev = newprev;
16248 }
16249 /* Add the line part to end of "prev". */
16250 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016251 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016252 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016253 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016254
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016255 /*
16256 * For a negative line count use only the lines at the end of the file,
16257 * free the rest.
16258 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016259 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016260 while (cnt > -maxline)
16261 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016262 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016263 --cnt;
16264 }
16265
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016266 if (failed)
16267 {
16268 list_free(rettv->vval.v_list, TRUE);
16269 /* readfile doc says an empty list is returned on error */
16270 rettv->vval.v_list = list_alloc();
16271 }
16272
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016273 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016274 fclose(fd);
16275}
16276
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016277#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016278static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016279
16280/*
16281 * Convert a List to proftime_T.
16282 * Return FAIL when there is something wrong.
16283 */
16284 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016285list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016286{
16287 long n1, n2;
16288 int error = FALSE;
16289
16290 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16291 || arg->vval.v_list->lv_len != 2)
16292 return FAIL;
16293 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16294 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16295# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016296 tm->HighPart = n1;
16297 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016298# else
16299 tm->tv_sec = n1;
16300 tm->tv_usec = n2;
16301# endif
16302 return error ? FAIL : OK;
16303}
16304#endif /* FEAT_RELTIME */
16305
16306/*
16307 * "reltime()" function
16308 */
16309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016310f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016311{
16312#ifdef FEAT_RELTIME
16313 proftime_T res;
16314 proftime_T start;
16315
16316 if (argvars[0].v_type == VAR_UNKNOWN)
16317 {
16318 /* No arguments: get current time. */
16319 profile_start(&res);
16320 }
16321 else if (argvars[1].v_type == VAR_UNKNOWN)
16322 {
16323 if (list2proftime(&argvars[0], &res) == FAIL)
16324 return;
16325 profile_end(&res);
16326 }
16327 else
16328 {
16329 /* Two arguments: compute the difference. */
16330 if (list2proftime(&argvars[0], &start) == FAIL
16331 || list2proftime(&argvars[1], &res) == FAIL)
16332 return;
16333 profile_sub(&res, &start);
16334 }
16335
16336 if (rettv_list_alloc(rettv) == OK)
16337 {
16338 long n1, n2;
16339
16340# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016341 n1 = res.HighPart;
16342 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016343# else
16344 n1 = res.tv_sec;
16345 n2 = res.tv_usec;
16346# endif
16347 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16348 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16349 }
16350#endif
16351}
16352
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016353#ifdef FEAT_FLOAT
16354/*
16355 * "reltimefloat()" function
16356 */
16357 static void
16358f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16359{
16360# ifdef FEAT_RELTIME
16361 proftime_T tm;
16362# endif
16363
16364 rettv->v_type = VAR_FLOAT;
16365 rettv->vval.v_float = 0;
16366# ifdef FEAT_RELTIME
16367 if (list2proftime(&argvars[0], &tm) == OK)
16368 rettv->vval.v_float = profile_float(&tm);
16369# endif
16370}
16371#endif
16372
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016373/*
16374 * "reltimestr()" function
16375 */
16376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016377f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016378{
16379#ifdef FEAT_RELTIME
16380 proftime_T tm;
16381#endif
16382
16383 rettv->v_type = VAR_STRING;
16384 rettv->vval.v_string = NULL;
16385#ifdef FEAT_RELTIME
16386 if (list2proftime(&argvars[0], &tm) == OK)
16387 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16388#endif
16389}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016390
Bram Moolenaar0d660222005-01-07 21:51:51 +000016391#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016392static void make_connection(void);
16393static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016394
16395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016396make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016397{
16398 if (X_DISPLAY == NULL
16399# ifdef FEAT_GUI
16400 && !gui.in_use
16401# endif
16402 )
16403 {
16404 x_force_connect = TRUE;
16405 setup_term_clip();
16406 x_force_connect = FALSE;
16407 }
16408}
16409
16410 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016411check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412{
16413 make_connection();
16414 if (X_DISPLAY == NULL)
16415 {
16416 EMSG(_("E240: No connection to Vim server"));
16417 return FAIL;
16418 }
16419 return OK;
16420}
16421#endif
16422
16423#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016424static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016425
16426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016427remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016428{
16429 char_u *server_name;
16430 char_u *keys;
16431 char_u *r = NULL;
16432 char_u buf[NUMBUFLEN];
16433# ifdef WIN32
16434 HWND w;
16435# else
16436 Window w;
16437# endif
16438
16439 if (check_restricted() || check_secure())
16440 return;
16441
16442# ifdef FEAT_X11
16443 if (check_connection() == FAIL)
16444 return;
16445# endif
16446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016447 server_name = get_tv_string_chk(&argvars[0]);
16448 if (server_name == NULL)
16449 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450 keys = get_tv_string_buf(&argvars[1], buf);
16451# ifdef WIN32
16452 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16453# else
16454 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16455 < 0)
16456# endif
16457 {
16458 if (r != NULL)
16459 EMSG(r); /* sending worked but evaluation failed */
16460 else
16461 EMSG2(_("E241: Unable to send to %s"), server_name);
16462 return;
16463 }
16464
16465 rettv->vval.v_string = r;
16466
16467 if (argvars[2].v_type != VAR_UNKNOWN)
16468 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016469 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016470 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016471 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016472
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016473 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016474 v.di_tv.v_type = VAR_STRING;
16475 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016476 idvar = get_tv_string_chk(&argvars[2]);
16477 if (idvar != NULL)
16478 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016479 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016480 }
16481}
16482#endif
16483
16484/*
16485 * "remote_expr()" function
16486 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016488f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489{
16490 rettv->v_type = VAR_STRING;
16491 rettv->vval.v_string = NULL;
16492#ifdef FEAT_CLIENTSERVER
16493 remote_common(argvars, rettv, TRUE);
16494#endif
16495}
16496
16497/*
16498 * "remote_foreground()" function
16499 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016501f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016502{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016503#ifdef FEAT_CLIENTSERVER
16504# ifdef WIN32
16505 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016506 {
16507 char_u *server_name = get_tv_string_chk(&argvars[0]);
16508
16509 if (server_name != NULL)
16510 serverForeground(server_name);
16511 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016512# else
16513 /* Send a foreground() expression to the server. */
16514 argvars[1].v_type = VAR_STRING;
16515 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16516 argvars[2].v_type = VAR_UNKNOWN;
16517 remote_common(argvars, rettv, TRUE);
16518 vim_free(argvars[1].vval.v_string);
16519# endif
16520#endif
16521}
16522
Bram Moolenaar0d660222005-01-07 21:51:51 +000016523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016524f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525{
16526#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016527 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016528 char_u *s = NULL;
16529# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016530 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016531# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016532 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016533
16534 if (check_restricted() || check_secure())
16535 {
16536 rettv->vval.v_number = -1;
16537 return;
16538 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016539 serverid = get_tv_string_chk(&argvars[0]);
16540 if (serverid == NULL)
16541 {
16542 rettv->vval.v_number = -1;
16543 return; /* type error; errmsg already given */
16544 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016545# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016546 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016547 if (n == 0)
16548 rettv->vval.v_number = -1;
16549 else
16550 {
16551 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16552 rettv->vval.v_number = (s != NULL);
16553 }
16554# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016555 if (check_connection() == FAIL)
16556 return;
16557
16558 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016559 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016560# endif
16561
16562 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16563 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016564 char_u *retvar;
16565
Bram Moolenaar33570922005-01-25 22:26:29 +000016566 v.di_tv.v_type = VAR_STRING;
16567 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016568 retvar = get_tv_string_chk(&argvars[1]);
16569 if (retvar != NULL)
16570 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016571 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016572 }
16573#else
16574 rettv->vval.v_number = -1;
16575#endif
16576}
16577
Bram Moolenaar0d660222005-01-07 21:51:51 +000016578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016579f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016580{
16581 char_u *r = NULL;
16582
16583#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016584 char_u *serverid = get_tv_string_chk(&argvars[0]);
16585
16586 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016587 {
16588# ifdef WIN32
16589 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016590 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016591
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016592 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016593 if (n != 0)
16594 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16595 if (r == NULL)
16596# else
16597 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016598 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016599# endif
16600 EMSG(_("E277: Unable to read a server reply"));
16601 }
16602#endif
16603 rettv->v_type = VAR_STRING;
16604 rettv->vval.v_string = r;
16605}
16606
16607/*
16608 * "remote_send()" function
16609 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016611f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016612{
16613 rettv->v_type = VAR_STRING;
16614 rettv->vval.v_string = NULL;
16615#ifdef FEAT_CLIENTSERVER
16616 remote_common(argvars, rettv, FALSE);
16617#endif
16618}
16619
16620/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016621 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016622 */
16623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016624f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016625{
Bram Moolenaar33570922005-01-25 22:26:29 +000016626 list_T *l;
16627 listitem_T *item, *item2;
16628 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016629 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016630 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016631 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016632 dict_T *d;
16633 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016634 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016635
Bram Moolenaar8c711452005-01-14 21:53:12 +000016636 if (argvars[0].v_type == VAR_DICT)
16637 {
16638 if (argvars[2].v_type != VAR_UNKNOWN)
16639 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016640 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016641 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016643 key = get_tv_string_chk(&argvars[1]);
16644 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016646 di = dict_find(d, key, -1);
16647 if (di == NULL)
16648 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016649 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16650 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016651 {
16652 *rettv = di->di_tv;
16653 init_tv(&di->di_tv);
16654 dictitem_remove(d, di);
16655 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016656 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016657 }
16658 }
16659 else if (argvars[0].v_type != VAR_LIST)
16660 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016661 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016662 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016664 int error = FALSE;
16665
16666 idx = get_tv_number_chk(&argvars[1], &error);
16667 if (error)
16668 ; /* type error: do nothing, errmsg already given */
16669 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016670 EMSGN(_(e_listidx), idx);
16671 else
16672 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016673 if (argvars[2].v_type == VAR_UNKNOWN)
16674 {
16675 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016676 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016677 *rettv = item->li_tv;
16678 vim_free(item);
16679 }
16680 else
16681 {
16682 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016683 end = get_tv_number_chk(&argvars[2], &error);
16684 if (error)
16685 ; /* type error: do nothing */
16686 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016687 EMSGN(_(e_listidx), end);
16688 else
16689 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016690 int cnt = 0;
16691
16692 for (li = item; li != NULL; li = li->li_next)
16693 {
16694 ++cnt;
16695 if (li == item2)
16696 break;
16697 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016698 if (li == NULL) /* didn't find "item2" after "item" */
16699 EMSG(_(e_invrange));
16700 else
16701 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016702 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016703 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016704 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016705 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016706 l->lv_first = item;
16707 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016708 item->li_prev = NULL;
16709 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016710 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016711 }
16712 }
16713 }
16714 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016715 }
16716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717}
16718
16719/*
16720 * "rename({from}, {to})" function
16721 */
16722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016723f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724{
16725 char_u buf[NUMBUFLEN];
16726
16727 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016728 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016730 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16731 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732}
16733
16734/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016735 * "repeat()" function
16736 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016738f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016739{
16740 char_u *p;
16741 int n;
16742 int slen;
16743 int len;
16744 char_u *r;
16745 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016746
16747 n = get_tv_number(&argvars[1]);
16748 if (argvars[0].v_type == VAR_LIST)
16749 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016750 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016751 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016752 if (list_extend(rettv->vval.v_list,
16753 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016754 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016755 }
16756 else
16757 {
16758 p = get_tv_string(&argvars[0]);
16759 rettv->v_type = VAR_STRING;
16760 rettv->vval.v_string = NULL;
16761
16762 slen = (int)STRLEN(p);
16763 len = slen * n;
16764 if (len <= 0)
16765 return;
16766
16767 r = alloc(len + 1);
16768 if (r != NULL)
16769 {
16770 for (i = 0; i < n; i++)
16771 mch_memmove(r + i * slen, p, (size_t)slen);
16772 r[len] = NUL;
16773 }
16774
16775 rettv->vval.v_string = r;
16776 }
16777}
16778
16779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780 * "resolve()" function
16781 */
16782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016783f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784{
16785 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016786#ifdef HAVE_READLINK
16787 char_u *buf = NULL;
16788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016790 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791#ifdef FEAT_SHORTCUT
16792 {
16793 char_u *v = NULL;
16794
16795 v = mch_resolve_shortcut(p);
16796 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016797 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016799 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800 }
16801#else
16802# ifdef HAVE_READLINK
16803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 char_u *cpy;
16805 int len;
16806 char_u *remain = NULL;
16807 char_u *q;
16808 int is_relative_to_current = FALSE;
16809 int has_trailing_pathsep = FALSE;
16810 int limit = 100;
16811
16812 p = vim_strsave(p);
16813
16814 if (p[0] == '.' && (vim_ispathsep(p[1])
16815 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16816 is_relative_to_current = TRUE;
16817
16818 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016819 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016822 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824
16825 q = getnextcomp(p);
16826 if (*q != NUL)
16827 {
16828 /* Separate the first path component in "p", and keep the
16829 * remainder (beginning with the path separator). */
16830 remain = vim_strsave(q - 1);
16831 q[-1] = NUL;
16832 }
16833
Bram Moolenaard9462e32011-04-11 21:35:11 +020016834 buf = alloc(MAXPATHL + 1);
16835 if (buf == NULL)
16836 goto fail;
16837
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 for (;;)
16839 {
16840 for (;;)
16841 {
16842 len = readlink((char *)p, (char *)buf, MAXPATHL);
16843 if (len <= 0)
16844 break;
16845 buf[len] = NUL;
16846
16847 if (limit-- == 0)
16848 {
16849 vim_free(p);
16850 vim_free(remain);
16851 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016852 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 goto fail;
16854 }
16855
16856 /* Ensure that the result will have a trailing path separator
16857 * if the argument has one. */
16858 if (remain == NULL && has_trailing_pathsep)
16859 add_pathsep(buf);
16860
16861 /* Separate the first path component in the link value and
16862 * concatenate the remainders. */
16863 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16864 if (*q != NUL)
16865 {
16866 if (remain == NULL)
16867 remain = vim_strsave(q - 1);
16868 else
16869 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016870 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 if (cpy != NULL)
16872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 vim_free(remain);
16874 remain = cpy;
16875 }
16876 }
16877 q[-1] = NUL;
16878 }
16879
16880 q = gettail(p);
16881 if (q > p && *q == NUL)
16882 {
16883 /* Ignore trailing path separator. */
16884 q[-1] = NUL;
16885 q = gettail(p);
16886 }
16887 if (q > p && !mch_isFullName(buf))
16888 {
16889 /* symlink is relative to directory of argument */
16890 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16891 if (cpy != NULL)
16892 {
16893 STRCPY(cpy, p);
16894 STRCPY(gettail(cpy), buf);
16895 vim_free(p);
16896 p = cpy;
16897 }
16898 }
16899 else
16900 {
16901 vim_free(p);
16902 p = vim_strsave(buf);
16903 }
16904 }
16905
16906 if (remain == NULL)
16907 break;
16908
16909 /* Append the first path component of "remain" to "p". */
16910 q = getnextcomp(remain + 1);
16911 len = q - remain - (*q != NUL);
16912 cpy = vim_strnsave(p, STRLEN(p) + len);
16913 if (cpy != NULL)
16914 {
16915 STRNCAT(cpy, remain, len);
16916 vim_free(p);
16917 p = cpy;
16918 }
16919 /* Shorten "remain". */
16920 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016921 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922 else
16923 {
16924 vim_free(remain);
16925 remain = NULL;
16926 }
16927 }
16928
16929 /* If the result is a relative path name, make it explicitly relative to
16930 * the current directory if and only if the argument had this form. */
16931 if (!vim_ispathsep(*p))
16932 {
16933 if (is_relative_to_current
16934 && *p != NUL
16935 && !(p[0] == '.'
16936 && (p[1] == NUL
16937 || vim_ispathsep(p[1])
16938 || (p[1] == '.'
16939 && (p[2] == NUL
16940 || vim_ispathsep(p[2]))))))
16941 {
16942 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016943 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 if (cpy != NULL)
16945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946 vim_free(p);
16947 p = cpy;
16948 }
16949 }
16950 else if (!is_relative_to_current)
16951 {
16952 /* Strip leading "./". */
16953 q = p;
16954 while (q[0] == '.' && vim_ispathsep(q[1]))
16955 q += 2;
16956 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016957 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 }
16959 }
16960
16961 /* Ensure that the result will have no trailing path separator
16962 * if the argument had none. But keep "/" or "//". */
16963 if (!has_trailing_pathsep)
16964 {
16965 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016966 if (after_pathsep(p, q))
16967 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 }
16969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016970 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971 }
16972# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016973 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974# endif
16975#endif
16976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016977 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978
16979#ifdef HAVE_READLINK
16980fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016981 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016983 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984}
16985
16986/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016987 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 */
16989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016990f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991{
Bram Moolenaar33570922005-01-25 22:26:29 +000016992 list_T *l;
16993 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994
Bram Moolenaar0d660222005-01-07 21:51:51 +000016995 if (argvars[0].v_type != VAR_LIST)
16996 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016997 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016998 && !tv_check_lock(l->lv_lock,
16999 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000 {
17001 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017002 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017003 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004 while (li != NULL)
17005 {
17006 ni = li->li_prev;
17007 list_append(l, li);
17008 li = ni;
17009 }
17010 rettv->vval.v_list = l;
17011 rettv->v_type = VAR_LIST;
17012 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017013 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015}
17016
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017017#define SP_NOMOVE 0x01 /* don't move cursor */
17018#define SP_REPEAT 0x02 /* repeat to find outer pair */
17019#define SP_RETCOUNT 0x04 /* return matchcount */
17020#define SP_SETPCMARK 0x08 /* set previous context mark */
17021#define SP_START 0x10 /* accept match at start position */
17022#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17023#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017024#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017025
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017026static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017027
17028/*
17029 * Get flags for a search function.
17030 * Possibly sets "p_ws".
17031 * Returns BACKWARD, FORWARD or zero (for an error).
17032 */
17033 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017034get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017035{
17036 int dir = FORWARD;
17037 char_u *flags;
17038 char_u nbuf[NUMBUFLEN];
17039 int mask;
17040
17041 if (varp->v_type != VAR_UNKNOWN)
17042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017043 flags = get_tv_string_buf_chk(varp, nbuf);
17044 if (flags == NULL)
17045 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017046 while (*flags != NUL)
17047 {
17048 switch (*flags)
17049 {
17050 case 'b': dir = BACKWARD; break;
17051 case 'w': p_ws = TRUE; break;
17052 case 'W': p_ws = FALSE; break;
17053 default: mask = 0;
17054 if (flagsp != NULL)
17055 switch (*flags)
17056 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017057 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017058 case 'e': mask = SP_END; break;
17059 case 'm': mask = SP_RETCOUNT; break;
17060 case 'n': mask = SP_NOMOVE; break;
17061 case 'p': mask = SP_SUBPAT; break;
17062 case 'r': mask = SP_REPEAT; break;
17063 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017064 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017065 }
17066 if (mask == 0)
17067 {
17068 EMSG2(_(e_invarg2), flags);
17069 dir = 0;
17070 }
17071 else
17072 *flagsp |= mask;
17073 }
17074 if (dir == 0)
17075 break;
17076 ++flags;
17077 }
17078 }
17079 return dir;
17080}
17081
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017083 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017086search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017088 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089 char_u *pat;
17090 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017091 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 int save_p_ws = p_ws;
17093 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017094 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017095 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017096 proftime_T tm;
17097#ifdef FEAT_RELTIME
17098 long time_limit = 0;
17099#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017100 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017101 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017103 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017104 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017105 if (dir == 0)
17106 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017107 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017108 if (flags & SP_START)
17109 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017110 if (flags & SP_END)
17111 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017112 if (flags & SP_COLUMN)
17113 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017114
Bram Moolenaar76929292008-01-06 19:07:36 +000017115 /* Optional arguments: line number to stop searching and timeout. */
17116 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017117 {
17118 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17119 if (lnum_stop < 0)
17120 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017121#ifdef FEAT_RELTIME
17122 if (argvars[3].v_type != VAR_UNKNOWN)
17123 {
17124 time_limit = get_tv_number_chk(&argvars[3], NULL);
17125 if (time_limit < 0)
17126 goto theend;
17127 }
17128#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017129 }
17130
Bram Moolenaar76929292008-01-06 19:07:36 +000017131#ifdef FEAT_RELTIME
17132 /* Set the time limit, if there is one. */
17133 profile_setlimit(time_limit, &tm);
17134#endif
17135
Bram Moolenaar231334e2005-07-25 20:46:57 +000017136 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017137 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017138 * Check to make sure only those flags are set.
17139 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17140 * flags cannot be set. Check for that condition also.
17141 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017142 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017143 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017144 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017145 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017146 goto theend;
17147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017149 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017150 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017151 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017152 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017154 if (flags & SP_SUBPAT)
17155 retval = subpatnum;
17156 else
17157 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017158 if (flags & SP_SETPCMARK)
17159 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017161 if (match_pos != NULL)
17162 {
17163 /* Store the match cursor position */
17164 match_pos->lnum = pos.lnum;
17165 match_pos->col = pos.col + 1;
17166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 /* "/$" will put the cursor after the end of the line, may need to
17168 * correct that here */
17169 check_cursor();
17170 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017171
17172 /* If 'n' flag is used: restore cursor position. */
17173 if (flags & SP_NOMOVE)
17174 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017175 else
17176 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017177theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017179
17180 return retval;
17181}
17182
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017183#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017184
17185/*
17186 * round() is not in C90, use ceil() or floor() instead.
17187 */
17188 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017189vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017190{
17191 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17192}
17193
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017194/*
17195 * "round({float})" function
17196 */
17197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017198f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017199{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017200 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017201
17202 rettv->v_type = VAR_FLOAT;
17203 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017204 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017205 else
17206 rettv->vval.v_float = 0.0;
17207}
17208#endif
17209
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017210/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017211 * "screenattr()" function
17212 */
17213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017214f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017215{
17216 int row;
17217 int col;
17218 int c;
17219
17220 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17221 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17222 if (row < 0 || row >= screen_Rows
17223 || col < 0 || col >= screen_Columns)
17224 c = -1;
17225 else
17226 c = ScreenAttrs[LineOffset[row] + col];
17227 rettv->vval.v_number = c;
17228}
17229
17230/*
17231 * "screenchar()" function
17232 */
17233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017234f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017235{
17236 int row;
17237 int col;
17238 int off;
17239 int c;
17240
17241 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17242 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17243 if (row < 0 || row >= screen_Rows
17244 || col < 0 || col >= screen_Columns)
17245 c = -1;
17246 else
17247 {
17248 off = LineOffset[row] + col;
17249#ifdef FEAT_MBYTE
17250 if (enc_utf8 && ScreenLinesUC[off] != 0)
17251 c = ScreenLinesUC[off];
17252 else
17253#endif
17254 c = ScreenLines[off];
17255 }
17256 rettv->vval.v_number = c;
17257}
17258
17259/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017260 * "screencol()" function
17261 *
17262 * First column is 1 to be consistent with virtcol().
17263 */
17264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017265f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017266{
17267 rettv->vval.v_number = screen_screencol() + 1;
17268}
17269
17270/*
17271 * "screenrow()" function
17272 */
17273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017274f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017275{
17276 rettv->vval.v_number = screen_screenrow() + 1;
17277}
17278
17279/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017280 * "search()" function
17281 */
17282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017283f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017284{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017285 int flags = 0;
17286
17287 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288}
17289
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017291 * "searchdecl()" function
17292 */
17293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017294f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017295{
17296 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017297 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017298 int error = FALSE;
17299 char_u *name;
17300
17301 rettv->vval.v_number = 1; /* default: FAIL */
17302
17303 name = get_tv_string_chk(&argvars[0]);
17304 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017305 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017306 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017307 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17308 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17309 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017310 if (!error && name != NULL)
17311 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017312 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017313}
17314
17315/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017316 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017318 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017319searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320{
17321 char_u *spat, *mpat, *epat;
17322 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 int dir;
17325 int flags = 0;
17326 char_u nbuf1[NUMBUFLEN];
17327 char_u nbuf2[NUMBUFLEN];
17328 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017329 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017330 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017331 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017334 spat = get_tv_string_chk(&argvars[0]);
17335 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17336 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17337 if (spat == NULL || mpat == NULL || epat == NULL)
17338 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340 /* Handle the optional fourth argument: flags */
17341 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017342 if (dir == 0)
17343 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017344
17345 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017346 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17347 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017348 if ((flags & (SP_END | SP_SUBPAT)) != 0
17349 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017350 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017351 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017352 goto theend;
17353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017355 /* Using 'r' implies 'W', otherwise it doesn't work. */
17356 if (flags & SP_REPEAT)
17357 p_ws = FALSE;
17358
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017359 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017360 if (argvars[3].v_type == VAR_UNKNOWN
17361 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 skip = (char_u *)"";
17363 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017364 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017365 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017366 if (argvars[5].v_type != VAR_UNKNOWN)
17367 {
17368 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17369 if (lnum_stop < 0)
17370 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017371#ifdef FEAT_RELTIME
17372 if (argvars[6].v_type != VAR_UNKNOWN)
17373 {
17374 time_limit = get_tv_number_chk(&argvars[6], NULL);
17375 if (time_limit < 0)
17376 goto theend;
17377 }
17378#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017379 }
17380 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017381 if (skip == NULL)
17382 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017384 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017385 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017386
17387theend:
17388 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017389
17390 return retval;
17391}
17392
17393/*
17394 * "searchpair()" function
17395 */
17396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017397f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017398{
17399 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17400}
17401
17402/*
17403 * "searchpairpos()" function
17404 */
17405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017406f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017407{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017408 pos_T match_pos;
17409 int lnum = 0;
17410 int col = 0;
17411
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017412 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017413 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017414
17415 if (searchpair_cmn(argvars, &match_pos) > 0)
17416 {
17417 lnum = match_pos.lnum;
17418 col = match_pos.col;
17419 }
17420
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017421 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17422 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017423}
17424
17425/*
17426 * Search for a start/middle/end thing.
17427 * Used by searchpair(), see its documentation for the details.
17428 * Returns 0 or -1 for no match,
17429 */
17430 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017431do_searchpair(
17432 char_u *spat, /* start pattern */
17433 char_u *mpat, /* middle pattern */
17434 char_u *epat, /* end pattern */
17435 int dir, /* BACKWARD or FORWARD */
17436 char_u *skip, /* skip expression */
17437 int flags, /* SP_SETPCMARK and other SP_ values */
17438 pos_T *match_pos,
17439 linenr_T lnum_stop, /* stop at this line if not zero */
17440 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017441{
17442 char_u *save_cpo;
17443 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17444 long retval = 0;
17445 pos_T pos;
17446 pos_T firstpos;
17447 pos_T foundpos;
17448 pos_T save_cursor;
17449 pos_T save_pos;
17450 int n;
17451 int r;
17452 int nest = 1;
17453 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017454 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017455 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017456
17457 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17458 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017459 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017460
Bram Moolenaar76929292008-01-06 19:07:36 +000017461#ifdef FEAT_RELTIME
17462 /* Set the time limit, if there is one. */
17463 profile_setlimit(time_limit, &tm);
17464#endif
17465
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017466 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17467 * start/middle/end (pat3, for the top pair). */
17468 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17469 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17470 if (pat2 == NULL || pat3 == NULL)
17471 goto theend;
17472 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17473 if (*mpat == NUL)
17474 STRCPY(pat3, pat2);
17475 else
17476 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17477 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017478 if (flags & SP_START)
17479 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017480
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 save_cursor = curwin->w_cursor;
17482 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017483 clearpos(&firstpos);
17484 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485 pat = pat3;
17486 for (;;)
17487 {
17488 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017489 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17491 /* didn't find it or found the first match again: FAIL */
17492 break;
17493
17494 if (firstpos.lnum == 0)
17495 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017496 if (equalpos(pos, foundpos))
17497 {
17498 /* Found the same position again. Can happen with a pattern that
17499 * has "\zs" at the end and searching backwards. Advance one
17500 * character and try again. */
17501 if (dir == BACKWARD)
17502 decl(&pos);
17503 else
17504 incl(&pos);
17505 }
17506 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017508 /* clear the start flag to avoid getting stuck here */
17509 options &= ~SEARCH_START;
17510
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 /* If the skip pattern matches, ignore this match. */
17512 if (*skip != NUL)
17513 {
17514 save_pos = curwin->w_cursor;
17515 curwin->w_cursor = pos;
17516 r = eval_to_bool(skip, &err, NULL, FALSE);
17517 curwin->w_cursor = save_pos;
17518 if (err)
17519 {
17520 /* Evaluating {skip} caused an error, break here. */
17521 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017522 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 break;
17524 }
17525 if (r)
17526 continue;
17527 }
17528
17529 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17530 {
17531 /* Found end when searching backwards or start when searching
17532 * forward: nested pair. */
17533 ++nest;
17534 pat = pat2; /* nested, don't search for middle */
17535 }
17536 else
17537 {
17538 /* Found end when searching forward or start when searching
17539 * backward: end of (nested) pair; or found middle in outer pair. */
17540 if (--nest == 1)
17541 pat = pat3; /* outer level, search for middle */
17542 }
17543
17544 if (nest == 0)
17545 {
17546 /* Found the match: return matchcount or line number. */
17547 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017548 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017550 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017551 if (flags & SP_SETPCMARK)
17552 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 curwin->w_cursor = pos;
17554 if (!(flags & SP_REPEAT))
17555 break;
17556 nest = 1; /* search for next unmatched */
17557 }
17558 }
17559
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017560 if (match_pos != NULL)
17561 {
17562 /* Store the match cursor position */
17563 match_pos->lnum = curwin->w_cursor.lnum;
17564 match_pos->col = curwin->w_cursor.col + 1;
17565 }
17566
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017568 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 curwin->w_cursor = save_cursor;
17570
17571theend:
17572 vim_free(pat2);
17573 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017574 if (p_cpo == empty_option)
17575 p_cpo = save_cpo;
17576 else
17577 /* Darn, evaluating the {skip} expression changed the value. */
17578 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017579
17580 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581}
17582
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017583/*
17584 * "searchpos()" function
17585 */
17586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017587f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017588{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017589 pos_T match_pos;
17590 int lnum = 0;
17591 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017592 int n;
17593 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017594
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017595 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017596 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017597
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017598 n = search_cmn(argvars, &match_pos, &flags);
17599 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017600 {
17601 lnum = match_pos.lnum;
17602 col = match_pos.col;
17603 }
17604
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017605 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17606 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017607 if (flags & SP_SUBPAT)
17608 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017609}
17610
Bram Moolenaar0d660222005-01-07 21:51:51 +000017611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017612f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017614#ifdef FEAT_CLIENTSERVER
17615 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017616 char_u *server = get_tv_string_chk(&argvars[0]);
17617 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618
Bram Moolenaar0d660222005-01-07 21:51:51 +000017619 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017620 if (server == NULL || reply == NULL)
17621 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017622 if (check_restricted() || check_secure())
17623 return;
17624# ifdef FEAT_X11
17625 if (check_connection() == FAIL)
17626 return;
17627# endif
17628
17629 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017631 EMSG(_("E258: Unable to send to client"));
17632 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017634 rettv->vval.v_number = 0;
17635#else
17636 rettv->vval.v_number = -1;
17637#endif
17638}
17639
Bram Moolenaar0d660222005-01-07 21:51:51 +000017640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017641f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017642{
17643 char_u *r = NULL;
17644
17645#ifdef FEAT_CLIENTSERVER
17646# ifdef WIN32
17647 r = serverGetVimNames();
17648# else
17649 make_connection();
17650 if (X_DISPLAY != NULL)
17651 r = serverGetVimNames(X_DISPLAY);
17652# endif
17653#endif
17654 rettv->v_type = VAR_STRING;
17655 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656}
17657
17658/*
17659 * "setbufvar()" function
17660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017662f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663{
17664 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017667 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 char_u nbuf[NUMBUFLEN];
17669
17670 if (check_restricted() || check_secure())
17671 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017672 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17673 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017674 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 varp = &argvars[2];
17676
17677 if (buf != NULL && varname != NULL && varp != NULL)
17678 {
17679 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681
17682 if (*varname == '&')
17683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017684 long numval;
17685 char_u *strval;
17686 int error = FALSE;
17687
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017689 numval = get_tv_number_chk(varp, &error);
17690 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017691 if (!error && strval != NULL)
17692 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693 }
17694 else
17695 {
17696 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17697 if (bufvarname != NULL)
17698 {
17699 STRCPY(bufvarname, "b:");
17700 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017701 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702 vim_free(bufvarname);
17703 }
17704 }
17705
17706 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709}
17710
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017712f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017713{
17714 dict_T *d;
17715 dictitem_T *di;
17716 char_u *csearch;
17717
17718 if (argvars[0].v_type != VAR_DICT)
17719 {
17720 EMSG(_(e_dictreq));
17721 return;
17722 }
17723
17724 if ((d = argvars[0].vval.v_dict) != NULL)
17725 {
17726 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17727 if (csearch != NULL)
17728 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017729#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017730 if (enc_utf8)
17731 {
17732 int pcc[MAX_MCO];
17733 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017734
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017735 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17736 }
17737 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017738#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017739 set_last_csearch(PTR2CHAR(csearch),
17740 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017741 }
17742
17743 di = dict_find(d, (char_u *)"forward", -1);
17744 if (di != NULL)
17745 set_csearch_direction(get_tv_number(&di->di_tv)
17746 ? FORWARD : BACKWARD);
17747
17748 di = dict_find(d, (char_u *)"until", -1);
17749 if (di != NULL)
17750 set_csearch_until(!!get_tv_number(&di->di_tv));
17751 }
17752}
17753
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754/*
17755 * "setcmdpos()" function
17756 */
17757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017758f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017760 int pos = (int)get_tv_number(&argvars[0]) - 1;
17761
17762 if (pos >= 0)
17763 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764}
17765
17766/*
17767 * "setline()" function
17768 */
17769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017770f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771{
17772 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017773 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017774 list_T *l = NULL;
17775 listitem_T *li = NULL;
17776 long added = 0;
17777 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017779 lnum = get_tv_lnum(&argvars[0]);
17780 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017782 l = argvars[1].vval.v_list;
17783 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017785 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017786 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017787
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017788 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017789 for (;;)
17790 {
17791 if (l != NULL)
17792 {
17793 /* list argument, get next string */
17794 if (li == NULL)
17795 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017796 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017797 li = li->li_next;
17798 }
17799
17800 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017801 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017802 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017803
17804 /* When coming here from Insert mode, sync undo, so that this can be
17805 * undone separately from what was previously inserted. */
17806 if (u_sync_once == 2)
17807 {
17808 u_sync_once = 1; /* notify that u_sync() was called */
17809 u_sync(TRUE);
17810 }
17811
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017812 if (lnum <= curbuf->b_ml.ml_line_count)
17813 {
17814 /* existing line, replace it */
17815 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17816 {
17817 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017818 if (lnum == curwin->w_cursor.lnum)
17819 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017820 rettv->vval.v_number = 0; /* OK */
17821 }
17822 }
17823 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17824 {
17825 /* lnum is one past the last line, append the line */
17826 ++added;
17827 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17828 rettv->vval.v_number = 0; /* OK */
17829 }
17830
17831 if (l == NULL) /* only one string argument */
17832 break;
17833 ++lnum;
17834 }
17835
17836 if (added > 0)
17837 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838}
17839
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017840static 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 +000017841
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017843 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017844 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017846set_qf_ll_list(
17847 win_T *wp UNUSED,
17848 typval_T *list_arg UNUSED,
17849 typval_T *action_arg UNUSED,
17850 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017851{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017852#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017853 char_u *act;
17854 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017855#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017856
Bram Moolenaar2641f772005-03-25 21:58:17 +000017857 rettv->vval.v_number = -1;
17858
17859#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017860 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017861 EMSG(_(e_listreq));
17862 else
17863 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017864 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017865
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017866 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017867 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017868 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017869 if (act == NULL)
17870 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017871 if (*act == 'a' || *act == 'r')
17872 action = *act;
17873 }
17874
Bram Moolenaar81484f42012-12-05 15:16:47 +010017875 if (l != NULL && set_errorlist(wp, l, action,
17876 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017877 rettv->vval.v_number = 0;
17878 }
17879#endif
17880}
17881
17882/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017883 * "setloclist()" function
17884 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017886f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017887{
17888 win_T *win;
17889
17890 rettv->vval.v_number = -1;
17891
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017892 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017893 if (win != NULL)
17894 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17895}
17896
17897/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017898 * "setmatches()" function
17899 */
17900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017901f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017902{
17903#ifdef FEAT_SEARCH_EXTRA
17904 list_T *l;
17905 listitem_T *li;
17906 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017907 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017908
17909 rettv->vval.v_number = -1;
17910 if (argvars[0].v_type != VAR_LIST)
17911 {
17912 EMSG(_(e_listreq));
17913 return;
17914 }
17915 if ((l = argvars[0].vval.v_list) != NULL)
17916 {
17917
17918 /* To some extent make sure that we are dealing with a list from
17919 * "getmatches()". */
17920 li = l->lv_first;
17921 while (li != NULL)
17922 {
17923 if (li->li_tv.v_type != VAR_DICT
17924 || (d = li->li_tv.vval.v_dict) == NULL)
17925 {
17926 EMSG(_(e_invarg));
17927 return;
17928 }
17929 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017930 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17931 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017932 && dict_find(d, (char_u *)"priority", -1) != NULL
17933 && dict_find(d, (char_u *)"id", -1) != NULL))
17934 {
17935 EMSG(_(e_invarg));
17936 return;
17937 }
17938 li = li->li_next;
17939 }
17940
17941 clear_matches(curwin);
17942 li = l->lv_first;
17943 while (li != NULL)
17944 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017945 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017946 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017947 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017948 char_u *group;
17949 int priority;
17950 int id;
17951 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017952
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017953 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017954 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17955 {
17956 if (s == NULL)
17957 {
17958 s = list_alloc();
17959 if (s == NULL)
17960 return;
17961 }
17962
17963 /* match from matchaddpos() */
17964 for (i = 1; i < 9; i++)
17965 {
17966 sprintf((char *)buf, (char *)"pos%d", i);
17967 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17968 {
17969 if (di->di_tv.v_type != VAR_LIST)
17970 return;
17971
17972 list_append_tv(s, &di->di_tv);
17973 s->lv_refcount++;
17974 }
17975 else
17976 break;
17977 }
17978 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017979
17980 group = get_dict_string(d, (char_u *)"group", FALSE);
17981 priority = (int)get_dict_number(d, (char_u *)"priority");
17982 id = (int)get_dict_number(d, (char_u *)"id");
17983 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17984 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17985 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017986 if (i == 0)
17987 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017988 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017989 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017990 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017991 }
17992 else
17993 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017994 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017995 list_unref(s);
17996 s = NULL;
17997 }
17998
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017999 li = li->li_next;
18000 }
18001 rettv->vval.v_number = 0;
18002 }
18003#endif
18004}
18005
18006/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018007 * "setpos()" function
18008 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018010f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018011{
18012 pos_T pos;
18013 int fnum;
18014 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018015 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018016
Bram Moolenaar08250432008-02-13 11:42:46 +000018017 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018018 name = get_tv_string_chk(argvars);
18019 if (name != NULL)
18020 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018021 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018022 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018023 if (--pos.col < 0)
18024 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018025 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018026 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018027 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018028 if (fnum == curbuf->b_fnum)
18029 {
18030 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018031 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018032 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018033 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018034 curwin->w_set_curswant = FALSE;
18035 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018036 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018037 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018038 }
18039 else
18040 EMSG(_(e_invarg));
18041 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018042 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18043 {
18044 /* set mark */
18045 if (setmark_pos(name[1], &pos, fnum) == OK)
18046 rettv->vval.v_number = 0;
18047 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018048 else
18049 EMSG(_(e_invarg));
18050 }
18051 }
18052}
18053
18054/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018055 * "setqflist()" function
18056 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018058f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018059{
18060 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18061}
18062
18063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 * "setreg()" function
18065 */
18066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018067f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068{
18069 int regname;
18070 char_u *strregname;
18071 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018072 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 int append;
18074 char_u yank_type;
18075 long block_len;
18076
18077 block_len = -1;
18078 yank_type = MAUTO;
18079 append = FALSE;
18080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018081 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018082 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018084 if (strregname == NULL)
18085 return; /* type error; errmsg already given */
18086 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087 if (regname == 0 || regname == '@')
18088 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018090 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018092 stropt = get_tv_string_chk(&argvars[2]);
18093 if (stropt == NULL)
18094 return; /* type error */
18095 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 switch (*stropt)
18097 {
18098 case 'a': case 'A': /* append */
18099 append = TRUE;
18100 break;
18101 case 'v': case 'c': /* character-wise selection */
18102 yank_type = MCHAR;
18103 break;
18104 case 'V': case 'l': /* line-wise selection */
18105 yank_type = MLINE;
18106 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107 case 'b': case Ctrl_V: /* block-wise selection */
18108 yank_type = MBLOCK;
18109 if (VIM_ISDIGIT(stropt[1]))
18110 {
18111 ++stropt;
18112 block_len = getdigits(&stropt) - 1;
18113 --stropt;
18114 }
18115 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 }
18117 }
18118
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018119 if (argvars[1].v_type == VAR_LIST)
18120 {
18121 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018122 char_u **allocval;
18123 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018124 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018125 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018126 int len = argvars[1].vval.v_list->lv_len;
18127 listitem_T *li;
18128
Bram Moolenaar7d647822014-04-05 21:28:56 +020018129 /* First half: use for pointers to result lines; second half: use for
18130 * pointers to allocated copies. */
18131 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018132 if (lstval == NULL)
18133 return;
18134 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018135 allocval = lstval + len + 2;
18136 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018137
18138 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18139 li = li->li_next)
18140 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018141 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018142 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018143 goto free_lstval;
18144 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018145 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018146 /* Need to make a copy, next get_tv_string_buf_chk() will
18147 * overwrite the string. */
18148 strval = vim_strsave(buf);
18149 if (strval == NULL)
18150 goto free_lstval;
18151 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018152 }
18153 *curval++ = strval;
18154 }
18155 *curval++ = NULL;
18156
18157 write_reg_contents_lst(regname, lstval, -1,
18158 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018159free_lstval:
18160 while (curallocval > allocval)
18161 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018162 vim_free(lstval);
18163 }
18164 else
18165 {
18166 strval = get_tv_string_chk(&argvars[1]);
18167 if (strval == NULL)
18168 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018169 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018171 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018172 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173}
18174
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018175/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018176 * "settabvar()" function
18177 */
18178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018179f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018180{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018181#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018182 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018183 tabpage_T *tp;
18184#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018185 char_u *varname, *tabvarname;
18186 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018187
18188 rettv->vval.v_number = 0;
18189
18190 if (check_restricted() || check_secure())
18191 return;
18192
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018193#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018194 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018195#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018196 varname = get_tv_string_chk(&argvars[1]);
18197 varp = &argvars[2];
18198
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018199 if (varname != NULL && varp != NULL
18200#ifdef FEAT_WINDOWS
18201 && tp != NULL
18202#endif
18203 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018204 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018205#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018206 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018207 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018208#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018209
18210 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18211 if (tabvarname != NULL)
18212 {
18213 STRCPY(tabvarname, "t:");
18214 STRCPY(tabvarname + 2, varname);
18215 set_var(tabvarname, varp, TRUE);
18216 vim_free(tabvarname);
18217 }
18218
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018219#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018220 /* Restore current tabpage */
18221 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018222 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018223#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018224 }
18225}
18226
18227/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018228 * "settabwinvar()" function
18229 */
18230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018231f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018232{
18233 setwinvar(argvars, rettv, 1);
18234}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235
18236/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018237 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018240f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018242 setwinvar(argvars, rettv, 0);
18243}
18244
18245/*
18246 * "setwinvar()" and "settabwinvar()" functions
18247 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018248
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018250setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018251{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 win_T *win;
18253#ifdef FEAT_WINDOWS
18254 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018255 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018256 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257#endif
18258 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018259 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018261 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262
18263 if (check_restricted() || check_secure())
18264 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018265
18266#ifdef FEAT_WINDOWS
18267 if (off == 1)
18268 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18269 else
18270 tp = curtab;
18271#endif
18272 win = find_win_by_nr(&argvars[off], tp);
18273 varname = get_tv_string_chk(&argvars[off + 1]);
18274 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275
18276 if (win != NULL && varname != NULL && varp != NULL)
18277 {
18278#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018279 need_switch_win = !(tp == curtab && win == curwin);
18280 if (!need_switch_win
18281 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018284 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018285 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018286 long numval;
18287 char_u *strval;
18288 int error = FALSE;
18289
18290 ++varname;
18291 numval = get_tv_number_chk(varp, &error);
18292 strval = get_tv_string_buf_chk(varp, nbuf);
18293 if (!error && strval != NULL)
18294 set_option_value(varname, numval, strval, OPT_LOCAL);
18295 }
18296 else
18297 {
18298 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18299 if (winvarname != NULL)
18300 {
18301 STRCPY(winvarname, "w:");
18302 STRCPY(winvarname + 2, varname);
18303 set_var(winvarname, varp, TRUE);
18304 vim_free(winvarname);
18305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 }
18307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018309 if (need_switch_win)
18310 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311#endif
18312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313}
18314
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018315#ifdef FEAT_CRYPT
18316/*
18317 * "sha256({string})" function
18318 */
18319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018320f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018321{
18322 char_u *p;
18323
18324 p = get_tv_string(&argvars[0]);
18325 rettv->vval.v_string = vim_strsave(
18326 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18327 rettv->v_type = VAR_STRING;
18328}
18329#endif /* FEAT_CRYPT */
18330
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018332 * "shellescape({string})" function
18333 */
18334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018335f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018336{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018337 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018338 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018339 rettv->v_type = VAR_STRING;
18340}
18341
18342/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018343 * shiftwidth() function
18344 */
18345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018346f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018347{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018348 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018349}
18350
18351/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018352 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 */
18354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018355f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018357 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358
Bram Moolenaar0d660222005-01-07 21:51:51 +000018359 p = get_tv_string(&argvars[0]);
18360 rettv->vval.v_string = vim_strsave(p);
18361 simplify_filename(rettv->vval.v_string); /* simplify in place */
18362 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363}
18364
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018365#ifdef FEAT_FLOAT
18366/*
18367 * "sin()" function
18368 */
18369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018370f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018371{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018372 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018373
18374 rettv->v_type = VAR_FLOAT;
18375 if (get_float_arg(argvars, &f) == OK)
18376 rettv->vval.v_float = sin(f);
18377 else
18378 rettv->vval.v_float = 0.0;
18379}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018380
18381/*
18382 * "sinh()" function
18383 */
18384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018385f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018386{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018387 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018388
18389 rettv->v_type = VAR_FLOAT;
18390 if (get_float_arg(argvars, &f) == OK)
18391 rettv->vval.v_float = sinh(f);
18392 else
18393 rettv->vval.v_float = 0.0;
18394}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018395#endif
18396
Bram Moolenaar0d660222005-01-07 21:51:51 +000018397static int
18398#ifdef __BORLANDC__
18399 _RTLENTRYF
18400#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018401 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018402static int
18403#ifdef __BORLANDC__
18404 _RTLENTRYF
18405#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018406 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018407
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018408/* struct used in the array that's given to qsort() */
18409typedef struct
18410{
18411 listitem_T *item;
18412 int idx;
18413} sortItem_T;
18414
Bram Moolenaar0d660222005-01-07 21:51:51 +000018415static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018416static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018417static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018418#ifdef FEAT_FLOAT
18419static int item_compare_float;
18420#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018421static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018422static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018423static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018424static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018425static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018426#define ITEM_COMPARE_FAIL 999
18427
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018429 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018430 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018431 static int
18432#ifdef __BORLANDC__
18433_RTLENTRYF
18434#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018435item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018437 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018438 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018439 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018440 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018441 int res;
18442 char_u numbuf1[NUMBUFLEN];
18443 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018445 si1 = (sortItem_T *)s1;
18446 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018447 tv1 = &si1->item->li_tv;
18448 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018449
18450 if (item_compare_numbers)
18451 {
18452 long v1 = get_tv_number(tv1);
18453 long v2 = get_tv_number(tv2);
18454
18455 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18456 }
18457
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018458#ifdef FEAT_FLOAT
18459 if (item_compare_float)
18460 {
18461 float_T v1 = get_tv_float(tv1);
18462 float_T v2 = get_tv_float(tv2);
18463
18464 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18465 }
18466#endif
18467
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018468 /* tv2string() puts quotes around a string and allocates memory. Don't do
18469 * that for string variables. Use a single quote when comparing with a
18470 * non-string to do what the docs promise. */
18471 if (tv1->v_type == VAR_STRING)
18472 {
18473 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18474 p1 = (char_u *)"'";
18475 else
18476 p1 = tv1->vval.v_string;
18477 }
18478 else
18479 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18480 if (tv2->v_type == VAR_STRING)
18481 {
18482 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18483 p2 = (char_u *)"'";
18484 else
18485 p2 = tv2->vval.v_string;
18486 }
18487 else
18488 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018489 if (p1 == NULL)
18490 p1 = (char_u *)"";
18491 if (p2 == NULL)
18492 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018493 if (!item_compare_numeric)
18494 {
18495 if (item_compare_ic)
18496 res = STRICMP(p1, p2);
18497 else
18498 res = STRCMP(p1, p2);
18499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018501 {
18502 double n1, n2;
18503 n1 = strtod((char *)p1, (char **)&p1);
18504 n2 = strtod((char *)p2, (char **)&p2);
18505 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18506 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018507
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018508 /* When the result would be zero, compare the item indexes. Makes the
18509 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018510 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018511 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018512
Bram Moolenaar0d660222005-01-07 21:51:51 +000018513 vim_free(tofree1);
18514 vim_free(tofree2);
18515 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516}
18517
18518 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018519#ifdef __BORLANDC__
18520_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018522item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018523{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018524 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018525 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018526 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018527 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018528 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018530 /* shortcut after failure in previous call; compare all items equal */
18531 if (item_compare_func_err)
18532 return 0;
18533
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018534 si1 = (sortItem_T *)s1;
18535 si2 = (sortItem_T *)s2;
18536
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018537 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018538 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018539 copy_tv(&si1->item->li_tv, &argv[0]);
18540 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018541
18542 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018543 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018544 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18545 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018546 clear_tv(&argv[0]);
18547 clear_tv(&argv[1]);
18548
18549 if (res == FAIL)
18550 res = ITEM_COMPARE_FAIL;
18551 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018552 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18553 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018554 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018555 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018556
18557 /* When the result would be zero, compare the pointers themselves. Makes
18558 * the sort stable. */
18559 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018560 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018561
Bram Moolenaar0d660222005-01-07 21:51:51 +000018562 return res;
18563}
18564
18565/*
18566 * "sort({list})" function
18567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018569do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570{
Bram Moolenaar33570922005-01-25 22:26:29 +000018571 list_T *l;
18572 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018573 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018574 long len;
18575 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576
Bram Moolenaar0d660222005-01-07 21:51:51 +000018577 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018578 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 else
18580 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018581 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018582 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018583 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18584 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018585 return;
18586 rettv->vval.v_list = l;
18587 rettv->v_type = VAR_LIST;
18588 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589
Bram Moolenaar0d660222005-01-07 21:51:51 +000018590 len = list_len(l);
18591 if (len <= 1)
18592 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593
Bram Moolenaar0d660222005-01-07 21:51:51 +000018594 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018595 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018596 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018597#ifdef FEAT_FLOAT
18598 item_compare_float = FALSE;
18599#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018600 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018601 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018602 if (argvars[1].v_type != VAR_UNKNOWN)
18603 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018604 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018605 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018606 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018607 else
18608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018609 int error = FALSE;
18610
18611 i = get_tv_number_chk(&argvars[1], &error);
18612 if (error)
18613 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018614 if (i == 1)
18615 item_compare_ic = TRUE;
18616 else
18617 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018618 if (item_compare_func != NULL)
18619 {
18620 if (STRCMP(item_compare_func, "n") == 0)
18621 {
18622 item_compare_func = NULL;
18623 item_compare_numeric = TRUE;
18624 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018625 else if (STRCMP(item_compare_func, "N") == 0)
18626 {
18627 item_compare_func = NULL;
18628 item_compare_numbers = TRUE;
18629 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018630#ifdef FEAT_FLOAT
18631 else if (STRCMP(item_compare_func, "f") == 0)
18632 {
18633 item_compare_func = NULL;
18634 item_compare_float = TRUE;
18635 }
18636#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018637 else if (STRCMP(item_compare_func, "i") == 0)
18638 {
18639 item_compare_func = NULL;
18640 item_compare_ic = TRUE;
18641 }
18642 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018643 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018644
18645 if (argvars[2].v_type != VAR_UNKNOWN)
18646 {
18647 /* optional third argument: {dict} */
18648 if (argvars[2].v_type != VAR_DICT)
18649 {
18650 EMSG(_(e_dictreq));
18651 return;
18652 }
18653 item_compare_selfdict = argvars[2].vval.v_dict;
18654 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656
Bram Moolenaar0d660222005-01-07 21:51:51 +000018657 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018658 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018659 if (ptrs == NULL)
18660 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661
Bram Moolenaar327aa022014-03-25 18:24:23 +010018662 i = 0;
18663 if (sort)
18664 {
18665 /* sort(): ptrs will be the list to sort */
18666 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018667 {
18668 ptrs[i].item = li;
18669 ptrs[i].idx = i;
18670 ++i;
18671 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018672
18673 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018674 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018675 /* test the compare function */
18676 if (item_compare_func != NULL
18677 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018678 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018679 EMSG(_("E702: Sort compare function failed"));
18680 else
18681 {
18682 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018683 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018684 item_compare_func == NULL ? item_compare : item_compare2);
18685
18686 if (!item_compare_func_err)
18687 {
18688 /* Clear the List and append the items in sorted order. */
18689 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18690 l->lv_len = 0;
18691 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018692 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018693 }
18694 }
18695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018697 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018698 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018699
18700 /* f_uniq(): ptrs will be a stack of items to remove */
18701 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018702 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018703 item_compare_func_ptr = item_compare_func
18704 ? item_compare2 : item_compare;
18705
18706 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18707 li = li->li_next)
18708 {
18709 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18710 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018711 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018712 if (item_compare_func_err)
18713 {
18714 EMSG(_("E882: Uniq compare function failed"));
18715 break;
18716 }
18717 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018719 if (!item_compare_func_err)
18720 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018721 while (--i >= 0)
18722 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018723 li = ptrs[i].item->li_next;
18724 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018725 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018726 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018727 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018728 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018729 list_fix_watch(l, li);
18730 listitem_free(li);
18731 l->lv_len--;
18732 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018733 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018734 }
18735
18736 vim_free(ptrs);
18737 }
18738}
18739
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018740/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018741 * "sort({list})" function
18742 */
18743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018744f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018745{
18746 do_sort_uniq(argvars, rettv, TRUE);
18747}
18748
18749/*
18750 * "uniq({list})" function
18751 */
18752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018753f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018754{
18755 do_sort_uniq(argvars, rettv, FALSE);
18756}
18757
18758/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018759 * "soundfold({word})" function
18760 */
18761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018762f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018763{
18764 char_u *s;
18765
18766 rettv->v_type = VAR_STRING;
18767 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018768#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018769 rettv->vval.v_string = eval_soundfold(s);
18770#else
18771 rettv->vval.v_string = vim_strsave(s);
18772#endif
18773}
18774
18775/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018776 * "spellbadword()" function
18777 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018779f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018780{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018781 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018782 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018783 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018784
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018785 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018786 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018787
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018788#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018789 if (argvars[0].v_type == VAR_UNKNOWN)
18790 {
18791 /* Find the start and length of the badly spelled word. */
18792 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18793 if (len != 0)
18794 word = ml_get_cursor();
18795 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018796 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018797 {
18798 char_u *str = get_tv_string_chk(&argvars[0]);
18799 int capcol = -1;
18800
18801 if (str != NULL)
18802 {
18803 /* Check the argument for spelling. */
18804 while (*str != NUL)
18805 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018806 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018807 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018808 {
18809 word = str;
18810 break;
18811 }
18812 str += len;
18813 }
18814 }
18815 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018816#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018817
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018818 list_append_string(rettv->vval.v_list, word, len);
18819 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018820 attr == HLF_SPB ? "bad" :
18821 attr == HLF_SPR ? "rare" :
18822 attr == HLF_SPL ? "local" :
18823 attr == HLF_SPC ? "caps" :
18824 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018825}
18826
18827/*
18828 * "spellsuggest()" function
18829 */
18830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018831f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018832{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018833#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018834 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018835 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018836 int maxcount;
18837 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018838 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018839 listitem_T *li;
18840 int need_capital = FALSE;
18841#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018842
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018843 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018844 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018845
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018846#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018847 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018848 {
18849 str = get_tv_string(&argvars[0]);
18850 if (argvars[1].v_type != VAR_UNKNOWN)
18851 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018852 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018853 if (maxcount <= 0)
18854 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018855 if (argvars[2].v_type != VAR_UNKNOWN)
18856 {
18857 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18858 if (typeerr)
18859 return;
18860 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018861 }
18862 else
18863 maxcount = 25;
18864
Bram Moolenaar4770d092006-01-12 23:22:24 +000018865 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018866
18867 for (i = 0; i < ga.ga_len; ++i)
18868 {
18869 str = ((char_u **)ga.ga_data)[i];
18870
18871 li = listitem_alloc();
18872 if (li == NULL)
18873 vim_free(str);
18874 else
18875 {
18876 li->li_tv.v_type = VAR_STRING;
18877 li->li_tv.v_lock = 0;
18878 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018879 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018880 }
18881 }
18882 ga_clear(&ga);
18883 }
18884#endif
18885}
18886
Bram Moolenaar0d660222005-01-07 21:51:51 +000018887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018888f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018889{
18890 char_u *str;
18891 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018892 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018893 regmatch_T regmatch;
18894 char_u patbuf[NUMBUFLEN];
18895 char_u *save_cpo;
18896 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018897 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018898 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018899 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018900
18901 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18902 save_cpo = p_cpo;
18903 p_cpo = (char_u *)"";
18904
18905 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018906 if (argvars[1].v_type != VAR_UNKNOWN)
18907 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018908 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18909 if (pat == NULL)
18910 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018911 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018912 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018913 }
18914 if (pat == NULL || *pat == NUL)
18915 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018916
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018917 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018919 if (typeerr)
18920 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921
Bram Moolenaar0d660222005-01-07 21:51:51 +000018922 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18923 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018925 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018926 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018927 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018928 if (*str == NUL)
18929 match = FALSE; /* empty item at the end */
18930 else
18931 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018932 if (match)
18933 end = regmatch.startp[0];
18934 else
18935 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018936 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18937 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018938 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018939 if (list_append_string(rettv->vval.v_list, str,
18940 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018941 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018942 }
18943 if (!match)
18944 break;
18945 /* Advance to just after the match. */
18946 if (regmatch.endp[0] > str)
18947 col = 0;
18948 else
18949 {
18950 /* Don't get stuck at the same match. */
18951#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018952 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018953#else
18954 col = 1;
18955#endif
18956 }
18957 str = regmatch.endp[0];
18958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959
Bram Moolenaar473de612013-06-08 18:19:48 +020018960 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964}
18965
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018966#ifdef FEAT_FLOAT
18967/*
18968 * "sqrt()" function
18969 */
18970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018971f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018972{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018973 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018974
18975 rettv->v_type = VAR_FLOAT;
18976 if (get_float_arg(argvars, &f) == OK)
18977 rettv->vval.v_float = sqrt(f);
18978 else
18979 rettv->vval.v_float = 0.0;
18980}
18981
18982/*
18983 * "str2float()" function
18984 */
18985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018986f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018987{
18988 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18989
18990 if (*p == '+')
18991 p = skipwhite(p + 1);
18992 (void)string2float(p, &rettv->vval.v_float);
18993 rettv->v_type = VAR_FLOAT;
18994}
18995#endif
18996
Bram Moolenaar2c932302006-03-18 21:42:09 +000018997/*
18998 * "str2nr()" function
18999 */
19000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019001f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019002{
19003 int base = 10;
19004 char_u *p;
19005 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019006 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019007
19008 if (argvars[1].v_type != VAR_UNKNOWN)
19009 {
19010 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019011 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019012 {
19013 EMSG(_(e_invarg));
19014 return;
19015 }
19016 }
19017
19018 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019019 if (*p == '+')
19020 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019021 switch (base)
19022 {
19023 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19024 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19025 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19026 default: what = 0;
19027 }
19028 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019029 rettv->vval.v_number = n;
19030}
19031
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032#ifdef HAVE_STRFTIME
19033/*
19034 * "strftime({format}[, {time}])" function
19035 */
19036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019037f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038{
19039 char_u result_buf[256];
19040 struct tm *curtime;
19041 time_t seconds;
19042 char_u *p;
19043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019044 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019046 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019047 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 seconds = time(NULL);
19049 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019050 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 curtime = localtime(&seconds);
19052 /* MSVC returns NULL for an invalid value of seconds. */
19053 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019054 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 else
19056 {
19057# ifdef FEAT_MBYTE
19058 vimconv_T conv;
19059 char_u *enc;
19060
19061 conv.vc_type = CONV_NONE;
19062 enc = enc_locale();
19063 convert_setup(&conv, p_enc, enc);
19064 if (conv.vc_type != CONV_NONE)
19065 p = string_convert(&conv, p, NULL);
19066# endif
19067 if (p != NULL)
19068 (void)strftime((char *)result_buf, sizeof(result_buf),
19069 (char *)p, curtime);
19070 else
19071 result_buf[0] = NUL;
19072
19073# ifdef FEAT_MBYTE
19074 if (conv.vc_type != CONV_NONE)
19075 vim_free(p);
19076 convert_setup(&conv, enc, p_enc);
19077 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019078 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079 else
19080# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019081 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082
19083# ifdef FEAT_MBYTE
19084 /* Release conversion descriptors */
19085 convert_setup(&conv, NULL, NULL);
19086 vim_free(enc);
19087# endif
19088 }
19089}
19090#endif
19091
19092/*
19093 * "stridx()" function
19094 */
19095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019096f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097{
19098 char_u buf[NUMBUFLEN];
19099 char_u *needle;
19100 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019101 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019103 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019105 needle = get_tv_string_chk(&argvars[1]);
19106 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019107 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019108 if (needle == NULL || haystack == NULL)
19109 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110
Bram Moolenaar33570922005-01-25 22:26:29 +000019111 if (argvars[2].v_type != VAR_UNKNOWN)
19112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019113 int error = FALSE;
19114
19115 start_idx = get_tv_number_chk(&argvars[2], &error);
19116 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019117 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019118 if (start_idx >= 0)
19119 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019120 }
19121
19122 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19123 if (pos != NULL)
19124 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125}
19126
19127/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019128 * "string()" function
19129 */
19130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019131f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019132{
19133 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019134 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019136 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019137 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019138 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019139 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019140 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141}
19142
19143/*
19144 * "strlen()" function
19145 */
19146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019147f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019149 rettv->vval.v_number = (varnumber_T)(STRLEN(
19150 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151}
19152
19153/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019154 * "strchars()" function
19155 */
19156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019157f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019158{
19159 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019160 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019161#ifdef FEAT_MBYTE
19162 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019163 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019164#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019165
19166 if (argvars[1].v_type != VAR_UNKNOWN)
19167 skipcc = get_tv_number_chk(&argvars[1], NULL);
19168 if (skipcc < 0 || skipcc > 1)
19169 EMSG(_(e_invarg));
19170 else
19171 {
19172#ifdef FEAT_MBYTE
19173 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19174 while (*s != NUL)
19175 {
19176 func_mb_ptr2char_adv(&s);
19177 ++len;
19178 }
19179 rettv->vval.v_number = len;
19180#else
19181 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19182#endif
19183 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019184}
19185
19186/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019187 * "strdisplaywidth()" function
19188 */
19189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019190f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019191{
19192 char_u *s = get_tv_string(&argvars[0]);
19193 int col = 0;
19194
19195 if (argvars[1].v_type != VAR_UNKNOWN)
19196 col = get_tv_number(&argvars[1]);
19197
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019198 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019199}
19200
19201/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019202 * "strwidth()" function
19203 */
19204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019205f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019206{
19207 char_u *s = get_tv_string(&argvars[0]);
19208
19209 rettv->vval.v_number = (varnumber_T)(
19210#ifdef FEAT_MBYTE
19211 mb_string2cells(s, -1)
19212#else
19213 STRLEN(s)
19214#endif
19215 );
19216}
19217
19218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219 * "strpart()" function
19220 */
19221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019222f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223{
19224 char_u *p;
19225 int n;
19226 int len;
19227 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019228 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019230 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 slen = (int)STRLEN(p);
19232
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019233 n = get_tv_number_chk(&argvars[1], &error);
19234 if (error)
19235 len = 0;
19236 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019237 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238 else
19239 len = slen - n; /* default len: all bytes that are available. */
19240
19241 /*
19242 * Only return the overlap between the specified part and the actual
19243 * string.
19244 */
19245 if (n < 0)
19246 {
19247 len += n;
19248 n = 0;
19249 }
19250 else if (n > slen)
19251 n = slen;
19252 if (len < 0)
19253 len = 0;
19254 else if (n + len > slen)
19255 len = slen - n;
19256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019257 rettv->v_type = VAR_STRING;
19258 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259}
19260
19261/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019262 * "strridx()" function
19263 */
19264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019265f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019266{
19267 char_u buf[NUMBUFLEN];
19268 char_u *needle;
19269 char_u *haystack;
19270 char_u *rest;
19271 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019272 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019274 needle = get_tv_string_chk(&argvars[1]);
19275 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019276
19277 rettv->vval.v_number = -1;
19278 if (needle == NULL || haystack == NULL)
19279 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019280
19281 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019282 if (argvars[2].v_type != VAR_UNKNOWN)
19283 {
19284 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019285 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019286 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019287 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019288 }
19289 else
19290 end_idx = haystack_len;
19291
Bram Moolenaar0d660222005-01-07 21:51:51 +000019292 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019293 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019294 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019295 lastmatch = haystack + end_idx;
19296 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019297 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019298 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019299 for (rest = haystack; *rest != '\0'; ++rest)
19300 {
19301 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019302 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019303 break;
19304 lastmatch = rest;
19305 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019306 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019307
19308 if (lastmatch == NULL)
19309 rettv->vval.v_number = -1;
19310 else
19311 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19312}
19313
19314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315 * "strtrans()" function
19316 */
19317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019318f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019320 rettv->v_type = VAR_STRING;
19321 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019322}
19323
19324/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019325 * "submatch()" function
19326 */
19327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019328f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019329{
Bram Moolenaar41571762014-04-02 19:00:58 +020019330 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019331 int no;
19332 int retList = 0;
19333
19334 no = (int)get_tv_number_chk(&argvars[0], &error);
19335 if (error)
19336 return;
19337 error = FALSE;
19338 if (argvars[1].v_type != VAR_UNKNOWN)
19339 retList = get_tv_number_chk(&argvars[1], &error);
19340 if (error)
19341 return;
19342
19343 if (retList == 0)
19344 {
19345 rettv->v_type = VAR_STRING;
19346 rettv->vval.v_string = reg_submatch(no);
19347 }
19348 else
19349 {
19350 rettv->v_type = VAR_LIST;
19351 rettv->vval.v_list = reg_submatch_list(no);
19352 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019353}
19354
19355/*
19356 * "substitute()" function
19357 */
19358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019359f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019360{
19361 char_u patbuf[NUMBUFLEN];
19362 char_u subbuf[NUMBUFLEN];
19363 char_u flagsbuf[NUMBUFLEN];
19364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019365 char_u *str = get_tv_string_chk(&argvars[0]);
19366 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19367 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19368 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19369
Bram Moolenaar0d660222005-01-07 21:51:51 +000019370 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019371 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19372 rettv->vval.v_string = NULL;
19373 else
19374 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019375}
19376
19377/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019378 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019381f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382{
19383 int id = 0;
19384#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019385 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386 long col;
19387 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019388 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019390 lnum = get_tv_lnum(argvars); /* -1 on type error */
19391 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19392 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019394 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019395 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019396 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397#endif
19398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019399 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400}
19401
19402/*
19403 * "synIDattr(id, what [, mode])" function
19404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019406f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407{
19408 char_u *p = NULL;
19409#ifdef FEAT_SYN_HL
19410 int id;
19411 char_u *what;
19412 char_u *mode;
19413 char_u modebuf[NUMBUFLEN];
19414 int modec;
19415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019416 id = get_tv_number(&argvars[0]);
19417 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019418 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019420 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019422 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423 modec = 0; /* replace invalid with current */
19424 }
19425 else
19426 {
19427#ifdef FEAT_GUI
19428 if (gui.in_use)
19429 modec = 'g';
19430 else
19431#endif
19432 if (t_colors > 1)
19433 modec = 'c';
19434 else
19435 modec = 't';
19436 }
19437
19438
19439 switch (TOLOWER_ASC(what[0]))
19440 {
19441 case 'b':
19442 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19443 p = highlight_color(id, what, modec);
19444 else /* bold */
19445 p = highlight_has_attr(id, HL_BOLD, modec);
19446 break;
19447
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019448 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 p = highlight_color(id, what, modec);
19450 break;
19451
19452 case 'i':
19453 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19454 p = highlight_has_attr(id, HL_INVERSE, modec);
19455 else /* italic */
19456 p = highlight_has_attr(id, HL_ITALIC, modec);
19457 break;
19458
19459 case 'n': /* name */
19460 p = get_highlight_name(NULL, id - 1);
19461 break;
19462
19463 case 'r': /* reverse */
19464 p = highlight_has_attr(id, HL_INVERSE, modec);
19465 break;
19466
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019467 case 's':
19468 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19469 p = highlight_color(id, what, modec);
19470 else /* standout */
19471 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 break;
19473
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019474 case 'u':
19475 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19476 /* underline */
19477 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19478 else
19479 /* undercurl */
19480 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 break;
19482 }
19483
19484 if (p != NULL)
19485 p = vim_strsave(p);
19486#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019487 rettv->v_type = VAR_STRING;
19488 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489}
19490
19491/*
19492 * "synIDtrans(id)" function
19493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019495f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496{
19497 int id;
19498
19499#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019500 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501
19502 if (id > 0)
19503 id = syn_get_final_id(id);
19504 else
19505#endif
19506 id = 0;
19507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019508 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509}
19510
19511/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019512 * "synconcealed(lnum, col)" function
19513 */
19514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019515f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019516{
19517#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19518 long lnum;
19519 long col;
19520 int syntax_flags = 0;
19521 int cchar;
19522 int matchid = 0;
19523 char_u str[NUMBUFLEN];
19524#endif
19525
19526 rettv->v_type = VAR_LIST;
19527 rettv->vval.v_list = NULL;
19528
19529#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19530 lnum = get_tv_lnum(argvars); /* -1 on type error */
19531 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19532
19533 vim_memset(str, NUL, sizeof(str));
19534
19535 if (rettv_list_alloc(rettv) != FAIL)
19536 {
19537 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19538 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19539 && curwin->w_p_cole > 0)
19540 {
19541 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19542 syntax_flags = get_syntax_info(&matchid);
19543
19544 /* get the conceal character */
19545 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19546 {
19547 cchar = syn_get_sub_char();
19548 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19549 cchar = lcs_conceal;
19550 if (cchar != NUL)
19551 {
19552# ifdef FEAT_MBYTE
19553 if (has_mbyte)
19554 (*mb_char2bytes)(cchar, str);
19555 else
19556# endif
19557 str[0] = cchar;
19558 }
19559 }
19560 }
19561
19562 list_append_number(rettv->vval.v_list,
19563 (syntax_flags & HL_CONCEAL) != 0);
19564 /* -1 to auto-determine strlen */
19565 list_append_string(rettv->vval.v_list, str, -1);
19566 list_append_number(rettv->vval.v_list, matchid);
19567 }
19568#endif
19569}
19570
19571/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019572 * "synstack(lnum, col)" function
19573 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019575f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019576{
19577#ifdef FEAT_SYN_HL
19578 long lnum;
19579 long col;
19580 int i;
19581 int id;
19582#endif
19583
19584 rettv->v_type = VAR_LIST;
19585 rettv->vval.v_list = NULL;
19586
19587#ifdef FEAT_SYN_HL
19588 lnum = get_tv_lnum(argvars); /* -1 on type error */
19589 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19590
19591 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019592 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019593 && rettv_list_alloc(rettv) != FAIL)
19594 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019595 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019596 for (i = 0; ; ++i)
19597 {
19598 id = syn_get_stack_item(i);
19599 if (id < 0)
19600 break;
19601 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19602 break;
19603 }
19604 }
19605#endif
19606}
19607
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019609get_cmd_output_as_rettv(
19610 typval_T *argvars,
19611 typval_T *rettv,
19612 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019614 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019616 char_u *infile = NULL;
19617 char_u buf[NUMBUFLEN];
19618 int err = FALSE;
19619 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019620 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019621 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019623 rettv->v_type = VAR_STRING;
19624 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019625 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019626 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019627
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019628 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019629 {
19630 /*
19631 * Write the string to a temp file, to be used for input of the shell
19632 * command.
19633 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019634 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019635 {
19636 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019637 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019638 }
19639
19640 fd = mch_fopen((char *)infile, WRITEBIN);
19641 if (fd == NULL)
19642 {
19643 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019644 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019645 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019646 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019647 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019648 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19649 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019650 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019651 else
19652 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019653 size_t len;
19654
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019655 p = get_tv_string_buf_chk(&argvars[1], buf);
19656 if (p == NULL)
19657 {
19658 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019659 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019660 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019661 len = STRLEN(p);
19662 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019663 err = TRUE;
19664 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019665 if (fclose(fd) != 0)
19666 err = TRUE;
19667 if (err)
19668 {
19669 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019670 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019671 }
19672 }
19673
Bram Moolenaar52a72462014-08-29 15:53:52 +020019674 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19675 * echoes typeahead, that messes up the display. */
19676 if (!msg_silent)
19677 flags += SHELL_COOKED;
19678
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019679 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019681 int len;
19682 listitem_T *li;
19683 char_u *s = NULL;
19684 char_u *start;
19685 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019686 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687
Bram Moolenaar52a72462014-08-29 15:53:52 +020019688 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019689 if (res == NULL)
19690 goto errret;
19691
19692 list = list_alloc();
19693 if (list == NULL)
19694 goto errret;
19695
19696 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019698 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019699 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019700 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019701 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019702
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019703 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019704 if (s == NULL)
19705 goto errret;
19706
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019707 for (p = s; start < end; ++p, ++start)
19708 *p = *start == NUL ? NL : *start;
19709 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019710
19711 li = listitem_alloc();
19712 if (li == NULL)
19713 {
19714 vim_free(s);
19715 goto errret;
19716 }
19717 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019718 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019719 li->li_tv.vval.v_string = s;
19720 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019722
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019723 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019724 rettv->v_type = VAR_LIST;
19725 rettv->vval.v_list = list;
19726 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019728 else
19729 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019730 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019731#ifdef USE_CR
19732 /* translate <CR> into <NL> */
19733 if (res != NULL)
19734 {
19735 char_u *s;
19736
19737 for (s = res; *s; ++s)
19738 {
19739 if (*s == CAR)
19740 *s = NL;
19741 }
19742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743#else
19744# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019745 /* translate <CR><NL> into <NL> */
19746 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019748 char_u *s, *d;
19749
19750 d = res;
19751 for (s = res; *s; ++s)
19752 {
19753 if (s[0] == CAR && s[1] == NL)
19754 ++s;
19755 *d++ = *s;
19756 }
19757 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759# endif
19760#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019761 rettv->vval.v_string = res;
19762 res = NULL;
19763 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019764
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019765errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019766 if (infile != NULL)
19767 {
19768 mch_remove(infile);
19769 vim_free(infile);
19770 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019771 if (res != NULL)
19772 vim_free(res);
19773 if (list != NULL)
19774 list_free(list, TRUE);
19775}
19776
19777/*
19778 * "system()" function
19779 */
19780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019781f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019782{
19783 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19784}
19785
19786/*
19787 * "systemlist()" function
19788 */
19789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019790f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019791{
19792 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793}
19794
19795/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019796 * "tabpagebuflist()" function
19797 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019799f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019800{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019801#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019802 tabpage_T *tp;
19803 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019804
19805 if (argvars[0].v_type == VAR_UNKNOWN)
19806 wp = firstwin;
19807 else
19808 {
19809 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19810 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019811 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019812 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019813 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019814 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019815 for (; wp != NULL; wp = wp->w_next)
19816 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019817 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019818 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019819 }
19820#endif
19821}
19822
19823
19824/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019825 * "tabpagenr()" function
19826 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019828f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019829{
19830 int nr = 1;
19831#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019832 char_u *arg;
19833
19834 if (argvars[0].v_type != VAR_UNKNOWN)
19835 {
19836 arg = get_tv_string_chk(&argvars[0]);
19837 nr = 0;
19838 if (arg != NULL)
19839 {
19840 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019841 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019842 else
19843 EMSG2(_(e_invexpr2), arg);
19844 }
19845 }
19846 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019847 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019848#endif
19849 rettv->vval.v_number = nr;
19850}
19851
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019852
19853#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019854static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019855
19856/*
19857 * Common code for tabpagewinnr() and winnr().
19858 */
19859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019860get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019861{
19862 win_T *twin;
19863 int nr = 1;
19864 win_T *wp;
19865 char_u *arg;
19866
19867 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19868 if (argvar->v_type != VAR_UNKNOWN)
19869 {
19870 arg = get_tv_string_chk(argvar);
19871 if (arg == NULL)
19872 nr = 0; /* type error; errmsg already given */
19873 else if (STRCMP(arg, "$") == 0)
19874 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19875 else if (STRCMP(arg, "#") == 0)
19876 {
19877 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19878 if (twin == NULL)
19879 nr = 0;
19880 }
19881 else
19882 {
19883 EMSG2(_(e_invexpr2), arg);
19884 nr = 0;
19885 }
19886 }
19887
19888 if (nr > 0)
19889 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19890 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019891 {
19892 if (wp == NULL)
19893 {
19894 /* didn't find it in this tabpage */
19895 nr = 0;
19896 break;
19897 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019898 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019899 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019900 return nr;
19901}
19902#endif
19903
19904/*
19905 * "tabpagewinnr()" function
19906 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019908f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019909{
19910 int nr = 1;
19911#ifdef FEAT_WINDOWS
19912 tabpage_T *tp;
19913
19914 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19915 if (tp == NULL)
19916 nr = 0;
19917 else
19918 nr = get_winnr(tp, &argvars[1]);
19919#endif
19920 rettv->vval.v_number = nr;
19921}
19922
19923
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019924/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019925 * "tagfiles()" function
19926 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019928f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019929{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019930 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019931 tagname_T tn;
19932 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019933
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019934 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019935 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019936 fname = alloc(MAXPATHL);
19937 if (fname == NULL)
19938 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019939
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019940 for (first = TRUE; ; first = FALSE)
19941 if (get_tagfname(&tn, first, fname) == FAIL
19942 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019943 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019944 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019945 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019946}
19947
19948/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019949 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019950 */
19951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019952f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019953{
19954 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019955
19956 tag_pattern = get_tv_string(&argvars[0]);
19957
19958 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019959 if (*tag_pattern == NUL)
19960 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019961
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019962 if (rettv_list_alloc(rettv) == OK)
19963 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019964}
19965
19966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 * "tempname()" function
19968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019970f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971{
19972 static int x = 'A';
19973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019974 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019975 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976
19977 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19978 * names. Skip 'I' and 'O', they are used for shell redirection. */
19979 do
19980 {
19981 if (x == 'Z')
19982 x = '0';
19983 else if (x == '9')
19984 x = 'A';
19985 else
19986 {
19987#ifdef EBCDIC
19988 if (x == 'I')
19989 x = 'J';
19990 else if (x == 'R')
19991 x = 'S';
19992 else
19993#endif
19994 ++x;
19995 }
19996 } while (x == 'I' || x == 'O');
19997}
19998
19999/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020000 * "test(list)" function: Just checking the walls...
20001 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020003f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020004{
20005 /* Used for unit testing. Change the code below to your liking. */
20006#if 0
20007 listitem_T *li;
20008 list_T *l;
20009 char_u *bad, *good;
20010
20011 if (argvars[0].v_type != VAR_LIST)
20012 return;
20013 l = argvars[0].vval.v_list;
20014 if (l == NULL)
20015 return;
20016 li = l->lv_first;
20017 if (li == NULL)
20018 return;
20019 bad = get_tv_string(&li->li_tv);
20020 li = li->li_next;
20021 if (li == NULL)
20022 return;
20023 good = get_tv_string(&li->li_tv);
20024 rettv->vval.v_number = test_edit_score(bad, good);
20025#endif
20026}
20027
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020028#ifdef FEAT_FLOAT
20029/*
20030 * "tan()" function
20031 */
20032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020033f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020034{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020035 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020036
20037 rettv->v_type = VAR_FLOAT;
20038 if (get_float_arg(argvars, &f) == OK)
20039 rettv->vval.v_float = tan(f);
20040 else
20041 rettv->vval.v_float = 0.0;
20042}
20043
20044/*
20045 * "tanh()" function
20046 */
20047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020048f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020049{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020050 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020051
20052 rettv->v_type = VAR_FLOAT;
20053 if (get_float_arg(argvars, &f) == OK)
20054 rettv->vval.v_float = tanh(f);
20055 else
20056 rettv->vval.v_float = 0.0;
20057}
20058#endif
20059
Bram Moolenaard52d9742005-08-21 22:20:28 +000020060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 * "tolower(string)" function
20062 */
20063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020064f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065{
20066 char_u *p;
20067
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020068 p = vim_strsave(get_tv_string(&argvars[0]));
20069 rettv->v_type = VAR_STRING;
20070 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071
20072 if (p != NULL)
20073 while (*p != NUL)
20074 {
20075#ifdef FEAT_MBYTE
20076 int l;
20077
20078 if (enc_utf8)
20079 {
20080 int c, lc;
20081
20082 c = utf_ptr2char(p);
20083 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020084 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 /* TODO: reallocate string when byte count changes. */
20086 if (utf_char2len(lc) == l)
20087 utf_char2bytes(lc, p);
20088 p += l;
20089 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020090 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 p += l; /* skip multi-byte character */
20092 else
20093#endif
20094 {
20095 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20096 ++p;
20097 }
20098 }
20099}
20100
20101/*
20102 * "toupper(string)" function
20103 */
20104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020105f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020107 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020108 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109}
20110
20111/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020112 * "tr(string, fromstr, tostr)" function
20113 */
20114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020115f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020116{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020117 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020118 char_u *fromstr;
20119 char_u *tostr;
20120 char_u *p;
20121#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020122 int inlen;
20123 int fromlen;
20124 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020125 int idx;
20126 char_u *cpstr;
20127 int cplen;
20128 int first = TRUE;
20129#endif
20130 char_u buf[NUMBUFLEN];
20131 char_u buf2[NUMBUFLEN];
20132 garray_T ga;
20133
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020134 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020135 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20136 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020137
20138 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020139 rettv->v_type = VAR_STRING;
20140 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020141 if (fromstr == NULL || tostr == NULL)
20142 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020143 ga_init2(&ga, (int)sizeof(char), 80);
20144
20145#ifdef FEAT_MBYTE
20146 if (!has_mbyte)
20147#endif
20148 /* not multi-byte: fromstr and tostr must be the same length */
20149 if (STRLEN(fromstr) != STRLEN(tostr))
20150 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020151#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020152error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020153#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020154 EMSG2(_(e_invarg2), fromstr);
20155 ga_clear(&ga);
20156 return;
20157 }
20158
20159 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020160 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020161 {
20162#ifdef FEAT_MBYTE
20163 if (has_mbyte)
20164 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020165 inlen = (*mb_ptr2len)(in_str);
20166 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020167 cplen = inlen;
20168 idx = 0;
20169 for (p = fromstr; *p != NUL; p += fromlen)
20170 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020171 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020172 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020173 {
20174 for (p = tostr; *p != NUL; p += tolen)
20175 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020176 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020177 if (idx-- == 0)
20178 {
20179 cplen = tolen;
20180 cpstr = p;
20181 break;
20182 }
20183 }
20184 if (*p == NUL) /* tostr is shorter than fromstr */
20185 goto error;
20186 break;
20187 }
20188 ++idx;
20189 }
20190
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020191 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020192 {
20193 /* Check that fromstr and tostr have the same number of
20194 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020195 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020196 first = FALSE;
20197 for (p = tostr; *p != NUL; p += tolen)
20198 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020199 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020200 --idx;
20201 }
20202 if (idx != 0)
20203 goto error;
20204 }
20205
Bram Moolenaarcde88542015-08-11 19:14:00 +020020206 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020207 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020208 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020209
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020210 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020211 }
20212 else
20213#endif
20214 {
20215 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020216 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020217 if (p != NULL)
20218 ga_append(&ga, tostr[p - fromstr]);
20219 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020220 ga_append(&ga, *in_str);
20221 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020222 }
20223 }
20224
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020225 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020226 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020227 ga_append(&ga, NUL);
20228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020229 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020230}
20231
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020232#ifdef FEAT_FLOAT
20233/*
20234 * "trunc({float})" function
20235 */
20236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020237f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020238{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020239 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020240
20241 rettv->v_type = VAR_FLOAT;
20242 if (get_float_arg(argvars, &f) == OK)
20243 /* trunc() is not in C90, use floor() or ceil() instead. */
20244 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20245 else
20246 rettv->vval.v_float = 0.0;
20247}
20248#endif
20249
Bram Moolenaar8299df92004-07-10 09:47:34 +000020250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 * "type(expr)" function
20252 */
20253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020254f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020256 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020257
20258 switch (argvars[0].v_type)
20259 {
20260 case VAR_NUMBER: n = 0; break;
20261 case VAR_STRING: n = 1; break;
20262 case VAR_FUNC: n = 2; break;
20263 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020264 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020265 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020266 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020267 if (argvars[0].vval.v_number == VVAL_FALSE
20268 || argvars[0].vval.v_number == VVAL_TRUE)
20269 n = 6;
20270 else
20271 n = 7;
20272 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020273 case VAR_JOB: n = 8; break;
20274 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020275 case VAR_UNKNOWN:
20276 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20277 n = -1;
20278 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020279 }
20280 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281}
20282
20283/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020284 * "undofile(name)" function
20285 */
20286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020287f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020288{
20289 rettv->v_type = VAR_STRING;
20290#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020291 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020292 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020293
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020294 if (*fname == NUL)
20295 {
20296 /* If there is no file name there will be no undo file. */
20297 rettv->vval.v_string = NULL;
20298 }
20299 else
20300 {
20301 char_u *ffname = FullName_save(fname, FALSE);
20302
20303 if (ffname != NULL)
20304 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20305 vim_free(ffname);
20306 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020307 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020308#else
20309 rettv->vval.v_string = NULL;
20310#endif
20311}
20312
20313/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020314 * "undotree()" function
20315 */
20316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020317f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020318{
20319 if (rettv_dict_alloc(rettv) == OK)
20320 {
20321 dict_T *dict = rettv->vval.v_dict;
20322 list_T *list;
20323
Bram Moolenaar730cde92010-06-27 05:18:54 +020020324 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020325 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020326 dict_add_nr_str(dict, "save_last",
20327 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020328 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20329 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020330 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020331
20332 list = list_alloc();
20333 if (list != NULL)
20334 {
20335 u_eval_tree(curbuf->b_u_oldhead, list);
20336 dict_add_list(dict, "entries", list);
20337 }
20338 }
20339}
20340
20341/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020342 * "values(dict)" function
20343 */
20344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020345f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020346{
20347 dict_list(argvars, rettv, 1);
20348}
20349
20350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 * "virtcol(string)" function
20352 */
20353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020354f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355{
20356 colnr_T vcol = 0;
20357 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020358 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020360 fp = var2fpos(&argvars[0], FALSE, &fnum);
20361 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20362 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 {
20364 getvvcol(curwin, fp, NULL, NULL, &vcol);
20365 ++vcol;
20366 }
20367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020368 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369}
20370
20371/*
20372 * "visualmode()" function
20373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020375f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 char_u str[2];
20378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020379 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 str[0] = curbuf->b_visual_mode_eval;
20381 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020382 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383
20384 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020385 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387}
20388
20389/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020390 * "wildmenumode()" function
20391 */
20392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020393f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020394{
20395#ifdef FEAT_WILDMENU
20396 if (wild_menu_showing)
20397 rettv->vval.v_number = 1;
20398#endif
20399}
20400
20401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402 * "winbufnr(nr)" function
20403 */
20404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020405f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406{
20407 win_T *wp;
20408
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020409 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020411 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020413 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414}
20415
20416/*
20417 * "wincol()" function
20418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020420f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421{
20422 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020423 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424}
20425
20426/*
20427 * "winheight(nr)" function
20428 */
20429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020430f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431{
20432 win_T *wp;
20433
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020434 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020436 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020438 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439}
20440
20441/*
20442 * "winline()" function
20443 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020445f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446{
20447 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020448 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449}
20450
20451/*
20452 * "winnr()" function
20453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020455f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456{
20457 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020458
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020460 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020462 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463}
20464
20465/*
20466 * "winrestcmd()" function
20467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020469f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470{
20471#ifdef FEAT_WINDOWS
20472 win_T *wp;
20473 int winnr = 1;
20474 garray_T ga;
20475 char_u buf[50];
20476
20477 ga_init2(&ga, (int)sizeof(char), 70);
20478 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20479 {
20480 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20481 ga_concat(&ga, buf);
20482# ifdef FEAT_VERTSPLIT
20483 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20484 ga_concat(&ga, buf);
20485# endif
20486 ++winnr;
20487 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020488 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020490 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020492 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020494 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495}
20496
20497/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020498 * "winrestview()" function
20499 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020501f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020502{
20503 dict_T *dict;
20504
20505 if (argvars[0].v_type != VAR_DICT
20506 || (dict = argvars[0].vval.v_dict) == NULL)
20507 EMSG(_(e_invarg));
20508 else
20509 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020510 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20511 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20512 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20513 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020514#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020515 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20516 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020517#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020518 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20519 {
20520 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20521 curwin->w_set_curswant = FALSE;
20522 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020523
Bram Moolenaar82c25852014-05-28 16:47:16 +020020524 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20525 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020526#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020527 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20528 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020529#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020530 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20531 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20532 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20533 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020534
20535 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020536 win_new_height(curwin, curwin->w_height);
20537# ifdef FEAT_VERTSPLIT
20538 win_new_width(curwin, W_WIDTH(curwin));
20539# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020540 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020541
Bram Moolenaarb851a962014-10-31 15:45:52 +010020542 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020543 curwin->w_topline = 1;
20544 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20545 curwin->w_topline = curbuf->b_ml.ml_line_count;
20546#ifdef FEAT_DIFF
20547 check_topfill(curwin, TRUE);
20548#endif
20549 }
20550}
20551
20552/*
20553 * "winsaveview()" function
20554 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020556f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020557{
20558 dict_T *dict;
20559
Bram Moolenaara800b422010-06-27 01:15:55 +020020560 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020561 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020562 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020563
20564 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20565 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20566#ifdef FEAT_VIRTUALEDIT
20567 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20568#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020569 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020570 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20571
20572 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20573#ifdef FEAT_DIFF
20574 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20575#endif
20576 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20577 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20578}
20579
20580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581 * "winwidth(nr)" function
20582 */
20583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020584f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585{
20586 win_T *wp;
20587
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020588 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020590 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 else
20592#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020593 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020595 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596#endif
20597}
20598
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020600 * "wordcount()" function
20601 */
20602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020603f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020604{
20605 if (rettv_dict_alloc(rettv) == FAIL)
20606 return;
20607 cursor_pos_info(rettv->vval.v_dict);
20608}
20609
20610/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020611 * Write list of strings to file
20612 */
20613 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020614write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020615{
20616 listitem_T *li;
20617 int c;
20618 int ret = OK;
20619 char_u *s;
20620
20621 for (li = list->lv_first; li != NULL; li = li->li_next)
20622 {
20623 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20624 {
20625 if (*s == '\n')
20626 c = putc(NUL, fd);
20627 else
20628 c = putc(*s, fd);
20629 if (c == EOF)
20630 {
20631 ret = FAIL;
20632 break;
20633 }
20634 }
20635 if (!binary || li->li_next != NULL)
20636 if (putc('\n', fd) == EOF)
20637 {
20638 ret = FAIL;
20639 break;
20640 }
20641 if (ret == FAIL)
20642 {
20643 EMSG(_(e_write));
20644 break;
20645 }
20646 }
20647 return ret;
20648}
20649
20650/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020651 * "writefile()" function
20652 */
20653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020654f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020655{
20656 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020657 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020658 char_u *fname;
20659 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020660 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020661
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020662 if (check_restricted() || check_secure())
20663 return;
20664
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020665 if (argvars[0].v_type != VAR_LIST)
20666 {
20667 EMSG2(_(e_listarg), "writefile()");
20668 return;
20669 }
20670 if (argvars[0].vval.v_list == NULL)
20671 return;
20672
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020673 if (argvars[2].v_type != VAR_UNKNOWN)
20674 {
20675 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20676 binary = TRUE;
20677 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20678 append = TRUE;
20679 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020680
20681 /* Always open the file in binary mode, library functions have a mind of
20682 * their own about CR-LF conversion. */
20683 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020684 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20685 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020686 {
20687 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20688 ret = -1;
20689 }
20690 else
20691 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020692 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20693 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020694 fclose(fd);
20695 }
20696
20697 rettv->vval.v_number = ret;
20698}
20699
20700/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020701 * "xor(expr, expr)" function
20702 */
20703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020704f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020705{
20706 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20707 ^ get_tv_number_chk(&argvars[1], NULL);
20708}
20709
20710
20711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020713 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 */
20715 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020716var2fpos(
20717 typval_T *varp,
20718 int dollar_lnum, /* TRUE when $ is last line */
20719 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020721 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020722 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020723 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020724
Bram Moolenaara5525202006-03-02 22:52:09 +000020725 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020726 if (varp->v_type == VAR_LIST)
20727 {
20728 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020729 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020730 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020731 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020732
20733 l = varp->vval.v_list;
20734 if (l == NULL)
20735 return NULL;
20736
20737 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020738 pos.lnum = list_find_nr(l, 0L, &error);
20739 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020740 return NULL; /* invalid line number */
20741
20742 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020743 pos.col = list_find_nr(l, 1L, &error);
20744 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020745 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020746 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020747
20748 /* We accept "$" for the column number: last column. */
20749 li = list_find(l, 1L);
20750 if (li != NULL && li->li_tv.v_type == VAR_STRING
20751 && li->li_tv.vval.v_string != NULL
20752 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20753 pos.col = len + 1;
20754
Bram Moolenaara5525202006-03-02 22:52:09 +000020755 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020756 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020757 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020758 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020759
Bram Moolenaara5525202006-03-02 22:52:09 +000020760#ifdef FEAT_VIRTUALEDIT
20761 /* Get the virtual offset. Defaults to zero. */
20762 pos.coladd = list_find_nr(l, 2L, &error);
20763 if (error)
20764 pos.coladd = 0;
20765#endif
20766
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020767 return &pos;
20768 }
20769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020770 name = get_tv_string_chk(varp);
20771 if (name == NULL)
20772 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020773 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020775 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20776 {
20777 if (VIsual_active)
20778 return &VIsual;
20779 return &curwin->w_cursor;
20780 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020781 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020783 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20785 return NULL;
20786 return pp;
20787 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020788
20789#ifdef FEAT_VIRTUALEDIT
20790 pos.coladd = 0;
20791#endif
20792
Bram Moolenaar477933c2007-07-17 14:32:23 +000020793 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020794 {
20795 pos.col = 0;
20796 if (name[1] == '0') /* "w0": first visible line */
20797 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020798 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020799 pos.lnum = curwin->w_topline;
20800 return &pos;
20801 }
20802 else if (name[1] == '$') /* "w$": last visible line */
20803 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020804 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020805 pos.lnum = curwin->w_botline - 1;
20806 return &pos;
20807 }
20808 }
20809 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020811 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812 {
20813 pos.lnum = curbuf->b_ml.ml_line_count;
20814 pos.col = 0;
20815 }
20816 else
20817 {
20818 pos.lnum = curwin->w_cursor.lnum;
20819 pos.col = (colnr_T)STRLEN(ml_get_curline());
20820 }
20821 return &pos;
20822 }
20823 return NULL;
20824}
20825
20826/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020827 * Convert list in "arg" into a position and optional file number.
20828 * When "fnump" is NULL there is no file number, only 3 items.
20829 * Note that the column is passed on as-is, the caller may want to decrement
20830 * it to use 1 for the first column.
20831 * Return FAIL when conversion is not possible, doesn't check the position for
20832 * validity.
20833 */
20834 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020835list2fpos(
20836 typval_T *arg,
20837 pos_T *posp,
20838 int *fnump,
20839 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020840{
20841 list_T *l = arg->vval.v_list;
20842 long i = 0;
20843 long n;
20844
Bram Moolenaar493c1782014-05-28 14:34:46 +020020845 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20846 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020847 if (arg->v_type != VAR_LIST
20848 || l == NULL
20849 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020850 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020851 return FAIL;
20852
20853 if (fnump != NULL)
20854 {
20855 n = list_find_nr(l, i++, NULL); /* fnum */
20856 if (n < 0)
20857 return FAIL;
20858 if (n == 0)
20859 n = curbuf->b_fnum; /* current buffer */
20860 *fnump = n;
20861 }
20862
20863 n = list_find_nr(l, i++, NULL); /* lnum */
20864 if (n < 0)
20865 return FAIL;
20866 posp->lnum = n;
20867
20868 n = list_find_nr(l, i++, NULL); /* col */
20869 if (n < 0)
20870 return FAIL;
20871 posp->col = n;
20872
20873#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020874 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020875 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020876 posp->coladd = 0;
20877 else
20878 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020879#endif
20880
Bram Moolenaar493c1782014-05-28 14:34:46 +020020881 if (curswantp != NULL)
20882 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20883
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020884 return OK;
20885}
20886
20887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888 * Get the length of an environment variable name.
20889 * Advance "arg" to the first character after the name.
20890 * Return 0 for error.
20891 */
20892 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020893get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894{
20895 char_u *p;
20896 int len;
20897
20898 for (p = *arg; vim_isIDc(*p); ++p)
20899 ;
20900 if (p == *arg) /* no name found */
20901 return 0;
20902
20903 len = (int)(p - *arg);
20904 *arg = p;
20905 return len;
20906}
20907
20908/*
20909 * Get the length of the name of a function or internal variable.
20910 * "arg" is advanced to the first non-white character after the name.
20911 * Return 0 if something is wrong.
20912 */
20913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020914get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915{
20916 char_u *p;
20917 int len;
20918
20919 /* Find the end of the name. */
20920 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020921 {
20922 if (*p == ':')
20923 {
20924 /* "s:" is start of "s:var", but "n:" is not and can be used in
20925 * slice "[n:]". Also "xx:" is not a namespace. */
20926 len = (int)(p - *arg);
20927 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20928 || len > 1)
20929 break;
20930 }
20931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 if (p == *arg) /* no name found */
20933 return 0;
20934
20935 len = (int)(p - *arg);
20936 *arg = skipwhite(p);
20937
20938 return len;
20939}
20940
20941/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020942 * Get the length of the name of a variable or function.
20943 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020945 * Return -1 if curly braces expansion failed.
20946 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 * If the name contains 'magic' {}'s, expand them and return the
20948 * expanded name in an allocated string via 'alias' - caller must free.
20949 */
20950 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020951get_name_len(
20952 char_u **arg,
20953 char_u **alias,
20954 int evaluate,
20955 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956{
20957 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 char_u *p;
20959 char_u *expr_start;
20960 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961
20962 *alias = NULL; /* default to no alias */
20963
20964 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20965 && (*arg)[2] == (int)KE_SNR)
20966 {
20967 /* hard coded <SNR>, already translated */
20968 *arg += 3;
20969 return get_id_len(arg) + 3;
20970 }
20971 len = eval_fname_script(*arg);
20972 if (len > 0)
20973 {
20974 /* literal "<SID>", "s:" or "<SNR>" */
20975 *arg += len;
20976 }
20977
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020979 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020981 p = find_name_end(*arg, &expr_start, &expr_end,
20982 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 if (expr_start != NULL)
20984 {
20985 char_u *temp_string;
20986
20987 if (!evaluate)
20988 {
20989 len += (int)(p - *arg);
20990 *arg = skipwhite(p);
20991 return len;
20992 }
20993
20994 /*
20995 * Include any <SID> etc in the expanded string:
20996 * Thus the -len here.
20997 */
20998 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20999 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021000 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 *alias = temp_string;
21002 *arg = skipwhite(p);
21003 return (int)STRLEN(temp_string);
21004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005
21006 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021007 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 EMSG2(_(e_invexpr2), *arg);
21009
21010 return len;
21011}
21012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021013/*
21014 * Find the end of a variable or function name, taking care of magic braces.
21015 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21016 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021017 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021018 * Return a pointer to just after the name. Equal to "arg" if there is no
21019 * valid name.
21020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021022find_name_end(
21023 char_u *arg,
21024 char_u **expr_start,
21025 char_u **expr_end,
21026 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021028 int mb_nest = 0;
21029 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021031 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021033 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021035 *expr_start = NULL;
21036 *expr_end = NULL;
21037 }
21038
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021039 /* Quick check for valid starting character. */
21040 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21041 return arg;
21042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021043 for (p = arg; *p != NUL
21044 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021045 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021046 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021047 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021048 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021049 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021050 if (*p == '\'')
21051 {
21052 /* skip over 'string' to avoid counting [ and ] inside it. */
21053 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21054 ;
21055 if (*p == NUL)
21056 break;
21057 }
21058 else if (*p == '"')
21059 {
21060 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21061 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21062 if (*p == '\\' && p[1] != NUL)
21063 ++p;
21064 if (*p == NUL)
21065 break;
21066 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021067 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21068 {
21069 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021070 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021071 len = (int)(p - arg);
21072 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021073 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021074 break;
21075 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021077 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021079 if (*p == '[')
21080 ++br_nest;
21081 else if (*p == ']')
21082 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021085 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021087 if (*p == '{')
21088 {
21089 mb_nest++;
21090 if (expr_start != NULL && *expr_start == NULL)
21091 *expr_start = p;
21092 }
21093 else if (*p == '}')
21094 {
21095 mb_nest--;
21096 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21097 *expr_end = p;
21098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100 }
21101
21102 return p;
21103}
21104
21105/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021106 * Expands out the 'magic' {}'s in a variable/function name.
21107 * Note that this can call itself recursively, to deal with
21108 * constructs like foo{bar}{baz}{bam}
21109 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21110 * "in_start" ^
21111 * "expr_start" ^
21112 * "expr_end" ^
21113 * "in_end" ^
21114 *
21115 * Returns a new allocated string, which the caller must free.
21116 * Returns NULL for failure.
21117 */
21118 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021119make_expanded_name(
21120 char_u *in_start,
21121 char_u *expr_start,
21122 char_u *expr_end,
21123 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021124{
21125 char_u c1;
21126 char_u *retval = NULL;
21127 char_u *temp_result;
21128 char_u *nextcmd = NULL;
21129
21130 if (expr_end == NULL || in_end == NULL)
21131 return NULL;
21132 *expr_start = NUL;
21133 *expr_end = NUL;
21134 c1 = *in_end;
21135 *in_end = NUL;
21136
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021137 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021138 if (temp_result != NULL && nextcmd == NULL)
21139 {
21140 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21141 + (in_end - expr_end) + 1));
21142 if (retval != NULL)
21143 {
21144 STRCPY(retval, in_start);
21145 STRCAT(retval, temp_result);
21146 STRCAT(retval, expr_end + 1);
21147 }
21148 }
21149 vim_free(temp_result);
21150
21151 *in_end = c1; /* put char back for error messages */
21152 *expr_start = '{';
21153 *expr_end = '}';
21154
21155 if (retval != NULL)
21156 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021157 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021158 if (expr_start != NULL)
21159 {
21160 /* Further expansion! */
21161 temp_result = make_expanded_name(retval, expr_start,
21162 expr_end, temp_result);
21163 vim_free(retval);
21164 retval = temp_result;
21165 }
21166 }
21167
21168 return retval;
21169}
21170
21171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021173 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 */
21175 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021176eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021178 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21179}
21180
21181/*
21182 * Return TRUE if character "c" can be used as the first character in a
21183 * variable or function name (excluding '{' and '}').
21184 */
21185 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021186eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021187{
21188 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189}
21190
21191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 * Set number v: variable to "val".
21193 */
21194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021195set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021197 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198}
21199
21200/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021201 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202 */
21203 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021204get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021206 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207}
21208
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021209/*
21210 * Get string v: variable value. Uses a static buffer, can only be used once.
21211 */
21212 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021213get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021214{
21215 return get_tv_string(&vimvars[idx].vv_tv);
21216}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021217
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021219 * Get List v: variable value. Caller must take care of reference count when
21220 * needed.
21221 */
21222 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021223get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021224{
21225 return vimvars[idx].vv_list;
21226}
21227
21228/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021229 * Set v:char to character "c".
21230 */
21231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021232set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021233{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021234 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021235
21236#ifdef FEAT_MBYTE
21237 if (has_mbyte)
21238 buf[(*mb_char2bytes)(c, buf)] = NUL;
21239 else
21240#endif
21241 {
21242 buf[0] = c;
21243 buf[1] = NUL;
21244 }
21245 set_vim_var_string(VV_CHAR, buf, -1);
21246}
21247
21248/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021249 * Set v:count to "count" and v:count1 to "count1".
21250 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 */
21252 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021253set_vcount(
21254 long count,
21255 long count1,
21256 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021258 if (set_prevcount)
21259 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021260 vimvars[VV_COUNT].vv_nr = count;
21261 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262}
21263
21264/*
21265 * Set string v: variable to a copy of "val".
21266 */
21267 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021268set_vim_var_string(
21269 int idx,
21270 char_u *val,
21271 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272{
Bram Moolenaara542c682016-01-31 16:28:04 +010021273 clear_tv(&vimvars[idx].vv_di.di_tv);
21274 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021276 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021278 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021280 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281}
21282
21283/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021284 * Set List v: variable to "val".
21285 */
21286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021287set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021288{
Bram Moolenaara542c682016-01-31 16:28:04 +010021289 clear_tv(&vimvars[idx].vv_di.di_tv);
21290 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021291 vimvars[idx].vv_list = val;
21292 if (val != NULL)
21293 ++val->lv_refcount;
21294}
21295
21296/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021297 * Set Dictionary v: variable to "val".
21298 */
21299 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021300set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021301{
21302 int todo;
21303 hashitem_T *hi;
21304
Bram Moolenaara542c682016-01-31 16:28:04 +010021305 clear_tv(&vimvars[idx].vv_di.di_tv);
21306 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021307 vimvars[idx].vv_dict = val;
21308 if (val != NULL)
21309 {
21310 ++val->dv_refcount;
21311
21312 /* Set readonly */
21313 todo = (int)val->dv_hashtab.ht_used;
21314 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21315 {
21316 if (HASHITEM_EMPTY(hi))
21317 continue;
21318 --todo;
21319 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21320 }
21321 }
21322}
21323
21324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325 * Set v:register if needed.
21326 */
21327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021328set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329{
21330 char_u regname;
21331
21332 if (c == 0 || c == ' ')
21333 regname = '"';
21334 else
21335 regname = c;
21336 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021337 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 set_vim_var_string(VV_REG, &regname, 1);
21339}
21340
21341/*
21342 * Get or set v:exception. If "oldval" == NULL, return the current value.
21343 * Otherwise, restore the value to "oldval" and return NULL.
21344 * Must always be called in pairs to save and restore v:exception! Does not
21345 * take care of memory allocations.
21346 */
21347 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021348v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349{
21350 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021351 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352
Bram Moolenaare9a41262005-01-15 22:18:47 +000021353 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354 return NULL;
21355}
21356
21357/*
21358 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21359 * Otherwise, restore the value to "oldval" and return NULL.
21360 * Must always be called in pairs to save and restore v:throwpoint! Does not
21361 * take care of memory allocations.
21362 */
21363 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021364v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365{
21366 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021367 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368
Bram Moolenaare9a41262005-01-15 22:18:47 +000021369 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370 return NULL;
21371}
21372
21373#if defined(FEAT_AUTOCMD) || defined(PROTO)
21374/*
21375 * Set v:cmdarg.
21376 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21377 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21378 * Must always be called in pairs!
21379 */
21380 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021381set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382{
21383 char_u *oldval;
21384 char_u *newval;
21385 unsigned len;
21386
Bram Moolenaare9a41262005-01-15 22:18:47 +000021387 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021388 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021390 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021391 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021392 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 }
21394
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021395 if (eap->force_bin == FORCE_BIN)
21396 len = 6;
21397 else if (eap->force_bin == FORCE_NOBIN)
21398 len = 8;
21399 else
21400 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021401
21402 if (eap->read_edit)
21403 len += 7;
21404
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021405 if (eap->force_ff != 0)
21406 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21407# ifdef FEAT_MBYTE
21408 if (eap->force_enc != 0)
21409 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021410 if (eap->bad_char != 0)
21411 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021412# endif
21413
21414 newval = alloc(len + 1);
21415 if (newval == NULL)
21416 return NULL;
21417
21418 if (eap->force_bin == FORCE_BIN)
21419 sprintf((char *)newval, " ++bin");
21420 else if (eap->force_bin == FORCE_NOBIN)
21421 sprintf((char *)newval, " ++nobin");
21422 else
21423 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021424
21425 if (eap->read_edit)
21426 STRCAT(newval, " ++edit");
21427
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021428 if (eap->force_ff != 0)
21429 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21430 eap->cmd + eap->force_ff);
21431# ifdef FEAT_MBYTE
21432 if (eap->force_enc != 0)
21433 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21434 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021435 if (eap->bad_char == BAD_KEEP)
21436 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21437 else if (eap->bad_char == BAD_DROP)
21438 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21439 else if (eap->bad_char != 0)
21440 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021441# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021442 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021443 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444}
21445#endif
21446
21447/*
21448 * Get the value of internal variable "name".
21449 * Return OK or FAIL.
21450 */
21451 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021452get_var_tv(
21453 char_u *name,
21454 int len, /* length of "name" */
21455 typval_T *rettv, /* NULL when only checking existence */
21456 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21457 int verbose, /* may give error message */
21458 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459{
21460 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021461 typval_T *tv = NULL;
21462 typval_T atv;
21463 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465
21466 /* truncate the name, so that we can use strcmp() */
21467 cc = name[len];
21468 name[len] = NUL;
21469
21470 /*
21471 * Check for "b:changedtick".
21472 */
21473 if (STRCMP(name, "b:changedtick") == 0)
21474 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021475 atv.v_type = VAR_NUMBER;
21476 atv.vval.v_number = curbuf->b_changedtick;
21477 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 }
21479
21480 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 * Check for user-defined variables.
21482 */
21483 else
21484 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021485 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021487 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021488 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021489 if (dip != NULL)
21490 *dip = v;
21491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 }
21493
Bram Moolenaare9a41262005-01-15 22:18:47 +000021494 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021496 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021497 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 ret = FAIL;
21499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021500 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021501 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502
21503 name[len] = cc;
21504
21505 return ret;
21506}
21507
21508/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021509 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21510 * Also handle function call with Funcref variable: func(expr)
21511 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21512 */
21513 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021514handle_subscript(
21515 char_u **arg,
21516 typval_T *rettv,
21517 int evaluate, /* do more than finding the end */
21518 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021519{
21520 int ret = OK;
21521 dict_T *selfdict = NULL;
21522 char_u *s;
21523 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021524 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021525
21526 while (ret == OK
21527 && (**arg == '['
21528 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021529 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021530 && !vim_iswhite(*(*arg - 1)))
21531 {
21532 if (**arg == '(')
21533 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021534 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021535 if (evaluate)
21536 {
21537 functv = *rettv;
21538 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021539
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021540 /* Invoke the function. Recursive! */
21541 s = functv.vval.v_string;
21542 }
21543 else
21544 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021545 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021546 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21547 &len, evaluate, selfdict);
21548
21549 /* Clear the funcref afterwards, so that deleting it while
21550 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021551 if (evaluate)
21552 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021553
21554 /* Stop the expression evaluation when immediately aborting on
21555 * error, or when an interrupt occurred or an exception was thrown
21556 * but not caught. */
21557 if (aborting())
21558 {
21559 if (ret == OK)
21560 clear_tv(rettv);
21561 ret = FAIL;
21562 }
21563 dict_unref(selfdict);
21564 selfdict = NULL;
21565 }
21566 else /* **arg == '[' || **arg == '.' */
21567 {
21568 dict_unref(selfdict);
21569 if (rettv->v_type == VAR_DICT)
21570 {
21571 selfdict = rettv->vval.v_dict;
21572 if (selfdict != NULL)
21573 ++selfdict->dv_refcount;
21574 }
21575 else
21576 selfdict = NULL;
21577 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21578 {
21579 clear_tv(rettv);
21580 ret = FAIL;
21581 }
21582 }
21583 }
21584 dict_unref(selfdict);
21585 return ret;
21586}
21587
21588/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021589 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021590 * value).
21591 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021592 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021593alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021594{
Bram Moolenaar33570922005-01-25 22:26:29 +000021595 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021596}
21597
21598/*
21599 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600 * The string "s" must have been allocated, it is consumed.
21601 * Return NULL for out of memory, the variable otherwise.
21602 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021603 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021604alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605{
Bram Moolenaar33570922005-01-25 22:26:29 +000021606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021608 rettv = alloc_tv();
21609 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021611 rettv->v_type = VAR_STRING;
21612 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 }
21614 else
21615 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021616 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617}
21618
21619/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021620 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021622 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021623free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624{
21625 if (varp != NULL)
21626 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021627 switch (varp->v_type)
21628 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021629 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021630 func_unref(varp->vval.v_string);
21631 /*FALLTHROUGH*/
21632 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021633 vim_free(varp->vval.v_string);
21634 break;
21635 case VAR_LIST:
21636 list_unref(varp->vval.v_list);
21637 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021638 case VAR_DICT:
21639 dict_unref(varp->vval.v_dict);
21640 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021641 case VAR_JOB:
21642#ifdef FEAT_JOB
21643 job_unref(varp->vval.v_job);
21644 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021645#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021646 case VAR_CHANNEL:
21647#ifdef FEAT_CHANNEL
21648 channel_unref(varp->vval.v_channel);
21649 break;
21650#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021651 case VAR_NUMBER:
21652 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021653 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021654 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021655 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 vim_free(varp);
21658 }
21659}
21660
21661/*
21662 * Free the memory for a variable value and set the value to NULL or 0.
21663 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021664 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021665clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666{
21667 if (varp != NULL)
21668 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021669 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021671 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021672 func_unref(varp->vval.v_string);
21673 /*FALLTHROUGH*/
21674 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021675 vim_free(varp->vval.v_string);
21676 varp->vval.v_string = NULL;
21677 break;
21678 case VAR_LIST:
21679 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021680 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021681 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021682 case VAR_DICT:
21683 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021684 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021685 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021686 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021687 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021688 varp->vval.v_number = 0;
21689 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021690 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021691#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021692 varp->vval.v_float = 0.0;
21693 break;
21694#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021695 case VAR_JOB:
21696#ifdef FEAT_JOB
21697 job_unref(varp->vval.v_job);
21698 varp->vval.v_job = NULL;
21699#endif
21700 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021701 case VAR_CHANNEL:
21702#ifdef FEAT_CHANNEL
21703 channel_unref(varp->vval.v_channel);
21704 varp->vval.v_channel = NULL;
21705#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021706 case VAR_UNKNOWN:
21707 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021709 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 }
21711}
21712
21713/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021714 * Set the value of a variable to NULL without freeing items.
21715 */
21716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021717init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021718{
21719 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021720 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021721}
21722
21723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724 * Get the number value of a variable.
21725 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021726 * For incompatible types, return 0.
21727 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21728 * caller of incompatible types: it sets *denote to TRUE if "denote"
21729 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 */
21731 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021732get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021734 int error = FALSE;
21735
21736 return get_tv_number_chk(varp, &error); /* return 0L on error */
21737}
21738
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021739 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021740get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021741{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021742 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021744 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021746 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021747 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021748 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021749#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021750 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021751 break;
21752#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021753 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021754 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021755 break;
21756 case VAR_STRING:
21757 if (varp->vval.v_string != NULL)
21758 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021759 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021760 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021761 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021762 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021763 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021764 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021765 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021766 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021767 case VAR_SPECIAL:
21768 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21769 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021770 case VAR_JOB:
21771#ifdef FEAT_JOB
21772 EMSG(_("E910: Using a Job as a Number"));
21773 break;
21774#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021775 case VAR_CHANNEL:
21776#ifdef FEAT_CHANNEL
21777 EMSG(_("E913: Using a Channel as a Number"));
21778 break;
21779#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021780 case VAR_UNKNOWN:
21781 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021782 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021784 if (denote == NULL) /* useful for values that must be unsigned */
21785 n = -1;
21786 else
21787 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021788 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789}
21790
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021791#ifdef FEAT_FLOAT
21792 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021793get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021794{
21795 switch (varp->v_type)
21796 {
21797 case VAR_NUMBER:
21798 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021799 case VAR_FLOAT:
21800 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021801 case VAR_FUNC:
21802 EMSG(_("E891: Using a Funcref as a Float"));
21803 break;
21804 case VAR_STRING:
21805 EMSG(_("E892: Using a String as a Float"));
21806 break;
21807 case VAR_LIST:
21808 EMSG(_("E893: Using a List as a Float"));
21809 break;
21810 case VAR_DICT:
21811 EMSG(_("E894: Using a Dictionary as a Float"));
21812 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021813 case VAR_SPECIAL:
21814 EMSG(_("E907: Using a special value as a Float"));
21815 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021816 case VAR_JOB:
21817# ifdef FEAT_JOB
21818 EMSG(_("E911: Using a Job as a Float"));
21819 break;
21820# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021821 case VAR_CHANNEL:
21822# ifdef FEAT_CHANNEL
21823 EMSG(_("E914: Using a Channel as a Float"));
21824 break;
21825# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021826 case VAR_UNKNOWN:
21827 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021828 break;
21829 }
21830 return 0;
21831}
21832#endif
21833
Bram Moolenaar071d4272004-06-13 20:20:40 +000021834/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021835 * Get the lnum from the first argument.
21836 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021837 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 */
21839 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021840get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841{
Bram Moolenaar33570922005-01-25 22:26:29 +000021842 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843 linenr_T lnum;
21844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021845 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846 if (lnum == 0) /* no valid number, try using line() */
21847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021848 rettv.v_type = VAR_NUMBER;
21849 f_line(argvars, &rettv);
21850 lnum = rettv.vval.v_number;
21851 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852 }
21853 return lnum;
21854}
21855
21856/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021857 * Get the lnum from the first argument.
21858 * Also accepts "$", then "buf" is used.
21859 * Returns 0 on error.
21860 */
21861 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021862get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021863{
21864 if (argvars[0].v_type == VAR_STRING
21865 && argvars[0].vval.v_string != NULL
21866 && argvars[0].vval.v_string[0] == '$'
21867 && buf != NULL)
21868 return buf->b_ml.ml_line_count;
21869 return get_tv_number_chk(&argvars[0], NULL);
21870}
21871
21872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873 * Get the string value of a variable.
21874 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021875 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21876 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 * If the String variable has never been set, return an empty string.
21878 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021879 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21880 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881 */
21882 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021883get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884{
21885 static char_u mybuf[NUMBUFLEN];
21886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021887 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888}
21889
21890 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021891get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021893 char_u *res = get_tv_string_buf_chk(varp, buf);
21894
21895 return res != NULL ? res : (char_u *)"";
21896}
21897
Bram Moolenaar7d647822014-04-05 21:28:56 +020021898/*
21899 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21900 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021901 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021902get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021903{
21904 static char_u mybuf[NUMBUFLEN];
21905
21906 return get_tv_string_buf_chk(varp, mybuf);
21907}
21908
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021909 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021910get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021911{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021912 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021914 case VAR_NUMBER:
21915 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21916 return buf;
21917 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021918 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021919 break;
21920 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021921 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021922 break;
21923 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021924 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021925 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021926 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021927#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021928 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021929 break;
21930#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021931 case VAR_STRING:
21932 if (varp->vval.v_string != NULL)
21933 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021934 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021935 case VAR_SPECIAL:
21936 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21937 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021938 case VAR_JOB:
21939#ifdef FEAT_JOB
21940 {
21941 job_T *job = varp->vval.v_job;
21942 char *status = job->jv_status == JOB_FAILED ? "fail"
21943 : job->jv_status == JOB_ENDED ? "dead"
21944 : "run";
21945# ifdef UNIX
21946 vim_snprintf((char *)buf, NUMBUFLEN,
21947 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021948# elif defined(WIN32)
21949 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021950 "process %ld %s",
21951 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021952 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021953# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021954 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021955 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21956# endif
21957 return buf;
21958 }
21959#endif
21960 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021961 case VAR_CHANNEL:
21962#ifdef FEAT_CHANNEL
21963 {
21964 channel_T *channel = varp->vval.v_channel;
21965 char *status = channel_status(channel);
21966
Bram Moolenaar5cefd402016-02-16 12:44:26 +010021967 if (channel == NULL)
21968 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
21969 else
21970 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010021971 "channel %d %s", channel->ch_id, status);
21972 return buf;
21973 }
21974#endif
21975 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021976 case VAR_UNKNOWN:
21977 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021978 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021980 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981}
21982
21983/*
21984 * Find variable "name" in the list of variables.
21985 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021986 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021987 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021988 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021991find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021994 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995
Bram Moolenaara7043832005-01-21 11:56:39 +000021996 ht = find_var_ht(name, &varname);
21997 if (htp != NULL)
21998 *htp = ht;
21999 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022001 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002}
22003
22004/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022005 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022006 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022008 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022009find_var_in_ht(
22010 hashtab_T *ht,
22011 int htname,
22012 char_u *varname,
22013 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022014{
Bram Moolenaar33570922005-01-25 22:26:29 +000022015 hashitem_T *hi;
22016
22017 if (*varname == NUL)
22018 {
22019 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022020 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022021 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022022 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022023 case 'g': return &globvars_var;
22024 case 'v': return &vimvars_var;
22025 case 'b': return &curbuf->b_bufvar;
22026 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022027#ifdef FEAT_WINDOWS
22028 case 't': return &curtab->tp_winvar;
22029#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022030 case 'l': return current_funccal == NULL
22031 ? NULL : &current_funccal->l_vars_var;
22032 case 'a': return current_funccal == NULL
22033 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022034 }
22035 return NULL;
22036 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022037
22038 hi = hash_find(ht, varname);
22039 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022040 {
22041 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022042 * worked find the variable again. Don't auto-load a script if it was
22043 * loaded already, otherwise it would be loaded every time when
22044 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022045 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022046 {
22047 /* Note: script_autoload() may make "hi" invalid. It must either
22048 * be obtained again or not used. */
22049 if (!script_autoload(varname, FALSE) || aborting())
22050 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022051 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022052 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022053 if (HASHITEM_EMPTY(hi))
22054 return NULL;
22055 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022056 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022057}
22058
22059/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022060 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022061 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022062 * Set "varname" to the start of name without ':'.
22063 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022064 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022065find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022067 hashitem_T *hi;
22068
Bram Moolenaar73627d02015-08-11 15:46:09 +020022069 if (name[0] == NUL)
22070 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 if (name[1] != ':')
22072 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022073 /* The name must not start with a colon or #. */
22074 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 return NULL;
22076 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022077
22078 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022079 hi = hash_find(&compat_hashtab, name);
22080 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022081 return &compat_hashtab;
22082
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022084 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022085 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086 }
22087 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022088 if (*name == 'g') /* global variable */
22089 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022090 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22091 */
22092 if (vim_strchr(name + 2, ':') != NULL
22093 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022094 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022096 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022098 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022099#ifdef FEAT_WINDOWS
22100 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022101 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022102#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022103 if (*name == 'v') /* v: variable */
22104 return &vimvarht;
22105 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022106 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022107 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022108 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109 if (*name == 's' /* script variable */
22110 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22111 return &SCRIPT_VARS(current_SID);
22112 return NULL;
22113}
22114
22115/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022116 * Get function call environment based on bactrace debug level
22117 */
22118 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022119get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022120{
22121 int i;
22122 funccall_T *funccal;
22123 funccall_T *temp_funccal;
22124
22125 funccal = current_funccal;
22126 if (debug_backtrace_level > 0)
22127 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022128 for (i = 0; i < debug_backtrace_level; i++)
22129 {
22130 temp_funccal = funccal->caller;
22131 if (temp_funccal)
22132 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022133 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022134 /* backtrace level overflow. reset to max */
22135 debug_backtrace_level = i;
22136 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022137 }
22138 return funccal;
22139}
22140
22141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022143 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 * Returns NULL when it doesn't exist.
22145 */
22146 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022147get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148{
Bram Moolenaar33570922005-01-25 22:26:29 +000022149 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022151 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 if (v == NULL)
22153 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022154 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155}
22156
22157/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022158 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 * sourcing this script and when executing functions defined in the script.
22160 */
22161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022162new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163{
Bram Moolenaara7043832005-01-21 11:56:39 +000022164 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022165 hashtab_T *ht;
22166 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022167
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22169 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022170 /* Re-allocating ga_data means that an ht_array pointing to
22171 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022172 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022173 for (i = 1; i <= ga_scripts.ga_len; ++i)
22174 {
22175 ht = &SCRIPT_VARS(i);
22176 if (ht->ht_mask == HT_INIT_SIZE - 1)
22177 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022178 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022179 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022180 }
22181
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 while (ga_scripts.ga_len < id)
22183 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022184 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022185 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022186 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 }
22189 }
22190}
22191
22192/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022193 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22194 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195 */
22196 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022197init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198{
Bram Moolenaar33570922005-01-25 22:26:29 +000022199 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022200 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022201 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022202 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022203 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022204 dict_var->di_tv.vval.v_dict = dict;
22205 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022206 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022207 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22208 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209}
22210
22211/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022212 * Unreference a dictionary initialized by init_var_dict().
22213 */
22214 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022215unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022216{
22217 /* Now the dict needs to be freed if no one else is using it, go back to
22218 * normal reference counting. */
22219 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22220 dict_unref(dict);
22221}
22222
22223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022225 * Frees all allocated variables and the value they contain.
22226 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 */
22228 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022229vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022230{
22231 vars_clear_ext(ht, TRUE);
22232}
22233
22234/*
22235 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22236 */
22237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022238vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239{
Bram Moolenaara7043832005-01-21 11:56:39 +000022240 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022241 hashitem_T *hi;
22242 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243
Bram Moolenaar33570922005-01-25 22:26:29 +000022244 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022245 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022246 for (hi = ht->ht_array; todo > 0; ++hi)
22247 {
22248 if (!HASHITEM_EMPTY(hi))
22249 {
22250 --todo;
22251
Bram Moolenaar33570922005-01-25 22:26:29 +000022252 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022253 * ht_array might change then. hash_clear() takes care of it
22254 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022255 v = HI2DI(hi);
22256 if (free_val)
22257 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022258 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022259 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022260 }
22261 }
22262 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022263 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264}
22265
Bram Moolenaara7043832005-01-21 11:56:39 +000022266/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022267 * Delete a variable from hashtab "ht" at item "hi".
22268 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022271delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272{
Bram Moolenaar33570922005-01-25 22:26:29 +000022273 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022274
22275 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022276 clear_tv(&di->di_tv);
22277 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278}
22279
22280/*
22281 * List the value of one internal variable.
22282 */
22283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022284list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022286 char_u *tofree;
22287 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022288 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022289
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022290 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022291 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022292 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022293 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294}
22295
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022297list_one_var_a(
22298 char_u *prefix,
22299 char_u *name,
22300 int type,
22301 char_u *string,
22302 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303{
Bram Moolenaar31859182007-08-14 20:41:13 +000022304 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22305 msg_start();
22306 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 if (name != NULL) /* "a:" vars don't have a name stored */
22308 msg_puts(name);
22309 msg_putchar(' ');
22310 msg_advance(22);
22311 if (type == VAR_NUMBER)
22312 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022313 else if (type == VAR_FUNC)
22314 msg_putchar('*');
22315 else if (type == VAR_LIST)
22316 {
22317 msg_putchar('[');
22318 if (*string == '[')
22319 ++string;
22320 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022321 else if (type == VAR_DICT)
22322 {
22323 msg_putchar('{');
22324 if (*string == '{')
22325 ++string;
22326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 else
22328 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022329
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022331
22332 if (type == VAR_FUNC)
22333 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022334 if (*first)
22335 {
22336 msg_clr_eos();
22337 *first = FALSE;
22338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339}
22340
22341/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022342 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 * If the variable already exists, the value is updated.
22344 * Otherwise the variable is created.
22345 */
22346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022347set_var(
22348 char_u *name,
22349 typval_T *tv,
22350 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351{
Bram Moolenaar33570922005-01-25 22:26:29 +000022352 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022354 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022356 ht = find_var_ht(name, &varname);
22357 if (ht == NULL || *varname == NUL)
22358 {
22359 EMSG2(_(e_illvar), name);
22360 return;
22361 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022362 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022363
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022364 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22365 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022366
Bram Moolenaar33570922005-01-25 22:26:29 +000022367 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022369 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022370 if (var_check_ro(v->di_flags, name, FALSE)
22371 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022372 return;
22373 if (v->di_tv.v_type != tv->v_type
22374 && !((v->di_tv.v_type == VAR_STRING
22375 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022376 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022377 || tv->v_type == VAR_NUMBER))
22378#ifdef FEAT_FLOAT
22379 && !((v->di_tv.v_type == VAR_NUMBER
22380 || v->di_tv.v_type == VAR_FLOAT)
22381 && (tv->v_type == VAR_NUMBER
22382 || tv->v_type == VAR_FLOAT))
22383#endif
22384 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022385 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022386 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022387 return;
22388 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022389
22390 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022391 * Handle setting internal v: variables separately where needed to
22392 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022393 */
22394 if (ht == &vimvarht)
22395 {
22396 if (v->di_tv.v_type == VAR_STRING)
22397 {
22398 vim_free(v->di_tv.vval.v_string);
22399 if (copy || tv->v_type != VAR_STRING)
22400 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22401 else
22402 {
22403 /* Take over the string to avoid an extra alloc/free. */
22404 v->di_tv.vval.v_string = tv->vval.v_string;
22405 tv->vval.v_string = NULL;
22406 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022407 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022408 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022409 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022410 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022411 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022412 if (STRCMP(varname, "searchforward") == 0)
22413 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022414#ifdef FEAT_SEARCH_EXTRA
22415 else if (STRCMP(varname, "hlsearch") == 0)
22416 {
22417 no_hlsearch = !v->di_tv.vval.v_number;
22418 redraw_all_later(SOME_VALID);
22419 }
22420#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022421 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022422 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022423 else if (v->di_tv.v_type != tv->v_type)
22424 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022425 }
22426
22427 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 }
22429 else /* add a new variable */
22430 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022431 /* Can't add "v:" variable. */
22432 if (ht == &vimvarht)
22433 {
22434 EMSG2(_(e_illvar), name);
22435 return;
22436 }
22437
Bram Moolenaar92124a32005-06-17 22:03:40 +000022438 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022439 if (!valid_varname(varname))
22440 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022441
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022442 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22443 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022444 if (v == NULL)
22445 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022446 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022447 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022449 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 return;
22451 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022452 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022454
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022455 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022456 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022457 else
22458 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022459 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022460 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022461 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463}
22464
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022465/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022466 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022467 * Also give an error message.
22468 */
22469 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022470var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022471{
22472 if (flags & DI_FLAGS_RO)
22473 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022474 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022475 return TRUE;
22476 }
22477 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22478 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022479 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022480 return TRUE;
22481 }
22482 return FALSE;
22483}
22484
22485/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022486 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22487 * Also give an error message.
22488 */
22489 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022490var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022491{
22492 if (flags & DI_FLAGS_FIX)
22493 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022494 EMSG2(_("E795: Cannot delete variable %s"),
22495 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022496 return TRUE;
22497 }
22498 return FALSE;
22499}
22500
22501/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022502 * Check if a funcref is assigned to a valid variable name.
22503 * Return TRUE and give an error if not.
22504 */
22505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022506var_check_func_name(
22507 char_u *name, /* points to start of variable name */
22508 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022509{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022510 /* Allow for w: b: s: and t:. */
22511 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022512 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22513 ? name[2] : name[0]))
22514 {
22515 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22516 name);
22517 return TRUE;
22518 }
22519 /* Don't allow hiding a function. When "v" is not NULL we might be
22520 * assigning another function to the same var, the type is checked
22521 * below. */
22522 if (new_var && function_exists(name))
22523 {
22524 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22525 name);
22526 return TRUE;
22527 }
22528 return FALSE;
22529}
22530
22531/*
22532 * Check if a variable name is valid.
22533 * Return FALSE and give an error if not.
22534 */
22535 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022536valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022537{
22538 char_u *p;
22539
22540 for (p = varname; *p != NUL; ++p)
22541 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22542 && *p != AUTOLOAD_CHAR)
22543 {
22544 EMSG2(_(e_illvar), varname);
22545 return FALSE;
22546 }
22547 return TRUE;
22548}
22549
22550/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022551 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022552 * Also give an error message, using "name" or _("name") when use_gettext is
22553 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022554 */
22555 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022556tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022557{
22558 if (lock & VAR_LOCKED)
22559 {
22560 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022561 name == NULL ? (char_u *)_("Unknown")
22562 : use_gettext ? (char_u *)_(name)
22563 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022564 return TRUE;
22565 }
22566 if (lock & VAR_FIXED)
22567 {
22568 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022569 name == NULL ? (char_u *)_("Unknown")
22570 : use_gettext ? (char_u *)_(name)
22571 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022572 return TRUE;
22573 }
22574 return FALSE;
22575}
22576
22577/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022579 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022580 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022581 * It is OK for "from" and "to" to point to the same item. This is used to
22582 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022583 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022585copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022587 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022588 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022589 switch (from->v_type)
22590 {
22591 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022592 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022593 to->vval.v_number = from->vval.v_number;
22594 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022595 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022596#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022597 to->vval.v_float = from->vval.v_float;
22598 break;
22599#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022600 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022601#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022602 to->vval.v_job = from->vval.v_job;
22603 ++to->vval.v_job->jv_refcount;
22604 break;
22605#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022606 case VAR_CHANNEL:
22607#ifdef FEAT_CHANNEL
22608 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022609 if (to->vval.v_channel != NULL)
22610 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022611 break;
22612#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022613 case VAR_STRING:
22614 case VAR_FUNC:
22615 if (from->vval.v_string == NULL)
22616 to->vval.v_string = NULL;
22617 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022618 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022619 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022620 if (from->v_type == VAR_FUNC)
22621 func_ref(to->vval.v_string);
22622 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022623 break;
22624 case VAR_LIST:
22625 if (from->vval.v_list == NULL)
22626 to->vval.v_list = NULL;
22627 else
22628 {
22629 to->vval.v_list = from->vval.v_list;
22630 ++to->vval.v_list->lv_refcount;
22631 }
22632 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022633 case VAR_DICT:
22634 if (from->vval.v_dict == NULL)
22635 to->vval.v_dict = NULL;
22636 else
22637 {
22638 to->vval.v_dict = from->vval.v_dict;
22639 ++to->vval.v_dict->dv_refcount;
22640 }
22641 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022642 case VAR_UNKNOWN:
22643 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022644 break;
22645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646}
22647
22648/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022649 * Make a copy of an item.
22650 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022651 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22652 * reference to an already copied list/dict can be used.
22653 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022654 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022656item_copy(
22657 typval_T *from,
22658 typval_T *to,
22659 int deep,
22660 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022661{
22662 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022663 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022664
Bram Moolenaar33570922005-01-25 22:26:29 +000022665 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022666 {
22667 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022668 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022669 }
22670 ++recurse;
22671
22672 switch (from->v_type)
22673 {
22674 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022675 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022676 case VAR_STRING:
22677 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022678 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022679 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022680 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022681 copy_tv(from, to);
22682 break;
22683 case VAR_LIST:
22684 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022685 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022686 if (from->vval.v_list == NULL)
22687 to->vval.v_list = NULL;
22688 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22689 {
22690 /* use the copy made earlier */
22691 to->vval.v_list = from->vval.v_list->lv_copylist;
22692 ++to->vval.v_list->lv_refcount;
22693 }
22694 else
22695 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22696 if (to->vval.v_list == NULL)
22697 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022698 break;
22699 case VAR_DICT:
22700 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022701 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022702 if (from->vval.v_dict == NULL)
22703 to->vval.v_dict = NULL;
22704 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22705 {
22706 /* use the copy made earlier */
22707 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22708 ++to->vval.v_dict->dv_refcount;
22709 }
22710 else
22711 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22712 if (to->vval.v_dict == NULL)
22713 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022714 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022715 case VAR_UNKNOWN:
22716 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022717 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022718 }
22719 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022720 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022721}
22722
22723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 * ":echo expr1 ..." print each argument separated with a space, add a
22725 * newline at the end.
22726 * ":echon expr1 ..." print each argument plain.
22727 */
22728 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022729ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730{
22731 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022732 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022733 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 char_u *p;
22735 int needclr = TRUE;
22736 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022737 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738
22739 if (eap->skip)
22740 ++emsg_skip;
22741 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22742 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022743 /* If eval1() causes an error message the text from the command may
22744 * still need to be cleared. E.g., "echo 22,44". */
22745 need_clr_eos = needclr;
22746
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022748 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 {
22750 /*
22751 * Report the invalid expression unless the expression evaluation
22752 * has been cancelled due to an aborting error, an interrupt, or an
22753 * exception.
22754 */
22755 if (!aborting())
22756 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022757 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 break;
22759 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022760 need_clr_eos = FALSE;
22761
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 if (!eap->skip)
22763 {
22764 if (atstart)
22765 {
22766 atstart = FALSE;
22767 /* Call msg_start() after eval1(), evaluating the expression
22768 * may cause a message to appear. */
22769 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022770 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022771 /* Mark the saved text as finishing the line, so that what
22772 * follows is displayed on a new line when scrolling back
22773 * at the more prompt. */
22774 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777 }
22778 else if (eap->cmdidx == CMD_echo)
22779 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022780 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022781 if (p != NULL)
22782 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022784 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022785 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022786 if (*p != TAB && needclr)
22787 {
22788 /* remove any text still there from the command */
22789 msg_clr_eos();
22790 needclr = FALSE;
22791 }
22792 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 }
22794 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022795 {
22796#ifdef FEAT_MBYTE
22797 if (has_mbyte)
22798 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022799 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022800
22801 (void)msg_outtrans_len_attr(p, i, echo_attr);
22802 p += i - 1;
22803 }
22804 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022806 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022809 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022811 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 arg = skipwhite(arg);
22813 }
22814 eap->nextcmd = check_nextcmd(arg);
22815
22816 if (eap->skip)
22817 --emsg_skip;
22818 else
22819 {
22820 /* remove text that may still be there from the command */
22821 if (needclr)
22822 msg_clr_eos();
22823 if (eap->cmdidx == CMD_echo)
22824 msg_end();
22825 }
22826}
22827
22828/*
22829 * ":echohl {name}".
22830 */
22831 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022832ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833{
22834 int id;
22835
22836 id = syn_name2id(eap->arg);
22837 if (id == 0)
22838 echo_attr = 0;
22839 else
22840 echo_attr = syn_id2attr(id);
22841}
22842
22843/*
22844 * ":execute expr1 ..." execute the result of an expression.
22845 * ":echomsg expr1 ..." Print a message
22846 * ":echoerr expr1 ..." Print an error
22847 * Each gets spaces around each argument and a newline at the end for
22848 * echo commands
22849 */
22850 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022851ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852{
22853 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022854 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 int ret = OK;
22856 char_u *p;
22857 garray_T ga;
22858 int len;
22859 int save_did_emsg;
22860
22861 ga_init2(&ga, 1, 80);
22862
22863 if (eap->skip)
22864 ++emsg_skip;
22865 while (*arg != NUL && *arg != '|' && *arg != '\n')
22866 {
22867 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022868 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 {
22870 /*
22871 * Report the invalid expression unless the expression evaluation
22872 * has been cancelled due to an aborting error, an interrupt, or an
22873 * exception.
22874 */
22875 if (!aborting())
22876 EMSG2(_(e_invexpr2), p);
22877 ret = FAIL;
22878 break;
22879 }
22880
22881 if (!eap->skip)
22882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022883 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884 len = (int)STRLEN(p);
22885 if (ga_grow(&ga, len + 2) == FAIL)
22886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022887 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 ret = FAIL;
22889 break;
22890 }
22891 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894 ga.ga_len += len;
22895 }
22896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022897 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898 arg = skipwhite(arg);
22899 }
22900
22901 if (ret != FAIL && ga.ga_data != NULL)
22902 {
22903 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022906 out_flush();
22907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 else if (eap->cmdidx == CMD_echoerr)
22909 {
22910 /* We don't want to abort following commands, restore did_emsg. */
22911 save_did_emsg = did_emsg;
22912 EMSG((char_u *)ga.ga_data);
22913 if (!force_abort)
22914 did_emsg = save_did_emsg;
22915 }
22916 else if (eap->cmdidx == CMD_execute)
22917 do_cmdline((char_u *)ga.ga_data,
22918 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22919 }
22920
22921 ga_clear(&ga);
22922
22923 if (eap->skip)
22924 --emsg_skip;
22925
22926 eap->nextcmd = check_nextcmd(arg);
22927}
22928
22929/*
22930 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22931 * "arg" points to the "&" or '+' when called, to "option" when returning.
22932 * Returns NULL when no option name found. Otherwise pointer to the char
22933 * after the option name.
22934 */
22935 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022936find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937{
22938 char_u *p = *arg;
22939
22940 ++p;
22941 if (*p == 'g' && p[1] == ':')
22942 {
22943 *opt_flags = OPT_GLOBAL;
22944 p += 2;
22945 }
22946 else if (*p == 'l' && p[1] == ':')
22947 {
22948 *opt_flags = OPT_LOCAL;
22949 p += 2;
22950 }
22951 else
22952 *opt_flags = 0;
22953
22954 if (!ASCII_ISALPHA(*p))
22955 return NULL;
22956 *arg = p;
22957
22958 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22959 p += 4; /* termcap option */
22960 else
22961 while (ASCII_ISALPHA(*p))
22962 ++p;
22963 return p;
22964}
22965
22966/*
22967 * ":function"
22968 */
22969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022970ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971{
22972 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022973 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 int j;
22975 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022977 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 char_u *name = NULL;
22979 char_u *p;
22980 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022981 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 garray_T newargs;
22983 garray_T newlines;
22984 int varargs = FALSE;
22985 int mustend = FALSE;
22986 int flags = 0;
22987 ufunc_T *fp;
22988 int indent;
22989 int nesting;
22990 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022991 dictitem_T *v;
22992 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022993 static int func_nr = 0; /* number for nameless function */
22994 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022995 hashtab_T *ht;
22996 int todo;
22997 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022998 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999
23000 /*
23001 * ":function" without argument: list functions.
23002 */
23003 if (ends_excmd(*eap->arg))
23004 {
23005 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023006 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023007 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023008 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023009 {
23010 if (!HASHITEM_EMPTY(hi))
23011 {
23012 --todo;
23013 fp = HI2UF(hi);
23014 if (!isdigit(*fp->uf_name))
23015 list_func_head(fp, FALSE);
23016 }
23017 }
23018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 eap->nextcmd = check_nextcmd(eap->arg);
23020 return;
23021 }
23022
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023023 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023024 * ":function /pat": list functions matching pattern.
23025 */
23026 if (*eap->arg == '/')
23027 {
23028 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23029 if (!eap->skip)
23030 {
23031 regmatch_T regmatch;
23032
23033 c = *p;
23034 *p = NUL;
23035 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23036 *p = c;
23037 if (regmatch.regprog != NULL)
23038 {
23039 regmatch.rm_ic = p_ic;
23040
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023041 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023042 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23043 {
23044 if (!HASHITEM_EMPTY(hi))
23045 {
23046 --todo;
23047 fp = HI2UF(hi);
23048 if (!isdigit(*fp->uf_name)
23049 && vim_regexec(&regmatch, fp->uf_name, 0))
23050 list_func_head(fp, FALSE);
23051 }
23052 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023053 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023054 }
23055 }
23056 if (*p == '/')
23057 ++p;
23058 eap->nextcmd = check_nextcmd(p);
23059 return;
23060 }
23061
23062 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023063 * Get the function name. There are these situations:
23064 * func normal function name
23065 * "name" == func, "fudi.fd_dict" == NULL
23066 * dict.func new dictionary entry
23067 * "name" == NULL, "fudi.fd_dict" set,
23068 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23069 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023070 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023071 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23072 * dict.func existing dict entry that's not a Funcref
23073 * "name" == NULL, "fudi.fd_dict" set,
23074 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023075 * s:func script-local function name
23076 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023079 name = trans_function_name(&p, eap->skip, 0, &fudi);
23080 paren = (vim_strchr(p, '(') != NULL);
23081 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 {
23083 /*
23084 * Return on an invalid expression in braces, unless the expression
23085 * evaluation has been cancelled due to an aborting error, an
23086 * interrupt, or an exception.
23087 */
23088 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023089 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023090 if (!eap->skip && fudi.fd_newkey != NULL)
23091 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023092 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023095 else
23096 eap->skip = TRUE;
23097 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023098
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 /* An error in a function call during evaluation of an expression in magic
23100 * braces should not cause the function not to be defined. */
23101 saved_did_emsg = did_emsg;
23102 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103
23104 /*
23105 * ":function func" with only function name: list function.
23106 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023107 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 {
23109 if (!ends_excmd(*skipwhite(p)))
23110 {
23111 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023112 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113 }
23114 eap->nextcmd = check_nextcmd(p);
23115 if (eap->nextcmd != NULL)
23116 *p = NUL;
23117 if (!eap->skip && !got_int)
23118 {
23119 fp = find_func(name);
23120 if (fp != NULL)
23121 {
23122 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023123 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023125 if (FUNCLINE(fp, j) == NULL)
23126 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023127 msg_putchar('\n');
23128 msg_outnum((long)(j + 1));
23129 if (j < 9)
23130 msg_putchar(' ');
23131 if (j < 99)
23132 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023133 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 out_flush(); /* show a line at a time */
23135 ui_breakcheck();
23136 }
23137 if (!got_int)
23138 {
23139 msg_putchar('\n');
23140 msg_puts((char_u *)" endfunction");
23141 }
23142 }
23143 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023144 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023146 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 }
23148
23149 /*
23150 * ":function name(arg1, arg2)" Define function.
23151 */
23152 p = skipwhite(p);
23153 if (*p != '(')
23154 {
23155 if (!eap->skip)
23156 {
23157 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023158 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159 }
23160 /* attempt to continue by skipping some text */
23161 if (vim_strchr(p, '(') != NULL)
23162 p = vim_strchr(p, '(');
23163 }
23164 p = skipwhite(p + 1);
23165
23166 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23167 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23168
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023169 if (!eap->skip)
23170 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023171 /* Check the name of the function. Unless it's a dictionary function
23172 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023173 if (name != NULL)
23174 arg = name;
23175 else
23176 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023177 if (arg != NULL && (fudi.fd_di == NULL
23178 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023179 {
23180 if (*arg == K_SPECIAL)
23181 j = 3;
23182 else
23183 j = 0;
23184 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23185 : eval_isnamec(arg[j])))
23186 ++j;
23187 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023188 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023189 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023190 /* Disallow using the g: dict. */
23191 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23192 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023193 }
23194
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 /*
23196 * Isolate the arguments: "arg1, arg2, ...)"
23197 */
23198 while (*p != ')')
23199 {
23200 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23201 {
23202 varargs = TRUE;
23203 p += 3;
23204 mustend = TRUE;
23205 }
23206 else
23207 {
23208 arg = p;
23209 while (ASCII_ISALNUM(*p) || *p == '_')
23210 ++p;
23211 if (arg == p || isdigit(*arg)
23212 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23213 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23214 {
23215 if (!eap->skip)
23216 EMSG2(_("E125: Illegal argument: %s"), arg);
23217 break;
23218 }
23219 if (ga_grow(&newargs, 1) == FAIL)
23220 goto erret;
23221 c = *p;
23222 *p = NUL;
23223 arg = vim_strsave(arg);
23224 if (arg == NULL)
23225 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023226
23227 /* Check for duplicate argument name. */
23228 for (i = 0; i < newargs.ga_len; ++i)
23229 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23230 {
23231 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023232 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023233 goto erret;
23234 }
23235
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23237 *p = c;
23238 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023239 if (*p == ',')
23240 ++p;
23241 else
23242 mustend = TRUE;
23243 }
23244 p = skipwhite(p);
23245 if (mustend && *p != ')')
23246 {
23247 if (!eap->skip)
23248 EMSG2(_(e_invarg2), eap->arg);
23249 break;
23250 }
23251 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023252 if (*p != ')')
23253 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023254 ++p; /* skip the ')' */
23255
Bram Moolenaare9a41262005-01-15 22:18:47 +000023256 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 for (;;)
23258 {
23259 p = skipwhite(p);
23260 if (STRNCMP(p, "range", 5) == 0)
23261 {
23262 flags |= FC_RANGE;
23263 p += 5;
23264 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023265 else if (STRNCMP(p, "dict", 4) == 0)
23266 {
23267 flags |= FC_DICT;
23268 p += 4;
23269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270 else if (STRNCMP(p, "abort", 5) == 0)
23271 {
23272 flags |= FC_ABORT;
23273 p += 5;
23274 }
23275 else
23276 break;
23277 }
23278
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023279 /* When there is a line break use what follows for the function body.
23280 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23281 if (*p == '\n')
23282 line_arg = p + 1;
23283 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284 EMSG(_(e_trailing));
23285
23286 /*
23287 * Read the body of the function, until ":endfunction" is found.
23288 */
23289 if (KeyTyped)
23290 {
23291 /* Check if the function already exists, don't let the user type the
23292 * whole function before telling him it doesn't work! For a script we
23293 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023294 if (!eap->skip && !eap->forceit)
23295 {
23296 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23297 EMSG(_(e_funcdict));
23298 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023299 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023302 if (!eap->skip && did_emsg)
23303 goto erret;
23304
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 msg_putchar('\n'); /* don't overwrite the function name */
23306 cmdline_row = msg_row;
23307 }
23308
23309 indent = 2;
23310 nesting = 0;
23311 for (;;)
23312 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023313 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023314 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023315 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023316 saved_wait_return = FALSE;
23317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023318 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023319 sourcing_lnum_off = sourcing_lnum;
23320
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023321 if (line_arg != NULL)
23322 {
23323 /* Use eap->arg, split up in parts by line breaks. */
23324 theline = line_arg;
23325 p = vim_strchr(theline, '\n');
23326 if (p == NULL)
23327 line_arg += STRLEN(line_arg);
23328 else
23329 {
23330 *p = NUL;
23331 line_arg = p + 1;
23332 }
23333 }
23334 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023335 theline = getcmdline(':', 0L, indent);
23336 else
23337 theline = eap->getline(':', eap->cookie, indent);
23338 if (KeyTyped)
23339 lines_left = Rows - 1;
23340 if (theline == NULL)
23341 {
23342 EMSG(_("E126: Missing :endfunction"));
23343 goto erret;
23344 }
23345
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023346 /* Detect line continuation: sourcing_lnum increased more than one. */
23347 if (sourcing_lnum > sourcing_lnum_off + 1)
23348 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23349 else
23350 sourcing_lnum_off = 0;
23351
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 if (skip_until != NULL)
23353 {
23354 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23355 * don't check for ":endfunc". */
23356 if (STRCMP(theline, skip_until) == 0)
23357 {
23358 vim_free(skip_until);
23359 skip_until = NULL;
23360 }
23361 }
23362 else
23363 {
23364 /* skip ':' and blanks*/
23365 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23366 ;
23367
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023368 /* Check for "endfunction". */
23369 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023371 if (line_arg == NULL)
23372 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023373 break;
23374 }
23375
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023376 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023377 * at "end". */
23378 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23379 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023380 else if (STRNCMP(p, "if", 2) == 0
23381 || STRNCMP(p, "wh", 2) == 0
23382 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383 || STRNCMP(p, "try", 3) == 0)
23384 indent += 2;
23385
23386 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023387 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023389 if (*p == '!')
23390 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023392 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23393 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023395 ++nesting;
23396 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023397 }
23398 }
23399
23400 /* Check for ":append" or ":insert". */
23401 p = skip_range(p, NULL);
23402 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23403 || (p[0] == 'i'
23404 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23405 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23406 skip_until = vim_strsave((char_u *)".");
23407
23408 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23409 arg = skipwhite(skiptowhite(p));
23410 if (arg[0] == '<' && arg[1] =='<'
23411 && ((p[0] == 'p' && p[1] == 'y'
23412 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23413 || (p[0] == 'p' && p[1] == 'e'
23414 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23415 || (p[0] == 't' && p[1] == 'c'
23416 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023417 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23418 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23420 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023421 || (p[0] == 'm' && p[1] == 'z'
23422 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423 ))
23424 {
23425 /* ":python <<" continues until a dot, like ":append" */
23426 p = skipwhite(arg + 2);
23427 if (*p == NUL)
23428 skip_until = vim_strsave((char_u *)".");
23429 else
23430 skip_until = vim_strsave(p);
23431 }
23432 }
23433
23434 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023435 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023436 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023437 if (line_arg == NULL)
23438 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023440 }
23441
23442 /* Copy the line to newly allocated memory. get_one_sourceline()
23443 * allocates 250 bytes per line, this saves 80% on average. The cost
23444 * is an extra alloc/free. */
23445 p = vim_strsave(theline);
23446 if (p != NULL)
23447 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023448 if (line_arg == NULL)
23449 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023450 theline = p;
23451 }
23452
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023453 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23454
23455 /* Add NULL lines for continuation lines, so that the line count is
23456 * equal to the index in the growarray. */
23457 while (sourcing_lnum_off-- > 0)
23458 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023459
23460 /* Check for end of eap->arg. */
23461 if (line_arg != NULL && *line_arg == NUL)
23462 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023463 }
23464
23465 /* Don't define the function when skipping commands or when an error was
23466 * detected. */
23467 if (eap->skip || did_emsg)
23468 goto erret;
23469
23470 /*
23471 * If there are no errors, add the function
23472 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023473 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023474 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023475 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023476 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023477 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023478 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023479 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023480 goto erret;
23481 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023482
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023483 fp = find_func(name);
23484 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023486 if (!eap->forceit)
23487 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023488 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023489 goto erret;
23490 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023491 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023492 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023493 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023494 name);
23495 goto erret;
23496 }
23497 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023498 ga_clear_strings(&(fp->uf_args));
23499 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023500 vim_free(name);
23501 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503 }
23504 else
23505 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023506 char numbuf[20];
23507
23508 fp = NULL;
23509 if (fudi.fd_newkey == NULL && !eap->forceit)
23510 {
23511 EMSG(_(e_funcdict));
23512 goto erret;
23513 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023514 if (fudi.fd_di == NULL)
23515 {
23516 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023517 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023518 goto erret;
23519 }
23520 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023521 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023522 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023523
23524 /* Give the function a sequential number. Can only be used with a
23525 * Funcref! */
23526 vim_free(name);
23527 sprintf(numbuf, "%d", ++func_nr);
23528 name = vim_strsave((char_u *)numbuf);
23529 if (name == NULL)
23530 goto erret;
23531 }
23532
23533 if (fp == NULL)
23534 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023535 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023536 {
23537 int slen, plen;
23538 char_u *scriptname;
23539
23540 /* Check that the autoload name matches the script name. */
23541 j = FAIL;
23542 if (sourcing_name != NULL)
23543 {
23544 scriptname = autoload_name(name);
23545 if (scriptname != NULL)
23546 {
23547 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023548 plen = (int)STRLEN(p);
23549 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023550 if (slen > plen && fnamecmp(p,
23551 sourcing_name + slen - plen) == 0)
23552 j = OK;
23553 vim_free(scriptname);
23554 }
23555 }
23556 if (j == FAIL)
23557 {
23558 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23559 goto erret;
23560 }
23561 }
23562
23563 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 if (fp == NULL)
23565 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023566
23567 if (fudi.fd_dict != NULL)
23568 {
23569 if (fudi.fd_di == NULL)
23570 {
23571 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023572 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023573 if (fudi.fd_di == NULL)
23574 {
23575 vim_free(fp);
23576 goto erret;
23577 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023578 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23579 {
23580 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023581 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023582 goto erret;
23583 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023584 }
23585 else
23586 /* overwrite existing dict entry */
23587 clear_tv(&fudi.fd_di->di_tv);
23588 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023589 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023590 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023591 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023592
23593 /* behave like "dict" was used */
23594 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023595 }
23596
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023598 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023599 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23600 {
23601 vim_free(fp);
23602 goto erret;
23603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023605 fp->uf_args = newargs;
23606 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023607#ifdef FEAT_PROFILE
23608 fp->uf_tml_count = NULL;
23609 fp->uf_tml_total = NULL;
23610 fp->uf_tml_self = NULL;
23611 fp->uf_profiling = FALSE;
23612 if (prof_def_func())
23613 func_do_profile(fp);
23614#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023615 fp->uf_varargs = varargs;
23616 fp->uf_flags = flags;
23617 fp->uf_calls = 0;
23618 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023619 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620
23621erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622 ga_clear_strings(&newargs);
23623 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023624ret_free:
23625 vim_free(skip_until);
23626 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023629 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630}
23631
23632/*
23633 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023634 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023636 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023637 * TFN_INT: internal function name OK
23638 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023639 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 * Advances "pp" to just after the function name (if no error).
23641 */
23642 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023643trans_function_name(
23644 char_u **pp,
23645 int skip, /* only find the end, don't evaluate */
23646 int flags,
23647 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023649 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 char_u *start;
23651 char_u *end;
23652 int lead;
23653 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023655 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023656
23657 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023658 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023660
23661 /* Check for hard coded <SNR>: already translated function ID (from a user
23662 * command). */
23663 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23664 && (*pp)[2] == (int)KE_SNR)
23665 {
23666 *pp += 3;
23667 len = get_id_len(pp) + 3;
23668 return vim_strnsave(start, len);
23669 }
23670
23671 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23672 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023674 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023676
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023677 /* Note that TFN_ flags use the same values as GLV_ flags. */
23678 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023679 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023680 if (end == start)
23681 {
23682 if (!skip)
23683 EMSG(_("E129: Function name required"));
23684 goto theend;
23685 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023686 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023687 {
23688 /*
23689 * Report an invalid expression in braces, unless the expression
23690 * evaluation has been cancelled due to an aborting error, an
23691 * interrupt, or an exception.
23692 */
23693 if (!aborting())
23694 {
23695 if (end != NULL)
23696 EMSG2(_(e_invarg2), start);
23697 }
23698 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023699 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023700 goto theend;
23701 }
23702
23703 if (lv.ll_tv != NULL)
23704 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023705 if (fdp != NULL)
23706 {
23707 fdp->fd_dict = lv.ll_dict;
23708 fdp->fd_newkey = lv.ll_newkey;
23709 lv.ll_newkey = NULL;
23710 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023712 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23713 {
23714 name = vim_strsave(lv.ll_tv->vval.v_string);
23715 *pp = end;
23716 }
23717 else
23718 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023719 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23720 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023721 EMSG(_(e_funcref));
23722 else
23723 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023724 name = NULL;
23725 }
23726 goto theend;
23727 }
23728
23729 if (lv.ll_name == NULL)
23730 {
23731 /* Error found, but continue after the function name. */
23732 *pp = end;
23733 goto theend;
23734 }
23735
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023736 /* Check if the name is a Funcref. If so, use the value. */
23737 if (lv.ll_exp_name != NULL)
23738 {
23739 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023740 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023741 if (name == lv.ll_exp_name)
23742 name = NULL;
23743 }
23744 else
23745 {
23746 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023747 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023748 if (name == *pp)
23749 name = NULL;
23750 }
23751 if (name != NULL)
23752 {
23753 name = vim_strsave(name);
23754 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023755 if (STRNCMP(name, "<SNR>", 5) == 0)
23756 {
23757 /* Change "<SNR>" to the byte sequence. */
23758 name[0] = K_SPECIAL;
23759 name[1] = KS_EXTRA;
23760 name[2] = (int)KE_SNR;
23761 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23762 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023763 goto theend;
23764 }
23765
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023766 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023767 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023768 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023769 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23770 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23771 {
23772 /* When there was "s:" already or the name expanded to get a
23773 * leading "s:" then remove it. */
23774 lv.ll_name += 2;
23775 len -= 2;
23776 lead = 2;
23777 }
23778 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023779 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023780 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023781 /* skip over "s:" and "g:" */
23782 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023783 lv.ll_name += 2;
23784 len = (int)(end - lv.ll_name);
23785 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023786
23787 /*
23788 * Copy the function name to allocated memory.
23789 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23790 * Accept <SNR>123_name() outside a script.
23791 */
23792 if (skip)
23793 lead = 0; /* do nothing */
23794 else if (lead > 0)
23795 {
23796 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023797 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23798 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023799 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023800 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023801 if (current_SID <= 0)
23802 {
23803 EMSG(_(e_usingsid));
23804 goto theend;
23805 }
23806 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23807 lead += (int)STRLEN(sid_buf);
23808 }
23809 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023810 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023811 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023812 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023813 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023814 goto theend;
23815 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023816 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023817 {
23818 char_u *cp = vim_strchr(lv.ll_name, ':');
23819
23820 if (cp != NULL && cp < end)
23821 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023822 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023823 goto theend;
23824 }
23825 }
23826
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023827 name = alloc((unsigned)(len + lead + 1));
23828 if (name != NULL)
23829 {
23830 if (lead > 0)
23831 {
23832 name[0] = K_SPECIAL;
23833 name[1] = KS_EXTRA;
23834 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023835 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023836 STRCPY(name + 3, sid_buf);
23837 }
23838 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023839 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023840 }
23841 *pp = end;
23842
23843theend:
23844 clear_lval(&lv);
23845 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846}
23847
23848/*
23849 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23850 * Return 2 if "p" starts with "s:".
23851 * Return 0 otherwise.
23852 */
23853 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023854eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023856 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23857 * the standard library function. */
23858 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23859 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860 return 5;
23861 if (p[0] == 's' && p[1] == ':')
23862 return 2;
23863 return 0;
23864}
23865
23866/*
23867 * Return TRUE if "p" starts with "<SID>" or "s:".
23868 * Only works if eval_fname_script() returned non-zero for "p"!
23869 */
23870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023871eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872{
23873 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23874}
23875
23876/*
23877 * List the head of the function: "name(arg1, arg2)".
23878 */
23879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023880list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881{
23882 int j;
23883
23884 msg_start();
23885 if (indent)
23886 MSG_PUTS(" ");
23887 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023888 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 {
23890 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023891 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 }
23893 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023894 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023895 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023896 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897 {
23898 if (j)
23899 MSG_PUTS(", ");
23900 msg_puts(FUNCARG(fp, j));
23901 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023902 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 {
23904 if (j)
23905 MSG_PUTS(", ");
23906 MSG_PUTS("...");
23907 }
23908 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023909 if (fp->uf_flags & FC_ABORT)
23910 MSG_PUTS(" abort");
23911 if (fp->uf_flags & FC_RANGE)
23912 MSG_PUTS(" range");
23913 if (fp->uf_flags & FC_DICT)
23914 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023915 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023916 if (p_verbose > 0)
23917 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918}
23919
23920/*
23921 * Find a function by name, return pointer to it in ufuncs.
23922 * Return NULL for unknown function.
23923 */
23924 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023925find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023927 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023928
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023929 hi = hash_find(&func_hashtab, name);
23930 if (!HASHITEM_EMPTY(hi))
23931 return HI2UF(hi);
23932 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023933}
23934
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023935#if defined(EXITFREE) || defined(PROTO)
23936 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023937free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023938{
23939 hashitem_T *hi;
23940
23941 /* Need to start all over every time, because func_free() may change the
23942 * hash table. */
23943 while (func_hashtab.ht_used > 0)
23944 for (hi = func_hashtab.ht_array; ; ++hi)
23945 if (!HASHITEM_EMPTY(hi))
23946 {
23947 func_free(HI2UF(hi));
23948 break;
23949 }
23950}
23951#endif
23952
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023954translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023955{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023956 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023957 return find_internal_func(name) >= 0;
23958 return find_func(name) != NULL;
23959}
23960
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023961/*
23962 * Return TRUE if a function "name" exists.
23963 */
23964 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023965function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023966{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023967 char_u *nm = name;
23968 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023969 int n = FALSE;
23970
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023971 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23972 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023973 nm = skipwhite(nm);
23974
23975 /* Only accept "funcname", "funcname ", "funcname (..." and
23976 * "funcname(...", not "funcname!...". */
23977 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023978 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023979 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023980 return n;
23981}
23982
Bram Moolenaara1544c02013-05-30 12:35:52 +020023983 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023984get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023985{
23986 char_u *nm = name;
23987 char_u *p;
23988
23989 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23990
23991 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023992 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023993 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023994
Bram Moolenaara1544c02013-05-30 12:35:52 +020023995 vim_free(p);
23996 return NULL;
23997}
23998
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023999/*
24000 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024001 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24002 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024003 */
24004 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024005builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024006{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024007 char_u *p;
24008
24009 if (!ASCII_ISLOWER(name[0]))
24010 return FALSE;
24011 p = vim_strchr(name, AUTOLOAD_CHAR);
24012 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024013}
24014
Bram Moolenaar05159a02005-02-26 23:04:13 +000024015#if defined(FEAT_PROFILE) || defined(PROTO)
24016/*
24017 * Start profiling function "fp".
24018 */
24019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024020func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024021{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024022 int len = fp->uf_lines.ga_len;
24023
24024 if (len == 0)
24025 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024026 fp->uf_tm_count = 0;
24027 profile_zero(&fp->uf_tm_self);
24028 profile_zero(&fp->uf_tm_total);
24029 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024030 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024031 if (fp->uf_tml_total == NULL)
24032 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024033 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024034 if (fp->uf_tml_self == NULL)
24035 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024036 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024037 fp->uf_tml_idx = -1;
24038 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24039 || fp->uf_tml_self == NULL)
24040 return; /* out of memory */
24041
24042 fp->uf_profiling = TRUE;
24043}
24044
24045/*
24046 * Dump the profiling results for all functions in file "fd".
24047 */
24048 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024049func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024050{
24051 hashitem_T *hi;
24052 int todo;
24053 ufunc_T *fp;
24054 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024055 ufunc_T **sorttab;
24056 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024057
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024058 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024059 if (todo == 0)
24060 return; /* nothing to dump */
24061
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024062 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024063
Bram Moolenaar05159a02005-02-26 23:04:13 +000024064 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24065 {
24066 if (!HASHITEM_EMPTY(hi))
24067 {
24068 --todo;
24069 fp = HI2UF(hi);
24070 if (fp->uf_profiling)
24071 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024072 if (sorttab != NULL)
24073 sorttab[st_len++] = fp;
24074
Bram Moolenaar05159a02005-02-26 23:04:13 +000024075 if (fp->uf_name[0] == K_SPECIAL)
24076 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24077 else
24078 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24079 if (fp->uf_tm_count == 1)
24080 fprintf(fd, "Called 1 time\n");
24081 else
24082 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24083 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24084 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24085 fprintf(fd, "\n");
24086 fprintf(fd, "count total (s) self (s)\n");
24087
24088 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24089 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024090 if (FUNCLINE(fp, i) == NULL)
24091 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024092 prof_func_line(fd, fp->uf_tml_count[i],
24093 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024094 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24095 }
24096 fprintf(fd, "\n");
24097 }
24098 }
24099 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024100
24101 if (sorttab != NULL && st_len > 0)
24102 {
24103 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24104 prof_total_cmp);
24105 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24106 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24107 prof_self_cmp);
24108 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24109 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024110
24111 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024112}
Bram Moolenaar73830342005-02-28 22:48:19 +000024113
24114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024115prof_sort_list(
24116 FILE *fd,
24117 ufunc_T **sorttab,
24118 int st_len,
24119 char *title,
24120 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024121{
24122 int i;
24123 ufunc_T *fp;
24124
24125 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24126 fprintf(fd, "count total (s) self (s) function\n");
24127 for (i = 0; i < 20 && i < st_len; ++i)
24128 {
24129 fp = sorttab[i];
24130 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24131 prefer_self);
24132 if (fp->uf_name[0] == K_SPECIAL)
24133 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24134 else
24135 fprintf(fd, " %s()\n", fp->uf_name);
24136 }
24137 fprintf(fd, "\n");
24138}
24139
24140/*
24141 * Print the count and times for one function or function line.
24142 */
24143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024144prof_func_line(
24145 FILE *fd,
24146 int count,
24147 proftime_T *total,
24148 proftime_T *self,
24149 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024150{
24151 if (count > 0)
24152 {
24153 fprintf(fd, "%5d ", count);
24154 if (prefer_self && profile_equal(total, self))
24155 fprintf(fd, " ");
24156 else
24157 fprintf(fd, "%s ", profile_msg(total));
24158 if (!prefer_self && profile_equal(total, self))
24159 fprintf(fd, " ");
24160 else
24161 fprintf(fd, "%s ", profile_msg(self));
24162 }
24163 else
24164 fprintf(fd, " ");
24165}
24166
24167/*
24168 * Compare function for total time sorting.
24169 */
24170 static int
24171#ifdef __BORLANDC__
24172_RTLENTRYF
24173#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024174prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024175{
24176 ufunc_T *p1, *p2;
24177
24178 p1 = *(ufunc_T **)s1;
24179 p2 = *(ufunc_T **)s2;
24180 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24181}
24182
24183/*
24184 * Compare function for self time sorting.
24185 */
24186 static int
24187#ifdef __BORLANDC__
24188_RTLENTRYF
24189#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024190prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024191{
24192 ufunc_T *p1, *p2;
24193
24194 p1 = *(ufunc_T **)s1;
24195 p2 = *(ufunc_T **)s2;
24196 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24197}
24198
Bram Moolenaar05159a02005-02-26 23:04:13 +000024199#endif
24200
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024201/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024202 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024203 * Return TRUE if a package was loaded.
24204 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024205 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024206script_autoload(
24207 char_u *name,
24208 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024209{
24210 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024211 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024212 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024213 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024214
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024215 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024216 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024217 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024218 return FALSE;
24219
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024220 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024221
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024222 /* Find the name in the list of previously loaded package names. Skip
24223 * "autoload/", it's always the same. */
24224 for (i = 0; i < ga_loaded.ga_len; ++i)
24225 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24226 break;
24227 if (!reload && i < ga_loaded.ga_len)
24228 ret = FALSE; /* was loaded already */
24229 else
24230 {
24231 /* Remember the name if it wasn't loaded already. */
24232 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24233 {
24234 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24235 tofree = NULL;
24236 }
24237
24238 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024239 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024240 ret = TRUE;
24241 }
24242
24243 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024244 return ret;
24245}
24246
24247/*
24248 * Return the autoload script name for a function or variable name.
24249 * Returns NULL when out of memory.
24250 */
24251 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024252autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024253{
24254 char_u *p;
24255 char_u *scriptname;
24256
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024257 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024258 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24259 if (scriptname == NULL)
24260 return FALSE;
24261 STRCPY(scriptname, "autoload/");
24262 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024263 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024264 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024265 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024266 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024267 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024268}
24269
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24271
24272/*
24273 * Function given to ExpandGeneric() to obtain the list of user defined
24274 * function names.
24275 */
24276 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024277get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024279 static long_u done;
24280 static hashitem_T *hi;
24281 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282
24283 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024285 done = 0;
24286 hi = func_hashtab.ht_array;
24287 }
24288 if (done < func_hashtab.ht_used)
24289 {
24290 if (done++ > 0)
24291 ++hi;
24292 while (HASHITEM_EMPTY(hi))
24293 ++hi;
24294 fp = HI2UF(hi);
24295
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024296 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024297 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024298
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024299 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24300 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024301
24302 cat_func_name(IObuff, fp);
24303 if (xp->xp_context != EXPAND_USER_FUNC)
24304 {
24305 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024306 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307 STRCAT(IObuff, ")");
24308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309 return IObuff;
24310 }
24311 return NULL;
24312}
24313
24314#endif /* FEAT_CMDL_COMPL */
24315
24316/*
24317 * Copy the function name of "fp" to buffer "buf".
24318 * "buf" must be able to hold the function name plus three bytes.
24319 * Takes care of script-local function names.
24320 */
24321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024322cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024323{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024324 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 {
24326 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024327 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328 }
24329 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024330 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331}
24332
24333/*
24334 * ":delfunction {name}"
24335 */
24336 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024337ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024338{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024339 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340 char_u *p;
24341 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024342 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024343
24344 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024345 name = trans_function_name(&p, eap->skip, 0, &fudi);
24346 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024347 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024348 {
24349 if (fudi.fd_dict != NULL && !eap->skip)
24350 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024351 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024353 if (!ends_excmd(*skipwhite(p)))
24354 {
24355 vim_free(name);
24356 EMSG(_(e_trailing));
24357 return;
24358 }
24359 eap->nextcmd = check_nextcmd(p);
24360 if (eap->nextcmd != NULL)
24361 *p = NUL;
24362
24363 if (!eap->skip)
24364 fp = find_func(name);
24365 vim_free(name);
24366
24367 if (!eap->skip)
24368 {
24369 if (fp == NULL)
24370 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024371 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372 return;
24373 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024374 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375 {
24376 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24377 return;
24378 }
24379
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024380 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024381 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024382 /* Delete the dict item that refers to the function, it will
24383 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024384 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024385 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024386 else
24387 func_free(fp);
24388 }
24389}
24390
24391/*
24392 * Free a function and remove it from the list of functions.
24393 */
24394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024395func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024396{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024397 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024398
24399 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024400 ga_clear_strings(&(fp->uf_args));
24401 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024402#ifdef FEAT_PROFILE
24403 vim_free(fp->uf_tml_count);
24404 vim_free(fp->uf_tml_total);
24405 vim_free(fp->uf_tml_self);
24406#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024407
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024408 /* remove the function from the function hashtable */
24409 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24410 if (HASHITEM_EMPTY(hi))
24411 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024412 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024413 hash_remove(&func_hashtab, hi);
24414
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024415 vim_free(fp);
24416}
24417
24418/*
24419 * Unreference a Function: decrement the reference count and free it when it
24420 * becomes zero. Only for numbered functions.
24421 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024422 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024423func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024424{
24425 ufunc_T *fp;
24426
24427 if (name != NULL && isdigit(*name))
24428 {
24429 fp = find_func(name);
24430 if (fp == NULL)
24431 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024432 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024433 {
24434 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024435 * when "uf_calls" becomes zero. */
24436 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024437 func_free(fp);
24438 }
24439 }
24440}
24441
24442/*
24443 * Count a reference to a Function.
24444 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024445 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024446func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024447{
24448 ufunc_T *fp;
24449
24450 if (name != NULL && isdigit(*name))
24451 {
24452 fp = find_func(name);
24453 if (fp == NULL)
24454 EMSG2(_(e_intern2), "func_ref()");
24455 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024456 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024457 }
24458}
24459
24460/*
24461 * Call a user function.
24462 */
24463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024464call_user_func(
24465 ufunc_T *fp, /* pointer to function */
24466 int argcount, /* nr of args */
24467 typval_T *argvars, /* arguments */
24468 typval_T *rettv, /* return value */
24469 linenr_T firstline, /* first line of range */
24470 linenr_T lastline, /* last line of range */
24471 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024472{
Bram Moolenaar33570922005-01-25 22:26:29 +000024473 char_u *save_sourcing_name;
24474 linenr_T save_sourcing_lnum;
24475 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024476 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024477 int save_did_emsg;
24478 static int depth = 0;
24479 dictitem_T *v;
24480 int fixvar_idx = 0; /* index in fixvar[] */
24481 int i;
24482 int ai;
24483 char_u numbuf[NUMBUFLEN];
24484 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024485 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024486#ifdef FEAT_PROFILE
24487 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024488 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024490
24491 /* If depth of calling is getting too high, don't execute the function */
24492 if (depth >= p_mfd)
24493 {
24494 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024495 rettv->v_type = VAR_NUMBER;
24496 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024497 return;
24498 }
24499 ++depth;
24500
24501 line_breakcheck(); /* check for CTRL-C hit */
24502
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024503 fc = (funccall_T *)alloc(sizeof(funccall_T));
24504 fc->caller = current_funccal;
24505 current_funccal = fc;
24506 fc->func = fp;
24507 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024508 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024509 fc->linenr = 0;
24510 fc->returned = FALSE;
24511 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024513 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24514 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515
Bram Moolenaar33570922005-01-25 22:26:29 +000024516 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024517 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024518 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24519 * each argument variable and saves a lot of time.
24520 */
24521 /*
24522 * Init l: variables.
24523 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024524 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024525 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024526 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024527 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24528 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024529 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024530 name = v->di_key;
24531 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024532 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024533 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024534 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024535 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024536 v->di_tv.vval.v_dict = selfdict;
24537 ++selfdict->dv_refcount;
24538 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024539
Bram Moolenaar33570922005-01-25 22:26:29 +000024540 /*
24541 * Init a: variables.
24542 * Set a:0 to "argcount".
24543 * Set a:000 to a list with room for the "..." arguments.
24544 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024545 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024546 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024547 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024548 /* Use "name" to avoid a warning from some compiler that checks the
24549 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024550 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024551 name = v->di_key;
24552 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024553 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024554 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024555 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024556 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024557 v->di_tv.vval.v_list = &fc->l_varlist;
24558 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24559 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24560 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024561
24562 /*
24563 * Set a:firstline to "firstline" and a:lastline to "lastline".
24564 * Set a:name to named arguments.
24565 * Set a:N to the "..." arguments.
24566 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024567 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024568 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024569 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024570 (varnumber_T)lastline);
24571 for (i = 0; i < argcount; ++i)
24572 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024573 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024574 if (ai < 0)
24575 /* named argument a:name */
24576 name = FUNCARG(fp, i);
24577 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024578 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024579 /* "..." argument a:1, a:2, etc. */
24580 sprintf((char *)numbuf, "%d", ai + 1);
24581 name = numbuf;
24582 }
24583 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24584 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024585 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024586 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24587 }
24588 else
24589 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024590 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24591 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024592 if (v == NULL)
24593 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024594 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024595 }
24596 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024597 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024598
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024599 /* Note: the values are copied directly to avoid alloc/free.
24600 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024601 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024602 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024603
24604 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24605 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024606 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24607 fc->l_listitems[ai].li_tv = argvars[i];
24608 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024609 }
24610 }
24611
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612 /* Don't redraw while executing the function. */
24613 ++RedrawingDisabled;
24614 save_sourcing_name = sourcing_name;
24615 save_sourcing_lnum = sourcing_lnum;
24616 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024617 /* need space for function name + ("function " + 3) or "[number]" */
24618 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24619 + STRLEN(fp->uf_name) + 20;
24620 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621 if (sourcing_name != NULL)
24622 {
24623 if (save_sourcing_name != NULL
24624 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024625 sprintf((char *)sourcing_name, "%s[%d]..",
24626 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024627 else
24628 STRCPY(sourcing_name, "function ");
24629 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24630
24631 if (p_verbose >= 12)
24632 {
24633 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024634 verbose_enter_scroll();
24635
Bram Moolenaar555b2802005-05-19 21:08:39 +000024636 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 if (p_verbose >= 14)
24638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024640 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024641 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024642 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643
24644 msg_puts((char_u *)"(");
24645 for (i = 0; i < argcount; ++i)
24646 {
24647 if (i > 0)
24648 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024649 if (argvars[i].v_type == VAR_NUMBER)
24650 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024651 else
24652 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024653 /* Do not want errors such as E724 here. */
24654 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024655 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024656 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024657 if (s != NULL)
24658 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024659 if (vim_strsize(s) > MSG_BUF_CLEN)
24660 {
24661 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24662 s = buf;
24663 }
24664 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024665 vim_free(tofree);
24666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 }
24668 }
24669 msg_puts((char_u *)")");
24670 }
24671 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024672
24673 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024674 --no_wait_return;
24675 }
24676 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024677#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024678 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024679 {
24680 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24681 func_do_profile(fp);
24682 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024683 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024684 {
24685 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024686 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024687 profile_zero(&fp->uf_tm_children);
24688 }
24689 script_prof_save(&wait_start);
24690 }
24691#endif
24692
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024694 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024695 save_did_emsg = did_emsg;
24696 did_emsg = FALSE;
24697
24698 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024699 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024700 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24701
24702 --RedrawingDisabled;
24703
24704 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024705 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024707 clear_tv(rettv);
24708 rettv->v_type = VAR_NUMBER;
24709 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024710 }
24711
Bram Moolenaar05159a02005-02-26 23:04:13 +000024712#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024713 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024714 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024715 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024716 profile_end(&call_start);
24717 profile_sub_wait(&wait_start, &call_start);
24718 profile_add(&fp->uf_tm_total, &call_start);
24719 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024720 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024721 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024722 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24723 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024724 }
24725 }
24726#endif
24727
Bram Moolenaar071d4272004-06-13 20:20:40 +000024728 /* when being verbose, mention the return value */
24729 if (p_verbose >= 12)
24730 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024731 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024732 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024733
Bram Moolenaar071d4272004-06-13 20:20:40 +000024734 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024735 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024736 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024737 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024738 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024739 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024740 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024741 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024742 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024743 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024744 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024745
Bram Moolenaar555b2802005-05-19 21:08:39 +000024746 /* The value may be very long. Skip the middle part, so that we
24747 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024748 * truncate it at the end. Don't want errors such as E724 here. */
24749 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024750 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024751 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024752 if (s != NULL)
24753 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024754 if (vim_strsize(s) > MSG_BUF_CLEN)
24755 {
24756 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24757 s = buf;
24758 }
24759 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024760 vim_free(tofree);
24761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762 }
24763 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024764
24765 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766 --no_wait_return;
24767 }
24768
24769 vim_free(sourcing_name);
24770 sourcing_name = save_sourcing_name;
24771 sourcing_lnum = save_sourcing_lnum;
24772 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024773#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024774 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024775 script_prof_restore(&wait_start);
24776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777
24778 if (p_verbose >= 12 && sourcing_name != NULL)
24779 {
24780 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024781 verbose_enter_scroll();
24782
Bram Moolenaar555b2802005-05-19 21:08:39 +000024783 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024784 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024785
24786 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024787 --no_wait_return;
24788 }
24789
24790 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024791 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024792 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024793
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024794 /* If the a:000 list and the l: and a: dicts are not referenced we can
24795 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024796 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24797 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24798 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24799 {
24800 free_funccal(fc, FALSE);
24801 }
24802 else
24803 {
24804 hashitem_T *hi;
24805 listitem_T *li;
24806 int todo;
24807
24808 /* "fc" is still in use. This can happen when returning "a:000" or
24809 * assigning "l:" to a global variable.
24810 * Link "fc" in the list for garbage collection later. */
24811 fc->caller = previous_funccal;
24812 previous_funccal = fc;
24813
24814 /* Make a copy of the a: variables, since we didn't do that above. */
24815 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24816 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24817 {
24818 if (!HASHITEM_EMPTY(hi))
24819 {
24820 --todo;
24821 v = HI2DI(hi);
24822 copy_tv(&v->di_tv, &v->di_tv);
24823 }
24824 }
24825
24826 /* Make a copy of the a:000 items, since we didn't do that above. */
24827 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24828 copy_tv(&li->li_tv, &li->li_tv);
24829 }
24830}
24831
24832/*
24833 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024834 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024835 */
24836 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024837can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024838{
24839 return (fc->l_varlist.lv_copyID != copyID
24840 && fc->l_vars.dv_copyID != copyID
24841 && fc->l_avars.dv_copyID != copyID);
24842}
24843
24844/*
24845 * Free "fc" and what it contains.
24846 */
24847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024848free_funccal(
24849 funccall_T *fc,
24850 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024851{
24852 listitem_T *li;
24853
24854 /* The a: variables typevals may not have been allocated, only free the
24855 * allocated variables. */
24856 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24857
24858 /* free all l: variables */
24859 vars_clear(&fc->l_vars.dv_hashtab);
24860
24861 /* Free the a:000 variables if they were allocated. */
24862 if (free_val)
24863 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24864 clear_tv(&li->li_tv);
24865
24866 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024867}
24868
24869/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024870 * Add a number variable "name" to dict "dp" with value "nr".
24871 */
24872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024873add_nr_var(
24874 dict_T *dp,
24875 dictitem_T *v,
24876 char *name,
24877 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024878{
24879 STRCPY(v->di_key, name);
24880 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24881 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24882 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024883 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024884 v->di_tv.vval.v_number = nr;
24885}
24886
24887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024888 * ":return [expr]"
24889 */
24890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024891ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024892{
24893 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024894 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895 int returning = FALSE;
24896
24897 if (current_funccal == NULL)
24898 {
24899 EMSG(_("E133: :return not inside a function"));
24900 return;
24901 }
24902
24903 if (eap->skip)
24904 ++emsg_skip;
24905
24906 eap->nextcmd = NULL;
24907 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024908 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909 {
24910 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024911 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024913 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024914 }
24915 /* It's safer to return also on error. */
24916 else if (!eap->skip)
24917 {
24918 /*
24919 * Return unless the expression evaluation has been cancelled due to an
24920 * aborting error, an interrupt, or an exception.
24921 */
24922 if (!aborting())
24923 returning = do_return(eap, FALSE, TRUE, NULL);
24924 }
24925
24926 /* When skipping or the return gets pending, advance to the next command
24927 * in this line (!returning). Otherwise, ignore the rest of the line.
24928 * Following lines will be ignored by get_func_line(). */
24929 if (returning)
24930 eap->nextcmd = NULL;
24931 else if (eap->nextcmd == NULL) /* no argument */
24932 eap->nextcmd = check_nextcmd(arg);
24933
24934 if (eap->skip)
24935 --emsg_skip;
24936}
24937
24938/*
24939 * Return from a function. Possibly makes the return pending. Also called
24940 * for a pending return at the ":endtry" or after returning from an extra
24941 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024942 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024943 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944 * FALSE when the return gets pending.
24945 */
24946 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024947do_return(
24948 exarg_T *eap,
24949 int reanimate,
24950 int is_cmd,
24951 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024952{
24953 int idx;
24954 struct condstack *cstack = eap->cstack;
24955
24956 if (reanimate)
24957 /* Undo the return. */
24958 current_funccal->returned = FALSE;
24959
24960 /*
24961 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24962 * not in its finally clause (which then is to be executed next) is found.
24963 * In this case, make the ":return" pending for execution at the ":endtry".
24964 * Otherwise, return normally.
24965 */
24966 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24967 if (idx >= 0)
24968 {
24969 cstack->cs_pending[idx] = CSTP_RETURN;
24970
24971 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024972 /* A pending return again gets pending. "rettv" points to an
24973 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024974 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024975 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024976 else
24977 {
24978 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024979 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024980 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024981 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024983 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024984 {
24985 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024986 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024987 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024988 else
24989 EMSG(_(e_outofmem));
24990 }
24991 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024992 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024993
24994 if (reanimate)
24995 {
24996 /* The pending return value could be overwritten by a ":return"
24997 * without argument in a finally clause; reset the default
24998 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024999 current_funccal->rettv->v_type = VAR_NUMBER;
25000 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025001 }
25002 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025003 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004 }
25005 else
25006 {
25007 current_funccal->returned = TRUE;
25008
25009 /* If the return is carried out now, store the return value. For
25010 * a return immediately after reanimation, the value is already
25011 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025012 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025014 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025015 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025016 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025017 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025018 }
25019 }
25020
25021 return idx < 0;
25022}
25023
25024/*
25025 * Free the variable with a pending return value.
25026 */
25027 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025028discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025029{
Bram Moolenaar33570922005-01-25 22:26:29 +000025030 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031}
25032
25033/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025034 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025035 * is an allocated string. Used by report_pending() for verbose messages.
25036 */
25037 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025038get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025040 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025041 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025042 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025044 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025045 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025046 if (s == NULL)
25047 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025048
25049 STRCPY(IObuff, ":return ");
25050 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25051 if (STRLEN(s) + 8 >= IOSIZE)
25052 STRCPY(IObuff + IOSIZE - 4, "...");
25053 vim_free(tofree);
25054 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025055}
25056
25057/*
25058 * Get next function line.
25059 * Called by do_cmdline() to get the next line.
25060 * Returns allocated string, or NULL for end of function.
25061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025062 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025063get_func_line(
25064 int c UNUSED,
25065 void *cookie,
25066 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025067{
Bram Moolenaar33570922005-01-25 22:26:29 +000025068 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025069 ufunc_T *fp = fcp->func;
25070 char_u *retval;
25071 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025072
25073 /* If breakpoints have been added/deleted need to check for it. */
25074 if (fcp->dbg_tick != debug_tick)
25075 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025076 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025077 sourcing_lnum);
25078 fcp->dbg_tick = debug_tick;
25079 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025080#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025081 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025082 func_line_end(cookie);
25083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084
Bram Moolenaar05159a02005-02-26 23:04:13 +000025085 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025086 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25087 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025088 retval = NULL;
25089 else
25090 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025091 /* Skip NULL lines (continuation lines). */
25092 while (fcp->linenr < gap->ga_len
25093 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25094 ++fcp->linenr;
25095 if (fcp->linenr >= gap->ga_len)
25096 retval = NULL;
25097 else
25098 {
25099 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25100 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025101#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025102 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025103 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025104#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025106 }
25107
25108 /* Did we encounter a breakpoint? */
25109 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25110 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025111 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025113 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025114 sourcing_lnum);
25115 fcp->dbg_tick = debug_tick;
25116 }
25117
25118 return retval;
25119}
25120
Bram Moolenaar05159a02005-02-26 23:04:13 +000025121#if defined(FEAT_PROFILE) || defined(PROTO)
25122/*
25123 * Called when starting to read a function line.
25124 * "sourcing_lnum" must be correct!
25125 * When skipping lines it may not actually be executed, but we won't find out
25126 * until later and we need to store the time now.
25127 */
25128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025129func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025130{
25131 funccall_T *fcp = (funccall_T *)cookie;
25132 ufunc_T *fp = fcp->func;
25133
25134 if (fp->uf_profiling && sourcing_lnum >= 1
25135 && sourcing_lnum <= fp->uf_lines.ga_len)
25136 {
25137 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025138 /* Skip continuation lines. */
25139 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25140 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025141 fp->uf_tml_execed = FALSE;
25142 profile_start(&fp->uf_tml_start);
25143 profile_zero(&fp->uf_tml_children);
25144 profile_get_wait(&fp->uf_tml_wait);
25145 }
25146}
25147
25148/*
25149 * Called when actually executing a function line.
25150 */
25151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025152func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025153{
25154 funccall_T *fcp = (funccall_T *)cookie;
25155 ufunc_T *fp = fcp->func;
25156
25157 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25158 fp->uf_tml_execed = TRUE;
25159}
25160
25161/*
25162 * Called when done with a function line.
25163 */
25164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025165func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025166{
25167 funccall_T *fcp = (funccall_T *)cookie;
25168 ufunc_T *fp = fcp->func;
25169
25170 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25171 {
25172 if (fp->uf_tml_execed)
25173 {
25174 ++fp->uf_tml_count[fp->uf_tml_idx];
25175 profile_end(&fp->uf_tml_start);
25176 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025177 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025178 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25179 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025180 }
25181 fp->uf_tml_idx = -1;
25182 }
25183}
25184#endif
25185
Bram Moolenaar071d4272004-06-13 20:20:40 +000025186/*
25187 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025188 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025189 */
25190 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025191func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025192{
Bram Moolenaar33570922005-01-25 22:26:29 +000025193 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025194
25195 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25196 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025197 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025198 || fcp->returned);
25199}
25200
25201/*
25202 * return TRUE if cookie indicates a function which "abort"s on errors.
25203 */
25204 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025205func_has_abort(
25206 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025208 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209}
25210
25211#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25212typedef enum
25213{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025214 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25215 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25216 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217} var_flavour_T;
25218
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025219static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220
25221 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025222var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025223{
25224 char_u *p = varname;
25225
25226 if (ASCII_ISUPPER(*p))
25227 {
25228 while (*(++p))
25229 if (ASCII_ISLOWER(*p))
25230 return VAR_FLAVOUR_SESSION;
25231 return VAR_FLAVOUR_VIMINFO;
25232 }
25233 else
25234 return VAR_FLAVOUR_DEFAULT;
25235}
25236#endif
25237
25238#if defined(FEAT_VIMINFO) || defined(PROTO)
25239/*
25240 * Restore global vars that start with a capital from the viminfo file
25241 */
25242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025243read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244{
25245 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025246 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025247 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025248 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249
25250 if (!writing && (find_viminfo_parameter('!') != NULL))
25251 {
25252 tab = vim_strchr(virp->vir_line + 1, '\t');
25253 if (tab != NULL)
25254 {
25255 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025256 switch (*tab)
25257 {
25258 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025259#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025260 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025261#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025262 case 'D': type = VAR_DICT; break;
25263 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025264 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025266
25267 tab = vim_strchr(tab, '\t');
25268 if (tab != NULL)
25269 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025270 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025271 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025272 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025274#ifdef FEAT_FLOAT
25275 else if (type == VAR_FLOAT)
25276 (void)string2float(tab + 1, &tv.vval.v_float);
25277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025278 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025279 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025280 if (type == VAR_DICT || type == VAR_LIST)
25281 {
25282 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25283
25284 if (etv == NULL)
25285 /* Failed to parse back the dict or list, use it as a
25286 * string. */
25287 tv.v_type = VAR_STRING;
25288 else
25289 {
25290 vim_free(tv.vval.v_string);
25291 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025292 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025293 }
25294 }
25295
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025296 /* when in a function use global variables */
25297 save_funccal = current_funccal;
25298 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025299 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025300 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025301
25302 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025303 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025304 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25305 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025306 }
25307 }
25308 }
25309
25310 return viminfo_readline(virp);
25311}
25312
25313/*
25314 * Write global vars that start with a capital to the viminfo file
25315 */
25316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025317write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025318{
Bram Moolenaar33570922005-01-25 22:26:29 +000025319 hashitem_T *hi;
25320 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025321 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025322 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025323 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025324 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025325 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025326
25327 if (find_viminfo_parameter('!') == NULL)
25328 return;
25329
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025330 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025331
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025332 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025333 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025334 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025335 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025337 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025338 this_var = HI2DI(hi);
25339 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025340 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025341 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025342 {
25343 case VAR_STRING: s = "STR"; break;
25344 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025345 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025346 case VAR_DICT: s = "DIC"; break;
25347 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025348 case VAR_SPECIAL: s = "XPL"; break;
25349
25350 case VAR_UNKNOWN:
25351 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025352 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025353 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025354 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025355 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025356 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025357 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025358 if (p != NULL)
25359 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025360 vim_free(tofree);
25361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025362 }
25363 }
25364}
25365#endif
25366
25367#if defined(FEAT_SESSION) || defined(PROTO)
25368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025369store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025370{
Bram Moolenaar33570922005-01-25 22:26:29 +000025371 hashitem_T *hi;
25372 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025373 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025374 char_u *p, *t;
25375
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025376 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025377 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025378 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025379 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025381 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025382 this_var = HI2DI(hi);
25383 if ((this_var->di_tv.v_type == VAR_NUMBER
25384 || this_var->di_tv.v_type == VAR_STRING)
25385 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025386 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025387 /* Escape special characters with a backslash. Turn a LF and
25388 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025389 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025390 (char_u *)"\\\"\n\r");
25391 if (p == NULL) /* out of memory */
25392 break;
25393 for (t = p; *t != NUL; ++t)
25394 if (*t == '\n')
25395 *t = 'n';
25396 else if (*t == '\r')
25397 *t = 'r';
25398 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025399 this_var->di_key,
25400 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25401 : ' ',
25402 p,
25403 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25404 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025405 || put_eol(fd) == FAIL)
25406 {
25407 vim_free(p);
25408 return FAIL;
25409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025410 vim_free(p);
25411 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025412#ifdef FEAT_FLOAT
25413 else if (this_var->di_tv.v_type == VAR_FLOAT
25414 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25415 {
25416 float_T f = this_var->di_tv.vval.v_float;
25417 int sign = ' ';
25418
25419 if (f < 0)
25420 {
25421 f = -f;
25422 sign = '-';
25423 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025424 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025425 this_var->di_key, sign, f) < 0)
25426 || put_eol(fd) == FAIL)
25427 return FAIL;
25428 }
25429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025430 }
25431 }
25432 return OK;
25433}
25434#endif
25435
Bram Moolenaar661b1822005-07-28 22:36:45 +000025436/*
25437 * Display script name where an item was last set.
25438 * Should only be invoked when 'verbose' is non-zero.
25439 */
25440 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025441last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025442{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025443 char_u *p;
25444
Bram Moolenaar661b1822005-07-28 22:36:45 +000025445 if (scriptID != 0)
25446 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025447 p = home_replace_save(NULL, get_scriptname(scriptID));
25448 if (p != NULL)
25449 {
25450 verbose_enter();
25451 MSG_PUTS(_("\n\tLast set from "));
25452 MSG_PUTS(p);
25453 vim_free(p);
25454 verbose_leave();
25455 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025456 }
25457}
25458
Bram Moolenaard812df62008-11-09 12:46:09 +000025459/*
25460 * List v:oldfiles in a nice way.
25461 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025462 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025463ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025464{
25465 list_T *l = vimvars[VV_OLDFILES].vv_list;
25466 listitem_T *li;
25467 int nr = 0;
25468
25469 if (l == NULL)
25470 msg((char_u *)_("No old files"));
25471 else
25472 {
25473 msg_start();
25474 msg_scroll = TRUE;
25475 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25476 {
25477 msg_outnum((long)++nr);
25478 MSG_PUTS(": ");
25479 msg_outtrans(get_tv_string(&li->li_tv));
25480 msg_putchar('\n');
25481 out_flush(); /* output one line at a time */
25482 ui_breakcheck();
25483 }
25484 /* Assume "got_int" was set to truncate the listing. */
25485 got_int = FALSE;
25486
25487#ifdef FEAT_BROWSE_CMD
25488 if (cmdmod.browse)
25489 {
25490 quit_more = FALSE;
25491 nr = prompt_for_number(FALSE);
25492 msg_starthere();
25493 if (nr > 0)
25494 {
25495 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25496 (long)nr);
25497
25498 if (p != NULL)
25499 {
25500 p = expand_env_save(p);
25501 eap->arg = p;
25502 eap->cmdidx = CMD_edit;
25503 cmdmod.browse = FALSE;
25504 do_exedit(eap, NULL);
25505 vim_free(p);
25506 }
25507 }
25508 }
25509#endif
25510 }
25511}
25512
Bram Moolenaar53744302015-07-17 17:38:22 +020025513/* reset v:option_new, v:option_old and v:option_type */
25514 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025515reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025516{
25517 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25518 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25519 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25520}
25521
25522
Bram Moolenaar071d4272004-06-13 20:20:40 +000025523#endif /* FEAT_EVAL */
25524
Bram Moolenaar071d4272004-06-13 20:20:40 +000025525
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025526#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527
25528#ifdef WIN3264
25529/*
25530 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25531 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025532static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25533static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25534static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025535
25536/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025537 * Get the short path (8.3) for the filename in "fnamep".
25538 * Only works for a valid file name.
25539 * When the path gets longer "fnamep" is changed and the allocated buffer
25540 * is put in "bufp".
25541 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25542 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025543 */
25544 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025545get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025546{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025547 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025548 char_u *newbuf;
25549
25550 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025551 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025552 if (l > len - 1)
25553 {
25554 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025555 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025556 newbuf = vim_strnsave(*fnamep, l);
25557 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025558 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559
25560 vim_free(*bufp);
25561 *fnamep = *bufp = newbuf;
25562
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025563 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025564 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565 }
25566
25567 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025568 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025569}
25570
25571/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025572 * Get the short path (8.3) for the filename in "fname". The converted
25573 * path is returned in "bufp".
25574 *
25575 * Some of the directories specified in "fname" may not exist. This function
25576 * will shorten the existing directories at the beginning of the path and then
25577 * append the remaining non-existing path.
25578 *
25579 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025580 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025581 * bufp - Pointer to an allocated buffer for the filename.
25582 * fnamelen - Length of the filename pointed to by fname
25583 *
25584 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025585 */
25586 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025587shortpath_for_invalid_fname(
25588 char_u **fname,
25589 char_u **bufp,
25590 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025591{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025592 char_u *short_fname, *save_fname, *pbuf_unused;
25593 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025595 int old_len, len;
25596 int new_len, sfx_len;
25597 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598
25599 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025600 old_len = *fnamelen;
25601 save_fname = vim_strnsave(*fname, old_len);
25602 pbuf_unused = NULL;
25603 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025604
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025605 endp = save_fname + old_len - 1; /* Find the end of the copy */
25606 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025608 /*
25609 * Try shortening the supplied path till it succeeds by removing one
25610 * directory at a time from the tail of the path.
25611 */
25612 len = 0;
25613 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025615 /* go back one path-separator */
25616 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25617 --endp;
25618 if (endp <= save_fname)
25619 break; /* processed the complete path */
25620
25621 /*
25622 * Replace the path separator with a NUL and try to shorten the
25623 * resulting path.
25624 */
25625 ch = *endp;
25626 *endp = 0;
25627 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025628 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025629 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25630 {
25631 retval = FAIL;
25632 goto theend;
25633 }
25634 *endp = ch; /* preserve the string */
25635
25636 if (len > 0)
25637 break; /* successfully shortened the path */
25638
25639 /* failed to shorten the path. Skip the path separator */
25640 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641 }
25642
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025643 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025644 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025645 /*
25646 * Succeeded in shortening the path. Now concatenate the shortened
25647 * path with the remaining path at the tail.
25648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025649
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025650 /* Compute the length of the new path. */
25651 sfx_len = (int)(save_endp - endp) + 1;
25652 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025654 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025656 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025658 /* There is not enough space in the currently allocated string,
25659 * copy it to a buffer big enough. */
25660 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025662 {
25663 retval = FAIL;
25664 goto theend;
25665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666 }
25667 else
25668 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025669 /* Transfer short_fname to the main buffer (it's big enough),
25670 * unless get_short_pathname() did its work in-place. */
25671 *fname = *bufp = save_fname;
25672 if (short_fname != save_fname)
25673 vim_strncpy(save_fname, short_fname, len);
25674 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025675 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025676
25677 /* concat the not-shortened part of the path */
25678 vim_strncpy(*fname + len, endp, sfx_len);
25679 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025680 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025681
25682theend:
25683 vim_free(pbuf_unused);
25684 vim_free(save_fname);
25685
25686 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025687}
25688
25689/*
25690 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025691 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692 */
25693 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025694shortpath_for_partial(
25695 char_u **fnamep,
25696 char_u **bufp,
25697 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025698{
25699 int sepcount, len, tflen;
25700 char_u *p;
25701 char_u *pbuf, *tfname;
25702 int hasTilde;
25703
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025704 /* Count up the path separators from the RHS.. so we know which part
25705 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025706 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025707 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025708 if (vim_ispathsep(*p))
25709 ++sepcount;
25710
25711 /* Need full path first (use expand_env() to remove a "~/") */
25712 hasTilde = (**fnamep == '~');
25713 if (hasTilde)
25714 pbuf = tfname = expand_env_save(*fnamep);
25715 else
25716 pbuf = tfname = FullName_save(*fnamep, FALSE);
25717
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025718 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025719
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025720 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25721 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025722
25723 if (len == 0)
25724 {
25725 /* Don't have a valid filename, so shorten the rest of the
25726 * path if we can. This CAN give us invalid 8.3 filenames, but
25727 * there's not a lot of point in guessing what it might be.
25728 */
25729 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025730 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25731 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025732 }
25733
25734 /* Count the paths backward to find the beginning of the desired string. */
25735 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025736 {
25737#ifdef FEAT_MBYTE
25738 if (has_mbyte)
25739 p -= mb_head_off(tfname, p);
25740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741 if (vim_ispathsep(*p))
25742 {
25743 if (sepcount == 0 || (hasTilde && sepcount == 1))
25744 break;
25745 else
25746 sepcount --;
25747 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025749 if (hasTilde)
25750 {
25751 --p;
25752 if (p >= tfname)
25753 *p = '~';
25754 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025755 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025756 }
25757 else
25758 ++p;
25759
25760 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25761 vim_free(*bufp);
25762 *fnamelen = (int)STRLEN(p);
25763 *bufp = pbuf;
25764 *fnamep = p;
25765
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025766 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025767}
25768#endif /* WIN3264 */
25769
25770/*
25771 * Adjust a filename, according to a string of modifiers.
25772 * *fnamep must be NUL terminated when called. When returning, the length is
25773 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025774 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025775 * When there is an error, *fnamep is set to NULL.
25776 */
25777 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025778modify_fname(
25779 char_u *src, /* string with modifiers */
25780 int *usedlen, /* characters after src that are used */
25781 char_u **fnamep, /* file name so far */
25782 char_u **bufp, /* buffer for allocated file name or NULL */
25783 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025784{
25785 int valid = 0;
25786 char_u *tail;
25787 char_u *s, *p, *pbuf;
25788 char_u dirname[MAXPATHL];
25789 int c;
25790 int has_fullname = 0;
25791#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025792 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793 int has_shortname = 0;
25794#endif
25795
25796repeat:
25797 /* ":p" - full path/file_name */
25798 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25799 {
25800 has_fullname = 1;
25801
25802 valid |= VALID_PATH;
25803 *usedlen += 2;
25804
25805 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25806 if ((*fnamep)[0] == '~'
25807#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25808 && ((*fnamep)[1] == '/'
25809# ifdef BACKSLASH_IN_FILENAME
25810 || (*fnamep)[1] == '\\'
25811# endif
25812 || (*fnamep)[1] == NUL)
25813
25814#endif
25815 )
25816 {
25817 *fnamep = expand_env_save(*fnamep);
25818 vim_free(*bufp); /* free any allocated file name */
25819 *bufp = *fnamep;
25820 if (*fnamep == NULL)
25821 return -1;
25822 }
25823
25824 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025825 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826 {
25827 if (vim_ispathsep(*p)
25828 && p[1] == '.'
25829 && (p[2] == NUL
25830 || vim_ispathsep(p[2])
25831 || (p[2] == '.'
25832 && (p[3] == NUL || vim_ispathsep(p[3])))))
25833 break;
25834 }
25835
25836 /* FullName_save() is slow, don't use it when not needed. */
25837 if (*p != NUL || !vim_isAbsName(*fnamep))
25838 {
25839 *fnamep = FullName_save(*fnamep, *p != NUL);
25840 vim_free(*bufp); /* free any allocated file name */
25841 *bufp = *fnamep;
25842 if (*fnamep == NULL)
25843 return -1;
25844 }
25845
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025846#ifdef WIN3264
25847# if _WIN32_WINNT >= 0x0500
25848 if (vim_strchr(*fnamep, '~') != NULL)
25849 {
25850 /* Expand 8.3 filename to full path. Needed to make sure the same
25851 * file does not have two different names.
25852 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25853 p = alloc(_MAX_PATH + 1);
25854 if (p != NULL)
25855 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025856 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025857 {
25858 vim_free(*bufp);
25859 *bufp = *fnamep = p;
25860 }
25861 else
25862 vim_free(p);
25863 }
25864 }
25865# endif
25866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025867 /* Append a path separator to a directory. */
25868 if (mch_isdir(*fnamep))
25869 {
25870 /* Make room for one or two extra characters. */
25871 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25872 vim_free(*bufp); /* free any allocated file name */
25873 *bufp = *fnamep;
25874 if (*fnamep == NULL)
25875 return -1;
25876 add_pathsep(*fnamep);
25877 }
25878 }
25879
25880 /* ":." - path relative to the current directory */
25881 /* ":~" - path relative to the home directory */
25882 /* ":8" - shortname path - postponed till after */
25883 while (src[*usedlen] == ':'
25884 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25885 {
25886 *usedlen += 2;
25887 if (c == '8')
25888 {
25889#ifdef WIN3264
25890 has_shortname = 1; /* Postpone this. */
25891#endif
25892 continue;
25893 }
25894 pbuf = NULL;
25895 /* Need full path first (use expand_env() to remove a "~/") */
25896 if (!has_fullname)
25897 {
25898 if (c == '.' && **fnamep == '~')
25899 p = pbuf = expand_env_save(*fnamep);
25900 else
25901 p = pbuf = FullName_save(*fnamep, FALSE);
25902 }
25903 else
25904 p = *fnamep;
25905
25906 has_fullname = 0;
25907
25908 if (p != NULL)
25909 {
25910 if (c == '.')
25911 {
25912 mch_dirname(dirname, MAXPATHL);
25913 s = shorten_fname(p, dirname);
25914 if (s != NULL)
25915 {
25916 *fnamep = s;
25917 if (pbuf != NULL)
25918 {
25919 vim_free(*bufp); /* free any allocated file name */
25920 *bufp = pbuf;
25921 pbuf = NULL;
25922 }
25923 }
25924 }
25925 else
25926 {
25927 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25928 /* Only replace it when it starts with '~' */
25929 if (*dirname == '~')
25930 {
25931 s = vim_strsave(dirname);
25932 if (s != NULL)
25933 {
25934 *fnamep = s;
25935 vim_free(*bufp);
25936 *bufp = s;
25937 }
25938 }
25939 }
25940 vim_free(pbuf);
25941 }
25942 }
25943
25944 tail = gettail(*fnamep);
25945 *fnamelen = (int)STRLEN(*fnamep);
25946
25947 /* ":h" - head, remove "/file_name", can be repeated */
25948 /* Don't remove the first "/" or "c:\" */
25949 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25950 {
25951 valid |= VALID_HEAD;
25952 *usedlen += 2;
25953 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025954 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025955 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025956 *fnamelen = (int)(tail - *fnamep);
25957#ifdef VMS
25958 if (*fnamelen > 0)
25959 *fnamelen += 1; /* the path separator is part of the path */
25960#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025961 if (*fnamelen == 0)
25962 {
25963 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25964 p = vim_strsave((char_u *)".");
25965 if (p == NULL)
25966 return -1;
25967 vim_free(*bufp);
25968 *bufp = *fnamep = tail = p;
25969 *fnamelen = 1;
25970 }
25971 else
25972 {
25973 while (tail > s && !after_pathsep(s, tail))
25974 mb_ptr_back(*fnamep, tail);
25975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025976 }
25977
25978 /* ":8" - shortname */
25979 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25980 {
25981 *usedlen += 2;
25982#ifdef WIN3264
25983 has_shortname = 1;
25984#endif
25985 }
25986
25987#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025988 /*
25989 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025990 */
25991 if (has_shortname)
25992 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025993 /* Copy the string if it is shortened by :h and when it wasn't copied
25994 * yet, because we are going to change it in place. Avoids changing
25995 * the buffer name for "%:8". */
25996 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997 {
25998 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025999 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026000 return -1;
26001 vim_free(*bufp);
26002 *bufp = *fnamep = p;
26003 }
26004
26005 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026006 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026007 if (!has_fullname && !vim_isAbsName(*fnamep))
26008 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026009 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026010 return -1;
26011 }
26012 else
26013 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026014 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026015
Bram Moolenaardc935552011-08-17 15:23:23 +020026016 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026017 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026018 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026019 return -1;
26020
26021 if (l == 0)
26022 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026023 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026024 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026025 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026 return -1;
26027 }
26028 *fnamelen = l;
26029 }
26030 }
26031#endif /* WIN3264 */
26032
26033 /* ":t" - tail, just the basename */
26034 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26035 {
26036 *usedlen += 2;
26037 *fnamelen -= (int)(tail - *fnamep);
26038 *fnamep = tail;
26039 }
26040
26041 /* ":e" - extension, can be repeated */
26042 /* ":r" - root, without extension, can be repeated */
26043 while (src[*usedlen] == ':'
26044 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26045 {
26046 /* find a '.' in the tail:
26047 * - for second :e: before the current fname
26048 * - otherwise: The last '.'
26049 */
26050 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26051 s = *fnamep - 2;
26052 else
26053 s = *fnamep + *fnamelen - 1;
26054 for ( ; s > tail; --s)
26055 if (s[0] == '.')
26056 break;
26057 if (src[*usedlen + 1] == 'e') /* :e */
26058 {
26059 if (s > tail)
26060 {
26061 *fnamelen += (int)(*fnamep - (s + 1));
26062 *fnamep = s + 1;
26063#ifdef VMS
26064 /* cut version from the extension */
26065 s = *fnamep + *fnamelen - 1;
26066 for ( ; s > *fnamep; --s)
26067 if (s[0] == ';')
26068 break;
26069 if (s > *fnamep)
26070 *fnamelen = s - *fnamep;
26071#endif
26072 }
26073 else if (*fnamep <= tail)
26074 *fnamelen = 0;
26075 }
26076 else /* :r */
26077 {
26078 if (s > tail) /* remove one extension */
26079 *fnamelen = (int)(s - *fnamep);
26080 }
26081 *usedlen += 2;
26082 }
26083
26084 /* ":s?pat?foo?" - substitute */
26085 /* ":gs?pat?foo?" - global substitute */
26086 if (src[*usedlen] == ':'
26087 && (src[*usedlen + 1] == 's'
26088 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26089 {
26090 char_u *str;
26091 char_u *pat;
26092 char_u *sub;
26093 int sep;
26094 char_u *flags;
26095 int didit = FALSE;
26096
26097 flags = (char_u *)"";
26098 s = src + *usedlen + 2;
26099 if (src[*usedlen + 1] == 'g')
26100 {
26101 flags = (char_u *)"g";
26102 ++s;
26103 }
26104
26105 sep = *s++;
26106 if (sep)
26107 {
26108 /* find end of pattern */
26109 p = vim_strchr(s, sep);
26110 if (p != NULL)
26111 {
26112 pat = vim_strnsave(s, (int)(p - s));
26113 if (pat != NULL)
26114 {
26115 s = p + 1;
26116 /* find end of substitution */
26117 p = vim_strchr(s, sep);
26118 if (p != NULL)
26119 {
26120 sub = vim_strnsave(s, (int)(p - s));
26121 str = vim_strnsave(*fnamep, *fnamelen);
26122 if (sub != NULL && str != NULL)
26123 {
26124 *usedlen = (int)(p + 1 - src);
26125 s = do_string_sub(str, pat, sub, flags);
26126 if (s != NULL)
26127 {
26128 *fnamep = s;
26129 *fnamelen = (int)STRLEN(s);
26130 vim_free(*bufp);
26131 *bufp = s;
26132 didit = TRUE;
26133 }
26134 }
26135 vim_free(sub);
26136 vim_free(str);
26137 }
26138 vim_free(pat);
26139 }
26140 }
26141 /* after using ":s", repeat all the modifiers */
26142 if (didit)
26143 goto repeat;
26144 }
26145 }
26146
Bram Moolenaar26df0922014-02-23 23:39:13 +010026147 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26148 {
26149 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26150 if (p == NULL)
26151 return -1;
26152 vim_free(*bufp);
26153 *bufp = *fnamep = p;
26154 *fnamelen = (int)STRLEN(p);
26155 *usedlen += 2;
26156 }
26157
Bram Moolenaar071d4272004-06-13 20:20:40 +000026158 return valid;
26159}
26160
26161/*
26162 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26163 * "flags" can be "g" to do a global substitute.
26164 * Returns an allocated string, NULL for error.
26165 */
26166 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026167do_string_sub(
26168 char_u *str,
26169 char_u *pat,
26170 char_u *sub,
26171 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026172{
26173 int sublen;
26174 regmatch_T regmatch;
26175 int i;
26176 int do_all;
26177 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026178 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026179 garray_T ga;
26180 char_u *ret;
26181 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026182 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026183
26184 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26185 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026186 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026187
26188 ga_init2(&ga, 1, 200);
26189
26190 do_all = (flags[0] == 'g');
26191
26192 regmatch.rm_ic = p_ic;
26193 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26194 if (regmatch.regprog != NULL)
26195 {
26196 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026197 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026198 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26199 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026200 /* Skip empty match except for first match. */
26201 if (regmatch.startp[0] == regmatch.endp[0])
26202 {
26203 if (zero_width == regmatch.startp[0])
26204 {
26205 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026206 i = MB_PTR2LEN(tail);
26207 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26208 (size_t)i);
26209 ga.ga_len += i;
26210 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026211 continue;
26212 }
26213 zero_width = regmatch.startp[0];
26214 }
26215
Bram Moolenaar071d4272004-06-13 20:20:40 +000026216 /*
26217 * Get some space for a temporary buffer to do the substitution
26218 * into. It will contain:
26219 * - The text up to where the match is.
26220 * - The substituted text.
26221 * - The text after the match.
26222 */
26223 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026224 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026225 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26226 {
26227 ga_clear(&ga);
26228 break;
26229 }
26230
26231 /* copy the text up to where the match is */
26232 i = (int)(regmatch.startp[0] - tail);
26233 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26234 /* add the substituted text */
26235 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26236 + ga.ga_len + i, TRUE, TRUE, FALSE);
26237 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026238 tail = regmatch.endp[0];
26239 if (*tail == NUL)
26240 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026241 if (!do_all)
26242 break;
26243 }
26244
26245 if (ga.ga_data != NULL)
26246 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26247
Bram Moolenaar473de612013-06-08 18:19:48 +020026248 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026249 }
26250
26251 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26252 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026253 if (p_cpo == empty_option)
26254 p_cpo = save_cpo;
26255 else
26256 /* Darn, evaluating {sub} expression changed the value. */
26257 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026258
26259 return ret;
26260}
26261
26262#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */