blob: 84b7197c80688755b9ad6f9999261ff2550b9d35 [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 Moolenaar6463ca22016-02-13 17:04:46 +0100507static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
508static void f_ch_open(typval_T *argvars, typval_T *rettv);
509static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100510static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
511static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100512static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100513#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100514static void f_changenr(typval_T *argvars, typval_T *rettv);
515static void f_char2nr(typval_T *argvars, typval_T *rettv);
516static void f_cindent(typval_T *argvars, typval_T *rettv);
517static void f_clearmatches(typval_T *argvars, typval_T *rettv);
518static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000519#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100520static void f_complete(typval_T *argvars, typval_T *rettv);
521static void f_complete_add(typval_T *argvars, typval_T *rettv);
522static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000523#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_confirm(typval_T *argvars, typval_T *rettv);
525static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_cos(typval_T *argvars, typval_T *rettv);
528static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_count(typval_T *argvars, typval_T *rettv);
531static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
532static void f_cursor(typval_T *argsvars, typval_T *rettv);
533static void f_deepcopy(typval_T *argvars, typval_T *rettv);
534static void f_delete(typval_T *argvars, typval_T *rettv);
535static void f_did_filetype(typval_T *argvars, typval_T *rettv);
536static void f_diff_filler(typval_T *argvars, typval_T *rettv);
537static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100538static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100539static void f_empty(typval_T *argvars, typval_T *rettv);
540static void f_escape(typval_T *argvars, typval_T *rettv);
541static void f_eval(typval_T *argvars, typval_T *rettv);
542static void f_eventhandler(typval_T *argvars, typval_T *rettv);
543static void f_executable(typval_T *argvars, typval_T *rettv);
544static void f_exepath(typval_T *argvars, typval_T *rettv);
545static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100549static void f_expand(typval_T *argvars, typval_T *rettv);
550static void f_extend(typval_T *argvars, typval_T *rettv);
551static void f_feedkeys(typval_T *argvars, typval_T *rettv);
552static void f_filereadable(typval_T *argvars, typval_T *rettv);
553static void f_filewritable(typval_T *argvars, typval_T *rettv);
554static void f_filter(typval_T *argvars, typval_T *rettv);
555static void f_finddir(typval_T *argvars, typval_T *rettv);
556static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000557#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_float2nr(typval_T *argvars, typval_T *rettv);
559static void f_floor(typval_T *argvars, typval_T *rettv);
560static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000561#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100562static void f_fnameescape(typval_T *argvars, typval_T *rettv);
563static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
564static void f_foldclosed(typval_T *argvars, typval_T *rettv);
565static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
566static void f_foldlevel(typval_T *argvars, typval_T *rettv);
567static void f_foldtext(typval_T *argvars, typval_T *rettv);
568static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
569static void f_foreground(typval_T *argvars, typval_T *rettv);
570static void f_function(typval_T *argvars, typval_T *rettv);
571static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
572static void f_get(typval_T *argvars, typval_T *rettv);
573static void f_getbufline(typval_T *argvars, typval_T *rettv);
574static void f_getbufvar(typval_T *argvars, typval_T *rettv);
575static void f_getchar(typval_T *argvars, typval_T *rettv);
576static void f_getcharmod(typval_T *argvars, typval_T *rettv);
577static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
578static void f_getcmdline(typval_T *argvars, typval_T *rettv);
579static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
580static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
581static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
582static void f_getcwd(typval_T *argvars, typval_T *rettv);
583static void f_getfontname(typval_T *argvars, typval_T *rettv);
584static void f_getfperm(typval_T *argvars, typval_T *rettv);
585static void f_getfsize(typval_T *argvars, typval_T *rettv);
586static void f_getftime(typval_T *argvars, typval_T *rettv);
587static void f_getftype(typval_T *argvars, typval_T *rettv);
588static void f_getline(typval_T *argvars, typval_T *rettv);
589static void f_getmatches(typval_T *argvars, typval_T *rettv);
590static void f_getpid(typval_T *argvars, typval_T *rettv);
591static void f_getcurpos(typval_T *argvars, typval_T *rettv);
592static void f_getpos(typval_T *argvars, typval_T *rettv);
593static void f_getqflist(typval_T *argvars, typval_T *rettv);
594static void f_getreg(typval_T *argvars, typval_T *rettv);
595static void f_getregtype(typval_T *argvars, typval_T *rettv);
596static void f_gettabvar(typval_T *argvars, typval_T *rettv);
597static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
598static void f_getwinposx(typval_T *argvars, typval_T *rettv);
599static void f_getwinposy(typval_T *argvars, typval_T *rettv);
600static void f_getwinvar(typval_T *argvars, typval_T *rettv);
601static void f_glob(typval_T *argvars, typval_T *rettv);
602static void f_globpath(typval_T *argvars, typval_T *rettv);
603static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
604static void f_has(typval_T *argvars, typval_T *rettv);
605static void f_has_key(typval_T *argvars, typval_T *rettv);
606static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
607static void f_hasmapto(typval_T *argvars, typval_T *rettv);
608static void f_histadd(typval_T *argvars, typval_T *rettv);
609static void f_histdel(typval_T *argvars, typval_T *rettv);
610static void f_histget(typval_T *argvars, typval_T *rettv);
611static void f_histnr(typval_T *argvars, typval_T *rettv);
612static void f_hlID(typval_T *argvars, typval_T *rettv);
613static void f_hlexists(typval_T *argvars, typval_T *rettv);
614static void f_hostname(typval_T *argvars, typval_T *rettv);
615static void f_iconv(typval_T *argvars, typval_T *rettv);
616static void f_indent(typval_T *argvars, typval_T *rettv);
617static void f_index(typval_T *argvars, typval_T *rettv);
618static void f_input(typval_T *argvars, typval_T *rettv);
619static void f_inputdialog(typval_T *argvars, typval_T *rettv);
620static void f_inputlist(typval_T *argvars, typval_T *rettv);
621static void f_inputrestore(typval_T *argvars, typval_T *rettv);
622static void f_inputsave(typval_T *argvars, typval_T *rettv);
623static void f_inputsecret(typval_T *argvars, typval_T *rettv);
624static void f_insert(typval_T *argvars, typval_T *rettv);
625static void f_invert(typval_T *argvars, typval_T *rettv);
626static void f_isdirectory(typval_T *argvars, typval_T *rettv);
627static void f_islocked(typval_T *argvars, typval_T *rettv);
628static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100629#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100630# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100631static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100632# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +0100633static void f_job_start(typval_T *argvars, typval_T *rettv);
634static void f_job_stop(typval_T *argvars, typval_T *rettv);
635static void f_job_status(typval_T *argvars, typval_T *rettv);
636#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100637static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100638static void f_js_decode(typval_T *argvars, typval_T *rettv);
639static void f_js_encode(typval_T *argvars, typval_T *rettv);
640static void f_json_decode(typval_T *argvars, typval_T *rettv);
641static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100642static void f_keys(typval_T *argvars, typval_T *rettv);
643static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
644static void f_len(typval_T *argvars, typval_T *rettv);
645static void f_libcall(typval_T *argvars, typval_T *rettv);
646static void f_libcallnr(typval_T *argvars, typval_T *rettv);
647static void f_line(typval_T *argvars, typval_T *rettv);
648static void f_line2byte(typval_T *argvars, typval_T *rettv);
649static void f_lispindent(typval_T *argvars, typval_T *rettv);
650static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000651#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100652static void f_log(typval_T *argvars, typval_T *rettv);
653static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200655#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200657#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_map(typval_T *argvars, typval_T *rettv);
659static void f_maparg(typval_T *argvars, typval_T *rettv);
660static void f_mapcheck(typval_T *argvars, typval_T *rettv);
661static void f_match(typval_T *argvars, typval_T *rettv);
662static void f_matchadd(typval_T *argvars, typval_T *rettv);
663static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
664static void f_matcharg(typval_T *argvars, typval_T *rettv);
665static void f_matchdelete(typval_T *argvars, typval_T *rettv);
666static void f_matchend(typval_T *argvars, typval_T *rettv);
667static void f_matchlist(typval_T *argvars, typval_T *rettv);
668static void f_matchstr(typval_T *argvars, typval_T *rettv);
669static void f_max(typval_T *argvars, typval_T *rettv);
670static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000671#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000673#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100675#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100677#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
679static void f_nr2char(typval_T *argvars, typval_T *rettv);
680static void f_or(typval_T *argvars, typval_T *rettv);
681static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100682#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100683static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100684#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000685#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000687#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
689static void f_printf(typval_T *argvars, typval_T *rettv);
690static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200691#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200693#endif
694#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_range(typval_T *argvars, typval_T *rettv);
698static void f_readfile(typval_T *argvars, typval_T *rettv);
699static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100700#ifdef FEAT_FLOAT
701static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
702#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_reltimestr(typval_T *argvars, typval_T *rettv);
704static void f_remote_expr(typval_T *argvars, typval_T *rettv);
705static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
706static void f_remote_peek(typval_T *argvars, typval_T *rettv);
707static void f_remote_read(typval_T *argvars, typval_T *rettv);
708static void f_remote_send(typval_T *argvars, typval_T *rettv);
709static void f_remove(typval_T *argvars, typval_T *rettv);
710static void f_rename(typval_T *argvars, typval_T *rettv);
711static void f_repeat(typval_T *argvars, typval_T *rettv);
712static void f_resolve(typval_T *argvars, typval_T *rettv);
713static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000714#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100715static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100717static void f_screenattr(typval_T *argvars, typval_T *rettv);
718static void f_screenchar(typval_T *argvars, typval_T *rettv);
719static void f_screencol(typval_T *argvars, typval_T *rettv);
720static void f_screenrow(typval_T *argvars, typval_T *rettv);
721static void f_search(typval_T *argvars, typval_T *rettv);
722static void f_searchdecl(typval_T *argvars, typval_T *rettv);
723static void f_searchpair(typval_T *argvars, typval_T *rettv);
724static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
725static void f_searchpos(typval_T *argvars, typval_T *rettv);
726static void f_server2client(typval_T *argvars, typval_T *rettv);
727static void f_serverlist(typval_T *argvars, typval_T *rettv);
728static void f_setbufvar(typval_T *argvars, typval_T *rettv);
729static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
730static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
731static void f_setline(typval_T *argvars, typval_T *rettv);
732static void f_setloclist(typval_T *argvars, typval_T *rettv);
733static void f_setmatches(typval_T *argvars, typval_T *rettv);
734static void f_setpos(typval_T *argvars, typval_T *rettv);
735static void f_setqflist(typval_T *argvars, typval_T *rettv);
736static void f_setreg(typval_T *argvars, typval_T *rettv);
737static void f_settabvar(typval_T *argvars, typval_T *rettv);
738static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
739static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100740#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100741static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100742#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100743static void f_shellescape(typval_T *argvars, typval_T *rettv);
744static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
745static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000746#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_sin(typval_T *argvars, typval_T *rettv);
748static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000749#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_sort(typval_T *argvars, typval_T *rettv);
751static void f_soundfold(typval_T *argvars, typval_T *rettv);
752static void f_spellbadword(typval_T *argvars, typval_T *rettv);
753static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
754static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000755#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_sqrt(typval_T *argvars, typval_T *rettv);
757static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000758#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100759static void f_str2nr(typval_T *argvars, typval_T *rettv);
760static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000761#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100762static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000763#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_stridx(typval_T *argvars, typval_T *rettv);
765static void f_string(typval_T *argvars, typval_T *rettv);
766static void f_strlen(typval_T *argvars, typval_T *rettv);
767static void f_strpart(typval_T *argvars, typval_T *rettv);
768static void f_strridx(typval_T *argvars, typval_T *rettv);
769static void f_strtrans(typval_T *argvars, typval_T *rettv);
770static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
771static void f_strwidth(typval_T *argvars, typval_T *rettv);
772static void f_submatch(typval_T *argvars, typval_T *rettv);
773static void f_substitute(typval_T *argvars, typval_T *rettv);
774static void f_synID(typval_T *argvars, typval_T *rettv);
775static void f_synIDattr(typval_T *argvars, typval_T *rettv);
776static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
777static void f_synstack(typval_T *argvars, typval_T *rettv);
778static void f_synconcealed(typval_T *argvars, typval_T *rettv);
779static void f_system(typval_T *argvars, typval_T *rettv);
780static void f_systemlist(typval_T *argvars, typval_T *rettv);
781static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
782static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
783static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
784static void f_taglist(typval_T *argvars, typval_T *rettv);
785static void f_tagfiles(typval_T *argvars, typval_T *rettv);
786static void f_tempname(typval_T *argvars, typval_T *rettv);
787static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200788#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100789static void f_tan(typval_T *argvars, typval_T *rettv);
790static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200791#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100792static void f_tolower(typval_T *argvars, typval_T *rettv);
793static void f_toupper(typval_T *argvars, typval_T *rettv);
794static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000795#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000797#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100798static void f_type(typval_T *argvars, typval_T *rettv);
799static void f_undofile(typval_T *argvars, typval_T *rettv);
800static void f_undotree(typval_T *argvars, typval_T *rettv);
801static void f_uniq(typval_T *argvars, typval_T *rettv);
802static void f_values(typval_T *argvars, typval_T *rettv);
803static void f_virtcol(typval_T *argvars, typval_T *rettv);
804static void f_visualmode(typval_T *argvars, typval_T *rettv);
805static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
806static void f_winbufnr(typval_T *argvars, typval_T *rettv);
807static void f_wincol(typval_T *argvars, typval_T *rettv);
808static void f_winheight(typval_T *argvars, typval_T *rettv);
809static void f_winline(typval_T *argvars, typval_T *rettv);
810static void f_winnr(typval_T *argvars, typval_T *rettv);
811static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
812static void f_winrestview(typval_T *argvars, typval_T *rettv);
813static void f_winsaveview(typval_T *argvars, typval_T *rettv);
814static void f_winwidth(typval_T *argvars, typval_T *rettv);
815static void f_writefile(typval_T *argvars, typval_T *rettv);
816static void f_wordcount(typval_T *argvars, typval_T *rettv);
817static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000818
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100819static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
820static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
821static int get_env_len(char_u **arg);
822static int get_id_len(char_u **arg);
823static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
824static 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 +0000825#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
826#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
827 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100828static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
829static int eval_isnamec(int c);
830static int eval_isnamec1(int c);
831static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
832static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static typval_T *alloc_string_tv(char_u *string);
834static void init_tv(typval_T *varp);
835static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100836#ifdef FEAT_FLOAT
837static float_T get_tv_float(typval_T *varp);
838#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100839static linenr_T get_tv_lnum(typval_T *argvars);
840static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
841static char_u *get_tv_string(typval_T *varp);
842static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
843static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
844static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
845static hashtab_T *find_var_ht(char_u *name, char_u **varname);
846static funccall_T *get_funccal(void);
847static void vars_clear_ext(hashtab_T *ht, int free_val);
848static void delete_var(hashtab_T *ht, hashitem_T *hi);
849static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
850static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
851static void set_var(char_u *name, typval_T *varp, int copy);
852static int var_check_ro(int flags, char_u *name, int use_gettext);
853static int var_check_fixed(int flags, char_u *name, int use_gettext);
854static int var_check_func_name(char_u *name, int new_var);
855static int valid_varname(char_u *varname);
856static int tv_check_lock(int lock, char_u *name, int use_gettext);
857static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
858static char_u *find_option_end(char_u **arg, int *opt_flags);
859static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
860static int eval_fname_script(char_u *p);
861static int eval_fname_sid(char_u *p);
862static void list_func_head(ufunc_T *fp, int indent);
863static ufunc_T *find_func(char_u *name);
864static int function_exists(char_u *name);
865static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000866#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100867static void func_do_profile(ufunc_T *fp);
868static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
869static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000870static int
871# ifdef __BORLANDC__
872 _RTLENTRYF
873# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000875static int
876# ifdef __BORLANDC__
877 _RTLENTRYF
878# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100879 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000880#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881static int script_autoload(char_u *name, int reload);
882static char_u *autoload_name(char_u *name);
883static void cat_func_name(char_u *buf, ufunc_T *fp);
884static void func_free(ufunc_T *fp);
885static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
886static int can_free_funccal(funccall_T *fc, int copyID) ;
887static void free_funccal(funccall_T *fc, int free_val);
888static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
889static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
890static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
891static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
892static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
893static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
894static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
895static int write_list(FILE *fd, list_T *list, int binary);
896static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000897
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200898
899#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100900static int compare_func_name(const void *s1, const void *s2);
901static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200902#endif
903
Bram Moolenaar33570922005-01-25 22:26:29 +0000904/*
905 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000906 */
907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100908eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000909{
Bram Moolenaar33570922005-01-25 22:26:29 +0000910 int i;
911 struct vimvar *p;
912
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200913 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
914 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200915 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000916 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000917 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000918
919 for (i = 0; i < VV_LEN; ++i)
920 {
921 p = &vimvars[i];
922 STRCPY(p->vv_di.di_key, p->vv_name);
923 if (p->vv_flags & VV_RO)
924 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
925 else if (p->vv_flags & VV_RO_SBX)
926 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
927 else
928 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000929
930 /* add to v: scope dict, unless the value is not always available */
931 if (p->vv_type != VAR_UNKNOWN)
932 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000933 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000934 /* add to compat scope dict */
935 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000936 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100937 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
938
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000939 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100940 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200941 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100942 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100943
944 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
945 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
946 set_vim_var_nr(VV_NONE, VVAL_NONE);
947 set_vim_var_nr(VV_NULL, VVAL_NULL);
948
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200949 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200950
951#ifdef EBCDIC
952 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100953 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200954 */
955 sortFunctions();
956#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000957}
958
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000959#if defined(EXITFREE) || defined(PROTO)
960 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100961eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000962{
963 int i;
964 struct vimvar *p;
965
966 for (i = 0; i < VV_LEN; ++i)
967 {
968 p = &vimvars[i];
969 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000970 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000971 vim_free(p->vv_str);
972 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000973 }
974 else if (p->vv_di.di_tv.v_type == VAR_LIST)
975 {
976 list_unref(p->vv_list);
977 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000978 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000979 }
980 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000981 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000982 hash_clear(&compat_hashtab);
983
Bram Moolenaard9fba312005-06-26 22:34:35 +0000984 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100985# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200986 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100987# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000988
989 /* global variables */
990 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000991
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000992 /* autoloaded script names */
993 ga_clear_strings(&ga_loaded);
994
Bram Moolenaarcca74132013-09-25 21:00:28 +0200995 /* Script-local variables. First clear all the variables and in a second
996 * loop free the scriptvar_T, because a variable in one script might hold
997 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200998 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200999 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001000 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001001 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001002 ga_clear(&ga_scripts);
1003
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001004 /* unreferenced lists and dicts */
1005 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001006
1007 /* functions */
1008 free_all_functions();
1009 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001010}
1011#endif
1012
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 * Return the name of the executed function.
1015 */
1016 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001017func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020}
1021
1022/*
1023 * Return the address holding the next breakpoint line for a funccall cookie.
1024 */
1025 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001026func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
Bram Moolenaar33570922005-01-25 22:26:29 +00001028 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029}
1030
1031/*
1032 * Return the address holding the debug tick for a funccall cookie.
1033 */
1034 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001035func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
Bram Moolenaar33570922005-01-25 22:26:29 +00001037 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038}
1039
1040/*
1041 * Return the nesting level for a funccall cookie.
1042 */
1043 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001044func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
Bram Moolenaar33570922005-01-25 22:26:29 +00001046 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047}
1048
1049/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001050funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001052/* pointer to list of previously used funccal, still around because some
1053 * item in it is still being used. */
1054funccall_T *previous_funccal = NULL;
1055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056/*
1057 * Return TRUE when a function was ended by a ":return" command.
1058 */
1059 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001060current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061{
1062 return current_funccal->returned;
1063}
1064
1065
1066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 * Set an internal variable to a string value. Creates the variable if it does
1068 * not already exist.
1069 */
1070 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001071set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001073 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001074 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
1076 val = vim_strsave(value);
1077 if (val != NULL)
1078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001079 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001080 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001082 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001083 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085 }
1086}
1087
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001089static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090static char_u *redir_endp = NULL;
1091static char_u *redir_varname = NULL;
1092
1093/*
1094 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001095 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 * Returns OK if successfully completed the setup. FAIL otherwise.
1097 */
1098 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001099var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100{
1101 int save_emsg;
1102 int err;
1103 typval_T tv;
1104
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001105 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001106 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 {
1108 EMSG(_(e_invarg));
1109 return FAIL;
1110 }
1111
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001112 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 redir_varname = vim_strsave(name);
1114 if (redir_varname == NULL)
1115 return FAIL;
1116
1117 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1118 if (redir_lval == NULL)
1119 {
1120 var_redir_stop();
1121 return FAIL;
1122 }
1123
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 /* The output is stored in growarray "redir_ga" until redirection ends. */
1125 ga_init2(&redir_ga, (int)sizeof(char), 500);
1126
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001128 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001129 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1131 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001132 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 if (redir_endp != NULL && *redir_endp != NUL)
1134 /* Trailing characters are present after the variable name */
1135 EMSG(_(e_trailing));
1136 else
1137 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001138 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 var_redir_stop();
1140 return FAIL;
1141 }
1142
1143 /* check if we can write to the variable: set it to or append an empty
1144 * string */
1145 save_emsg = did_emsg;
1146 did_emsg = FALSE;
1147 tv.v_type = VAR_STRING;
1148 tv.vval.v_string = (char_u *)"";
1149 if (append)
1150 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1151 else
1152 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001153 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001155 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 if (err)
1157 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 var_redir_stop();
1160 return FAIL;
1161 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162
1163 return OK;
1164}
1165
1166/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167 * Append "value[value_len]" to the variable set by var_redir_start().
1168 * The actual appending is postponed until redirection ends, because the value
1169 * appended may in fact be the string we write to, changing it may cause freed
1170 * memory to be used:
1171 * :redir => foo
1172 * :let foo
1173 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174 */
1175 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001176var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001177{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001178 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179
1180 if (redir_lval == NULL)
1181 return;
1182
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001183 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001184 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001186 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189 {
1190 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001191 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001192 }
1193 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195}
1196
1197/*
1198 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001199 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200 */
1201 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001202var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001204 typval_T tv;
1205
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206 if (redir_lval != NULL)
1207 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001208 /* If there was no error: assign the text to the variable. */
1209 if (redir_endp != NULL)
1210 {
1211 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1212 tv.v_type = VAR_STRING;
1213 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001214 /* Call get_lval() again, if it's inside a Dict or List it may
1215 * have changed. */
1216 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001217 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001218 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1219 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1220 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001221 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001222
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001223 /* free the collected output */
1224 vim_free(redir_ga.ga_data);
1225 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001226
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 vim_free(redir_lval);
1228 redir_lval = NULL;
1229 }
1230 vim_free(redir_varname);
1231 redir_varname = NULL;
1232}
1233
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234# if defined(FEAT_MBYTE) || defined(PROTO)
1235 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001236eval_charconvert(
1237 char_u *enc_from,
1238 char_u *enc_to,
1239 char_u *fname_from,
1240 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241{
1242 int err = FALSE;
1243
1244 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1245 set_vim_var_string(VV_CC_TO, enc_to, -1);
1246 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1247 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1248 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1249 err = TRUE;
1250 set_vim_var_string(VV_CC_FROM, NULL, -1);
1251 set_vim_var_string(VV_CC_TO, NULL, -1);
1252 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1253 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1254
1255 if (err)
1256 return FAIL;
1257 return OK;
1258}
1259# endif
1260
1261# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1262 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001263eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
1265 int err = FALSE;
1266
1267 set_vim_var_string(VV_FNAME_IN, fname, -1);
1268 set_vim_var_string(VV_CMDARG, args, -1);
1269 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1270 err = TRUE;
1271 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1272 set_vim_var_string(VV_CMDARG, NULL, -1);
1273
1274 if (err)
1275 {
1276 mch_remove(fname);
1277 return FAIL;
1278 }
1279 return OK;
1280}
1281# endif
1282
1283# if defined(FEAT_DIFF) || defined(PROTO)
1284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001285eval_diff(
1286 char_u *origfile,
1287 char_u *newfile,
1288 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289{
1290 int err = FALSE;
1291
1292 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1293 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1294 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1295 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1296 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1297 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1298 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1299}
1300
1301 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001302eval_patch(
1303 char_u *origfile,
1304 char_u *difffile,
1305 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
1307 int err;
1308
1309 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1310 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1311 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1312 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1313 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1314 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1315 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1316}
1317# endif
1318
1319/*
1320 * Top level evaluation function, returning a boolean.
1321 * Sets "error" to TRUE if there was an error.
1322 * Return TRUE or FALSE.
1323 */
1324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001325eval_to_bool(
1326 char_u *arg,
1327 int *error,
1328 char_u **nextcmd,
1329 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330{
Bram Moolenaar33570922005-01-25 22:26:29 +00001331 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 int retval = FALSE;
1333
1334 if (skip)
1335 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 else
1339 {
1340 *error = FALSE;
1341 if (!skip)
1342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001343 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001344 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346 }
1347 if (skip)
1348 --emsg_skip;
1349
1350 return retval;
1351}
1352
1353/*
1354 * Top level evaluation function, returning a string. If "skip" is TRUE,
1355 * only parsing to "nextcmd" is done, without reporting errors. Return
1356 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1357 */
1358 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001359eval_to_string_skip(
1360 char_u *arg,
1361 char_u **nextcmd,
1362 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
Bram Moolenaar33570922005-01-25 22:26:29 +00001364 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 char_u *retval;
1366
1367 if (skip)
1368 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001369 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 retval = NULL;
1371 else
1372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 retval = vim_strsave(get_tv_string(&tv));
1374 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
1376 if (skip)
1377 --emsg_skip;
1378
1379 return retval;
1380}
1381
1382/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001383 * Skip over an expression at "*pp".
1384 * Return FAIL for an error, OK otherwise.
1385 */
1386 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001387skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001388{
Bram Moolenaar33570922005-01-25 22:26:29 +00001389 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001390
1391 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001392 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001393}
1394
1395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001397 * When "convert" is TRUE convert a List into a sequence of lines and convert
1398 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 * Return pointer to allocated memory, or NULL for failure.
1400 */
1401 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402eval_to_string(
1403 char_u *arg,
1404 char_u **nextcmd,
1405 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001409 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001410#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001411 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001414 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 retval = NULL;
1416 else
1417 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001418 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001419 {
1420 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001421 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001422 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001423 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001424 if (tv.vval.v_list->lv_len > 0)
1425 ga_append(&ga, NL);
1426 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001427 ga_append(&ga, NUL);
1428 retval = (char_u *)ga.ga_data;
1429 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001430#ifdef FEAT_FLOAT
1431 else if (convert && tv.v_type == VAR_FLOAT)
1432 {
1433 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1434 retval = vim_strsave(numbuf);
1435 }
1436#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001437 else
1438 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 }
1441
1442 return retval;
1443}
1444
1445/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001446 * Call eval_to_string() without using current local variables and using
1447 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 */
1449 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001450eval_to_string_safe(
1451 char_u *arg,
1452 char_u **nextcmd,
1453 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 char_u *retval;
1456 void *save_funccalp;
1457
1458 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001459 if (use_sandbox)
1460 ++sandbox;
1461 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001462 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001463 if (use_sandbox)
1464 --sandbox;
1465 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 restore_funccal(save_funccalp);
1467 return retval;
1468}
1469
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470/*
1471 * Top level evaluation function, returning a number.
1472 * Evaluates "expr" silently.
1473 * Returns -1 for an error.
1474 */
1475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001476eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
Bram Moolenaar33570922005-01-25 22:26:29 +00001478 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001480 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481
1482 ++emsg_off;
1483
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001484 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 retval = -1;
1486 else
1487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001488 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491 --emsg_off;
1492
1493 return retval;
1494}
1495
Bram Moolenaara40058a2005-07-11 22:42:07 +00001496/*
1497 * Prepare v: variable "idx" to be used.
1498 * Save the current typeval in "save_tv".
1499 * When not used yet add the variable to the v: hashtable.
1500 */
1501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001503{
1504 *save_tv = vimvars[idx].vv_tv;
1505 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1506 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1507}
1508
1509/*
1510 * Restore v: variable "idx" to typeval "save_tv".
1511 * When no longer defined, remove the variable from the v: hashtable.
1512 */
1513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001514restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001515{
1516 hashitem_T *hi;
1517
Bram Moolenaara40058a2005-07-11 22:42:07 +00001518 vimvars[idx].vv_tv = *save_tv;
1519 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1520 {
1521 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1522 if (HASHITEM_EMPTY(hi))
1523 EMSG2(_(e_intern2), "restore_vimvar()");
1524 else
1525 hash_remove(&vimvarht, hi);
1526 }
1527}
1528
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001529#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001530/*
1531 * Evaluate an expression to a list with suggestions.
1532 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001533 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001534 */
1535 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001536eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001537{
1538 typval_T save_val;
1539 typval_T rettv;
1540 list_T *list = NULL;
1541 char_u *p = skipwhite(expr);
1542
1543 /* Set "v:val" to the bad word. */
1544 prepare_vimvar(VV_VAL, &save_val);
1545 vimvars[VV_VAL].vv_type = VAR_STRING;
1546 vimvars[VV_VAL].vv_str = badword;
1547 if (p_verbose == 0)
1548 ++emsg_off;
1549
1550 if (eval1(&p, &rettv, TRUE) == OK)
1551 {
1552 if (rettv.v_type != VAR_LIST)
1553 clear_tv(&rettv);
1554 else
1555 list = rettv.vval.v_list;
1556 }
1557
1558 if (p_verbose == 0)
1559 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001560 restore_vimvar(VV_VAL, &save_val);
1561
1562 return list;
1563}
1564
1565/*
1566 * "list" is supposed to contain two items: a word and a number. Return the
1567 * word in "pp" and the number as the return value.
1568 * Return -1 if anything isn't right.
1569 * Used to get the good word and score from the eval_spell_expr() result.
1570 */
1571 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001572get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001573{
1574 listitem_T *li;
1575
1576 li = list->lv_first;
1577 if (li == NULL)
1578 return -1;
1579 *pp = get_tv_string(&li->li_tv);
1580
1581 li = li->li_next;
1582 if (li == NULL)
1583 return -1;
1584 return get_tv_number(&li->li_tv);
1585}
1586#endif
1587
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001588/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001589 * Top level evaluation function.
1590 * Returns an allocated typval_T with the result.
1591 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001592 */
1593 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001594eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001595{
1596 typval_T *tv;
1597
1598 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001599 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001600 {
1601 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001602 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001603 }
1604
1605 return tv;
1606}
1607
1608
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001611 * Uses argv[argc] for the function arguments. Only Number and String
1612 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001616call_vim_function(
1617 char_u *func,
1618 int argc,
1619 char_u **argv,
1620 int safe, /* use the sandbox */
1621 int str_arg_only, /* all arguments are strings */
1622 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
Bram Moolenaar33570922005-01-25 22:26:29 +00001624 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 long n;
1626 int len;
1627 int i;
1628 int doesrange;
1629 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001632 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
1636 for (i = 0; i < argc; i++)
1637 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001638 /* Pass a NULL or empty argument as an empty string */
1639 if (argv[i] == NULL || *argv[i] == NUL)
1640 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001641 argvars[i].v_type = VAR_STRING;
1642 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001643 continue;
1644 }
1645
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001646 if (str_arg_only)
1647 len = 0;
1648 else
1649 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001650 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (len != 0 && len == (int)STRLEN(argv[i]))
1652 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001653 argvars[i].v_type = VAR_NUMBER;
1654 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 }
1656 else
1657 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001658 argvars[i].v_type = VAR_STRING;
1659 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661 }
1662
1663 if (safe)
1664 {
1665 save_funccalp = save_funccal();
1666 ++sandbox;
1667 }
1668
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1670 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (safe)
1674 {
1675 --sandbox;
1676 restore_funccal(save_funccalp);
1677 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001678 vim_free(argvars);
1679
1680 if (ret == FAIL)
1681 clear_tv(rettv);
1682
1683 return ret;
1684}
1685
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001686/*
1687 * Call vimL function "func" and return the result as a number.
1688 * Returns -1 when calling the function fails.
1689 * Uses argv[argc] for the function arguments.
1690 */
1691 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001692call_func_retnr(
1693 char_u *func,
1694 int argc,
1695 char_u **argv,
1696 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001697{
1698 typval_T rettv;
1699 long retval;
1700
1701 /* All arguments are passed as strings, no conversion to number. */
1702 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1703 return -1;
1704
1705 retval = get_tv_number_chk(&rettv, NULL);
1706 clear_tv(&rettv);
1707 return retval;
1708}
1709
1710#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1711 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1712
Bram Moolenaar4f688582007-07-24 12:34:30 +00001713# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001715 * Call vimL function "func" and return the result as a string.
1716 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001717 * Uses argv[argc] for the function arguments.
1718 */
1719 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001720call_func_retstr(
1721 char_u *func,
1722 int argc,
1723 char_u **argv,
1724 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725{
1726 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001727 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001729 /* All arguments are passed as strings, no conversion to number. */
1730 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731 return NULL;
1732
1733 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001734 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 return retval;
1736}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001737# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001739/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001740 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001742 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743 */
1744 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001745call_func_retlist(
1746 char_u *func,
1747 int argc,
1748 char_u **argv,
1749 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750{
1751 typval_T rettv;
1752
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001753 /* All arguments are passed as strings, no conversion to number. */
1754 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001755 return NULL;
1756
1757 if (rettv.v_type != VAR_LIST)
1758 {
1759 clear_tv(&rettv);
1760 return NULL;
1761 }
1762
1763 return rettv.vval.v_list;
1764}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#endif
1766
1767/*
1768 * Save the current function call pointer, and set it to NULL.
1769 * Used when executing autocommands and for ":source".
1770 */
1771 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001772save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001774 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 current_funccal = NULL;
1777 return (void *)fc;
1778}
1779
1780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001781restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001783 funccall_T *fc = (funccall_T *)vfc;
1784
1785 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786}
1787
Bram Moolenaar05159a02005-02-26 23:04:13 +00001788#if defined(FEAT_PROFILE) || defined(PROTO)
1789/*
1790 * Prepare profiling for entering a child or something else that is not
1791 * counted for the script/function itself.
1792 * Should always be called in pair with prof_child_exit().
1793 */
1794 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001795prof_child_enter(
1796 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001797{
1798 funccall_T *fc = current_funccal;
1799
1800 if (fc != NULL && fc->func->uf_profiling)
1801 profile_start(&fc->prof_child);
1802 script_prof_save(tm);
1803}
1804
1805/*
1806 * Take care of time spent in a child.
1807 * Should always be called after prof_child_enter().
1808 */
1809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001810prof_child_exit(
1811 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001812{
1813 funccall_T *fc = current_funccal;
1814
1815 if (fc != NULL && fc->func->uf_profiling)
1816 {
1817 profile_end(&fc->prof_child);
1818 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1819 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1820 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1821 }
1822 script_prof_restore(tm);
1823}
1824#endif
1825
1826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827#ifdef FEAT_FOLDING
1828/*
1829 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1830 * it in "*cp". Doesn't give error messages.
1831 */
1832 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001833eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
Bram Moolenaar33570922005-01-25 22:26:29 +00001835 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 int retval;
1837 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001838 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1839 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001842 if (use_sandbox)
1843 ++sandbox;
1844 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 retval = 0;
1848 else
1849 {
1850 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 if (tv.v_type == VAR_NUMBER)
1852 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001853 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 retval = 0;
1855 else
1856 {
1857 /* If the result is a string, check if there is a non-digit before
1858 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (!VIM_ISDIGIT(*s) && *s != '-')
1861 *cp = *s++;
1862 retval = atol((char *)s);
1863 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001867 if (use_sandbox)
1868 --sandbox;
1869 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870
1871 return retval;
1872}
1873#endif
1874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 * ":let" list all variable values
1877 * ":let var1 var2" list variable values
1878 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 * ":let var += expr" assignment command.
1880 * ":let var -= expr" assignment command.
1881 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 */
1884 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001885ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886{
1887 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001889 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 int var_count = 0;
1892 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001894 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001895 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
Bram Moolenaardb552d602006-03-23 22:59:57 +00001897 argend = skip_var_list(arg, &var_count, &semicolon);
1898 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001900 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1901 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001902 expr = skipwhite(argend);
1903 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1904 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001906 /*
1907 * ":let" without "=": list variables
1908 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 if (*arg == '[')
1910 EMSG(_(e_invarg));
1911 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001912 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001913 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001917 list_glob_vars(&first);
1918 list_buf_vars(&first);
1919 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001920#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001921 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001922#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001923 list_script_vars(&first);
1924 list_func_vars(&first);
1925 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 eap->nextcmd = check_nextcmd(arg);
1928 }
1929 else
1930 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001931 op[0] = '=';
1932 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001933 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001935 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1936 op[0] = *expr; /* +=, -= or .= */
1937 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001939 else
1940 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 if (eap->skip)
1943 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 if (eap->skip)
1946 {
1947 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 --emsg_skip;
1950 }
1951 else if (i != FAIL)
1952 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001954 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957 }
1958}
1959
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960/*
1961 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1962 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 * When "nextchars" is not NULL it points to a string with characters that
1964 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1965 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 * Returns OK or FAIL;
1967 */
1968 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001969ex_let_vars(
1970 char_u *arg_start,
1971 typval_T *tv,
1972 int copy, /* copy values from "tv", don't move */
1973 int semicolon, /* from skip_var_list() */
1974 int var_count, /* from skip_var_list() */
1975 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976{
1977 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001978 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001980 listitem_T *item;
1981 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982
1983 if (*arg != '[')
1984 {
1985 /*
1986 * ":let var = expr" or ":for var in list"
1987 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001988 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 return FAIL;
1990 return OK;
1991 }
1992
1993 /*
1994 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1995 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001996 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 {
1998 EMSG(_(e_listreq));
1999 return FAIL;
2000 }
2001
2002 i = list_len(l);
2003 if (semicolon == 0 && var_count < i)
2004 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002005 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 return FAIL;
2007 }
2008 if (var_count - semicolon > i)
2009 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002010 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 return FAIL;
2012 }
2013
2014 item = l->lv_first;
2015 while (*arg != ']')
2016 {
2017 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002018 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 item = item->li_next;
2020 if (arg == NULL)
2021 return FAIL;
2022
2023 arg = skipwhite(arg);
2024 if (*arg == ';')
2025 {
2026 /* Put the rest of the list (may be empty) in the var after ';'.
2027 * Create a new list for this. */
2028 l = list_alloc();
2029 if (l == NULL)
2030 return FAIL;
2031 while (item != NULL)
2032 {
2033 list_append_tv(l, &item->li_tv);
2034 item = item->li_next;
2035 }
2036
2037 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002038 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 ltv.vval.v_list = l;
2040 l->lv_refcount = 1;
2041
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002042 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2043 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 clear_tv(&ltv);
2045 if (arg == NULL)
2046 return FAIL;
2047 break;
2048 }
2049 else if (*arg != ',' && *arg != ']')
2050 {
2051 EMSG2(_(e_intern2), "ex_let_vars()");
2052 return FAIL;
2053 }
2054 }
2055
2056 return OK;
2057}
2058
2059/*
2060 * Skip over assignable variable "var" or list of variables "[var, var]".
2061 * Used for ":let varvar = expr" and ":for varvar in expr".
2062 * For "[var, var]" increment "*var_count" for each variable.
2063 * for "[var, var; var]" set "semicolon".
2064 * Return NULL for an error.
2065 */
2066 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002067skip_var_list(
2068 char_u *arg,
2069 int *var_count,
2070 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002071{
2072 char_u *p, *s;
2073
2074 if (*arg == '[')
2075 {
2076 /* "[var, var]": find the matching ']'. */
2077 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002078 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079 {
2080 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2081 s = skip_var_one(p);
2082 if (s == p)
2083 {
2084 EMSG2(_(e_invarg2), p);
2085 return NULL;
2086 }
2087 ++*var_count;
2088
2089 p = skipwhite(s);
2090 if (*p == ']')
2091 break;
2092 else if (*p == ';')
2093 {
2094 if (*semicolon == 1)
2095 {
2096 EMSG(_("Double ; in list of variables"));
2097 return NULL;
2098 }
2099 *semicolon = 1;
2100 }
2101 else if (*p != ',')
2102 {
2103 EMSG2(_(e_invarg2), p);
2104 return NULL;
2105 }
2106 }
2107 return p + 1;
2108 }
2109 else
2110 return skip_var_one(arg);
2111}
2112
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002113/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002114 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002115 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002116 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002117 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002118skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002119{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002120 if (*arg == '@' && arg[1] != NUL)
2121 return arg + 2;
2122 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2123 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124}
2125
Bram Moolenaara7043832005-01-21 11:56:39 +00002126/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002127 * List variables for hashtab "ht" with prefix "prefix".
2128 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002129 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002131list_hashtable_vars(
2132 hashtab_T *ht,
2133 char_u *prefix,
2134 int empty,
2135 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002136{
Bram Moolenaar33570922005-01-25 22:26:29 +00002137 hashitem_T *hi;
2138 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002139 int todo;
2140
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002141 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002142 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2143 {
2144 if (!HASHITEM_EMPTY(hi))
2145 {
2146 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002147 di = HI2DI(hi);
2148 if (empty || di->di_tv.v_type != VAR_STRING
2149 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002151 }
2152 }
2153}
2154
2155/*
2156 * List global variables.
2157 */
2158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002159list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002160{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002162}
2163
2164/*
2165 * List buffer variables.
2166 */
2167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002168list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002169{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002170 char_u numbuf[NUMBUFLEN];
2171
Bram Moolenaar429fa852013-04-15 12:27:36 +02002172 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002174
2175 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2177 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002178}
2179
2180/*
2181 * List window variables.
2182 */
2183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002184list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002185{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002186 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002188}
2189
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002190#ifdef FEAT_WINDOWS
2191/*
2192 * List tab page variables.
2193 */
2194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002196{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002197 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002199}
2200#endif
2201
Bram Moolenaara7043832005-01-21 11:56:39 +00002202/*
2203 * List Vim variables.
2204 */
2205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002206list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209}
2210
2211/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002212 * List script-local variables, if there is a script.
2213 */
2214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002215list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216{
2217 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2219 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220}
2221
2222/*
2223 * List function variables, if there is a function.
2224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002227{
2228 if (current_funccal != NULL)
2229 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002231}
2232
2233/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 * List variables in "arg".
2235 */
2236 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238{
2239 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 char_u *name_start;
2243 char_u *arg_subsc;
2244 char_u *tofree;
2245 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246
2247 while (!ends_excmd(*arg) && !got_int)
2248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002251 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2253 {
2254 emsg_severe = TRUE;
2255 EMSG(_(e_trailing));
2256 break;
2257 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 /* get_name_len() takes care of expanding curly braces */
2262 name_start = name = arg;
2263 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2264 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 /* This is mainly to keep test 49 working: when expanding
2267 * curly braces fails overrule the exception error message. */
2268 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 emsg_severe = TRUE;
2271 EMSG2(_(e_invarg2), arg);
2272 break;
2273 }
2274 error = TRUE;
2275 }
2276 else
2277 {
2278 if (tofree != NULL)
2279 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002280 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 else
2283 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 /* handle d.key, l[idx], f(expr) */
2285 arg_subsc = arg;
2286 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002287 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002290 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002294 case 'g': list_glob_vars(first); break;
2295 case 'b': list_buf_vars(first); break;
2296 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002297#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002298 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002299#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002300 case 'v': list_vim_vars(first); break;
2301 case 's': list_script_vars(first); break;
2302 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 default:
2304 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002305 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
2307 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 {
2309 char_u numbuf[NUMBUFLEN];
2310 char_u *tf;
2311 int c;
2312 char_u *s;
2313
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002314 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 c = *arg;
2316 *arg = NUL;
2317 list_one_var_a((char_u *)"",
2318 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002319 tv.v_type,
2320 s == NULL ? (char_u *)"" : s,
2321 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322 *arg = c;
2323 vim_free(tf);
2324 }
2325 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002326 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 }
2328 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329
2330 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002332
2333 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 }
2335
2336 return arg;
2337}
2338
2339/*
2340 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2341 * Returns a pointer to the char just after the var name.
2342 * Returns NULL if there is an error.
2343 */
2344 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002345ex_let_one(
2346 char_u *arg, /* points to variable name */
2347 typval_T *tv, /* value to assign to variable */
2348 int copy, /* copy value from "tv" */
2349 char_u *endchars, /* valid chars after variable name or NULL */
2350 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351{
2352 int c1;
2353 char_u *name;
2354 char_u *p;
2355 char_u *arg_end = NULL;
2356 int len;
2357 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359
2360 /*
2361 * ":let $VAR = expr": Set environment variable.
2362 */
2363 if (*arg == '$')
2364 {
2365 /* Find the end of the name. */
2366 ++arg;
2367 name = arg;
2368 len = get_env_len(&arg);
2369 if (len == 0)
2370 EMSG2(_(e_invarg2), name - 1);
2371 else
2372 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002373 if (op != NULL && (*op == '+' || *op == '-'))
2374 EMSG2(_(e_letwrong), op);
2375 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002376 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002378 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 {
2380 c1 = name[len];
2381 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002382 p = get_tv_string_chk(tv);
2383 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 {
2385 int mustfree = FALSE;
2386 char_u *s = vim_getenv(name, &mustfree);
2387
2388 if (s != NULL)
2389 {
2390 p = tofree = concat_str(s, p);
2391 if (mustfree)
2392 vim_free(s);
2393 }
2394 }
2395 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002396 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002398 if (STRICMP(name, "HOME") == 0)
2399 init_homedir();
2400 else if (didset_vim && STRICMP(name, "VIM") == 0)
2401 didset_vim = FALSE;
2402 else if (didset_vimruntime
2403 && STRICMP(name, "VIMRUNTIME") == 0)
2404 didset_vimruntime = FALSE;
2405 arg_end = arg;
2406 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002409 }
2410 }
2411 }
2412
2413 /*
2414 * ":let &option = expr": Set option value.
2415 * ":let &l:option = expr": Set local option value.
2416 * ":let &g:option = expr": Set global option value.
2417 */
2418 else if (*arg == '&')
2419 {
2420 /* Find the end of the name. */
2421 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002422 if (p == NULL || (endchars != NULL
2423 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 EMSG(_(e_letunexp));
2425 else
2426 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 long n;
2428 int opt_type;
2429 long numval;
2430 char_u *stringval = NULL;
2431 char_u *s;
2432
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 c1 = *p;
2434 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435
2436 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002437 s = get_tv_string_chk(tv); /* != NULL if number or string */
2438 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 {
2440 opt_type = get_option_value(arg, &numval,
2441 &stringval, opt_flags);
2442 if ((opt_type == 1 && *op == '.')
2443 || (opt_type == 0 && *op != '.'))
2444 EMSG2(_(e_letwrong), op);
2445 else
2446 {
2447 if (opt_type == 1) /* number */
2448 {
2449 if (*op == '+')
2450 n = numval + n;
2451 else
2452 n = numval - n;
2453 }
2454 else if (opt_type == 0 && stringval != NULL) /* string */
2455 {
2456 s = concat_str(stringval, s);
2457 vim_free(stringval);
2458 stringval = s;
2459 }
2460 }
2461 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002462 if (s != NULL)
2463 {
2464 set_option_value(arg, n, s, opt_flags);
2465 arg_end = p;
2466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 }
2470 }
2471
2472 /*
2473 * ":let @r = expr": Set register contents.
2474 */
2475 else if (*arg == '@')
2476 {
2477 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 if (op != NULL && (*op == '+' || *op == '-'))
2479 EMSG2(_(e_letwrong), op);
2480 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002481 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 EMSG(_(e_letunexp));
2483 else
2484 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002485 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 char_u *s;
2487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002488 p = get_tv_string_chk(tv);
2489 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002491 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 if (s != NULL)
2493 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002494 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 vim_free(s);
2496 }
2497 }
2498 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002499 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 arg_end = arg + 1;
2502 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002503 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 }
2505 }
2506
2507 /*
2508 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002511 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002513 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002515 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2519 EMSG(_(e_letunexp));
2520 else
2521 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002522 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 arg_end = p;
2524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 }
2528
2529 else
2530 EMSG2(_(e_invarg2), arg);
2531
2532 return arg_end;
2533}
2534
2535/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2537 */
2538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002539check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540{
2541 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2542 {
2543 EMSG2(_(e_readonlyvar), arg);
2544 return TRUE;
2545 }
2546 return FALSE;
2547}
2548
2549/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 * Get an lval: variable, Dict item or List item that can be assigned a value
2551 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2552 * "name.key", "name.key[expr]" etc.
2553 * Indexing only works if "name" is an existing List or Dictionary.
2554 * "name" points to the start of the name.
2555 * If "rettv" is not NULL it points to the value to be assigned.
2556 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2557 * wrong; must end in space or cmd separator.
2558 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002559 * flags:
2560 * GLV_QUIET: do not give error messages
2561 * GLV_NO_AUTOLOAD: do not use script autoloading
2562 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002564 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 */
2567 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002568get_lval(
2569 char_u *name,
2570 typval_T *rettv,
2571 lval_T *lp,
2572 int unlet,
2573 int skip,
2574 int flags, /* GLV_ values */
2575 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 char_u *expr_start, *expr_end;
2579 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 dictitem_T *v;
2581 typval_T var1;
2582 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002588 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592
2593 if (skip)
2594 {
2595 /* When skipping just find the end of the name. */
2596 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002597 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 }
2599
2600 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002601 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (expr_start != NULL)
2603 {
2604 /* Don't expand the name when we already know there is an error. */
2605 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2606 && *p != '[' && *p != '.')
2607 {
2608 EMSG(_(e_trailing));
2609 return NULL;
2610 }
2611
2612 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2613 if (lp->ll_exp_name == NULL)
2614 {
2615 /* Report an invalid expression in braces, unless the
2616 * expression evaluation has been cancelled due to an
2617 * aborting error, an interrupt, or an exception. */
2618 if (!aborting() && !quiet)
2619 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002620 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 EMSG2(_(e_invarg2), name);
2622 return NULL;
2623 }
2624 }
2625 lp->ll_name = lp->ll_exp_name;
2626 }
2627 else
2628 lp->ll_name = name;
2629
2630 /* Without [idx] or .key we are done. */
2631 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2632 return p;
2633
2634 cc = *p;
2635 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002636 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (v == NULL && !quiet)
2638 EMSG2(_(e_undefvar), lp->ll_name);
2639 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002640 if (v == NULL)
2641 return NULL;
2642
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 /*
2644 * Loop until no more [idx] or .key is following.
2645 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2650 && !(lp->ll_tv->v_type == VAR_DICT
2651 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_("E689: Can only index a List or Dictionary"));
2655 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_("E708: [:] must come last"));
2661 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 len = -1;
2665 if (*p == '.')
2666 {
2667 key = p + 1;
2668 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2669 ;
2670 if (len == 0)
2671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (!quiet)
2673 EMSG(_(e_emptykey));
2674 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
2676 p = key + len;
2677 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 else
2679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 if (*p == ':')
2683 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 else
2685 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 empty1 = FALSE;
2687 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002689 if (get_tv_string_chk(&var1) == NULL)
2690 {
2691 /* not a number or string */
2692 clear_tv(&var1);
2693 return NULL;
2694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 }
2696
2697 /* Optionally get the second index [ :expr]. */
2698 if (*p == ':')
2699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002703 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 if (!empty1)
2705 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (rettv != NULL && (rettv->v_type != VAR_LIST
2709 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (!quiet)
2712 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 if (!empty1)
2714 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 }
2717 p = skipwhite(p + 1);
2718 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 else
2721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 if (!empty1)
2726 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002729 if (get_tv_string_chk(&var2) == NULL)
2730 {
2731 /* not a number or string */
2732 if (!empty1)
2733 clear_tv(&var1);
2734 clear_tv(&var2);
2735 return NULL;
2736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002742
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (!quiet)
2746 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 if (!empty1)
2748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 }
2753
2754 /* Skip to past ']'. */
2755 ++p;
2756 }
2757
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 {
2760 if (len == -1)
2761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002763 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 if (*key == NUL)
2765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (!quiet)
2767 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
2771 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 lp->ll_list = NULL;
2773 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002774 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002775
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002776 /* When assigning to a scope dictionary check that a function and
2777 * variable name is valid (only variable name unless it is l: or
2778 * g: dictionary). Disallow overwriting a builtin function. */
2779 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002780 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002781 int prevval;
2782 int wrong;
2783
2784 if (len != -1)
2785 {
2786 prevval = key[len];
2787 key[len] = NUL;
2788 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002789 else
2790 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002791 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2792 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002794 || !valid_varname(key);
2795 if (len != -1)
2796 key[len] = prevval;
2797 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002798 return NULL;
2799 }
2800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 /* Can't add "v:" variable. */
2804 if (lp->ll_dict == &vimvardict)
2805 {
2806 EMSG2(_(e_illvar), name);
2807 return NULL;
2808 }
2809
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002810 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 if (len == -1)
2816 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 }
2819 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 if (len == -1)
2824 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 p = NULL;
2827 break;
2828 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002829 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002830 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002831 return NULL;
2832
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 if (len == -1)
2834 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 }
2837 else
2838 {
2839 /*
2840 * Get the number and item for the only or first index of the List.
2841 */
2842 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 else
2845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002846 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 clear_tv(&var1);
2848 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002849 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 lp->ll_list = lp->ll_tv->vval.v_list;
2851 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2852 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002854 if (lp->ll_n1 < 0)
2855 {
2856 lp->ll_n1 = 0;
2857 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2858 }
2859 }
2860 if (lp->ll_li == NULL)
2861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 if (!quiet)
2865 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 }
2868
2869 /*
2870 * May need to find the item or absolute index for the second
2871 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 * When no index given: "lp->ll_empty2" is TRUE.
2873 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002877 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002883 {
2884 if (!quiet)
2885 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002887 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 }
2890
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2892 if (lp->ll_n1 < 0)
2893 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2894 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002895 {
2896 if (!quiet)
2897 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002899 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002900 }
2901
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002904 }
2905
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return p;
2907}
2908
2909/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 */
2912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002913clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914{
2915 vim_free(lp->ll_exp_name);
2916 vim_free(lp->ll_newkey);
2917}
2918
2919/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002920 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 */
2924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002925set_var_lval(
2926 lval_T *lp,
2927 char_u *endp,
2928 typval_T *rettv,
2929 int copy,
2930 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931{
2932 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002933 listitem_T *ri;
2934 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935
2936 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 cc = *endp;
2941 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 if (op != NULL && *op != '=')
2943 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002944 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002946 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002947 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002948 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002949 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002951 if ((di == NULL
2952 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2953 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2954 FALSE)))
2955 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956 set_var(lp->ll_name, &tv, FALSE);
2957 clear_tv(&tv);
2958 }
2959 }
2960 else
2961 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002963 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002965 else if (tv_check_lock(lp->ll_newkey == NULL
2966 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002967 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002968 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 else if (lp->ll_range)
2970 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002971 listitem_T *ll_li = lp->ll_li;
2972 int ll_n1 = lp->ll_n1;
2973
2974 /*
2975 * Check whether any of the list items is locked
2976 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002977 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002978 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002979 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002980 return;
2981 ri = ri->li_next;
2982 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2983 break;
2984 ll_li = ll_li->li_next;
2985 ++ll_n1;
2986 }
2987
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 /*
2989 * Assign the List values to the list items.
2990 */
2991 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002992 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002993 if (op != NULL && *op != '=')
2994 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2995 else
2996 {
2997 clear_tv(&lp->ll_li->li_tv);
2998 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2999 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 ri = ri->li_next;
3001 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3002 break;
3003 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003004 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003006 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003007 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 ri = NULL;
3009 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003010 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003011 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 lp->ll_li = lp->ll_li->li_next;
3013 ++lp->ll_n1;
3014 }
3015 if (ri != NULL)
3016 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003017 else if (lp->ll_empty2
3018 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 : lp->ll_n1 != lp->ll_n2)
3020 EMSG(_("E711: List value has not enough items"));
3021 }
3022 else
3023 {
3024 /*
3025 * Assign to a List or Dictionary item.
3026 */
3027 if (lp->ll_newkey != NULL)
3028 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 if (op != NULL && *op != '=')
3030 {
3031 EMSG2(_(e_letwrong), op);
3032 return;
3033 }
3034
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003036 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 if (di == NULL)
3038 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003039 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3040 {
3041 vim_free(di);
3042 return;
3043 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003045 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003046 else if (op != NULL && *op != '=')
3047 {
3048 tv_op(lp->ll_tv, rettv, op);
3049 return;
3050 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003051 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003053
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003054 /*
3055 * Assign the value to the variable or list item.
3056 */
3057 if (copy)
3058 copy_tv(rettv, lp->ll_tv);
3059 else
3060 {
3061 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003062 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003063 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003064 }
3065 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066}
3067
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003068/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3070 * Returns OK or FAIL.
3071 */
3072 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003073tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074{
3075 long n;
3076 char_u numbuf[NUMBUFLEN];
3077 char_u *s;
3078
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003079 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3080 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3081 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082 {
3083 switch (tv1->v_type)
3084 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003085 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086 case VAR_DICT:
3087 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003088 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003089 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003090 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 break;
3092
3093 case VAR_LIST:
3094 if (*op != '+' || tv2->v_type != VAR_LIST)
3095 break;
3096 /* List += List */
3097 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3098 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3099 return OK;
3100
3101 case VAR_NUMBER:
3102 case VAR_STRING:
3103 if (tv2->v_type == VAR_LIST)
3104 break;
3105 if (*op == '+' || *op == '-')
3106 {
3107 /* nr += nr or nr -= nr*/
3108 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003109#ifdef FEAT_FLOAT
3110 if (tv2->v_type == VAR_FLOAT)
3111 {
3112 float_T f = n;
3113
3114 if (*op == '+')
3115 f += tv2->vval.v_float;
3116 else
3117 f -= tv2->vval.v_float;
3118 clear_tv(tv1);
3119 tv1->v_type = VAR_FLOAT;
3120 tv1->vval.v_float = f;
3121 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003123#endif
3124 {
3125 if (*op == '+')
3126 n += get_tv_number(tv2);
3127 else
3128 n -= get_tv_number(tv2);
3129 clear_tv(tv1);
3130 tv1->v_type = VAR_NUMBER;
3131 tv1->vval.v_number = n;
3132 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 }
3134 else
3135 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136 if (tv2->v_type == VAR_FLOAT)
3137 break;
3138
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003139 /* str .= str */
3140 s = get_tv_string(tv1);
3141 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3142 clear_tv(tv1);
3143 tv1->v_type = VAR_STRING;
3144 tv1->vval.v_string = s;
3145 }
3146 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003147
3148#ifdef FEAT_FLOAT
3149 case VAR_FLOAT:
3150 {
3151 float_T f;
3152
3153 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3154 && tv2->v_type != VAR_NUMBER
3155 && tv2->v_type != VAR_STRING))
3156 break;
3157 if (tv2->v_type == VAR_FLOAT)
3158 f = tv2->vval.v_float;
3159 else
3160 f = get_tv_number(tv2);
3161 if (*op == '+')
3162 tv1->vval.v_float += f;
3163 else
3164 tv1->vval.v_float -= f;
3165 }
3166 return OK;
3167#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003168 }
3169 }
3170
3171 EMSG2(_(e_letwrong), op);
3172 return FAIL;
3173}
3174
3175/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176 * Add a watcher to a list.
3177 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003179list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180{
3181 lw->lw_next = l->lv_watch;
3182 l->lv_watch = lw;
3183}
3184
3185/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003186 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187 * No warning when it isn't found...
3188 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003190list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191{
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193
3194 lwp = &l->lv_watch;
3195 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3196 {
3197 if (lw == lwrem)
3198 {
3199 *lwp = lw->lw_next;
3200 break;
3201 }
3202 lwp = &lw->lw_next;
3203 }
3204}
3205
3206/*
3207 * Just before removing an item from a list: advance watchers to the next
3208 * item.
3209 */
3210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003211list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212{
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214
3215 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3216 if (lw->lw_item == item)
3217 lw->lw_item = item->li_next;
3218}
3219
3220/*
3221 * Evaluate the expression used in a ":for var in expr" command.
3222 * "arg" points to "var".
3223 * Set "*errp" to TRUE for an error, FALSE otherwise;
3224 * Return a pointer that holds the info. Null when there is an error.
3225 */
3226 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003227eval_for_line(
3228 char_u *arg,
3229 int *errp,
3230 char_u **nextcmdp,
3231 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232{
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 typval_T tv;
3236 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237
3238 *errp = TRUE; /* default: there is an error */
3239
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 if (fi == NULL)
3242 return NULL;
3243
3244 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3245 if (expr == NULL)
3246 return fi;
3247
3248 expr = skipwhite(expr);
3249 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3250 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003251 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252 return fi;
3253 }
3254
3255 if (skip)
3256 ++emsg_skip;
3257 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3258 {
3259 *errp = FALSE;
3260 if (!skip)
3261 {
3262 l = tv.vval.v_list;
3263 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 clear_tv(&tv);
3267 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268 else
3269 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003270 /* No need to increment the refcount, it's already set for the
3271 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 fi->fi_list = l;
3273 list_add_watch(l, &fi->fi_lw);
3274 fi->fi_lw.lw_item = l->lv_first;
3275 }
3276 }
3277 }
3278 if (skip)
3279 --emsg_skip;
3280
3281 return fi;
3282}
3283
3284/*
3285 * Use the first item in a ":for" list. Advance to the next.
3286 * Assign the values to the variable (list). "arg" points to the first one.
3287 * Return TRUE when a valid item was found, FALSE when at end of list or
3288 * something wrong.
3289 */
3290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003291next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003293 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296
3297 item = fi->fi_lw.lw_item;
3298 if (item == NULL)
3299 result = FALSE;
3300 else
3301 {
3302 fi->fi_lw.lw_item = item->li_next;
3303 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3304 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3305 }
3306 return result;
3307}
3308
3309/*
3310 * Free the structure used to store info used by ":for".
3311 */
3312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003313free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314{
Bram Moolenaar33570922005-01-25 22:26:29 +00003315 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003316
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003317 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003318 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003320 list_unref(fi->fi_list);
3321 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322 vim_free(fi);
3323}
3324
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3326
3327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003328set_context_for_expression(
3329 expand_T *xp,
3330 char_u *arg,
3331 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
3333 int got_eq = FALSE;
3334 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003335 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 if (cmdidx == CMD_let)
3338 {
3339 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003340 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 {
3342 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003343 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 {
3345 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003346 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003347 if (vim_iswhite(*p))
3348 break;
3349 }
3350 return;
3351 }
3352 }
3353 else
3354 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3355 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 while ((xp->xp_pattern = vim_strpbrk(arg,
3357 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3358 {
3359 c = *xp->xp_pattern;
3360 if (c == '&')
3361 {
3362 c = xp->xp_pattern[1];
3363 if (c == '&')
3364 {
3365 ++xp->xp_pattern;
3366 xp->xp_context = cmdidx != CMD_let || got_eq
3367 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3368 }
3369 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003372 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3373 xp->xp_pattern += 2;
3374
3375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377 else if (c == '$')
3378 {
3379 /* environment variable */
3380 xp->xp_context = EXPAND_ENV_VARS;
3381 }
3382 else if (c == '=')
3383 {
3384 got_eq = TRUE;
3385 xp->xp_context = EXPAND_EXPRESSION;
3386 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003387 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 && xp->xp_context == EXPAND_FUNCTIONS
3389 && vim_strchr(xp->xp_pattern, '(') == NULL)
3390 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003391 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 break;
3393 }
3394 else if (cmdidx != CMD_let || got_eq)
3395 {
3396 if (c == '"') /* string */
3397 {
3398 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3399 if (c == '\\' && xp->xp_pattern[1] != NUL)
3400 ++xp->xp_pattern;
3401 xp->xp_context = EXPAND_NOTHING;
3402 }
3403 else if (c == '\'') /* literal string */
3404 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003405 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3407 /* skip */ ;
3408 xp->xp_context = EXPAND_NOTHING;
3409 }
3410 else if (c == '|')
3411 {
3412 if (xp->xp_pattern[1] == '|')
3413 {
3414 ++xp->xp_pattern;
3415 xp->xp_context = EXPAND_EXPRESSION;
3416 }
3417 else
3418 xp->xp_context = EXPAND_COMMANDS;
3419 }
3420 else
3421 xp->xp_context = EXPAND_EXPRESSION;
3422 }
3423 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003424 /* Doesn't look like something valid, expand as an expression
3425 * anyway. */
3426 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 arg = xp->xp_pattern;
3428 if (*arg != NUL)
3429 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3430 /* skip */ ;
3431 }
3432 xp->xp_pattern = arg;
3433}
3434
3435#endif /* FEAT_CMDL_COMPL */
3436
3437/*
3438 * ":1,25call func(arg1, arg2)" function call.
3439 */
3440 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003441ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 char_u *arg = eap->arg;
3444 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003446 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003448 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 linenr_T lnum;
3450 int doesrange;
3451 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003452 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003454 if (eap->skip)
3455 {
3456 /* trans_function_name() doesn't work well when skipping, use eval0()
3457 * instead to skip to any following command, e.g. for:
3458 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003459 ++emsg_skip;
3460 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3461 clear_tv(&rettv);
3462 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003463 return;
3464 }
3465
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003466 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003467 if (fudi.fd_newkey != NULL)
3468 {
3469 /* Still need to give an error message for missing key. */
3470 EMSG2(_(e_dictkey), fudi.fd_newkey);
3471 vim_free(fudi.fd_newkey);
3472 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003473 if (tofree == NULL)
3474 return;
3475
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003476 /* Increase refcount on dictionary, it could get deleted when evaluating
3477 * the arguments. */
3478 if (fudi.fd_dict != NULL)
3479 ++fudi.fd_dict->dv_refcount;
3480
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003481 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003482 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003483 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
Bram Moolenaar532c7802005-01-27 14:44:31 +00003485 /* Skip white space to allow ":call func ()". Not good, but required for
3486 * backward compatibility. */
3487 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003488 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490 if (*startarg != '(')
3491 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003492 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 goto end;
3494 }
3495
3496 /*
3497 * When skipping, evaluate the function once, to find the end of the
3498 * arguments.
3499 * When the function takes a range, this is discovered after the first
3500 * call, and the loop is broken.
3501 */
3502 if (eap->skip)
3503 {
3504 ++emsg_skip;
3505 lnum = eap->line2; /* do it once, also with an invalid range */
3506 }
3507 else
3508 lnum = eap->line1;
3509 for ( ; lnum <= eap->line2; ++lnum)
3510 {
3511 if (!eap->skip && eap->addr_count > 0)
3512 {
3513 curwin->w_cursor.lnum = lnum;
3514 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003515#ifdef FEAT_VIRTUALEDIT
3516 curwin->w_cursor.coladd = 0;
3517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003520 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003521 eap->line1, eap->line2, &doesrange,
3522 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524 failed = TRUE;
3525 break;
3526 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003527
3528 /* Handle a function returning a Funcref, Dictionary or List. */
3529 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3530 {
3531 failed = TRUE;
3532 break;
3533 }
3534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003535 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 if (doesrange || eap->skip)
3537 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003540 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003541 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003542 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (aborting())
3544 break;
3545 }
3546 if (eap->skip)
3547 --emsg_skip;
3548
3549 if (!failed)
3550 {
3551 /* Check for trailing illegal characters and a following command. */
3552 if (!ends_excmd(*arg))
3553 {
3554 emsg_severe = TRUE;
3555 EMSG(_(e_trailing));
3556 }
3557 else
3558 eap->nextcmd = check_nextcmd(arg);
3559 }
3560
3561end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003562 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003563 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564}
3565
3566/*
3567 * ":unlet[!] var1 ... " command.
3568 */
3569 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003570ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003572 ex_unletlock(eap, eap->arg, 0);
3573}
3574
3575/*
3576 * ":lockvar" and ":unlockvar" commands
3577 */
3578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003579ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582 int deep = 2;
3583
3584 if (eap->forceit)
3585 deep = -1;
3586 else if (vim_isdigit(*arg))
3587 {
3588 deep = getdigits(&arg);
3589 arg = skipwhite(arg);
3590 }
3591
3592 ex_unletlock(eap, arg, deep);
3593}
3594
3595/*
3596 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3597 */
3598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003599ex_unletlock(
3600 exarg_T *eap,
3601 char_u *argstart,
3602 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003603{
3604 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 do
3610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003612 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003613 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 if (lv.ll_name == NULL)
3615 error = TRUE; /* error but continue parsing */
3616 if (name_end == NULL || (!vim_iswhite(*name_end)
3617 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 if (name_end != NULL)
3620 {
3621 emsg_severe = TRUE;
3622 EMSG(_(e_trailing));
3623 }
3624 if (!(eap->skip || error))
3625 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 break;
3627 }
3628
3629 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003630 {
3631 if (eap->cmdidx == CMD_unlet)
3632 {
3633 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3634 error = TRUE;
3635 }
3636 else
3637 {
3638 if (do_lock_var(&lv, name_end, deep,
3639 eap->cmdidx == CMD_lockvar) == FAIL)
3640 error = TRUE;
3641 }
3642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 if (!eap->skip)
3645 clear_lval(&lv);
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 arg = skipwhite(name_end);
3648 } while (!ends_excmd(*arg));
3649
3650 eap->nextcmd = check_nextcmd(arg);
3651}
3652
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003654do_unlet_var(
3655 lval_T *lp,
3656 char_u *name_end,
3657 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 int ret = OK;
3660 int cc;
3661
3662 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 cc = *name_end;
3665 *name_end = NUL;
3666
3667 /* Normal name or expanded name. */
3668 if (check_changedtick(lp->ll_name))
3669 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003673 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003674 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003675 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003676 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003677 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 else if (lp->ll_range)
3680 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003681 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003682 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003683 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003684
3685 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3686 {
3687 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003688 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689 return FAIL;
3690 ll_li = li;
3691 ++ll_n1;
3692 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693
3694 /* Delete a range of List items. */
3695 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3696 {
3697 li = lp->ll_li->li_next;
3698 listitem_remove(lp->ll_list, lp->ll_li);
3699 lp->ll_li = li;
3700 ++lp->ll_n1;
3701 }
3702 }
3703 else
3704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003705 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 /* unlet a List item. */
3707 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003710 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 }
3712
3713 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714}
3715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716/*
3717 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003718 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
3720 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003721do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 hashtab_T *ht;
3724 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003725 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003726 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003727 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
Bram Moolenaar33570922005-01-25 22:26:29 +00003729 ht = find_var_ht(name, &varname);
3730 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003732 if (ht == &globvarht)
3733 d = &globvardict;
3734 else if (current_funccal != NULL
3735 && ht == &current_funccal->l_vars.dv_hashtab)
3736 d = &current_funccal->l_vars;
3737 else if (ht == &compat_hashtab)
3738 d = &vimvardict;
3739 else
3740 {
3741 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3742 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3743 }
3744 if (d == NULL)
3745 {
3746 EMSG2(_(e_intern2), "do_unlet()");
3747 return FAIL;
3748 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003749 hi = hash_find(ht, varname);
3750 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003751 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003752 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003753 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003754 || var_check_ro(di->di_flags, name, FALSE)
3755 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003756 return FAIL;
3757
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003758 delete_var(ht, hi);
3759 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003762 if (forceit)
3763 return OK;
3764 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 return FAIL;
3766}
3767
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003768/*
3769 * Lock or unlock variable indicated by "lp".
3770 * "deep" is the levels to go (-1 for unlimited);
3771 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3772 */
3773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003774do_lock_var(
3775 lval_T *lp,
3776 char_u *name_end,
3777 int deep,
3778 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003779{
3780 int ret = OK;
3781 int cc;
3782 dictitem_T *di;
3783
3784 if (deep == 0) /* nothing to do */
3785 return OK;
3786
3787 if (lp->ll_tv == NULL)
3788 {
3789 cc = *name_end;
3790 *name_end = NUL;
3791
3792 /* Normal name or expanded name. */
3793 if (check_changedtick(lp->ll_name))
3794 ret = FAIL;
3795 else
3796 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003797 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003798 if (di == NULL)
3799 ret = FAIL;
3800 else
3801 {
3802 if (lock)
3803 di->di_flags |= DI_FLAGS_LOCK;
3804 else
3805 di->di_flags &= ~DI_FLAGS_LOCK;
3806 item_lock(&di->di_tv, deep, lock);
3807 }
3808 }
3809 *name_end = cc;
3810 }
3811 else if (lp->ll_range)
3812 {
3813 listitem_T *li = lp->ll_li;
3814
3815 /* (un)lock a range of List items. */
3816 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3817 {
3818 item_lock(&li->li_tv, deep, lock);
3819 li = li->li_next;
3820 ++lp->ll_n1;
3821 }
3822 }
3823 else if (lp->ll_list != NULL)
3824 /* (un)lock a List item. */
3825 item_lock(&lp->ll_li->li_tv, deep, lock);
3826 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003827 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003828 item_lock(&lp->ll_di->di_tv, deep, lock);
3829
3830 return ret;
3831}
3832
3833/*
3834 * Lock or unlock an item. "deep" is nr of levels to go.
3835 */
3836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003837item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003838{
3839 static int recurse = 0;
3840 list_T *l;
3841 listitem_T *li;
3842 dict_T *d;
3843 hashitem_T *hi;
3844 int todo;
3845
3846 if (recurse >= DICT_MAXNEST)
3847 {
3848 EMSG(_("E743: variable nested too deep for (un)lock"));
3849 return;
3850 }
3851 if (deep == 0)
3852 return;
3853 ++recurse;
3854
3855 /* lock/unlock the item itself */
3856 if (lock)
3857 tv->v_lock |= VAR_LOCKED;
3858 else
3859 tv->v_lock &= ~VAR_LOCKED;
3860
3861 switch (tv->v_type)
3862 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003863 case VAR_UNKNOWN:
3864 case VAR_NUMBER:
3865 case VAR_STRING:
3866 case VAR_FUNC:
3867 case VAR_FLOAT:
3868 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003869 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003870 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003871 break;
3872
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003873 case VAR_LIST:
3874 if ((l = tv->vval.v_list) != NULL)
3875 {
3876 if (lock)
3877 l->lv_lock |= VAR_LOCKED;
3878 else
3879 l->lv_lock &= ~VAR_LOCKED;
3880 if (deep < 0 || deep > 1)
3881 /* recursive: lock/unlock the items the List contains */
3882 for (li = l->lv_first; li != NULL; li = li->li_next)
3883 item_lock(&li->li_tv, deep - 1, lock);
3884 }
3885 break;
3886 case VAR_DICT:
3887 if ((d = tv->vval.v_dict) != NULL)
3888 {
3889 if (lock)
3890 d->dv_lock |= VAR_LOCKED;
3891 else
3892 d->dv_lock &= ~VAR_LOCKED;
3893 if (deep < 0 || deep > 1)
3894 {
3895 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003896 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003897 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3898 {
3899 if (!HASHITEM_EMPTY(hi))
3900 {
3901 --todo;
3902 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3903 }
3904 }
3905 }
3906 }
3907 }
3908 --recurse;
3909}
3910
Bram Moolenaara40058a2005-07-11 22:42:07 +00003911/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003912 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3913 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003914 */
3915 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003916tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003917{
3918 return (tv->v_lock & VAR_LOCKED)
3919 || (tv->v_type == VAR_LIST
3920 && tv->vval.v_list != NULL
3921 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3922 || (tv->v_type == VAR_DICT
3923 && tv->vval.v_dict != NULL
3924 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3925}
3926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3928/*
3929 * Delete all "menutrans_" variables.
3930 */
3931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003932del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003935 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003938 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 {
3941 if (!HASHITEM_EMPTY(hi))
3942 {
3943 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3945 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003946 }
3947 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949}
3950#endif
3951
3952#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3953
3954/*
3955 * Local string buffer for the next two functions to store a variable name
3956 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3957 * get_user_var_name().
3958 */
3959
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003960static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
3962static char_u *varnamebuf = NULL;
3963static int varnamebuflen = 0;
3964
3965/*
3966 * Function to concatenate a prefix and a variable name.
3967 */
3968 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003969cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
3971 int len;
3972
3973 len = (int)STRLEN(name) + 3;
3974 if (len > varnamebuflen)
3975 {
3976 vim_free(varnamebuf);
3977 len += 10; /* some additional space */
3978 varnamebuf = alloc(len);
3979 if (varnamebuf == NULL)
3980 {
3981 varnamebuflen = 0;
3982 return NULL;
3983 }
3984 varnamebuflen = len;
3985 }
3986 *varnamebuf = prefix;
3987 varnamebuf[1] = ':';
3988 STRCPY(varnamebuf + 2, name);
3989 return varnamebuf;
3990}
3991
3992/*
3993 * Function given to ExpandGeneric() to obtain the list of user defined
3994 * (global/buffer/window/built-in) variable names.
3995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003997get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003999 static long_u gdone;
4000 static long_u bdone;
4001 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004002#ifdef FEAT_WINDOWS
4003 static long_u tdone;
4004#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004005 static int vidx;
4006 static hashitem_T *hi;
4007 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004010 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004011 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004012#ifdef FEAT_WINDOWS
4013 tdone = 0;
4014#endif
4015 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004016
4017 /* Global variables */
4018 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004020 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004021 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004022 else
4023 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 while (HASHITEM_EMPTY(hi))
4025 ++hi;
4026 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4027 return cat_prefix_varname('g', hi->hi_key);
4028 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004030
4031 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004032 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004033 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004035 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004036 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004037 else
4038 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004039 while (HASHITEM_EMPTY(hi))
4040 ++hi;
4041 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004045 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 return (char_u *)"b:changedtick";
4047 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004048
4049 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004050 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004053 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004054 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004055 else
4056 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004057 while (HASHITEM_EMPTY(hi))
4058 ++hi;
4059 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004061
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004062#ifdef FEAT_WINDOWS
4063 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004064 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004065 if (tdone < ht->ht_used)
4066 {
4067 if (tdone++ == 0)
4068 hi = ht->ht_array;
4069 else
4070 ++hi;
4071 while (HASHITEM_EMPTY(hi))
4072 ++hi;
4073 return cat_prefix_varname('t', hi->hi_key);
4074 }
4075#endif
4076
Bram Moolenaar33570922005-01-25 22:26:29 +00004077 /* v: variables */
4078 if (vidx < VV_LEN)
4079 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 vim_free(varnamebuf);
4082 varnamebuf = NULL;
4083 varnamebuflen = 0;
4084 return NULL;
4085}
4086
4087#endif /* FEAT_CMDL_COMPL */
4088
4089/*
4090 * types for expressions.
4091 */
4092typedef enum
4093{
4094 TYPE_UNKNOWN = 0
4095 , TYPE_EQUAL /* == */
4096 , TYPE_NEQUAL /* != */
4097 , TYPE_GREATER /* > */
4098 , TYPE_GEQUAL /* >= */
4099 , TYPE_SMALLER /* < */
4100 , TYPE_SEQUAL /* <= */
4101 , TYPE_MATCH /* =~ */
4102 , TYPE_NOMATCH /* !~ */
4103} exptype_T;
4104
4105/*
4106 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4109 */
4110
4111/*
4112 * Handle zero level expression.
4113 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004115 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * Return OK or FAIL.
4117 */
4118 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004119eval0(
4120 char_u *arg,
4121 typval_T *rettv,
4122 char_u **nextcmd,
4123 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
4125 int ret;
4126 char_u *p;
4127
4128 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 if (ret == FAIL || !ends_excmd(*p))
4131 {
4132 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 /*
4135 * Report the invalid expression unless the expression evaluation has
4136 * been cancelled due to an aborting error, an interrupt, or an
4137 * exception.
4138 */
4139 if (!aborting())
4140 EMSG2(_(e_invexpr2), arg);
4141 ret = FAIL;
4142 }
4143 if (nextcmd != NULL)
4144 *nextcmd = check_nextcmd(p);
4145
4146 return ret;
4147}
4148
4149/*
4150 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004151 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 *
4153 * "arg" must point to the first non-white of the expression.
4154 * "arg" is advanced to the next non-white after the recognized expression.
4155 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004156 * Note: "rettv.v_lock" is not set.
4157 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 * Return OK or FAIL.
4159 */
4160 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004161eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162{
4163 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004164 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 /*
4167 * Get the first variable.
4168 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 return FAIL;
4171
4172 if ((*arg)[0] == '?')
4173 {
4174 result = FALSE;
4175 if (evaluate)
4176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 int error = FALSE;
4178
4179 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (error)
4183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185
4186 /*
4187 * Get the second variable.
4188 */
4189 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
4192
4193 /*
4194 * Check for the ":".
4195 */
4196 if ((*arg)[0] != ':')
4197 {
4198 EMSG(_("E109: Missing ':' after '?'"));
4199 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return FAIL;
4202 }
4203
4204 /*
4205 * Get the third variable.
4206 */
4207 *arg = skipwhite(*arg + 1);
4208 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4209 {
4210 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213 }
4214 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217
4218 return OK;
4219}
4220
4221/*
4222 * Handle first level expression:
4223 * expr2 || expr2 || expr2 logical OR
4224 *
4225 * "arg" must point to the first non-white of the expression.
4226 * "arg" is advanced to the next non-white after the recognized expression.
4227 *
4228 * Return OK or FAIL.
4229 */
4230 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004231eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232{
Bram Moolenaar33570922005-01-25 22:26:29 +00004233 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 long result;
4235 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 /*
4239 * Get the first variable.
4240 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 return FAIL;
4243
4244 /*
4245 * Repeat until there is no following "||".
4246 */
4247 first = TRUE;
4248 result = FALSE;
4249 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4250 {
4251 if (evaluate && first)
4252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 if (error)
4257 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 first = FALSE;
4259 }
4260
4261 /*
4262 * Get the second variable.
4263 */
4264 *arg = skipwhite(*arg + 2);
4265 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4266 return FAIL;
4267
4268 /*
4269 * Compute the result.
4270 */
4271 if (evaluate && !result)
4272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004273 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 if (error)
4277 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 if (evaluate)
4280 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 rettv->v_type = VAR_NUMBER;
4282 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 }
4285
4286 return OK;
4287}
4288
4289/*
4290 * Handle second level expression:
4291 * expr3 && expr3 && expr3 logical AND
4292 *
4293 * "arg" must point to the first non-white of the expression.
4294 * "arg" is advanced to the next non-white after the recognized expression.
4295 *
4296 * Return OK or FAIL.
4297 */
4298 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004299eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300{
Bram Moolenaar33570922005-01-25 22:26:29 +00004301 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 long result;
4303 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 /*
4307 * Get the first variable.
4308 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 return FAIL;
4311
4312 /*
4313 * Repeat until there is no following "&&".
4314 */
4315 first = TRUE;
4316 result = TRUE;
4317 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4318 {
4319 if (evaluate && first)
4320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 if (error)
4325 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 first = FALSE;
4327 }
4328
4329 /*
4330 * Get the second variable.
4331 */
4332 *arg = skipwhite(*arg + 2);
4333 if (eval4(arg, &var2, evaluate && result) == FAIL)
4334 return FAIL;
4335
4336 /*
4337 * Compute the result.
4338 */
4339 if (evaluate && result)
4340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004344 if (error)
4345 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 if (evaluate)
4348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349 rettv->v_type = VAR_NUMBER;
4350 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 }
4353
4354 return OK;
4355}
4356
4357/*
4358 * Handle third level expression:
4359 * var1 == var2
4360 * var1 =~ var2
4361 * var1 != var2
4362 * var1 !~ var2
4363 * var1 > var2
4364 * var1 >= var2
4365 * var1 < var2
4366 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004367 * var1 is var2
4368 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 *
4370 * "arg" must point to the first non-white of the expression.
4371 * "arg" is advanced to the next non-white after the recognized expression.
4372 *
4373 * Return OK or FAIL.
4374 */
4375 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004376eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377{
Bram Moolenaar33570922005-01-25 22:26:29 +00004378 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 char_u *p;
4380 int i;
4381 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004382 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 int len = 2;
4384 long n1, n2;
4385 char_u *s1, *s2;
4386 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4387 regmatch_T regmatch;
4388 int ic;
4389 char_u *save_cpo;
4390
4391 /*
4392 * Get the first variable.
4393 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 return FAIL;
4396
4397 p = *arg;
4398 switch (p[0])
4399 {
4400 case '=': if (p[1] == '=')
4401 type = TYPE_EQUAL;
4402 else if (p[1] == '~')
4403 type = TYPE_MATCH;
4404 break;
4405 case '!': if (p[1] == '=')
4406 type = TYPE_NEQUAL;
4407 else if (p[1] == '~')
4408 type = TYPE_NOMATCH;
4409 break;
4410 case '>': if (p[1] != '=')
4411 {
4412 type = TYPE_GREATER;
4413 len = 1;
4414 }
4415 else
4416 type = TYPE_GEQUAL;
4417 break;
4418 case '<': if (p[1] != '=')
4419 {
4420 type = TYPE_SMALLER;
4421 len = 1;
4422 }
4423 else
4424 type = TYPE_SEQUAL;
4425 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004426 case 'i': if (p[1] == 's')
4427 {
4428 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4429 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004430 i = p[len];
4431 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004432 {
4433 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4434 type_is = TRUE;
4435 }
4436 }
4437 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439
4440 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004441 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 */
4443 if (type != TYPE_UNKNOWN)
4444 {
4445 /* extra question mark appended: ignore case */
4446 if (p[len] == '?')
4447 {
4448 ic = TRUE;
4449 ++len;
4450 }
4451 /* extra '#' appended: match case */
4452 else if (p[len] == '#')
4453 {
4454 ic = FALSE;
4455 ++len;
4456 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004457 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 else
4459 ic = p_ic;
4460
4461 /*
4462 * Get the second variable.
4463 */
4464 *arg = skipwhite(p + len);
4465 if (eval5(arg, &var2, evaluate) == FAIL)
4466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 return FAIL;
4469 }
4470
4471 if (evaluate)
4472 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004473 if (type_is && rettv->v_type != var2.v_type)
4474 {
4475 /* For "is" a different type always means FALSE, for "notis"
4476 * it means TRUE. */
4477 n1 = (type == TYPE_NEQUAL);
4478 }
4479 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4480 {
4481 if (type_is)
4482 {
4483 n1 = (rettv->v_type == var2.v_type
4484 && rettv->vval.v_list == var2.vval.v_list);
4485 if (type == TYPE_NEQUAL)
4486 n1 = !n1;
4487 }
4488 else if (rettv->v_type != var2.v_type
4489 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4490 {
4491 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004492 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004493 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004494 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 clear_tv(rettv);
4496 clear_tv(&var2);
4497 return FAIL;
4498 }
4499 else
4500 {
4501 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004502 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4503 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004504 if (type == TYPE_NEQUAL)
4505 n1 = !n1;
4506 }
4507 }
4508
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004509 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4510 {
4511 if (type_is)
4512 {
4513 n1 = (rettv->v_type == var2.v_type
4514 && rettv->vval.v_dict == var2.vval.v_dict);
4515 if (type == TYPE_NEQUAL)
4516 n1 = !n1;
4517 }
4518 else if (rettv->v_type != var2.v_type
4519 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4520 {
4521 if (rettv->v_type != var2.v_type)
4522 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4523 else
4524 EMSG(_("E736: Invalid operation for Dictionary"));
4525 clear_tv(rettv);
4526 clear_tv(&var2);
4527 return FAIL;
4528 }
4529 else
4530 {
4531 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004532 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4533 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004534 if (type == TYPE_NEQUAL)
4535 n1 = !n1;
4536 }
4537 }
4538
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004539 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4540 {
4541 if (rettv->v_type != var2.v_type
4542 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4543 {
4544 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004545 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004546 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004547 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004548 clear_tv(rettv);
4549 clear_tv(&var2);
4550 return FAIL;
4551 }
4552 else
4553 {
4554 /* Compare two Funcrefs for being equal or unequal. */
4555 if (rettv->vval.v_string == NULL
4556 || var2.vval.v_string == NULL)
4557 n1 = FALSE;
4558 else
4559 n1 = STRCMP(rettv->vval.v_string,
4560 var2.vval.v_string) == 0;
4561 if (type == TYPE_NEQUAL)
4562 n1 = !n1;
4563 }
4564 }
4565
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004566#ifdef FEAT_FLOAT
4567 /*
4568 * If one of the two variables is a float, compare as a float.
4569 * When using "=~" or "!~", always compare as string.
4570 */
4571 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4572 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4573 {
4574 float_T f1, f2;
4575
4576 if (rettv->v_type == VAR_FLOAT)
4577 f1 = rettv->vval.v_float;
4578 else
4579 f1 = get_tv_number(rettv);
4580 if (var2.v_type == VAR_FLOAT)
4581 f2 = var2.vval.v_float;
4582 else
4583 f2 = get_tv_number(&var2);
4584 n1 = FALSE;
4585 switch (type)
4586 {
4587 case TYPE_EQUAL: n1 = (f1 == f2); break;
4588 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4589 case TYPE_GREATER: n1 = (f1 > f2); break;
4590 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4591 case TYPE_SMALLER: n1 = (f1 < f2); break;
4592 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4593 case TYPE_UNKNOWN:
4594 case TYPE_MATCH:
4595 case TYPE_NOMATCH: break; /* avoid gcc warning */
4596 }
4597 }
4598#endif
4599
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 /*
4601 * If one of the two variables is a number, compare as a number.
4602 * When using "=~" or "!~", always compare as string.
4603 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004604 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4606 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004607 n1 = get_tv_number(rettv);
4608 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 switch (type)
4610 {
4611 case TYPE_EQUAL: n1 = (n1 == n2); break;
4612 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4613 case TYPE_GREATER: n1 = (n1 > n2); break;
4614 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4615 case TYPE_SMALLER: n1 = (n1 < n2); break;
4616 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4617 case TYPE_UNKNOWN:
4618 case TYPE_MATCH:
4619 case TYPE_NOMATCH: break; /* avoid gcc warning */
4620 }
4621 }
4622 else
4623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004624 s1 = get_tv_string_buf(rettv, buf1);
4625 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4627 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4628 else
4629 i = 0;
4630 n1 = FALSE;
4631 switch (type)
4632 {
4633 case TYPE_EQUAL: n1 = (i == 0); break;
4634 case TYPE_NEQUAL: n1 = (i != 0); break;
4635 case TYPE_GREATER: n1 = (i > 0); break;
4636 case TYPE_GEQUAL: n1 = (i >= 0); break;
4637 case TYPE_SMALLER: n1 = (i < 0); break;
4638 case TYPE_SEQUAL: n1 = (i <= 0); break;
4639
4640 case TYPE_MATCH:
4641 case TYPE_NOMATCH:
4642 /* avoid 'l' flag in 'cpoptions' */
4643 save_cpo = p_cpo;
4644 p_cpo = (char_u *)"";
4645 regmatch.regprog = vim_regcomp(s2,
4646 RE_MAGIC + RE_STRING);
4647 regmatch.rm_ic = ic;
4648 if (regmatch.regprog != NULL)
4649 {
4650 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004651 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if (type == TYPE_NOMATCH)
4653 n1 = !n1;
4654 }
4655 p_cpo = save_cpo;
4656 break;
4657
4658 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4659 }
4660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 clear_tv(rettv);
4662 clear_tv(&var2);
4663 rettv->v_type = VAR_NUMBER;
4664 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 }
4666 }
4667
4668 return OK;
4669}
4670
4671/*
4672 * Handle fourth level expression:
4673 * + number addition
4674 * - number subtraction
4675 * . string concatenation
4676 *
4677 * "arg" must point to the first non-white of the expression.
4678 * "arg" is advanced to the next non-white after the recognized expression.
4679 *
4680 * Return OK or FAIL.
4681 */
4682 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004683eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
Bram Moolenaar33570922005-01-25 22:26:29 +00004685 typval_T var2;
4686 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 int op;
4688 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004689#ifdef FEAT_FLOAT
4690 float_T f1 = 0, f2 = 0;
4691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 char_u *s1, *s2;
4693 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4694 char_u *p;
4695
4696 /*
4697 * Get the first variable.
4698 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004699 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 return FAIL;
4701
4702 /*
4703 * Repeat computing, until no '+', '-' or '.' is following.
4704 */
4705 for (;;)
4706 {
4707 op = **arg;
4708 if (op != '+' && op != '-' && op != '.')
4709 break;
4710
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711 if ((op != '+' || rettv->v_type != VAR_LIST)
4712#ifdef FEAT_FLOAT
4713 && (op == '.' || rettv->v_type != VAR_FLOAT)
4714#endif
4715 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004716 {
4717 /* For "list + ...", an illegal use of the first operand as
4718 * a number cannot be determined before evaluating the 2nd
4719 * operand: if this is also a list, all is ok.
4720 * For "something . ...", "something - ..." or "non-list + ...",
4721 * we know that the first operand needs to be a string or number
4722 * without evaluating the 2nd operand. So check before to avoid
4723 * side effects after an error. */
4724 if (evaluate && get_tv_string_chk(rettv) == NULL)
4725 {
4726 clear_tv(rettv);
4727 return FAIL;
4728 }
4729 }
4730
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 /*
4732 * Get the second variable.
4733 */
4734 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004735 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004737 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 return FAIL;
4739 }
4740
4741 if (evaluate)
4742 {
4743 /*
4744 * Compute the result.
4745 */
4746 if (op == '.')
4747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4749 s2 = get_tv_string_buf_chk(&var2, buf2);
4750 if (s2 == NULL) /* type error ? */
4751 {
4752 clear_tv(rettv);
4753 clear_tv(&var2);
4754 return FAIL;
4755 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004756 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004757 clear_tv(rettv);
4758 rettv->v_type = VAR_STRING;
4759 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004761 else if (op == '+' && rettv->v_type == VAR_LIST
4762 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004763 {
4764 /* concatenate Lists */
4765 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4766 &var3) == FAIL)
4767 {
4768 clear_tv(rettv);
4769 clear_tv(&var2);
4770 return FAIL;
4771 }
4772 clear_tv(rettv);
4773 *rettv = var3;
4774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 else
4776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777 int error = FALSE;
4778
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004779#ifdef FEAT_FLOAT
4780 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004782 f1 = rettv->vval.v_float;
4783 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004785 else
4786#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004787 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004788 n1 = get_tv_number_chk(rettv, &error);
4789 if (error)
4790 {
4791 /* This can only happen for "list + non-list". For
4792 * "non-list + ..." or "something - ...", we returned
4793 * before evaluating the 2nd operand. */
4794 clear_tv(rettv);
4795 return FAIL;
4796 }
4797#ifdef FEAT_FLOAT
4798 if (var2.v_type == VAR_FLOAT)
4799 f1 = n1;
4800#endif
4801 }
4802#ifdef FEAT_FLOAT
4803 if (var2.v_type == VAR_FLOAT)
4804 {
4805 f2 = var2.vval.v_float;
4806 n2 = 0;
4807 }
4808 else
4809#endif
4810 {
4811 n2 = get_tv_number_chk(&var2, &error);
4812 if (error)
4813 {
4814 clear_tv(rettv);
4815 clear_tv(&var2);
4816 return FAIL;
4817 }
4818#ifdef FEAT_FLOAT
4819 if (rettv->v_type == VAR_FLOAT)
4820 f2 = n2;
4821#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004823 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004824
4825#ifdef FEAT_FLOAT
4826 /* If there is a float on either side the result is a float. */
4827 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4828 {
4829 if (op == '+')
4830 f1 = f1 + f2;
4831 else
4832 f1 = f1 - f2;
4833 rettv->v_type = VAR_FLOAT;
4834 rettv->vval.v_float = f1;
4835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837#endif
4838 {
4839 if (op == '+')
4840 n1 = n1 + n2;
4841 else
4842 n1 = n1 - n2;
4843 rettv->v_type = VAR_NUMBER;
4844 rettv->vval.v_number = n1;
4845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004847 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849 }
4850 return OK;
4851}
4852
4853/*
4854 * Handle fifth level expression:
4855 * * number multiplication
4856 * / number division
4857 * % number modulo
4858 *
4859 * "arg" must point to the first non-white of the expression.
4860 * "arg" is advanced to the next non-white after the recognized expression.
4861 *
4862 * Return OK or FAIL.
4863 */
4864 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004865eval6(
4866 char_u **arg,
4867 typval_T *rettv,
4868 int evaluate,
4869 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
Bram Moolenaar33570922005-01-25 22:26:29 +00004871 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 int op;
4873 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874#ifdef FEAT_FLOAT
4875 int use_float = FALSE;
4876 float_T f1 = 0, f2;
4877#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004878 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 /*
4881 * Get the first variable.
4882 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004883 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 return FAIL;
4885
4886 /*
4887 * Repeat computing, until no '*', '/' or '%' is following.
4888 */
4889 for (;;)
4890 {
4891 op = **arg;
4892 if (op != '*' && op != '/' && op != '%')
4893 break;
4894
4895 if (evaluate)
4896 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897#ifdef FEAT_FLOAT
4898 if (rettv->v_type == VAR_FLOAT)
4899 {
4900 f1 = rettv->vval.v_float;
4901 use_float = TRUE;
4902 n1 = 0;
4903 }
4904 else
4905#endif
4906 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004907 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004908 if (error)
4909 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
4911 else
4912 n1 = 0;
4913
4914 /*
4915 * Get the second variable.
4916 */
4917 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004918 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 return FAIL;
4920
4921 if (evaluate)
4922 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923#ifdef FEAT_FLOAT
4924 if (var2.v_type == VAR_FLOAT)
4925 {
4926 if (!use_float)
4927 {
4928 f1 = n1;
4929 use_float = TRUE;
4930 }
4931 f2 = var2.vval.v_float;
4932 n2 = 0;
4933 }
4934 else
4935#endif
4936 {
4937 n2 = get_tv_number_chk(&var2, &error);
4938 clear_tv(&var2);
4939 if (error)
4940 return FAIL;
4941#ifdef FEAT_FLOAT
4942 if (use_float)
4943 f2 = n2;
4944#endif
4945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
4947 /*
4948 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004951#ifdef FEAT_FLOAT
4952 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954 if (op == '*')
4955 f1 = f1 * f2;
4956 else if (op == '/')
4957 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004958# ifdef VMS
4959 /* VMS crashes on divide by zero, work around it */
4960 if (f2 == 0.0)
4961 {
4962 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004963 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004964 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004965 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004967 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968 }
4969 else
4970 f1 = f1 / f2;
4971# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 /* We rely on the floating point library to handle divide
4973 * by zero to result in "inf" and not a crash. */
4974 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004979 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 return FAIL;
4981 }
4982 rettv->v_type = VAR_FLOAT;
4983 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 if (op == '*')
4989 n1 = n1 * n2;
4990 else if (op == '/')
4991 {
4992 if (n2 == 0) /* give an error message? */
4993 {
4994 if (n1 == 0)
4995 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4996 else if (n1 < 0)
4997 n1 = -0x7fffffffL;
4998 else
4999 n1 = 0x7fffffffL;
5000 }
5001 else
5002 n1 = n1 / n2;
5003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005005 {
5006 if (n2 == 0) /* give an error message? */
5007 n1 = 0;
5008 else
5009 n1 = n1 % n2;
5010 }
5011 rettv->v_type = VAR_NUMBER;
5012 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
5015 }
5016
5017 return OK;
5018}
5019
5020/*
5021 * Handle sixth level expression:
5022 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005023 * "string" string constant
5024 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 * &option-name option value
5026 * @r register contents
5027 * identifier variable value
5028 * function() function call
5029 * $VAR environment variable
5030 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005031 * [expr, expr] List
5032 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 *
5034 * Also handle:
5035 * ! in front logical NOT
5036 * - in front unary minus
5037 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 * trailing [] subscript in String or List
5039 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 *
5041 * "arg" must point to the first non-white of the expression.
5042 * "arg" is advanced to the next non-white after the recognized expression.
5043 *
5044 * Return OK or FAIL.
5045 */
5046 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005047eval7(
5048 char_u **arg,
5049 typval_T *rettv,
5050 int evaluate,
5051 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 long n;
5054 int len;
5055 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 char_u *start_leader, *end_leader;
5057 int ret = OK;
5058 char_u *alias;
5059
5060 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005061 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005062 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065
5066 /*
5067 * Skip '!' and '-' characters. They are handled later.
5068 */
5069 start_leader = *arg;
5070 while (**arg == '!' || **arg == '-' || **arg == '+')
5071 *arg = skipwhite(*arg + 1);
5072 end_leader = *arg;
5073
5074 switch (**arg)
5075 {
5076 /*
5077 * Number constant.
5078 */
5079 case '0':
5080 case '1':
5081 case '2':
5082 case '3':
5083 case '4':
5084 case '5':
5085 case '6':
5086 case '7':
5087 case '8':
5088 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005089 {
5090#ifdef FEAT_FLOAT
5091 char_u *p = skipdigits(*arg + 1);
5092 int get_float = FALSE;
5093
5094 /* We accept a float when the format matches
5095 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005096 * strict to avoid backwards compatibility problems.
5097 * Don't look for a float after the "." operator, so that
5098 * ":let vers = 1.2.3" doesn't fail. */
5099 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005101 get_float = TRUE;
5102 p = skipdigits(p + 2);
5103 if (*p == 'e' || *p == 'E')
5104 {
5105 ++p;
5106 if (*p == '-' || *p == '+')
5107 ++p;
5108 if (!vim_isdigit(*p))
5109 get_float = FALSE;
5110 else
5111 p = skipdigits(p + 1);
5112 }
5113 if (ASCII_ISALPHA(*p) || *p == '.')
5114 get_float = FALSE;
5115 }
5116 if (get_float)
5117 {
5118 float_T f;
5119
5120 *arg += string2float(*arg, &f);
5121 if (evaluate)
5122 {
5123 rettv->v_type = VAR_FLOAT;
5124 rettv->vval.v_float = f;
5125 }
5126 }
5127 else
5128#endif
5129 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005130 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005131 *arg += len;
5132 if (evaluate)
5133 {
5134 rettv->v_type = VAR_NUMBER;
5135 rettv->vval.v_number = n;
5136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
5138 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140
5141 /*
5142 * String constant: "string".
5143 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 break;
5146
5147 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151 break;
5152
5153 /*
5154 * List: [expr, expr]
5155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 break;
5158
5159 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005160 * Dictionary: {key: val, key: val}
5161 */
5162 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5163 break;
5164
5165 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005166 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005168 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 break;
5170
5171 /*
5172 * Environment variable: $VAR.
5173 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005174 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 break;
5176
5177 /*
5178 * Register contents: @r.
5179 */
5180 case '@': ++*arg;
5181 if (evaluate)
5182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005184 rettv->vval.v_string = get_reg_contents(**arg,
5185 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
5187 if (**arg != NUL)
5188 ++*arg;
5189 break;
5190
5191 /*
5192 * nested expression: (expression).
5193 */
5194 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005195 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 if (**arg == ')')
5197 ++*arg;
5198 else if (ret == OK)
5199 {
5200 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 ret = FAIL;
5203 }
5204 break;
5205
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 break;
5208 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209
5210 if (ret == NOTDONE)
5211 {
5212 /*
5213 * Must be a variable or function name.
5214 * Can also be a curly-braces kind of name: {expr}.
5215 */
5216 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005217 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218 if (alias != NULL)
5219 s = alias;
5220
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005221 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 ret = FAIL;
5223 else
5224 {
5225 if (**arg == '(') /* recursive! */
5226 {
5227 /* If "s" is the name of a variable of type VAR_FUNC
5228 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005229 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230
5231 /* Invoke the function. */
5232 ret = get_func_tv(s, len, rettv, arg,
5233 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005234 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005235
5236 /* If evaluate is FALSE rettv->v_type was not set in
5237 * get_func_tv, but it's needed in handle_subscript() to parse
5238 * what follows. So set it here. */
5239 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5240 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005241 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005242 rettv->v_type = VAR_FUNC;
5243 }
5244
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 /* Stop the expression evaluation when immediately
5246 * aborting on error, or when an interrupt occurred or
5247 * an exception was thrown but not caught. */
5248 if (aborting())
5249 {
5250 if (ret == OK)
5251 clear_tv(rettv);
5252 ret = FAIL;
5253 }
5254 }
5255 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005256 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005257 else
5258 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005260 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 }
5262
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 *arg = skipwhite(*arg);
5264
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5266 * expr(expr). */
5267 if (ret == OK)
5268 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 /*
5271 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5272 */
5273 if (ret == OK && evaluate && end_leader > start_leader)
5274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005275 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005276 int val = 0;
5277#ifdef FEAT_FLOAT
5278 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005279
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005280 if (rettv->v_type == VAR_FLOAT)
5281 f = rettv->vval.v_float;
5282 else
5283#endif
5284 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287 clear_tv(rettv);
5288 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005290 else
5291 {
5292 while (end_leader > start_leader)
5293 {
5294 --end_leader;
5295 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005296 {
5297#ifdef FEAT_FLOAT
5298 if (rettv->v_type == VAR_FLOAT)
5299 f = !f;
5300 else
5301#endif
5302 val = !val;
5303 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005304 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005305 {
5306#ifdef FEAT_FLOAT
5307 if (rettv->v_type == VAR_FLOAT)
5308 f = -f;
5309 else
5310#endif
5311 val = -val;
5312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005314#ifdef FEAT_FLOAT
5315 if (rettv->v_type == VAR_FLOAT)
5316 {
5317 clear_tv(rettv);
5318 rettv->vval.v_float = f;
5319 }
5320 else
5321#endif
5322 {
5323 clear_tv(rettv);
5324 rettv->v_type = VAR_NUMBER;
5325 rettv->vval.v_number = val;
5326 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329
5330 return ret;
5331}
5332
5333/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005334 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5335 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5337 */
5338 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005339eval_index(
5340 char_u **arg,
5341 typval_T *rettv,
5342 int evaluate,
5343 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344{
5345 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005346 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 long len = -1;
5349 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352
Bram Moolenaara03f2332016-02-06 18:09:59 +01005353 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005355 case VAR_FUNC:
5356 if (verbose)
5357 EMSG(_("E695: Cannot index a Funcref"));
5358 return FAIL;
5359 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005360#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005361 if (verbose)
5362 EMSG(_(e_float_as_string));
5363 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005364#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005365 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005366 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005367 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005368 if (verbose)
5369 EMSG(_("E909: Cannot index a special variable"));
5370 return FAIL;
5371 case VAR_UNKNOWN:
5372 if (evaluate)
5373 return FAIL;
5374 /* FALLTHROUGH */
5375
5376 case VAR_STRING:
5377 case VAR_NUMBER:
5378 case VAR_LIST:
5379 case VAR_DICT:
5380 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005381 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005383 init_tv(&var1);
5384 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 /*
5388 * dict.name
5389 */
5390 key = *arg + 1;
5391 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5392 ;
5393 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 }
5397 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399 /*
5400 * something[idx]
5401 *
5402 * Get the (first) variable from inside the [].
5403 */
5404 *arg = skipwhite(*arg + 1);
5405 if (**arg == ':')
5406 empty1 = TRUE;
5407 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5408 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005409 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5410 {
5411 /* not a number or string */
5412 clear_tv(&var1);
5413 return FAIL;
5414 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415
5416 /*
5417 * Get the second variable from inside the [:].
5418 */
5419 if (**arg == ':')
5420 {
5421 range = TRUE;
5422 *arg = skipwhite(*arg + 1);
5423 if (**arg == ']')
5424 empty2 = TRUE;
5425 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005427 if (!empty1)
5428 clear_tv(&var1);
5429 return FAIL;
5430 }
5431 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5432 {
5433 /* not a number or string */
5434 if (!empty1)
5435 clear_tv(&var1);
5436 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437 return FAIL;
5438 }
5439 }
5440
5441 /* Check for the ']'. */
5442 if (**arg != ']')
5443 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005444 if (verbose)
5445 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 clear_tv(&var1);
5447 if (range)
5448 clear_tv(&var2);
5449 return FAIL;
5450 }
5451 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 }
5453
5454 if (evaluate)
5455 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 n1 = 0;
5457 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459 n1 = get_tv_number(&var1);
5460 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462 if (range)
5463 {
5464 if (empty2)
5465 n2 = -1;
5466 else
5467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 n2 = get_tv_number(&var2);
5469 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 }
5471 }
5472
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005473 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005475 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005476 case VAR_FUNC:
5477 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005478 case VAR_SPECIAL:
5479 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005480 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005481 break; /* not evaluating, skipping over subscript */
5482
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 case VAR_NUMBER:
5484 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 len = (long)STRLEN(s);
5487 if (range)
5488 {
5489 /* The resulting variable is a substring. If the indexes
5490 * are out of range the result is empty. */
5491 if (n1 < 0)
5492 {
5493 n1 = len + n1;
5494 if (n1 < 0)
5495 n1 = 0;
5496 }
5497 if (n2 < 0)
5498 n2 = len + n2;
5499 else if (n2 >= len)
5500 n2 = len;
5501 if (n1 >= len || n2 < 0 || n1 > n2)
5502 s = NULL;
5503 else
5504 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5505 }
5506 else
5507 {
5508 /* The resulting variable is a string of a single
5509 * character. If the index is too big or negative the
5510 * result is empty. */
5511 if (n1 >= len || n1 < 0)
5512 s = NULL;
5513 else
5514 s = vim_strnsave(s + n1, 1);
5515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 clear_tv(rettv);
5517 rettv->v_type = VAR_STRING;
5518 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 break;
5520
5521 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 if (n1 < 0)
5524 n1 = len + n1;
5525 if (!empty1 && (n1 < 0 || n1 >= len))
5526 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005527 /* For a range we allow invalid values and return an empty
5528 * list. A list index out of range is an error. */
5529 if (!range)
5530 {
5531 if (verbose)
5532 EMSGN(_(e_listidx), n1);
5533 return FAIL;
5534 }
5535 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 }
5537 if (range)
5538 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005539 list_T *l;
5540 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541
5542 if (n2 < 0)
5543 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005544 else if (n2 >= len)
5545 n2 = len - 1;
5546 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005547 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 l = list_alloc();
5549 if (l == NULL)
5550 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 n1 <= n2; ++n1)
5553 {
5554 if (list_append_tv(l, &item->li_tv) == FAIL)
5555 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005556 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 return FAIL;
5558 }
5559 item = item->li_next;
5560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 clear_tv(rettv);
5562 rettv->v_type = VAR_LIST;
5563 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005564 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 }
5566 else
5567 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005568 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 clear_tv(rettv);
5570 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 }
5572 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573
5574 case VAR_DICT:
5575 if (range)
5576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005577 if (verbose)
5578 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 if (len == -1)
5580 clear_tv(&var1);
5581 return FAIL;
5582 }
5583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005584 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585
5586 if (len == -1)
5587 {
5588 key = get_tv_string(&var1);
5589 if (*key == NUL)
5590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005591 if (verbose)
5592 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 clear_tv(&var1);
5594 return FAIL;
5595 }
5596 }
5597
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005598 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005600 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005601 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 if (len == -1)
5603 clear_tv(&var1);
5604 if (item == NULL)
5605 return FAIL;
5606
5607 copy_tv(&item->di_tv, &var1);
5608 clear_tv(rettv);
5609 *rettv = var1;
5610 }
5611 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 }
5613 }
5614
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 return OK;
5616}
5617
5618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 * Get an option value.
5620 * "arg" points to the '&' or '+' before the option name.
5621 * "arg" is advanced to character after the option name.
5622 * Return OK or FAIL.
5623 */
5624 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005625get_option_tv(
5626 char_u **arg,
5627 typval_T *rettv, /* when NULL, only check if option exists */
5628 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629{
5630 char_u *option_end;
5631 long numval;
5632 char_u *stringval;
5633 int opt_type;
5634 int c;
5635 int working = (**arg == '+'); /* has("+option") */
5636 int ret = OK;
5637 int opt_flags;
5638
5639 /*
5640 * Isolate the option name and find its value.
5641 */
5642 option_end = find_option_end(arg, &opt_flags);
5643 if (option_end == NULL)
5644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005645 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 EMSG2(_("E112: Option name missing: %s"), *arg);
5647 return FAIL;
5648 }
5649
5650 if (!evaluate)
5651 {
5652 *arg = option_end;
5653 return OK;
5654 }
5655
5656 c = *option_end;
5657 *option_end = NUL;
5658 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
5661 if (opt_type == -3) /* invalid name */
5662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 EMSG2(_("E113: Unknown option: %s"), *arg);
5665 ret = FAIL;
5666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 if (opt_type == -2) /* hidden string option */
5670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 rettv->v_type = VAR_STRING;
5672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674 else if (opt_type == -1) /* hidden number option */
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 rettv->v_type = VAR_NUMBER;
5677 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 else if (opt_type == 1) /* number option */
5680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 rettv->v_type = VAR_NUMBER;
5682 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 else /* string option */
5685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 rettv->v_type = VAR_STRING;
5687 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 }
5690 else if (working && (opt_type == -2 || opt_type == -1))
5691 ret = FAIL;
5692
5693 *option_end = c; /* put back for error messages */
5694 *arg = option_end;
5695
5696 return ret;
5697}
5698
5699/*
5700 * Allocate a variable for a string constant.
5701 * Return OK or FAIL.
5702 */
5703 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005704get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705{
5706 char_u *p;
5707 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 int extra = 0;
5709
5710 /*
5711 * Find the end of the string, skipping backslashed characters.
5712 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005713 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 {
5715 if (*p == '\\' && p[1] != NUL)
5716 {
5717 ++p;
5718 /* A "\<x>" form occupies at least 4 characters, and produces up
5719 * to 6 characters: reserve space for 2 extra */
5720 if (*p == '<')
5721 extra += 2;
5722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724
5725 if (*p != '"')
5726 {
5727 EMSG2(_("E114: Missing quote: %s"), *arg);
5728 return FAIL;
5729 }
5730
5731 /* If only parsing, set *arg and return here */
5732 if (!evaluate)
5733 {
5734 *arg = p + 1;
5735 return OK;
5736 }
5737
5738 /*
5739 * Copy the string into allocated memory, handling backslashed
5740 * characters.
5741 */
5742 name = alloc((unsigned)(p - *arg + extra));
5743 if (name == NULL)
5744 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 rettv->v_type = VAR_STRING;
5746 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 {
5750 if (*p == '\\')
5751 {
5752 switch (*++p)
5753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 case 'b': *name++ = BS; ++p; break;
5755 case 'e': *name++ = ESC; ++p; break;
5756 case 'f': *name++ = FF; ++p; break;
5757 case 'n': *name++ = NL; ++p; break;
5758 case 'r': *name++ = CAR; ++p; break;
5759 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
5761 case 'X': /* hex: "\x1", "\x12" */
5762 case 'x':
5763 case 'u': /* Unicode: "\u0023" */
5764 case 'U':
5765 if (vim_isxdigit(p[1]))
5766 {
5767 int n, nr;
5768 int c = toupper(*p);
5769
5770 if (c == 'X')
5771 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005772 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005774 else
5775 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 nr = 0;
5777 while (--n >= 0 && vim_isxdigit(p[1]))
5778 {
5779 ++p;
5780 nr = (nr << 4) + hex2nr(*p);
5781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783#ifdef FEAT_MBYTE
5784 /* For "\u" store the number according to
5785 * 'encoding'. */
5786 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 else
5789#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 break;
5793
5794 /* octal: "\1", "\12", "\123" */
5795 case '0':
5796 case '1':
5797 case '2':
5798 case '3':
5799 case '4':
5800 case '5':
5801 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 case '7': *name = *p++ - '0';
5803 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 *name = (*name << 3) + *p++ - '0';
5806 if (*p >= '0' && *p <= '7')
5807 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 break;
5811
5812 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 if (extra != 0)
5815 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 break;
5818 }
5819 /* FALLTHROUGH */
5820
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 break;
5823 }
5824 }
5825 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 *arg = p + 1;
5831
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 return OK;
5833}
5834
5835/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 * Return OK or FAIL.
5838 */
5839 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005840get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841{
5842 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005843 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 int reduce = 0;
5845
5846 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 */
5849 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5850 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 break;
5855 ++reduce;
5856 ++p;
5857 }
5858 }
5859
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 return FAIL;
5864 }
5865
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 if (!evaluate)
5868 {
5869 *arg = p + 1;
5870 return OK;
5871 }
5872
5873 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 */
5876 str = alloc((unsigned)((p - *arg) - reduce));
5877 if (str == NULL)
5878 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 rettv->v_type = VAR_STRING;
5880 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 break;
5888 ++p;
5889 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 *arg = p + 1;
5894
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 return OK;
5896}
5897
5898/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899 * Allocate a variable for a List and fill it from "*arg".
5900 * Return OK or FAIL.
5901 */
5902 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005903get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904{
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 list_T *l = NULL;
5906 typval_T tv;
5907 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908
5909 if (evaluate)
5910 {
5911 l = list_alloc();
5912 if (l == NULL)
5913 return FAIL;
5914 }
5915
5916 *arg = skipwhite(*arg + 1);
5917 while (**arg != ']' && **arg != NUL)
5918 {
5919 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5920 goto failret;
5921 if (evaluate)
5922 {
5923 item = listitem_alloc();
5924 if (item != NULL)
5925 {
5926 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005927 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928 list_append(l, item);
5929 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005930 else
5931 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 }
5933
5934 if (**arg == ']')
5935 break;
5936 if (**arg != ',')
5937 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 goto failret;
5940 }
5941 *arg = skipwhite(*arg + 1);
5942 }
5943
5944 if (**arg != ']')
5945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005946 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947failret:
5948 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005949 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 return FAIL;
5951 }
5952
5953 *arg = skipwhite(*arg + 1);
5954 if (evaluate)
5955 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005956 rettv->v_type = VAR_LIST;
5957 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 ++l->lv_refcount;
5959 }
5960
5961 return OK;
5962}
5963
5964/*
5965 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005966 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005968 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005969list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005971 list_T *l;
5972
5973 l = (list_T *)alloc_clear(sizeof(list_T));
5974 if (l != NULL)
5975 {
5976 /* Prepend the list to the list of lists for garbage collection. */
5977 if (first_list != NULL)
5978 first_list->lv_used_prev = l;
5979 l->lv_used_prev = NULL;
5980 l->lv_used_next = first_list;
5981 first_list = l;
5982 }
5983 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984}
5985
5986/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005987 * Allocate an empty list for a return value.
5988 * Returns OK or FAIL.
5989 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005990 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005991rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005992{
5993 list_T *l = list_alloc();
5994
5995 if (l == NULL)
5996 return FAIL;
5997
5998 rettv->vval.v_list = l;
5999 rettv->v_type = VAR_LIST;
6000 ++l->lv_refcount;
6001 return OK;
6002}
6003
6004/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005 * Unreference a list: decrement the reference count and free it when it
6006 * becomes zero.
6007 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006009list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006011 if (l != NULL && --l->lv_refcount <= 0)
6012 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013}
6014
6015/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006016 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 * Ignores the reference count.
6018 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006019 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006020list_free(
6021 list_T *l,
6022 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023{
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006026 /* Remove the list from the list of lists for garbage collection. */
6027 if (l->lv_used_prev == NULL)
6028 first_list = l->lv_used_next;
6029 else
6030 l->lv_used_prev->lv_used_next = l->lv_used_next;
6031 if (l->lv_used_next != NULL)
6032 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6033
Bram Moolenaard9fba312005-06-26 22:34:35 +00006034 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006036 /* Remove the item before deleting it. */
6037 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006038 if (recurse || (item->li_tv.v_type != VAR_LIST
6039 && item->li_tv.v_type != VAR_DICT))
6040 clear_tv(&item->li_tv);
6041 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 }
6043 vim_free(l);
6044}
6045
6046/*
6047 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006048 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006050 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006051listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052{
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054}
6055
6056/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006057 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006059 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006060listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006062 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 vim_free(item);
6064}
6065
6066/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006067 * Remove a list item from a List and free it. Also clears the value.
6068 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006069 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006070listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006071{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006072 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006073 listitem_free(item);
6074}
6075
6076/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077 * Get the number of items in a list.
6078 */
6079 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082 if (l == NULL)
6083 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006084 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085}
6086
6087/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006088 * Return TRUE when two lists have exactly the same values.
6089 */
6090 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006091list_equal(
6092 list_T *l1,
6093 list_T *l2,
6094 int ic, /* ignore case for strings */
6095 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096{
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006099 if (l1 == NULL || l2 == NULL)
6100 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006101 if (l1 == l2)
6102 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006103 if (list_len(l1) != list_len(l2))
6104 return FALSE;
6105
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106 for (item1 = l1->lv_first, item2 = l2->lv_first;
6107 item1 != NULL && item2 != NULL;
6108 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006109 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 return FALSE;
6111 return item1 == NULL && item2 == NULL;
6112}
6113
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006114/*
6115 * Return the dictitem that an entry in a hashtable points to.
6116 */
6117 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006119{
6120 return HI2DI(hi);
6121}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006122
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006123/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006124 * Return TRUE when two dictionaries have exactly the same key/values.
6125 */
6126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006127dict_equal(
6128 dict_T *d1,
6129 dict_T *d2,
6130 int ic, /* ignore case for strings */
6131 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132{
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 hashitem_T *hi;
6134 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006135 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006137 if (d1 == NULL || d2 == NULL)
6138 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006139 if (d1 == d2)
6140 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006141 if (dict_len(d1) != dict_len(d2))
6142 return FALSE;
6143
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006144 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006147 if (!HASHITEM_EMPTY(hi))
6148 {
6149 item2 = dict_find(d2, hi->hi_key, -1);
6150 if (item2 == NULL)
6151 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006152 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006153 return FALSE;
6154 --todo;
6155 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006156 }
6157 return TRUE;
6158}
6159
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006160static int tv_equal_recurse_limit;
6161
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006162/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006163 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006164 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006165 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006166 */
6167 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006168tv_equal(
6169 typval_T *tv1,
6170 typval_T *tv2,
6171 int ic, /* ignore case */
6172 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173{
6174 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006175 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006176 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006177 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006179 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006180 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006181
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006182 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 * recursiveness to a limit. We guess they are equal then.
6184 * A fixed limit has the problem of still taking an awful long time.
6185 * Reduce the limit every time running into it. That should work fine for
6186 * deeply linked structures that are not recursively linked and catch
6187 * recursiveness quickly. */
6188 if (!recursive)
6189 tv_equal_recurse_limit = 1000;
6190 if (recursive_cnt >= tv_equal_recurse_limit)
6191 {
6192 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006193 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006194 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006195
6196 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006199 ++recursive_cnt;
6200 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6201 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006202 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203
6204 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205 ++recursive_cnt;
6206 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6207 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006208 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006209
6210 case VAR_FUNC:
6211 return (tv1->vval.v_string != NULL
6212 && tv2->vval.v_string != NULL
6213 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6214
6215 case VAR_NUMBER:
6216 return tv1->vval.v_number == tv2->vval.v_number;
6217
6218 case VAR_STRING:
6219 s1 = get_tv_string_buf(tv1, buf1);
6220 s2 = get_tv_string_buf(tv2, buf2);
6221 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006222
6223 case VAR_SPECIAL:
6224 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006225
6226 case VAR_FLOAT:
6227#ifdef FEAT_FLOAT
6228 return tv1->vval.v_float == tv2->vval.v_float;
6229#endif
6230 case VAR_JOB:
6231#ifdef FEAT_JOB
6232 return tv1->vval.v_job == tv2->vval.v_job;
6233#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006234 case VAR_CHANNEL:
6235#ifdef FEAT_CHANNEL
6236 return tv1->vval.v_channel == tv2->vval.v_channel;
6237#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006238 case VAR_UNKNOWN:
6239 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006240 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006241
Bram Moolenaara03f2332016-02-06 18:09:59 +01006242 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6243 * does not equal anything, not even itself. */
6244 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245}
6246
6247/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248 * Locate item with index "n" in list "l" and return it.
6249 * A negative index is counted from the end; -1 is the last item.
6250 * Returns NULL when "n" is out of range.
6251 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006252 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006253list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254{
Bram Moolenaar33570922005-01-25 22:26:29 +00006255 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 long idx;
6257
6258 if (l == NULL)
6259 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006260
6261 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006263 n = l->lv_len + n;
6264
6265 /* Check for index out of range. */
6266 if (n < 0 || n >= l->lv_len)
6267 return NULL;
6268
6269 /* When there is a cached index may start search from there. */
6270 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006272 if (n < l->lv_idx / 2)
6273 {
6274 /* closest to the start of the list */
6275 item = l->lv_first;
6276 idx = 0;
6277 }
6278 else if (n > (l->lv_idx + l->lv_len) / 2)
6279 {
6280 /* closest to the end of the list */
6281 item = l->lv_last;
6282 idx = l->lv_len - 1;
6283 }
6284 else
6285 {
6286 /* closest to the cached index */
6287 item = l->lv_idx_item;
6288 idx = l->lv_idx;
6289 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 }
6291 else
6292 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006293 if (n < l->lv_len / 2)
6294 {
6295 /* closest to the start of the list */
6296 item = l->lv_first;
6297 idx = 0;
6298 }
6299 else
6300 {
6301 /* closest to the end of the list */
6302 item = l->lv_last;
6303 idx = l->lv_len - 1;
6304 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006306
6307 while (n > idx)
6308 {
6309 /* search forward */
6310 item = item->li_next;
6311 ++idx;
6312 }
6313 while (n < idx)
6314 {
6315 /* search backward */
6316 item = item->li_prev;
6317 --idx;
6318 }
6319
6320 /* cache the used index */
6321 l->lv_idx = idx;
6322 l->lv_idx_item = item;
6323
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 return item;
6325}
6326
6327/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006328 * Get list item "l[idx]" as a number.
6329 */
6330 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006331list_find_nr(
6332 list_T *l,
6333 long idx,
6334 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006335{
6336 listitem_T *li;
6337
6338 li = list_find(l, idx);
6339 if (li == NULL)
6340 {
6341 if (errorp != NULL)
6342 *errorp = TRUE;
6343 return -1L;
6344 }
6345 return get_tv_number_chk(&li->li_tv, errorp);
6346}
6347
6348/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006349 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6350 */
6351 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006352list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006353{
6354 listitem_T *li;
6355
6356 li = list_find(l, idx - 1);
6357 if (li == NULL)
6358 {
6359 EMSGN(_(e_listidx), idx);
6360 return NULL;
6361 }
6362 return get_tv_string(&li->li_tv);
6363}
6364
6365/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006366 * Locate "item" list "l" and return its index.
6367 * Returns -1 when "item" is not in the list.
6368 */
6369 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006370list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006371{
6372 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006374
6375 if (l == NULL)
6376 return -1;
6377 idx = 0;
6378 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6379 ++idx;
6380 if (li == NULL)
6381 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006382 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006383}
6384
6385/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006386 * Append item "item" to the end of list "l".
6387 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006388 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006389list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390{
6391 if (l->lv_last == NULL)
6392 {
6393 /* empty list */
6394 l->lv_first = item;
6395 l->lv_last = item;
6396 item->li_prev = NULL;
6397 }
6398 else
6399 {
6400 l->lv_last->li_next = item;
6401 item->li_prev = l->lv_last;
6402 l->lv_last = item;
6403 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006404 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405 item->li_next = NULL;
6406}
6407
6408/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006409 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006410 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006413list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006415 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416
Bram Moolenaar05159a02005-02-26 23:04:13 +00006417 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006419 copy_tv(tv, &li->li_tv);
6420 list_append(l, li);
6421 return OK;
6422}
6423
6424/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006425 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006426 * Return FAIL when out of memory.
6427 */
6428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006429list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430{
6431 listitem_T *li = listitem_alloc();
6432
6433 if (li == NULL)
6434 return FAIL;
6435 li->li_tv.v_type = VAR_DICT;
6436 li->li_tv.v_lock = 0;
6437 li->li_tv.vval.v_dict = dict;
6438 list_append(list, li);
6439 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006440 return OK;
6441}
6442
6443/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006444 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006445 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006446 * Returns FAIL when out of memory.
6447 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006448 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006449list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006450{
6451 listitem_T *li = listitem_alloc();
6452
6453 if (li == NULL)
6454 return FAIL;
6455 list_append(l, li);
6456 li->li_tv.v_type = VAR_STRING;
6457 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006458 if (str == NULL)
6459 li->li_tv.vval.v_string = NULL;
6460 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006461 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006462 return FAIL;
6463 return OK;
6464}
6465
6466/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006467 * Append "n" to list "l".
6468 * Returns FAIL when out of memory.
6469 */
6470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006471list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006472{
6473 listitem_T *li;
6474
6475 li = listitem_alloc();
6476 if (li == NULL)
6477 return FAIL;
6478 li->li_tv.v_type = VAR_NUMBER;
6479 li->li_tv.v_lock = 0;
6480 li->li_tv.vval.v_number = n;
6481 list_append(l, li);
6482 return OK;
6483}
6484
6485/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006487 * If "item" is NULL append at the end.
6488 * Return FAIL when out of memory.
6489 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006490 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006491list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492{
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494
6495 if (ni == NULL)
6496 return FAIL;
6497 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006498 list_insert(l, ni, item);
6499 return OK;
6500}
6501
6502 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006503list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006504{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505 if (item == NULL)
6506 /* Append new item at end of list. */
6507 list_append(l, ni);
6508 else
6509 {
6510 /* Insert new item before existing item. */
6511 ni->li_prev = item->li_prev;
6512 ni->li_next = item;
6513 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006514 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006516 ++l->lv_idx;
6517 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006519 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 l->lv_idx_item = NULL;
6522 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006524 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526}
6527
6528/*
6529 * Extend "l1" with "l2".
6530 * If "bef" is NULL append at the end, otherwise insert before this item.
6531 * Returns FAIL when out of memory.
6532 */
6533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006534list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535{
Bram Moolenaar33570922005-01-25 22:26:29 +00006536 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006537 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006539 /* We also quit the loop when we have inserted the original item count of
6540 * the list, avoid a hang when we extend a list with itself. */
6541 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6543 return FAIL;
6544 return OK;
6545}
6546
6547/*
6548 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6549 * Return FAIL when out of memory.
6550 */
6551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006552list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553{
Bram Moolenaar33570922005-01-25 22:26:29 +00006554 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006556 if (l1 == NULL || l2 == NULL)
6557 return FAIL;
6558
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006560 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561 if (l == NULL)
6562 return FAIL;
6563 tv->v_type = VAR_LIST;
6564 tv->vval.v_list = l;
6565
6566 /* append all items from the second list */
6567 return list_extend(l, l2, NULL);
6568}
6569
6570/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006573 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 * Returns NULL when out of memory.
6575 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006577list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578{
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 list_T *copy;
6580 listitem_T *item;
6581 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582
6583 if (orig == NULL)
6584 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585
6586 copy = list_alloc();
6587 if (copy != NULL)
6588 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006589 if (copyID != 0)
6590 {
6591 /* Do this before adding the items, because one of the items may
6592 * refer back to this list. */
6593 orig->lv_copyID = copyID;
6594 orig->lv_copylist = copy;
6595 }
6596 for (item = orig->lv_first; item != NULL && !got_int;
6597 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 {
6599 ni = listitem_alloc();
6600 if (ni == NULL)
6601 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006602 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006603 {
6604 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6605 {
6606 vim_free(ni);
6607 break;
6608 }
6609 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006611 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 list_append(copy, ni);
6613 }
6614 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006615 if (item != NULL)
6616 {
6617 list_unref(copy);
6618 copy = NULL;
6619 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 }
6621
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622 return copy;
6623}
6624
6625/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006626 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006627 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006628 * This used to be called list_remove, but that conflicts with a Sun header
6629 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006632vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006633{
Bram Moolenaar33570922005-01-25 22:26:29 +00006634 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006636 /* notify watchers */
6637 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006639 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006640 list_fix_watch(l, ip);
6641 if (ip == item2)
6642 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644
6645 if (item2->li_next == NULL)
6646 l->lv_last = item->li_prev;
6647 else
6648 item2->li_next->li_prev = item->li_prev;
6649 if (item->li_prev == NULL)
6650 l->lv_first = item2->li_next;
6651 else
6652 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006653 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654}
6655
6656/*
6657 * Return an allocated string with the string representation of a list.
6658 * May return NULL.
6659 */
6660 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006661list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662{
6663 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664
6665 if (tv->vval.v_list == NULL)
6666 return NULL;
6667 ga_init2(&ga, (int)sizeof(char), 80);
6668 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006669 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006670 {
6671 vim_free(ga.ga_data);
6672 return NULL;
6673 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006674 ga_append(&ga, ']');
6675 ga_append(&ga, NUL);
6676 return (char_u *)ga.ga_data;
6677}
6678
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006679typedef struct join_S {
6680 char_u *s;
6681 char_u *tofree;
6682} join_T;
6683
6684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685list_join_inner(
6686 garray_T *gap, /* to store the result in */
6687 list_T *l,
6688 char_u *sep,
6689 int echo_style,
6690 int copyID,
6691 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006692{
6693 int i;
6694 join_T *p;
6695 int len;
6696 int sumlen = 0;
6697 int first = TRUE;
6698 char_u *tofree;
6699 char_u numbuf[NUMBUFLEN];
6700 listitem_T *item;
6701 char_u *s;
6702
6703 /* Stringify each item in the list. */
6704 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6705 {
6706 if (echo_style)
6707 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6708 else
6709 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6710 if (s == NULL)
6711 return FAIL;
6712
6713 len = (int)STRLEN(s);
6714 sumlen += len;
6715
Bram Moolenaarcde88542015-08-11 19:14:00 +02006716 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006717 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6718 if (tofree != NULL || s != numbuf)
6719 {
6720 p->s = s;
6721 p->tofree = tofree;
6722 }
6723 else
6724 {
6725 p->s = vim_strnsave(s, len);
6726 p->tofree = p->s;
6727 }
6728
6729 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006730 if (did_echo_string_emsg) /* recursion error, bail out */
6731 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006732 }
6733
6734 /* Allocate result buffer with its total size, avoid re-allocation and
6735 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6736 if (join_gap->ga_len >= 2)
6737 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6738 if (ga_grow(gap, sumlen + 2) == FAIL)
6739 return FAIL;
6740
6741 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6742 {
6743 if (first)
6744 first = FALSE;
6745 else
6746 ga_concat(gap, sep);
6747 p = ((join_T *)join_gap->ga_data) + i;
6748
6749 if (p->s != NULL)
6750 ga_concat(gap, p->s);
6751 line_breakcheck();
6752 }
6753
6754 return OK;
6755}
6756
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006757/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006758 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006759 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006760 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006761 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006763list_join(
6764 garray_T *gap,
6765 list_T *l,
6766 char_u *sep,
6767 int echo_style,
6768 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006769{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006770 garray_T join_ga;
6771 int retval;
6772 join_T *p;
6773 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006774
Bram Moolenaard39a7512015-04-16 22:51:22 +02006775 if (l->lv_len < 1)
6776 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006777 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6778 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6779
6780 /* Dispose each item in join_ga. */
6781 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006783 p = (join_T *)join_ga.ga_data;
6784 for (i = 0; i < join_ga.ga_len; ++i)
6785 {
6786 vim_free(p->tofree);
6787 ++p;
6788 }
6789 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006791
6792 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006793}
6794
6795/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006796 * Return the next (unique) copy ID.
6797 * Used for serializing nested structures.
6798 */
6799 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006800get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006801{
6802 current_copyID += COPYID_INC;
6803 return current_copyID;
6804}
6805
6806/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006807 * Garbage collection for lists and dictionaries.
6808 *
6809 * We use reference counts to be able to free most items right away when they
6810 * are no longer used. But for composite items it's possible that it becomes
6811 * unused while the reference count is > 0: When there is a recursive
6812 * reference. Example:
6813 * :let l = [1, 2, 3]
6814 * :let d = {9: l}
6815 * :let l[1] = d
6816 *
6817 * Since this is quite unusual we handle this with garbage collection: every
6818 * once in a while find out which lists and dicts are not referenced from any
6819 * variable.
6820 *
6821 * Here is a good reference text about garbage collection (refers to Python
6822 * but it applies to all reference-counting mechanisms):
6823 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006824 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006825
6826/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006827 * Do garbage collection for lists and dicts.
6828 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006829 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006830 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006831garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006833 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006834 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835 buf_T *buf;
6836 win_T *wp;
6837 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006838 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006839 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006840 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006841#ifdef FEAT_WINDOWS
6842 tabpage_T *tp;
6843#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006844
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006845 /* Only do this once. */
6846 want_garbage_collect = FALSE;
6847 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006848 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006849
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006850 /* We advance by two because we add one for items referenced through
6851 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006852 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 /*
6855 * 1. Go through all accessible variables and mark all lists and dicts
6856 * with copyID.
6857 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858
6859 /* Don't free variables in the previous_funccal list unless they are only
6860 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006861 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006862 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6863 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006864 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6865 NULL);
6866 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6867 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868 }
6869
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006870 /* script-local variables */
6871 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006872 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006873
6874 /* buffer-local variables */
6875 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006876 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6877 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878
6879 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006880 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006881 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6882 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006883#ifdef FEAT_AUTOCMD
6884 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6886 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006887#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006888
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006889#ifdef FEAT_WINDOWS
6890 /* tabpage-local variables */
6891 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6893 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006894#endif
6895
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898
6899 /* function-local variables */
6900 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6901 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6903 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904 }
6905
Bram Moolenaard812df62008-11-09 12:46:09 +00006906 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006908
Bram Moolenaar1dced572012-04-05 16:54:08 +02006909#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006911#endif
6912
Bram Moolenaardb913952012-06-29 12:54:53 +02006913#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006915#endif
6916
6917#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006919#endif
6920
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006921#ifdef FEAT_CHANNEL
6922 abort = abort || set_ref_in_channel(copyID);
6923#endif
6924
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006926 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 /*
6928 * 2. Free lists and dictionaries that are not referenced.
6929 */
6930 did_free = free_unref_items(copyID);
6931
6932 /*
6933 * 3. Check if any funccal can be freed now.
6934 */
6935 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006936 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006937 if (can_free_funccal(*pfc, copyID))
6938 {
6939 fc = *pfc;
6940 *pfc = fc->caller;
6941 free_funccal(fc, TRUE);
6942 did_free = TRUE;
6943 did_free_funccal = TRUE;
6944 }
6945 else
6946 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006947 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006948 if (did_free_funccal)
6949 /* When a funccal was freed some more items might be garbage
6950 * collected, so run again. */
6951 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006952 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 else if (p_verbose > 0)
6954 {
6955 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6956 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957
6958 return did_free;
6959}
6960
6961/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006962 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006963 */
6964 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006965free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006967 dict_T *dd, *dd_next;
6968 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006969 int did_free = FALSE;
6970
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 */
6974 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006975 {
6976 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006979 /* Free the Dictionary and ordinary items it contains, but don't
6980 * recurse into Lists and Dictionaries, they will be in the list
6981 * of dicts or list of lists. */
6982 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006985 dd = dd_next;
6986 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006987
6988 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006989 * Go through the list of lists and free items without the copyID.
6990 * But don't free a list that has a watcher (used in a for loop), these
6991 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 */
6993 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006994 {
6995 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006996 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6997 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006999 /* Free the List and ordinary items it contains, but don't recurse
7000 * into Lists and Dictionaries, they will be in the list of dicts
7001 * or list of lists. */
7002 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007005 ll = ll_next;
7006 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007007
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 return did_free;
7009}
7010
7011/*
7012 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007013 * "list_stack" is used to add lists to be marked. Can be NULL.
7014 *
7015 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007017 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007018set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019{
7020 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007021 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007023 hashtab_T *cur_ht;
7024 ht_stack_T *ht_stack = NULL;
7025 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 cur_ht = ht;
7028 for (;;)
7029 {
7030 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007032 /* Mark each item in the hashtab. If the item contains a hashtab
7033 * it is added to ht_stack, if it contains a list it is added to
7034 * list_stack. */
7035 todo = (int)cur_ht->ht_used;
7036 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7037 if (!HASHITEM_EMPTY(hi))
7038 {
7039 --todo;
7040 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7041 &ht_stack, list_stack);
7042 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044
7045 if (ht_stack == NULL)
7046 break;
7047
7048 /* take an item from the stack */
7049 cur_ht = ht_stack->ht;
7050 tempitem = ht_stack;
7051 ht_stack = ht_stack->prev;
7052 free(tempitem);
7053 }
7054
7055 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056}
7057
7058/*
7059 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007060 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7061 *
7062 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007065set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007067 listitem_T *li;
7068 int abort = FALSE;
7069 list_T *cur_l;
7070 list_stack_T *list_stack = NULL;
7071 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007072
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007073 cur_l = l;
7074 for (;;)
7075 {
7076 if (!abort)
7077 /* Mark each item in the list. If the item contains a hashtab
7078 * it is added to ht_stack, if it contains a list it is added to
7079 * list_stack. */
7080 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7081 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7082 ht_stack, &list_stack);
7083 if (list_stack == NULL)
7084 break;
7085
7086 /* take an item from the stack */
7087 cur_l = list_stack->list;
7088 tempitem = list_stack;
7089 list_stack = list_stack->prev;
7090 free(tempitem);
7091 }
7092
7093 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094}
7095
7096/*
7097 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007098 * "list_stack" is used to add lists to be marked. Can be NULL.
7099 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7100 *
7101 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007103 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007104set_ref_in_item(
7105 typval_T *tv,
7106 int copyID,
7107 ht_stack_T **ht_stack,
7108 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109{
7110 dict_T *dd;
7111 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007112 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007113
Bram Moolenaara03f2332016-02-06 18:09:59 +01007114 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007115 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007116 dd = tv->vval.v_dict;
7117 if (dd != NULL && dd->dv_copyID != copyID)
7118 {
7119 /* Didn't see this dict yet. */
7120 dd->dv_copyID = copyID;
7121 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007122 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007123 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7124 }
7125 else
7126 {
7127 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7128 if (newitem == NULL)
7129 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007130 else
7131 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007132 newitem->ht = &dd->dv_hashtab;
7133 newitem->prev = *ht_stack;
7134 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007136 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007137 }
7138 }
7139 else if (tv->v_type == VAR_LIST)
7140 {
7141 ll = tv->vval.v_list;
7142 if (ll != NULL && ll->lv_copyID != copyID)
7143 {
7144 /* Didn't see this list yet. */
7145 ll->lv_copyID = copyID;
7146 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007147 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007148 abort = set_ref_in_list(ll, copyID, ht_stack);
7149 }
7150 else
7151 {
7152 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007153 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007154 if (newitem == NULL)
7155 abort = TRUE;
7156 else
7157 {
7158 newitem->list = ll;
7159 newitem->prev = *list_stack;
7160 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007162 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007163 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007164 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007165 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007166}
7167
7168/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169 * Allocate an empty header for a dictionary.
7170 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007171 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007172dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007173{
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007175
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007177 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007178 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007179 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007180 if (first_dict != NULL)
7181 first_dict->dv_used_prev = d;
7182 d->dv_used_next = first_dict;
7183 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007184 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007185
Bram Moolenaar33570922005-01-25 22:26:29 +00007186 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007187 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007188 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007189 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007190 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007191 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007192 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007193}
7194
7195/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007196 * Allocate an empty dict for a return value.
7197 * Returns OK or FAIL.
7198 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007199 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007200rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007201{
7202 dict_T *d = dict_alloc();
7203
7204 if (d == NULL)
7205 return FAIL;
7206
7207 rettv->vval.v_dict = d;
7208 rettv->v_type = VAR_DICT;
7209 ++d->dv_refcount;
7210 return OK;
7211}
7212
7213
7214/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 * Unreference a Dictionary: decrement the reference count and free it when it
7216 * becomes zero.
7217 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007218 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007219dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007221 if (d != NULL && --d->dv_refcount <= 0)
7222 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223}
7224
7225/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007226 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227 * Ignores the reference count.
7228 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007229 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007230dict_free(
7231 dict_T *d,
7232 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007234 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007235 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007236 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007238 /* Remove the dict from the list of dicts for garbage collection. */
7239 if (d->dv_used_prev == NULL)
7240 first_dict = d->dv_used_next;
7241 else
7242 d->dv_used_prev->dv_used_next = d->dv_used_next;
7243 if (d->dv_used_next != NULL)
7244 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7245
7246 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007247 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007248 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007251 if (!HASHITEM_EMPTY(hi))
7252 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007253 /* Remove the item before deleting it, just in case there is
7254 * something recursive causing trouble. */
7255 di = HI2DI(hi);
7256 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007257 if (recurse || (di->di_tv.v_type != VAR_LIST
7258 && di->di_tv.v_type != VAR_DICT))
7259 clear_tv(&di->di_tv);
7260 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007261 --todo;
7262 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007264 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 vim_free(d);
7266}
7267
7268/*
7269 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 * The "key" is copied to the new item.
7271 * Note that the value of the item "di_tv" still needs to be initialized!
7272 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007274 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007275dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276{
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007278
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007279 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007281 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007283 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007284 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286}
7287
7288/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007289 * Make a copy of a Dictionary item.
7290 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007291 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007292dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293{
Bram Moolenaar33570922005-01-25 22:26:29 +00007294 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007296 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7297 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298 if (di != NULL)
7299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007301 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 copy_tv(&org->di_tv, &di->di_tv);
7303 }
7304 return di;
7305}
7306
7307/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007308 * Remove item "item" from Dictionary "dict" and free it.
7309 */
7310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007311dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312{
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316 if (HASHITEM_EMPTY(hi))
7317 EMSG2(_(e_intern2), "dictitem_remove()");
7318 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 dictitem_free(item);
7321}
7322
7323/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007324 * Free a dict item. Also clears the value.
7325 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007327dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007328{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007330 if (item->di_flags & DI_FLAGS_ALLOC)
7331 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332}
7333
7334/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7336 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007337 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007338 * Returns NULL when out of memory.
7339 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007340 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007341dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342{
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 dict_T *copy;
7344 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007347
7348 if (orig == NULL)
7349 return NULL;
7350
7351 copy = dict_alloc();
7352 if (copy != NULL)
7353 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007354 if (copyID != 0)
7355 {
7356 orig->dv_copyID = copyID;
7357 orig->dv_copydict = copy;
7358 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007359 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007360 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007361 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 --todo;
7365
7366 di = dictitem_alloc(hi->hi_key);
7367 if (di == NULL)
7368 break;
7369 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007370 {
7371 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7372 copyID) == FAIL)
7373 {
7374 vim_free(di);
7375 break;
7376 }
7377 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007378 else
7379 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7380 if (dict_add(copy, di) == FAIL)
7381 {
7382 dictitem_free(di);
7383 break;
7384 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007386 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007389 if (todo > 0)
7390 {
7391 dict_unref(copy);
7392 copy = NULL;
7393 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 }
7395
7396 return copy;
7397}
7398
7399/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007401 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007403 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007404dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405{
Bram Moolenaar33570922005-01-25 22:26:29 +00007406 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407}
7408
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007410 * Add a number or string entry to dictionary "d".
7411 * When "str" is NULL use number "nr", otherwise use "str".
7412 * Returns FAIL when out of memory and when key already exists.
7413 */
7414 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007415dict_add_nr_str(
7416 dict_T *d,
7417 char *key,
7418 long nr,
7419 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007420{
7421 dictitem_T *item;
7422
7423 item = dictitem_alloc((char_u *)key);
7424 if (item == NULL)
7425 return FAIL;
7426 item->di_tv.v_lock = 0;
7427 if (str == NULL)
7428 {
7429 item->di_tv.v_type = VAR_NUMBER;
7430 item->di_tv.vval.v_number = nr;
7431 }
7432 else
7433 {
7434 item->di_tv.v_type = VAR_STRING;
7435 item->di_tv.vval.v_string = vim_strsave(str);
7436 }
7437 if (dict_add(d, item) == FAIL)
7438 {
7439 dictitem_free(item);
7440 return FAIL;
7441 }
7442 return OK;
7443}
7444
7445/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007446 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007447 * Returns FAIL when out of memory and when key already exists.
7448 */
7449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007450dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007451{
7452 dictitem_T *item;
7453
7454 item = dictitem_alloc((char_u *)key);
7455 if (item == NULL)
7456 return FAIL;
7457 item->di_tv.v_lock = 0;
7458 item->di_tv.v_type = VAR_LIST;
7459 item->di_tv.vval.v_list = list;
7460 if (dict_add(d, item) == FAIL)
7461 {
7462 dictitem_free(item);
7463 return FAIL;
7464 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007465 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007466 return OK;
7467}
7468
7469/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007470 * Get the number of items in a Dictionary.
7471 */
7472 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007473dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007474{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475 if (d == NULL)
7476 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007477 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478}
7479
7480/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481 * Find item "key[len]" in Dictionary "d".
7482 * If "len" is negative use strlen(key).
7483 * Returns NULL when not found.
7484 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007485 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007486dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488#define AKEYLEN 200
7489 char_u buf[AKEYLEN];
7490 char_u *akey;
7491 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494 if (len < 0)
7495 akey = key;
7496 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007497 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007498 tofree = akey = vim_strnsave(key, len);
7499 if (akey == NULL)
7500 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007501 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 else
7503 {
7504 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007505 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 akey = buf;
7507 }
7508
Bram Moolenaar33570922005-01-25 22:26:29 +00007509 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 vim_free(tofree);
7511 if (HASHITEM_EMPTY(hi))
7512 return NULL;
7513 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514}
7515
7516/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007517 * Get a string item from a dictionary.
7518 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007519 * Returns NULL if the entry doesn't exist or out of memory.
7520 */
7521 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007522get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007523{
7524 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007525 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007526
7527 di = dict_find(d, key, -1);
7528 if (di == NULL)
7529 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007530 s = get_tv_string(&di->di_tv);
7531 if (save && s != NULL)
7532 s = vim_strsave(s);
7533 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007534}
7535
7536/*
7537 * Get a number item from a dictionary.
7538 * Returns 0 if the entry doesn't exist or out of memory.
7539 */
7540 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007541get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007542{
7543 dictitem_T *di;
7544
7545 di = dict_find(d, key, -1);
7546 if (di == NULL)
7547 return 0;
7548 return get_tv_number(&di->di_tv);
7549}
7550
7551/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552 * Return an allocated string with the string representation of a Dictionary.
7553 * May return NULL.
7554 */
7555 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557{
7558 garray_T ga;
7559 int first = TRUE;
7560 char_u *tofree;
7561 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007562 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007566
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007567 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 return NULL;
7569 ga_init2(&ga, (int)sizeof(char), 80);
7570 ga_append(&ga, '{');
7571
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007572 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007573 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007575 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577 --todo;
7578
7579 if (first)
7580 first = FALSE;
7581 else
7582 ga_concat(&ga, (char_u *)", ");
7583
7584 tofree = string_quote(hi->hi_key, FALSE);
7585 if (tofree != NULL)
7586 {
7587 ga_concat(&ga, tofree);
7588 vim_free(tofree);
7589 }
7590 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007591 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 if (s != NULL)
7593 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007595 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007596 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007597 line_breakcheck();
7598
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007601 if (todo > 0)
7602 {
7603 vim_free(ga.ga_data);
7604 return NULL;
7605 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606
7607 ga_append(&ga, '}');
7608 ga_append(&ga, NUL);
7609 return (char_u *)ga.ga_data;
7610}
7611
7612/*
7613 * Allocate a variable for a Dictionary and fill it from "*arg".
7614 * Return OK or FAIL. Returns NOTDONE for {expr}.
7615 */
7616 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007617get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618{
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 dict_T *d = NULL;
7620 typval_T tvkey;
7621 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007622 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007623 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007625 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626
7627 /*
7628 * First check if it's not a curly-braces thing: {expr}.
7629 * Must do this without evaluating, otherwise a function may be called
7630 * twice. Unfortunately this means we need to call eval1() twice for the
7631 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007632 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007634 if (*start != '}')
7635 {
7636 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7637 return FAIL;
7638 if (*start == '}')
7639 return NOTDONE;
7640 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641
7642 if (evaluate)
7643 {
7644 d = dict_alloc();
7645 if (d == NULL)
7646 return FAIL;
7647 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007648 tvkey.v_type = VAR_UNKNOWN;
7649 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650
7651 *arg = skipwhite(*arg + 1);
7652 while (**arg != '}' && **arg != NUL)
7653 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007654 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655 goto failret;
7656 if (**arg != ':')
7657 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007658 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 goto failret;
7661 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007662 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007664 key = get_tv_string_buf_chk(&tvkey, buf);
7665 if (key == NULL || *key == NUL)
7666 {
7667 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7668 if (key != NULL)
7669 EMSG(_(e_emptykey));
7670 clear_tv(&tvkey);
7671 goto failret;
7672 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674
7675 *arg = skipwhite(*arg + 1);
7676 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7677 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007678 if (evaluate)
7679 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 goto failret;
7681 }
7682 if (evaluate)
7683 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007684 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685 if (item != NULL)
7686 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007687 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007688 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 clear_tv(&tv);
7690 goto failret;
7691 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007692 item = dictitem_alloc(key);
7693 clear_tv(&tvkey);
7694 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007697 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007698 if (dict_add(d, item) == FAIL)
7699 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 }
7701 }
7702
7703 if (**arg == '}')
7704 break;
7705 if (**arg != ',')
7706 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007707 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 goto failret;
7709 }
7710 *arg = skipwhite(*arg + 1);
7711 }
7712
7713 if (**arg != '}')
7714 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007715 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716failret:
7717 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007718 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719 return FAIL;
7720 }
7721
7722 *arg = skipwhite(*arg + 1);
7723 if (evaluate)
7724 {
7725 rettv->v_type = VAR_DICT;
7726 rettv->vval.v_dict = d;
7727 ++d->dv_refcount;
7728 }
7729
7730 return OK;
7731}
7732
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007733#if defined(FEAT_CHANNEL) || defined(PROTO)
7734/*
7735 * Decrement the reference count on "channel" and free it when it goes down to
7736 * zero.
7737 * Returns TRUE when the channel was freed.
7738 */
7739 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007740channel_unref(channel_T *channel)
7741{
7742 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007743 {
Bram Moolenaar77073442016-02-13 23:23:53 +01007744 channel_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007745 return TRUE;
7746 }
7747 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007748}
7749#endif
7750
Bram Moolenaar835dc632016-02-07 14:27:38 +01007751#ifdef FEAT_JOB
7752 static void
7753job_free(job_T *job)
7754{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007755# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007756 if (job->jv_channel != NULL)
7757 {
7758 /* The channel doesn't count as a references for the job, we need to
7759 * NULL the reference when the job is freed. */
7760 job->jv_channel->ch_job = NULL;
7761 channel_unref(job->jv_channel);
7762 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007763# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007764 mch_clear_job(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007765 vim_free(job);
7766}
7767
7768 static void
7769job_unref(job_T *job)
7770{
7771 if (job != NULL && --job->jv_refcount <= 0)
7772 job_free(job);
7773}
7774
7775/*
7776 * Allocate a job. Sets the refcount to one.
7777 */
7778 static job_T *
7779job_alloc(void)
7780{
7781 job_T *job;
7782
7783 job = (job_T *)alloc_clear(sizeof(job_T));
7784 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007785 job->jv_refcount = 1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007786 return job;
7787}
7788
7789#endif
7790
Bram Moolenaar17a13432016-01-24 14:22:10 +01007791 static char *
7792get_var_special_name(int nr)
7793{
7794 switch (nr)
7795 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007796 case VVAL_FALSE: return "v:false";
7797 case VVAL_TRUE: return "v:true";
7798 case VVAL_NONE: return "v:none";
7799 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007800 }
7801 EMSG2(_(e_intern2), "get_var_special_name()");
7802 return "42";
7803}
7804
Bram Moolenaar8c711452005-01-14 21:53:12 +00007805/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007806 * Return a string with the string representation of a variable.
7807 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007808 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007809 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007810 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007811 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007812 */
7813 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007814echo_string(
7815 typval_T *tv,
7816 char_u **tofree,
7817 char_u *numbuf,
7818 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007819{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007820 static int recurse = 0;
7821 char_u *r = NULL;
7822
Bram Moolenaar33570922005-01-25 22:26:29 +00007823 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007825 if (!did_echo_string_emsg)
7826 {
7827 /* Only give this message once for a recursive call to avoid
7828 * flooding the user with errors. And stop iterating over lists
7829 * and dicts. */
7830 did_echo_string_emsg = TRUE;
7831 EMSG(_("E724: variable nested too deep for displaying"));
7832 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007833 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007834 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007835 }
7836 ++recurse;
7837
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007838 switch (tv->v_type)
7839 {
7840 case VAR_FUNC:
7841 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007842 r = tv->vval.v_string;
7843 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007844
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007845 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007846 if (tv->vval.v_list == NULL)
7847 {
7848 *tofree = NULL;
7849 r = NULL;
7850 }
7851 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7852 {
7853 *tofree = NULL;
7854 r = (char_u *)"[...]";
7855 }
7856 else
7857 {
7858 tv->vval.v_list->lv_copyID = copyID;
7859 *tofree = list2string(tv, copyID);
7860 r = *tofree;
7861 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007862 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007863
Bram Moolenaar8c711452005-01-14 21:53:12 +00007864 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007865 if (tv->vval.v_dict == NULL)
7866 {
7867 *tofree = NULL;
7868 r = NULL;
7869 }
7870 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7871 {
7872 *tofree = NULL;
7873 r = (char_u *)"{...}";
7874 }
7875 else
7876 {
7877 tv->vval.v_dict->dv_copyID = copyID;
7878 *tofree = dict2string(tv, copyID);
7879 r = *tofree;
7880 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007881 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007882
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007883 case VAR_STRING:
7884 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007885 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007886 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007887 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007888 *tofree = NULL;
7889 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007890 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007891
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007892 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007893#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007894 *tofree = NULL;
7895 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7896 r = numbuf;
7897 break;
7898#endif
7899
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007900 case VAR_SPECIAL:
7901 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007902 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007903 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007904 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007905
Bram Moolenaar8502c702014-06-17 12:51:16 +02007906 if (--recurse == 0)
7907 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007908 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007909}
7910
7911/*
7912 * Return a string with the string representation of a variable.
7913 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7914 * "numbuf" is used for a number.
7915 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007916 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007917 */
7918 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007919tv2string(
7920 typval_T *tv,
7921 char_u **tofree,
7922 char_u *numbuf,
7923 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007924{
7925 switch (tv->v_type)
7926 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007927 case VAR_FUNC:
7928 *tofree = string_quote(tv->vval.v_string, TRUE);
7929 return *tofree;
7930 case VAR_STRING:
7931 *tofree = string_quote(tv->vval.v_string, FALSE);
7932 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007933#ifdef FEAT_FLOAT
7934 case VAR_FLOAT:
7935 *tofree = NULL;
7936 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7937 return numbuf;
7938#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007939 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007940 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007942 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007943 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007944 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007945 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007946 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007947 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007948 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949}
7950
7951/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 * Return string "str" in ' quotes, doubling ' characters.
7953 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007954 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007955 */
7956 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007957string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007958{
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960 char_u *p, *r, *s;
7961
Bram Moolenaar33570922005-01-25 22:26:29 +00007962 len = (function ? 13 : 3);
7963 if (str != NULL)
7964 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007965 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 for (p = str; *p != NUL; mb_ptr_adv(p))
7967 if (*p == '\'')
7968 ++len;
7969 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007970 s = r = alloc(len);
7971 if (r != NULL)
7972 {
7973 if (function)
7974 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007975 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007976 r += 10;
7977 }
7978 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007979 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 if (str != NULL)
7981 for (p = str; *p != NUL; )
7982 {
7983 if (*p == '\'')
7984 *r++ = '\'';
7985 MB_COPY_CHAR(p, r);
7986 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007987 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007988 if (function)
7989 *r++ = ')';
7990 *r++ = NUL;
7991 }
7992 return s;
7993}
7994
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007995#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007996/*
7997 * Convert the string "text" to a floating point number.
7998 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7999 * this always uses a decimal point.
8000 * Returns the length of the text that was consumed.
8001 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008002 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008003string2float(
8004 char_u *text,
8005 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008006{
8007 char *s = (char *)text;
8008 float_T f;
8009
8010 f = strtod(s, &s);
8011 *value = f;
8012 return (int)((char_u *)s - text);
8013}
8014#endif
8015
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 * Get the value of an environment variable.
8018 * "arg" is pointing to the '$'. It is advanced to after the name.
8019 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008020 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 */
8022 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008023get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
8025 char_u *string = NULL;
8026 int len;
8027 int cc;
8028 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008029 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030
8031 ++*arg;
8032 name = *arg;
8033 len = get_env_len(arg);
8034 if (evaluate)
8035 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008036 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008037 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008038
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008039 cc = name[len];
8040 name[len] = NUL;
8041 /* first try vim_getenv(), fast for normal environment vars */
8042 string = vim_getenv(name, &mustfree);
8043 if (string != NULL && *string != NUL)
8044 {
8045 if (!mustfree)
8046 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008048 else
8049 {
8050 if (mustfree)
8051 vim_free(string);
8052
8053 /* next try expanding things like $VIM and ${HOME} */
8054 string = expand_env_save(name - 1);
8055 if (string != NULL && *string == '$')
8056 {
8057 vim_free(string);
8058 string = NULL;
8059 }
8060 }
8061 name[len] = cc;
8062
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008063 rettv->v_type = VAR_STRING;
8064 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 }
8066
8067 return OK;
8068}
8069
8070/*
8071 * Array with names and number of arguments of all internal functions
8072 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8073 */
8074static struct fst
8075{
8076 char *f_name; /* function name */
8077 char f_min_argc; /* minimal number of arguments */
8078 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008079 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008080 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081} functions[] =
8082{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008083#ifdef FEAT_FLOAT
8084 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008085 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008087 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008088 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008089 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {"append", 2, 2, f_append},
8091 {"argc", 0, 0, f_argc},
8092 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008093 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008094 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008095#ifdef FEAT_FLOAT
8096 {"asin", 1, 1, f_asin}, /* WJMc */
8097#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008098 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008099 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008100 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008101 {"assert_false", 1, 2, f_assert_false},
8102 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008103#ifdef FEAT_FLOAT
8104 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008105 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008108 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"bufexists", 1, 1, f_bufexists},
8110 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8111 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8112 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8113 {"buflisted", 1, 1, f_buflisted},
8114 {"bufloaded", 1, 1, f_bufloaded},
8115 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008116 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"bufwinnr", 1, 1, f_bufwinnr},
8118 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008119 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008120 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008121 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008122#ifdef FEAT_FLOAT
8123 {"ceil", 1, 1, f_ceil},
8124#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008125#ifdef FEAT_CHANNEL
8126 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008127 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008128 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008129 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008130 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8131 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar77073442016-02-13 23:23:53 +01008132 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008133#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008134 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008135 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008137 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008139#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008140 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008141 {"complete_add", 1, 1, f_complete_add},
8142 {"complete_check", 0, 0, f_complete_check},
8143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008145 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008146#ifdef FEAT_FLOAT
8147 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008148 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008149#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008150 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008152 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008153 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008154 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008156 {"diff_filler", 1, 1, f_diff_filler},
8157 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008158 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008159 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008161 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"eventhandler", 0, 0, f_eventhandler},
8163 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008164 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008166#ifdef FEAT_FLOAT
8167 {"exp", 1, 1, f_exp},
8168#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008169 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008170 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008171 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8173 {"filereadable", 1, 1, f_filereadable},
8174 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008175 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008176 {"finddir", 1, 3, f_finddir},
8177 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008178#ifdef FEAT_FLOAT
8179 {"float2nr", 1, 1, f_float2nr},
8180 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008181 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008182#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008183 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {"fnamemodify", 2, 2, f_fnamemodify},
8185 {"foldclosed", 1, 1, f_foldclosed},
8186 {"foldclosedend", 1, 1, f_foldclosedend},
8187 {"foldlevel", 1, 1, f_foldlevel},
8188 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008189 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008191 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008192 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008193 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008194 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008195 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"getchar", 0, 1, f_getchar},
8197 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008198 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"getcmdline", 0, 0, f_getcmdline},
8200 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008201 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008202 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008203 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008204 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008205 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008206 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"getfsize", 1, 1, f_getfsize},
8208 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008209 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008210 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008211 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008212 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008213 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008214 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008215 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008216 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008218 {"gettabvar", 2, 3, f_gettabvar},
8219 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 {"getwinposx", 0, 0, f_getwinposx},
8221 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008222 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008223 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008224 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008225 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008227 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008228 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008229 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8231 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8232 {"histadd", 2, 2, f_histadd},
8233 {"histdel", 1, 2, f_histdel},
8234 {"histget", 1, 2, f_histget},
8235 {"histnr", 1, 1, f_histnr},
8236 {"hlID", 1, 1, f_hlID},
8237 {"hlexists", 1, 1, f_hlexists},
8238 {"hostname", 0, 0, f_hostname},
8239 {"iconv", 3, 3, f_iconv},
8240 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008241 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008242 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008244 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {"inputrestore", 0, 0, f_inputrestore},
8246 {"inputsave", 0, 0, f_inputsave},
8247 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008248 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008249 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008251 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008252 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008253#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008254# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008255 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008256# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008257 {"job_start", 1, 2, f_job_start},
8258 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008259 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008260#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008261 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008262 {"js_decode", 1, 1, f_js_decode},
8263 {"js_encode", 1, 1, f_js_encode},
8264 {"json_decode", 1, 1, f_json_decode},
8265 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008266 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008268 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"libcall", 3, 3, f_libcall},
8270 {"libcallnr", 3, 3, f_libcallnr},
8271 {"line", 1, 1, f_line},
8272 {"line2byte", 1, 1, f_line2byte},
8273 {"lispindent", 1, 1, f_lispindent},
8274 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008275#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008276 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008277 {"log10", 1, 1, f_log10},
8278#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008279#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008280 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008281#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008282 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008283 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008284 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008285 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008286 {"matchadd", 2, 5, f_matchadd},
8287 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008288 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008289 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008290 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008291 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008292 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008293 {"max", 1, 1, f_max},
8294 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008295#ifdef vim_mkdir
8296 {"mkdir", 1, 3, f_mkdir},
8297#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008298 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008299#ifdef FEAT_MZSCHEME
8300 {"mzeval", 1, 1, f_mzeval},
8301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008303 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008304 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008305 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008306#ifdef FEAT_PERL
8307 {"perleval", 1, 1, f_perleval},
8308#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008309#ifdef FEAT_FLOAT
8310 {"pow", 2, 2, f_pow},
8311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008313 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008314 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008315#ifdef FEAT_PYTHON3
8316 {"py3eval", 1, 1, f_py3eval},
8317#endif
8318#ifdef FEAT_PYTHON
8319 {"pyeval", 1, 1, f_pyeval},
8320#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008321 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008322 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008323 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008324 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008325 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {"remote_expr", 2, 3, f_remote_expr},
8327 {"remote_foreground", 1, 1, f_remote_foreground},
8328 {"remote_peek", 1, 2, f_remote_peek},
8329 {"remote_read", 1, 1, f_remote_read},
8330 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008331 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008333 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008335 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008336#ifdef FEAT_FLOAT
8337 {"round", 1, 1, f_round},
8338#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008339 {"screenattr", 2, 2, f_screenattr},
8340 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008341 {"screencol", 0, 0, f_screencol},
8342 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008343 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008344 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008345 {"searchpair", 3, 7, f_searchpair},
8346 {"searchpairpos", 3, 7, f_searchpairpos},
8347 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"server2client", 2, 2, f_server2client},
8349 {"serverlist", 0, 0, f_serverlist},
8350 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008351 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"setcmdpos", 1, 1, f_setcmdpos},
8353 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008354 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008355 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008356 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008357 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008359 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008360 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008362#ifdef FEAT_CRYPT
8363 {"sha256", 1, 1, f_sha256},
8364#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008365 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008366 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008368#ifdef FEAT_FLOAT
8369 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008370 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008371#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008372 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008373 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008374 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008375 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008376 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008377#ifdef FEAT_FLOAT
8378 {"sqrt", 1, 1, f_sqrt},
8379 {"str2float", 1, 1, f_str2float},
8380#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008381 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008382 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008383 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384#ifdef HAVE_STRFTIME
8385 {"strftime", 1, 2, f_strftime},
8386#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008387 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008388 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {"strlen", 1, 1, f_strlen},
8390 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008391 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008393 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008394 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {"substitute", 4, 4, f_substitute},
8396 {"synID", 3, 3, f_synID},
8397 {"synIDattr", 2, 3, f_synIDattr},
8398 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008399 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008400 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008401 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008402 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008403 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008404 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008405 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008406 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008407 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008408#ifdef FEAT_FLOAT
8409 {"tan", 1, 1, f_tan},
8410 {"tanh", 1, 1, f_tanh},
8411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008413 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {"tolower", 1, 1, f_tolower},
8415 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008416 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008417#ifdef FEAT_FLOAT
8418 {"trunc", 1, 1, f_trunc},
8419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008421 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008422 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008423 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008424 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {"virtcol", 1, 1, f_virtcol},
8426 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008427 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {"winbufnr", 1, 1, f_winbufnr},
8429 {"wincol", 0, 0, f_wincol},
8430 {"winheight", 1, 1, f_winheight},
8431 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008432 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008434 {"winrestview", 1, 1, f_winrestview},
8435 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008437 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008438 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008439 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440};
8441
8442#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8443
8444/*
8445 * Function given to ExpandGeneric() to obtain the list of internal
8446 * or user defined function names.
8447 */
8448 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008449get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450{
8451 static int intidx = -1;
8452 char_u *name;
8453
8454 if (idx == 0)
8455 intidx = -1;
8456 if (intidx < 0)
8457 {
8458 name = get_user_func_name(xp, idx);
8459 if (name != NULL)
8460 return name;
8461 }
8462 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8463 {
8464 STRCPY(IObuff, functions[intidx].f_name);
8465 STRCAT(IObuff, "(");
8466 if (functions[intidx].f_max_argc == 0)
8467 STRCAT(IObuff, ")");
8468 return IObuff;
8469 }
8470
8471 return NULL;
8472}
8473
8474/*
8475 * Function given to ExpandGeneric() to obtain the list of internal or
8476 * user defined variable or function names.
8477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008479get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480{
8481 static int intidx = -1;
8482 char_u *name;
8483
8484 if (idx == 0)
8485 intidx = -1;
8486 if (intidx < 0)
8487 {
8488 name = get_function_name(xp, idx);
8489 if (name != NULL)
8490 return name;
8491 }
8492 return get_user_var_name(xp, ++intidx);
8493}
8494
8495#endif /* FEAT_CMDL_COMPL */
8496
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008497#if defined(EBCDIC) || defined(PROTO)
8498/*
8499 * Compare struct fst by function name.
8500 */
8501 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008502compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008503{
8504 struct fst *p1 = (struct fst *)s1;
8505 struct fst *p2 = (struct fst *)s2;
8506
8507 return STRCMP(p1->f_name, p2->f_name);
8508}
8509
8510/*
8511 * Sort the function table by function name.
8512 * The sorting of the table above is ASCII dependant.
8513 * On machines using EBCDIC we have to sort it.
8514 */
8515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008516sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008517{
8518 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8519
8520 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8521}
8522#endif
8523
8524
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525/*
8526 * Find internal function in table above.
8527 * Return index, or -1 if not found
8528 */
8529 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008530find_internal_func(
8531 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
8533 int first = 0;
8534 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8535 int cmp;
8536 int x;
8537
8538 /*
8539 * Find the function name in the table. Binary search.
8540 */
8541 while (first <= last)
8542 {
8543 x = first + ((unsigned)(last - first) >> 1);
8544 cmp = STRCMP(name, functions[x].f_name);
8545 if (cmp < 0)
8546 last = x - 1;
8547 else if (cmp > 0)
8548 first = x + 1;
8549 else
8550 return x;
8551 }
8552 return -1;
8553}
8554
8555/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008556 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8557 * name it contains, otherwise return "name".
8558 */
8559 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008560deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008561{
Bram Moolenaar33570922005-01-25 22:26:29 +00008562 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008563 int cc;
8564
8565 cc = name[*lenp];
8566 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008567 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008568 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008569 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008570 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008572 {
8573 *lenp = 0;
8574 return (char_u *)""; /* just in case */
8575 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008576 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008577 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008578 }
8579
8580 return name;
8581}
8582
8583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 * Allocate a variable for the result of a function.
8585 * Return OK or FAIL.
8586 */
8587 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008588get_func_tv(
8589 char_u *name, /* name of the function */
8590 int len, /* length of "name" */
8591 typval_T *rettv,
8592 char_u **arg, /* argument, pointing to the '(' */
8593 linenr_T firstline, /* first line of range */
8594 linenr_T lastline, /* last line of range */
8595 int *doesrange, /* return: function handled range */
8596 int evaluate,
8597 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598{
8599 char_u *argp;
8600 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008601 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 int argcount = 0; /* number of arguments found */
8603
8604 /*
8605 * Get the arguments.
8606 */
8607 argp = *arg;
8608 while (argcount < MAX_FUNC_ARGS)
8609 {
8610 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8611 if (*argp == ')' || *argp == ',' || *argp == NUL)
8612 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8614 {
8615 ret = FAIL;
8616 break;
8617 }
8618 ++argcount;
8619 if (*argp != ',')
8620 break;
8621 }
8622 if (*argp == ')')
8623 ++argp;
8624 else
8625 ret = FAIL;
8626
8627 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008628 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008629 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008631 {
8632 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008633 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008634 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008635 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637
8638 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640
8641 *arg = skipwhite(argp);
8642 return ret;
8643}
8644
8645
8646/*
8647 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008648 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008649 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008651 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008652call_func(
8653 char_u *funcname, /* name of the function */
8654 int len, /* length of "name" */
8655 typval_T *rettv, /* return value goes here */
8656 int argcount, /* number of "argvars" */
8657 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008658 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008659 linenr_T firstline, /* first line of range */
8660 linenr_T lastline, /* last line of range */
8661 int *doesrange, /* return: function handled range */
8662 int evaluate,
8663 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664{
8665 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666#define ERROR_UNKNOWN 0
8667#define ERROR_TOOMANY 1
8668#define ERROR_TOOFEW 2
8669#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008670#define ERROR_DICT 4
8671#define ERROR_NONE 5
8672#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 int error = ERROR_NONE;
8674 int i;
8675 int llen;
8676 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677#define FLEN_FIXED 40
8678 char_u fname_buf[FLEN_FIXED + 1];
8679 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008680 char_u *name;
8681
8682 /* Make a copy of the name, if it comes from a funcref variable it could
8683 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008684 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008685 if (name == NULL)
8686 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687
8688 /*
8689 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8690 * Change <SNR>123_name() to K_SNR 123_name().
8691 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 llen = eval_fname_script(name);
8694 if (llen > 0)
8695 {
8696 fname_buf[0] = K_SPECIAL;
8697 fname_buf[1] = KS_EXTRA;
8698 fname_buf[2] = (int)KE_SNR;
8699 i = 3;
8700 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8701 {
8702 if (current_SID <= 0)
8703 error = ERROR_SCRIPT;
8704 else
8705 {
8706 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8707 i = (int)STRLEN(fname_buf);
8708 }
8709 }
8710 if (i + STRLEN(name + llen) < FLEN_FIXED)
8711 {
8712 STRCPY(fname_buf + i, name + llen);
8713 fname = fname_buf;
8714 }
8715 else
8716 {
8717 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8718 if (fname == NULL)
8719 error = ERROR_OTHER;
8720 else
8721 {
8722 mch_memmove(fname, fname_buf, (size_t)i);
8723 STRCPY(fname + i, name + llen);
8724 }
8725 }
8726 }
8727 else
8728 fname = name;
8729
8730 *doesrange = FALSE;
8731
8732
8733 /* execute the function if no errors detected and executing */
8734 if (evaluate && error == ERROR_NONE)
8735 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008736 char_u *rfname = fname;
8737
8738 /* Ignore "g:" before a function name. */
8739 if (fname[0] == 'g' && fname[1] == ':')
8740 rfname = fname + 2;
8741
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008742 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8743 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 error = ERROR_UNKNOWN;
8745
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008746 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 {
8748 /*
8749 * User defined function.
8750 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008751 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008752
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008754 /* Trigger FuncUndefined event, may load the function. */
8755 if (fp == NULL
8756 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008757 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008758 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008760 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008761 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 }
8763#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008764 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008765 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008766 {
8767 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008768 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008769 }
8770
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 if (fp != NULL)
8772 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008773 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008775 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008777 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008779 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008780 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 else
8782 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008783 int did_save_redo = FALSE;
8784
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 /*
8786 * Call the user function.
8787 * Save and restore search patterns, script variables and
8788 * redo buffer.
8789 */
8790 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008791#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008792 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008793#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008794 {
8795 saveRedobuff();
8796 did_save_redo = TRUE;
8797 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008798 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008800 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008801 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8802 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8803 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008804 /* Function was unreferenced while being used, free it
8805 * now. */
8806 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008807 if (did_save_redo)
8808 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 restore_search_patterns();
8810 error = ERROR_NONE;
8811 }
8812 }
8813 }
8814 else
8815 {
8816 /*
8817 * Find the function name in the table, call its implementation.
8818 */
8819 i = find_internal_func(fname);
8820 if (i >= 0)
8821 {
8822 if (argcount < functions[i].f_min_argc)
8823 error = ERROR_TOOFEW;
8824 else if (argcount > functions[i].f_max_argc)
8825 error = ERROR_TOOMANY;
8826 else
8827 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008828 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008829 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 error = ERROR_NONE;
8831 }
8832 }
8833 }
8834 /*
8835 * The function call (or "FuncUndefined" autocommand sequence) might
8836 * have been aborted by an error, an interrupt, or an explicitly thrown
8837 * exception that has not been caught so far. This situation can be
8838 * tested for by calling aborting(). For an error in an internal
8839 * function or for the "E132" error in call_user_func(), however, the
8840 * throw point at which the "force_abort" flag (temporarily reset by
8841 * emsg()) is normally updated has not been reached yet. We need to
8842 * update that flag first to make aborting() reliable.
8843 */
8844 update_force_abort();
8845 }
8846 if (error == ERROR_NONE)
8847 ret = OK;
8848
8849 /*
8850 * Report an error unless the argument evaluation or function call has been
8851 * cancelled due to an aborting error, an interrupt, or an exception.
8852 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008853 if (!aborting())
8854 {
8855 switch (error)
8856 {
8857 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008858 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008859 break;
8860 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008861 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008862 break;
8863 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008864 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008865 name);
8866 break;
8867 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008868 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008869 name);
8870 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008871 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008872 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008873 name);
8874 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008875 }
8876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 if (fname != name && fname != fname_buf)
8879 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008880 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
8882 return ret;
8883}
8884
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008885/*
8886 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008887 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008888 */
8889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008890emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008891{
8892 char_u *p;
8893
8894 if (*name == K_SPECIAL)
8895 p = concat_str((char_u *)"<SNR>", name + 3);
8896 else
8897 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008898 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008899 if (p != name)
8900 vim_free(p);
8901}
8902
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008903/*
8904 * Return TRUE for a non-zero Number and a non-empty String.
8905 */
8906 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008907non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008908{
8909 return ((argvars[0].v_type == VAR_NUMBER
8910 && argvars[0].vval.v_number != 0)
8911 || (argvars[0].v_type == VAR_STRING
8912 && argvars[0].vval.v_string != NULL
8913 && *argvars[0].vval.v_string != NUL));
8914}
8915
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916/*********************************************
8917 * Implementation of the built-in functions
8918 */
8919
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008920#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008921static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008922
8923/*
8924 * Get the float value of "argvars[0]" into "f".
8925 * Returns FAIL when the argument is not a Number or Float.
8926 */
8927 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008928get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008929{
8930 if (argvars[0].v_type == VAR_FLOAT)
8931 {
8932 *f = argvars[0].vval.v_float;
8933 return OK;
8934 }
8935 if (argvars[0].v_type == VAR_NUMBER)
8936 {
8937 *f = (float_T)argvars[0].vval.v_number;
8938 return OK;
8939 }
8940 EMSG(_("E808: Number or Float required"));
8941 return FAIL;
8942}
8943
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008944/*
8945 * "abs(expr)" function
8946 */
8947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008948f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008949{
8950 if (argvars[0].v_type == VAR_FLOAT)
8951 {
8952 rettv->v_type = VAR_FLOAT;
8953 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8954 }
8955 else
8956 {
8957 varnumber_T n;
8958 int error = FALSE;
8959
8960 n = get_tv_number_chk(&argvars[0], &error);
8961 if (error)
8962 rettv->vval.v_number = -1;
8963 else if (n > 0)
8964 rettv->vval.v_number = n;
8965 else
8966 rettv->vval.v_number = -n;
8967 }
8968}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008969
8970/*
8971 * "acos()" function
8972 */
8973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008974f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008975{
8976 float_T f;
8977
8978 rettv->v_type = VAR_FLOAT;
8979 if (get_float_arg(argvars, &f) == OK)
8980 rettv->vval.v_float = acos(f);
8981 else
8982 rettv->vval.v_float = 0.0;
8983}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008984#endif
8985
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008987 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 */
8989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008990f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991{
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008995 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008997 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008998 && !tv_check_lock(l->lv_lock,
8999 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009000 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009002 }
9003 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009004 EMSG(_(e_listreq));
9005}
9006
9007/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009008 * "alloc_fail(id, countdown, repeat)" function
9009 */
9010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009011f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009012{
9013 if (argvars[0].v_type != VAR_NUMBER
9014 || argvars[0].vval.v_number <= 0
9015 || argvars[1].v_type != VAR_NUMBER
9016 || argvars[1].vval.v_number < 0
9017 || argvars[2].v_type != VAR_NUMBER)
9018 EMSG(_(e_invarg));
9019 else
9020 {
9021 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009022 if (alloc_fail_id >= aid_last)
9023 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009024 alloc_fail_countdown = argvars[1].vval.v_number;
9025 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009026 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009027 }
9028}
9029
9030/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009031 * "and(expr, expr)" function
9032 */
9033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009034f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009035{
9036 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9037 & get_tv_number_chk(&argvars[1], NULL);
9038}
9039
9040/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009041 * "append(lnum, string/list)" function
9042 */
9043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009044f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009045{
9046 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009047 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009048 list_T *l = NULL;
9049 listitem_T *li = NULL;
9050 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009051 long added = 0;
9052
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009053 /* When coming here from Insert mode, sync undo, so that this can be
9054 * undone separately from what was previously inserted. */
9055 if (u_sync_once == 2)
9056 {
9057 u_sync_once = 1; /* notify that u_sync() was called */
9058 u_sync(TRUE);
9059 }
9060
Bram Moolenaar0d660222005-01-07 21:51:51 +00009061 lnum = get_tv_lnum(argvars);
9062 if (lnum >= 0
9063 && lnum <= curbuf->b_ml.ml_line_count
9064 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009065 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009066 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009067 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009068 l = argvars[1].vval.v_list;
9069 if (l == NULL)
9070 return;
9071 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009072 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009073 for (;;)
9074 {
9075 if (l == NULL)
9076 tv = &argvars[1]; /* append a string */
9077 else if (li == NULL)
9078 break; /* end of list */
9079 else
9080 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009081 line = get_tv_string_chk(tv);
9082 if (line == NULL) /* type error */
9083 {
9084 rettv->vval.v_number = 1; /* Failed */
9085 break;
9086 }
9087 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009088 ++added;
9089 if (l == NULL)
9090 break;
9091 li = li->li_next;
9092 }
9093
9094 appended_lines_mark(lnum, added);
9095 if (curwin->w_cursor.lnum > lnum)
9096 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009098 else
9099 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100}
9101
9102/*
9103 * "argc()" function
9104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009106f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109}
9110
9111/*
9112 * "argidx()" function
9113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009115f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009117 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118}
9119
9120/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009121 * "arglistid()" function
9122 */
9123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009124f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009125{
9126 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009127
9128 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009129 wp = find_tabwin(&argvars[0], &argvars[1]);
9130 if (wp != NULL)
9131 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009132}
9133
9134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 * "argv(nr)" function
9136 */
9137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009138f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139{
9140 int idx;
9141
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009142 if (argvars[0].v_type != VAR_UNKNOWN)
9143 {
9144 idx = get_tv_number_chk(&argvars[0], NULL);
9145 if (idx >= 0 && idx < ARGCOUNT)
9146 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9147 else
9148 rettv->vval.v_string = NULL;
9149 rettv->v_type = VAR_STRING;
9150 }
9151 else if (rettv_list_alloc(rettv) == OK)
9152 for (idx = 0; idx < ARGCOUNT; ++idx)
9153 list_append_string(rettv->vval.v_list,
9154 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155}
9156
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009157static void prepare_assert_error(garray_T*gap);
9158static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9159static void assert_error(garray_T *gap);
9160static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009161
9162/*
9163 * Prepare "gap" for an assert error and add the sourcing position.
9164 */
9165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009166prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009167{
9168 char buf[NUMBUFLEN];
9169
9170 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009171 if (sourcing_name != NULL)
9172 {
9173 ga_concat(gap, sourcing_name);
9174 if (sourcing_lnum > 0)
9175 ga_concat(gap, (char_u *)" ");
9176 }
9177 if (sourcing_lnum > 0)
9178 {
9179 sprintf(buf, "line %ld", (long)sourcing_lnum);
9180 ga_concat(gap, (char_u *)buf);
9181 }
9182 if (sourcing_name != NULL || sourcing_lnum > 0)
9183 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009184}
9185
9186/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009187 * Append "str" to "gap", escaping unprintable characters.
9188 * Changes NL to \n, CR to \r, etc.
9189 */
9190 static void
9191ga_concat_esc(garray_T *gap, char_u *str)
9192{
9193 char_u *p;
9194 char_u buf[NUMBUFLEN];
9195
9196 for (p = str; *p != NUL; ++p)
9197 switch (*p)
9198 {
9199 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9200 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9201 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9202 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9203 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9204 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9205 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9206 default:
9207 if (*p < ' ')
9208 {
9209 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9210 ga_concat(gap, buf);
9211 }
9212 else
9213 ga_append(gap, *p);
9214 break;
9215 }
9216}
9217
9218/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009219 * Fill "gap" with information about an assert error.
9220 */
9221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009222fill_assert_error(
9223 garray_T *gap,
9224 typval_T *opt_msg_tv,
9225 char_u *exp_str,
9226 typval_T *exp_tv,
9227 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009228{
9229 char_u numbuf[NUMBUFLEN];
9230 char_u *tofree;
9231
9232 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9233 {
9234 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9235 vim_free(tofree);
9236 }
9237 else
9238 {
9239 ga_concat(gap, (char_u *)"Expected ");
9240 if (exp_str == NULL)
9241 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009242 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009243 vim_free(tofree);
9244 }
9245 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009246 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009247 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009248 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009249 vim_free(tofree);
9250 }
9251}
Bram Moolenaar43345542015-11-29 17:35:35 +01009252
9253/*
9254 * Add an assert error to v:errors.
9255 */
9256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009257assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009258{
9259 struct vimvar *vp = &vimvars[VV_ERRORS];
9260
9261 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9262 /* Make sure v:errors is a list. */
9263 set_vim_var_list(VV_ERRORS, list_alloc());
9264 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9265}
9266
Bram Moolenaar43345542015-11-29 17:35:35 +01009267/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009268 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009269 */
9270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009271f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009272{
9273 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009274
9275 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9276 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009277 prepare_assert_error(&ga);
9278 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9279 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009280 ga_clear(&ga);
9281 }
9282}
9283
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009284/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009285 * "assert_exception(string[, msg])" function
9286 */
9287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009288f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009289{
9290 garray_T ga;
9291 char *error;
9292
9293 error = (char *)get_tv_string_chk(&argvars[0]);
9294 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9295 {
9296 prepare_assert_error(&ga);
9297 ga_concat(&ga, (char_u *)"v:exception is not set");
9298 assert_error(&ga);
9299 ga_clear(&ga);
9300 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009301 else if (error != NULL
9302 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009303 {
9304 prepare_assert_error(&ga);
9305 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9306 &vimvars[VV_EXCEPTION].vv_tv);
9307 assert_error(&ga);
9308 ga_clear(&ga);
9309 }
9310}
9311
9312/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009313 * "assert_fails(cmd [, error])" function
9314 */
9315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009316f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009317{
9318 char_u *cmd = get_tv_string_chk(&argvars[0]);
9319 garray_T ga;
9320
9321 called_emsg = FALSE;
9322 suppress_errthrow = TRUE;
9323 emsg_silent = TRUE;
9324 do_cmdline_cmd(cmd);
9325 if (!called_emsg)
9326 {
9327 prepare_assert_error(&ga);
9328 ga_concat(&ga, (char_u *)"command did not fail: ");
9329 ga_concat(&ga, cmd);
9330 assert_error(&ga);
9331 ga_clear(&ga);
9332 }
9333 else if (argvars[1].v_type != VAR_UNKNOWN)
9334 {
9335 char_u buf[NUMBUFLEN];
9336 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9337
9338 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9339 {
9340 prepare_assert_error(&ga);
9341 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9342 &vimvars[VV_ERRMSG].vv_tv);
9343 assert_error(&ga);
9344 ga_clear(&ga);
9345 }
9346 }
9347
9348 called_emsg = FALSE;
9349 suppress_errthrow = FALSE;
9350 emsg_silent = FALSE;
9351 emsg_on_display = FALSE;
9352 set_vim_var_string(VV_ERRMSG, NULL, 0);
9353}
9354
9355/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009356 * Common for assert_true() and assert_false().
9357 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009359assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009360{
9361 int error = FALSE;
9362 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009363
Bram Moolenaar37127922016-02-06 20:29:28 +01009364 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009365 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009366 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009367 if (argvars[0].v_type != VAR_NUMBER
9368 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9369 || error)
9370 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009371 prepare_assert_error(&ga);
9372 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009373 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009374 NULL, &argvars[0]);
9375 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009376 ga_clear(&ga);
9377 }
9378}
9379
9380/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009381 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009382 */
9383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009384f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009385{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009386 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009387}
9388
9389/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009390 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009391 */
9392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009393f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009394{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009395 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009396}
9397
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009398#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009399/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009400 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009401 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009403f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009404{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009405 float_T f;
9406
9407 rettv->v_type = VAR_FLOAT;
9408 if (get_float_arg(argvars, &f) == OK)
9409 rettv->vval.v_float = asin(f);
9410 else
9411 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009412}
9413
9414/*
9415 * "atan()" function
9416 */
9417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009418f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009419{
9420 float_T f;
9421
9422 rettv->v_type = VAR_FLOAT;
9423 if (get_float_arg(argvars, &f) == OK)
9424 rettv->vval.v_float = atan(f);
9425 else
9426 rettv->vval.v_float = 0.0;
9427}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009428
9429/*
9430 * "atan2()" function
9431 */
9432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009433f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009434{
9435 float_T fx, fy;
9436
9437 rettv->v_type = VAR_FLOAT;
9438 if (get_float_arg(argvars, &fx) == OK
9439 && get_float_arg(&argvars[1], &fy) == OK)
9440 rettv->vval.v_float = atan2(fx, fy);
9441 else
9442 rettv->vval.v_float = 0.0;
9443}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009444#endif
9445
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446/*
9447 * "browse(save, title, initdir, default)" function
9448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009450f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
9452#ifdef FEAT_BROWSE
9453 int save;
9454 char_u *title;
9455 char_u *initdir;
9456 char_u *defname;
9457 char_u buf[NUMBUFLEN];
9458 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009459 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009461 save = get_tv_number_chk(&argvars[0], &error);
9462 title = get_tv_string_chk(&argvars[1]);
9463 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9464 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009466 if (error || title == NULL || initdir == NULL || defname == NULL)
9467 rettv->vval.v_string = NULL;
9468 else
9469 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009470 do_browse(save ? BROWSE_SAVE : 0,
9471 title, defname, NULL, initdir, NULL, curbuf);
9472#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009473 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009474#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009475 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009476}
9477
9478/*
9479 * "browsedir(title, initdir)" function
9480 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009482f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009483{
9484#ifdef FEAT_BROWSE
9485 char_u *title;
9486 char_u *initdir;
9487 char_u buf[NUMBUFLEN];
9488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009489 title = get_tv_string_chk(&argvars[0]);
9490 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009492 if (title == NULL || initdir == NULL)
9493 rettv->vval.v_string = NULL;
9494 else
9495 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009496 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009500 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501}
9502
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009503static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009504
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505/*
9506 * Find a buffer by number or exact name.
9507 */
9508 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009509find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510{
9511 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009513 if (avar->v_type == VAR_NUMBER)
9514 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009515 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009517 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009518 if (buf == NULL)
9519 {
9520 /* No full path name match, try a match with a URL or a "nofile"
9521 * buffer, these don't use the full path. */
9522 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9523 if (buf->b_fname != NULL
9524 && (path_with_url(buf->b_fname)
9525#ifdef FEAT_QUICKFIX
9526 || bt_nofile(buf)
9527#endif
9528 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009529 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009530 break;
9531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 }
9533 return buf;
9534}
9535
9536/*
9537 * "bufexists(expr)" function
9538 */
9539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009540f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009542 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543}
9544
9545/*
9546 * "buflisted(expr)" function
9547 */
9548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009549f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550{
9551 buf_T *buf;
9552
9553 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555}
9556
9557/*
9558 * "bufloaded(expr)" function
9559 */
9560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009561f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562{
9563 buf_T *buf;
9564
9565 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567}
9568
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009569static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009570
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571/*
9572 * Get buffer by number or pattern.
9573 */
9574 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009575get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 int save_magic;
9579 char_u *save_cpo;
9580 buf_T *buf;
9581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009582 if (tv->v_type == VAR_NUMBER)
9583 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009584 if (tv->v_type != VAR_STRING)
9585 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 if (name == NULL || *name == NUL)
9587 return curbuf;
9588 if (name[0] == '$' && name[1] == NUL)
9589 return lastbuf;
9590
9591 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9592 save_magic = p_magic;
9593 p_magic = TRUE;
9594 save_cpo = p_cpo;
9595 p_cpo = (char_u *)"";
9596
9597 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009598 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599
9600 p_magic = save_magic;
9601 p_cpo = save_cpo;
9602
9603 /* If not found, try expanding the name, like done for bufexists(). */
9604 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606
9607 return buf;
9608}
9609
9610/*
9611 * "bufname(expr)" function
9612 */
9613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009614f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615{
9616 buf_T *buf;
9617
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009618 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009620 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009621 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 --emsg_off;
9627}
9628
9629/*
9630 * "bufnr(expr)" function
9631 */
9632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009633f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
9635 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009636 int error = FALSE;
9637 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009639 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009641 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009642 --emsg_off;
9643
9644 /* If the buffer isn't found and the second argument is not zero create a
9645 * new buffer. */
9646 if (buf == NULL
9647 && argvars[1].v_type != VAR_UNKNOWN
9648 && get_tv_number_chk(&argvars[1], &error) != 0
9649 && !error
9650 && (name = get_tv_string_chk(&argvars[0])) != NULL
9651 && !error)
9652 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9653
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009657 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658}
9659
9660/*
9661 * "bufwinnr(nr)" function
9662 */
9663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009664f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665{
9666#ifdef FEAT_WINDOWS
9667 win_T *wp;
9668 int winnr = 0;
9669#endif
9670 buf_T *buf;
9671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009672 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009674 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675#ifdef FEAT_WINDOWS
9676 for (wp = firstwin; wp; wp = wp->w_next)
9677 {
9678 ++winnr;
9679 if (wp->w_buffer == buf)
9680 break;
9681 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009682 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685#endif
9686 --emsg_off;
9687}
9688
9689/*
9690 * "byte2line(byte)" function
9691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009693f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694{
9695#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697#else
9698 long boff = 0;
9699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009700 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 (linenr_T)0, &boff);
9706#endif
9707}
9708
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009710byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009711{
9712#ifdef FEAT_MBYTE
9713 char_u *t;
9714#endif
9715 char_u *str;
9716 long idx;
9717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009718 str = get_tv_string_chk(&argvars[0]);
9719 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009721 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009722 return;
9723
9724#ifdef FEAT_MBYTE
9725 t = str;
9726 for ( ; idx > 0; idx--)
9727 {
9728 if (*t == NUL) /* EOL reached */
9729 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009730 if (enc_utf8 && comp)
9731 t += utf_ptr2len(t);
9732 else
9733 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009734 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009735 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009736#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009737 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009739#endif
9740}
9741
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009742/*
9743 * "byteidx()" function
9744 */
9745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009746f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009747{
9748 byteidx(argvars, rettv, FALSE);
9749}
9750
9751/*
9752 * "byteidxcomp()" function
9753 */
9754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009755f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009756{
9757 byteidx(argvars, rettv, TRUE);
9758}
9759
Bram Moolenaardb913952012-06-29 12:54:53 +02009760 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009761func_call(
9762 char_u *name,
9763 typval_T *args,
9764 dict_T *selfdict,
9765 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009766{
9767 listitem_T *item;
9768 typval_T argv[MAX_FUNC_ARGS + 1];
9769 int argc = 0;
9770 int dummy;
9771 int r = 0;
9772
9773 for (item = args->vval.v_list->lv_first; item != NULL;
9774 item = item->li_next)
9775 {
9776 if (argc == MAX_FUNC_ARGS)
9777 {
9778 EMSG(_("E699: Too many arguments"));
9779 break;
9780 }
9781 /* Make a copy of each argument. This is needed to be able to set
9782 * v_lock to VAR_FIXED in the copy without changing the original list.
9783 */
9784 copy_tv(&item->li_tv, &argv[argc++]);
9785 }
9786
9787 if (item == NULL)
9788 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9789 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9790 &dummy, TRUE, selfdict);
9791
9792 /* Free the arguments. */
9793 while (argc > 0)
9794 clear_tv(&argv[--argc]);
9795
9796 return r;
9797}
9798
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009799/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009800 * "call(func, arglist)" function
9801 */
9802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009803f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009804{
9805 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009806 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009807
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009808 if (argvars[1].v_type != VAR_LIST)
9809 {
9810 EMSG(_(e_listreq));
9811 return;
9812 }
9813 if (argvars[1].vval.v_list == NULL)
9814 return;
9815
9816 if (argvars[0].v_type == VAR_FUNC)
9817 func = argvars[0].vval.v_string;
9818 else
9819 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009820 if (*func == NUL)
9821 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009822
Bram Moolenaare9a41262005-01-15 22:18:47 +00009823 if (argvars[2].v_type != VAR_UNKNOWN)
9824 {
9825 if (argvars[2].v_type != VAR_DICT)
9826 {
9827 EMSG(_(e_dictreq));
9828 return;
9829 }
9830 selfdict = argvars[2].vval.v_dict;
9831 }
9832
Bram Moolenaardb913952012-06-29 12:54:53 +02009833 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009834}
9835
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009836#ifdef FEAT_FLOAT
9837/*
9838 * "ceil({float})" function
9839 */
9840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009841f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009842{
9843 float_T f;
9844
9845 rettv->v_type = VAR_FLOAT;
9846 if (get_float_arg(argvars, &f) == OK)
9847 rettv->vval.v_float = ceil(f);
9848 else
9849 rettv->vval.v_float = 0.0;
9850}
9851#endif
9852
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009853#ifdef FEAT_CHANNEL
9854/*
Bram Moolenaar77073442016-02-13 23:23:53 +01009855 * Get the channel from the argument.
9856 * Returns NULL if the handle is invalid.
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009857 */
Bram Moolenaar77073442016-02-13 23:23:53 +01009858 static channel_T *
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009859get_channel_arg(typval_T *tv)
9860{
Bram Moolenaar77073442016-02-13 23:23:53 +01009861 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009862
Bram Moolenaar77073442016-02-13 23:23:53 +01009863 if (tv->v_type != VAR_CHANNEL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009864 {
9865 EMSG2(_(e_invarg2), get_tv_string(tv));
Bram Moolenaar77073442016-02-13 23:23:53 +01009866 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009867 }
Bram Moolenaar77073442016-02-13 23:23:53 +01009868 channel = tv->vval.v_channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009869
Bram Moolenaar77073442016-02-13 23:23:53 +01009870 if (channel == NULL || !channel_is_open(channel))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009871 {
Bram Moolenaar77073442016-02-13 23:23:53 +01009872 EMSG(_("E906: not an open channel"));
9873 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009874 }
Bram Moolenaar77073442016-02-13 23:23:53 +01009875 return channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009876}
9877
9878/*
9879 * "ch_close()" function
9880 */
9881 static void
9882f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9883{
Bram Moolenaar77073442016-02-13 23:23:53 +01009884 channel_T *channel = get_channel_arg(&argvars[0]);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009885
Bram Moolenaar77073442016-02-13 23:23:53 +01009886 if (channel != NULL)
9887 channel_close(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009888}
9889
9890/*
9891 * Get a callback from "arg". It can be a Funcref or a function name.
9892 * When "arg" is zero return an empty string.
9893 * Return NULL for an invalid argument.
9894 */
9895 static char_u *
9896get_callback(typval_T *arg)
9897{
9898 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9899 return arg->vval.v_string;
9900 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9901 return (char_u *)"";
9902 EMSG(_("E999: Invalid callback argument"));
9903 return NULL;
9904}
9905
9906/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +01009907 * "ch_logfile()" function
9908 */
9909 static void
9910f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
9911{
9912 char_u *fname;
9913 char_u *opt = (char_u *)"";
9914 char_u buf[NUMBUFLEN];
9915 FILE *file = NULL;
9916
9917 fname = get_tv_string(&argvars[0]);
9918 if (argvars[1].v_type == VAR_STRING)
9919 opt = get_tv_string_buf(&argvars[1], buf);
9920 if (*fname != NUL)
9921 {
9922 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
9923 if (file == NULL)
9924 {
9925 EMSG2(_(e_notopen), fname);
9926 return;
9927 }
9928 }
9929 ch_logfile(file);
9930}
9931
9932/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009933 * "ch_open()" function
9934 */
9935 static void
9936f_ch_open(typval_T *argvars, typval_T *rettv)
9937{
9938 char_u *address;
9939 char_u *mode;
9940 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009941 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009942 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009943 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009944 int waittime = 0;
9945 int timeout = 2000;
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009946 ch_mode_T ch_mode = MODE_JSON;
Bram Moolenaar77073442016-02-13 23:23:53 +01009947 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009948
9949 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +01009950 rettv->v_type = VAR_CHANNEL;
9951 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009952
9953 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009954 if (argvars[1].v_type != VAR_UNKNOWN
9955 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009956 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009957 EMSG(_(e_invarg));
9958 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009959 }
9960
9961 /* parse address */
9962 p = vim_strchr(address, ':');
9963 if (p == NULL)
9964 {
9965 EMSG2(_(e_invarg2), address);
9966 return;
9967 }
9968 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009969 port = strtol((char *)p, &rest, 10);
9970 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009971 {
9972 p[-1] = ':';
9973 EMSG2(_(e_invarg2), address);
9974 return;
9975 }
9976
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009977 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009978 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009979 dict_T *dict = argvars[1].vval.v_dict;
9980 dictitem_T *item;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009981
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009982 /* parse argdict */
9983 if ((item = dict_find(dict, (char_u *)"mode", -1)) != NULL)
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009984 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009985 mode = get_tv_string(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009986 if (STRCMP(mode, "raw") == 0)
Bram Moolenaar595e64e2016-02-07 19:19:53 +01009987 ch_mode = MODE_RAW;
9988 else if (STRCMP(mode, "js") == 0)
9989 ch_mode = MODE_JS;
9990 else if (STRCMP(mode, "json") == 0)
9991 ch_mode = MODE_JSON;
9992 else
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009993 {
9994 EMSG2(_(e_invarg2), mode);
9995 return;
9996 }
9997 }
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +01009998 if ((item = dict_find(dict, (char_u *)"waittime", -1)) != NULL)
9999 waittime = get_tv_number(&item->di_tv);
10000 if ((item = dict_find(dict, (char_u *)"timeout", -1)) != NULL)
10001 timeout = get_tv_number(&item->di_tv);
10002 if ((item = dict_find(dict, (char_u *)"callback", -1)) != NULL)
10003 callback = get_callback(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010004 }
10005 if (waittime < 0 || timeout < 0)
10006 {
10007 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010008 return;
10009 }
10010
Bram Moolenaar77073442016-02-13 23:23:53 +010010011 channel = channel_open((char *)address, port, waittime, NULL);
10012 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010013 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010014 rettv->vval.v_channel = channel;
Bram Moolenaar77073442016-02-13 23:23:53 +010010015 channel_set_json_mode(channel, ch_mode);
10016 channel_set_timeout(channel, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010017 if (callback != NULL && *callback != NUL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010018 channel_set_callback(channel, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010019 }
10020}
10021
10022/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010023 * "ch_readraw()" function
10024 */
10025 static void
10026f_ch_readraw(typval_T *argvars, typval_T *rettv)
10027{
Bram Moolenaar77073442016-02-13 23:23:53 +010010028 channel_T *channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010029
10030 /* return an empty string by default */
10031 rettv->v_type = VAR_STRING;
10032 rettv->vval.v_string = NULL;
10033
Bram Moolenaar77073442016-02-13 23:23:53 +010010034 channel = get_channel_arg(&argvars[0]);
10035 if (channel != NULL)
10036 rettv->vval.v_string = channel_read_block(channel);
10037}
10038
10039/*
10040 * "ch_status()" function
10041 */
10042 static void
10043f_ch_status(typval_T *argvars, typval_T *rettv)
10044{
10045 /* return an empty string by default */
10046 rettv->v_type = VAR_STRING;
10047
10048 if (argvars[0].v_type != VAR_CHANNEL)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010049 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010050 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10051 rettv->vval.v_string = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010052 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010053 else
10054 rettv->vval.v_string = vim_strsave(
10055 (char_u *)channel_status(argvars[0].vval.v_channel));
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010056}
10057
10058/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010059 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010060 * Returns the channel if the caller should read the response.
10061 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010062 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010063 static channel_T *
Bram Moolenaara07fec92016-02-05 21:04:08 +010010064send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010065{
Bram Moolenaar77073442016-02-13 23:23:53 +010010066 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010067 char_u *callback = NULL;
10068
Bram Moolenaar77073442016-02-13 23:23:53 +010010069 channel = get_channel_arg(&argvars[0]);
10070 if (channel == NULL)
10071 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010072
10073 if (argvars[2].v_type != VAR_UNKNOWN)
10074 {
10075 callback = get_callback(&argvars[2]);
10076 if (callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010077 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010078 }
Bram Moolenaara07fec92016-02-05 21:04:08 +010010079 /* Set the callback. An empty callback means no callback and not reading
10080 * the response. */
10081 if (callback != NULL && *callback != NUL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010082 channel_set_req_callback(channel, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010083
Bram Moolenaar77073442016-02-13 23:23:53 +010010084 if (channel_send(channel, text, fun) == OK && callback == NULL)
10085 return channel;
10086 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010087}
10088
10089/*
10090 * "ch_sendexpr()" function
10091 */
10092 static void
10093f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10094{
10095 char_u *text;
10096 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010097 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010098 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010099 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010100
10101 /* return an empty string by default */
10102 rettv->v_type = VAR_STRING;
10103 rettv->vval.v_string = NULL;
10104
Bram Moolenaar77073442016-02-13 23:23:53 +010010105 channel = get_channel_arg(&argvars[0]);
10106 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010107 return;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010108
Bram Moolenaar77073442016-02-13 23:23:53 +010010109 ch_mode = channel_get_mode(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010110 if (ch_mode == MODE_RAW)
10111 {
10112 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
10113 return;
10114 }
10115
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010116 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010117 text = json_encode_nr_expr(id, &argvars[1],
10118 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010119 if (text == NULL)
10120 return;
10121
Bram Moolenaar77073442016-02-13 23:23:53 +010010122 channel = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010123 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010124 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010125 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010126 if (channel_read_json_block(channel, id, &listtv) == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010127 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010128 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010129
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010130 /* Move the item from the list and then change the type to
10131 * avoid the value being freed. */
10132 *rettv = list->lv_last->li_tv;
10133 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010134 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010135 }
10136 }
10137}
10138
10139/*
10140 * "ch_sendraw()" function
10141 */
10142 static void
10143f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10144{
10145 char_u buf[NUMBUFLEN];
10146 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010147 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010148
10149 /* return an empty string by default */
10150 rettv->v_type = VAR_STRING;
10151 rettv->vval.v_string = NULL;
10152
10153 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar77073442016-02-13 23:23:53 +010010154 channel = send_common(argvars, text, 0, "sendraw");
10155 if (channel != NULL)
10156 rettv->vval.v_string = channel_read_block(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010157}
10158#endif
10159
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010160/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010161 * "changenr()" function
10162 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010164f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010165{
10166 rettv->vval.v_number = curbuf->b_u_seq_cur;
10167}
10168
10169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 * "char2nr(string)" function
10171 */
10172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010173f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174{
10175#ifdef FEAT_MBYTE
10176 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010177 {
10178 int utf8 = 0;
10179
10180 if (argvars[1].v_type != VAR_UNKNOWN)
10181 utf8 = get_tv_number_chk(&argvars[1], NULL);
10182
10183 if (utf8)
10184 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10185 else
10186 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 else
10189#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010190 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191}
10192
10193/*
10194 * "cindent(lnum)" function
10195 */
10196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010197f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198{
10199#ifdef FEAT_CINDENT
10200 pos_T pos;
10201 linenr_T lnum;
10202
10203 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010204 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10206 {
10207 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010208 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 curwin->w_cursor = pos;
10210 }
10211 else
10212#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214}
10215
10216/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010217 * "clearmatches()" function
10218 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010220f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010221{
10222#ifdef FEAT_SEARCH_EXTRA
10223 clear_matches(curwin);
10224#endif
10225}
10226
10227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 * "col(string)" function
10229 */
10230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010231f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232{
10233 colnr_T col = 0;
10234 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010235 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010237 fp = var2fpos(&argvars[0], FALSE, &fnum);
10238 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 {
10240 if (fp->col == MAXCOL)
10241 {
10242 /* '> can be MAXCOL, get the length of the line then */
10243 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010244 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 else
10246 col = MAXCOL;
10247 }
10248 else
10249 {
10250 col = fp->col + 1;
10251#ifdef FEAT_VIRTUALEDIT
10252 /* col(".") when the cursor is on the NUL at the end of the line
10253 * because of "coladd" can be seen as an extra column. */
10254 if (virtual_active() && fp == &curwin->w_cursor)
10255 {
10256 char_u *p = ml_get_cursor();
10257
10258 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10259 curwin->w_virtcol - curwin->w_cursor.coladd))
10260 {
10261# ifdef FEAT_MBYTE
10262 int l;
10263
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010264 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 col += l;
10266# else
10267 if (*p != NUL && p[1] == NUL)
10268 ++col;
10269# endif
10270 }
10271 }
10272#endif
10273 }
10274 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276}
10277
Bram Moolenaar572cb562005-08-05 21:35:02 +000010278#if defined(FEAT_INS_EXPAND)
10279/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010280 * "complete()" function
10281 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010283f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010284{
10285 int startcol;
10286
10287 if ((State & INSERT) == 0)
10288 {
10289 EMSG(_("E785: complete() can only be used in Insert mode"));
10290 return;
10291 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010292
10293 /* Check for undo allowed here, because if something was already inserted
10294 * the line was already saved for undo and this check isn't done. */
10295 if (!undo_allowed())
10296 return;
10297
Bram Moolenaarade00832006-03-10 21:46:58 +000010298 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10299 {
10300 EMSG(_(e_invarg));
10301 return;
10302 }
10303
10304 startcol = get_tv_number_chk(&argvars[0], NULL);
10305 if (startcol <= 0)
10306 return;
10307
10308 set_completion(startcol - 1, argvars[1].vval.v_list);
10309}
10310
10311/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010312 * "complete_add()" function
10313 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010315f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010316{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010317 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010318}
10319
10320/*
10321 * "complete_check()" function
10322 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010324f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010325{
10326 int saved = RedrawingDisabled;
10327
10328 RedrawingDisabled = 0;
10329 ins_compl_check_keys(0);
10330 rettv->vval.v_number = compl_interrupted;
10331 RedrawingDisabled = saved;
10332}
10333#endif
10334
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335/*
10336 * "confirm(message, buttons[, default [, type]])" function
10337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010339f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340{
10341#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10342 char_u *message;
10343 char_u *buttons = NULL;
10344 char_u buf[NUMBUFLEN];
10345 char_u buf2[NUMBUFLEN];
10346 int def = 1;
10347 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010348 char_u *typestr;
10349 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010351 message = get_tv_string_chk(&argvars[0]);
10352 if (message == NULL)
10353 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010354 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010356 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10357 if (buttons == NULL)
10358 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010359 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010361 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010362 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010364 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10365 if (typestr == NULL)
10366 error = TRUE;
10367 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010369 switch (TOUPPER_ASC(*typestr))
10370 {
10371 case 'E': type = VIM_ERROR; break;
10372 case 'Q': type = VIM_QUESTION; break;
10373 case 'I': type = VIM_INFO; break;
10374 case 'W': type = VIM_WARNING; break;
10375 case 'G': type = VIM_GENERIC; break;
10376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 }
10378 }
10379 }
10380 }
10381
10382 if (buttons == NULL || *buttons == NUL)
10383 buttons = (char_u *)_("&Ok");
10384
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010385 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010386 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010387 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388#endif
10389}
10390
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010391/*
10392 * "copy()" function
10393 */
10394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010395f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010396{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010397 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010398}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010400#ifdef FEAT_FLOAT
10401/*
10402 * "cos()" function
10403 */
10404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010405f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010406{
10407 float_T f;
10408
10409 rettv->v_type = VAR_FLOAT;
10410 if (get_float_arg(argvars, &f) == OK)
10411 rettv->vval.v_float = cos(f);
10412 else
10413 rettv->vval.v_float = 0.0;
10414}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010415
10416/*
10417 * "cosh()" function
10418 */
10419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010420f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010421{
10422 float_T f;
10423
10424 rettv->v_type = VAR_FLOAT;
10425 if (get_float_arg(argvars, &f) == OK)
10426 rettv->vval.v_float = cosh(f);
10427 else
10428 rettv->vval.v_float = 0.0;
10429}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010430#endif
10431
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010433 * "count()" function
10434 */
10435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010436f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010437{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010438 long n = 0;
10439 int ic = FALSE;
10440
Bram Moolenaare9a41262005-01-15 22:18:47 +000010441 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010442 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010443 listitem_T *li;
10444 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010445 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010446
Bram Moolenaare9a41262005-01-15 22:18:47 +000010447 if ((l = argvars[0].vval.v_list) != NULL)
10448 {
10449 li = l->lv_first;
10450 if (argvars[2].v_type != VAR_UNKNOWN)
10451 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010452 int error = FALSE;
10453
10454 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010455 if (argvars[3].v_type != VAR_UNKNOWN)
10456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010457 idx = get_tv_number_chk(&argvars[3], &error);
10458 if (!error)
10459 {
10460 li = list_find(l, idx);
10461 if (li == NULL)
10462 EMSGN(_(e_listidx), idx);
10463 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010464 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010465 if (error)
10466 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010467 }
10468
10469 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010470 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010471 ++n;
10472 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010473 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010474 else if (argvars[0].v_type == VAR_DICT)
10475 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010476 int todo;
10477 dict_T *d;
10478 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010479
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010480 if ((d = argvars[0].vval.v_dict) != NULL)
10481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 int error = FALSE;
10483
Bram Moolenaare9a41262005-01-15 22:18:47 +000010484 if (argvars[2].v_type != VAR_UNKNOWN)
10485 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010486 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010487 if (argvars[3].v_type != VAR_UNKNOWN)
10488 EMSG(_(e_invarg));
10489 }
10490
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010491 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010492 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010493 {
10494 if (!HASHITEM_EMPTY(hi))
10495 {
10496 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010497 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010498 ++n;
10499 }
10500 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010501 }
10502 }
10503 else
10504 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010505 rettv->vval.v_number = n;
10506}
10507
10508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10510 *
10511 * Checks the existence of a cscope connection.
10512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010514f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515{
10516#ifdef FEAT_CSCOPE
10517 int num = 0;
10518 char_u *dbpath = NULL;
10519 char_u *prepend = NULL;
10520 char_u buf[NUMBUFLEN];
10521
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010522 if (argvars[0].v_type != VAR_UNKNOWN
10523 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010525 num = (int)get_tv_number(&argvars[0]);
10526 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010527 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 }
10530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532#endif
10533}
10534
10535/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010536 * "cursor(lnum, col)" function, or
10537 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010539 * Moves the cursor to the specified line and column.
10540 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010543f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544{
10545 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010546#ifdef FEAT_VIRTUALEDIT
10547 long coladd = 0;
10548#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010549 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010551 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010552 if (argvars[1].v_type == VAR_UNKNOWN)
10553 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010554 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010555 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010556
Bram Moolenaar493c1782014-05-28 14:34:46 +020010557 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010558 {
10559 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010560 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010561 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010562 line = pos.lnum;
10563 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010564#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010565 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010566#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010567 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010568 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010569 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010570 set_curswant = FALSE;
10571 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010572 }
10573 else
10574 {
10575 line = get_tv_lnum(argvars);
10576 col = get_tv_number_chk(&argvars[1], NULL);
10577#ifdef FEAT_VIRTUALEDIT
10578 if (argvars[2].v_type != VAR_UNKNOWN)
10579 coladd = get_tv_number_chk(&argvars[2], NULL);
10580#endif
10581 }
10582 if (line < 0 || col < 0
10583#ifdef FEAT_VIRTUALEDIT
10584 || coladd < 0
10585#endif
10586 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010587 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 if (line > 0)
10589 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 if (col > 0)
10591 curwin->w_cursor.col = col - 1;
10592#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010593 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594#endif
10595
10596 /* Make sure the cursor is in a valid position. */
10597 check_cursor();
10598#ifdef FEAT_MBYTE
10599 /* Correct cursor for multi-byte character. */
10600 if (has_mbyte)
10601 mb_adjust_cursor();
10602#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010603
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010604 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010605 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606}
10607
10608/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010609 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 */
10611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010612f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010614 int noref = 0;
10615
10616 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010618 if (noref < 0 || noref > 1)
10619 EMSG(_(e_invarg));
10620 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010621 {
10622 current_copyID += COPYID_INC;
10623 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625}
10626
10627/*
10628 * "delete()" function
10629 */
10630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010631f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010633 char_u nbuf[NUMBUFLEN];
10634 char_u *name;
10635 char_u *flags;
10636
10637 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010639 return;
10640
10641 name = get_tv_string(&argvars[0]);
10642 if (name == NULL || *name == NUL)
10643 {
10644 EMSG(_(e_invarg));
10645 return;
10646 }
10647
10648 if (argvars[1].v_type != VAR_UNKNOWN)
10649 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010651 flags = (char_u *)"";
10652
10653 if (*flags == NUL)
10654 /* delete a file */
10655 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10656 else if (STRCMP(flags, "d") == 0)
10657 /* delete an empty directory */
10658 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10659 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010660 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010661 rettv->vval.v_number = delete_recursive(name);
10662 else
10663 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664}
10665
10666/*
10667 * "did_filetype()" function
10668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010670f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671{
10672#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674#endif
10675}
10676
10677/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010678 * "diff_filler()" function
10679 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010681f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010682{
10683#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010685#endif
10686}
10687
10688/*
10689 * "diff_hlID()" function
10690 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010692f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010693{
10694#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010696 static linenr_T prev_lnum = 0;
10697 static int changedtick = 0;
10698 static int fnum = 0;
10699 static int change_start = 0;
10700 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010701 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010702 int filler_lines;
10703 int col;
10704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010705 if (lnum < 0) /* ignore type error in {lnum} arg */
10706 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010707 if (lnum != prev_lnum
10708 || changedtick != curbuf->b_changedtick
10709 || fnum != curbuf->b_fnum)
10710 {
10711 /* New line, buffer, change: need to get the values. */
10712 filler_lines = diff_check(curwin, lnum);
10713 if (filler_lines < 0)
10714 {
10715 if (filler_lines == -1)
10716 {
10717 change_start = MAXCOL;
10718 change_end = -1;
10719 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10720 hlID = HLF_ADD; /* added line */
10721 else
10722 hlID = HLF_CHD; /* changed line */
10723 }
10724 else
10725 hlID = HLF_ADD; /* added line */
10726 }
10727 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010728 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010729 prev_lnum = lnum;
10730 changedtick = curbuf->b_changedtick;
10731 fnum = curbuf->b_fnum;
10732 }
10733
10734 if (hlID == HLF_CHD || hlID == HLF_TXD)
10735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010736 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010737 if (col >= change_start && col <= change_end)
10738 hlID = HLF_TXD; /* changed text */
10739 else
10740 hlID = HLF_CHD; /* changed line */
10741 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010742 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010743#endif
10744}
10745
10746/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010747 * "disable_char_avail_for_testing({expr})" function
10748 */
10749 static void
10750f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10751{
10752 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10753}
10754
10755/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010756 * "empty({expr})" function
10757 */
10758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010759f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010760{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010761 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010762
10763 switch (argvars[0].v_type)
10764 {
10765 case VAR_STRING:
10766 case VAR_FUNC:
10767 n = argvars[0].vval.v_string == NULL
10768 || *argvars[0].vval.v_string == NUL;
10769 break;
10770 case VAR_NUMBER:
10771 n = argvars[0].vval.v_number == 0;
10772 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010773 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010774#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010775 n = argvars[0].vval.v_float == 0.0;
10776 break;
10777#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010778 case VAR_LIST:
10779 n = argvars[0].vval.v_list == NULL
10780 || argvars[0].vval.v_list->lv_first == NULL;
10781 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010782 case VAR_DICT:
10783 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010784 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010785 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010786 case VAR_SPECIAL:
10787 n = argvars[0].vval.v_number != VVAL_TRUE;
10788 break;
10789
Bram Moolenaar835dc632016-02-07 14:27:38 +010010790 case VAR_JOB:
10791#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010010792 n = argvars[0].vval.v_job == NULL
10793 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10794 break;
10795#endif
10796 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010010797#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010798 n = argvars[0].vval.v_channel == NULL
10799 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010800 break;
10801#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010802 case VAR_UNKNOWN:
10803 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10804 n = TRUE;
10805 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010806 }
10807
10808 rettv->vval.v_number = n;
10809}
10810
10811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 * "escape({string}, {chars})" function
10813 */
10814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010815f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816{
10817 char_u buf[NUMBUFLEN];
10818
Bram Moolenaar758711c2005-02-02 23:11:38 +000010819 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10820 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822}
10823
10824/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010825 * "eval()" function
10826 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010828f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010829{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010830 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010832 s = get_tv_string_chk(&argvars[0]);
10833 if (s != NULL)
10834 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010835
Bram Moolenaar615b9972015-01-14 17:15:05 +010010836 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010837 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10838 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010839 if (p != NULL && !aborting())
10840 EMSG2(_(e_invexpr2), p);
10841 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010843 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010844 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010845 else if (*s != NUL)
10846 EMSG(_(e_trailing));
10847}
10848
10849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 * "eventhandler()" function
10851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010853f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010855 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856}
10857
10858/*
10859 * "executable()" function
10860 */
10861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010862f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010864 char_u *name = get_tv_string(&argvars[0]);
10865
10866 /* Check in $PATH and also check directly if there is a directory name. */
10867 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10868 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010869}
10870
10871/*
10872 * "exepath()" function
10873 */
10874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010875f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010876{
10877 char_u *p = NULL;
10878
Bram Moolenaarb5971142015-03-21 17:32:19 +010010879 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010880 rettv->v_type = VAR_STRING;
10881 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882}
10883
10884/*
10885 * "exists()" function
10886 */
10887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010888f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889{
10890 char_u *p;
10891 char_u *name;
10892 int n = FALSE;
10893 int len = 0;
10894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 if (*p == '$') /* environment variable */
10897 {
10898 /* first try "normal" environment variables (fast) */
10899 if (mch_getenv(p + 1) != NULL)
10900 n = TRUE;
10901 else
10902 {
10903 /* try expanding things like $VIM and ${HOME} */
10904 p = expand_env_save(p);
10905 if (p != NULL && *p != '$')
10906 n = TRUE;
10907 vim_free(p);
10908 }
10909 }
10910 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010913 if (*skipwhite(p) != NUL)
10914 n = FALSE; /* trailing garbage */
10915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 else if (*p == '*') /* internal or user defined function */
10917 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010918 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 }
10920 else if (*p == ':')
10921 {
10922 n = cmd_exists(p + 1);
10923 }
10924 else if (*p == '#')
10925 {
10926#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010927 if (p[1] == '#')
10928 n = autocmd_supported(p + 2);
10929 else
10930 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931#endif
10932 }
10933 else /* internal variable */
10934 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010935 char_u *tofree;
10936 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010938 /* get_name_len() takes care of expanding curly braces */
10939 name = p;
10940 len = get_name_len(&p, &tofree, TRUE, FALSE);
10941 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010943 if (tofree != NULL)
10944 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010945 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010946 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010948 /* handle d.key, l[idx], f(expr) */
10949 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10950 if (n)
10951 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 }
10953 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010954 if (*p != NUL)
10955 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010957 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 }
10959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010960 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961}
10962
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010963#ifdef FEAT_FLOAT
10964/*
10965 * "exp()" function
10966 */
10967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010968f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010969{
10970 float_T f;
10971
10972 rettv->v_type = VAR_FLOAT;
10973 if (get_float_arg(argvars, &f) == OK)
10974 rettv->vval.v_float = exp(f);
10975 else
10976 rettv->vval.v_float = 0.0;
10977}
10978#endif
10979
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980/*
10981 * "expand()" function
10982 */
10983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010984f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985{
10986 char_u *s;
10987 int len;
10988 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010989 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010991 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010992 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010994 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010995 if (argvars[1].v_type != VAR_UNKNOWN
10996 && argvars[2].v_type != VAR_UNKNOWN
10997 && get_tv_number_chk(&argvars[2], &error)
10998 && !error)
10999 {
11000 rettv->v_type = VAR_LIST;
11001 rettv->vval.v_list = NULL;
11002 }
11003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 if (*s == '%' || *s == '#' || *s == '<')
11006 {
11007 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011008 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011010 if (rettv->v_type == VAR_LIST)
11011 {
11012 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11013 list_append_string(rettv->vval.v_list, result, -1);
11014 else
11015 vim_free(result);
11016 }
11017 else
11018 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 }
11020 else
11021 {
11022 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011023 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011024 if (argvars[1].v_type != VAR_UNKNOWN
11025 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011026 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011027 if (!error)
11028 {
11029 ExpandInit(&xpc);
11030 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011031 if (p_wic)
11032 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011033 if (rettv->v_type == VAR_STRING)
11034 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11035 options, WILD_ALL);
11036 else if (rettv_list_alloc(rettv) != FAIL)
11037 {
11038 int i;
11039
11040 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11041 for (i = 0; i < xpc.xp_numfiles; i++)
11042 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11043 ExpandCleanup(&xpc);
11044 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011045 }
11046 else
11047 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 }
11049}
11050
11051/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011052 * Go over all entries in "d2" and add them to "d1".
11053 * When "action" is "error" then a duplicate key is an error.
11054 * When "action" is "force" then a duplicate key is overwritten.
11055 * Otherwise duplicate keys are ignored ("action" is "keep").
11056 */
11057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011058dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011059{
11060 dictitem_T *di1;
11061 hashitem_T *hi2;
11062 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011063 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011064
11065 todo = (int)d2->dv_hashtab.ht_used;
11066 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11067 {
11068 if (!HASHITEM_EMPTY(hi2))
11069 {
11070 --todo;
11071 di1 = dict_find(d1, hi2->hi_key, -1);
11072 if (d1->dv_scope != 0)
11073 {
11074 /* Disallow replacing a builtin function in l: and g:.
11075 * Check the key to be valid when adding to any
11076 * scope. */
11077 if (d1->dv_scope == VAR_DEF_SCOPE
11078 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11079 && var_check_func_name(hi2->hi_key,
11080 di1 == NULL))
11081 break;
11082 if (!valid_varname(hi2->hi_key))
11083 break;
11084 }
11085 if (di1 == NULL)
11086 {
11087 di1 = dictitem_copy(HI2DI(hi2));
11088 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11089 dictitem_free(di1);
11090 }
11091 else if (*action == 'e')
11092 {
11093 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11094 break;
11095 }
11096 else if (*action == 'f' && HI2DI(hi2) != di1)
11097 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011098 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11099 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011100 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011101 clear_tv(&di1->di_tv);
11102 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11103 }
11104 }
11105 }
11106}
11107
11108/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011109 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011110 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011111 */
11112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011113f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011114{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011115 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011116
Bram Moolenaare9a41262005-01-15 22:18:47 +000011117 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011118 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011119 list_T *l1, *l2;
11120 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011121 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011122 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011123
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 l1 = argvars[0].vval.v_list;
11125 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011126 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011127 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011128 {
11129 if (argvars[2].v_type != VAR_UNKNOWN)
11130 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011131 before = get_tv_number_chk(&argvars[2], &error);
11132 if (error)
11133 return; /* type error; errmsg already given */
11134
Bram Moolenaar758711c2005-02-02 23:11:38 +000011135 if (before == l1->lv_len)
11136 item = NULL;
11137 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011138 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011139 item = list_find(l1, before);
11140 if (item == NULL)
11141 {
11142 EMSGN(_(e_listidx), before);
11143 return;
11144 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011145 }
11146 }
11147 else
11148 item = NULL;
11149 list_extend(l1, l2, item);
11150
Bram Moolenaare9a41262005-01-15 22:18:47 +000011151 copy_tv(&argvars[0], rettv);
11152 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011153 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011154 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11155 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011156 dict_T *d1, *d2;
11157 char_u *action;
11158 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011159
11160 d1 = argvars[0].vval.v_dict;
11161 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011162 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011163 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011164 {
11165 /* Check the third argument. */
11166 if (argvars[2].v_type != VAR_UNKNOWN)
11167 {
11168 static char *(av[]) = {"keep", "force", "error"};
11169
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011170 action = get_tv_string_chk(&argvars[2]);
11171 if (action == NULL)
11172 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011173 for (i = 0; i < 3; ++i)
11174 if (STRCMP(action, av[i]) == 0)
11175 break;
11176 if (i == 3)
11177 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011178 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011179 return;
11180 }
11181 }
11182 else
11183 action = (char_u *)"force";
11184
Bram Moolenaara9922d62013-05-30 13:01:18 +020011185 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011186
Bram Moolenaare9a41262005-01-15 22:18:47 +000011187 copy_tv(&argvars[0], rettv);
11188 }
11189 }
11190 else
11191 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011192}
11193
11194/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011195 * "feedkeys()" function
11196 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011198f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011199{
11200 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011201 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011202 char_u *keys, *flags;
11203 char_u nbuf[NUMBUFLEN];
11204 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011205 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011206 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011207
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011208 /* This is not allowed in the sandbox. If the commands would still be
11209 * executed in the sandbox it would be OK, but it probably happens later,
11210 * when "sandbox" is no longer set. */
11211 if (check_secure())
11212 return;
11213
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011214 keys = get_tv_string(&argvars[0]);
11215 if (*keys != NUL)
11216 {
11217 if (argvars[1].v_type != VAR_UNKNOWN)
11218 {
11219 flags = get_tv_string_buf(&argvars[1], nbuf);
11220 for ( ; *flags != NUL; ++flags)
11221 {
11222 switch (*flags)
11223 {
11224 case 'n': remap = FALSE; break;
11225 case 'm': remap = TRUE; break;
11226 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011227 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011228 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011229 }
11230 }
11231 }
11232
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011233 /* Need to escape K_SPECIAL and CSI before putting the string in the
11234 * typeahead buffer. */
11235 keys_esc = vim_strsave_escape_csi(keys);
11236 if (keys_esc != NULL)
11237 {
11238 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011239 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011240 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011241 if (vgetc_busy)
11242 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011243 if (execute)
11244 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011245 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011246 }
11247}
11248
11249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 * "filereadable()" function
11251 */
11252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011253f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011255 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 char_u *p;
11257 int n;
11258
Bram Moolenaarc236c162008-07-13 17:41:49 +000011259#ifndef O_NONBLOCK
11260# define O_NONBLOCK 0
11261#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011263 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11264 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 {
11266 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011267 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 }
11269 else
11270 n = FALSE;
11271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273}
11274
11275/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011276 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 * rights to write into.
11278 */
11279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011280f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011282 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283}
11284
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011285 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011286findfilendir(
11287 typval_T *argvars UNUSED,
11288 typval_T *rettv,
11289 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011290{
11291#ifdef FEAT_SEARCHPATH
11292 char_u *fname;
11293 char_u *fresult = NULL;
11294 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11295 char_u *p;
11296 char_u pathbuf[NUMBUFLEN];
11297 int count = 1;
11298 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011299 int error = FALSE;
11300#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011301
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011302 rettv->vval.v_string = NULL;
11303 rettv->v_type = VAR_STRING;
11304
11305#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011307
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011308 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011310 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11311 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011312 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011313 else
11314 {
11315 if (*p != NUL)
11316 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011319 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011321 }
11322
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011323 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11324 error = TRUE;
11325
11326 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011328 do
11329 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011330 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011331 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 fresult = find_file_in_path_option(first ? fname : NULL,
11333 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011334 0, first, path,
11335 find_what,
11336 curbuf->b_ffname,
11337 find_what == FINDFILE_DIR
11338 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011339 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011340
11341 if (fresult != NULL && rettv->v_type == VAR_LIST)
11342 list_append_string(rettv->vval.v_list, fresult, -1);
11343
11344 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011345 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011346
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011347 if (rettv->v_type == VAR_STRING)
11348 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011349#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011350}
11351
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011352static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11353static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011354
11355/*
11356 * Implementation of map() and filter().
11357 */
11358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011359filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011360{
11361 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011362 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 listitem_T *li, *nli;
11364 list_T *l = NULL;
11365 dictitem_T *di;
11366 hashtab_T *ht;
11367 hashitem_T *hi;
11368 dict_T *d = NULL;
11369 typval_T save_val;
11370 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011371 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011372 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011373 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011374 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011375 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011376 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011377 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011378
Bram Moolenaare9a41262005-01-15 22:18:47 +000011379 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011380 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011381 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011382 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011383 return;
11384 }
11385 else if (argvars[0].v_type == VAR_DICT)
11386 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011387 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011388 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011389 return;
11390 }
11391 else
11392 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011393 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011394 return;
11395 }
11396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397 expr = get_tv_string_buf_chk(&argvars[1], buf);
11398 /* On type errors, the preceding call has already displayed an error
11399 * message. Avoid a misleading error message for an empty string that
11400 * was not passed as argument. */
11401 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011403 prepare_vimvar(VV_VAL, &save_val);
11404 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011405
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011406 /* We reset "did_emsg" to be able to detect whether an error
11407 * occurred during evaluation of the expression. */
11408 save_did_emsg = did_emsg;
11409 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011410
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011411 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011412 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 vimvars[VV_KEY].vv_type = VAR_STRING;
11415
11416 ht = &d->dv_hashtab;
11417 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011418 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011419 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011421 if (!HASHITEM_EMPTY(hi))
11422 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011423 int r;
11424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011425 --todo;
11426 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011427 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011428 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11429 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011430 break;
11431 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011432 r = filter_map_one(&di->di_tv, expr, map, &rem);
11433 clear_tv(&vimvars[VV_KEY].vv_tv);
11434 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435 break;
11436 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011437 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011438 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11439 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011440 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011441 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011442 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443 }
11444 }
11445 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011446 }
11447 else
11448 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011449 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011451 for (li = l->lv_first; li != NULL; li = nli)
11452 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011453 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011454 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011455 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011456 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011457 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011458 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011459 break;
11460 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011461 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011462 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011463 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011464 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011465
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011466 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011467 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011468
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011469 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011470 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011471
11472 copy_tv(&argvars[0], rettv);
11473}
11474
11475 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011476filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011477{
Bram Moolenaar33570922005-01-25 22:26:29 +000011478 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011479 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011480 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011481
Bram Moolenaar33570922005-01-25 22:26:29 +000011482 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011483 s = expr;
11484 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011485 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011486 if (*s != NUL) /* check for trailing chars after expr */
11487 {
11488 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011489 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011490 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011491 }
11492 if (map)
11493 {
11494 /* map(): replace the list item value */
11495 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011496 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011497 *tv = rettv;
11498 }
11499 else
11500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 int error = FALSE;
11502
Bram Moolenaare9a41262005-01-15 22:18:47 +000011503 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011504 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011505 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 /* On type error, nothing has been removed; return FAIL to stop the
11507 * loop. The error message was given by get_tv_number_chk(). */
11508 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011509 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011510 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011511 retval = OK;
11512theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011513 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011514 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011515}
11516
11517/*
11518 * "filter()" function
11519 */
11520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011521f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011522{
11523 filter_map(argvars, rettv, FALSE);
11524}
11525
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011526/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011527 * "finddir({fname}[, {path}[, {count}]])" function
11528 */
11529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011530f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011531{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011532 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011533}
11534
11535/*
11536 * "findfile({fname}[, {path}[, {count}]])" function
11537 */
11538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011539f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011540{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011541 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011542}
11543
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011544#ifdef FEAT_FLOAT
11545/*
11546 * "float2nr({float})" function
11547 */
11548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011549f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011550{
11551 float_T f;
11552
11553 if (get_float_arg(argvars, &f) == OK)
11554 {
11555 if (f < -0x7fffffff)
11556 rettv->vval.v_number = -0x7fffffff;
11557 else if (f > 0x7fffffff)
11558 rettv->vval.v_number = 0x7fffffff;
11559 else
11560 rettv->vval.v_number = (varnumber_T)f;
11561 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011562}
11563
11564/*
11565 * "floor({float})" function
11566 */
11567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011568f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011569{
11570 float_T f;
11571
11572 rettv->v_type = VAR_FLOAT;
11573 if (get_float_arg(argvars, &f) == OK)
11574 rettv->vval.v_float = floor(f);
11575 else
11576 rettv->vval.v_float = 0.0;
11577}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011578
11579/*
11580 * "fmod()" function
11581 */
11582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011583f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011584{
11585 float_T fx, fy;
11586
11587 rettv->v_type = VAR_FLOAT;
11588 if (get_float_arg(argvars, &fx) == OK
11589 && get_float_arg(&argvars[1], &fy) == OK)
11590 rettv->vval.v_float = fmod(fx, fy);
11591 else
11592 rettv->vval.v_float = 0.0;
11593}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011594#endif
11595
Bram Moolenaar0d660222005-01-07 21:51:51 +000011596/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011597 * "fnameescape({string})" function
11598 */
11599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011600f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011601{
11602 rettv->vval.v_string = vim_strsave_fnameescape(
11603 get_tv_string(&argvars[0]), FALSE);
11604 rettv->v_type = VAR_STRING;
11605}
11606
11607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 * "fnamemodify({fname}, {mods})" function
11609 */
11610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011611f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612{
11613 char_u *fname;
11614 char_u *mods;
11615 int usedlen = 0;
11616 int len;
11617 char_u *fbuf = NULL;
11618 char_u buf[NUMBUFLEN];
11619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011620 fname = get_tv_string_chk(&argvars[0]);
11621 mods = get_tv_string_buf_chk(&argvars[1], buf);
11622 if (fname == NULL || mods == NULL)
11623 fname = NULL;
11624 else
11625 {
11626 len = (int)STRLEN(fname);
11627 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011634 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635 vim_free(fbuf);
11636}
11637
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011638static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639
11640/*
11641 * "foldclosed()" function
11642 */
11643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011644foldclosed_both(
11645 typval_T *argvars UNUSED,
11646 typval_T *rettv,
11647 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648{
11649#ifdef FEAT_FOLDING
11650 linenr_T lnum;
11651 linenr_T first, last;
11652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011653 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11655 {
11656 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11657 {
11658 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 return;
11663 }
11664 }
11665#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667}
11668
11669/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011670 * "foldclosed()" function
11671 */
11672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011673f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011674{
11675 foldclosed_both(argvars, rettv, FALSE);
11676}
11677
11678/*
11679 * "foldclosedend()" function
11680 */
11681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011682f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011683{
11684 foldclosed_both(argvars, rettv, TRUE);
11685}
11686
11687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 * "foldlevel()" function
11689 */
11690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011691f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692{
11693#ifdef FEAT_FOLDING
11694 linenr_T lnum;
11695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011698 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700}
11701
11702/*
11703 * "foldtext()" function
11704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011706f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707{
11708#ifdef FEAT_FOLDING
11709 linenr_T lnum;
11710 char_u *s;
11711 char_u *r;
11712 int len;
11713 char *txt;
11714#endif
11715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011716 rettv->v_type = VAR_STRING;
11717 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011719 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11720 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11721 <= curbuf->b_ml.ml_line_count
11722 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723 {
11724 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011725 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11726 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 {
11728 if (!linewhite(lnum))
11729 break;
11730 ++lnum;
11731 }
11732
11733 /* Find interesting text in this line. */
11734 s = skipwhite(ml_get(lnum));
11735 /* skip C comment-start */
11736 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011737 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011739 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011740 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011741 {
11742 s = skipwhite(ml_get(lnum + 1));
11743 if (*s == '*')
11744 s = skipwhite(s + 1);
11745 }
11746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747 txt = _("+-%s%3ld lines: ");
11748 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011749 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750 + 20 /* for %3ld */
11751 + STRLEN(s))); /* concatenated */
11752 if (r != NULL)
11753 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011754 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11755 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11756 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 len = (int)STRLEN(r);
11758 STRCAT(r, s);
11759 /* remove 'foldmarker' and 'commentstring' */
11760 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 }
11763 }
11764#endif
11765}
11766
11767/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011768 * "foldtextresult(lnum)" function
11769 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011771f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011772{
11773#ifdef FEAT_FOLDING
11774 linenr_T lnum;
11775 char_u *text;
11776 char_u buf[51];
11777 foldinfo_T foldinfo;
11778 int fold_count;
11779#endif
11780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011781 rettv->v_type = VAR_STRING;
11782 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011783#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 /* treat illegal types and illegal string values for {lnum} the same */
11786 if (lnum < 0)
11787 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011788 fold_count = foldedCount(curwin, lnum, &foldinfo);
11789 if (fold_count > 0)
11790 {
11791 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11792 &foldinfo, buf);
11793 if (text == buf)
11794 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011796 }
11797#endif
11798}
11799
11800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 * "foreground()" function
11802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011804f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806#ifdef FEAT_GUI
11807 if (gui.in_use)
11808 gui_mch_set_foreground();
11809#else
11810# ifdef WIN32
11811 win32_set_foreground();
11812# endif
11813#endif
11814}
11815
11816/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011817 * "function()" function
11818 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011820f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011821{
11822 char_u *s;
11823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011824 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011825 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011826 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011827 /* Don't check an autoload name for existence here. */
11828 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011829 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011830 else
11831 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011832 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011833 {
11834 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011835 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011836
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011837 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11838 * also be called from another script. Using trans_function_name()
11839 * would also work, but some plugins depend on the name being
11840 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011841 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011842 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011843 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011844 if (rettv->vval.v_string != NULL)
11845 {
11846 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011847 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011848 }
11849 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011850 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011851 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011853 }
11854}
11855
11856/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011857 * "garbagecollect()" function
11858 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011860f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011861{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011862 /* This is postponed until we are back at the toplevel, because we may be
11863 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11864 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011865
11866 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11867 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011868}
11869
11870/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011871 * "get()" function
11872 */
11873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011874f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011875{
Bram Moolenaar33570922005-01-25 22:26:29 +000011876 listitem_T *li;
11877 list_T *l;
11878 dictitem_T *di;
11879 dict_T *d;
11880 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011881
Bram Moolenaare9a41262005-01-15 22:18:47 +000011882 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011883 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011884 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011886 int error = FALSE;
11887
11888 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11889 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011890 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011891 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011892 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011893 else if (argvars[0].v_type == VAR_DICT)
11894 {
11895 if ((d = argvars[0].vval.v_dict) != NULL)
11896 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011897 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011898 if (di != NULL)
11899 tv = &di->di_tv;
11900 }
11901 }
11902 else
11903 EMSG2(_(e_listdictarg), "get()");
11904
11905 if (tv == NULL)
11906 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011907 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011908 copy_tv(&argvars[2], rettv);
11909 }
11910 else
11911 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011912}
11913
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011914static 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 +000011915
11916/*
11917 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011918 * Return a range (from start to end) of lines in rettv from the specified
11919 * buffer.
11920 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011921 */
11922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011923get_buffer_lines(
11924 buf_T *buf,
11925 linenr_T start,
11926 linenr_T end,
11927 int retlist,
11928 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011929{
11930 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011931
Bram Moolenaar959a1432013-12-14 12:17:38 +010011932 rettv->v_type = VAR_STRING;
11933 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011934 if (retlist && rettv_list_alloc(rettv) == FAIL)
11935 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011936
11937 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11938 return;
11939
11940 if (!retlist)
11941 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011942 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11943 p = ml_get_buf(buf, start, FALSE);
11944 else
11945 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011946 rettv->vval.v_string = vim_strsave(p);
11947 }
11948 else
11949 {
11950 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011951 return;
11952
11953 if (start < 1)
11954 start = 1;
11955 if (end > buf->b_ml.ml_line_count)
11956 end = buf->b_ml.ml_line_count;
11957 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011958 if (list_append_string(rettv->vval.v_list,
11959 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011960 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011961 }
11962}
11963
11964/*
11965 * "getbufline()" function
11966 */
11967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011968f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011969{
11970 linenr_T lnum;
11971 linenr_T end;
11972 buf_T *buf;
11973
11974 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11975 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011976 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011977 --emsg_off;
11978
Bram Moolenaar661b1822005-07-28 22:36:45 +000011979 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011980 if (argvars[2].v_type == VAR_UNKNOWN)
11981 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011982 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011983 end = get_tv_lnum_buf(&argvars[2], buf);
11984
Bram Moolenaar342337a2005-07-21 21:11:17 +000011985 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011986}
11987
Bram Moolenaar0d660222005-01-07 21:51:51 +000011988/*
11989 * "getbufvar()" function
11990 */
11991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011992f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011993{
11994 buf_T *buf;
11995 buf_T *save_curbuf;
11996 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011997 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011998 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012000 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12001 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012002 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012003 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012004
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012005 rettv->v_type = VAR_STRING;
12006 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012007
12008 if (buf != NULL && varname != NULL)
12009 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012010 /* set curbuf to be our buf, temporarily */
12011 save_curbuf = curbuf;
12012 curbuf = buf;
12013
Bram Moolenaar0d660222005-01-07 21:51:51 +000012014 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012015 {
12016 if (get_option_tv(&varname, rettv, TRUE) == OK)
12017 done = TRUE;
12018 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012019 else if (STRCMP(varname, "changedtick") == 0)
12020 {
12021 rettv->v_type = VAR_NUMBER;
12022 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012023 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012024 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012025 else
12026 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012027 /* Look up the variable. */
12028 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12029 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12030 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012031 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012032 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012033 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012034 done = TRUE;
12035 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012036 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012037
12038 /* restore previous notion of curbuf */
12039 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012040 }
12041
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012042 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12043 /* use the default value */
12044 copy_tv(&argvars[2], rettv);
12045
Bram Moolenaar0d660222005-01-07 21:51:51 +000012046 --emsg_off;
12047}
12048
12049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 * "getchar()" function
12051 */
12052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012053f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054{
12055 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012056 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012058 /* Position the cursor. Needed after a message that ends in a space. */
12059 windgoto(msg_row, msg_col);
12060
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 ++no_mapping;
12062 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012063 for (;;)
12064 {
12065 if (argvars[0].v_type == VAR_UNKNOWN)
12066 /* getchar(): blocking wait. */
12067 n = safe_vgetc();
12068 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12069 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012070 n = vpeekc_any();
12071 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012072 /* illegal argument or getchar(0) and no char avail: return zero */
12073 n = 0;
12074 else
12075 /* getchar(0) and char avail: return char */
12076 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012077
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012078 if (n == K_IGNORE)
12079 continue;
12080 break;
12081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 --no_mapping;
12083 --allow_keys;
12084
Bram Moolenaar219b8702006-11-01 14:32:36 +000012085 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12086 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12087 vimvars[VV_MOUSE_COL].vv_nr = 0;
12088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012089 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090 if (IS_SPECIAL(n) || mod_mask != 0)
12091 {
12092 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12093 int i = 0;
12094
12095 /* Turn a special key into three bytes, plus modifier. */
12096 if (mod_mask != 0)
12097 {
12098 temp[i++] = K_SPECIAL;
12099 temp[i++] = KS_MODIFIER;
12100 temp[i++] = mod_mask;
12101 }
12102 if (IS_SPECIAL(n))
12103 {
12104 temp[i++] = K_SPECIAL;
12105 temp[i++] = K_SECOND(n);
12106 temp[i++] = K_THIRD(n);
12107 }
12108#ifdef FEAT_MBYTE
12109 else if (has_mbyte)
12110 i += (*mb_char2bytes)(n, temp + i);
12111#endif
12112 else
12113 temp[i++] = n;
12114 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012115 rettv->v_type = VAR_STRING;
12116 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012117
12118#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012119 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012120 {
12121 int row = mouse_row;
12122 int col = mouse_col;
12123 win_T *win;
12124 linenr_T lnum;
12125# ifdef FEAT_WINDOWS
12126 win_T *wp;
12127# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012128 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012129
12130 if (row >= 0 && col >= 0)
12131 {
12132 /* Find the window at the mouse coordinates and compute the
12133 * text position. */
12134 win = mouse_find_win(&row, &col);
12135 (void)mouse_comp_pos(win, &row, &col, &lnum);
12136# ifdef FEAT_WINDOWS
12137 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012138 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012139# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012140 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012141 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12142 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12143 }
12144 }
12145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146 }
12147}
12148
12149/*
12150 * "getcharmod()" function
12151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012153f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156}
12157
12158/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012159 * "getcharsearch()" function
12160 */
12161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012162f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012163{
12164 if (rettv_dict_alloc(rettv) != FAIL)
12165 {
12166 dict_T *dict = rettv->vval.v_dict;
12167
12168 dict_add_nr_str(dict, "char", 0L, last_csearch());
12169 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12170 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12171 }
12172}
12173
12174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 * "getcmdline()" function
12176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012178f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 rettv->v_type = VAR_STRING;
12181 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182}
12183
12184/*
12185 * "getcmdpos()" function
12186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012188f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191}
12192
12193/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012194 * "getcmdtype()" function
12195 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012197f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012198{
12199 rettv->v_type = VAR_STRING;
12200 rettv->vval.v_string = alloc(2);
12201 if (rettv->vval.v_string != NULL)
12202 {
12203 rettv->vval.v_string[0] = get_cmdline_type();
12204 rettv->vval.v_string[1] = NUL;
12205 }
12206}
12207
12208/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012209 * "getcmdwintype()" function
12210 */
12211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012212f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012213{
12214 rettv->v_type = VAR_STRING;
12215 rettv->vval.v_string = NULL;
12216#ifdef FEAT_CMDWIN
12217 rettv->vval.v_string = alloc(2);
12218 if (rettv->vval.v_string != NULL)
12219 {
12220 rettv->vval.v_string[0] = cmdwin_type;
12221 rettv->vval.v_string[1] = NUL;
12222 }
12223#endif
12224}
12225
12226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 * "getcwd()" function
12228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012230f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012232 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012233 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012235 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012236 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012237
12238 wp = find_tabwin(&argvars[0], &argvars[1]);
12239 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012241 if (wp->w_localdir != NULL)
12242 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12243 else if(globaldir != NULL)
12244 rettv->vval.v_string = vim_strsave(globaldir);
12245 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012246 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012247 cwd = alloc(MAXPATHL);
12248 if (cwd != NULL)
12249 {
12250 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12251 rettv->vval.v_string = vim_strsave(cwd);
12252 vim_free(cwd);
12253 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012254 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012255#ifdef BACKSLASH_IN_FILENAME
12256 if (rettv->vval.v_string != NULL)
12257 slash_adjust(rettv->vval.v_string);
12258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 }
12260}
12261
12262/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012263 * "getfontname()" function
12264 */
12265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012266f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012267{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268 rettv->v_type = VAR_STRING;
12269 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012270#ifdef FEAT_GUI
12271 if (gui.in_use)
12272 {
12273 GuiFont font;
12274 char_u *name = NULL;
12275
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012276 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012277 {
12278 /* Get the "Normal" font. Either the name saved by
12279 * hl_set_font_name() or from the font ID. */
12280 font = gui.norm_font;
12281 name = hl_get_font_name();
12282 }
12283 else
12284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012285 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012286 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12287 return;
12288 font = gui_mch_get_font(name, FALSE);
12289 if (font == NOFONT)
12290 return; /* Invalid font name, return empty string. */
12291 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012292 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012293 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012294 gui_mch_free_font(font);
12295 }
12296#endif
12297}
12298
12299/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012300 * "getfperm({fname})" function
12301 */
12302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012303f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012304{
12305 char_u *fname;
12306 struct stat st;
12307 char_u *perm = NULL;
12308 char_u flags[] = "rwx";
12309 int i;
12310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012311 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012313 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012314 if (mch_stat((char *)fname, &st) >= 0)
12315 {
12316 perm = vim_strsave((char_u *)"---------");
12317 if (perm != NULL)
12318 {
12319 for (i = 0; i < 9; i++)
12320 {
12321 if (st.st_mode & (1 << (8 - i)))
12322 perm[i] = flags[i % 3];
12323 }
12324 }
12325 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012326 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012327}
12328
12329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 * "getfsize({fname})" function
12331 */
12332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012333f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334{
12335 char_u *fname;
12336 struct stat st;
12337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012340 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341
12342 if (mch_stat((char *)fname, &st) >= 0)
12343 {
12344 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012345 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012349
12350 /* non-perfect check for overflow */
12351 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12352 rettv->vval.v_number = -2;
12353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 }
12355 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012356 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357}
12358
12359/*
12360 * "getftime({fname})" function
12361 */
12362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012363f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364{
12365 char_u *fname;
12366 struct stat st;
12367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369
12370 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012371 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012373 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374}
12375
12376/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012377 * "getftype({fname})" function
12378 */
12379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012380f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012381{
12382 char_u *fname;
12383 struct stat st;
12384 char_u *type = NULL;
12385 char *t;
12386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012387 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012389 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012390 if (mch_lstat((char *)fname, &st) >= 0)
12391 {
12392#ifdef S_ISREG
12393 if (S_ISREG(st.st_mode))
12394 t = "file";
12395 else if (S_ISDIR(st.st_mode))
12396 t = "dir";
12397# ifdef S_ISLNK
12398 else if (S_ISLNK(st.st_mode))
12399 t = "link";
12400# endif
12401# ifdef S_ISBLK
12402 else if (S_ISBLK(st.st_mode))
12403 t = "bdev";
12404# endif
12405# ifdef S_ISCHR
12406 else if (S_ISCHR(st.st_mode))
12407 t = "cdev";
12408# endif
12409# ifdef S_ISFIFO
12410 else if (S_ISFIFO(st.st_mode))
12411 t = "fifo";
12412# endif
12413# ifdef S_ISSOCK
12414 else if (S_ISSOCK(st.st_mode))
12415 t = "fifo";
12416# endif
12417 else
12418 t = "other";
12419#else
12420# ifdef S_IFMT
12421 switch (st.st_mode & S_IFMT)
12422 {
12423 case S_IFREG: t = "file"; break;
12424 case S_IFDIR: t = "dir"; break;
12425# ifdef S_IFLNK
12426 case S_IFLNK: t = "link"; break;
12427# endif
12428# ifdef S_IFBLK
12429 case S_IFBLK: t = "bdev"; break;
12430# endif
12431# ifdef S_IFCHR
12432 case S_IFCHR: t = "cdev"; break;
12433# endif
12434# ifdef S_IFIFO
12435 case S_IFIFO: t = "fifo"; break;
12436# endif
12437# ifdef S_IFSOCK
12438 case S_IFSOCK: t = "socket"; break;
12439# endif
12440 default: t = "other";
12441 }
12442# else
12443 if (mch_isdir(fname))
12444 t = "dir";
12445 else
12446 t = "file";
12447# endif
12448#endif
12449 type = vim_strsave((char_u *)t);
12450 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012451 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012452}
12453
12454/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012455 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012456 */
12457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012458f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012459{
12460 linenr_T lnum;
12461 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012462 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012463
12464 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012465 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012466 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012467 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012468 retlist = FALSE;
12469 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012470 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012471 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012472 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012473 retlist = TRUE;
12474 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012475
Bram Moolenaar342337a2005-07-21 21:11:17 +000012476 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012477}
12478
12479/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012480 * "getmatches()" function
12481 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012483f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012484{
12485#ifdef FEAT_SEARCH_EXTRA
12486 dict_T *dict;
12487 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012488 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012489
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012490 if (rettv_list_alloc(rettv) == OK)
12491 {
12492 while (cur != NULL)
12493 {
12494 dict = dict_alloc();
12495 if (dict == NULL)
12496 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012497 if (cur->match.regprog == NULL)
12498 {
12499 /* match added with matchaddpos() */
12500 for (i = 0; i < MAXPOSMATCH; ++i)
12501 {
12502 llpos_T *llpos;
12503 char buf[6];
12504 list_T *l;
12505
12506 llpos = &cur->pos.pos[i];
12507 if (llpos->lnum == 0)
12508 break;
12509 l = list_alloc();
12510 if (l == NULL)
12511 break;
12512 list_append_number(l, (varnumber_T)llpos->lnum);
12513 if (llpos->col > 0)
12514 {
12515 list_append_number(l, (varnumber_T)llpos->col);
12516 list_append_number(l, (varnumber_T)llpos->len);
12517 }
12518 sprintf(buf, "pos%d", i + 1);
12519 dict_add_list(dict, buf, l);
12520 }
12521 }
12522 else
12523 {
12524 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12525 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012526 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012527 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12528 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012529# ifdef FEAT_CONCEAL
12530 if (cur->conceal_char)
12531 {
12532 char_u buf[MB_MAXBYTES + 1];
12533
12534 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12535 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12536 }
12537# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012538 list_append_dict(rettv->vval.v_list, dict);
12539 cur = cur->next;
12540 }
12541 }
12542#endif
12543}
12544
12545/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012546 * "getpid()" function
12547 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012549f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012550{
12551 rettv->vval.v_number = mch_get_pid();
12552}
12553
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012554static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012555
12556/*
12557 * "getcurpos()" function
12558 */
12559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012560f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012561{
12562 getpos_both(argvars, rettv, TRUE);
12563}
12564
Bram Moolenaar18081e32008-02-20 19:11:07 +000012565/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012566 * "getpos(string)" function
12567 */
12568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012569f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012570{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012571 getpos_both(argvars, rettv, FALSE);
12572}
12573
12574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012575getpos_both(
12576 typval_T *argvars,
12577 typval_T *rettv,
12578 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012579{
Bram Moolenaara5525202006-03-02 22:52:09 +000012580 pos_T *fp;
12581 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012582 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012583
12584 if (rettv_list_alloc(rettv) == OK)
12585 {
12586 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012587 if (getcurpos)
12588 fp = &curwin->w_cursor;
12589 else
12590 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012591 if (fnum != -1)
12592 list_append_number(l, (varnumber_T)fnum);
12593 else
12594 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012595 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12596 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012597 list_append_number(l, (fp != NULL)
12598 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012599 : (varnumber_T)0);
12600 list_append_number(l,
12601#ifdef FEAT_VIRTUALEDIT
12602 (fp != NULL) ? (varnumber_T)fp->coladd :
12603#endif
12604 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012605 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012606 {
12607 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012608 list_append_number(l, curwin->w_curswant == MAXCOL ?
12609 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012610 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012611 }
12612 else
12613 rettv->vval.v_number = FALSE;
12614}
12615
12616/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012617 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012618 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012620f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012621{
12622#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012623 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012624#endif
12625
Bram Moolenaar2641f772005-03-25 21:58:17 +000012626#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012627 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012628 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012629 wp = NULL;
12630 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12631 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012632 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012633 if (wp == NULL)
12634 return;
12635 }
12636
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012637 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012638 }
12639#endif
12640}
12641
12642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643 * "getreg()" function
12644 */
12645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012646f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647{
12648 char_u *strregname;
12649 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012650 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012651 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012652 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012654 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012656 strregname = get_tv_string_chk(&argvars[0]);
12657 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012658 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012660 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012661 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12662 return_list = get_tv_number_chk(&argvars[2], &error);
12663 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012666 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012667
12668 if (error)
12669 return;
12670
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671 regname = (strregname == NULL ? '"' : *strregname);
12672 if (regname == 0)
12673 regname = '"';
12674
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012675 if (return_list)
12676 {
12677 rettv->v_type = VAR_LIST;
12678 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12679 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012680 if (rettv->vval.v_list != NULL)
12681 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012682 }
12683 else
12684 {
12685 rettv->v_type = VAR_STRING;
12686 rettv->vval.v_string = get_reg_contents(regname,
12687 arg2 ? GREG_EXPR_SRC : 0);
12688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689}
12690
12691/*
12692 * "getregtype()" function
12693 */
12694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012695f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696{
12697 char_u *strregname;
12698 int regname;
12699 char_u buf[NUMBUFLEN + 2];
12700 long reglen = 0;
12701
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012702 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012703 {
12704 strregname = get_tv_string_chk(&argvars[0]);
12705 if (strregname == NULL) /* type error; errmsg already given */
12706 {
12707 rettv->v_type = VAR_STRING;
12708 rettv->vval.v_string = NULL;
12709 return;
12710 }
12711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 else
12713 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012714 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715
12716 regname = (strregname == NULL ? '"' : *strregname);
12717 if (regname == 0)
12718 regname = '"';
12719
12720 buf[0] = NUL;
12721 buf[1] = NUL;
12722 switch (get_reg_type(regname, &reglen))
12723 {
12724 case MLINE: buf[0] = 'V'; break;
12725 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 case MBLOCK:
12727 buf[0] = Ctrl_V;
12728 sprintf((char *)buf + 1, "%ld", reglen + 1);
12729 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012731 rettv->v_type = VAR_STRING;
12732 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733}
12734
12735/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012736 * "gettabvar()" function
12737 */
12738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012739f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012740{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012741 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012742 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012743 dictitem_T *v;
12744 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012745 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012746
12747 rettv->v_type = VAR_STRING;
12748 rettv->vval.v_string = NULL;
12749
12750 varname = get_tv_string_chk(&argvars[1]);
12751 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12752 if (tp != NULL && varname != NULL)
12753 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012754 /* Set tp to be our tabpage, temporarily. Also set the window to the
12755 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012756 if (switch_win(&oldcurwin, &oldtabpage,
12757 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012758 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012759 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012760 /* look up the variable */
12761 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12762 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12763 if (v != NULL)
12764 {
12765 copy_tv(&v->di_tv, rettv);
12766 done = TRUE;
12767 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012768 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012769
12770 /* restore previous notion of curwin */
12771 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012772 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012773
12774 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012775 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012776}
12777
12778/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012779 * "gettabwinvar()" function
12780 */
12781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012782f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012783{
12784 getwinvar(argvars, rettv, 1);
12785}
12786
12787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788 * "getwinposx()" function
12789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012791f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794#ifdef FEAT_GUI
12795 if (gui.in_use)
12796 {
12797 int x, y;
12798
12799 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 }
12802#endif
12803}
12804
12805/*
12806 * "getwinposy()" function
12807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012809f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812#ifdef FEAT_GUI
12813 if (gui.in_use)
12814 {
12815 int x, y;
12816
12817 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 }
12820#endif
12821}
12822
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012823/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012824 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012825 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012826 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012827find_win_by_nr(
12828 typval_T *vp,
12829 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012830{
12831#ifdef FEAT_WINDOWS
12832 win_T *wp;
12833#endif
12834 int nr;
12835
12836 nr = get_tv_number_chk(vp, NULL);
12837
12838#ifdef FEAT_WINDOWS
12839 if (nr < 0)
12840 return NULL;
12841 if (nr == 0)
12842 return curwin;
12843
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012844 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12845 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012846 if (--nr <= 0)
12847 break;
12848 return wp;
12849#else
12850 if (nr == 0 || nr == 1)
12851 return curwin;
12852 return NULL;
12853#endif
12854}
12855
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012857 * Find window specified by "wvp" in tabpage "tvp".
12858 */
12859 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012860find_tabwin(
12861 typval_T *wvp, /* VAR_UNKNOWN for current window */
12862 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012863{
12864 win_T *wp = NULL;
12865 tabpage_T *tp = NULL;
12866 long n;
12867
12868 if (wvp->v_type != VAR_UNKNOWN)
12869 {
12870 if (tvp->v_type != VAR_UNKNOWN)
12871 {
12872 n = get_tv_number(tvp);
12873 if (n >= 0)
12874 tp = find_tabpage(n);
12875 }
12876 else
12877 tp = curtab;
12878
12879 if (tp != NULL)
12880 wp = find_win_by_nr(wvp, tp);
12881 }
12882 else
12883 wp = curwin;
12884
12885 return wp;
12886}
12887
12888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889 * "getwinvar()" function
12890 */
12891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012892f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012894 getwinvar(argvars, rettv, 0);
12895}
12896
12897/*
12898 * getwinvar() and gettabwinvar()
12899 */
12900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012901getwinvar(
12902 typval_T *argvars,
12903 typval_T *rettv,
12904 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012905{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012906 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012908 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012909 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012910 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012911#ifdef FEAT_WINDOWS
12912 win_T *oldcurwin;
12913 tabpage_T *oldtabpage;
12914 int need_switch_win;
12915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012917#ifdef FEAT_WINDOWS
12918 if (off == 1)
12919 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12920 else
12921 tp = curtab;
12922#endif
12923 win = find_win_by_nr(&argvars[off], tp);
12924 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012925 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012927 rettv->v_type = VAR_STRING;
12928 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929
12930 if (win != NULL && varname != NULL)
12931 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012932#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012933 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012934 * otherwise the window is not valid. Only do this when needed,
12935 * autocommands get blocked. */
12936 need_switch_win = !(tp == curtab && win == curwin);
12937 if (!need_switch_win
12938 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12939#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012940 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012941 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012942 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012943 if (get_option_tv(&varname, rettv, 1) == OK)
12944 done = TRUE;
12945 }
12946 else
12947 {
12948 /* Look up the variable. */
12949 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12950 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12951 varname, FALSE);
12952 if (v != NULL)
12953 {
12954 copy_tv(&v->di_tv, rettv);
12955 done = TRUE;
12956 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012959
Bram Moolenaarba117c22015-09-29 16:53:22 +020012960#ifdef FEAT_WINDOWS
12961 if (need_switch_win)
12962 /* restore previous notion of curwin */
12963 restore_win(oldcurwin, oldtabpage, TRUE);
12964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965 }
12966
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012967 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12968 /* use the default return value */
12969 copy_tv(&argvars[off + 2], rettv);
12970
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971 --emsg_off;
12972}
12973
12974/*
12975 * "glob()" function
12976 */
12977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012978f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012980 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012982 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012984 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012985 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012986 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012987 if (argvars[1].v_type != VAR_UNKNOWN)
12988 {
12989 if (get_tv_number_chk(&argvars[1], &error))
12990 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012991 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012992 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012993 if (get_tv_number_chk(&argvars[2], &error))
12994 {
12995 rettv->v_type = VAR_LIST;
12996 rettv->vval.v_list = NULL;
12997 }
12998 if (argvars[3].v_type != VAR_UNKNOWN
12999 && get_tv_number_chk(&argvars[3], &error))
13000 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013001 }
13002 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013003 if (!error)
13004 {
13005 ExpandInit(&xpc);
13006 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013007 if (p_wic)
13008 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013009 if (rettv->v_type == VAR_STRING)
13010 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013011 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013012 else if (rettv_list_alloc(rettv) != FAIL)
13013 {
13014 int i;
13015
13016 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13017 NULL, options, WILD_ALL_KEEP);
13018 for (i = 0; i < xpc.xp_numfiles; i++)
13019 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13020
13021 ExpandCleanup(&xpc);
13022 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013023 }
13024 else
13025 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026}
13027
13028/*
13029 * "globpath()" function
13030 */
13031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013032f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013034 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013036 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013037 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013038 garray_T ga;
13039 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013041 /* When the optional second argument is non-zero, don't remove matches
13042 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013044 if (argvars[2].v_type != VAR_UNKNOWN)
13045 {
13046 if (get_tv_number_chk(&argvars[2], &error))
13047 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013048 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013049 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013050 if (get_tv_number_chk(&argvars[3], &error))
13051 {
13052 rettv->v_type = VAR_LIST;
13053 rettv->vval.v_list = NULL;
13054 }
13055 if (argvars[4].v_type != VAR_UNKNOWN
13056 && get_tv_number_chk(&argvars[4], &error))
13057 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013058 }
13059 }
13060 if (file != NULL && !error)
13061 {
13062 ga_init2(&ga, (int)sizeof(char_u *), 10);
13063 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13064 if (rettv->v_type == VAR_STRING)
13065 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13066 else if (rettv_list_alloc(rettv) != FAIL)
13067 for (i = 0; i < ga.ga_len; ++i)
13068 list_append_string(rettv->vval.v_list,
13069 ((char_u **)(ga.ga_data))[i], -1);
13070 ga_clear_strings(&ga);
13071 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013072 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074}
13075
13076/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013077 * "glob2regpat()" function
13078 */
13079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013080f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013081{
13082 char_u *pat = get_tv_string_chk(&argvars[0]);
13083
13084 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013085 rettv->vval.v_string = (pat == NULL)
13086 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013087}
13088
13089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090 * "has()" function
13091 */
13092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013093f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094{
13095 int i;
13096 char_u *name;
13097 int n = FALSE;
13098 static char *(has_list[]) =
13099 {
13100#ifdef AMIGA
13101 "amiga",
13102# ifdef FEAT_ARP
13103 "arp",
13104# endif
13105#endif
13106#ifdef __BEOS__
13107 "beos",
13108#endif
13109#ifdef MSDOS
13110# ifdef DJGPP
13111 "dos32",
13112# else
13113 "dos16",
13114# endif
13115#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013116#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117 "mac",
13118#endif
13119#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013120 "macunix", /* built with 'darwin' enabled */
13121#endif
13122#if defined(__APPLE__) && __APPLE__ == 1
13123 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125#ifdef __QNX__
13126 "qnx",
13127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128#ifdef UNIX
13129 "unix",
13130#endif
13131#ifdef VMS
13132 "vms",
13133#endif
13134#ifdef WIN16
13135 "win16",
13136#endif
13137#ifdef WIN32
13138 "win32",
13139#endif
13140#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13141 "win32unix",
13142#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013143#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 "win64",
13145#endif
13146#ifdef EBCDIC
13147 "ebcdic",
13148#endif
13149#ifndef CASE_INSENSITIVE_FILENAME
13150 "fname_case",
13151#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013152#ifdef HAVE_ACL
13153 "acl",
13154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155#ifdef FEAT_ARABIC
13156 "arabic",
13157#endif
13158#ifdef FEAT_AUTOCMD
13159 "autocmd",
13160#endif
13161#ifdef FEAT_BEVAL
13162 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013163# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13164 "balloon_multiline",
13165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166#endif
13167#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13168 "builtin_terms",
13169# ifdef ALL_BUILTIN_TCAPS
13170 "all_builtin_terms",
13171# endif
13172#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013173#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13174 || defined(FEAT_GUI_W32) \
13175 || defined(FEAT_GUI_MOTIF))
13176 "browsefilter",
13177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178#ifdef FEAT_BYTEOFF
13179 "byte_offset",
13180#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013181#ifdef FEAT_CHANNEL
13182 "channel",
13183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184#ifdef FEAT_CINDENT
13185 "cindent",
13186#endif
13187#ifdef FEAT_CLIENTSERVER
13188 "clientserver",
13189#endif
13190#ifdef FEAT_CLIPBOARD
13191 "clipboard",
13192#endif
13193#ifdef FEAT_CMDL_COMPL
13194 "cmdline_compl",
13195#endif
13196#ifdef FEAT_CMDHIST
13197 "cmdline_hist",
13198#endif
13199#ifdef FEAT_COMMENTS
13200 "comments",
13201#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013202#ifdef FEAT_CONCEAL
13203 "conceal",
13204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205#ifdef FEAT_CRYPT
13206 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013207 "crypt-blowfish",
13208 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209#endif
13210#ifdef FEAT_CSCOPE
13211 "cscope",
13212#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013213#ifdef FEAT_CURSORBIND
13214 "cursorbind",
13215#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013216#ifdef CURSOR_SHAPE
13217 "cursorshape",
13218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219#ifdef DEBUG
13220 "debug",
13221#endif
13222#ifdef FEAT_CON_DIALOG
13223 "dialog_con",
13224#endif
13225#ifdef FEAT_GUI_DIALOG
13226 "dialog_gui",
13227#endif
13228#ifdef FEAT_DIFF
13229 "diff",
13230#endif
13231#ifdef FEAT_DIGRAPHS
13232 "digraphs",
13233#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013234#ifdef FEAT_DIRECTX
13235 "directx",
13236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237#ifdef FEAT_DND
13238 "dnd",
13239#endif
13240#ifdef FEAT_EMACS_TAGS
13241 "emacs_tags",
13242#endif
13243 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013244 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245#ifdef FEAT_SEARCH_EXTRA
13246 "extra_search",
13247#endif
13248#ifdef FEAT_FKMAP
13249 "farsi",
13250#endif
13251#ifdef FEAT_SEARCHPATH
13252 "file_in_path",
13253#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013254#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013255 "filterpipe",
13256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257#ifdef FEAT_FIND_ID
13258 "find_in_path",
13259#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013260#ifdef FEAT_FLOAT
13261 "float",
13262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263#ifdef FEAT_FOLDING
13264 "folding",
13265#endif
13266#ifdef FEAT_FOOTER
13267 "footer",
13268#endif
13269#if !defined(USE_SYSTEM) && defined(UNIX)
13270 "fork",
13271#endif
13272#ifdef FEAT_GETTEXT
13273 "gettext",
13274#endif
13275#ifdef FEAT_GUI
13276 "gui",
13277#endif
13278#ifdef FEAT_GUI_ATHENA
13279# ifdef FEAT_GUI_NEXTAW
13280 "gui_neXtaw",
13281# else
13282 "gui_athena",
13283# endif
13284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285#ifdef FEAT_GUI_GTK
13286 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013289#ifdef FEAT_GUI_GNOME
13290 "gui_gnome",
13291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292#ifdef FEAT_GUI_MAC
13293 "gui_mac",
13294#endif
13295#ifdef FEAT_GUI_MOTIF
13296 "gui_motif",
13297#endif
13298#ifdef FEAT_GUI_PHOTON
13299 "gui_photon",
13300#endif
13301#ifdef FEAT_GUI_W16
13302 "gui_win16",
13303#endif
13304#ifdef FEAT_GUI_W32
13305 "gui_win32",
13306#endif
13307#ifdef FEAT_HANGULIN
13308 "hangul_input",
13309#endif
13310#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13311 "iconv",
13312#endif
13313#ifdef FEAT_INS_EXPAND
13314 "insert_expand",
13315#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013316#ifdef FEAT_JOB
13317 "job",
13318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319#ifdef FEAT_JUMPLIST
13320 "jumplist",
13321#endif
13322#ifdef FEAT_KEYMAP
13323 "keymap",
13324#endif
13325#ifdef FEAT_LANGMAP
13326 "langmap",
13327#endif
13328#ifdef FEAT_LIBCALL
13329 "libcall",
13330#endif
13331#ifdef FEAT_LINEBREAK
13332 "linebreak",
13333#endif
13334#ifdef FEAT_LISP
13335 "lispindent",
13336#endif
13337#ifdef FEAT_LISTCMDS
13338 "listcmds",
13339#endif
13340#ifdef FEAT_LOCALMAP
13341 "localmap",
13342#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013343#ifdef FEAT_LUA
13344# ifndef DYNAMIC_LUA
13345 "lua",
13346# endif
13347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348#ifdef FEAT_MENU
13349 "menu",
13350#endif
13351#ifdef FEAT_SESSION
13352 "mksession",
13353#endif
13354#ifdef FEAT_MODIFY_FNAME
13355 "modify_fname",
13356#endif
13357#ifdef FEAT_MOUSE
13358 "mouse",
13359#endif
13360#ifdef FEAT_MOUSESHAPE
13361 "mouseshape",
13362#endif
13363#if defined(UNIX) || defined(VMS)
13364# ifdef FEAT_MOUSE_DEC
13365 "mouse_dec",
13366# endif
13367# ifdef FEAT_MOUSE_GPM
13368 "mouse_gpm",
13369# endif
13370# ifdef FEAT_MOUSE_JSB
13371 "mouse_jsbterm",
13372# endif
13373# ifdef FEAT_MOUSE_NET
13374 "mouse_netterm",
13375# endif
13376# ifdef FEAT_MOUSE_PTERM
13377 "mouse_pterm",
13378# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013379# ifdef FEAT_MOUSE_SGR
13380 "mouse_sgr",
13381# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013382# ifdef FEAT_SYSMOUSE
13383 "mouse_sysmouse",
13384# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013385# ifdef FEAT_MOUSE_URXVT
13386 "mouse_urxvt",
13387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388# ifdef FEAT_MOUSE_XTERM
13389 "mouse_xterm",
13390# endif
13391#endif
13392#ifdef FEAT_MBYTE
13393 "multi_byte",
13394#endif
13395#ifdef FEAT_MBYTE_IME
13396 "multi_byte_ime",
13397#endif
13398#ifdef FEAT_MULTI_LANG
13399 "multi_lang",
13400#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013401#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013402#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013403 "mzscheme",
13404#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406#ifdef FEAT_OLE
13407 "ole",
13408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409#ifdef FEAT_PATH_EXTRA
13410 "path_extra",
13411#endif
13412#ifdef FEAT_PERL
13413#ifndef DYNAMIC_PERL
13414 "perl",
13415#endif
13416#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013417#ifdef FEAT_PERSISTENT_UNDO
13418 "persistent_undo",
13419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420#ifdef FEAT_PYTHON
13421#ifndef DYNAMIC_PYTHON
13422 "python",
13423#endif
13424#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013425#ifdef FEAT_PYTHON3
13426#ifndef DYNAMIC_PYTHON3
13427 "python3",
13428#endif
13429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430#ifdef FEAT_POSTSCRIPT
13431 "postscript",
13432#endif
13433#ifdef FEAT_PRINTER
13434 "printer",
13435#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013436#ifdef FEAT_PROFILE
13437 "profile",
13438#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013439#ifdef FEAT_RELTIME
13440 "reltime",
13441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442#ifdef FEAT_QUICKFIX
13443 "quickfix",
13444#endif
13445#ifdef FEAT_RIGHTLEFT
13446 "rightleft",
13447#endif
13448#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13449 "ruby",
13450#endif
13451#ifdef FEAT_SCROLLBIND
13452 "scrollbind",
13453#endif
13454#ifdef FEAT_CMDL_INFO
13455 "showcmd",
13456 "cmdline_info",
13457#endif
13458#ifdef FEAT_SIGNS
13459 "signs",
13460#endif
13461#ifdef FEAT_SMARTINDENT
13462 "smartindent",
13463#endif
13464#ifdef FEAT_SNIFF
13465 "sniff",
13466#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013467#ifdef STARTUPTIME
13468 "startuptime",
13469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470#ifdef FEAT_STL_OPT
13471 "statusline",
13472#endif
13473#ifdef FEAT_SUN_WORKSHOP
13474 "sun_workshop",
13475#endif
13476#ifdef FEAT_NETBEANS_INTG
13477 "netbeans_intg",
13478#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013479#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013480 "spell",
13481#endif
13482#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483 "syntax",
13484#endif
13485#if defined(USE_SYSTEM) || !defined(UNIX)
13486 "system",
13487#endif
13488#ifdef FEAT_TAG_BINS
13489 "tag_binary",
13490#endif
13491#ifdef FEAT_TAG_OLDSTATIC
13492 "tag_old_static",
13493#endif
13494#ifdef FEAT_TAG_ANYWHITE
13495 "tag_any_white",
13496#endif
13497#ifdef FEAT_TCL
13498# ifndef DYNAMIC_TCL
13499 "tcl",
13500# endif
13501#endif
13502#ifdef TERMINFO
13503 "terminfo",
13504#endif
13505#ifdef FEAT_TERMRESPONSE
13506 "termresponse",
13507#endif
13508#ifdef FEAT_TEXTOBJ
13509 "textobjects",
13510#endif
13511#ifdef HAVE_TGETENT
13512 "tgetent",
13513#endif
13514#ifdef FEAT_TITLE
13515 "title",
13516#endif
13517#ifdef FEAT_TOOLBAR
13518 "toolbar",
13519#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013520#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13521 "unnamedplus",
13522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523#ifdef FEAT_USR_CMDS
13524 "user-commands", /* was accidentally included in 5.4 */
13525 "user_commands",
13526#endif
13527#ifdef FEAT_VIMINFO
13528 "viminfo",
13529#endif
13530#ifdef FEAT_VERTSPLIT
13531 "vertsplit",
13532#endif
13533#ifdef FEAT_VIRTUALEDIT
13534 "virtualedit",
13535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537#ifdef FEAT_VISUALEXTRA
13538 "visualextra",
13539#endif
13540#ifdef FEAT_VREPLACE
13541 "vreplace",
13542#endif
13543#ifdef FEAT_WILDIGN
13544 "wildignore",
13545#endif
13546#ifdef FEAT_WILDMENU
13547 "wildmenu",
13548#endif
13549#ifdef FEAT_WINDOWS
13550 "windows",
13551#endif
13552#ifdef FEAT_WAK
13553 "winaltkeys",
13554#endif
13555#ifdef FEAT_WRITEBACKUP
13556 "writebackup",
13557#endif
13558#ifdef FEAT_XIM
13559 "xim",
13560#endif
13561#ifdef FEAT_XFONTSET
13562 "xfontset",
13563#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013564#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013565 "xpm",
13566 "xpm_w32", /* for backward compatibility */
13567#else
13568# if defined(HAVE_XPM)
13569 "xpm",
13570# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572#ifdef USE_XSMP
13573 "xsmp",
13574#endif
13575#ifdef USE_XSMP_INTERACT
13576 "xsmp_interact",
13577#endif
13578#ifdef FEAT_XCLIPBOARD
13579 "xterm_clipboard",
13580#endif
13581#ifdef FEAT_XTERM_SAVE
13582 "xterm_save",
13583#endif
13584#if defined(UNIX) && defined(FEAT_X11)
13585 "X11",
13586#endif
13587 NULL
13588 };
13589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013590 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 for (i = 0; has_list[i] != NULL; ++i)
13592 if (STRICMP(name, has_list[i]) == 0)
13593 {
13594 n = TRUE;
13595 break;
13596 }
13597
13598 if (n == FALSE)
13599 {
13600 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013601 {
13602 if (name[5] == '-'
13603 && STRLEN(name) > 11
13604 && vim_isdigit(name[6])
13605 && vim_isdigit(name[8])
13606 && vim_isdigit(name[10]))
13607 {
13608 int major = atoi((char *)name + 6);
13609 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013610
13611 /* Expect "patch-9.9.01234". */
13612 n = (major < VIM_VERSION_MAJOR
13613 || (major == VIM_VERSION_MAJOR
13614 && (minor < VIM_VERSION_MINOR
13615 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013616 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013617 }
13618 else
13619 n = has_patch(atoi((char *)name + 5));
13620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 else if (STRICMP(name, "vim_starting") == 0)
13622 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013623#ifdef FEAT_MBYTE
13624 else if (STRICMP(name, "multi_byte_encoding") == 0)
13625 n = has_mbyte;
13626#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013627#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13628 else if (STRICMP(name, "balloon_multiline") == 0)
13629 n = multiline_balloon_available();
13630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631#ifdef DYNAMIC_TCL
13632 else if (STRICMP(name, "tcl") == 0)
13633 n = tcl_enabled(FALSE);
13634#endif
13635#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13636 else if (STRICMP(name, "iconv") == 0)
13637 n = iconv_enabled(FALSE);
13638#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013639#ifdef DYNAMIC_LUA
13640 else if (STRICMP(name, "lua") == 0)
13641 n = lua_enabled(FALSE);
13642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013643#ifdef DYNAMIC_MZSCHEME
13644 else if (STRICMP(name, "mzscheme") == 0)
13645 n = mzscheme_enabled(FALSE);
13646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647#ifdef DYNAMIC_RUBY
13648 else if (STRICMP(name, "ruby") == 0)
13649 n = ruby_enabled(FALSE);
13650#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013651#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652#ifdef DYNAMIC_PYTHON
13653 else if (STRICMP(name, "python") == 0)
13654 n = python_enabled(FALSE);
13655#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013656#endif
13657#ifdef FEAT_PYTHON3
13658#ifdef DYNAMIC_PYTHON3
13659 else if (STRICMP(name, "python3") == 0)
13660 n = python3_enabled(FALSE);
13661#endif
13662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663#ifdef DYNAMIC_PERL
13664 else if (STRICMP(name, "perl") == 0)
13665 n = perl_enabled(FALSE);
13666#endif
13667#ifdef FEAT_GUI
13668 else if (STRICMP(name, "gui_running") == 0)
13669 n = (gui.in_use || gui.starting);
13670# ifdef FEAT_GUI_W32
13671 else if (STRICMP(name, "gui_win32s") == 0)
13672 n = gui_is_win32s();
13673# endif
13674# ifdef FEAT_BROWSE
13675 else if (STRICMP(name, "browse") == 0)
13676 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13677# endif
13678#endif
13679#ifdef FEAT_SYN_HL
13680 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013681 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682#endif
13683#if defined(WIN3264)
13684 else if (STRICMP(name, "win95") == 0)
13685 n = mch_windows95();
13686#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013687#ifdef FEAT_NETBEANS_INTG
13688 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013689 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 }
13692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013693 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694}
13695
13696/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013697 * "has_key()" function
13698 */
13699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013700f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013701{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013702 if (argvars[0].v_type != VAR_DICT)
13703 {
13704 EMSG(_(e_dictreq));
13705 return;
13706 }
13707 if (argvars[0].vval.v_dict == NULL)
13708 return;
13709
13710 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013711 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013712}
13713
13714/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013715 * "haslocaldir()" function
13716 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013718f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013719{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013720 win_T *wp = NULL;
13721
13722 wp = find_tabwin(&argvars[0], &argvars[1]);
13723 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013724}
13725
13726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 * "hasmapto()" function
13728 */
13729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013730f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731{
13732 char_u *name;
13733 char_u *mode;
13734 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013735 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013737 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013738 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 mode = (char_u *)"nvo";
13740 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013741 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013742 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013743 if (argvars[2].v_type != VAR_UNKNOWN)
13744 abbr = get_tv_number(&argvars[2]);
13745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746
Bram Moolenaar2c932302006-03-18 21:42:09 +000013747 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013750 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751}
13752
13753/*
13754 * "histadd()" function
13755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013757f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758{
13759#ifdef FEAT_CMDHIST
13760 int histype;
13761 char_u *str;
13762 char_u buf[NUMBUFLEN];
13763#endif
13764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013765 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766 if (check_restricted() || check_secure())
13767 return;
13768#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13770 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771 if (histype >= 0)
13772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013773 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 if (*str != NUL)
13775 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013776 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 return;
13780 }
13781 }
13782#endif
13783}
13784
13785/*
13786 * "histdel()" function
13787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013789f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790{
13791#ifdef FEAT_CMDHIST
13792 int n;
13793 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013794 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013796 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13797 if (str == NULL)
13798 n = 0;
13799 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013801 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013802 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013804 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013805 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806 else
13807 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013808 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013809 get_tv_string_buf(&argvars[1], buf));
13810 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811#endif
13812}
13813
13814/*
13815 * "histget()" function
13816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013818f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819{
13820#ifdef FEAT_CMDHIST
13821 int type;
13822 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013823 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013825 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13826 if (str == NULL)
13827 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013829 {
13830 type = get_histtype(str);
13831 if (argvars[1].v_type == VAR_UNKNOWN)
13832 idx = get_history_idx(type);
13833 else
13834 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13835 /* -1 on type error */
13836 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013839 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013841 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842}
13843
13844/*
13845 * "histnr()" function
13846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013848f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849{
13850 int i;
13851
13852#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013853 char_u *history = get_tv_string_chk(&argvars[0]);
13854
13855 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856 if (i >= HIST_CMD && i < HIST_COUNT)
13857 i = get_history_idx(i);
13858 else
13859#endif
13860 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013861 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862}
13863
13864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 * "highlightID(name)" function
13866 */
13867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013868f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013870 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871}
13872
13873/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013874 * "highlight_exists()" function
13875 */
13876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013877f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013878{
13879 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13880}
13881
13882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 * "hostname()" function
13884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013886f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887{
13888 char_u hostname[256];
13889
13890 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013891 rettv->v_type = VAR_STRING;
13892 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893}
13894
13895/*
13896 * iconv() function
13897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013899f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900{
13901#ifdef FEAT_MBYTE
13902 char_u buf1[NUMBUFLEN];
13903 char_u buf2[NUMBUFLEN];
13904 char_u *from, *to, *str;
13905 vimconv_T vimconv;
13906#endif
13907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013908 rettv->v_type = VAR_STRING;
13909 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910
13911#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013912 str = get_tv_string(&argvars[0]);
13913 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13914 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 vimconv.vc_type = CONV_NONE;
13916 convert_setup(&vimconv, from, to);
13917
13918 /* If the encodings are equal, no conversion needed. */
13919 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013920 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013922 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923
13924 convert_setup(&vimconv, NULL, NULL);
13925 vim_free(from);
13926 vim_free(to);
13927#endif
13928}
13929
13930/*
13931 * "indent()" function
13932 */
13933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013934f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935{
13936 linenr_T lnum;
13937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013938 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013940 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013942 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943}
13944
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013945/*
13946 * "index()" function
13947 */
13948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013949f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013950{
Bram Moolenaar33570922005-01-25 22:26:29 +000013951 list_T *l;
13952 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013953 long idx = 0;
13954 int ic = FALSE;
13955
13956 rettv->vval.v_number = -1;
13957 if (argvars[0].v_type != VAR_LIST)
13958 {
13959 EMSG(_(e_listreq));
13960 return;
13961 }
13962 l = argvars[0].vval.v_list;
13963 if (l != NULL)
13964 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013965 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013966 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013967 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013968 int error = FALSE;
13969
Bram Moolenaar758711c2005-02-02 23:11:38 +000013970 /* Start at specified item. Use the cached index that list_find()
13971 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013972 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013973 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013974 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975 ic = get_tv_number_chk(&argvars[3], &error);
13976 if (error)
13977 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013978 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013979
Bram Moolenaar758711c2005-02-02 23:11:38 +000013980 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013981 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013982 {
13983 rettv->vval.v_number = idx;
13984 break;
13985 }
13986 }
13987}
13988
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989static int inputsecret_flag = 0;
13990
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013991static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013992
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013994 * This function is used by f_input() and f_inputdialog() functions. The third
13995 * argument to f_input() specifies the type of completion to use at the
13996 * prompt. The third argument to f_inputdialog() specifies the value to return
13997 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998 */
13999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014000get_user_input(
14001 typval_T *argvars,
14002 typval_T *rettv,
14003 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014005 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006 char_u *p = NULL;
14007 int c;
14008 char_u buf[NUMBUFLEN];
14009 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014010 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014011 int xp_type = EXPAND_NOTHING;
14012 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014014 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014015 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016
14017#ifdef NO_CONSOLE_INPUT
14018 /* While starting up, there is no place to enter text. */
14019 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021#endif
14022
14023 cmd_silent = FALSE; /* Want to see the prompt. */
14024 if (prompt != NULL)
14025 {
14026 /* Only the part of the message after the last NL is considered as
14027 * prompt for the command line */
14028 p = vim_strrchr(prompt, '\n');
14029 if (p == NULL)
14030 p = prompt;
14031 else
14032 {
14033 ++p;
14034 c = *p;
14035 *p = NUL;
14036 msg_start();
14037 msg_clr_eos();
14038 msg_puts_attr(prompt, echo_attr);
14039 msg_didout = FALSE;
14040 msg_starthere();
14041 *p = c;
14042 }
14043 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014045 if (argvars[1].v_type != VAR_UNKNOWN)
14046 {
14047 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14048 if (defstr != NULL)
14049 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014051 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014052 {
14053 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014054 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014055 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014056
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014057 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014058 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014059
Bram Moolenaar4463f292005-09-25 22:20:24 +000014060 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14061 if (xp_name == NULL)
14062 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014063
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014064 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014065
Bram Moolenaar4463f292005-09-25 22:20:24 +000014066 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14067 &xp_arg) == FAIL)
14068 return;
14069 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014070 }
14071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014072 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014073 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014074 int save_ex_normal_busy = ex_normal_busy;
14075 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014076 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014077 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14078 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014079 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014080 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014081 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014082 && argvars[1].v_type != VAR_UNKNOWN
14083 && argvars[2].v_type != VAR_UNKNOWN)
14084 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14085 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014086
14087 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014089 /* since the user typed this, no need to wait for return */
14090 need_wait_return = FALSE;
14091 msg_didout = FALSE;
14092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 cmd_silent = cmd_silent_save;
14094}
14095
14096/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014097 * "input()" function
14098 * Also handles inputsecret() when inputsecret is set.
14099 */
14100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014101f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014102{
14103 get_user_input(argvars, rettv, FALSE);
14104}
14105
14106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107 * "inputdialog()" function
14108 */
14109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014110f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111{
14112#if defined(FEAT_GUI_TEXTDIALOG)
14113 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14114 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14115 {
14116 char_u *message;
14117 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014118 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014120 message = get_tv_string_chk(&argvars[0]);
14121 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014122 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014123 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 else
14125 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014126 if (message != NULL && defstr != NULL
14127 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014128 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 else
14131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014132 if (message != NULL && defstr != NULL
14133 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014134 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014135 rettv->vval.v_string = vim_strsave(
14136 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014138 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014140 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 }
14142 else
14143#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014144 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145}
14146
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014147/*
14148 * "inputlist()" function
14149 */
14150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014151f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014152{
14153 listitem_T *li;
14154 int selected;
14155 int mouse_used;
14156
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014157#ifdef NO_CONSOLE_INPUT
14158 /* While starting up, there is no place to enter text. */
14159 if (no_console_input())
14160 return;
14161#endif
14162 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14163 {
14164 EMSG2(_(e_listarg), "inputlist()");
14165 return;
14166 }
14167
14168 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014169 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014170 lines_left = Rows; /* avoid more prompt */
14171 msg_scroll = TRUE;
14172 msg_clr_eos();
14173
14174 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14175 {
14176 msg_puts(get_tv_string(&li->li_tv));
14177 msg_putchar('\n');
14178 }
14179
14180 /* Ask for choice. */
14181 selected = prompt_for_number(&mouse_used);
14182 if (mouse_used)
14183 selected -= lines_left;
14184
14185 rettv->vval.v_number = selected;
14186}
14187
14188
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14190
14191/*
14192 * "inputrestore()" function
14193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014195f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196{
14197 if (ga_userinput.ga_len > 0)
14198 {
14199 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14201 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014202 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203 }
14204 else if (p_verbose > 1)
14205 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014206 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014207 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 }
14209}
14210
14211/*
14212 * "inputsave()" function
14213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014215f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014217 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218 if (ga_grow(&ga_userinput, 1) == OK)
14219 {
14220 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14221 + ga_userinput.ga_len);
14222 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014223 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224 }
14225 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014226 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227}
14228
14229/*
14230 * "inputsecret()" function
14231 */
14232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014233f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234{
14235 ++cmdline_star;
14236 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014237 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 --cmdline_star;
14239 --inputsecret_flag;
14240}
14241
14242/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014243 * "insert()" function
14244 */
14245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014246f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014247{
14248 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014249 listitem_T *item;
14250 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014252
14253 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014254 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014255 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014256 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014257 {
14258 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014259 before = get_tv_number_chk(&argvars[2], &error);
14260 if (error)
14261 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014262
Bram Moolenaar758711c2005-02-02 23:11:38 +000014263 if (before == l->lv_len)
14264 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014265 else
14266 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014267 item = list_find(l, before);
14268 if (item == NULL)
14269 {
14270 EMSGN(_(e_listidx), before);
14271 l = NULL;
14272 }
14273 }
14274 if (l != NULL)
14275 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014276 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014277 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014278 }
14279 }
14280}
14281
14282/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014283 * "invert(expr)" function
14284 */
14285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014286f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014287{
14288 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14289}
14290
14291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292 * "isdirectory()" function
14293 */
14294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014295f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014297 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298}
14299
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014300/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014301 * "islocked()" function
14302 */
14303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014304f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014305{
14306 lval_T lv;
14307 char_u *end;
14308 dictitem_T *di;
14309
14310 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014311 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14312 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014313 if (end != NULL && lv.ll_name != NULL)
14314 {
14315 if (*end != NUL)
14316 EMSG(_(e_trailing));
14317 else
14318 {
14319 if (lv.ll_tv == NULL)
14320 {
14321 if (check_changedtick(lv.ll_name))
14322 rettv->vval.v_number = 1; /* always locked */
14323 else
14324 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014325 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014326 if (di != NULL)
14327 {
14328 /* Consider a variable locked when:
14329 * 1. the variable itself is locked
14330 * 2. the value of the variable is locked.
14331 * 3. the List or Dict value is locked.
14332 */
14333 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14334 || tv_islocked(&di->di_tv));
14335 }
14336 }
14337 }
14338 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014339 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014340 else if (lv.ll_newkey != NULL)
14341 EMSG2(_(e_dictkey), lv.ll_newkey);
14342 else if (lv.ll_list != NULL)
14343 /* List item. */
14344 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14345 else
14346 /* Dictionary item. */
14347 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14348 }
14349 }
14350
14351 clear_lval(&lv);
14352}
14353
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014354static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014355
14356/*
14357 * Turn a dict into a list:
14358 * "what" == 0: list of keys
14359 * "what" == 1: list of values
14360 * "what" == 2: list of items
14361 */
14362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014363dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014364{
Bram Moolenaar33570922005-01-25 22:26:29 +000014365 list_T *l2;
14366 dictitem_T *di;
14367 hashitem_T *hi;
14368 listitem_T *li;
14369 listitem_T *li2;
14370 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014371 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014372
Bram Moolenaar8c711452005-01-14 21:53:12 +000014373 if (argvars[0].v_type != VAR_DICT)
14374 {
14375 EMSG(_(e_dictreq));
14376 return;
14377 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014378 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014379 return;
14380
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014381 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014382 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014383
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014384 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014385 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014386 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014387 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014388 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014389 --todo;
14390 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014391
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014392 li = listitem_alloc();
14393 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014394 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014395 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014396
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014397 if (what == 0)
14398 {
14399 /* keys() */
14400 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014401 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014402 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14403 }
14404 else if (what == 1)
14405 {
14406 /* values() */
14407 copy_tv(&di->di_tv, &li->li_tv);
14408 }
14409 else
14410 {
14411 /* items() */
14412 l2 = list_alloc();
14413 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014414 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014415 li->li_tv.vval.v_list = l2;
14416 if (l2 == NULL)
14417 break;
14418 ++l2->lv_refcount;
14419
14420 li2 = listitem_alloc();
14421 if (li2 == NULL)
14422 break;
14423 list_append(l2, li2);
14424 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014425 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014426 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14427
14428 li2 = listitem_alloc();
14429 if (li2 == NULL)
14430 break;
14431 list_append(l2, li2);
14432 copy_tv(&di->di_tv, &li2->li_tv);
14433 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014434 }
14435 }
14436}
14437
14438/*
14439 * "items(dict)" function
14440 */
14441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014442f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014443{
14444 dict_list(argvars, rettv, 2);
14445}
14446
Bram Moolenaar835dc632016-02-07 14:27:38 +010014447#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014448
14449# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014450/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014451 * "job_getchannel()" function
14452 */
14453 static void
14454f_job_getchannel(typval_T *argvars, typval_T *rettv)
14455{
14456 if (argvars[0].v_type != VAR_JOB)
14457 EMSG(_(e_invarg));
14458 else
14459 {
14460 job_T *job = argvars[0].vval.v_job;
14461
Bram Moolenaar77073442016-02-13 23:23:53 +010014462 rettv->v_type = VAR_CHANNEL;
14463 rettv->vval.v_channel = job->jv_channel;
14464 if (job->jv_channel != NULL)
14465 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014466 }
14467}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014468# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014469
14470/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014471 * "job_start()" function
14472 */
14473 static void
14474f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14475{
14476 job_T *job;
14477 char_u *cmd = NULL;
14478#if defined(UNIX)
14479# define USE_ARGV
14480 char **argv = NULL;
14481 int argc = 0;
14482#else
14483 garray_T ga;
14484#endif
14485
14486 rettv->v_type = VAR_JOB;
14487 job = job_alloc();
14488 rettv->vval.v_job = job;
14489 if (job == NULL)
14490 return;
14491
14492 rettv->vval.v_job->jv_status = JOB_FAILED;
14493#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014494 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014495#endif
14496
14497 if (argvars[0].v_type == VAR_STRING)
14498 {
14499 /* Command is a string. */
14500 cmd = argvars[0].vval.v_string;
14501#ifdef USE_ARGV
14502 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14503 return;
14504 argv[argc] = NULL;
14505#endif
14506 }
14507 else if (argvars[0].v_type != VAR_LIST
14508 || argvars[0].vval.v_list == NULL
14509 || argvars[0].vval.v_list->lv_len < 1)
14510 {
14511 EMSG(_(e_invarg));
14512 return;
14513 }
14514 else
14515 {
14516 list_T *l = argvars[0].vval.v_list;
14517 listitem_T *li;
14518 char_u *s;
14519
14520#ifdef USE_ARGV
14521 /* Pass argv[] to mch_call_shell(). */
14522 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14523 if (argv == NULL)
14524 return;
14525#endif
14526 for (li = l->lv_first; li != NULL; li = li->li_next)
14527 {
14528 s = get_tv_string_chk(&li->li_tv);
14529 if (s == NULL)
14530 goto theend;
14531#ifdef USE_ARGV
14532 argv[argc++] = (char *)s;
14533#else
14534 if (li != l->lv_first)
14535 {
14536 s = vim_strsave_shellescape(s, FALSE, TRUE);
14537 if (s == NULL)
14538 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014539 ga_concat(&ga, s);
14540 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014541 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014542 else
14543 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014544 if (li->li_next != NULL)
14545 ga_append(&ga, ' ');
14546#endif
14547 }
14548#ifdef USE_ARGV
14549 argv[argc] = NULL;
14550#else
14551 cmd = ga.ga_data;
14552#endif
14553 }
14554#ifdef USE_ARGV
14555 mch_start_job(argv, job);
14556#else
14557 mch_start_job(cmd, job);
14558#endif
14559
14560theend:
14561#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014562 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014563#else
14564 vim_free(ga.ga_data);
14565#endif
14566}
14567
14568/*
14569 * "job_status()" function
14570 */
14571 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014572f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014573{
14574 char *result;
14575
14576 if (argvars[0].v_type != VAR_JOB)
14577 EMSG(_(e_invarg));
14578 else
14579 {
14580 job_T *job = argvars[0].vval.v_job;
14581
14582 if (job->jv_status == JOB_ENDED)
14583 /* No need to check, dead is dead. */
14584 result = "dead";
14585 else if (job->jv_status == JOB_FAILED)
14586 result = "fail";
14587 else
14588 result = mch_job_status(job);
14589 rettv->v_type = VAR_STRING;
14590 rettv->vval.v_string = vim_strsave((char_u *)result);
14591 }
14592}
14593
14594/*
14595 * "job_stop()" function
14596 */
14597 static void
14598f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14599{
14600 if (argvars[0].v_type != VAR_JOB)
14601 EMSG(_(e_invarg));
14602 else
14603 {
14604 char_u *arg;
14605
14606 if (argvars[1].v_type == VAR_UNKNOWN)
14607 arg = (char_u *)"";
14608 else
14609 {
14610 arg = get_tv_string_chk(&argvars[1]);
14611 if (arg == NULL)
14612 {
14613 EMSG(_(e_invarg));
14614 return;
14615 }
14616 }
14617 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14618 rettv->vval.v_number = 0;
14619 else
14620 rettv->vval.v_number = 1;
14621 }
14622}
14623#endif
14624
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014626 * "join()" function
14627 */
14628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014629f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014630{
14631 garray_T ga;
14632 char_u *sep;
14633
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014634 if (argvars[0].v_type != VAR_LIST)
14635 {
14636 EMSG(_(e_listreq));
14637 return;
14638 }
14639 if (argvars[0].vval.v_list == NULL)
14640 return;
14641 if (argvars[1].v_type == VAR_UNKNOWN)
14642 sep = (char_u *)" ";
14643 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014644 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014645
14646 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014647
14648 if (sep != NULL)
14649 {
14650 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014651 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014652 ga_append(&ga, NUL);
14653 rettv->vval.v_string = (char_u *)ga.ga_data;
14654 }
14655 else
14656 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014657}
14658
14659/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014660 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014661 */
14662 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014663f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014664{
14665 js_read_T reader;
14666
14667 reader.js_buf = get_tv_string(&argvars[0]);
14668 reader.js_fill = NULL;
14669 reader.js_used = 0;
14670 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14671 EMSG(_(e_invarg));
14672}
14673
14674/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014675 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014676 */
14677 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014678f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014679{
14680 rettv->v_type = VAR_STRING;
14681 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14682}
14683
14684/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014685 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014686 */
14687 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014688f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014689{
14690 js_read_T reader;
14691
14692 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014693 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014694 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014695 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014696 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014697}
14698
14699/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014700 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014701 */
14702 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014703f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014704{
14705 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014706 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014707}
14708
14709/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014710 * "keys()" function
14711 */
14712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014713f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014714{
14715 dict_list(argvars, rettv, 0);
14716}
14717
14718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719 * "last_buffer_nr()" function.
14720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014722f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723{
14724 int n = 0;
14725 buf_T *buf;
14726
14727 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14728 if (n < buf->b_fnum)
14729 n = buf->b_fnum;
14730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014731 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014732}
14733
14734/*
14735 * "len()" function
14736 */
14737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014738f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014739{
14740 switch (argvars[0].v_type)
14741 {
14742 case VAR_STRING:
14743 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014744 rettv->vval.v_number = (varnumber_T)STRLEN(
14745 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014746 break;
14747 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014748 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014749 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014750 case VAR_DICT:
14751 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14752 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014753 case VAR_UNKNOWN:
14754 case VAR_SPECIAL:
14755 case VAR_FLOAT:
14756 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014757 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014758 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014759 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014760 break;
14761 }
14762}
14763
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014764static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014765
14766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014767libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014768{
14769#ifdef FEAT_LIBCALL
14770 char_u *string_in;
14771 char_u **string_result;
14772 int nr_result;
14773#endif
14774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014775 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014776 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014777 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014778
14779 if (check_restricted() || check_secure())
14780 return;
14781
14782#ifdef FEAT_LIBCALL
14783 /* The first two args must be strings, otherwise its meaningless */
14784 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14785 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014786 string_in = NULL;
14787 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014788 string_in = argvars[2].vval.v_string;
14789 if (type == VAR_NUMBER)
14790 string_result = NULL;
14791 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014792 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014793 if (mch_libcall(argvars[0].vval.v_string,
14794 argvars[1].vval.v_string,
14795 string_in,
14796 argvars[2].vval.v_number,
14797 string_result,
14798 &nr_result) == OK
14799 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014800 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014801 }
14802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803}
14804
14805/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014806 * "libcall()" function
14807 */
14808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014809f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014810{
14811 libcall_common(argvars, rettv, VAR_STRING);
14812}
14813
14814/*
14815 * "libcallnr()" function
14816 */
14817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014818f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014819{
14820 libcall_common(argvars, rettv, VAR_NUMBER);
14821}
14822
14823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 * "line(string)" function
14825 */
14826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014827f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828{
14829 linenr_T lnum = 0;
14830 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014831 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014833 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 if (fp != NULL)
14835 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014836 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837}
14838
14839/*
14840 * "line2byte(lnum)" function
14841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014843f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844{
14845#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847#else
14848 linenr_T lnum;
14849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014850 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014852 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014854 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14855 if (rettv->vval.v_number >= 0)
14856 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857#endif
14858}
14859
14860/*
14861 * "lispindent(lnum)" function
14862 */
14863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014864f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865{
14866#ifdef FEAT_LISP
14867 pos_T pos;
14868 linenr_T lnum;
14869
14870 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014871 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14873 {
14874 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014875 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876 curwin->w_cursor = pos;
14877 }
14878 else
14879#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014880 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881}
14882
14883/*
14884 * "localtime()" function
14885 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014887f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014889 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890}
14891
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014892static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893
14894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014895get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896{
14897 char_u *keys;
14898 char_u *which;
14899 char_u buf[NUMBUFLEN];
14900 char_u *keys_buf = NULL;
14901 char_u *rhs;
14902 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014903 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014904 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014905 mapblock_T *mp;
14906 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907
14908 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014909 rettv->v_type = VAR_STRING;
14910 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014912 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913 if (*keys == NUL)
14914 return;
14915
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014916 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014917 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014918 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014919 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014920 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014921 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014922 if (argvars[3].v_type != VAR_UNKNOWN)
14923 get_dict = get_tv_number(&argvars[3]);
14924 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926 else
14927 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014928 if (which == NULL)
14929 return;
14930
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 mode = get_map_mode(&which, 0);
14932
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014933 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014934 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014936
14937 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014939 /* Return a string. */
14940 if (rhs != NULL)
14941 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942
Bram Moolenaarbd743252010-10-20 21:23:33 +020014943 }
14944 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14945 {
14946 /* Return a dictionary. */
14947 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14948 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14949 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950
Bram Moolenaarbd743252010-10-20 21:23:33 +020014951 dict_add_nr_str(dict, "lhs", 0L, lhs);
14952 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14953 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14954 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14955 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14956 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14957 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014958 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014959 dict_add_nr_str(dict, "mode", 0L, mapmode);
14960
14961 vim_free(lhs);
14962 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963 }
14964}
14965
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014966#ifdef FEAT_FLOAT
14967/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014968 * "log()" function
14969 */
14970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014971f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014972{
14973 float_T f;
14974
14975 rettv->v_type = VAR_FLOAT;
14976 if (get_float_arg(argvars, &f) == OK)
14977 rettv->vval.v_float = log(f);
14978 else
14979 rettv->vval.v_float = 0.0;
14980}
14981
14982/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014983 * "log10()" function
14984 */
14985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014986f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014987{
14988 float_T f;
14989
14990 rettv->v_type = VAR_FLOAT;
14991 if (get_float_arg(argvars, &f) == OK)
14992 rettv->vval.v_float = log10(f);
14993 else
14994 rettv->vval.v_float = 0.0;
14995}
14996#endif
14997
Bram Moolenaar1dced572012-04-05 16:54:08 +020014998#ifdef FEAT_LUA
14999/*
15000 * "luaeval()" function
15001 */
15002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015003f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015004{
15005 char_u *str;
15006 char_u buf[NUMBUFLEN];
15007
15008 str = get_tv_string_buf(&argvars[0], buf);
15009 do_luaeval(str, argvars + 1, rettv);
15010}
15011#endif
15012
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015014 * "map()" function
15015 */
15016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015017f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015018{
15019 filter_map(argvars, rettv, TRUE);
15020}
15021
15022/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015023 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 */
15025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015026f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015028 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029}
15030
15031/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015032 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033 */
15034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015035f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015037 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038}
15039
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015040static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041
15042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015043find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015045 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015046 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015047 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015048 char_u *pat;
15049 regmatch_T regmatch;
15050 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015051 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052 char_u *save_cpo;
15053 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015054 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015055 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015056 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015057 list_T *l = NULL;
15058 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015059 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015060 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061
15062 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15063 save_cpo = p_cpo;
15064 p_cpo = (char_u *)"";
15065
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015066 rettv->vval.v_number = -1;
15067 if (type == 3)
15068 {
15069 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015070 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015071 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015072 }
15073 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015075 rettv->v_type = VAR_STRING;
15076 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015079 if (argvars[0].v_type == VAR_LIST)
15080 {
15081 if ((l = argvars[0].vval.v_list) == NULL)
15082 goto theend;
15083 li = l->lv_first;
15084 }
15085 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015086 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015087 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015088 len = (long)STRLEN(str);
15089 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015090
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015091 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15092 if (pat == NULL)
15093 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015094
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015095 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015097 int error = FALSE;
15098
15099 start = get_tv_number_chk(&argvars[2], &error);
15100 if (error)
15101 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015102 if (l != NULL)
15103 {
15104 li = list_find(l, start);
15105 if (li == NULL)
15106 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015107 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015108 }
15109 else
15110 {
15111 if (start < 0)
15112 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015113 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015114 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015115 /* When "count" argument is there ignore matches before "start",
15116 * otherwise skip part of the string. Differs when pattern is "^"
15117 * or "\<". */
15118 if (argvars[3].v_type != VAR_UNKNOWN)
15119 startcol = start;
15120 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015121 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015122 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015123 len -= start;
15124 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015125 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015126
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015127 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015128 nth = get_tv_number_chk(&argvars[3], &error);
15129 if (error)
15130 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 }
15132
15133 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15134 if (regmatch.regprog != NULL)
15135 {
15136 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015137
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015138 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015139 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015140 if (l != NULL)
15141 {
15142 if (li == NULL)
15143 {
15144 match = FALSE;
15145 break;
15146 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015147 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015148 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015149 if (str == NULL)
15150 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015151 }
15152
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015153 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015154
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015155 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015156 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015157 if (l == NULL && !match)
15158 break;
15159
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015160 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015161 if (l != NULL)
15162 {
15163 li = li->li_next;
15164 ++idx;
15165 }
15166 else
15167 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015168#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015169 startcol = (colnr_T)(regmatch.startp[0]
15170 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015171#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015172 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015173#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015174 if (startcol > (colnr_T)len
15175 || str + startcol <= regmatch.startp[0])
15176 {
15177 match = FALSE;
15178 break;
15179 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015180 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015181 }
15182
15183 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015185 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015186 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015187 int i;
15188
15189 /* return list with matched string and submatches */
15190 for (i = 0; i < NSUBEXP; ++i)
15191 {
15192 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015193 {
15194 if (list_append_string(rettv->vval.v_list,
15195 (char_u *)"", 0) == FAIL)
15196 break;
15197 }
15198 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015199 regmatch.startp[i],
15200 (int)(regmatch.endp[i] - regmatch.startp[i]))
15201 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015202 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015203 }
15204 }
15205 else if (type == 2)
15206 {
15207 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015208 if (l != NULL)
15209 copy_tv(&li->li_tv, rettv);
15210 else
15211 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015212 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015213 }
15214 else if (l != NULL)
15215 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216 else
15217 {
15218 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015219 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220 (varnumber_T)(regmatch.startp[0] - str);
15221 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015222 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015224 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225 }
15226 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015227 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228 }
15229
15230theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015231 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232 p_cpo = save_cpo;
15233}
15234
15235/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015236 * "match()" function
15237 */
15238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015239f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015240{
15241 find_some_match(argvars, rettv, 1);
15242}
15243
15244/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015245 * "matchadd()" function
15246 */
15247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015248f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015249{
15250#ifdef FEAT_SEARCH_EXTRA
15251 char_u buf[NUMBUFLEN];
15252 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15253 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15254 int prio = 10; /* default priority */
15255 int id = -1;
15256 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015257 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015258
15259 rettv->vval.v_number = -1;
15260
15261 if (grp == NULL || pat == NULL)
15262 return;
15263 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015264 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015265 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015266 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015267 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015268 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015269 if (argvars[4].v_type != VAR_UNKNOWN)
15270 {
15271 if (argvars[4].v_type != VAR_DICT)
15272 {
15273 EMSG(_(e_dictreq));
15274 return;
15275 }
15276 if (dict_find(argvars[4].vval.v_dict,
15277 (char_u *)"conceal", -1) != NULL)
15278 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15279 (char_u *)"conceal", FALSE);
15280 }
15281 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015282 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015283 if (error == TRUE)
15284 return;
15285 if (id >= 1 && id <= 3)
15286 {
15287 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15288 return;
15289 }
15290
Bram Moolenaar6561d522015-07-21 15:48:27 +020015291 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15292 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015293#endif
15294}
15295
15296/*
15297 * "matchaddpos()" function
15298 */
15299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015300f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015301{
15302#ifdef FEAT_SEARCH_EXTRA
15303 char_u buf[NUMBUFLEN];
15304 char_u *group;
15305 int prio = 10;
15306 int id = -1;
15307 int error = FALSE;
15308 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015309 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015310
15311 rettv->vval.v_number = -1;
15312
15313 group = get_tv_string_buf_chk(&argvars[0], buf);
15314 if (group == NULL)
15315 return;
15316
15317 if (argvars[1].v_type != VAR_LIST)
15318 {
15319 EMSG2(_(e_listarg), "matchaddpos()");
15320 return;
15321 }
15322 l = argvars[1].vval.v_list;
15323 if (l == NULL)
15324 return;
15325
15326 if (argvars[2].v_type != VAR_UNKNOWN)
15327 {
15328 prio = get_tv_number_chk(&argvars[2], &error);
15329 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015330 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015331 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015332 if (argvars[4].v_type != VAR_UNKNOWN)
15333 {
15334 if (argvars[4].v_type != VAR_DICT)
15335 {
15336 EMSG(_(e_dictreq));
15337 return;
15338 }
15339 if (dict_find(argvars[4].vval.v_dict,
15340 (char_u *)"conceal", -1) != NULL)
15341 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15342 (char_u *)"conceal", FALSE);
15343 }
15344 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015345 }
15346 if (error == TRUE)
15347 return;
15348
15349 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15350 if (id == 1 || id == 2)
15351 {
15352 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15353 return;
15354 }
15355
Bram Moolenaar6561d522015-07-21 15:48:27 +020015356 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15357 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015358#endif
15359}
15360
15361/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015362 * "matcharg()" function
15363 */
15364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015365f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015366{
15367 if (rettv_list_alloc(rettv) == OK)
15368 {
15369#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015370 int id = get_tv_number(&argvars[0]);
15371 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015372
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015373 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015374 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015375 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15376 {
15377 list_append_string(rettv->vval.v_list,
15378 syn_id2name(m->hlg_id), -1);
15379 list_append_string(rettv->vval.v_list, m->pattern, -1);
15380 }
15381 else
15382 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015383 list_append_string(rettv->vval.v_list, NULL, -1);
15384 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015385 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015386 }
15387#endif
15388 }
15389}
15390
15391/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015392 * "matchdelete()" function
15393 */
15394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015395f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015396{
15397#ifdef FEAT_SEARCH_EXTRA
15398 rettv->vval.v_number = match_delete(curwin,
15399 (int)get_tv_number(&argvars[0]), TRUE);
15400#endif
15401}
15402
15403/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015404 * "matchend()" function
15405 */
15406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015407f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015408{
15409 find_some_match(argvars, rettv, 0);
15410}
15411
15412/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015413 * "matchlist()" function
15414 */
15415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015416f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015417{
15418 find_some_match(argvars, rettv, 3);
15419}
15420
15421/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015422 * "matchstr()" function
15423 */
15424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015425f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015426{
15427 find_some_match(argvars, rettv, 2);
15428}
15429
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015430static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015431
15432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015433max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015434{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015435 long n = 0;
15436 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015437 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015438
15439 if (argvars[0].v_type == VAR_LIST)
15440 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015441 list_T *l;
15442 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015443
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015444 l = argvars[0].vval.v_list;
15445 if (l != NULL)
15446 {
15447 li = l->lv_first;
15448 if (li != NULL)
15449 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015450 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015451 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015452 {
15453 li = li->li_next;
15454 if (li == NULL)
15455 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015456 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015457 if (domax ? i > n : i < n)
15458 n = i;
15459 }
15460 }
15461 }
15462 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015463 else if (argvars[0].v_type == VAR_DICT)
15464 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015465 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015466 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015467 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015468 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015469
15470 d = argvars[0].vval.v_dict;
15471 if (d != NULL)
15472 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015473 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015474 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015475 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015476 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015477 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015478 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015479 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015480 if (first)
15481 {
15482 n = i;
15483 first = FALSE;
15484 }
15485 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015486 n = i;
15487 }
15488 }
15489 }
15490 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015491 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015492 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015493 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015494}
15495
15496/*
15497 * "max()" function
15498 */
15499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015500f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015501{
15502 max_min(argvars, rettv, TRUE);
15503}
15504
15505/*
15506 * "min()" function
15507 */
15508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015509f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015510{
15511 max_min(argvars, rettv, FALSE);
15512}
15513
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015514static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015515
15516/*
15517 * Create the directory in which "dir" is located, and higher levels when
15518 * needed.
15519 */
15520 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015521mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015522{
15523 char_u *p;
15524 char_u *updir;
15525 int r = FAIL;
15526
15527 /* Get end of directory name in "dir".
15528 * We're done when it's "/" or "c:/". */
15529 p = gettail_sep(dir);
15530 if (p <= get_past_head(dir))
15531 return OK;
15532
15533 /* If the directory exists we're done. Otherwise: create it.*/
15534 updir = vim_strnsave(dir, (int)(p - dir));
15535 if (updir == NULL)
15536 return FAIL;
15537 if (mch_isdir(updir))
15538 r = OK;
15539 else if (mkdir_recurse(updir, prot) == OK)
15540 r = vim_mkdir_emsg(updir, prot);
15541 vim_free(updir);
15542 return r;
15543}
15544
15545#ifdef vim_mkdir
15546/*
15547 * "mkdir()" function
15548 */
15549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015550f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015551{
15552 char_u *dir;
15553 char_u buf[NUMBUFLEN];
15554 int prot = 0755;
15555
15556 rettv->vval.v_number = FAIL;
15557 if (check_restricted() || check_secure())
15558 return;
15559
15560 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015561 if (*dir == NUL)
15562 rettv->vval.v_number = FAIL;
15563 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015564 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015565 if (*gettail(dir) == NUL)
15566 /* remove trailing slashes */
15567 *gettail_sep(dir) = NUL;
15568
15569 if (argvars[1].v_type != VAR_UNKNOWN)
15570 {
15571 if (argvars[2].v_type != VAR_UNKNOWN)
15572 prot = get_tv_number_chk(&argvars[2], NULL);
15573 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15574 mkdir_recurse(dir, prot);
15575 }
15576 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015577 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015578}
15579#endif
15580
Bram Moolenaar0d660222005-01-07 21:51:51 +000015581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 * "mode()" function
15583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015585f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015587 char_u buf[3];
15588
15589 buf[1] = NUL;
15590 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 if (VIsual_active)
15593 {
15594 if (VIsual_select)
15595 buf[0] = VIsual_mode + 's' - 'v';
15596 else
15597 buf[0] = VIsual_mode;
15598 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015599 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015600 || State == CONFIRM)
15601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015603 if (State == ASKMORE)
15604 buf[1] = 'm';
15605 else if (State == CONFIRM)
15606 buf[1] = '?';
15607 }
15608 else if (State == EXTERNCMD)
15609 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610 else if (State & INSERT)
15611 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015612#ifdef FEAT_VREPLACE
15613 if (State & VREPLACE_FLAG)
15614 {
15615 buf[0] = 'R';
15616 buf[1] = 'v';
15617 }
15618 else
15619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 if (State & REPLACE_FLAG)
15621 buf[0] = 'R';
15622 else
15623 buf[0] = 'i';
15624 }
15625 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015628 if (exmode_active)
15629 buf[1] = 'v';
15630 }
15631 else if (exmode_active)
15632 {
15633 buf[0] = 'c';
15634 buf[1] = 'e';
15635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015637 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015639 if (finish_op)
15640 buf[1] = 'o';
15641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015643 /* Clear out the minor mode when the argument is not a non-zero number or
15644 * non-empty string. */
15645 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015646 buf[1] = NUL;
15647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015648 rettv->vval.v_string = vim_strsave(buf);
15649 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650}
15651
Bram Moolenaar429fa852013-04-15 12:27:36 +020015652#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015653/*
15654 * "mzeval()" function
15655 */
15656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015657f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015658{
15659 char_u *str;
15660 char_u buf[NUMBUFLEN];
15661
15662 str = get_tv_string_buf(&argvars[0], buf);
15663 do_mzeval(str, rettv);
15664}
Bram Moolenaar75676462013-01-30 14:55:42 +010015665
15666 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015667mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015668{
15669 typval_T argvars[3];
15670
15671 argvars[0].v_type = VAR_STRING;
15672 argvars[0].vval.v_string = name;
15673 copy_tv(args, &argvars[1]);
15674 argvars[2].v_type = VAR_UNKNOWN;
15675 f_call(argvars, rettv);
15676 clear_tv(&argvars[1]);
15677}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015678#endif
15679
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015681 * "nextnonblank()" function
15682 */
15683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015684f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015685{
15686 linenr_T lnum;
15687
15688 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015690 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015691 {
15692 lnum = 0;
15693 break;
15694 }
15695 if (*skipwhite(ml_get(lnum)) != NUL)
15696 break;
15697 }
15698 rettv->vval.v_number = lnum;
15699}
15700
15701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 * "nr2char()" function
15703 */
15704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015705f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706{
15707 char_u buf[NUMBUFLEN];
15708
15709#ifdef FEAT_MBYTE
15710 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015711 {
15712 int utf8 = 0;
15713
15714 if (argvars[1].v_type != VAR_UNKNOWN)
15715 utf8 = get_tv_number_chk(&argvars[1], NULL);
15716 if (utf8)
15717 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15718 else
15719 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721 else
15722#endif
15723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015724 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 buf[1] = NUL;
15726 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015727 rettv->v_type = VAR_STRING;
15728 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015729}
15730
15731/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015732 * "or(expr, expr)" function
15733 */
15734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015735f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015736{
15737 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15738 | get_tv_number_chk(&argvars[1], NULL);
15739}
15740
15741/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015742 * "pathshorten()" function
15743 */
15744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015745f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015746{
15747 char_u *p;
15748
15749 rettv->v_type = VAR_STRING;
15750 p = get_tv_string_chk(&argvars[0]);
15751 if (p == NULL)
15752 rettv->vval.v_string = NULL;
15753 else
15754 {
15755 p = vim_strsave(p);
15756 rettv->vval.v_string = p;
15757 if (p != NULL)
15758 shorten_dir(p);
15759 }
15760}
15761
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015762#ifdef FEAT_PERL
15763/*
15764 * "perleval()" function
15765 */
15766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015767f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015768{
15769 char_u *str;
15770 char_u buf[NUMBUFLEN];
15771
15772 str = get_tv_string_buf(&argvars[0], buf);
15773 do_perleval(str, rettv);
15774}
15775#endif
15776
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015777#ifdef FEAT_FLOAT
15778/*
15779 * "pow()" function
15780 */
15781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015782f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015783{
15784 float_T fx, fy;
15785
15786 rettv->v_type = VAR_FLOAT;
15787 if (get_float_arg(argvars, &fx) == OK
15788 && get_float_arg(&argvars[1], &fy) == OK)
15789 rettv->vval.v_float = pow(fx, fy);
15790 else
15791 rettv->vval.v_float = 0.0;
15792}
15793#endif
15794
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015795/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015796 * "prevnonblank()" function
15797 */
15798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015799f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015800{
15801 linenr_T lnum;
15802
15803 lnum = get_tv_lnum(argvars);
15804 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15805 lnum = 0;
15806 else
15807 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15808 --lnum;
15809 rettv->vval.v_number = lnum;
15810}
15811
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015812/* This dummy va_list is here because:
15813 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15814 * - locally in the function results in a "used before set" warning
15815 * - using va_start() to initialize it gives "function with fixed args" error */
15816static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015817
Bram Moolenaar8c711452005-01-14 21:53:12 +000015818/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015819 * "printf()" function
15820 */
15821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015822f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015823{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015824 char_u buf[NUMBUFLEN];
15825 int len;
15826 char_u *s;
15827 int saved_did_emsg = did_emsg;
15828 char *fmt;
15829
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015830 rettv->v_type = VAR_STRING;
15831 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015832
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015833 /* Get the required length, allocate the buffer and do it for real. */
15834 did_emsg = FALSE;
15835 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15836 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15837 if (!did_emsg)
15838 {
15839 s = alloc(len + 1);
15840 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015841 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015842 rettv->vval.v_string = s;
15843 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015844 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015845 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015846 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015847}
15848
15849/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015850 * "pumvisible()" function
15851 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015853f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015854{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015855#ifdef FEAT_INS_EXPAND
15856 if (pum_visible())
15857 rettv->vval.v_number = 1;
15858#endif
15859}
15860
Bram Moolenaardb913952012-06-29 12:54:53 +020015861#ifdef FEAT_PYTHON3
15862/*
15863 * "py3eval()" function
15864 */
15865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015866f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015867{
15868 char_u *str;
15869 char_u buf[NUMBUFLEN];
15870
15871 str = get_tv_string_buf(&argvars[0], buf);
15872 do_py3eval(str, rettv);
15873}
15874#endif
15875
15876#ifdef FEAT_PYTHON
15877/*
15878 * "pyeval()" function
15879 */
15880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015881f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015882{
15883 char_u *str;
15884 char_u buf[NUMBUFLEN];
15885
15886 str = get_tv_string_buf(&argvars[0], buf);
15887 do_pyeval(str, rettv);
15888}
15889#endif
15890
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015891/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015892 * "range()" function
15893 */
15894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015895f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015896{
15897 long start;
15898 long end;
15899 long stride = 1;
15900 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015901 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015903 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015904 if (argvars[1].v_type == VAR_UNKNOWN)
15905 {
15906 end = start - 1;
15907 start = 0;
15908 }
15909 else
15910 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015911 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015912 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015913 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015914 }
15915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015916 if (error)
15917 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015918 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015919 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015920 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015921 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015922 else
15923 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015924 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015925 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015926 if (list_append_number(rettv->vval.v_list,
15927 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015928 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015929 }
15930}
15931
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015932/*
15933 * "readfile()" function
15934 */
15935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015936f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015937{
15938 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015939 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015940 char_u *fname;
15941 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015942 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15943 int io_size = sizeof(buf);
15944 int readlen; /* size of last fread() */
15945 char_u *prev = NULL; /* previously read bytes, if any */
15946 long prevlen = 0; /* length of data in prev */
15947 long prevsize = 0; /* size of prev buffer */
15948 long maxline = MAXLNUM;
15949 long cnt = 0;
15950 char_u *p; /* position in buf */
15951 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015952
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015953 if (argvars[1].v_type != VAR_UNKNOWN)
15954 {
15955 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15956 binary = TRUE;
15957 if (argvars[2].v_type != VAR_UNKNOWN)
15958 maxline = get_tv_number(&argvars[2]);
15959 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015960
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015961 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015962 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015963
15964 /* Always open the file in binary mode, library functions have a mind of
15965 * their own about CR-LF conversion. */
15966 fname = get_tv_string(&argvars[0]);
15967 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15968 {
15969 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15970 return;
15971 }
15972
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015973 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015974 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015975 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015976
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015977 /* This for loop processes what was read, but is also entered at end
15978 * of file so that either:
15979 * - an incomplete line gets written
15980 * - a "binary" file gets an empty line at the end if it ends in a
15981 * newline. */
15982 for (p = buf, start = buf;
15983 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15984 ++p)
15985 {
15986 if (*p == '\n' || readlen <= 0)
15987 {
15988 listitem_T *li;
15989 char_u *s = NULL;
15990 long_u len = p - start;
15991
15992 /* Finished a line. Remove CRs before NL. */
15993 if (readlen > 0 && !binary)
15994 {
15995 while (len > 0 && start[len - 1] == '\r')
15996 --len;
15997 /* removal may cross back to the "prev" string */
15998 if (len == 0)
15999 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16000 --prevlen;
16001 }
16002 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016003 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016004 else
16005 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016006 /* Change "prev" buffer to be the right size. This way
16007 * the bytes are only copied once, and very long lines are
16008 * allocated only once. */
16009 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016010 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016011 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016012 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016013 prev = NULL; /* the list will own the string */
16014 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016015 }
16016 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016017 if (s == NULL)
16018 {
16019 do_outofmem_msg((long_u) prevlen + len + 1);
16020 failed = TRUE;
16021 break;
16022 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016023
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016024 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016025 {
16026 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016027 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016028 break;
16029 }
16030 li->li_tv.v_type = VAR_STRING;
16031 li->li_tv.v_lock = 0;
16032 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016033 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016034
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016035 start = p + 1; /* step over newline */
16036 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016037 break;
16038 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016039 else if (*p == NUL)
16040 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016041#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016042 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16043 * when finding the BF and check the previous two bytes. */
16044 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016045 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016046 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16047 * + 1, these may be in the "prev" string. */
16048 char_u back1 = p >= buf + 1 ? p[-1]
16049 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16050 char_u back2 = p >= buf + 2 ? p[-2]
16051 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16052 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016053
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016054 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016055 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016056 char_u *dest = p - 2;
16057
16058 /* Usually a BOM is at the beginning of a file, and so at
16059 * the beginning of a line; then we can just step over it.
16060 */
16061 if (start == dest)
16062 start = p + 1;
16063 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016064 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016065 /* have to shuffle buf to close gap */
16066 int adjust_prevlen = 0;
16067
16068 if (dest < buf)
16069 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016070 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016071 dest = buf;
16072 }
16073 if (readlen > p - buf + 1)
16074 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16075 readlen -= 3 - adjust_prevlen;
16076 prevlen -= adjust_prevlen;
16077 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016078 }
16079 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016080 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016081#endif
16082 } /* for */
16083
16084 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16085 break;
16086 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016087 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016088 /* There's part of a line in buf, store it in "prev". */
16089 if (p - start + prevlen >= prevsize)
16090 {
16091 /* need bigger "prev" buffer */
16092 char_u *newprev;
16093
16094 /* A common use case is ordinary text files and "prev" gets a
16095 * fragment of a line, so the first allocation is made
16096 * small, to avoid repeatedly 'allocing' large and
16097 * 'reallocing' small. */
16098 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016099 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016100 else
16101 {
16102 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016103 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016104 prevsize = grow50pc > growmin ? grow50pc : growmin;
16105 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016106 newprev = prev == NULL ? alloc(prevsize)
16107 : vim_realloc(prev, prevsize);
16108 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016109 {
16110 do_outofmem_msg((long_u)prevsize);
16111 failed = TRUE;
16112 break;
16113 }
16114 prev = newprev;
16115 }
16116 /* Add the line part to end of "prev". */
16117 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016118 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016119 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016120 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016121
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016122 /*
16123 * For a negative line count use only the lines at the end of the file,
16124 * free the rest.
16125 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016126 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016127 while (cnt > -maxline)
16128 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016129 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016130 --cnt;
16131 }
16132
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016133 if (failed)
16134 {
16135 list_free(rettv->vval.v_list, TRUE);
16136 /* readfile doc says an empty list is returned on error */
16137 rettv->vval.v_list = list_alloc();
16138 }
16139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016140 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016141 fclose(fd);
16142}
16143
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016144#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016145static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016146
16147/*
16148 * Convert a List to proftime_T.
16149 * Return FAIL when there is something wrong.
16150 */
16151 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016152list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016153{
16154 long n1, n2;
16155 int error = FALSE;
16156
16157 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16158 || arg->vval.v_list->lv_len != 2)
16159 return FAIL;
16160 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16161 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16162# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016163 tm->HighPart = n1;
16164 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016165# else
16166 tm->tv_sec = n1;
16167 tm->tv_usec = n2;
16168# endif
16169 return error ? FAIL : OK;
16170}
16171#endif /* FEAT_RELTIME */
16172
16173/*
16174 * "reltime()" function
16175 */
16176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016177f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016178{
16179#ifdef FEAT_RELTIME
16180 proftime_T res;
16181 proftime_T start;
16182
16183 if (argvars[0].v_type == VAR_UNKNOWN)
16184 {
16185 /* No arguments: get current time. */
16186 profile_start(&res);
16187 }
16188 else if (argvars[1].v_type == VAR_UNKNOWN)
16189 {
16190 if (list2proftime(&argvars[0], &res) == FAIL)
16191 return;
16192 profile_end(&res);
16193 }
16194 else
16195 {
16196 /* Two arguments: compute the difference. */
16197 if (list2proftime(&argvars[0], &start) == FAIL
16198 || list2proftime(&argvars[1], &res) == FAIL)
16199 return;
16200 profile_sub(&res, &start);
16201 }
16202
16203 if (rettv_list_alloc(rettv) == OK)
16204 {
16205 long n1, n2;
16206
16207# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016208 n1 = res.HighPart;
16209 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016210# else
16211 n1 = res.tv_sec;
16212 n2 = res.tv_usec;
16213# endif
16214 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16215 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16216 }
16217#endif
16218}
16219
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016220#ifdef FEAT_FLOAT
16221/*
16222 * "reltimefloat()" function
16223 */
16224 static void
16225f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16226{
16227# ifdef FEAT_RELTIME
16228 proftime_T tm;
16229# endif
16230
16231 rettv->v_type = VAR_FLOAT;
16232 rettv->vval.v_float = 0;
16233# ifdef FEAT_RELTIME
16234 if (list2proftime(&argvars[0], &tm) == OK)
16235 rettv->vval.v_float = profile_float(&tm);
16236# endif
16237}
16238#endif
16239
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016240/*
16241 * "reltimestr()" function
16242 */
16243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016244f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016245{
16246#ifdef FEAT_RELTIME
16247 proftime_T tm;
16248#endif
16249
16250 rettv->v_type = VAR_STRING;
16251 rettv->vval.v_string = NULL;
16252#ifdef FEAT_RELTIME
16253 if (list2proftime(&argvars[0], &tm) == OK)
16254 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16255#endif
16256}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016257
Bram Moolenaar0d660222005-01-07 21:51:51 +000016258#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016259static void make_connection(void);
16260static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016261
16262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016263make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016264{
16265 if (X_DISPLAY == NULL
16266# ifdef FEAT_GUI
16267 && !gui.in_use
16268# endif
16269 )
16270 {
16271 x_force_connect = TRUE;
16272 setup_term_clip();
16273 x_force_connect = FALSE;
16274 }
16275}
16276
16277 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016278check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016279{
16280 make_connection();
16281 if (X_DISPLAY == NULL)
16282 {
16283 EMSG(_("E240: No connection to Vim server"));
16284 return FAIL;
16285 }
16286 return OK;
16287}
16288#endif
16289
16290#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016291static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016292
16293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016294remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295{
16296 char_u *server_name;
16297 char_u *keys;
16298 char_u *r = NULL;
16299 char_u buf[NUMBUFLEN];
16300# ifdef WIN32
16301 HWND w;
16302# else
16303 Window w;
16304# endif
16305
16306 if (check_restricted() || check_secure())
16307 return;
16308
16309# ifdef FEAT_X11
16310 if (check_connection() == FAIL)
16311 return;
16312# endif
16313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016314 server_name = get_tv_string_chk(&argvars[0]);
16315 if (server_name == NULL)
16316 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016317 keys = get_tv_string_buf(&argvars[1], buf);
16318# ifdef WIN32
16319 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16320# else
16321 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16322 < 0)
16323# endif
16324 {
16325 if (r != NULL)
16326 EMSG(r); /* sending worked but evaluation failed */
16327 else
16328 EMSG2(_("E241: Unable to send to %s"), server_name);
16329 return;
16330 }
16331
16332 rettv->vval.v_string = r;
16333
16334 if (argvars[2].v_type != VAR_UNKNOWN)
16335 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016336 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016337 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016338 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016339
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016340 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016341 v.di_tv.v_type = VAR_STRING;
16342 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016343 idvar = get_tv_string_chk(&argvars[2]);
16344 if (idvar != NULL)
16345 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016346 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016347 }
16348}
16349#endif
16350
16351/*
16352 * "remote_expr()" function
16353 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016355f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016356{
16357 rettv->v_type = VAR_STRING;
16358 rettv->vval.v_string = NULL;
16359#ifdef FEAT_CLIENTSERVER
16360 remote_common(argvars, rettv, TRUE);
16361#endif
16362}
16363
16364/*
16365 * "remote_foreground()" function
16366 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016368f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016369{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016370#ifdef FEAT_CLIENTSERVER
16371# ifdef WIN32
16372 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016373 {
16374 char_u *server_name = get_tv_string_chk(&argvars[0]);
16375
16376 if (server_name != NULL)
16377 serverForeground(server_name);
16378 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016379# else
16380 /* Send a foreground() expression to the server. */
16381 argvars[1].v_type = VAR_STRING;
16382 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16383 argvars[2].v_type = VAR_UNKNOWN;
16384 remote_common(argvars, rettv, TRUE);
16385 vim_free(argvars[1].vval.v_string);
16386# endif
16387#endif
16388}
16389
Bram Moolenaar0d660222005-01-07 21:51:51 +000016390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016391f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016392{
16393#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016394 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016395 char_u *s = NULL;
16396# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016397 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016398# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016399 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016400
16401 if (check_restricted() || check_secure())
16402 {
16403 rettv->vval.v_number = -1;
16404 return;
16405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016406 serverid = get_tv_string_chk(&argvars[0]);
16407 if (serverid == NULL)
16408 {
16409 rettv->vval.v_number = -1;
16410 return; /* type error; errmsg already given */
16411 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016413 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016414 if (n == 0)
16415 rettv->vval.v_number = -1;
16416 else
16417 {
16418 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16419 rettv->vval.v_number = (s != NULL);
16420 }
16421# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016422 if (check_connection() == FAIL)
16423 return;
16424
16425 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016426 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427# endif
16428
16429 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016431 char_u *retvar;
16432
Bram Moolenaar33570922005-01-25 22:26:29 +000016433 v.di_tv.v_type = VAR_STRING;
16434 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016435 retvar = get_tv_string_chk(&argvars[1]);
16436 if (retvar != NULL)
16437 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016438 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016439 }
16440#else
16441 rettv->vval.v_number = -1;
16442#endif
16443}
16444
Bram Moolenaar0d660222005-01-07 21:51:51 +000016445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016446f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016447{
16448 char_u *r = NULL;
16449
16450#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016451 char_u *serverid = get_tv_string_chk(&argvars[0]);
16452
16453 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016454 {
16455# ifdef WIN32
16456 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016457 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016459 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016460 if (n != 0)
16461 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16462 if (r == NULL)
16463# else
16464 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016465 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016466# endif
16467 EMSG(_("E277: Unable to read a server reply"));
16468 }
16469#endif
16470 rettv->v_type = VAR_STRING;
16471 rettv->vval.v_string = r;
16472}
16473
16474/*
16475 * "remote_send()" function
16476 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016478f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016479{
16480 rettv->v_type = VAR_STRING;
16481 rettv->vval.v_string = NULL;
16482#ifdef FEAT_CLIENTSERVER
16483 remote_common(argvars, rettv, FALSE);
16484#endif
16485}
16486
16487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016488 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016489 */
16490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016491f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016492{
Bram Moolenaar33570922005-01-25 22:26:29 +000016493 list_T *l;
16494 listitem_T *item, *item2;
16495 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016496 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016497 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016498 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016499 dict_T *d;
16500 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016501 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016502
Bram Moolenaar8c711452005-01-14 21:53:12 +000016503 if (argvars[0].v_type == VAR_DICT)
16504 {
16505 if (argvars[2].v_type != VAR_UNKNOWN)
16506 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016507 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016508 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016510 key = get_tv_string_chk(&argvars[1]);
16511 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016513 di = dict_find(d, key, -1);
16514 if (di == NULL)
16515 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016516 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16517 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016518 {
16519 *rettv = di->di_tv;
16520 init_tv(&di->di_tv);
16521 dictitem_remove(d, di);
16522 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016523 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016524 }
16525 }
16526 else if (argvars[0].v_type != VAR_LIST)
16527 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016528 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016529 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016531 int error = FALSE;
16532
16533 idx = get_tv_number_chk(&argvars[1], &error);
16534 if (error)
16535 ; /* type error: do nothing, errmsg already given */
16536 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016537 EMSGN(_(e_listidx), idx);
16538 else
16539 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016540 if (argvars[2].v_type == VAR_UNKNOWN)
16541 {
16542 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016543 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016544 *rettv = item->li_tv;
16545 vim_free(item);
16546 }
16547 else
16548 {
16549 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016550 end = get_tv_number_chk(&argvars[2], &error);
16551 if (error)
16552 ; /* type error: do nothing */
16553 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016554 EMSGN(_(e_listidx), end);
16555 else
16556 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016557 int cnt = 0;
16558
16559 for (li = item; li != NULL; li = li->li_next)
16560 {
16561 ++cnt;
16562 if (li == item2)
16563 break;
16564 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016565 if (li == NULL) /* didn't find "item2" after "item" */
16566 EMSG(_(e_invrange));
16567 else
16568 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016569 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016570 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016571 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016572 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016573 l->lv_first = item;
16574 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016575 item->li_prev = NULL;
16576 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016577 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016578 }
16579 }
16580 }
16581 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016582 }
16583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584}
16585
16586/*
16587 * "rename({from}, {to})" function
16588 */
16589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016590f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591{
16592 char_u buf[NUMBUFLEN];
16593
16594 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016595 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016597 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16598 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599}
16600
16601/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016602 * "repeat()" function
16603 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016605f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016606{
16607 char_u *p;
16608 int n;
16609 int slen;
16610 int len;
16611 char_u *r;
16612 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016613
16614 n = get_tv_number(&argvars[1]);
16615 if (argvars[0].v_type == VAR_LIST)
16616 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016617 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016618 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016619 if (list_extend(rettv->vval.v_list,
16620 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016621 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016622 }
16623 else
16624 {
16625 p = get_tv_string(&argvars[0]);
16626 rettv->v_type = VAR_STRING;
16627 rettv->vval.v_string = NULL;
16628
16629 slen = (int)STRLEN(p);
16630 len = slen * n;
16631 if (len <= 0)
16632 return;
16633
16634 r = alloc(len + 1);
16635 if (r != NULL)
16636 {
16637 for (i = 0; i < n; i++)
16638 mch_memmove(r + i * slen, p, (size_t)slen);
16639 r[len] = NUL;
16640 }
16641
16642 rettv->vval.v_string = r;
16643 }
16644}
16645
16646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647 * "resolve()" function
16648 */
16649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016650f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016651{
16652 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016653#ifdef HAVE_READLINK
16654 char_u *buf = NULL;
16655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016657 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658#ifdef FEAT_SHORTCUT
16659 {
16660 char_u *v = NULL;
16661
16662 v = mch_resolve_shortcut(p);
16663 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016664 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016665 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016666 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 }
16668#else
16669# ifdef HAVE_READLINK
16670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671 char_u *cpy;
16672 int len;
16673 char_u *remain = NULL;
16674 char_u *q;
16675 int is_relative_to_current = FALSE;
16676 int has_trailing_pathsep = FALSE;
16677 int limit = 100;
16678
16679 p = vim_strsave(p);
16680
16681 if (p[0] == '.' && (vim_ispathsep(p[1])
16682 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16683 is_relative_to_current = TRUE;
16684
16685 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016686 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016688 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016689 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691
16692 q = getnextcomp(p);
16693 if (*q != NUL)
16694 {
16695 /* Separate the first path component in "p", and keep the
16696 * remainder (beginning with the path separator). */
16697 remain = vim_strsave(q - 1);
16698 q[-1] = NUL;
16699 }
16700
Bram Moolenaard9462e32011-04-11 21:35:11 +020016701 buf = alloc(MAXPATHL + 1);
16702 if (buf == NULL)
16703 goto fail;
16704
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705 for (;;)
16706 {
16707 for (;;)
16708 {
16709 len = readlink((char *)p, (char *)buf, MAXPATHL);
16710 if (len <= 0)
16711 break;
16712 buf[len] = NUL;
16713
16714 if (limit-- == 0)
16715 {
16716 vim_free(p);
16717 vim_free(remain);
16718 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016719 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 goto fail;
16721 }
16722
16723 /* Ensure that the result will have a trailing path separator
16724 * if the argument has one. */
16725 if (remain == NULL && has_trailing_pathsep)
16726 add_pathsep(buf);
16727
16728 /* Separate the first path component in the link value and
16729 * concatenate the remainders. */
16730 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16731 if (*q != NUL)
16732 {
16733 if (remain == NULL)
16734 remain = vim_strsave(q - 1);
16735 else
16736 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016737 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738 if (cpy != NULL)
16739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740 vim_free(remain);
16741 remain = cpy;
16742 }
16743 }
16744 q[-1] = NUL;
16745 }
16746
16747 q = gettail(p);
16748 if (q > p && *q == NUL)
16749 {
16750 /* Ignore trailing path separator. */
16751 q[-1] = NUL;
16752 q = gettail(p);
16753 }
16754 if (q > p && !mch_isFullName(buf))
16755 {
16756 /* symlink is relative to directory of argument */
16757 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16758 if (cpy != NULL)
16759 {
16760 STRCPY(cpy, p);
16761 STRCPY(gettail(cpy), buf);
16762 vim_free(p);
16763 p = cpy;
16764 }
16765 }
16766 else
16767 {
16768 vim_free(p);
16769 p = vim_strsave(buf);
16770 }
16771 }
16772
16773 if (remain == NULL)
16774 break;
16775
16776 /* Append the first path component of "remain" to "p". */
16777 q = getnextcomp(remain + 1);
16778 len = q - remain - (*q != NUL);
16779 cpy = vim_strnsave(p, STRLEN(p) + len);
16780 if (cpy != NULL)
16781 {
16782 STRNCAT(cpy, remain, len);
16783 vim_free(p);
16784 p = cpy;
16785 }
16786 /* Shorten "remain". */
16787 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016788 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789 else
16790 {
16791 vim_free(remain);
16792 remain = NULL;
16793 }
16794 }
16795
16796 /* If the result is a relative path name, make it explicitly relative to
16797 * the current directory if and only if the argument had this form. */
16798 if (!vim_ispathsep(*p))
16799 {
16800 if (is_relative_to_current
16801 && *p != NUL
16802 && !(p[0] == '.'
16803 && (p[1] == NUL
16804 || vim_ispathsep(p[1])
16805 || (p[1] == '.'
16806 && (p[2] == NUL
16807 || vim_ispathsep(p[2]))))))
16808 {
16809 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016810 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811 if (cpy != NULL)
16812 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 vim_free(p);
16814 p = cpy;
16815 }
16816 }
16817 else if (!is_relative_to_current)
16818 {
16819 /* Strip leading "./". */
16820 q = p;
16821 while (q[0] == '.' && vim_ispathsep(q[1]))
16822 q += 2;
16823 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016824 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 }
16826 }
16827
16828 /* Ensure that the result will have no trailing path separator
16829 * if the argument had none. But keep "/" or "//". */
16830 if (!has_trailing_pathsep)
16831 {
16832 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016833 if (after_pathsep(p, q))
16834 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 }
16836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016837 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 }
16839# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016840 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841# endif
16842#endif
16843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016844 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845
16846#ifdef HAVE_READLINK
16847fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016848 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016850 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851}
16852
16853/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016854 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855 */
16856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016857f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858{
Bram Moolenaar33570922005-01-25 22:26:29 +000016859 list_T *l;
16860 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861
Bram Moolenaar0d660222005-01-07 21:51:51 +000016862 if (argvars[0].v_type != VAR_LIST)
16863 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016864 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016865 && !tv_check_lock(l->lv_lock,
16866 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016867 {
16868 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016869 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016870 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016871 while (li != NULL)
16872 {
16873 ni = li->li_prev;
16874 list_append(l, li);
16875 li = ni;
16876 }
16877 rettv->vval.v_list = l;
16878 rettv->v_type = VAR_LIST;
16879 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016880 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882}
16883
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016884#define SP_NOMOVE 0x01 /* don't move cursor */
16885#define SP_REPEAT 0x02 /* repeat to find outer pair */
16886#define SP_RETCOUNT 0x04 /* return matchcount */
16887#define SP_SETPCMARK 0x08 /* set previous context mark */
16888#define SP_START 0x10 /* accept match at start position */
16889#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16890#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016891#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016892
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016893static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016894
16895/*
16896 * Get flags for a search function.
16897 * Possibly sets "p_ws".
16898 * Returns BACKWARD, FORWARD or zero (for an error).
16899 */
16900 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016901get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016902{
16903 int dir = FORWARD;
16904 char_u *flags;
16905 char_u nbuf[NUMBUFLEN];
16906 int mask;
16907
16908 if (varp->v_type != VAR_UNKNOWN)
16909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016910 flags = get_tv_string_buf_chk(varp, nbuf);
16911 if (flags == NULL)
16912 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016913 while (*flags != NUL)
16914 {
16915 switch (*flags)
16916 {
16917 case 'b': dir = BACKWARD; break;
16918 case 'w': p_ws = TRUE; break;
16919 case 'W': p_ws = FALSE; break;
16920 default: mask = 0;
16921 if (flagsp != NULL)
16922 switch (*flags)
16923 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016924 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016925 case 'e': mask = SP_END; break;
16926 case 'm': mask = SP_RETCOUNT; break;
16927 case 'n': mask = SP_NOMOVE; break;
16928 case 'p': mask = SP_SUBPAT; break;
16929 case 'r': mask = SP_REPEAT; break;
16930 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016931 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016932 }
16933 if (mask == 0)
16934 {
16935 EMSG2(_(e_invarg2), flags);
16936 dir = 0;
16937 }
16938 else
16939 *flagsp |= mask;
16940 }
16941 if (dir == 0)
16942 break;
16943 ++flags;
16944 }
16945 }
16946 return dir;
16947}
16948
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016950 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016952 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016953search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016955 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 char_u *pat;
16957 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016958 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959 int save_p_ws = p_ws;
16960 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016961 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016962 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016963 proftime_T tm;
16964#ifdef FEAT_RELTIME
16965 long time_limit = 0;
16966#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016967 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016968 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016970 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016971 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016972 if (dir == 0)
16973 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016974 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016975 if (flags & SP_START)
16976 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016977 if (flags & SP_END)
16978 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016979 if (flags & SP_COLUMN)
16980 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016981
Bram Moolenaar76929292008-01-06 19:07:36 +000016982 /* Optional arguments: line number to stop searching and timeout. */
16983 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016984 {
16985 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16986 if (lnum_stop < 0)
16987 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016988#ifdef FEAT_RELTIME
16989 if (argvars[3].v_type != VAR_UNKNOWN)
16990 {
16991 time_limit = get_tv_number_chk(&argvars[3], NULL);
16992 if (time_limit < 0)
16993 goto theend;
16994 }
16995#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016996 }
16997
Bram Moolenaar76929292008-01-06 19:07:36 +000016998#ifdef FEAT_RELTIME
16999 /* Set the time limit, if there is one. */
17000 profile_setlimit(time_limit, &tm);
17001#endif
17002
Bram Moolenaar231334e2005-07-25 20:46:57 +000017003 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017004 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017005 * Check to make sure only those flags are set.
17006 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17007 * flags cannot be set. Check for that condition also.
17008 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017009 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017010 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017012 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017013 goto theend;
17014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017016 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017017 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017018 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017019 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017021 if (flags & SP_SUBPAT)
17022 retval = subpatnum;
17023 else
17024 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017025 if (flags & SP_SETPCMARK)
17026 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017028 if (match_pos != NULL)
17029 {
17030 /* Store the match cursor position */
17031 match_pos->lnum = pos.lnum;
17032 match_pos->col = pos.col + 1;
17033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 /* "/$" will put the cursor after the end of the line, may need to
17035 * correct that here */
17036 check_cursor();
17037 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017038
17039 /* If 'n' flag is used: restore cursor position. */
17040 if (flags & SP_NOMOVE)
17041 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017042 else
17043 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017044theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017046
17047 return retval;
17048}
17049
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017050#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017051
17052/*
17053 * round() is not in C90, use ceil() or floor() instead.
17054 */
17055 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017056vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017057{
17058 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17059}
17060
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017061/*
17062 * "round({float})" function
17063 */
17064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017065f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017066{
17067 float_T f;
17068
17069 rettv->v_type = VAR_FLOAT;
17070 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017071 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017072 else
17073 rettv->vval.v_float = 0.0;
17074}
17075#endif
17076
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017077/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017078 * "screenattr()" function
17079 */
17080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017081f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017082{
17083 int row;
17084 int col;
17085 int c;
17086
17087 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17088 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17089 if (row < 0 || row >= screen_Rows
17090 || col < 0 || col >= screen_Columns)
17091 c = -1;
17092 else
17093 c = ScreenAttrs[LineOffset[row] + col];
17094 rettv->vval.v_number = c;
17095}
17096
17097/*
17098 * "screenchar()" function
17099 */
17100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017101f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017102{
17103 int row;
17104 int col;
17105 int off;
17106 int c;
17107
17108 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17109 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17110 if (row < 0 || row >= screen_Rows
17111 || col < 0 || col >= screen_Columns)
17112 c = -1;
17113 else
17114 {
17115 off = LineOffset[row] + col;
17116#ifdef FEAT_MBYTE
17117 if (enc_utf8 && ScreenLinesUC[off] != 0)
17118 c = ScreenLinesUC[off];
17119 else
17120#endif
17121 c = ScreenLines[off];
17122 }
17123 rettv->vval.v_number = c;
17124}
17125
17126/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017127 * "screencol()" function
17128 *
17129 * First column is 1 to be consistent with virtcol().
17130 */
17131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017132f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017133{
17134 rettv->vval.v_number = screen_screencol() + 1;
17135}
17136
17137/*
17138 * "screenrow()" function
17139 */
17140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017141f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017142{
17143 rettv->vval.v_number = screen_screenrow() + 1;
17144}
17145
17146/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017147 * "search()" function
17148 */
17149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017150f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017151{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017152 int flags = 0;
17153
17154 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155}
17156
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017158 * "searchdecl()" function
17159 */
17160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017161f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017162{
17163 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017164 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017165 int error = FALSE;
17166 char_u *name;
17167
17168 rettv->vval.v_number = 1; /* default: FAIL */
17169
17170 name = get_tv_string_chk(&argvars[0]);
17171 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017172 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017173 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017174 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17175 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17176 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017177 if (!error && name != NULL)
17178 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017179 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017180}
17181
17182/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017183 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017185 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017186searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187{
17188 char_u *spat, *mpat, *epat;
17189 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 int dir;
17192 int flags = 0;
17193 char_u nbuf1[NUMBUFLEN];
17194 char_u nbuf2[NUMBUFLEN];
17195 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017196 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017197 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017198 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017201 spat = get_tv_string_chk(&argvars[0]);
17202 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17203 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17204 if (spat == NULL || mpat == NULL || epat == NULL)
17205 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207 /* Handle the optional fourth argument: flags */
17208 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017209 if (dir == 0)
17210 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017211
17212 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017213 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17214 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017215 if ((flags & (SP_END | SP_SUBPAT)) != 0
17216 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017217 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017218 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017219 goto theend;
17220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017222 /* Using 'r' implies 'W', otherwise it doesn't work. */
17223 if (flags & SP_REPEAT)
17224 p_ws = FALSE;
17225
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017226 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017227 if (argvars[3].v_type == VAR_UNKNOWN
17228 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 skip = (char_u *)"";
17230 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017232 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017233 if (argvars[5].v_type != VAR_UNKNOWN)
17234 {
17235 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17236 if (lnum_stop < 0)
17237 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017238#ifdef FEAT_RELTIME
17239 if (argvars[6].v_type != VAR_UNKNOWN)
17240 {
17241 time_limit = get_tv_number_chk(&argvars[6], NULL);
17242 if (time_limit < 0)
17243 goto theend;
17244 }
17245#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017246 }
17247 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017248 if (skip == NULL)
17249 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017251 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017252 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017253
17254theend:
17255 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017256
17257 return retval;
17258}
17259
17260/*
17261 * "searchpair()" function
17262 */
17263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017264f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017265{
17266 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17267}
17268
17269/*
17270 * "searchpairpos()" function
17271 */
17272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017273f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017274{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017275 pos_T match_pos;
17276 int lnum = 0;
17277 int col = 0;
17278
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017279 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017280 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017281
17282 if (searchpair_cmn(argvars, &match_pos) > 0)
17283 {
17284 lnum = match_pos.lnum;
17285 col = match_pos.col;
17286 }
17287
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017288 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17289 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017290}
17291
17292/*
17293 * Search for a start/middle/end thing.
17294 * Used by searchpair(), see its documentation for the details.
17295 * Returns 0 or -1 for no match,
17296 */
17297 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017298do_searchpair(
17299 char_u *spat, /* start pattern */
17300 char_u *mpat, /* middle pattern */
17301 char_u *epat, /* end pattern */
17302 int dir, /* BACKWARD or FORWARD */
17303 char_u *skip, /* skip expression */
17304 int flags, /* SP_SETPCMARK and other SP_ values */
17305 pos_T *match_pos,
17306 linenr_T lnum_stop, /* stop at this line if not zero */
17307 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017308{
17309 char_u *save_cpo;
17310 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17311 long retval = 0;
17312 pos_T pos;
17313 pos_T firstpos;
17314 pos_T foundpos;
17315 pos_T save_cursor;
17316 pos_T save_pos;
17317 int n;
17318 int r;
17319 int nest = 1;
17320 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017321 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017322 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017323
17324 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17325 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017326 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017327
Bram Moolenaar76929292008-01-06 19:07:36 +000017328#ifdef FEAT_RELTIME
17329 /* Set the time limit, if there is one. */
17330 profile_setlimit(time_limit, &tm);
17331#endif
17332
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017333 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17334 * start/middle/end (pat3, for the top pair). */
17335 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17336 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17337 if (pat2 == NULL || pat3 == NULL)
17338 goto theend;
17339 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17340 if (*mpat == NUL)
17341 STRCPY(pat3, pat2);
17342 else
17343 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17344 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017345 if (flags & SP_START)
17346 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017347
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 save_cursor = curwin->w_cursor;
17349 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017350 clearpos(&firstpos);
17351 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 pat = pat3;
17353 for (;;)
17354 {
17355 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017356 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17358 /* didn't find it or found the first match again: FAIL */
17359 break;
17360
17361 if (firstpos.lnum == 0)
17362 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017363 if (equalpos(pos, foundpos))
17364 {
17365 /* Found the same position again. Can happen with a pattern that
17366 * has "\zs" at the end and searching backwards. Advance one
17367 * character and try again. */
17368 if (dir == BACKWARD)
17369 decl(&pos);
17370 else
17371 incl(&pos);
17372 }
17373 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017375 /* clear the start flag to avoid getting stuck here */
17376 options &= ~SEARCH_START;
17377
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378 /* If the skip pattern matches, ignore this match. */
17379 if (*skip != NUL)
17380 {
17381 save_pos = curwin->w_cursor;
17382 curwin->w_cursor = pos;
17383 r = eval_to_bool(skip, &err, NULL, FALSE);
17384 curwin->w_cursor = save_pos;
17385 if (err)
17386 {
17387 /* Evaluating {skip} caused an error, break here. */
17388 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017389 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 break;
17391 }
17392 if (r)
17393 continue;
17394 }
17395
17396 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17397 {
17398 /* Found end when searching backwards or start when searching
17399 * forward: nested pair. */
17400 ++nest;
17401 pat = pat2; /* nested, don't search for middle */
17402 }
17403 else
17404 {
17405 /* Found end when searching forward or start when searching
17406 * backward: end of (nested) pair; or found middle in outer pair. */
17407 if (--nest == 1)
17408 pat = pat3; /* outer level, search for middle */
17409 }
17410
17411 if (nest == 0)
17412 {
17413 /* Found the match: return matchcount or line number. */
17414 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017415 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017417 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017418 if (flags & SP_SETPCMARK)
17419 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420 curwin->w_cursor = pos;
17421 if (!(flags & SP_REPEAT))
17422 break;
17423 nest = 1; /* search for next unmatched */
17424 }
17425 }
17426
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017427 if (match_pos != NULL)
17428 {
17429 /* Store the match cursor position */
17430 match_pos->lnum = curwin->w_cursor.lnum;
17431 match_pos->col = curwin->w_cursor.col + 1;
17432 }
17433
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017435 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436 curwin->w_cursor = save_cursor;
17437
17438theend:
17439 vim_free(pat2);
17440 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017441 if (p_cpo == empty_option)
17442 p_cpo = save_cpo;
17443 else
17444 /* Darn, evaluating the {skip} expression changed the value. */
17445 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017446
17447 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448}
17449
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017450/*
17451 * "searchpos()" function
17452 */
17453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017454f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017455{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017456 pos_T match_pos;
17457 int lnum = 0;
17458 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017459 int n;
17460 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017461
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017462 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017463 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017464
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017465 n = search_cmn(argvars, &match_pos, &flags);
17466 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017467 {
17468 lnum = match_pos.lnum;
17469 col = match_pos.col;
17470 }
17471
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017472 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17473 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017474 if (flags & SP_SUBPAT)
17475 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017476}
17477
Bram Moolenaar0d660222005-01-07 21:51:51 +000017478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017479f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017481#ifdef FEAT_CLIENTSERVER
17482 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017483 char_u *server = get_tv_string_chk(&argvars[0]);
17484 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485
Bram Moolenaar0d660222005-01-07 21:51:51 +000017486 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017487 if (server == NULL || reply == NULL)
17488 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017489 if (check_restricted() || check_secure())
17490 return;
17491# ifdef FEAT_X11
17492 if (check_connection() == FAIL)
17493 return;
17494# endif
17495
17496 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017498 EMSG(_("E258: Unable to send to client"));
17499 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017501 rettv->vval.v_number = 0;
17502#else
17503 rettv->vval.v_number = -1;
17504#endif
17505}
17506
Bram Moolenaar0d660222005-01-07 21:51:51 +000017507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017508f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017509{
17510 char_u *r = NULL;
17511
17512#ifdef FEAT_CLIENTSERVER
17513# ifdef WIN32
17514 r = serverGetVimNames();
17515# else
17516 make_connection();
17517 if (X_DISPLAY != NULL)
17518 r = serverGetVimNames(X_DISPLAY);
17519# endif
17520#endif
17521 rettv->v_type = VAR_STRING;
17522 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523}
17524
17525/*
17526 * "setbufvar()" function
17527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017529f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530{
17531 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017534 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535 char_u nbuf[NUMBUFLEN];
17536
17537 if (check_restricted() || check_secure())
17538 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017539 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17540 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017541 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542 varp = &argvars[2];
17543
17544 if (buf != NULL && varname != NULL && varp != NULL)
17545 {
17546 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548
17549 if (*varname == '&')
17550 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017551 long numval;
17552 char_u *strval;
17553 int error = FALSE;
17554
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017556 numval = get_tv_number_chk(varp, &error);
17557 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017558 if (!error && strval != NULL)
17559 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560 }
17561 else
17562 {
17563 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17564 if (bufvarname != NULL)
17565 {
17566 STRCPY(bufvarname, "b:");
17567 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017568 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 vim_free(bufvarname);
17570 }
17571 }
17572
17573 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576}
17577
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017579f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017580{
17581 dict_T *d;
17582 dictitem_T *di;
17583 char_u *csearch;
17584
17585 if (argvars[0].v_type != VAR_DICT)
17586 {
17587 EMSG(_(e_dictreq));
17588 return;
17589 }
17590
17591 if ((d = argvars[0].vval.v_dict) != NULL)
17592 {
17593 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17594 if (csearch != NULL)
17595 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017596#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017597 if (enc_utf8)
17598 {
17599 int pcc[MAX_MCO];
17600 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017601
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017602 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17603 }
17604 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017605#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017606 set_last_csearch(PTR2CHAR(csearch),
17607 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017608 }
17609
17610 di = dict_find(d, (char_u *)"forward", -1);
17611 if (di != NULL)
17612 set_csearch_direction(get_tv_number(&di->di_tv)
17613 ? FORWARD : BACKWARD);
17614
17615 di = dict_find(d, (char_u *)"until", -1);
17616 if (di != NULL)
17617 set_csearch_until(!!get_tv_number(&di->di_tv));
17618 }
17619}
17620
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621/*
17622 * "setcmdpos()" function
17623 */
17624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017625f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017627 int pos = (int)get_tv_number(&argvars[0]) - 1;
17628
17629 if (pos >= 0)
17630 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631}
17632
17633/*
17634 * "setline()" function
17635 */
17636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017637f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638{
17639 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017640 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017641 list_T *l = NULL;
17642 listitem_T *li = NULL;
17643 long added = 0;
17644 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017646 lnum = get_tv_lnum(&argvars[0]);
17647 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017649 l = argvars[1].vval.v_list;
17650 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017652 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017653 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017654
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017655 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017656 for (;;)
17657 {
17658 if (l != NULL)
17659 {
17660 /* list argument, get next string */
17661 if (li == NULL)
17662 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017663 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017664 li = li->li_next;
17665 }
17666
17667 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017668 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017669 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017670
17671 /* When coming here from Insert mode, sync undo, so that this can be
17672 * undone separately from what was previously inserted. */
17673 if (u_sync_once == 2)
17674 {
17675 u_sync_once = 1; /* notify that u_sync() was called */
17676 u_sync(TRUE);
17677 }
17678
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017679 if (lnum <= curbuf->b_ml.ml_line_count)
17680 {
17681 /* existing line, replace it */
17682 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17683 {
17684 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017685 if (lnum == curwin->w_cursor.lnum)
17686 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017687 rettv->vval.v_number = 0; /* OK */
17688 }
17689 }
17690 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17691 {
17692 /* lnum is one past the last line, append the line */
17693 ++added;
17694 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17695 rettv->vval.v_number = 0; /* OK */
17696 }
17697
17698 if (l == NULL) /* only one string argument */
17699 break;
17700 ++lnum;
17701 }
17702
17703 if (added > 0)
17704 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705}
17706
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017707static 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 +000017708
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017710 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017711 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017713set_qf_ll_list(
17714 win_T *wp UNUSED,
17715 typval_T *list_arg UNUSED,
17716 typval_T *action_arg UNUSED,
17717 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017718{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017719#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017720 char_u *act;
17721 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017722#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017723
Bram Moolenaar2641f772005-03-25 21:58:17 +000017724 rettv->vval.v_number = -1;
17725
17726#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017727 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017728 EMSG(_(e_listreq));
17729 else
17730 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017731 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017732
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017733 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017734 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017735 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017736 if (act == NULL)
17737 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017738 if (*act == 'a' || *act == 'r')
17739 action = *act;
17740 }
17741
Bram Moolenaar81484f42012-12-05 15:16:47 +010017742 if (l != NULL && set_errorlist(wp, l, action,
17743 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017744 rettv->vval.v_number = 0;
17745 }
17746#endif
17747}
17748
17749/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017750 * "setloclist()" function
17751 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017753f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017754{
17755 win_T *win;
17756
17757 rettv->vval.v_number = -1;
17758
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017759 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017760 if (win != NULL)
17761 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17762}
17763
17764/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017765 * "setmatches()" function
17766 */
17767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017768f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017769{
17770#ifdef FEAT_SEARCH_EXTRA
17771 list_T *l;
17772 listitem_T *li;
17773 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017774 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017775
17776 rettv->vval.v_number = -1;
17777 if (argvars[0].v_type != VAR_LIST)
17778 {
17779 EMSG(_(e_listreq));
17780 return;
17781 }
17782 if ((l = argvars[0].vval.v_list) != NULL)
17783 {
17784
17785 /* To some extent make sure that we are dealing with a list from
17786 * "getmatches()". */
17787 li = l->lv_first;
17788 while (li != NULL)
17789 {
17790 if (li->li_tv.v_type != VAR_DICT
17791 || (d = li->li_tv.vval.v_dict) == NULL)
17792 {
17793 EMSG(_(e_invarg));
17794 return;
17795 }
17796 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017797 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17798 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017799 && dict_find(d, (char_u *)"priority", -1) != NULL
17800 && dict_find(d, (char_u *)"id", -1) != NULL))
17801 {
17802 EMSG(_(e_invarg));
17803 return;
17804 }
17805 li = li->li_next;
17806 }
17807
17808 clear_matches(curwin);
17809 li = l->lv_first;
17810 while (li != NULL)
17811 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017812 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017813 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017814 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017815 char_u *group;
17816 int priority;
17817 int id;
17818 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017819
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017820 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017821 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17822 {
17823 if (s == NULL)
17824 {
17825 s = list_alloc();
17826 if (s == NULL)
17827 return;
17828 }
17829
17830 /* match from matchaddpos() */
17831 for (i = 1; i < 9; i++)
17832 {
17833 sprintf((char *)buf, (char *)"pos%d", i);
17834 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17835 {
17836 if (di->di_tv.v_type != VAR_LIST)
17837 return;
17838
17839 list_append_tv(s, &di->di_tv);
17840 s->lv_refcount++;
17841 }
17842 else
17843 break;
17844 }
17845 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017846
17847 group = get_dict_string(d, (char_u *)"group", FALSE);
17848 priority = (int)get_dict_number(d, (char_u *)"priority");
17849 id = (int)get_dict_number(d, (char_u *)"id");
17850 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17851 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17852 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017853 if (i == 0)
17854 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017855 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017856 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017857 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017858 }
17859 else
17860 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017861 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017862 list_unref(s);
17863 s = NULL;
17864 }
17865
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017866 li = li->li_next;
17867 }
17868 rettv->vval.v_number = 0;
17869 }
17870#endif
17871}
17872
17873/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017874 * "setpos()" function
17875 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017877f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017878{
17879 pos_T pos;
17880 int fnum;
17881 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017882 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017883
Bram Moolenaar08250432008-02-13 11:42:46 +000017884 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017885 name = get_tv_string_chk(argvars);
17886 if (name != NULL)
17887 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017888 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017889 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017890 if (--pos.col < 0)
17891 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017892 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017893 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017894 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017895 if (fnum == curbuf->b_fnum)
17896 {
17897 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017898 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017899 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017900 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017901 curwin->w_set_curswant = FALSE;
17902 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017903 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017904 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017905 }
17906 else
17907 EMSG(_(e_invarg));
17908 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017909 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17910 {
17911 /* set mark */
17912 if (setmark_pos(name[1], &pos, fnum) == OK)
17913 rettv->vval.v_number = 0;
17914 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017915 else
17916 EMSG(_(e_invarg));
17917 }
17918 }
17919}
17920
17921/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017922 * "setqflist()" function
17923 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017925f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017926{
17927 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17928}
17929
17930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 * "setreg()" function
17932 */
17933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017934f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935{
17936 int regname;
17937 char_u *strregname;
17938 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017939 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 int append;
17941 char_u yank_type;
17942 long block_len;
17943
17944 block_len = -1;
17945 yank_type = MAUTO;
17946 append = FALSE;
17947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017948 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017949 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017951 if (strregname == NULL)
17952 return; /* type error; errmsg already given */
17953 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 if (regname == 0 || regname == '@')
17955 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017957 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017959 stropt = get_tv_string_chk(&argvars[2]);
17960 if (stropt == NULL)
17961 return; /* type error */
17962 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963 switch (*stropt)
17964 {
17965 case 'a': case 'A': /* append */
17966 append = TRUE;
17967 break;
17968 case 'v': case 'c': /* character-wise selection */
17969 yank_type = MCHAR;
17970 break;
17971 case 'V': case 'l': /* line-wise selection */
17972 yank_type = MLINE;
17973 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 case 'b': case Ctrl_V: /* block-wise selection */
17975 yank_type = MBLOCK;
17976 if (VIM_ISDIGIT(stropt[1]))
17977 {
17978 ++stropt;
17979 block_len = getdigits(&stropt) - 1;
17980 --stropt;
17981 }
17982 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983 }
17984 }
17985
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017986 if (argvars[1].v_type == VAR_LIST)
17987 {
17988 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017989 char_u **allocval;
17990 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017991 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017992 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017993 int len = argvars[1].vval.v_list->lv_len;
17994 listitem_T *li;
17995
Bram Moolenaar7d647822014-04-05 21:28:56 +020017996 /* First half: use for pointers to result lines; second half: use for
17997 * pointers to allocated copies. */
17998 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017999 if (lstval == NULL)
18000 return;
18001 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018002 allocval = lstval + len + 2;
18003 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018004
18005 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18006 li = li->li_next)
18007 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018008 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018009 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018010 goto free_lstval;
18011 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018012 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018013 /* Need to make a copy, next get_tv_string_buf_chk() will
18014 * overwrite the string. */
18015 strval = vim_strsave(buf);
18016 if (strval == NULL)
18017 goto free_lstval;
18018 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018019 }
18020 *curval++ = strval;
18021 }
18022 *curval++ = NULL;
18023
18024 write_reg_contents_lst(regname, lstval, -1,
18025 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018026free_lstval:
18027 while (curallocval > allocval)
18028 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018029 vim_free(lstval);
18030 }
18031 else
18032 {
18033 strval = get_tv_string_chk(&argvars[1]);
18034 if (strval == NULL)
18035 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018036 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018039 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040}
18041
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018042/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018043 * "settabvar()" function
18044 */
18045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018046f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018047{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018048#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018049 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018050 tabpage_T *tp;
18051#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018052 char_u *varname, *tabvarname;
18053 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018054
18055 rettv->vval.v_number = 0;
18056
18057 if (check_restricted() || check_secure())
18058 return;
18059
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018060#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018061 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018062#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018063 varname = get_tv_string_chk(&argvars[1]);
18064 varp = &argvars[2];
18065
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018066 if (varname != NULL && varp != NULL
18067#ifdef FEAT_WINDOWS
18068 && tp != NULL
18069#endif
18070 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018071 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018072#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018073 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018074 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018075#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018076
18077 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18078 if (tabvarname != NULL)
18079 {
18080 STRCPY(tabvarname, "t:");
18081 STRCPY(tabvarname + 2, varname);
18082 set_var(tabvarname, varp, TRUE);
18083 vim_free(tabvarname);
18084 }
18085
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018086#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018087 /* Restore current tabpage */
18088 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018089 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018090#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018091 }
18092}
18093
18094/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018095 * "settabwinvar()" function
18096 */
18097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018098f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018099{
18100 setwinvar(argvars, rettv, 1);
18101}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102
18103/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018104 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018107f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018109 setwinvar(argvars, rettv, 0);
18110}
18111
18112/*
18113 * "setwinvar()" and "settabwinvar()" functions
18114 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018115
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018117setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018118{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 win_T *win;
18120#ifdef FEAT_WINDOWS
18121 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018122 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018123 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124#endif
18125 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018126 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018128 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129
18130 if (check_restricted() || check_secure())
18131 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018132
18133#ifdef FEAT_WINDOWS
18134 if (off == 1)
18135 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18136 else
18137 tp = curtab;
18138#endif
18139 win = find_win_by_nr(&argvars[off], tp);
18140 varname = get_tv_string_chk(&argvars[off + 1]);
18141 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142
18143 if (win != NULL && varname != NULL && varp != NULL)
18144 {
18145#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018146 need_switch_win = !(tp == curtab && win == curwin);
18147 if (!need_switch_win
18148 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018151 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018153 long numval;
18154 char_u *strval;
18155 int error = FALSE;
18156
18157 ++varname;
18158 numval = get_tv_number_chk(varp, &error);
18159 strval = get_tv_string_buf_chk(varp, nbuf);
18160 if (!error && strval != NULL)
18161 set_option_value(varname, numval, strval, OPT_LOCAL);
18162 }
18163 else
18164 {
18165 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18166 if (winvarname != NULL)
18167 {
18168 STRCPY(winvarname, "w:");
18169 STRCPY(winvarname + 2, varname);
18170 set_var(winvarname, varp, TRUE);
18171 vim_free(winvarname);
18172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 }
18174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018176 if (need_switch_win)
18177 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178#endif
18179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180}
18181
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018182#ifdef FEAT_CRYPT
18183/*
18184 * "sha256({string})" function
18185 */
18186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018187f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018188{
18189 char_u *p;
18190
18191 p = get_tv_string(&argvars[0]);
18192 rettv->vval.v_string = vim_strsave(
18193 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18194 rettv->v_type = VAR_STRING;
18195}
18196#endif /* FEAT_CRYPT */
18197
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018199 * "shellescape({string})" function
18200 */
18201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018202f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018203{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018204 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018205 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018206 rettv->v_type = VAR_STRING;
18207}
18208
18209/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018210 * shiftwidth() function
18211 */
18212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018213f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018214{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018215 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018216}
18217
18218/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018219 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220 */
18221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018222f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018224 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225
Bram Moolenaar0d660222005-01-07 21:51:51 +000018226 p = get_tv_string(&argvars[0]);
18227 rettv->vval.v_string = vim_strsave(p);
18228 simplify_filename(rettv->vval.v_string); /* simplify in place */
18229 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230}
18231
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018232#ifdef FEAT_FLOAT
18233/*
18234 * "sin()" function
18235 */
18236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018237f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018238{
18239 float_T f;
18240
18241 rettv->v_type = VAR_FLOAT;
18242 if (get_float_arg(argvars, &f) == OK)
18243 rettv->vval.v_float = sin(f);
18244 else
18245 rettv->vval.v_float = 0.0;
18246}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018247
18248/*
18249 * "sinh()" function
18250 */
18251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018252f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018253{
18254 float_T f;
18255
18256 rettv->v_type = VAR_FLOAT;
18257 if (get_float_arg(argvars, &f) == OK)
18258 rettv->vval.v_float = sinh(f);
18259 else
18260 rettv->vval.v_float = 0.0;
18261}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018262#endif
18263
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264static int
18265#ifdef __BORLANDC__
18266 _RTLENTRYF
18267#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018268 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018269static int
18270#ifdef __BORLANDC__
18271 _RTLENTRYF
18272#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018273 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018274
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018275/* struct used in the array that's given to qsort() */
18276typedef struct
18277{
18278 listitem_T *item;
18279 int idx;
18280} sortItem_T;
18281
Bram Moolenaar0d660222005-01-07 21:51:51 +000018282static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018283static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018284static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018285#ifdef FEAT_FLOAT
18286static int item_compare_float;
18287#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018288static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018289static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018290static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018291static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018292static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018293#define ITEM_COMPARE_FAIL 999
18294
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018296 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018298 static int
18299#ifdef __BORLANDC__
18300_RTLENTRYF
18301#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018302item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018304 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018305 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018306 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018307 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018308 int res;
18309 char_u numbuf1[NUMBUFLEN];
18310 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018312 si1 = (sortItem_T *)s1;
18313 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018314 tv1 = &si1->item->li_tv;
18315 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018316
18317 if (item_compare_numbers)
18318 {
18319 long v1 = get_tv_number(tv1);
18320 long v2 = get_tv_number(tv2);
18321
18322 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18323 }
18324
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018325#ifdef FEAT_FLOAT
18326 if (item_compare_float)
18327 {
18328 float_T v1 = get_tv_float(tv1);
18329 float_T v2 = get_tv_float(tv2);
18330
18331 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18332 }
18333#endif
18334
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018335 /* tv2string() puts quotes around a string and allocates memory. Don't do
18336 * that for string variables. Use a single quote when comparing with a
18337 * non-string to do what the docs promise. */
18338 if (tv1->v_type == VAR_STRING)
18339 {
18340 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18341 p1 = (char_u *)"'";
18342 else
18343 p1 = tv1->vval.v_string;
18344 }
18345 else
18346 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18347 if (tv2->v_type == VAR_STRING)
18348 {
18349 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18350 p2 = (char_u *)"'";
18351 else
18352 p2 = tv2->vval.v_string;
18353 }
18354 else
18355 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018356 if (p1 == NULL)
18357 p1 = (char_u *)"";
18358 if (p2 == NULL)
18359 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018360 if (!item_compare_numeric)
18361 {
18362 if (item_compare_ic)
18363 res = STRICMP(p1, p2);
18364 else
18365 res = STRCMP(p1, p2);
18366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018368 {
18369 double n1, n2;
18370 n1 = strtod((char *)p1, (char **)&p1);
18371 n2 = strtod((char *)p2, (char **)&p2);
18372 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18373 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018374
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018375 /* When the result would be zero, compare the item indexes. Makes the
18376 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018377 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018378 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018379
Bram Moolenaar0d660222005-01-07 21:51:51 +000018380 vim_free(tofree1);
18381 vim_free(tofree2);
18382 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383}
18384
18385 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018386#ifdef __BORLANDC__
18387_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018389item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018390{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018391 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018392 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018393 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018394 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018395 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018397 /* shortcut after failure in previous call; compare all items equal */
18398 if (item_compare_func_err)
18399 return 0;
18400
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018401 si1 = (sortItem_T *)s1;
18402 si2 = (sortItem_T *)s2;
18403
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018404 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018405 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018406 copy_tv(&si1->item->li_tv, &argv[0]);
18407 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018408
18409 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018410 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018411 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18412 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018413 clear_tv(&argv[0]);
18414 clear_tv(&argv[1]);
18415
18416 if (res == FAIL)
18417 res = ITEM_COMPARE_FAIL;
18418 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018419 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18420 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018421 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018422 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018423
18424 /* When the result would be zero, compare the pointers themselves. Makes
18425 * the sort stable. */
18426 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018427 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018428
Bram Moolenaar0d660222005-01-07 21:51:51 +000018429 return res;
18430}
18431
18432/*
18433 * "sort({list})" function
18434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018436do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437{
Bram Moolenaar33570922005-01-25 22:26:29 +000018438 list_T *l;
18439 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018440 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018441 long len;
18442 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443
Bram Moolenaar0d660222005-01-07 21:51:51 +000018444 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018445 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446 else
18447 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018448 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018449 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018450 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18451 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018452 return;
18453 rettv->vval.v_list = l;
18454 rettv->v_type = VAR_LIST;
18455 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456
Bram Moolenaar0d660222005-01-07 21:51:51 +000018457 len = list_len(l);
18458 if (len <= 1)
18459 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460
Bram Moolenaar0d660222005-01-07 21:51:51 +000018461 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018462 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018463 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018464#ifdef FEAT_FLOAT
18465 item_compare_float = FALSE;
18466#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018467 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018468 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018469 if (argvars[1].v_type != VAR_UNKNOWN)
18470 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018471 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018472 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018473 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018474 else
18475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018476 int error = FALSE;
18477
18478 i = get_tv_number_chk(&argvars[1], &error);
18479 if (error)
18480 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018481 if (i == 1)
18482 item_compare_ic = TRUE;
18483 else
18484 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018485 if (item_compare_func != NULL)
18486 {
18487 if (STRCMP(item_compare_func, "n") == 0)
18488 {
18489 item_compare_func = NULL;
18490 item_compare_numeric = TRUE;
18491 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018492 else if (STRCMP(item_compare_func, "N") == 0)
18493 {
18494 item_compare_func = NULL;
18495 item_compare_numbers = TRUE;
18496 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018497#ifdef FEAT_FLOAT
18498 else if (STRCMP(item_compare_func, "f") == 0)
18499 {
18500 item_compare_func = NULL;
18501 item_compare_float = TRUE;
18502 }
18503#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018504 else if (STRCMP(item_compare_func, "i") == 0)
18505 {
18506 item_compare_func = NULL;
18507 item_compare_ic = TRUE;
18508 }
18509 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018510 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018511
18512 if (argvars[2].v_type != VAR_UNKNOWN)
18513 {
18514 /* optional third argument: {dict} */
18515 if (argvars[2].v_type != VAR_DICT)
18516 {
18517 EMSG(_(e_dictreq));
18518 return;
18519 }
18520 item_compare_selfdict = argvars[2].vval.v_dict;
18521 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523
Bram Moolenaar0d660222005-01-07 21:51:51 +000018524 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018525 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018526 if (ptrs == NULL)
18527 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528
Bram Moolenaar327aa022014-03-25 18:24:23 +010018529 i = 0;
18530 if (sort)
18531 {
18532 /* sort(): ptrs will be the list to sort */
18533 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018534 {
18535 ptrs[i].item = li;
18536 ptrs[i].idx = i;
18537 ++i;
18538 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018539
18540 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018541 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018542 /* test the compare function */
18543 if (item_compare_func != NULL
18544 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018545 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018546 EMSG(_("E702: Sort compare function failed"));
18547 else
18548 {
18549 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018550 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018551 item_compare_func == NULL ? item_compare : item_compare2);
18552
18553 if (!item_compare_func_err)
18554 {
18555 /* Clear the List and append the items in sorted order. */
18556 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18557 l->lv_len = 0;
18558 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018559 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018560 }
18561 }
18562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018564 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018565 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018566
18567 /* f_uniq(): ptrs will be a stack of items to remove */
18568 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018569 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018570 item_compare_func_ptr = item_compare_func
18571 ? item_compare2 : item_compare;
18572
18573 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18574 li = li->li_next)
18575 {
18576 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18577 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018578 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018579 if (item_compare_func_err)
18580 {
18581 EMSG(_("E882: Uniq compare function failed"));
18582 break;
18583 }
18584 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018585
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018586 if (!item_compare_func_err)
18587 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018588 while (--i >= 0)
18589 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018590 li = ptrs[i].item->li_next;
18591 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018592 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018593 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018594 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018595 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018596 list_fix_watch(l, li);
18597 listitem_free(li);
18598 l->lv_len--;
18599 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018600 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018601 }
18602
18603 vim_free(ptrs);
18604 }
18605}
18606
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018607/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018608 * "sort({list})" function
18609 */
18610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018611f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018612{
18613 do_sort_uniq(argvars, rettv, TRUE);
18614}
18615
18616/*
18617 * "uniq({list})" function
18618 */
18619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018620f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018621{
18622 do_sort_uniq(argvars, rettv, FALSE);
18623}
18624
18625/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018626 * "soundfold({word})" function
18627 */
18628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018629f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018630{
18631 char_u *s;
18632
18633 rettv->v_type = VAR_STRING;
18634 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018635#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018636 rettv->vval.v_string = eval_soundfold(s);
18637#else
18638 rettv->vval.v_string = vim_strsave(s);
18639#endif
18640}
18641
18642/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018643 * "spellbadword()" function
18644 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018646f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018647{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018648 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018649 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018650 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018651
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018652 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018653 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018654
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018655#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018656 if (argvars[0].v_type == VAR_UNKNOWN)
18657 {
18658 /* Find the start and length of the badly spelled word. */
18659 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18660 if (len != 0)
18661 word = ml_get_cursor();
18662 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018663 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018664 {
18665 char_u *str = get_tv_string_chk(&argvars[0]);
18666 int capcol = -1;
18667
18668 if (str != NULL)
18669 {
18670 /* Check the argument for spelling. */
18671 while (*str != NUL)
18672 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018673 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018674 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018675 {
18676 word = str;
18677 break;
18678 }
18679 str += len;
18680 }
18681 }
18682 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018683#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018684
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018685 list_append_string(rettv->vval.v_list, word, len);
18686 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018687 attr == HLF_SPB ? "bad" :
18688 attr == HLF_SPR ? "rare" :
18689 attr == HLF_SPL ? "local" :
18690 attr == HLF_SPC ? "caps" :
18691 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018692}
18693
18694/*
18695 * "spellsuggest()" function
18696 */
18697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018698f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018699{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018700#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018701 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018702 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018703 int maxcount;
18704 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018705 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018706 listitem_T *li;
18707 int need_capital = FALSE;
18708#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018709
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018710 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018711 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018712
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018713#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018714 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018715 {
18716 str = get_tv_string(&argvars[0]);
18717 if (argvars[1].v_type != VAR_UNKNOWN)
18718 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018719 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018720 if (maxcount <= 0)
18721 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018722 if (argvars[2].v_type != VAR_UNKNOWN)
18723 {
18724 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18725 if (typeerr)
18726 return;
18727 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018728 }
18729 else
18730 maxcount = 25;
18731
Bram Moolenaar4770d092006-01-12 23:22:24 +000018732 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018733
18734 for (i = 0; i < ga.ga_len; ++i)
18735 {
18736 str = ((char_u **)ga.ga_data)[i];
18737
18738 li = listitem_alloc();
18739 if (li == NULL)
18740 vim_free(str);
18741 else
18742 {
18743 li->li_tv.v_type = VAR_STRING;
18744 li->li_tv.v_lock = 0;
18745 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018746 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018747 }
18748 }
18749 ga_clear(&ga);
18750 }
18751#endif
18752}
18753
Bram Moolenaar0d660222005-01-07 21:51:51 +000018754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018755f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018756{
18757 char_u *str;
18758 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018759 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018760 regmatch_T regmatch;
18761 char_u patbuf[NUMBUFLEN];
18762 char_u *save_cpo;
18763 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018764 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018765 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018766 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018767
18768 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18769 save_cpo = p_cpo;
18770 p_cpo = (char_u *)"";
18771
18772 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018773 if (argvars[1].v_type != VAR_UNKNOWN)
18774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018775 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18776 if (pat == NULL)
18777 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018778 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018779 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018780 }
18781 if (pat == NULL || *pat == NUL)
18782 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018783
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018784 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018786 if (typeerr)
18787 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788
Bram Moolenaar0d660222005-01-07 21:51:51 +000018789 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18790 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018792 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018793 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018794 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018795 if (*str == NUL)
18796 match = FALSE; /* empty item at the end */
18797 else
18798 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018799 if (match)
18800 end = regmatch.startp[0];
18801 else
18802 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018803 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18804 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018805 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018806 if (list_append_string(rettv->vval.v_list, str,
18807 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018808 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018809 }
18810 if (!match)
18811 break;
18812 /* Advance to just after the match. */
18813 if (regmatch.endp[0] > str)
18814 col = 0;
18815 else
18816 {
18817 /* Don't get stuck at the same match. */
18818#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018819 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018820#else
18821 col = 1;
18822#endif
18823 }
18824 str = regmatch.endp[0];
18825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826
Bram Moolenaar473de612013-06-08 18:19:48 +020018827 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829
Bram Moolenaar0d660222005-01-07 21:51:51 +000018830 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831}
18832
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018833#ifdef FEAT_FLOAT
18834/*
18835 * "sqrt()" function
18836 */
18837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018838f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018839{
18840 float_T f;
18841
18842 rettv->v_type = VAR_FLOAT;
18843 if (get_float_arg(argvars, &f) == OK)
18844 rettv->vval.v_float = sqrt(f);
18845 else
18846 rettv->vval.v_float = 0.0;
18847}
18848
18849/*
18850 * "str2float()" function
18851 */
18852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018853f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018854{
18855 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18856
18857 if (*p == '+')
18858 p = skipwhite(p + 1);
18859 (void)string2float(p, &rettv->vval.v_float);
18860 rettv->v_type = VAR_FLOAT;
18861}
18862#endif
18863
Bram Moolenaar2c932302006-03-18 21:42:09 +000018864/*
18865 * "str2nr()" function
18866 */
18867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018868f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018869{
18870 int base = 10;
18871 char_u *p;
18872 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018873 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018874
18875 if (argvars[1].v_type != VAR_UNKNOWN)
18876 {
18877 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018878 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018879 {
18880 EMSG(_(e_invarg));
18881 return;
18882 }
18883 }
18884
18885 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018886 if (*p == '+')
18887 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018888 switch (base)
18889 {
18890 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18891 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18892 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18893 default: what = 0;
18894 }
18895 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018896 rettv->vval.v_number = n;
18897}
18898
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899#ifdef HAVE_STRFTIME
18900/*
18901 * "strftime({format}[, {time}])" function
18902 */
18903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018904f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905{
18906 char_u result_buf[256];
18907 struct tm *curtime;
18908 time_t seconds;
18909 char_u *p;
18910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018911 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018913 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018914 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 seconds = time(NULL);
18916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018917 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 curtime = localtime(&seconds);
18919 /* MSVC returns NULL for an invalid value of seconds. */
18920 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018921 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 else
18923 {
18924# ifdef FEAT_MBYTE
18925 vimconv_T conv;
18926 char_u *enc;
18927
18928 conv.vc_type = CONV_NONE;
18929 enc = enc_locale();
18930 convert_setup(&conv, p_enc, enc);
18931 if (conv.vc_type != CONV_NONE)
18932 p = string_convert(&conv, p, NULL);
18933# endif
18934 if (p != NULL)
18935 (void)strftime((char *)result_buf, sizeof(result_buf),
18936 (char *)p, curtime);
18937 else
18938 result_buf[0] = NUL;
18939
18940# ifdef FEAT_MBYTE
18941 if (conv.vc_type != CONV_NONE)
18942 vim_free(p);
18943 convert_setup(&conv, enc, p_enc);
18944 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018945 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946 else
18947# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018948 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949
18950# ifdef FEAT_MBYTE
18951 /* Release conversion descriptors */
18952 convert_setup(&conv, NULL, NULL);
18953 vim_free(enc);
18954# endif
18955 }
18956}
18957#endif
18958
18959/*
18960 * "stridx()" function
18961 */
18962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018963f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964{
18965 char_u buf[NUMBUFLEN];
18966 char_u *needle;
18967 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018968 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018970 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018972 needle = get_tv_string_chk(&argvars[1]);
18973 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018974 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018975 if (needle == NULL || haystack == NULL)
18976 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977
Bram Moolenaar33570922005-01-25 22:26:29 +000018978 if (argvars[2].v_type != VAR_UNKNOWN)
18979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018980 int error = FALSE;
18981
18982 start_idx = get_tv_number_chk(&argvars[2], &error);
18983 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018984 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018985 if (start_idx >= 0)
18986 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018987 }
18988
18989 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18990 if (pos != NULL)
18991 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992}
18993
18994/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018995 * "string()" function
18996 */
18997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018998f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018999{
19000 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019001 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019003 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019004 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019005 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019006 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019007 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008}
19009
19010/*
19011 * "strlen()" function
19012 */
19013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019014f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019016 rettv->vval.v_number = (varnumber_T)(STRLEN(
19017 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018}
19019
19020/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019021 * "strchars()" function
19022 */
19023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019024f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019025{
19026 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019027 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019028#ifdef FEAT_MBYTE
19029 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019030 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019031#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019032
19033 if (argvars[1].v_type != VAR_UNKNOWN)
19034 skipcc = get_tv_number_chk(&argvars[1], NULL);
19035 if (skipcc < 0 || skipcc > 1)
19036 EMSG(_(e_invarg));
19037 else
19038 {
19039#ifdef FEAT_MBYTE
19040 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19041 while (*s != NUL)
19042 {
19043 func_mb_ptr2char_adv(&s);
19044 ++len;
19045 }
19046 rettv->vval.v_number = len;
19047#else
19048 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19049#endif
19050 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019051}
19052
19053/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019054 * "strdisplaywidth()" function
19055 */
19056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019057f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019058{
19059 char_u *s = get_tv_string(&argvars[0]);
19060 int col = 0;
19061
19062 if (argvars[1].v_type != VAR_UNKNOWN)
19063 col = get_tv_number(&argvars[1]);
19064
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019065 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019066}
19067
19068/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019069 * "strwidth()" function
19070 */
19071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019072f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019073{
19074 char_u *s = get_tv_string(&argvars[0]);
19075
19076 rettv->vval.v_number = (varnumber_T)(
19077#ifdef FEAT_MBYTE
19078 mb_string2cells(s, -1)
19079#else
19080 STRLEN(s)
19081#endif
19082 );
19083}
19084
19085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086 * "strpart()" function
19087 */
19088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019089f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090{
19091 char_u *p;
19092 int n;
19093 int len;
19094 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019095 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019097 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 slen = (int)STRLEN(p);
19099
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019100 n = get_tv_number_chk(&argvars[1], &error);
19101 if (error)
19102 len = 0;
19103 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019104 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105 else
19106 len = slen - n; /* default len: all bytes that are available. */
19107
19108 /*
19109 * Only return the overlap between the specified part and the actual
19110 * string.
19111 */
19112 if (n < 0)
19113 {
19114 len += n;
19115 n = 0;
19116 }
19117 else if (n > slen)
19118 n = slen;
19119 if (len < 0)
19120 len = 0;
19121 else if (n + len > slen)
19122 len = slen - n;
19123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019124 rettv->v_type = VAR_STRING;
19125 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126}
19127
19128/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019129 * "strridx()" function
19130 */
19131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019132f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019133{
19134 char_u buf[NUMBUFLEN];
19135 char_u *needle;
19136 char_u *haystack;
19137 char_u *rest;
19138 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019139 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019141 needle = get_tv_string_chk(&argvars[1]);
19142 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019143
19144 rettv->vval.v_number = -1;
19145 if (needle == NULL || haystack == NULL)
19146 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019147
19148 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019149 if (argvars[2].v_type != VAR_UNKNOWN)
19150 {
19151 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019152 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019153 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019154 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019155 }
19156 else
19157 end_idx = haystack_len;
19158
Bram Moolenaar0d660222005-01-07 21:51:51 +000019159 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019160 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019161 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019162 lastmatch = haystack + end_idx;
19163 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019164 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019165 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019166 for (rest = haystack; *rest != '\0'; ++rest)
19167 {
19168 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019169 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019170 break;
19171 lastmatch = rest;
19172 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019173 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019174
19175 if (lastmatch == NULL)
19176 rettv->vval.v_number = -1;
19177 else
19178 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19179}
19180
19181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182 * "strtrans()" function
19183 */
19184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019185f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019187 rettv->v_type = VAR_STRING;
19188 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189}
19190
19191/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019192 * "submatch()" function
19193 */
19194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019195f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019196{
Bram Moolenaar41571762014-04-02 19:00:58 +020019197 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019198 int no;
19199 int retList = 0;
19200
19201 no = (int)get_tv_number_chk(&argvars[0], &error);
19202 if (error)
19203 return;
19204 error = FALSE;
19205 if (argvars[1].v_type != VAR_UNKNOWN)
19206 retList = get_tv_number_chk(&argvars[1], &error);
19207 if (error)
19208 return;
19209
19210 if (retList == 0)
19211 {
19212 rettv->v_type = VAR_STRING;
19213 rettv->vval.v_string = reg_submatch(no);
19214 }
19215 else
19216 {
19217 rettv->v_type = VAR_LIST;
19218 rettv->vval.v_list = reg_submatch_list(no);
19219 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019220}
19221
19222/*
19223 * "substitute()" function
19224 */
19225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019226f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019227{
19228 char_u patbuf[NUMBUFLEN];
19229 char_u subbuf[NUMBUFLEN];
19230 char_u flagsbuf[NUMBUFLEN];
19231
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019232 char_u *str = get_tv_string_chk(&argvars[0]);
19233 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19234 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19235 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19236
Bram Moolenaar0d660222005-01-07 21:51:51 +000019237 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019238 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19239 rettv->vval.v_string = NULL;
19240 else
19241 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019242}
19243
19244/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019245 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019248f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249{
19250 int id = 0;
19251#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019252 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 long col;
19254 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019255 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019257 lnum = get_tv_lnum(argvars); /* -1 on type error */
19258 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19259 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019261 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019262 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019263 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264#endif
19265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019266 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019267}
19268
19269/*
19270 * "synIDattr(id, what [, mode])" function
19271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019273f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274{
19275 char_u *p = NULL;
19276#ifdef FEAT_SYN_HL
19277 int id;
19278 char_u *what;
19279 char_u *mode;
19280 char_u modebuf[NUMBUFLEN];
19281 int modec;
19282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019283 id = get_tv_number(&argvars[0]);
19284 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019285 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019287 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019289 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290 modec = 0; /* replace invalid with current */
19291 }
19292 else
19293 {
19294#ifdef FEAT_GUI
19295 if (gui.in_use)
19296 modec = 'g';
19297 else
19298#endif
19299 if (t_colors > 1)
19300 modec = 'c';
19301 else
19302 modec = 't';
19303 }
19304
19305
19306 switch (TOLOWER_ASC(what[0]))
19307 {
19308 case 'b':
19309 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19310 p = highlight_color(id, what, modec);
19311 else /* bold */
19312 p = highlight_has_attr(id, HL_BOLD, modec);
19313 break;
19314
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019315 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316 p = highlight_color(id, what, modec);
19317 break;
19318
19319 case 'i':
19320 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19321 p = highlight_has_attr(id, HL_INVERSE, modec);
19322 else /* italic */
19323 p = highlight_has_attr(id, HL_ITALIC, modec);
19324 break;
19325
19326 case 'n': /* name */
19327 p = get_highlight_name(NULL, id - 1);
19328 break;
19329
19330 case 'r': /* reverse */
19331 p = highlight_has_attr(id, HL_INVERSE, modec);
19332 break;
19333
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019334 case 's':
19335 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19336 p = highlight_color(id, what, modec);
19337 else /* standout */
19338 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 break;
19340
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019341 case 'u':
19342 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19343 /* underline */
19344 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19345 else
19346 /* undercurl */
19347 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348 break;
19349 }
19350
19351 if (p != NULL)
19352 p = vim_strsave(p);
19353#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019354 rettv->v_type = VAR_STRING;
19355 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356}
19357
19358/*
19359 * "synIDtrans(id)" function
19360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019362f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363{
19364 int id;
19365
19366#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019367 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368
19369 if (id > 0)
19370 id = syn_get_final_id(id);
19371 else
19372#endif
19373 id = 0;
19374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019375 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376}
19377
19378/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019379 * "synconcealed(lnum, col)" function
19380 */
19381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019382f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019383{
19384#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19385 long lnum;
19386 long col;
19387 int syntax_flags = 0;
19388 int cchar;
19389 int matchid = 0;
19390 char_u str[NUMBUFLEN];
19391#endif
19392
19393 rettv->v_type = VAR_LIST;
19394 rettv->vval.v_list = NULL;
19395
19396#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19397 lnum = get_tv_lnum(argvars); /* -1 on type error */
19398 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19399
19400 vim_memset(str, NUL, sizeof(str));
19401
19402 if (rettv_list_alloc(rettv) != FAIL)
19403 {
19404 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19405 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19406 && curwin->w_p_cole > 0)
19407 {
19408 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19409 syntax_flags = get_syntax_info(&matchid);
19410
19411 /* get the conceal character */
19412 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19413 {
19414 cchar = syn_get_sub_char();
19415 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19416 cchar = lcs_conceal;
19417 if (cchar != NUL)
19418 {
19419# ifdef FEAT_MBYTE
19420 if (has_mbyte)
19421 (*mb_char2bytes)(cchar, str);
19422 else
19423# endif
19424 str[0] = cchar;
19425 }
19426 }
19427 }
19428
19429 list_append_number(rettv->vval.v_list,
19430 (syntax_flags & HL_CONCEAL) != 0);
19431 /* -1 to auto-determine strlen */
19432 list_append_string(rettv->vval.v_list, str, -1);
19433 list_append_number(rettv->vval.v_list, matchid);
19434 }
19435#endif
19436}
19437
19438/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019439 * "synstack(lnum, col)" function
19440 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019442f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019443{
19444#ifdef FEAT_SYN_HL
19445 long lnum;
19446 long col;
19447 int i;
19448 int id;
19449#endif
19450
19451 rettv->v_type = VAR_LIST;
19452 rettv->vval.v_list = NULL;
19453
19454#ifdef FEAT_SYN_HL
19455 lnum = get_tv_lnum(argvars); /* -1 on type error */
19456 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19457
19458 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019459 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019460 && rettv_list_alloc(rettv) != FAIL)
19461 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019462 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019463 for (i = 0; ; ++i)
19464 {
19465 id = syn_get_stack_item(i);
19466 if (id < 0)
19467 break;
19468 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19469 break;
19470 }
19471 }
19472#endif
19473}
19474
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019476get_cmd_output_as_rettv(
19477 typval_T *argvars,
19478 typval_T *rettv,
19479 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019481 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019483 char_u *infile = NULL;
19484 char_u buf[NUMBUFLEN];
19485 int err = FALSE;
19486 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019487 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019488 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019490 rettv->v_type = VAR_STRING;
19491 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019492 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019493 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019494
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019495 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019496 {
19497 /*
19498 * Write the string to a temp file, to be used for input of the shell
19499 * command.
19500 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019501 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019502 {
19503 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019504 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019505 }
19506
19507 fd = mch_fopen((char *)infile, WRITEBIN);
19508 if (fd == NULL)
19509 {
19510 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019511 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019512 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019513 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019514 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019515 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19516 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019517 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019518 else
19519 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019520 size_t len;
19521
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019522 p = get_tv_string_buf_chk(&argvars[1], buf);
19523 if (p == NULL)
19524 {
19525 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019526 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019527 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019528 len = STRLEN(p);
19529 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019530 err = TRUE;
19531 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019532 if (fclose(fd) != 0)
19533 err = TRUE;
19534 if (err)
19535 {
19536 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019537 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019538 }
19539 }
19540
Bram Moolenaar52a72462014-08-29 15:53:52 +020019541 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19542 * echoes typeahead, that messes up the display. */
19543 if (!msg_silent)
19544 flags += SHELL_COOKED;
19545
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019546 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019548 int len;
19549 listitem_T *li;
19550 char_u *s = NULL;
19551 char_u *start;
19552 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019553 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554
Bram Moolenaar52a72462014-08-29 15:53:52 +020019555 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019556 if (res == NULL)
19557 goto errret;
19558
19559 list = list_alloc();
19560 if (list == NULL)
19561 goto errret;
19562
19563 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019565 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019566 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019567 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019568 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019569
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019570 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019571 if (s == NULL)
19572 goto errret;
19573
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019574 for (p = s; start < end; ++p, ++start)
19575 *p = *start == NUL ? NL : *start;
19576 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019577
19578 li = listitem_alloc();
19579 if (li == NULL)
19580 {
19581 vim_free(s);
19582 goto errret;
19583 }
19584 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019585 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019586 li->li_tv.vval.v_string = s;
19587 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019589
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019590 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019591 rettv->v_type = VAR_LIST;
19592 rettv->vval.v_list = list;
19593 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019595 else
19596 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019597 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019598#ifdef USE_CR
19599 /* translate <CR> into <NL> */
19600 if (res != NULL)
19601 {
19602 char_u *s;
19603
19604 for (s = res; *s; ++s)
19605 {
19606 if (*s == CAR)
19607 *s = NL;
19608 }
19609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610#else
19611# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019612 /* translate <CR><NL> into <NL> */
19613 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019615 char_u *s, *d;
19616
19617 d = res;
19618 for (s = res; *s; ++s)
19619 {
19620 if (s[0] == CAR && s[1] == NL)
19621 ++s;
19622 *d++ = *s;
19623 }
19624 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626# endif
19627#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019628 rettv->vval.v_string = res;
19629 res = NULL;
19630 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019631
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019632errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019633 if (infile != NULL)
19634 {
19635 mch_remove(infile);
19636 vim_free(infile);
19637 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019638 if (res != NULL)
19639 vim_free(res);
19640 if (list != NULL)
19641 list_free(list, TRUE);
19642}
19643
19644/*
19645 * "system()" function
19646 */
19647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019648f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019649{
19650 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19651}
19652
19653/*
19654 * "systemlist()" function
19655 */
19656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019657f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019658{
19659 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660}
19661
19662/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019663 * "tabpagebuflist()" function
19664 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019666f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019667{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019668#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019669 tabpage_T *tp;
19670 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019671
19672 if (argvars[0].v_type == VAR_UNKNOWN)
19673 wp = firstwin;
19674 else
19675 {
19676 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19677 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019678 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019679 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019680 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019681 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019682 for (; wp != NULL; wp = wp->w_next)
19683 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019684 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019685 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019686 }
19687#endif
19688}
19689
19690
19691/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019692 * "tabpagenr()" function
19693 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019695f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019696{
19697 int nr = 1;
19698#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019699 char_u *arg;
19700
19701 if (argvars[0].v_type != VAR_UNKNOWN)
19702 {
19703 arg = get_tv_string_chk(&argvars[0]);
19704 nr = 0;
19705 if (arg != NULL)
19706 {
19707 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019708 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019709 else
19710 EMSG2(_(e_invexpr2), arg);
19711 }
19712 }
19713 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019714 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019715#endif
19716 rettv->vval.v_number = nr;
19717}
19718
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019719
19720#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019721static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019722
19723/*
19724 * Common code for tabpagewinnr() and winnr().
19725 */
19726 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019727get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019728{
19729 win_T *twin;
19730 int nr = 1;
19731 win_T *wp;
19732 char_u *arg;
19733
19734 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19735 if (argvar->v_type != VAR_UNKNOWN)
19736 {
19737 arg = get_tv_string_chk(argvar);
19738 if (arg == NULL)
19739 nr = 0; /* type error; errmsg already given */
19740 else if (STRCMP(arg, "$") == 0)
19741 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19742 else if (STRCMP(arg, "#") == 0)
19743 {
19744 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19745 if (twin == NULL)
19746 nr = 0;
19747 }
19748 else
19749 {
19750 EMSG2(_(e_invexpr2), arg);
19751 nr = 0;
19752 }
19753 }
19754
19755 if (nr > 0)
19756 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19757 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019758 {
19759 if (wp == NULL)
19760 {
19761 /* didn't find it in this tabpage */
19762 nr = 0;
19763 break;
19764 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019765 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019766 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019767 return nr;
19768}
19769#endif
19770
19771/*
19772 * "tabpagewinnr()" function
19773 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019775f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019776{
19777 int nr = 1;
19778#ifdef FEAT_WINDOWS
19779 tabpage_T *tp;
19780
19781 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19782 if (tp == NULL)
19783 nr = 0;
19784 else
19785 nr = get_winnr(tp, &argvars[1]);
19786#endif
19787 rettv->vval.v_number = nr;
19788}
19789
19790
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019791/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019792 * "tagfiles()" function
19793 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019795f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019796{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019797 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019798 tagname_T tn;
19799 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019800
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019801 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019802 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019803 fname = alloc(MAXPATHL);
19804 if (fname == NULL)
19805 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019806
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019807 for (first = TRUE; ; first = FALSE)
19808 if (get_tagfname(&tn, first, fname) == FAIL
19809 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019810 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019811 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019812 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019813}
19814
19815/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019816 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019817 */
19818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019819f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019820{
19821 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019822
19823 tag_pattern = get_tv_string(&argvars[0]);
19824
19825 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019826 if (*tag_pattern == NUL)
19827 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019828
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019829 if (rettv_list_alloc(rettv) == OK)
19830 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019831}
19832
19833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834 * "tempname()" function
19835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019837f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838{
19839 static int x = 'A';
19840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019841 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019842 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843
19844 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19845 * names. Skip 'I' and 'O', they are used for shell redirection. */
19846 do
19847 {
19848 if (x == 'Z')
19849 x = '0';
19850 else if (x == '9')
19851 x = 'A';
19852 else
19853 {
19854#ifdef EBCDIC
19855 if (x == 'I')
19856 x = 'J';
19857 else if (x == 'R')
19858 x = 'S';
19859 else
19860#endif
19861 ++x;
19862 }
19863 } while (x == 'I' || x == 'O');
19864}
19865
19866/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019867 * "test(list)" function: Just checking the walls...
19868 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019870f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019871{
19872 /* Used for unit testing. Change the code below to your liking. */
19873#if 0
19874 listitem_T *li;
19875 list_T *l;
19876 char_u *bad, *good;
19877
19878 if (argvars[0].v_type != VAR_LIST)
19879 return;
19880 l = argvars[0].vval.v_list;
19881 if (l == NULL)
19882 return;
19883 li = l->lv_first;
19884 if (li == NULL)
19885 return;
19886 bad = get_tv_string(&li->li_tv);
19887 li = li->li_next;
19888 if (li == NULL)
19889 return;
19890 good = get_tv_string(&li->li_tv);
19891 rettv->vval.v_number = test_edit_score(bad, good);
19892#endif
19893}
19894
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019895#ifdef FEAT_FLOAT
19896/*
19897 * "tan()" function
19898 */
19899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019900f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019901{
19902 float_T f;
19903
19904 rettv->v_type = VAR_FLOAT;
19905 if (get_float_arg(argvars, &f) == OK)
19906 rettv->vval.v_float = tan(f);
19907 else
19908 rettv->vval.v_float = 0.0;
19909}
19910
19911/*
19912 * "tanh()" function
19913 */
19914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019915f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019916{
19917 float_T f;
19918
19919 rettv->v_type = VAR_FLOAT;
19920 if (get_float_arg(argvars, &f) == OK)
19921 rettv->vval.v_float = tanh(f);
19922 else
19923 rettv->vval.v_float = 0.0;
19924}
19925#endif
19926
Bram Moolenaard52d9742005-08-21 22:20:28 +000019927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 * "tolower(string)" function
19929 */
19930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019931f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932{
19933 char_u *p;
19934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019935 p = vim_strsave(get_tv_string(&argvars[0]));
19936 rettv->v_type = VAR_STRING;
19937 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938
19939 if (p != NULL)
19940 while (*p != NUL)
19941 {
19942#ifdef FEAT_MBYTE
19943 int l;
19944
19945 if (enc_utf8)
19946 {
19947 int c, lc;
19948
19949 c = utf_ptr2char(p);
19950 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019951 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 /* TODO: reallocate string when byte count changes. */
19953 if (utf_char2len(lc) == l)
19954 utf_char2bytes(lc, p);
19955 p += l;
19956 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019957 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958 p += l; /* skip multi-byte character */
19959 else
19960#endif
19961 {
19962 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19963 ++p;
19964 }
19965 }
19966}
19967
19968/*
19969 * "toupper(string)" function
19970 */
19971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019972f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019974 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019975 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976}
19977
19978/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019979 * "tr(string, fromstr, tostr)" function
19980 */
19981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019982f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019983{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019984 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019985 char_u *fromstr;
19986 char_u *tostr;
19987 char_u *p;
19988#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019989 int inlen;
19990 int fromlen;
19991 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019992 int idx;
19993 char_u *cpstr;
19994 int cplen;
19995 int first = TRUE;
19996#endif
19997 char_u buf[NUMBUFLEN];
19998 char_u buf2[NUMBUFLEN];
19999 garray_T ga;
20000
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020001 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020002 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20003 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020004
20005 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020006 rettv->v_type = VAR_STRING;
20007 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020008 if (fromstr == NULL || tostr == NULL)
20009 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020010 ga_init2(&ga, (int)sizeof(char), 80);
20011
20012#ifdef FEAT_MBYTE
20013 if (!has_mbyte)
20014#endif
20015 /* not multi-byte: fromstr and tostr must be the same length */
20016 if (STRLEN(fromstr) != STRLEN(tostr))
20017 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020018#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020019error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020020#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020021 EMSG2(_(e_invarg2), fromstr);
20022 ga_clear(&ga);
20023 return;
20024 }
20025
20026 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020027 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020028 {
20029#ifdef FEAT_MBYTE
20030 if (has_mbyte)
20031 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020032 inlen = (*mb_ptr2len)(in_str);
20033 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020034 cplen = inlen;
20035 idx = 0;
20036 for (p = fromstr; *p != NUL; p += fromlen)
20037 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020038 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020039 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020040 {
20041 for (p = tostr; *p != NUL; p += tolen)
20042 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020043 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020044 if (idx-- == 0)
20045 {
20046 cplen = tolen;
20047 cpstr = p;
20048 break;
20049 }
20050 }
20051 if (*p == NUL) /* tostr is shorter than fromstr */
20052 goto error;
20053 break;
20054 }
20055 ++idx;
20056 }
20057
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020058 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020059 {
20060 /* Check that fromstr and tostr have the same number of
20061 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020062 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020063 first = FALSE;
20064 for (p = tostr; *p != NUL; p += tolen)
20065 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020066 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020067 --idx;
20068 }
20069 if (idx != 0)
20070 goto error;
20071 }
20072
Bram Moolenaarcde88542015-08-11 19:14:00 +020020073 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020074 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020075 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020076
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020077 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020078 }
20079 else
20080#endif
20081 {
20082 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020083 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020084 if (p != NULL)
20085 ga_append(&ga, tostr[p - fromstr]);
20086 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020087 ga_append(&ga, *in_str);
20088 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020089 }
20090 }
20091
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020092 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020093 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020094 ga_append(&ga, NUL);
20095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020097}
20098
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020099#ifdef FEAT_FLOAT
20100/*
20101 * "trunc({float})" function
20102 */
20103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020104f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020105{
20106 float_T f;
20107
20108 rettv->v_type = VAR_FLOAT;
20109 if (get_float_arg(argvars, &f) == OK)
20110 /* trunc() is not in C90, use floor() or ceil() instead. */
20111 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20112 else
20113 rettv->vval.v_float = 0.0;
20114}
20115#endif
20116
Bram Moolenaar8299df92004-07-10 09:47:34 +000020117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 * "type(expr)" function
20119 */
20120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020121f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020123 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020124
20125 switch (argvars[0].v_type)
20126 {
20127 case VAR_NUMBER: n = 0; break;
20128 case VAR_STRING: n = 1; break;
20129 case VAR_FUNC: n = 2; break;
20130 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020131 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020132 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020133 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020134 if (argvars[0].vval.v_number == VVAL_FALSE
20135 || argvars[0].vval.v_number == VVAL_TRUE)
20136 n = 6;
20137 else
20138 n = 7;
20139 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020140 case VAR_JOB: n = 8; break;
20141 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020142 case VAR_UNKNOWN:
20143 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20144 n = -1;
20145 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020146 }
20147 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148}
20149
20150/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020151 * "undofile(name)" function
20152 */
20153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020154f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020155{
20156 rettv->v_type = VAR_STRING;
20157#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020158 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020159 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020160
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020161 if (*fname == NUL)
20162 {
20163 /* If there is no file name there will be no undo file. */
20164 rettv->vval.v_string = NULL;
20165 }
20166 else
20167 {
20168 char_u *ffname = FullName_save(fname, FALSE);
20169
20170 if (ffname != NULL)
20171 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20172 vim_free(ffname);
20173 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020174 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020175#else
20176 rettv->vval.v_string = NULL;
20177#endif
20178}
20179
20180/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020181 * "undotree()" function
20182 */
20183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020184f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020185{
20186 if (rettv_dict_alloc(rettv) == OK)
20187 {
20188 dict_T *dict = rettv->vval.v_dict;
20189 list_T *list;
20190
Bram Moolenaar730cde92010-06-27 05:18:54 +020020191 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020192 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020193 dict_add_nr_str(dict, "save_last",
20194 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020195 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20196 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020197 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020198
20199 list = list_alloc();
20200 if (list != NULL)
20201 {
20202 u_eval_tree(curbuf->b_u_oldhead, list);
20203 dict_add_list(dict, "entries", list);
20204 }
20205 }
20206}
20207
20208/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020209 * "values(dict)" function
20210 */
20211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020212f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020213{
20214 dict_list(argvars, rettv, 1);
20215}
20216
20217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 * "virtcol(string)" function
20219 */
20220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020221f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222{
20223 colnr_T vcol = 0;
20224 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020225 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020227 fp = var2fpos(&argvars[0], FALSE, &fnum);
20228 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20229 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 {
20231 getvvcol(curwin, fp, NULL, NULL, &vcol);
20232 ++vcol;
20233 }
20234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020235 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236}
20237
20238/*
20239 * "visualmode()" function
20240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020242f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 char_u str[2];
20245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020246 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 str[0] = curbuf->b_visual_mode_eval;
20248 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020249 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250
20251 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020252 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254}
20255
20256/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020257 * "wildmenumode()" function
20258 */
20259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020260f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020261{
20262#ifdef FEAT_WILDMENU
20263 if (wild_menu_showing)
20264 rettv->vval.v_number = 1;
20265#endif
20266}
20267
20268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 * "winbufnr(nr)" function
20270 */
20271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020272f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273{
20274 win_T *wp;
20275
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020276 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020278 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020280 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281}
20282
20283/*
20284 * "wincol()" function
20285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020287f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288{
20289 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020290 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291}
20292
20293/*
20294 * "winheight(nr)" function
20295 */
20296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020297f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298{
20299 win_T *wp;
20300
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020301 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020303 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020305 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306}
20307
20308/*
20309 * "winline()" function
20310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020312f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313{
20314 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020315 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316}
20317
20318/*
20319 * "winnr()" function
20320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020322f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323{
20324 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020325
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020327 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020329 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330}
20331
20332/*
20333 * "winrestcmd()" function
20334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020336f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337{
20338#ifdef FEAT_WINDOWS
20339 win_T *wp;
20340 int winnr = 1;
20341 garray_T ga;
20342 char_u buf[50];
20343
20344 ga_init2(&ga, (int)sizeof(char), 70);
20345 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20346 {
20347 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20348 ga_concat(&ga, buf);
20349# ifdef FEAT_VERTSPLIT
20350 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20351 ga_concat(&ga, buf);
20352# endif
20353 ++winnr;
20354 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020355 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020357 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020359 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020361 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362}
20363
20364/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020365 * "winrestview()" function
20366 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020368f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020369{
20370 dict_T *dict;
20371
20372 if (argvars[0].v_type != VAR_DICT
20373 || (dict = argvars[0].vval.v_dict) == NULL)
20374 EMSG(_(e_invarg));
20375 else
20376 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020377 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20378 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20379 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20380 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020381#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020382 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20383 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020384#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020385 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20386 {
20387 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20388 curwin->w_set_curswant = FALSE;
20389 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020390
Bram Moolenaar82c25852014-05-28 16:47:16 +020020391 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20392 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020393#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020394 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20395 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020396#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020397 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20398 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20399 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20400 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020401
20402 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020403 win_new_height(curwin, curwin->w_height);
20404# ifdef FEAT_VERTSPLIT
20405 win_new_width(curwin, W_WIDTH(curwin));
20406# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020407 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020408
Bram Moolenaarb851a962014-10-31 15:45:52 +010020409 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020410 curwin->w_topline = 1;
20411 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20412 curwin->w_topline = curbuf->b_ml.ml_line_count;
20413#ifdef FEAT_DIFF
20414 check_topfill(curwin, TRUE);
20415#endif
20416 }
20417}
20418
20419/*
20420 * "winsaveview()" function
20421 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020423f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020424{
20425 dict_T *dict;
20426
Bram Moolenaara800b422010-06-27 01:15:55 +020020427 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020428 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020429 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020430
20431 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20432 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20433#ifdef FEAT_VIRTUALEDIT
20434 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20435#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020436 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020437 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20438
20439 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20440#ifdef FEAT_DIFF
20441 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20442#endif
20443 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20444 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20445}
20446
20447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 * "winwidth(nr)" function
20449 */
20450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020451f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452{
20453 win_T *wp;
20454
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020455 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020457 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 else
20459#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020460 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020462 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463#endif
20464}
20465
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020467 * "wordcount()" function
20468 */
20469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020470f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020471{
20472 if (rettv_dict_alloc(rettv) == FAIL)
20473 return;
20474 cursor_pos_info(rettv->vval.v_dict);
20475}
20476
20477/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020478 * Write list of strings to file
20479 */
20480 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020481write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020482{
20483 listitem_T *li;
20484 int c;
20485 int ret = OK;
20486 char_u *s;
20487
20488 for (li = list->lv_first; li != NULL; li = li->li_next)
20489 {
20490 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20491 {
20492 if (*s == '\n')
20493 c = putc(NUL, fd);
20494 else
20495 c = putc(*s, fd);
20496 if (c == EOF)
20497 {
20498 ret = FAIL;
20499 break;
20500 }
20501 }
20502 if (!binary || li->li_next != NULL)
20503 if (putc('\n', fd) == EOF)
20504 {
20505 ret = FAIL;
20506 break;
20507 }
20508 if (ret == FAIL)
20509 {
20510 EMSG(_(e_write));
20511 break;
20512 }
20513 }
20514 return ret;
20515}
20516
20517/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020518 * "writefile()" function
20519 */
20520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020521f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020522{
20523 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020524 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020525 char_u *fname;
20526 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020527 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020528
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020529 if (check_restricted() || check_secure())
20530 return;
20531
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020532 if (argvars[0].v_type != VAR_LIST)
20533 {
20534 EMSG2(_(e_listarg), "writefile()");
20535 return;
20536 }
20537 if (argvars[0].vval.v_list == NULL)
20538 return;
20539
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020540 if (argvars[2].v_type != VAR_UNKNOWN)
20541 {
20542 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20543 binary = TRUE;
20544 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20545 append = TRUE;
20546 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020547
20548 /* Always open the file in binary mode, library functions have a mind of
20549 * their own about CR-LF conversion. */
20550 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020551 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20552 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020553 {
20554 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20555 ret = -1;
20556 }
20557 else
20558 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020559 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20560 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020561 fclose(fd);
20562 }
20563
20564 rettv->vval.v_number = ret;
20565}
20566
20567/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020568 * "xor(expr, expr)" function
20569 */
20570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020571f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020572{
20573 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20574 ^ get_tv_number_chk(&argvars[1], NULL);
20575}
20576
20577
20578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020580 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581 */
20582 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020583var2fpos(
20584 typval_T *varp,
20585 int dollar_lnum, /* TRUE when $ is last line */
20586 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020588 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020590 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591
Bram Moolenaara5525202006-03-02 22:52:09 +000020592 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020593 if (varp->v_type == VAR_LIST)
20594 {
20595 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020596 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020597 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020598 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020599
20600 l = varp->vval.v_list;
20601 if (l == NULL)
20602 return NULL;
20603
20604 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020605 pos.lnum = list_find_nr(l, 0L, &error);
20606 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020607 return NULL; /* invalid line number */
20608
20609 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020610 pos.col = list_find_nr(l, 1L, &error);
20611 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020612 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020613 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020614
20615 /* We accept "$" for the column number: last column. */
20616 li = list_find(l, 1L);
20617 if (li != NULL && li->li_tv.v_type == VAR_STRING
20618 && li->li_tv.vval.v_string != NULL
20619 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20620 pos.col = len + 1;
20621
Bram Moolenaara5525202006-03-02 22:52:09 +000020622 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020623 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020624 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020625 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020626
Bram Moolenaara5525202006-03-02 22:52:09 +000020627#ifdef FEAT_VIRTUALEDIT
20628 /* Get the virtual offset. Defaults to zero. */
20629 pos.coladd = list_find_nr(l, 2L, &error);
20630 if (error)
20631 pos.coladd = 0;
20632#endif
20633
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020634 return &pos;
20635 }
20636
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020637 name = get_tv_string_chk(varp);
20638 if (name == NULL)
20639 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020640 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020642 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20643 {
20644 if (VIsual_active)
20645 return &VIsual;
20646 return &curwin->w_cursor;
20647 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020648 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020650 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020651 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20652 return NULL;
20653 return pp;
20654 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020655
20656#ifdef FEAT_VIRTUALEDIT
20657 pos.coladd = 0;
20658#endif
20659
Bram Moolenaar477933c2007-07-17 14:32:23 +000020660 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020661 {
20662 pos.col = 0;
20663 if (name[1] == '0') /* "w0": first visible line */
20664 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020665 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020666 pos.lnum = curwin->w_topline;
20667 return &pos;
20668 }
20669 else if (name[1] == '$') /* "w$": last visible line */
20670 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020671 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020672 pos.lnum = curwin->w_botline - 1;
20673 return &pos;
20674 }
20675 }
20676 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020677 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020678 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 {
20680 pos.lnum = curbuf->b_ml.ml_line_count;
20681 pos.col = 0;
20682 }
20683 else
20684 {
20685 pos.lnum = curwin->w_cursor.lnum;
20686 pos.col = (colnr_T)STRLEN(ml_get_curline());
20687 }
20688 return &pos;
20689 }
20690 return NULL;
20691}
20692
20693/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020694 * Convert list in "arg" into a position and optional file number.
20695 * When "fnump" is NULL there is no file number, only 3 items.
20696 * Note that the column is passed on as-is, the caller may want to decrement
20697 * it to use 1 for the first column.
20698 * Return FAIL when conversion is not possible, doesn't check the position for
20699 * validity.
20700 */
20701 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020702list2fpos(
20703 typval_T *arg,
20704 pos_T *posp,
20705 int *fnump,
20706 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020707{
20708 list_T *l = arg->vval.v_list;
20709 long i = 0;
20710 long n;
20711
Bram Moolenaar493c1782014-05-28 14:34:46 +020020712 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20713 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020714 if (arg->v_type != VAR_LIST
20715 || l == NULL
20716 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020717 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020718 return FAIL;
20719
20720 if (fnump != NULL)
20721 {
20722 n = list_find_nr(l, i++, NULL); /* fnum */
20723 if (n < 0)
20724 return FAIL;
20725 if (n == 0)
20726 n = curbuf->b_fnum; /* current buffer */
20727 *fnump = n;
20728 }
20729
20730 n = list_find_nr(l, i++, NULL); /* lnum */
20731 if (n < 0)
20732 return FAIL;
20733 posp->lnum = n;
20734
20735 n = list_find_nr(l, i++, NULL); /* col */
20736 if (n < 0)
20737 return FAIL;
20738 posp->col = n;
20739
20740#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020741 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020742 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020743 posp->coladd = 0;
20744 else
20745 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020746#endif
20747
Bram Moolenaar493c1782014-05-28 14:34:46 +020020748 if (curswantp != NULL)
20749 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20750
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020751 return OK;
20752}
20753
20754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755 * Get the length of an environment variable name.
20756 * Advance "arg" to the first character after the name.
20757 * Return 0 for error.
20758 */
20759 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020760get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761{
20762 char_u *p;
20763 int len;
20764
20765 for (p = *arg; vim_isIDc(*p); ++p)
20766 ;
20767 if (p == *arg) /* no name found */
20768 return 0;
20769
20770 len = (int)(p - *arg);
20771 *arg = p;
20772 return len;
20773}
20774
20775/*
20776 * Get the length of the name of a function or internal variable.
20777 * "arg" is advanced to the first non-white character after the name.
20778 * Return 0 if something is wrong.
20779 */
20780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020781get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782{
20783 char_u *p;
20784 int len;
20785
20786 /* Find the end of the name. */
20787 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020788 {
20789 if (*p == ':')
20790 {
20791 /* "s:" is start of "s:var", but "n:" is not and can be used in
20792 * slice "[n:]". Also "xx:" is not a namespace. */
20793 len = (int)(p - *arg);
20794 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20795 || len > 1)
20796 break;
20797 }
20798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799 if (p == *arg) /* no name found */
20800 return 0;
20801
20802 len = (int)(p - *arg);
20803 *arg = skipwhite(p);
20804
20805 return len;
20806}
20807
20808/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020809 * Get the length of the name of a variable or function.
20810 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020812 * Return -1 if curly braces expansion failed.
20813 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814 * If the name contains 'magic' {}'s, expand them and return the
20815 * expanded name in an allocated string via 'alias' - caller must free.
20816 */
20817 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020818get_name_len(
20819 char_u **arg,
20820 char_u **alias,
20821 int evaluate,
20822 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823{
20824 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825 char_u *p;
20826 char_u *expr_start;
20827 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828
20829 *alias = NULL; /* default to no alias */
20830
20831 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20832 && (*arg)[2] == (int)KE_SNR)
20833 {
20834 /* hard coded <SNR>, already translated */
20835 *arg += 3;
20836 return get_id_len(arg) + 3;
20837 }
20838 len = eval_fname_script(*arg);
20839 if (len > 0)
20840 {
20841 /* literal "<SID>", "s:" or "<SNR>" */
20842 *arg += len;
20843 }
20844
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020846 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020848 p = find_name_end(*arg, &expr_start, &expr_end,
20849 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 if (expr_start != NULL)
20851 {
20852 char_u *temp_string;
20853
20854 if (!evaluate)
20855 {
20856 len += (int)(p - *arg);
20857 *arg = skipwhite(p);
20858 return len;
20859 }
20860
20861 /*
20862 * Include any <SID> etc in the expanded string:
20863 * Thus the -len here.
20864 */
20865 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20866 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020867 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 *alias = temp_string;
20869 *arg = skipwhite(p);
20870 return (int)STRLEN(temp_string);
20871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872
20873 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020874 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 EMSG2(_(e_invexpr2), *arg);
20876
20877 return len;
20878}
20879
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020880/*
20881 * Find the end of a variable or function name, taking care of magic braces.
20882 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20883 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020884 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020885 * Return a pointer to just after the name. Equal to "arg" if there is no
20886 * valid name.
20887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020889find_name_end(
20890 char_u *arg,
20891 char_u **expr_start,
20892 char_u **expr_end,
20893 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020895 int mb_nest = 0;
20896 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020898 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020900 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020902 *expr_start = NULL;
20903 *expr_end = NULL;
20904 }
20905
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020906 /* Quick check for valid starting character. */
20907 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20908 return arg;
20909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020910 for (p = arg; *p != NUL
20911 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020912 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020913 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020914 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020915 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020916 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020917 if (*p == '\'')
20918 {
20919 /* skip over 'string' to avoid counting [ and ] inside it. */
20920 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20921 ;
20922 if (*p == NUL)
20923 break;
20924 }
20925 else if (*p == '"')
20926 {
20927 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20928 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20929 if (*p == '\\' && p[1] != NUL)
20930 ++p;
20931 if (*p == NUL)
20932 break;
20933 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020934 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20935 {
20936 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020937 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020938 len = (int)(p - arg);
20939 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020940 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020941 break;
20942 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020944 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020946 if (*p == '[')
20947 ++br_nest;
20948 else if (*p == ']')
20949 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020952 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020954 if (*p == '{')
20955 {
20956 mb_nest++;
20957 if (expr_start != NULL && *expr_start == NULL)
20958 *expr_start = p;
20959 }
20960 else if (*p == '}')
20961 {
20962 mb_nest--;
20963 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20964 *expr_end = p;
20965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967 }
20968
20969 return p;
20970}
20971
20972/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020973 * Expands out the 'magic' {}'s in a variable/function name.
20974 * Note that this can call itself recursively, to deal with
20975 * constructs like foo{bar}{baz}{bam}
20976 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20977 * "in_start" ^
20978 * "expr_start" ^
20979 * "expr_end" ^
20980 * "in_end" ^
20981 *
20982 * Returns a new allocated string, which the caller must free.
20983 * Returns NULL for failure.
20984 */
20985 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020986make_expanded_name(
20987 char_u *in_start,
20988 char_u *expr_start,
20989 char_u *expr_end,
20990 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020991{
20992 char_u c1;
20993 char_u *retval = NULL;
20994 char_u *temp_result;
20995 char_u *nextcmd = NULL;
20996
20997 if (expr_end == NULL || in_end == NULL)
20998 return NULL;
20999 *expr_start = NUL;
21000 *expr_end = NUL;
21001 c1 = *in_end;
21002 *in_end = NUL;
21003
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021004 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021005 if (temp_result != NULL && nextcmd == NULL)
21006 {
21007 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21008 + (in_end - expr_end) + 1));
21009 if (retval != NULL)
21010 {
21011 STRCPY(retval, in_start);
21012 STRCAT(retval, temp_result);
21013 STRCAT(retval, expr_end + 1);
21014 }
21015 }
21016 vim_free(temp_result);
21017
21018 *in_end = c1; /* put char back for error messages */
21019 *expr_start = '{';
21020 *expr_end = '}';
21021
21022 if (retval != NULL)
21023 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021024 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021025 if (expr_start != NULL)
21026 {
21027 /* Further expansion! */
21028 temp_result = make_expanded_name(retval, expr_start,
21029 expr_end, temp_result);
21030 vim_free(retval);
21031 retval = temp_result;
21032 }
21033 }
21034
21035 return retval;
21036}
21037
21038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021040 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041 */
21042 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021043eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021045 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21046}
21047
21048/*
21049 * Return TRUE if character "c" can be used as the first character in a
21050 * variable or function name (excluding '{' and '}').
21051 */
21052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021053eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021054{
21055 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056}
21057
21058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059 * Set number v: variable to "val".
21060 */
21061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021062set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021064 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065}
21066
21067/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021068 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069 */
21070 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021071get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021073 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074}
21075
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021076/*
21077 * Get string v: variable value. Uses a static buffer, can only be used once.
21078 */
21079 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021080get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021081{
21082 return get_tv_string(&vimvars[idx].vv_tv);
21083}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021084
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021086 * Get List v: variable value. Caller must take care of reference count when
21087 * needed.
21088 */
21089 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021090get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021091{
21092 return vimvars[idx].vv_list;
21093}
21094
21095/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021096 * Set v:char to character "c".
21097 */
21098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021099set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021100{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021101 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021102
21103#ifdef FEAT_MBYTE
21104 if (has_mbyte)
21105 buf[(*mb_char2bytes)(c, buf)] = NUL;
21106 else
21107#endif
21108 {
21109 buf[0] = c;
21110 buf[1] = NUL;
21111 }
21112 set_vim_var_string(VV_CHAR, buf, -1);
21113}
21114
21115/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021116 * Set v:count to "count" and v:count1 to "count1".
21117 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 */
21119 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021120set_vcount(
21121 long count,
21122 long count1,
21123 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021125 if (set_prevcount)
21126 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021127 vimvars[VV_COUNT].vv_nr = count;
21128 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129}
21130
21131/*
21132 * Set string v: variable to a copy of "val".
21133 */
21134 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021135set_vim_var_string(
21136 int idx,
21137 char_u *val,
21138 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139{
Bram Moolenaara542c682016-01-31 16:28:04 +010021140 clear_tv(&vimvars[idx].vv_di.di_tv);
21141 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021143 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021145 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021147 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148}
21149
21150/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021151 * Set List v: variable to "val".
21152 */
21153 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021154set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021155{
Bram Moolenaara542c682016-01-31 16:28:04 +010021156 clear_tv(&vimvars[idx].vv_di.di_tv);
21157 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021158 vimvars[idx].vv_list = val;
21159 if (val != NULL)
21160 ++val->lv_refcount;
21161}
21162
21163/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021164 * Set Dictionary v: variable to "val".
21165 */
21166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021167set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021168{
21169 int todo;
21170 hashitem_T *hi;
21171
Bram Moolenaara542c682016-01-31 16:28:04 +010021172 clear_tv(&vimvars[idx].vv_di.di_tv);
21173 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021174 vimvars[idx].vv_dict = val;
21175 if (val != NULL)
21176 {
21177 ++val->dv_refcount;
21178
21179 /* Set readonly */
21180 todo = (int)val->dv_hashtab.ht_used;
21181 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21182 {
21183 if (HASHITEM_EMPTY(hi))
21184 continue;
21185 --todo;
21186 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21187 }
21188 }
21189}
21190
21191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 * Set v:register if needed.
21193 */
21194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021195set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196{
21197 char_u regname;
21198
21199 if (c == 0 || c == ' ')
21200 regname = '"';
21201 else
21202 regname = c;
21203 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021204 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205 set_vim_var_string(VV_REG, &regname, 1);
21206}
21207
21208/*
21209 * Get or set v:exception. If "oldval" == NULL, return the current value.
21210 * Otherwise, restore the value to "oldval" and return NULL.
21211 * Must always be called in pairs to save and restore v:exception! Does not
21212 * take care of memory allocations.
21213 */
21214 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021215v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216{
21217 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021218 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219
Bram Moolenaare9a41262005-01-15 22:18:47 +000021220 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 return NULL;
21222}
21223
21224/*
21225 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21226 * Otherwise, restore the value to "oldval" and return NULL.
21227 * Must always be called in pairs to save and restore v:throwpoint! Does not
21228 * take care of memory allocations.
21229 */
21230 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021231v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232{
21233 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021234 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235
Bram Moolenaare9a41262005-01-15 22:18:47 +000021236 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 return NULL;
21238}
21239
21240#if defined(FEAT_AUTOCMD) || defined(PROTO)
21241/*
21242 * Set v:cmdarg.
21243 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21244 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21245 * Must always be called in pairs!
21246 */
21247 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021248set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249{
21250 char_u *oldval;
21251 char_u *newval;
21252 unsigned len;
21253
Bram Moolenaare9a41262005-01-15 22:18:47 +000021254 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021255 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021257 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021258 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021259 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 }
21261
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021262 if (eap->force_bin == FORCE_BIN)
21263 len = 6;
21264 else if (eap->force_bin == FORCE_NOBIN)
21265 len = 8;
21266 else
21267 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021268
21269 if (eap->read_edit)
21270 len += 7;
21271
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021272 if (eap->force_ff != 0)
21273 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21274# ifdef FEAT_MBYTE
21275 if (eap->force_enc != 0)
21276 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021277 if (eap->bad_char != 0)
21278 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021279# endif
21280
21281 newval = alloc(len + 1);
21282 if (newval == NULL)
21283 return NULL;
21284
21285 if (eap->force_bin == FORCE_BIN)
21286 sprintf((char *)newval, " ++bin");
21287 else if (eap->force_bin == FORCE_NOBIN)
21288 sprintf((char *)newval, " ++nobin");
21289 else
21290 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021291
21292 if (eap->read_edit)
21293 STRCAT(newval, " ++edit");
21294
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021295 if (eap->force_ff != 0)
21296 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21297 eap->cmd + eap->force_ff);
21298# ifdef FEAT_MBYTE
21299 if (eap->force_enc != 0)
21300 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21301 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021302 if (eap->bad_char == BAD_KEEP)
21303 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21304 else if (eap->bad_char == BAD_DROP)
21305 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21306 else if (eap->bad_char != 0)
21307 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021308# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021309 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021310 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311}
21312#endif
21313
21314/*
21315 * Get the value of internal variable "name".
21316 * Return OK or FAIL.
21317 */
21318 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021319get_var_tv(
21320 char_u *name,
21321 int len, /* length of "name" */
21322 typval_T *rettv, /* NULL when only checking existence */
21323 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21324 int verbose, /* may give error message */
21325 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326{
21327 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021328 typval_T *tv = NULL;
21329 typval_T atv;
21330 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332
21333 /* truncate the name, so that we can use strcmp() */
21334 cc = name[len];
21335 name[len] = NUL;
21336
21337 /*
21338 * Check for "b:changedtick".
21339 */
21340 if (STRCMP(name, "b:changedtick") == 0)
21341 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021342 atv.v_type = VAR_NUMBER;
21343 atv.vval.v_number = curbuf->b_changedtick;
21344 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 }
21346
21347 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348 * Check for user-defined variables.
21349 */
21350 else
21351 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021352 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021354 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021355 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021356 if (dip != NULL)
21357 *dip = v;
21358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 }
21360
Bram Moolenaare9a41262005-01-15 22:18:47 +000021361 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021363 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021364 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 ret = FAIL;
21366 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021367 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021368 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369
21370 name[len] = cc;
21371
21372 return ret;
21373}
21374
21375/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021376 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21377 * Also handle function call with Funcref variable: func(expr)
21378 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21379 */
21380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021381handle_subscript(
21382 char_u **arg,
21383 typval_T *rettv,
21384 int evaluate, /* do more than finding the end */
21385 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021386{
21387 int ret = OK;
21388 dict_T *selfdict = NULL;
21389 char_u *s;
21390 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021391 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021392
21393 while (ret == OK
21394 && (**arg == '['
21395 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021396 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021397 && !vim_iswhite(*(*arg - 1)))
21398 {
21399 if (**arg == '(')
21400 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021401 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021402 if (evaluate)
21403 {
21404 functv = *rettv;
21405 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021406
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021407 /* Invoke the function. Recursive! */
21408 s = functv.vval.v_string;
21409 }
21410 else
21411 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021412 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021413 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21414 &len, evaluate, selfdict);
21415
21416 /* Clear the funcref afterwards, so that deleting it while
21417 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021418 if (evaluate)
21419 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021420
21421 /* Stop the expression evaluation when immediately aborting on
21422 * error, or when an interrupt occurred or an exception was thrown
21423 * but not caught. */
21424 if (aborting())
21425 {
21426 if (ret == OK)
21427 clear_tv(rettv);
21428 ret = FAIL;
21429 }
21430 dict_unref(selfdict);
21431 selfdict = NULL;
21432 }
21433 else /* **arg == '[' || **arg == '.' */
21434 {
21435 dict_unref(selfdict);
21436 if (rettv->v_type == VAR_DICT)
21437 {
21438 selfdict = rettv->vval.v_dict;
21439 if (selfdict != NULL)
21440 ++selfdict->dv_refcount;
21441 }
21442 else
21443 selfdict = NULL;
21444 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21445 {
21446 clear_tv(rettv);
21447 ret = FAIL;
21448 }
21449 }
21450 }
21451 dict_unref(selfdict);
21452 return ret;
21453}
21454
21455/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021456 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021457 * value).
21458 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021459 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021460alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021461{
Bram Moolenaar33570922005-01-25 22:26:29 +000021462 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021463}
21464
21465/*
21466 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467 * The string "s" must have been allocated, it is consumed.
21468 * Return NULL for out of memory, the variable otherwise.
21469 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021470 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021471alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472{
Bram Moolenaar33570922005-01-25 22:26:29 +000021473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021475 rettv = alloc_tv();
21476 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021478 rettv->v_type = VAR_STRING;
21479 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 }
21481 else
21482 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021483 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021484}
21485
21486/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021487 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021489 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021490free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491{
21492 if (varp != NULL)
21493 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021494 switch (varp->v_type)
21495 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021496 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021497 func_unref(varp->vval.v_string);
21498 /*FALLTHROUGH*/
21499 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021500 vim_free(varp->vval.v_string);
21501 break;
21502 case VAR_LIST:
21503 list_unref(varp->vval.v_list);
21504 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021505 case VAR_DICT:
21506 dict_unref(varp->vval.v_dict);
21507 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021508 case VAR_JOB:
21509#ifdef FEAT_JOB
21510 job_unref(varp->vval.v_job);
21511 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021512#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021513 case VAR_CHANNEL:
21514#ifdef FEAT_CHANNEL
21515 channel_unref(varp->vval.v_channel);
21516 break;
21517#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021518 case VAR_NUMBER:
21519 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021520 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021521 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021522 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 vim_free(varp);
21525 }
21526}
21527
21528/*
21529 * Free the memory for a variable value and set the value to NULL or 0.
21530 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021532clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533{
21534 if (varp != NULL)
21535 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021536 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021538 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021539 func_unref(varp->vval.v_string);
21540 /*FALLTHROUGH*/
21541 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021542 vim_free(varp->vval.v_string);
21543 varp->vval.v_string = NULL;
21544 break;
21545 case VAR_LIST:
21546 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021547 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021548 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021549 case VAR_DICT:
21550 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021551 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021552 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021553 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021554 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021555 varp->vval.v_number = 0;
21556 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021557 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021558#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021559 varp->vval.v_float = 0.0;
21560 break;
21561#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021562 case VAR_JOB:
21563#ifdef FEAT_JOB
21564 job_unref(varp->vval.v_job);
21565 varp->vval.v_job = NULL;
21566#endif
21567 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021568 case VAR_CHANNEL:
21569#ifdef FEAT_CHANNEL
21570 channel_unref(varp->vval.v_channel);
21571 varp->vval.v_channel = NULL;
21572#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021573 case VAR_UNKNOWN:
21574 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021576 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 }
21578}
21579
21580/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021581 * Set the value of a variable to NULL without freeing items.
21582 */
21583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021584init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021585{
21586 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021587 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021588}
21589
21590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 * Get the number value of a variable.
21592 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021593 * For incompatible types, return 0.
21594 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21595 * caller of incompatible types: it sets *denote to TRUE if "denote"
21596 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597 */
21598 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021599get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021601 int error = FALSE;
21602
21603 return get_tv_number_chk(varp, &error); /* return 0L on error */
21604}
21605
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021606 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021607get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021608{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021609 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021611 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021613 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021614 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021615 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021616#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021617 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021618 break;
21619#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021620 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021621 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021622 break;
21623 case VAR_STRING:
21624 if (varp->vval.v_string != NULL)
21625 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021626 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021627 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021628 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021629 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021630 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021631 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021632 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021633 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021634 case VAR_SPECIAL:
21635 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21636 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021637 case VAR_JOB:
21638#ifdef FEAT_JOB
21639 EMSG(_("E910: Using a Job as a Number"));
21640 break;
21641#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021642 case VAR_CHANNEL:
21643#ifdef FEAT_CHANNEL
21644 EMSG(_("E913: Using a Channel as a Number"));
21645 break;
21646#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021647 case VAR_UNKNOWN:
21648 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021649 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021651 if (denote == NULL) /* useful for values that must be unsigned */
21652 n = -1;
21653 else
21654 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021655 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656}
21657
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021658#ifdef FEAT_FLOAT
21659 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021660get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021661{
21662 switch (varp->v_type)
21663 {
21664 case VAR_NUMBER:
21665 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021666 case VAR_FLOAT:
21667 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021668 case VAR_FUNC:
21669 EMSG(_("E891: Using a Funcref as a Float"));
21670 break;
21671 case VAR_STRING:
21672 EMSG(_("E892: Using a String as a Float"));
21673 break;
21674 case VAR_LIST:
21675 EMSG(_("E893: Using a List as a Float"));
21676 break;
21677 case VAR_DICT:
21678 EMSG(_("E894: Using a Dictionary as a Float"));
21679 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021680 case VAR_SPECIAL:
21681 EMSG(_("E907: Using a special value as a Float"));
21682 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021683 case VAR_JOB:
21684# ifdef FEAT_JOB
21685 EMSG(_("E911: Using a Job as a Float"));
21686 break;
21687# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021688 case VAR_CHANNEL:
21689# ifdef FEAT_CHANNEL
21690 EMSG(_("E914: Using a Channel as a Float"));
21691 break;
21692# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021693 case VAR_UNKNOWN:
21694 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021695 break;
21696 }
21697 return 0;
21698}
21699#endif
21700
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021702 * Get the lnum from the first argument.
21703 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021704 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705 */
21706 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021707get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708{
Bram Moolenaar33570922005-01-25 22:26:29 +000021709 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 linenr_T lnum;
21711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021712 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 if (lnum == 0) /* no valid number, try using line() */
21714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021715 rettv.v_type = VAR_NUMBER;
21716 f_line(argvars, &rettv);
21717 lnum = rettv.vval.v_number;
21718 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719 }
21720 return lnum;
21721}
21722
21723/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021724 * Get the lnum from the first argument.
21725 * Also accepts "$", then "buf" is used.
21726 * Returns 0 on error.
21727 */
21728 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021729get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021730{
21731 if (argvars[0].v_type == VAR_STRING
21732 && argvars[0].vval.v_string != NULL
21733 && argvars[0].vval.v_string[0] == '$'
21734 && buf != NULL)
21735 return buf->b_ml.ml_line_count;
21736 return get_tv_number_chk(&argvars[0], NULL);
21737}
21738
21739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 * Get the string value of a variable.
21741 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021742 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21743 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744 * If the String variable has never been set, return an empty string.
21745 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021746 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21747 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748 */
21749 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021750get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751{
21752 static char_u mybuf[NUMBUFLEN];
21753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021754 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755}
21756
21757 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021758get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021760 char_u *res = get_tv_string_buf_chk(varp, buf);
21761
21762 return res != NULL ? res : (char_u *)"";
21763}
21764
Bram Moolenaar7d647822014-04-05 21:28:56 +020021765/*
21766 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21767 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021768 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021769get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021770{
21771 static char_u mybuf[NUMBUFLEN];
21772
21773 return get_tv_string_buf_chk(varp, mybuf);
21774}
21775
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021776 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021777get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021778{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021779 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021781 case VAR_NUMBER:
21782 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21783 return buf;
21784 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021785 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021786 break;
21787 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021788 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021789 break;
21790 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021791 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021792 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021793 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021794#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021795 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021796 break;
21797#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021798 case VAR_STRING:
21799 if (varp->vval.v_string != NULL)
21800 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021801 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021802 case VAR_SPECIAL:
21803 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21804 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021805 case VAR_JOB:
21806#ifdef FEAT_JOB
21807 {
21808 job_T *job = varp->vval.v_job;
21809 char *status = job->jv_status == JOB_FAILED ? "fail"
21810 : job->jv_status == JOB_ENDED ? "dead"
21811 : "run";
21812# ifdef UNIX
21813 vim_snprintf((char *)buf, NUMBUFLEN,
21814 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021815# elif defined(WIN32)
21816 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021817 "process %ld %s",
21818 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021819 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021820# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021821 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021822 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21823# endif
21824 return buf;
21825 }
21826#endif
21827 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021828 case VAR_CHANNEL:
21829#ifdef FEAT_CHANNEL
21830 {
21831 channel_T *channel = varp->vval.v_channel;
21832 char *status = channel_status(channel);
21833
Bram Moolenaar5cefd402016-02-16 12:44:26 +010021834 if (channel == NULL)
21835 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
21836 else
21837 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010021838 "channel %d %s", channel->ch_id, status);
21839 return buf;
21840 }
21841#endif
21842 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021843 case VAR_UNKNOWN:
21844 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021845 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021847 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848}
21849
21850/*
21851 * Find variable "name" in the list of variables.
21852 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021853 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021854 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021855 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021857 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021858find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021861 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862
Bram Moolenaara7043832005-01-21 11:56:39 +000021863 ht = find_var_ht(name, &varname);
21864 if (htp != NULL)
21865 *htp = ht;
21866 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021868 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869}
21870
21871/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021872 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021873 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021875 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021876find_var_in_ht(
21877 hashtab_T *ht,
21878 int htname,
21879 char_u *varname,
21880 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021881{
Bram Moolenaar33570922005-01-25 22:26:29 +000021882 hashitem_T *hi;
21883
21884 if (*varname == NUL)
21885 {
21886 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021887 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021888 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021889 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021890 case 'g': return &globvars_var;
21891 case 'v': return &vimvars_var;
21892 case 'b': return &curbuf->b_bufvar;
21893 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021894#ifdef FEAT_WINDOWS
21895 case 't': return &curtab->tp_winvar;
21896#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021897 case 'l': return current_funccal == NULL
21898 ? NULL : &current_funccal->l_vars_var;
21899 case 'a': return current_funccal == NULL
21900 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021901 }
21902 return NULL;
21903 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021904
21905 hi = hash_find(ht, varname);
21906 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021907 {
21908 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021909 * worked find the variable again. Don't auto-load a script if it was
21910 * loaded already, otherwise it would be loaded every time when
21911 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021912 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021913 {
21914 /* Note: script_autoload() may make "hi" invalid. It must either
21915 * be obtained again or not used. */
21916 if (!script_autoload(varname, FALSE) || aborting())
21917 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021918 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021919 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021920 if (HASHITEM_EMPTY(hi))
21921 return NULL;
21922 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021923 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021924}
21925
21926/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021927 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021928 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021929 * Set "varname" to the start of name without ':'.
21930 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021931 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021932find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021934 hashitem_T *hi;
21935
Bram Moolenaar73627d02015-08-11 15:46:09 +020021936 if (name[0] == NUL)
21937 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 if (name[1] != ':')
21939 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021940 /* The name must not start with a colon or #. */
21941 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942 return NULL;
21943 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021944
21945 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021946 hi = hash_find(&compat_hashtab, name);
21947 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021948 return &compat_hashtab;
21949
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021951 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021952 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953 }
21954 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021955 if (*name == 'g') /* global variable */
21956 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021957 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21958 */
21959 if (vim_strchr(name + 2, ':') != NULL
21960 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021961 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021963 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021965 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021966#ifdef FEAT_WINDOWS
21967 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021968 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021969#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021970 if (*name == 'v') /* v: variable */
21971 return &vimvarht;
21972 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021973 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021974 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021975 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976 if (*name == 's' /* script variable */
21977 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21978 return &SCRIPT_VARS(current_SID);
21979 return NULL;
21980}
21981
21982/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021983 * Get function call environment based on bactrace debug level
21984 */
21985 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021986get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021987{
21988 int i;
21989 funccall_T *funccal;
21990 funccall_T *temp_funccal;
21991
21992 funccal = current_funccal;
21993 if (debug_backtrace_level > 0)
21994 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021995 for (i = 0; i < debug_backtrace_level; i++)
21996 {
21997 temp_funccal = funccal->caller;
21998 if (temp_funccal)
21999 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022000 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022001 /* backtrace level overflow. reset to max */
22002 debug_backtrace_level = i;
22003 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022004 }
22005 return funccal;
22006}
22007
22008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022010 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 * Returns NULL when it doesn't exist.
22012 */
22013 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022014get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022015{
Bram Moolenaar33570922005-01-25 22:26:29 +000022016 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022018 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 if (v == NULL)
22020 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022021 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022}
22023
22024/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022025 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 * sourcing this script and when executing functions defined in the script.
22027 */
22028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022029new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030{
Bram Moolenaara7043832005-01-21 11:56:39 +000022031 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022032 hashtab_T *ht;
22033 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022034
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22036 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022037 /* Re-allocating ga_data means that an ht_array pointing to
22038 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022039 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022040 for (i = 1; i <= ga_scripts.ga_len; ++i)
22041 {
22042 ht = &SCRIPT_VARS(i);
22043 if (ht->ht_mask == HT_INIT_SIZE - 1)
22044 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022045 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022046 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022047 }
22048
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 while (ga_scripts.ga_len < id)
22050 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022051 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022052 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022053 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055 }
22056 }
22057}
22058
22059/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022060 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22061 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062 */
22063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022064init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065{
Bram Moolenaar33570922005-01-25 22:26:29 +000022066 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022067 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022068 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022069 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022070 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022071 dict_var->di_tv.vval.v_dict = dict;
22072 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022073 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022074 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22075 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076}
22077
22078/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022079 * Unreference a dictionary initialized by init_var_dict().
22080 */
22081 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022082unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022083{
22084 /* Now the dict needs to be freed if no one else is using it, go back to
22085 * normal reference counting. */
22086 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22087 dict_unref(dict);
22088}
22089
22090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022092 * Frees all allocated variables and the value they contain.
22093 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 */
22095 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022096vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022097{
22098 vars_clear_ext(ht, TRUE);
22099}
22100
22101/*
22102 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22103 */
22104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022105vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106{
Bram Moolenaara7043832005-01-21 11:56:39 +000022107 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022108 hashitem_T *hi;
22109 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110
Bram Moolenaar33570922005-01-25 22:26:29 +000022111 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022112 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022113 for (hi = ht->ht_array; todo > 0; ++hi)
22114 {
22115 if (!HASHITEM_EMPTY(hi))
22116 {
22117 --todo;
22118
Bram Moolenaar33570922005-01-25 22:26:29 +000022119 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022120 * ht_array might change then. hash_clear() takes care of it
22121 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022122 v = HI2DI(hi);
22123 if (free_val)
22124 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022125 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022126 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022127 }
22128 }
22129 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022130 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131}
22132
Bram Moolenaara7043832005-01-21 11:56:39 +000022133/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022134 * Delete a variable from hashtab "ht" at item "hi".
22135 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022138delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139{
Bram Moolenaar33570922005-01-25 22:26:29 +000022140 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022141
22142 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022143 clear_tv(&di->di_tv);
22144 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145}
22146
22147/*
22148 * List the value of one internal variable.
22149 */
22150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022151list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022153 char_u *tofree;
22154 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022155 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022156
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022157 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022158 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022159 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022160 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161}
22162
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022164list_one_var_a(
22165 char_u *prefix,
22166 char_u *name,
22167 int type,
22168 char_u *string,
22169 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170{
Bram Moolenaar31859182007-08-14 20:41:13 +000022171 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22172 msg_start();
22173 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 if (name != NULL) /* "a:" vars don't have a name stored */
22175 msg_puts(name);
22176 msg_putchar(' ');
22177 msg_advance(22);
22178 if (type == VAR_NUMBER)
22179 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022180 else if (type == VAR_FUNC)
22181 msg_putchar('*');
22182 else if (type == VAR_LIST)
22183 {
22184 msg_putchar('[');
22185 if (*string == '[')
22186 ++string;
22187 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022188 else if (type == VAR_DICT)
22189 {
22190 msg_putchar('{');
22191 if (*string == '{')
22192 ++string;
22193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194 else
22195 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022196
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022198
22199 if (type == VAR_FUNC)
22200 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022201 if (*first)
22202 {
22203 msg_clr_eos();
22204 *first = FALSE;
22205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206}
22207
22208/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022209 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 * If the variable already exists, the value is updated.
22211 * Otherwise the variable is created.
22212 */
22213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022214set_var(
22215 char_u *name,
22216 typval_T *tv,
22217 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218{
Bram Moolenaar33570922005-01-25 22:26:29 +000022219 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022221 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022223 ht = find_var_ht(name, &varname);
22224 if (ht == NULL || *varname == NUL)
22225 {
22226 EMSG2(_(e_illvar), name);
22227 return;
22228 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022229 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022230
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022231 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22232 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022233
Bram Moolenaar33570922005-01-25 22:26:29 +000022234 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022236 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022237 if (var_check_ro(v->di_flags, name, FALSE)
22238 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022239 return;
22240 if (v->di_tv.v_type != tv->v_type
22241 && !((v->di_tv.v_type == VAR_STRING
22242 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022243 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022244 || tv->v_type == VAR_NUMBER))
22245#ifdef FEAT_FLOAT
22246 && !((v->di_tv.v_type == VAR_NUMBER
22247 || v->di_tv.v_type == VAR_FLOAT)
22248 && (tv->v_type == VAR_NUMBER
22249 || tv->v_type == VAR_FLOAT))
22250#endif
22251 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022252 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022253 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022254 return;
22255 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022256
22257 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022258 * Handle setting internal v: variables separately where needed to
22259 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022260 */
22261 if (ht == &vimvarht)
22262 {
22263 if (v->di_tv.v_type == VAR_STRING)
22264 {
22265 vim_free(v->di_tv.vval.v_string);
22266 if (copy || tv->v_type != VAR_STRING)
22267 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22268 else
22269 {
22270 /* Take over the string to avoid an extra alloc/free. */
22271 v->di_tv.vval.v_string = tv->vval.v_string;
22272 tv->vval.v_string = NULL;
22273 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022274 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022275 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022276 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022277 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022278 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022279 if (STRCMP(varname, "searchforward") == 0)
22280 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022281#ifdef FEAT_SEARCH_EXTRA
22282 else if (STRCMP(varname, "hlsearch") == 0)
22283 {
22284 no_hlsearch = !v->di_tv.vval.v_number;
22285 redraw_all_later(SOME_VALID);
22286 }
22287#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022288 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022289 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022290 else if (v->di_tv.v_type != tv->v_type)
22291 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022292 }
22293
22294 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 }
22296 else /* add a new variable */
22297 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022298 /* Can't add "v:" variable. */
22299 if (ht == &vimvarht)
22300 {
22301 EMSG2(_(e_illvar), name);
22302 return;
22303 }
22304
Bram Moolenaar92124a32005-06-17 22:03:40 +000022305 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022306 if (!valid_varname(varname))
22307 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022308
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022309 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22310 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022311 if (v == NULL)
22312 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022313 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022314 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022316 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 return;
22318 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022319 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022321
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022322 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022323 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022324 else
22325 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022326 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022327 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022328 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330}
22331
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022332/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022333 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022334 * Also give an error message.
22335 */
22336 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022337var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022338{
22339 if (flags & DI_FLAGS_RO)
22340 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022341 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022342 return TRUE;
22343 }
22344 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22345 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022346 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022347 return TRUE;
22348 }
22349 return FALSE;
22350}
22351
22352/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022353 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22354 * Also give an error message.
22355 */
22356 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022357var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022358{
22359 if (flags & DI_FLAGS_FIX)
22360 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022361 EMSG2(_("E795: Cannot delete variable %s"),
22362 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022363 return TRUE;
22364 }
22365 return FALSE;
22366}
22367
22368/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022369 * Check if a funcref is assigned to a valid variable name.
22370 * Return TRUE and give an error if not.
22371 */
22372 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022373var_check_func_name(
22374 char_u *name, /* points to start of variable name */
22375 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022376{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022377 /* Allow for w: b: s: and t:. */
22378 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022379 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22380 ? name[2] : name[0]))
22381 {
22382 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22383 name);
22384 return TRUE;
22385 }
22386 /* Don't allow hiding a function. When "v" is not NULL we might be
22387 * assigning another function to the same var, the type is checked
22388 * below. */
22389 if (new_var && function_exists(name))
22390 {
22391 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22392 name);
22393 return TRUE;
22394 }
22395 return FALSE;
22396}
22397
22398/*
22399 * Check if a variable name is valid.
22400 * Return FALSE and give an error if not.
22401 */
22402 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022403valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022404{
22405 char_u *p;
22406
22407 for (p = varname; *p != NUL; ++p)
22408 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22409 && *p != AUTOLOAD_CHAR)
22410 {
22411 EMSG2(_(e_illvar), varname);
22412 return FALSE;
22413 }
22414 return TRUE;
22415}
22416
22417/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022418 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022419 * Also give an error message, using "name" or _("name") when use_gettext is
22420 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022421 */
22422 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022423tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022424{
22425 if (lock & VAR_LOCKED)
22426 {
22427 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022428 name == NULL ? (char_u *)_("Unknown")
22429 : use_gettext ? (char_u *)_(name)
22430 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022431 return TRUE;
22432 }
22433 if (lock & VAR_FIXED)
22434 {
22435 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022436 name == NULL ? (char_u *)_("Unknown")
22437 : use_gettext ? (char_u *)_(name)
22438 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022439 return TRUE;
22440 }
22441 return FALSE;
22442}
22443
22444/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022445 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022446 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022447 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022448 * It is OK for "from" and "to" to point to the same item. This is used to
22449 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022450 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022451 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022452copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022454 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022455 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022456 switch (from->v_type)
22457 {
22458 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022459 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022460 to->vval.v_number = from->vval.v_number;
22461 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022462 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022463#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022464 to->vval.v_float = from->vval.v_float;
22465 break;
22466#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022467 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022468#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022469 to->vval.v_job = from->vval.v_job;
22470 ++to->vval.v_job->jv_refcount;
22471 break;
22472#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022473 case VAR_CHANNEL:
22474#ifdef FEAT_CHANNEL
22475 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022476 if (to->vval.v_channel != NULL)
22477 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022478 break;
22479#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022480 case VAR_STRING:
22481 case VAR_FUNC:
22482 if (from->vval.v_string == NULL)
22483 to->vval.v_string = NULL;
22484 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022485 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022486 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022487 if (from->v_type == VAR_FUNC)
22488 func_ref(to->vval.v_string);
22489 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022490 break;
22491 case VAR_LIST:
22492 if (from->vval.v_list == NULL)
22493 to->vval.v_list = NULL;
22494 else
22495 {
22496 to->vval.v_list = from->vval.v_list;
22497 ++to->vval.v_list->lv_refcount;
22498 }
22499 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022500 case VAR_DICT:
22501 if (from->vval.v_dict == NULL)
22502 to->vval.v_dict = NULL;
22503 else
22504 {
22505 to->vval.v_dict = from->vval.v_dict;
22506 ++to->vval.v_dict->dv_refcount;
22507 }
22508 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022509 case VAR_UNKNOWN:
22510 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022511 break;
22512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513}
22514
22515/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022516 * Make a copy of an item.
22517 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022518 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22519 * reference to an already copied list/dict can be used.
22520 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022521 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022522 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022523item_copy(
22524 typval_T *from,
22525 typval_T *to,
22526 int deep,
22527 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022528{
22529 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022530 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022531
Bram Moolenaar33570922005-01-25 22:26:29 +000022532 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022533 {
22534 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022535 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022536 }
22537 ++recurse;
22538
22539 switch (from->v_type)
22540 {
22541 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022542 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022543 case VAR_STRING:
22544 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022545 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022546 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022547 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022548 copy_tv(from, to);
22549 break;
22550 case VAR_LIST:
22551 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022552 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022553 if (from->vval.v_list == NULL)
22554 to->vval.v_list = NULL;
22555 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22556 {
22557 /* use the copy made earlier */
22558 to->vval.v_list = from->vval.v_list->lv_copylist;
22559 ++to->vval.v_list->lv_refcount;
22560 }
22561 else
22562 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22563 if (to->vval.v_list == NULL)
22564 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022565 break;
22566 case VAR_DICT:
22567 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022568 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022569 if (from->vval.v_dict == NULL)
22570 to->vval.v_dict = NULL;
22571 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22572 {
22573 /* use the copy made earlier */
22574 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22575 ++to->vval.v_dict->dv_refcount;
22576 }
22577 else
22578 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22579 if (to->vval.v_dict == NULL)
22580 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022581 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022582 case VAR_UNKNOWN:
22583 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022584 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022585 }
22586 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022587 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022588}
22589
22590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591 * ":echo expr1 ..." print each argument separated with a space, add a
22592 * newline at the end.
22593 * ":echon expr1 ..." print each argument plain.
22594 */
22595 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022596ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597{
22598 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022599 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022600 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 char_u *p;
22602 int needclr = TRUE;
22603 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022604 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605
22606 if (eap->skip)
22607 ++emsg_skip;
22608 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22609 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022610 /* If eval1() causes an error message the text from the command may
22611 * still need to be cleared. E.g., "echo 22,44". */
22612 need_clr_eos = needclr;
22613
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022615 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 {
22617 /*
22618 * Report the invalid expression unless the expression evaluation
22619 * has been cancelled due to an aborting error, an interrupt, or an
22620 * exception.
22621 */
22622 if (!aborting())
22623 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022624 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625 break;
22626 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022627 need_clr_eos = FALSE;
22628
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 if (!eap->skip)
22630 {
22631 if (atstart)
22632 {
22633 atstart = FALSE;
22634 /* Call msg_start() after eval1(), evaluating the expression
22635 * may cause a message to appear. */
22636 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022637 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022638 /* Mark the saved text as finishing the line, so that what
22639 * follows is displayed on a new line when scrolling back
22640 * at the more prompt. */
22641 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022642 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 }
22645 else if (eap->cmdidx == CMD_echo)
22646 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022647 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022648 if (p != NULL)
22649 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022651 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022653 if (*p != TAB && needclr)
22654 {
22655 /* remove any text still there from the command */
22656 msg_clr_eos();
22657 needclr = FALSE;
22658 }
22659 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 }
22661 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022662 {
22663#ifdef FEAT_MBYTE
22664 if (has_mbyte)
22665 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022666 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022667
22668 (void)msg_outtrans_len_attr(p, i, echo_attr);
22669 p += i - 1;
22670 }
22671 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022673 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022676 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022678 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022679 arg = skipwhite(arg);
22680 }
22681 eap->nextcmd = check_nextcmd(arg);
22682
22683 if (eap->skip)
22684 --emsg_skip;
22685 else
22686 {
22687 /* remove text that may still be there from the command */
22688 if (needclr)
22689 msg_clr_eos();
22690 if (eap->cmdidx == CMD_echo)
22691 msg_end();
22692 }
22693}
22694
22695/*
22696 * ":echohl {name}".
22697 */
22698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022699ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700{
22701 int id;
22702
22703 id = syn_name2id(eap->arg);
22704 if (id == 0)
22705 echo_attr = 0;
22706 else
22707 echo_attr = syn_id2attr(id);
22708}
22709
22710/*
22711 * ":execute expr1 ..." execute the result of an expression.
22712 * ":echomsg expr1 ..." Print a message
22713 * ":echoerr expr1 ..." Print an error
22714 * Each gets spaces around each argument and a newline at the end for
22715 * echo commands
22716 */
22717 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022718ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719{
22720 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022721 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 int ret = OK;
22723 char_u *p;
22724 garray_T ga;
22725 int len;
22726 int save_did_emsg;
22727
22728 ga_init2(&ga, 1, 80);
22729
22730 if (eap->skip)
22731 ++emsg_skip;
22732 while (*arg != NUL && *arg != '|' && *arg != '\n')
22733 {
22734 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022735 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736 {
22737 /*
22738 * Report the invalid expression unless the expression evaluation
22739 * has been cancelled due to an aborting error, an interrupt, or an
22740 * exception.
22741 */
22742 if (!aborting())
22743 EMSG2(_(e_invexpr2), p);
22744 ret = FAIL;
22745 break;
22746 }
22747
22748 if (!eap->skip)
22749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022750 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 len = (int)STRLEN(p);
22752 if (ga_grow(&ga, len + 2) == FAIL)
22753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022754 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 ret = FAIL;
22756 break;
22757 }
22758 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761 ga.ga_len += len;
22762 }
22763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022764 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765 arg = skipwhite(arg);
22766 }
22767
22768 if (ret != FAIL && ga.ga_data != NULL)
22769 {
22770 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022771 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022773 out_flush();
22774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 else if (eap->cmdidx == CMD_echoerr)
22776 {
22777 /* We don't want to abort following commands, restore did_emsg. */
22778 save_did_emsg = did_emsg;
22779 EMSG((char_u *)ga.ga_data);
22780 if (!force_abort)
22781 did_emsg = save_did_emsg;
22782 }
22783 else if (eap->cmdidx == CMD_execute)
22784 do_cmdline((char_u *)ga.ga_data,
22785 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22786 }
22787
22788 ga_clear(&ga);
22789
22790 if (eap->skip)
22791 --emsg_skip;
22792
22793 eap->nextcmd = check_nextcmd(arg);
22794}
22795
22796/*
22797 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22798 * "arg" points to the "&" or '+' when called, to "option" when returning.
22799 * Returns NULL when no option name found. Otherwise pointer to the char
22800 * after the option name.
22801 */
22802 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022803find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804{
22805 char_u *p = *arg;
22806
22807 ++p;
22808 if (*p == 'g' && p[1] == ':')
22809 {
22810 *opt_flags = OPT_GLOBAL;
22811 p += 2;
22812 }
22813 else if (*p == 'l' && p[1] == ':')
22814 {
22815 *opt_flags = OPT_LOCAL;
22816 p += 2;
22817 }
22818 else
22819 *opt_flags = 0;
22820
22821 if (!ASCII_ISALPHA(*p))
22822 return NULL;
22823 *arg = p;
22824
22825 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22826 p += 4; /* termcap option */
22827 else
22828 while (ASCII_ISALPHA(*p))
22829 ++p;
22830 return p;
22831}
22832
22833/*
22834 * ":function"
22835 */
22836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022837ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838{
22839 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022840 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 int j;
22842 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022844 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845 char_u *name = NULL;
22846 char_u *p;
22847 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022848 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 garray_T newargs;
22850 garray_T newlines;
22851 int varargs = FALSE;
22852 int mustend = FALSE;
22853 int flags = 0;
22854 ufunc_T *fp;
22855 int indent;
22856 int nesting;
22857 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022858 dictitem_T *v;
22859 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022860 static int func_nr = 0; /* number for nameless function */
22861 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022862 hashtab_T *ht;
22863 int todo;
22864 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022865 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866
22867 /*
22868 * ":function" without argument: list functions.
22869 */
22870 if (ends_excmd(*eap->arg))
22871 {
22872 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022873 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022874 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022875 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022876 {
22877 if (!HASHITEM_EMPTY(hi))
22878 {
22879 --todo;
22880 fp = HI2UF(hi);
22881 if (!isdigit(*fp->uf_name))
22882 list_func_head(fp, FALSE);
22883 }
22884 }
22885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 eap->nextcmd = check_nextcmd(eap->arg);
22887 return;
22888 }
22889
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022890 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022891 * ":function /pat": list functions matching pattern.
22892 */
22893 if (*eap->arg == '/')
22894 {
22895 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22896 if (!eap->skip)
22897 {
22898 regmatch_T regmatch;
22899
22900 c = *p;
22901 *p = NUL;
22902 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22903 *p = c;
22904 if (regmatch.regprog != NULL)
22905 {
22906 regmatch.rm_ic = p_ic;
22907
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022908 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022909 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22910 {
22911 if (!HASHITEM_EMPTY(hi))
22912 {
22913 --todo;
22914 fp = HI2UF(hi);
22915 if (!isdigit(*fp->uf_name)
22916 && vim_regexec(&regmatch, fp->uf_name, 0))
22917 list_func_head(fp, FALSE);
22918 }
22919 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022920 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022921 }
22922 }
22923 if (*p == '/')
22924 ++p;
22925 eap->nextcmd = check_nextcmd(p);
22926 return;
22927 }
22928
22929 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022930 * Get the function name. There are these situations:
22931 * func normal function name
22932 * "name" == func, "fudi.fd_dict" == NULL
22933 * dict.func new dictionary entry
22934 * "name" == NULL, "fudi.fd_dict" set,
22935 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22936 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022937 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022938 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22939 * dict.func existing dict entry that's not a Funcref
22940 * "name" == NULL, "fudi.fd_dict" set,
22941 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022942 * s:func script-local function name
22943 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022946 name = trans_function_name(&p, eap->skip, 0, &fudi);
22947 paren = (vim_strchr(p, '(') != NULL);
22948 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 {
22950 /*
22951 * Return on an invalid expression in braces, unless the expression
22952 * evaluation has been cancelled due to an aborting error, an
22953 * interrupt, or an exception.
22954 */
22955 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022956 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022957 if (!eap->skip && fudi.fd_newkey != NULL)
22958 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022959 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022960 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 else
22963 eap->skip = TRUE;
22964 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022965
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 /* An error in a function call during evaluation of an expression in magic
22967 * braces should not cause the function not to be defined. */
22968 saved_did_emsg = did_emsg;
22969 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970
22971 /*
22972 * ":function func" with only function name: list function.
22973 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022974 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 {
22976 if (!ends_excmd(*skipwhite(p)))
22977 {
22978 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022979 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 }
22981 eap->nextcmd = check_nextcmd(p);
22982 if (eap->nextcmd != NULL)
22983 *p = NUL;
22984 if (!eap->skip && !got_int)
22985 {
22986 fp = find_func(name);
22987 if (fp != NULL)
22988 {
22989 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022990 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022991 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022992 if (FUNCLINE(fp, j) == NULL)
22993 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994 msg_putchar('\n');
22995 msg_outnum((long)(j + 1));
22996 if (j < 9)
22997 msg_putchar(' ');
22998 if (j < 99)
22999 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023000 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 out_flush(); /* show a line at a time */
23002 ui_breakcheck();
23003 }
23004 if (!got_int)
23005 {
23006 msg_putchar('\n');
23007 msg_puts((char_u *)" endfunction");
23008 }
23009 }
23010 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023011 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023013 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014 }
23015
23016 /*
23017 * ":function name(arg1, arg2)" Define function.
23018 */
23019 p = skipwhite(p);
23020 if (*p != '(')
23021 {
23022 if (!eap->skip)
23023 {
23024 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023025 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026 }
23027 /* attempt to continue by skipping some text */
23028 if (vim_strchr(p, '(') != NULL)
23029 p = vim_strchr(p, '(');
23030 }
23031 p = skipwhite(p + 1);
23032
23033 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23034 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23035
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023036 if (!eap->skip)
23037 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023038 /* Check the name of the function. Unless it's a dictionary function
23039 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023040 if (name != NULL)
23041 arg = name;
23042 else
23043 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023044 if (arg != NULL && (fudi.fd_di == NULL
23045 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023046 {
23047 if (*arg == K_SPECIAL)
23048 j = 3;
23049 else
23050 j = 0;
23051 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23052 : eval_isnamec(arg[j])))
23053 ++j;
23054 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023055 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023056 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023057 /* Disallow using the g: dict. */
23058 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23059 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023060 }
23061
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062 /*
23063 * Isolate the arguments: "arg1, arg2, ...)"
23064 */
23065 while (*p != ')')
23066 {
23067 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23068 {
23069 varargs = TRUE;
23070 p += 3;
23071 mustend = TRUE;
23072 }
23073 else
23074 {
23075 arg = p;
23076 while (ASCII_ISALNUM(*p) || *p == '_')
23077 ++p;
23078 if (arg == p || isdigit(*arg)
23079 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23080 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23081 {
23082 if (!eap->skip)
23083 EMSG2(_("E125: Illegal argument: %s"), arg);
23084 break;
23085 }
23086 if (ga_grow(&newargs, 1) == FAIL)
23087 goto erret;
23088 c = *p;
23089 *p = NUL;
23090 arg = vim_strsave(arg);
23091 if (arg == NULL)
23092 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023093
23094 /* Check for duplicate argument name. */
23095 for (i = 0; i < newargs.ga_len; ++i)
23096 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23097 {
23098 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023099 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023100 goto erret;
23101 }
23102
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23104 *p = c;
23105 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106 if (*p == ',')
23107 ++p;
23108 else
23109 mustend = TRUE;
23110 }
23111 p = skipwhite(p);
23112 if (mustend && *p != ')')
23113 {
23114 if (!eap->skip)
23115 EMSG2(_(e_invarg2), eap->arg);
23116 break;
23117 }
23118 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023119 if (*p != ')')
23120 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121 ++p; /* skip the ')' */
23122
Bram Moolenaare9a41262005-01-15 22:18:47 +000023123 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 for (;;)
23125 {
23126 p = skipwhite(p);
23127 if (STRNCMP(p, "range", 5) == 0)
23128 {
23129 flags |= FC_RANGE;
23130 p += 5;
23131 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023132 else if (STRNCMP(p, "dict", 4) == 0)
23133 {
23134 flags |= FC_DICT;
23135 p += 4;
23136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 else if (STRNCMP(p, "abort", 5) == 0)
23138 {
23139 flags |= FC_ABORT;
23140 p += 5;
23141 }
23142 else
23143 break;
23144 }
23145
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023146 /* When there is a line break use what follows for the function body.
23147 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23148 if (*p == '\n')
23149 line_arg = p + 1;
23150 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 EMSG(_(e_trailing));
23152
23153 /*
23154 * Read the body of the function, until ":endfunction" is found.
23155 */
23156 if (KeyTyped)
23157 {
23158 /* Check if the function already exists, don't let the user type the
23159 * whole function before telling him it doesn't work! For a script we
23160 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023161 if (!eap->skip && !eap->forceit)
23162 {
23163 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23164 EMSG(_(e_funcdict));
23165 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023166 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023169 if (!eap->skip && did_emsg)
23170 goto erret;
23171
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 msg_putchar('\n'); /* don't overwrite the function name */
23173 cmdline_row = msg_row;
23174 }
23175
23176 indent = 2;
23177 nesting = 0;
23178 for (;;)
23179 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023180 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023181 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023182 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023183 saved_wait_return = FALSE;
23184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023186 sourcing_lnum_off = sourcing_lnum;
23187
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023188 if (line_arg != NULL)
23189 {
23190 /* Use eap->arg, split up in parts by line breaks. */
23191 theline = line_arg;
23192 p = vim_strchr(theline, '\n');
23193 if (p == NULL)
23194 line_arg += STRLEN(line_arg);
23195 else
23196 {
23197 *p = NUL;
23198 line_arg = p + 1;
23199 }
23200 }
23201 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 theline = getcmdline(':', 0L, indent);
23203 else
23204 theline = eap->getline(':', eap->cookie, indent);
23205 if (KeyTyped)
23206 lines_left = Rows - 1;
23207 if (theline == NULL)
23208 {
23209 EMSG(_("E126: Missing :endfunction"));
23210 goto erret;
23211 }
23212
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023213 /* Detect line continuation: sourcing_lnum increased more than one. */
23214 if (sourcing_lnum > sourcing_lnum_off + 1)
23215 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23216 else
23217 sourcing_lnum_off = 0;
23218
Bram Moolenaar071d4272004-06-13 20:20:40 +000023219 if (skip_until != NULL)
23220 {
23221 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23222 * don't check for ":endfunc". */
23223 if (STRCMP(theline, skip_until) == 0)
23224 {
23225 vim_free(skip_until);
23226 skip_until = NULL;
23227 }
23228 }
23229 else
23230 {
23231 /* skip ':' and blanks*/
23232 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23233 ;
23234
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023235 /* Check for "endfunction". */
23236 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023238 if (line_arg == NULL)
23239 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 break;
23241 }
23242
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023243 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244 * at "end". */
23245 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23246 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023247 else if (STRNCMP(p, "if", 2) == 0
23248 || STRNCMP(p, "wh", 2) == 0
23249 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250 || STRNCMP(p, "try", 3) == 0)
23251 indent += 2;
23252
23253 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023254 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023255 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023256 if (*p == '!')
23257 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023259 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23260 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023262 ++nesting;
23263 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264 }
23265 }
23266
23267 /* Check for ":append" or ":insert". */
23268 p = skip_range(p, NULL);
23269 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23270 || (p[0] == 'i'
23271 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23272 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23273 skip_until = vim_strsave((char_u *)".");
23274
23275 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23276 arg = skipwhite(skiptowhite(p));
23277 if (arg[0] == '<' && arg[1] =='<'
23278 && ((p[0] == 'p' && p[1] == 'y'
23279 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23280 || (p[0] == 'p' && p[1] == 'e'
23281 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23282 || (p[0] == 't' && p[1] == 'c'
23283 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023284 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23285 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023286 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23287 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023288 || (p[0] == 'm' && p[1] == 'z'
23289 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 ))
23291 {
23292 /* ":python <<" continues until a dot, like ":append" */
23293 p = skipwhite(arg + 2);
23294 if (*p == NUL)
23295 skip_until = vim_strsave((char_u *)".");
23296 else
23297 skip_until = vim_strsave(p);
23298 }
23299 }
23300
23301 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023302 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023303 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023304 if (line_arg == NULL)
23305 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023307 }
23308
23309 /* Copy the line to newly allocated memory. get_one_sourceline()
23310 * allocates 250 bytes per line, this saves 80% on average. The cost
23311 * is an extra alloc/free. */
23312 p = vim_strsave(theline);
23313 if (p != NULL)
23314 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023315 if (line_arg == NULL)
23316 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023317 theline = p;
23318 }
23319
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023320 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23321
23322 /* Add NULL lines for continuation lines, so that the line count is
23323 * equal to the index in the growarray. */
23324 while (sourcing_lnum_off-- > 0)
23325 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023326
23327 /* Check for end of eap->arg. */
23328 if (line_arg != NULL && *line_arg == NUL)
23329 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 }
23331
23332 /* Don't define the function when skipping commands or when an error was
23333 * detected. */
23334 if (eap->skip || did_emsg)
23335 goto erret;
23336
23337 /*
23338 * If there are no errors, add the function
23339 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023340 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023341 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023342 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023343 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023344 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023345 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023346 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023347 goto erret;
23348 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023349
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023350 fp = find_func(name);
23351 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023353 if (!eap->forceit)
23354 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023355 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023356 goto erret;
23357 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023358 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023359 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023360 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023361 name);
23362 goto erret;
23363 }
23364 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023365 ga_clear_strings(&(fp->uf_args));
23366 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023367 vim_free(name);
23368 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 }
23371 else
23372 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023373 char numbuf[20];
23374
23375 fp = NULL;
23376 if (fudi.fd_newkey == NULL && !eap->forceit)
23377 {
23378 EMSG(_(e_funcdict));
23379 goto erret;
23380 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023381 if (fudi.fd_di == NULL)
23382 {
23383 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023384 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023385 goto erret;
23386 }
23387 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023388 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023389 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023390
23391 /* Give the function a sequential number. Can only be used with a
23392 * Funcref! */
23393 vim_free(name);
23394 sprintf(numbuf, "%d", ++func_nr);
23395 name = vim_strsave((char_u *)numbuf);
23396 if (name == NULL)
23397 goto erret;
23398 }
23399
23400 if (fp == NULL)
23401 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023402 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023403 {
23404 int slen, plen;
23405 char_u *scriptname;
23406
23407 /* Check that the autoload name matches the script name. */
23408 j = FAIL;
23409 if (sourcing_name != NULL)
23410 {
23411 scriptname = autoload_name(name);
23412 if (scriptname != NULL)
23413 {
23414 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023415 plen = (int)STRLEN(p);
23416 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023417 if (slen > plen && fnamecmp(p,
23418 sourcing_name + slen - plen) == 0)
23419 j = OK;
23420 vim_free(scriptname);
23421 }
23422 }
23423 if (j == FAIL)
23424 {
23425 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23426 goto erret;
23427 }
23428 }
23429
23430 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023431 if (fp == NULL)
23432 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023433
23434 if (fudi.fd_dict != NULL)
23435 {
23436 if (fudi.fd_di == NULL)
23437 {
23438 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023439 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023440 if (fudi.fd_di == NULL)
23441 {
23442 vim_free(fp);
23443 goto erret;
23444 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023445 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23446 {
23447 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023448 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023449 goto erret;
23450 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023451 }
23452 else
23453 /* overwrite existing dict entry */
23454 clear_tv(&fudi.fd_di->di_tv);
23455 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023456 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023457 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023458 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023459
23460 /* behave like "dict" was used */
23461 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023462 }
23463
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023465 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023466 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23467 {
23468 vim_free(fp);
23469 goto erret;
23470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023472 fp->uf_args = newargs;
23473 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023474#ifdef FEAT_PROFILE
23475 fp->uf_tml_count = NULL;
23476 fp->uf_tml_total = NULL;
23477 fp->uf_tml_self = NULL;
23478 fp->uf_profiling = FALSE;
23479 if (prof_def_func())
23480 func_do_profile(fp);
23481#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023482 fp->uf_varargs = varargs;
23483 fp->uf_flags = flags;
23484 fp->uf_calls = 0;
23485 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023486 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487
23488erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 ga_clear_strings(&newargs);
23490 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023491ret_free:
23492 vim_free(skip_until);
23493 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023496 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497}
23498
23499/*
23500 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023501 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023502 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023503 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023504 * TFN_INT: internal function name OK
23505 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023506 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023507 * Advances "pp" to just after the function name (if no error).
23508 */
23509 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023510trans_function_name(
23511 char_u **pp,
23512 int skip, /* only find the end, don't evaluate */
23513 int flags,
23514 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023515{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023516 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 char_u *start;
23518 char_u *end;
23519 int lead;
23520 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023522 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023523
23524 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023525 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023527
23528 /* Check for hard coded <SNR>: already translated function ID (from a user
23529 * command). */
23530 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23531 && (*pp)[2] == (int)KE_SNR)
23532 {
23533 *pp += 3;
23534 len = get_id_len(pp) + 3;
23535 return vim_strnsave(start, len);
23536 }
23537
23538 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23539 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023541 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023543
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023544 /* Note that TFN_ flags use the same values as GLV_ flags. */
23545 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023546 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023547 if (end == start)
23548 {
23549 if (!skip)
23550 EMSG(_("E129: Function name required"));
23551 goto theend;
23552 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023553 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023554 {
23555 /*
23556 * Report an invalid expression in braces, unless the expression
23557 * evaluation has been cancelled due to an aborting error, an
23558 * interrupt, or an exception.
23559 */
23560 if (!aborting())
23561 {
23562 if (end != NULL)
23563 EMSG2(_(e_invarg2), start);
23564 }
23565 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023566 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023567 goto theend;
23568 }
23569
23570 if (lv.ll_tv != NULL)
23571 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023572 if (fdp != NULL)
23573 {
23574 fdp->fd_dict = lv.ll_dict;
23575 fdp->fd_newkey = lv.ll_newkey;
23576 lv.ll_newkey = NULL;
23577 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023578 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023579 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23580 {
23581 name = vim_strsave(lv.ll_tv->vval.v_string);
23582 *pp = end;
23583 }
23584 else
23585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023586 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23587 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023588 EMSG(_(e_funcref));
23589 else
23590 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023591 name = NULL;
23592 }
23593 goto theend;
23594 }
23595
23596 if (lv.ll_name == NULL)
23597 {
23598 /* Error found, but continue after the function name. */
23599 *pp = end;
23600 goto theend;
23601 }
23602
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023603 /* Check if the name is a Funcref. If so, use the value. */
23604 if (lv.ll_exp_name != NULL)
23605 {
23606 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023607 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023608 if (name == lv.ll_exp_name)
23609 name = NULL;
23610 }
23611 else
23612 {
23613 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023614 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023615 if (name == *pp)
23616 name = NULL;
23617 }
23618 if (name != NULL)
23619 {
23620 name = vim_strsave(name);
23621 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023622 if (STRNCMP(name, "<SNR>", 5) == 0)
23623 {
23624 /* Change "<SNR>" to the byte sequence. */
23625 name[0] = K_SPECIAL;
23626 name[1] = KS_EXTRA;
23627 name[2] = (int)KE_SNR;
23628 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23629 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023630 goto theend;
23631 }
23632
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023633 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023634 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023635 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023636 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23637 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23638 {
23639 /* When there was "s:" already or the name expanded to get a
23640 * leading "s:" then remove it. */
23641 lv.ll_name += 2;
23642 len -= 2;
23643 lead = 2;
23644 }
23645 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023646 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023647 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023648 /* skip over "s:" and "g:" */
23649 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023650 lv.ll_name += 2;
23651 len = (int)(end - lv.ll_name);
23652 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023653
23654 /*
23655 * Copy the function name to allocated memory.
23656 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23657 * Accept <SNR>123_name() outside a script.
23658 */
23659 if (skip)
23660 lead = 0; /* do nothing */
23661 else if (lead > 0)
23662 {
23663 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023664 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23665 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023666 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023667 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023668 if (current_SID <= 0)
23669 {
23670 EMSG(_(e_usingsid));
23671 goto theend;
23672 }
23673 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23674 lead += (int)STRLEN(sid_buf);
23675 }
23676 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023677 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023678 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023679 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023680 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023681 goto theend;
23682 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023683 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023684 {
23685 char_u *cp = vim_strchr(lv.ll_name, ':');
23686
23687 if (cp != NULL && cp < end)
23688 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023689 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023690 goto theend;
23691 }
23692 }
23693
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023694 name = alloc((unsigned)(len + lead + 1));
23695 if (name != NULL)
23696 {
23697 if (lead > 0)
23698 {
23699 name[0] = K_SPECIAL;
23700 name[1] = KS_EXTRA;
23701 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023702 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023703 STRCPY(name + 3, sid_buf);
23704 }
23705 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023706 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023707 }
23708 *pp = end;
23709
23710theend:
23711 clear_lval(&lv);
23712 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023713}
23714
23715/*
23716 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23717 * Return 2 if "p" starts with "s:".
23718 * Return 0 otherwise.
23719 */
23720 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023721eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023723 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23724 * the standard library function. */
23725 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23726 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023727 return 5;
23728 if (p[0] == 's' && p[1] == ':')
23729 return 2;
23730 return 0;
23731}
23732
23733/*
23734 * Return TRUE if "p" starts with "<SID>" or "s:".
23735 * Only works if eval_fname_script() returned non-zero for "p"!
23736 */
23737 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023738eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023739{
23740 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23741}
23742
23743/*
23744 * List the head of the function: "name(arg1, arg2)".
23745 */
23746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023747list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748{
23749 int j;
23750
23751 msg_start();
23752 if (indent)
23753 MSG_PUTS(" ");
23754 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023755 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756 {
23757 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023758 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759 }
23760 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023761 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023762 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023763 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764 {
23765 if (j)
23766 MSG_PUTS(", ");
23767 msg_puts(FUNCARG(fp, j));
23768 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023769 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023770 {
23771 if (j)
23772 MSG_PUTS(", ");
23773 MSG_PUTS("...");
23774 }
23775 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023776 if (fp->uf_flags & FC_ABORT)
23777 MSG_PUTS(" abort");
23778 if (fp->uf_flags & FC_RANGE)
23779 MSG_PUTS(" range");
23780 if (fp->uf_flags & FC_DICT)
23781 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023782 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023783 if (p_verbose > 0)
23784 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785}
23786
23787/*
23788 * Find a function by name, return pointer to it in ufuncs.
23789 * Return NULL for unknown function.
23790 */
23791 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023792find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023793{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023794 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023796 hi = hash_find(&func_hashtab, name);
23797 if (!HASHITEM_EMPTY(hi))
23798 return HI2UF(hi);
23799 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800}
23801
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023802#if defined(EXITFREE) || defined(PROTO)
23803 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023804free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023805{
23806 hashitem_T *hi;
23807
23808 /* Need to start all over every time, because func_free() may change the
23809 * hash table. */
23810 while (func_hashtab.ht_used > 0)
23811 for (hi = func_hashtab.ht_array; ; ++hi)
23812 if (!HASHITEM_EMPTY(hi))
23813 {
23814 func_free(HI2UF(hi));
23815 break;
23816 }
23817}
23818#endif
23819
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023820 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023821translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023822{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023823 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023824 return find_internal_func(name) >= 0;
23825 return find_func(name) != NULL;
23826}
23827
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023828/*
23829 * Return TRUE if a function "name" exists.
23830 */
23831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023832function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023833{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023834 char_u *nm = name;
23835 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023836 int n = FALSE;
23837
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023838 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23839 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023840 nm = skipwhite(nm);
23841
23842 /* Only accept "funcname", "funcname ", "funcname (..." and
23843 * "funcname(...", not "funcname!...". */
23844 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023845 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023846 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023847 return n;
23848}
23849
Bram Moolenaara1544c02013-05-30 12:35:52 +020023850 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023851get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023852{
23853 char_u *nm = name;
23854 char_u *p;
23855
23856 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23857
23858 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023859 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023860 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023861
Bram Moolenaara1544c02013-05-30 12:35:52 +020023862 vim_free(p);
23863 return NULL;
23864}
23865
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023866/*
23867 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023868 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23869 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023870 */
23871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023872builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023873{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023874 char_u *p;
23875
23876 if (!ASCII_ISLOWER(name[0]))
23877 return FALSE;
23878 p = vim_strchr(name, AUTOLOAD_CHAR);
23879 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023880}
23881
Bram Moolenaar05159a02005-02-26 23:04:13 +000023882#if defined(FEAT_PROFILE) || defined(PROTO)
23883/*
23884 * Start profiling function "fp".
23885 */
23886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023887func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023888{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023889 int len = fp->uf_lines.ga_len;
23890
23891 if (len == 0)
23892 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023893 fp->uf_tm_count = 0;
23894 profile_zero(&fp->uf_tm_self);
23895 profile_zero(&fp->uf_tm_total);
23896 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023897 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023898 if (fp->uf_tml_total == NULL)
23899 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023900 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023901 if (fp->uf_tml_self == NULL)
23902 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023903 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023904 fp->uf_tml_idx = -1;
23905 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23906 || fp->uf_tml_self == NULL)
23907 return; /* out of memory */
23908
23909 fp->uf_profiling = TRUE;
23910}
23911
23912/*
23913 * Dump the profiling results for all functions in file "fd".
23914 */
23915 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023916func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023917{
23918 hashitem_T *hi;
23919 int todo;
23920 ufunc_T *fp;
23921 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023922 ufunc_T **sorttab;
23923 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023924
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023925 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023926 if (todo == 0)
23927 return; /* nothing to dump */
23928
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023929 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023930
Bram Moolenaar05159a02005-02-26 23:04:13 +000023931 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23932 {
23933 if (!HASHITEM_EMPTY(hi))
23934 {
23935 --todo;
23936 fp = HI2UF(hi);
23937 if (fp->uf_profiling)
23938 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023939 if (sorttab != NULL)
23940 sorttab[st_len++] = fp;
23941
Bram Moolenaar05159a02005-02-26 23:04:13 +000023942 if (fp->uf_name[0] == K_SPECIAL)
23943 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23944 else
23945 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23946 if (fp->uf_tm_count == 1)
23947 fprintf(fd, "Called 1 time\n");
23948 else
23949 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23950 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23951 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23952 fprintf(fd, "\n");
23953 fprintf(fd, "count total (s) self (s)\n");
23954
23955 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23956 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023957 if (FUNCLINE(fp, i) == NULL)
23958 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023959 prof_func_line(fd, fp->uf_tml_count[i],
23960 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023961 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23962 }
23963 fprintf(fd, "\n");
23964 }
23965 }
23966 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023967
23968 if (sorttab != NULL && st_len > 0)
23969 {
23970 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23971 prof_total_cmp);
23972 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23973 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23974 prof_self_cmp);
23975 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23976 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023977
23978 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023979}
Bram Moolenaar73830342005-02-28 22:48:19 +000023980
23981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023982prof_sort_list(
23983 FILE *fd,
23984 ufunc_T **sorttab,
23985 int st_len,
23986 char *title,
23987 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023988{
23989 int i;
23990 ufunc_T *fp;
23991
23992 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23993 fprintf(fd, "count total (s) self (s) function\n");
23994 for (i = 0; i < 20 && i < st_len; ++i)
23995 {
23996 fp = sorttab[i];
23997 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23998 prefer_self);
23999 if (fp->uf_name[0] == K_SPECIAL)
24000 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24001 else
24002 fprintf(fd, " %s()\n", fp->uf_name);
24003 }
24004 fprintf(fd, "\n");
24005}
24006
24007/*
24008 * Print the count and times for one function or function line.
24009 */
24010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024011prof_func_line(
24012 FILE *fd,
24013 int count,
24014 proftime_T *total,
24015 proftime_T *self,
24016 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024017{
24018 if (count > 0)
24019 {
24020 fprintf(fd, "%5d ", count);
24021 if (prefer_self && profile_equal(total, self))
24022 fprintf(fd, " ");
24023 else
24024 fprintf(fd, "%s ", profile_msg(total));
24025 if (!prefer_self && profile_equal(total, self))
24026 fprintf(fd, " ");
24027 else
24028 fprintf(fd, "%s ", profile_msg(self));
24029 }
24030 else
24031 fprintf(fd, " ");
24032}
24033
24034/*
24035 * Compare function for total time sorting.
24036 */
24037 static int
24038#ifdef __BORLANDC__
24039_RTLENTRYF
24040#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024041prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024042{
24043 ufunc_T *p1, *p2;
24044
24045 p1 = *(ufunc_T **)s1;
24046 p2 = *(ufunc_T **)s2;
24047 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24048}
24049
24050/*
24051 * Compare function for self time sorting.
24052 */
24053 static int
24054#ifdef __BORLANDC__
24055_RTLENTRYF
24056#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024057prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024058{
24059 ufunc_T *p1, *p2;
24060
24061 p1 = *(ufunc_T **)s1;
24062 p2 = *(ufunc_T **)s2;
24063 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24064}
24065
Bram Moolenaar05159a02005-02-26 23:04:13 +000024066#endif
24067
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024068/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024069 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024070 * Return TRUE if a package was loaded.
24071 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024072 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024073script_autoload(
24074 char_u *name,
24075 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024076{
24077 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024078 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024079 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024080 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024081
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024082 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024083 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024084 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024085 return FALSE;
24086
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024087 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024088
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024089 /* Find the name in the list of previously loaded package names. Skip
24090 * "autoload/", it's always the same. */
24091 for (i = 0; i < ga_loaded.ga_len; ++i)
24092 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24093 break;
24094 if (!reload && i < ga_loaded.ga_len)
24095 ret = FALSE; /* was loaded already */
24096 else
24097 {
24098 /* Remember the name if it wasn't loaded already. */
24099 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24100 {
24101 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24102 tofree = NULL;
24103 }
24104
24105 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024106 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024107 ret = TRUE;
24108 }
24109
24110 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024111 return ret;
24112}
24113
24114/*
24115 * Return the autoload script name for a function or variable name.
24116 * Returns NULL when out of memory.
24117 */
24118 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024119autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024120{
24121 char_u *p;
24122 char_u *scriptname;
24123
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024124 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024125 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24126 if (scriptname == NULL)
24127 return FALSE;
24128 STRCPY(scriptname, "autoload/");
24129 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024130 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024131 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024132 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024133 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024134 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024135}
24136
Bram Moolenaar071d4272004-06-13 20:20:40 +000024137#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24138
24139/*
24140 * Function given to ExpandGeneric() to obtain the list of user defined
24141 * function names.
24142 */
24143 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024144get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024146 static long_u done;
24147 static hashitem_T *hi;
24148 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149
24150 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024152 done = 0;
24153 hi = func_hashtab.ht_array;
24154 }
24155 if (done < func_hashtab.ht_used)
24156 {
24157 if (done++ > 0)
24158 ++hi;
24159 while (HASHITEM_EMPTY(hi))
24160 ++hi;
24161 fp = HI2UF(hi);
24162
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024163 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024164 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024165
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024166 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24167 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024168
24169 cat_func_name(IObuff, fp);
24170 if (xp->xp_context != EXPAND_USER_FUNC)
24171 {
24172 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024173 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024174 STRCAT(IObuff, ")");
24175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 return IObuff;
24177 }
24178 return NULL;
24179}
24180
24181#endif /* FEAT_CMDL_COMPL */
24182
24183/*
24184 * Copy the function name of "fp" to buffer "buf".
24185 * "buf" must be able to hold the function name plus three bytes.
24186 * Takes care of script-local function names.
24187 */
24188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024189cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024190{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024191 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024192 {
24193 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024194 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 }
24196 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024197 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198}
24199
24200/*
24201 * ":delfunction {name}"
24202 */
24203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024204ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024206 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 char_u *p;
24208 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024209 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024210
24211 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024212 name = trans_function_name(&p, eap->skip, 0, &fudi);
24213 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024215 {
24216 if (fudi.fd_dict != NULL && !eap->skip)
24217 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220 if (!ends_excmd(*skipwhite(p)))
24221 {
24222 vim_free(name);
24223 EMSG(_(e_trailing));
24224 return;
24225 }
24226 eap->nextcmd = check_nextcmd(p);
24227 if (eap->nextcmd != NULL)
24228 *p = NUL;
24229
24230 if (!eap->skip)
24231 fp = find_func(name);
24232 vim_free(name);
24233
24234 if (!eap->skip)
24235 {
24236 if (fp == NULL)
24237 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024238 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 return;
24240 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024241 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 {
24243 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24244 return;
24245 }
24246
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024247 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024248 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024249 /* Delete the dict item that refers to the function, it will
24250 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024251 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024253 else
24254 func_free(fp);
24255 }
24256}
24257
24258/*
24259 * Free a function and remove it from the list of functions.
24260 */
24261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024262func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024263{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024264 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024265
24266 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024267 ga_clear_strings(&(fp->uf_args));
24268 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024269#ifdef FEAT_PROFILE
24270 vim_free(fp->uf_tml_count);
24271 vim_free(fp->uf_tml_total);
24272 vim_free(fp->uf_tml_self);
24273#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024274
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024275 /* remove the function from the function hashtable */
24276 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24277 if (HASHITEM_EMPTY(hi))
24278 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024279 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024280 hash_remove(&func_hashtab, hi);
24281
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024282 vim_free(fp);
24283}
24284
24285/*
24286 * Unreference a Function: decrement the reference count and free it when it
24287 * becomes zero. Only for numbered functions.
24288 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024290func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024291{
24292 ufunc_T *fp;
24293
24294 if (name != NULL && isdigit(*name))
24295 {
24296 fp = find_func(name);
24297 if (fp == NULL)
24298 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024299 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024300 {
24301 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024302 * when "uf_calls" becomes zero. */
24303 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024304 func_free(fp);
24305 }
24306 }
24307}
24308
24309/*
24310 * Count a reference to a Function.
24311 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024313func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024314{
24315 ufunc_T *fp;
24316
24317 if (name != NULL && isdigit(*name))
24318 {
24319 fp = find_func(name);
24320 if (fp == NULL)
24321 EMSG2(_(e_intern2), "func_ref()");
24322 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024323 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324 }
24325}
24326
24327/*
24328 * Call a user function.
24329 */
24330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024331call_user_func(
24332 ufunc_T *fp, /* pointer to function */
24333 int argcount, /* nr of args */
24334 typval_T *argvars, /* arguments */
24335 typval_T *rettv, /* return value */
24336 linenr_T firstline, /* first line of range */
24337 linenr_T lastline, /* last line of range */
24338 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339{
Bram Moolenaar33570922005-01-25 22:26:29 +000024340 char_u *save_sourcing_name;
24341 linenr_T save_sourcing_lnum;
24342 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024343 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024344 int save_did_emsg;
24345 static int depth = 0;
24346 dictitem_T *v;
24347 int fixvar_idx = 0; /* index in fixvar[] */
24348 int i;
24349 int ai;
24350 char_u numbuf[NUMBUFLEN];
24351 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024352 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024353#ifdef FEAT_PROFILE
24354 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024355 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357
24358 /* If depth of calling is getting too high, don't execute the function */
24359 if (depth >= p_mfd)
24360 {
24361 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024362 rettv->v_type = VAR_NUMBER;
24363 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024364 return;
24365 }
24366 ++depth;
24367
24368 line_breakcheck(); /* check for CTRL-C hit */
24369
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024370 fc = (funccall_T *)alloc(sizeof(funccall_T));
24371 fc->caller = current_funccal;
24372 current_funccal = fc;
24373 fc->func = fp;
24374 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024375 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024376 fc->linenr = 0;
24377 fc->returned = FALSE;
24378 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024380 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24381 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024382
Bram Moolenaar33570922005-01-25 22:26:29 +000024383 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024384 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024385 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24386 * each argument variable and saves a lot of time.
24387 */
24388 /*
24389 * Init l: variables.
24390 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024391 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024392 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024393 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024394 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24395 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024396 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024397 name = v->di_key;
24398 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024399 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024400 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024401 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024402 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024403 v->di_tv.vval.v_dict = selfdict;
24404 ++selfdict->dv_refcount;
24405 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024406
Bram Moolenaar33570922005-01-25 22:26:29 +000024407 /*
24408 * Init a: variables.
24409 * Set a:0 to "argcount".
24410 * Set a:000 to a list with room for the "..." arguments.
24411 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024412 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024413 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024414 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024415 /* Use "name" to avoid a warning from some compiler that checks the
24416 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024417 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024418 name = v->di_key;
24419 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024420 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024421 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024422 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024423 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024424 v->di_tv.vval.v_list = &fc->l_varlist;
24425 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24426 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24427 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024428
24429 /*
24430 * Set a:firstline to "firstline" and a:lastline to "lastline".
24431 * Set a:name to named arguments.
24432 * Set a:N to the "..." arguments.
24433 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024434 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024435 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024436 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024437 (varnumber_T)lastline);
24438 for (i = 0; i < argcount; ++i)
24439 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024440 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024441 if (ai < 0)
24442 /* named argument a:name */
24443 name = FUNCARG(fp, i);
24444 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024445 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024446 /* "..." argument a:1, a:2, etc. */
24447 sprintf((char *)numbuf, "%d", ai + 1);
24448 name = numbuf;
24449 }
24450 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24451 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024452 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024453 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24454 }
24455 else
24456 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024457 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24458 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024459 if (v == NULL)
24460 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024461 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024462 }
24463 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024464 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024465
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024466 /* Note: the values are copied directly to avoid alloc/free.
24467 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024468 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024469 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024470
24471 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24472 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024473 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24474 fc->l_listitems[ai].li_tv = argvars[i];
24475 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024476 }
24477 }
24478
Bram Moolenaar071d4272004-06-13 20:20:40 +000024479 /* Don't redraw while executing the function. */
24480 ++RedrawingDisabled;
24481 save_sourcing_name = sourcing_name;
24482 save_sourcing_lnum = sourcing_lnum;
24483 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024484 /* need space for function name + ("function " + 3) or "[number]" */
24485 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24486 + STRLEN(fp->uf_name) + 20;
24487 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024488 if (sourcing_name != NULL)
24489 {
24490 if (save_sourcing_name != NULL
24491 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024492 sprintf((char *)sourcing_name, "%s[%d]..",
24493 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024494 else
24495 STRCPY(sourcing_name, "function ");
24496 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24497
24498 if (p_verbose >= 12)
24499 {
24500 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024501 verbose_enter_scroll();
24502
Bram Moolenaar555b2802005-05-19 21:08:39 +000024503 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024504 if (p_verbose >= 14)
24505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024506 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024507 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024508 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024509 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024510
24511 msg_puts((char_u *)"(");
24512 for (i = 0; i < argcount; ++i)
24513 {
24514 if (i > 0)
24515 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024516 if (argvars[i].v_type == VAR_NUMBER)
24517 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518 else
24519 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024520 /* Do not want errors such as E724 here. */
24521 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024522 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024523 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024524 if (s != NULL)
24525 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024526 if (vim_strsize(s) > MSG_BUF_CLEN)
24527 {
24528 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24529 s = buf;
24530 }
24531 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024532 vim_free(tofree);
24533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 }
24535 }
24536 msg_puts((char_u *)")");
24537 }
24538 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024539
24540 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541 --no_wait_return;
24542 }
24543 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024544#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024545 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024546 {
24547 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24548 func_do_profile(fp);
24549 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024550 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024551 {
24552 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024553 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024554 profile_zero(&fp->uf_tm_children);
24555 }
24556 script_prof_save(&wait_start);
24557 }
24558#endif
24559
Bram Moolenaar071d4272004-06-13 20:20:40 +000024560 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024561 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024562 save_did_emsg = did_emsg;
24563 did_emsg = FALSE;
24564
24565 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024566 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24568
24569 --RedrawingDisabled;
24570
24571 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024572 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024574 clear_tv(rettv);
24575 rettv->v_type = VAR_NUMBER;
24576 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024577 }
24578
Bram Moolenaar05159a02005-02-26 23:04:13 +000024579#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024580 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024581 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024582 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024583 profile_end(&call_start);
24584 profile_sub_wait(&wait_start, &call_start);
24585 profile_add(&fp->uf_tm_total, &call_start);
24586 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024587 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024588 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024589 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24590 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024591 }
24592 }
24593#endif
24594
Bram Moolenaar071d4272004-06-13 20:20:40 +000024595 /* when being verbose, mention the return value */
24596 if (p_verbose >= 12)
24597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024598 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024599 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024600
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024602 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024603 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024604 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024605 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024606 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024608 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024609 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024610 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024611 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024612
Bram Moolenaar555b2802005-05-19 21:08:39 +000024613 /* The value may be very long. Skip the middle part, so that we
24614 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024615 * truncate it at the end. Don't want errors such as E724 here. */
24616 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024617 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024618 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024619 if (s != NULL)
24620 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024621 if (vim_strsize(s) > MSG_BUF_CLEN)
24622 {
24623 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24624 s = buf;
24625 }
24626 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024627 vim_free(tofree);
24628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024629 }
24630 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024631
24632 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633 --no_wait_return;
24634 }
24635
24636 vim_free(sourcing_name);
24637 sourcing_name = save_sourcing_name;
24638 sourcing_lnum = save_sourcing_lnum;
24639 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024640#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024641 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024642 script_prof_restore(&wait_start);
24643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024644
24645 if (p_verbose >= 12 && sourcing_name != NULL)
24646 {
24647 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024648 verbose_enter_scroll();
24649
Bram Moolenaar555b2802005-05-19 21:08:39 +000024650 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024651 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024652
24653 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024654 --no_wait_return;
24655 }
24656
24657 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024658 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024660
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024661 /* If the a:000 list and the l: and a: dicts are not referenced we can
24662 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024663 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24664 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24665 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24666 {
24667 free_funccal(fc, FALSE);
24668 }
24669 else
24670 {
24671 hashitem_T *hi;
24672 listitem_T *li;
24673 int todo;
24674
24675 /* "fc" is still in use. This can happen when returning "a:000" or
24676 * assigning "l:" to a global variable.
24677 * Link "fc" in the list for garbage collection later. */
24678 fc->caller = previous_funccal;
24679 previous_funccal = fc;
24680
24681 /* Make a copy of the a: variables, since we didn't do that above. */
24682 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24683 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24684 {
24685 if (!HASHITEM_EMPTY(hi))
24686 {
24687 --todo;
24688 v = HI2DI(hi);
24689 copy_tv(&v->di_tv, &v->di_tv);
24690 }
24691 }
24692
24693 /* Make a copy of the a:000 items, since we didn't do that above. */
24694 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24695 copy_tv(&li->li_tv, &li->li_tv);
24696 }
24697}
24698
24699/*
24700 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024701 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024702 */
24703 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024704can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024705{
24706 return (fc->l_varlist.lv_copyID != copyID
24707 && fc->l_vars.dv_copyID != copyID
24708 && fc->l_avars.dv_copyID != copyID);
24709}
24710
24711/*
24712 * Free "fc" and what it contains.
24713 */
24714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024715free_funccal(
24716 funccall_T *fc,
24717 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024718{
24719 listitem_T *li;
24720
24721 /* The a: variables typevals may not have been allocated, only free the
24722 * allocated variables. */
24723 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24724
24725 /* free all l: variables */
24726 vars_clear(&fc->l_vars.dv_hashtab);
24727
24728 /* Free the a:000 variables if they were allocated. */
24729 if (free_val)
24730 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24731 clear_tv(&li->li_tv);
24732
24733 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024734}
24735
24736/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024737 * Add a number variable "name" to dict "dp" with value "nr".
24738 */
24739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024740add_nr_var(
24741 dict_T *dp,
24742 dictitem_T *v,
24743 char *name,
24744 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024745{
24746 STRCPY(v->di_key, name);
24747 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24748 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24749 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024750 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024751 v->di_tv.vval.v_number = nr;
24752}
24753
24754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024755 * ":return [expr]"
24756 */
24757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024758ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024759{
24760 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024761 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762 int returning = FALSE;
24763
24764 if (current_funccal == NULL)
24765 {
24766 EMSG(_("E133: :return not inside a function"));
24767 return;
24768 }
24769
24770 if (eap->skip)
24771 ++emsg_skip;
24772
24773 eap->nextcmd = NULL;
24774 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024775 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024776 {
24777 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024778 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024780 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781 }
24782 /* It's safer to return also on error. */
24783 else if (!eap->skip)
24784 {
24785 /*
24786 * Return unless the expression evaluation has been cancelled due to an
24787 * aborting error, an interrupt, or an exception.
24788 */
24789 if (!aborting())
24790 returning = do_return(eap, FALSE, TRUE, NULL);
24791 }
24792
24793 /* When skipping or the return gets pending, advance to the next command
24794 * in this line (!returning). Otherwise, ignore the rest of the line.
24795 * Following lines will be ignored by get_func_line(). */
24796 if (returning)
24797 eap->nextcmd = NULL;
24798 else if (eap->nextcmd == NULL) /* no argument */
24799 eap->nextcmd = check_nextcmd(arg);
24800
24801 if (eap->skip)
24802 --emsg_skip;
24803}
24804
24805/*
24806 * Return from a function. Possibly makes the return pending. Also called
24807 * for a pending return at the ":endtry" or after returning from an extra
24808 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024809 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024810 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024811 * FALSE when the return gets pending.
24812 */
24813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024814do_return(
24815 exarg_T *eap,
24816 int reanimate,
24817 int is_cmd,
24818 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024819{
24820 int idx;
24821 struct condstack *cstack = eap->cstack;
24822
24823 if (reanimate)
24824 /* Undo the return. */
24825 current_funccal->returned = FALSE;
24826
24827 /*
24828 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24829 * not in its finally clause (which then is to be executed next) is found.
24830 * In this case, make the ":return" pending for execution at the ":endtry".
24831 * Otherwise, return normally.
24832 */
24833 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24834 if (idx >= 0)
24835 {
24836 cstack->cs_pending[idx] = CSTP_RETURN;
24837
24838 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024839 /* A pending return again gets pending. "rettv" points to an
24840 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024842 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024843 else
24844 {
24845 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024846 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024848 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024850 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 {
24852 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024853 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024854 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024855 else
24856 EMSG(_(e_outofmem));
24857 }
24858 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024859 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024860
24861 if (reanimate)
24862 {
24863 /* The pending return value could be overwritten by a ":return"
24864 * without argument in a finally clause; reset the default
24865 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024866 current_funccal->rettv->v_type = VAR_NUMBER;
24867 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024868 }
24869 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024870 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871 }
24872 else
24873 {
24874 current_funccal->returned = TRUE;
24875
24876 /* If the return is carried out now, store the return value. For
24877 * a return immediately after reanimation, the value is already
24878 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024879 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024881 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024882 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024883 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024884 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024885 }
24886 }
24887
24888 return idx < 0;
24889}
24890
24891/*
24892 * Free the variable with a pending return value.
24893 */
24894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024895discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896{
Bram Moolenaar33570922005-01-25 22:26:29 +000024897 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024898}
24899
24900/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024901 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902 * is an allocated string. Used by report_pending() for verbose messages.
24903 */
24904 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024905get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024907 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024908 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024909 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024910
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024911 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024912 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024913 if (s == NULL)
24914 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024915
24916 STRCPY(IObuff, ":return ");
24917 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24918 if (STRLEN(s) + 8 >= IOSIZE)
24919 STRCPY(IObuff + IOSIZE - 4, "...");
24920 vim_free(tofree);
24921 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922}
24923
24924/*
24925 * Get next function line.
24926 * Called by do_cmdline() to get the next line.
24927 * Returns allocated string, or NULL for end of function.
24928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024929 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024930get_func_line(
24931 int c UNUSED,
24932 void *cookie,
24933 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024934{
Bram Moolenaar33570922005-01-25 22:26:29 +000024935 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024936 ufunc_T *fp = fcp->func;
24937 char_u *retval;
24938 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939
24940 /* If breakpoints have been added/deleted need to check for it. */
24941 if (fcp->dbg_tick != debug_tick)
24942 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024943 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944 sourcing_lnum);
24945 fcp->dbg_tick = debug_tick;
24946 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024947#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024948 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024949 func_line_end(cookie);
24950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951
Bram Moolenaar05159a02005-02-26 23:04:13 +000024952 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024953 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24954 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024955 retval = NULL;
24956 else
24957 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024958 /* Skip NULL lines (continuation lines). */
24959 while (fcp->linenr < gap->ga_len
24960 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24961 ++fcp->linenr;
24962 if (fcp->linenr >= gap->ga_len)
24963 retval = NULL;
24964 else
24965 {
24966 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24967 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024968#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024969 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024970 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024971#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024973 }
24974
24975 /* Did we encounter a breakpoint? */
24976 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24977 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024978 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024979 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024980 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024981 sourcing_lnum);
24982 fcp->dbg_tick = debug_tick;
24983 }
24984
24985 return retval;
24986}
24987
Bram Moolenaar05159a02005-02-26 23:04:13 +000024988#if defined(FEAT_PROFILE) || defined(PROTO)
24989/*
24990 * Called when starting to read a function line.
24991 * "sourcing_lnum" must be correct!
24992 * When skipping lines it may not actually be executed, but we won't find out
24993 * until later and we need to store the time now.
24994 */
24995 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024996func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024997{
24998 funccall_T *fcp = (funccall_T *)cookie;
24999 ufunc_T *fp = fcp->func;
25000
25001 if (fp->uf_profiling && sourcing_lnum >= 1
25002 && sourcing_lnum <= fp->uf_lines.ga_len)
25003 {
25004 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025005 /* Skip continuation lines. */
25006 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25007 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025008 fp->uf_tml_execed = FALSE;
25009 profile_start(&fp->uf_tml_start);
25010 profile_zero(&fp->uf_tml_children);
25011 profile_get_wait(&fp->uf_tml_wait);
25012 }
25013}
25014
25015/*
25016 * Called when actually executing a function line.
25017 */
25018 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025019func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025020{
25021 funccall_T *fcp = (funccall_T *)cookie;
25022 ufunc_T *fp = fcp->func;
25023
25024 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25025 fp->uf_tml_execed = TRUE;
25026}
25027
25028/*
25029 * Called when done with a function line.
25030 */
25031 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025032func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025033{
25034 funccall_T *fcp = (funccall_T *)cookie;
25035 ufunc_T *fp = fcp->func;
25036
25037 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25038 {
25039 if (fp->uf_tml_execed)
25040 {
25041 ++fp->uf_tml_count[fp->uf_tml_idx];
25042 profile_end(&fp->uf_tml_start);
25043 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025044 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025045 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25046 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025047 }
25048 fp->uf_tml_idx = -1;
25049 }
25050}
25051#endif
25052
Bram Moolenaar071d4272004-06-13 20:20:40 +000025053/*
25054 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025055 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025056 */
25057 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025058func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025059{
Bram Moolenaar33570922005-01-25 22:26:29 +000025060 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025061
25062 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25063 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025064 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025065 || fcp->returned);
25066}
25067
25068/*
25069 * return TRUE if cookie indicates a function which "abort"s on errors.
25070 */
25071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025072func_has_abort(
25073 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025074{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025075 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076}
25077
25078#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25079typedef enum
25080{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025081 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25082 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25083 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084} var_flavour_T;
25085
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025086static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025087
25088 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025089var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025090{
25091 char_u *p = varname;
25092
25093 if (ASCII_ISUPPER(*p))
25094 {
25095 while (*(++p))
25096 if (ASCII_ISLOWER(*p))
25097 return VAR_FLAVOUR_SESSION;
25098 return VAR_FLAVOUR_VIMINFO;
25099 }
25100 else
25101 return VAR_FLAVOUR_DEFAULT;
25102}
25103#endif
25104
25105#if defined(FEAT_VIMINFO) || defined(PROTO)
25106/*
25107 * Restore global vars that start with a capital from the viminfo file
25108 */
25109 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025110read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025111{
25112 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025113 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025114 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025115 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025116
25117 if (!writing && (find_viminfo_parameter('!') != NULL))
25118 {
25119 tab = vim_strchr(virp->vir_line + 1, '\t');
25120 if (tab != NULL)
25121 {
25122 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025123 switch (*tab)
25124 {
25125 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025126#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025127 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025128#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025129 case 'D': type = VAR_DICT; break;
25130 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025131 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025133
25134 tab = vim_strchr(tab, '\t');
25135 if (tab != NULL)
25136 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025137 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025138 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025139 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025140 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025141#ifdef FEAT_FLOAT
25142 else if (type == VAR_FLOAT)
25143 (void)string2float(tab + 1, &tv.vval.v_float);
25144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025145 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025146 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025147 if (type == VAR_DICT || type == VAR_LIST)
25148 {
25149 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25150
25151 if (etv == NULL)
25152 /* Failed to parse back the dict or list, use it as a
25153 * string. */
25154 tv.v_type = VAR_STRING;
25155 else
25156 {
25157 vim_free(tv.vval.v_string);
25158 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025159 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025160 }
25161 }
25162
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025163 /* when in a function use global variables */
25164 save_funccal = current_funccal;
25165 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025166 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025167 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025168
25169 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025170 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025171 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25172 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025173 }
25174 }
25175 }
25176
25177 return viminfo_readline(virp);
25178}
25179
25180/*
25181 * Write global vars that start with a capital to the viminfo file
25182 */
25183 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025184write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185{
Bram Moolenaar33570922005-01-25 22:26:29 +000025186 hashitem_T *hi;
25187 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025188 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025189 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025190 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025191 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025192 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193
25194 if (find_viminfo_parameter('!') == NULL)
25195 return;
25196
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025197 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025198
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025199 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025200 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025202 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025203 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025204 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025205 this_var = HI2DI(hi);
25206 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025207 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025208 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025209 {
25210 case VAR_STRING: s = "STR"; break;
25211 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025212 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025213 case VAR_DICT: s = "DIC"; break;
25214 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025215 case VAR_SPECIAL: s = "XPL"; break;
25216
25217 case VAR_UNKNOWN:
25218 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025219 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025220 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025221 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025222 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025223 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025224 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025225 if (p != NULL)
25226 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025227 vim_free(tofree);
25228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025229 }
25230 }
25231}
25232#endif
25233
25234#if defined(FEAT_SESSION) || defined(PROTO)
25235 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025236store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025237{
Bram Moolenaar33570922005-01-25 22:26:29 +000025238 hashitem_T *hi;
25239 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025240 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025241 char_u *p, *t;
25242
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025243 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025244 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025246 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025247 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025248 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025249 this_var = HI2DI(hi);
25250 if ((this_var->di_tv.v_type == VAR_NUMBER
25251 || this_var->di_tv.v_type == VAR_STRING)
25252 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025253 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025254 /* Escape special characters with a backslash. Turn a LF and
25255 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025256 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025257 (char_u *)"\\\"\n\r");
25258 if (p == NULL) /* out of memory */
25259 break;
25260 for (t = p; *t != NUL; ++t)
25261 if (*t == '\n')
25262 *t = 'n';
25263 else if (*t == '\r')
25264 *t = 'r';
25265 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025266 this_var->di_key,
25267 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25268 : ' ',
25269 p,
25270 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25271 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025272 || put_eol(fd) == FAIL)
25273 {
25274 vim_free(p);
25275 return FAIL;
25276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277 vim_free(p);
25278 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025279#ifdef FEAT_FLOAT
25280 else if (this_var->di_tv.v_type == VAR_FLOAT
25281 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25282 {
25283 float_T f = this_var->di_tv.vval.v_float;
25284 int sign = ' ';
25285
25286 if (f < 0)
25287 {
25288 f = -f;
25289 sign = '-';
25290 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025291 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025292 this_var->di_key, sign, f) < 0)
25293 || put_eol(fd) == FAIL)
25294 return FAIL;
25295 }
25296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025297 }
25298 }
25299 return OK;
25300}
25301#endif
25302
Bram Moolenaar661b1822005-07-28 22:36:45 +000025303/*
25304 * Display script name where an item was last set.
25305 * Should only be invoked when 'verbose' is non-zero.
25306 */
25307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025308last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025309{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025310 char_u *p;
25311
Bram Moolenaar661b1822005-07-28 22:36:45 +000025312 if (scriptID != 0)
25313 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025314 p = home_replace_save(NULL, get_scriptname(scriptID));
25315 if (p != NULL)
25316 {
25317 verbose_enter();
25318 MSG_PUTS(_("\n\tLast set from "));
25319 MSG_PUTS(p);
25320 vim_free(p);
25321 verbose_leave();
25322 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025323 }
25324}
25325
Bram Moolenaard812df62008-11-09 12:46:09 +000025326/*
25327 * List v:oldfiles in a nice way.
25328 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025329 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025330ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025331{
25332 list_T *l = vimvars[VV_OLDFILES].vv_list;
25333 listitem_T *li;
25334 int nr = 0;
25335
25336 if (l == NULL)
25337 msg((char_u *)_("No old files"));
25338 else
25339 {
25340 msg_start();
25341 msg_scroll = TRUE;
25342 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25343 {
25344 msg_outnum((long)++nr);
25345 MSG_PUTS(": ");
25346 msg_outtrans(get_tv_string(&li->li_tv));
25347 msg_putchar('\n');
25348 out_flush(); /* output one line at a time */
25349 ui_breakcheck();
25350 }
25351 /* Assume "got_int" was set to truncate the listing. */
25352 got_int = FALSE;
25353
25354#ifdef FEAT_BROWSE_CMD
25355 if (cmdmod.browse)
25356 {
25357 quit_more = FALSE;
25358 nr = prompt_for_number(FALSE);
25359 msg_starthere();
25360 if (nr > 0)
25361 {
25362 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25363 (long)nr);
25364
25365 if (p != NULL)
25366 {
25367 p = expand_env_save(p);
25368 eap->arg = p;
25369 eap->cmdidx = CMD_edit;
25370 cmdmod.browse = FALSE;
25371 do_exedit(eap, NULL);
25372 vim_free(p);
25373 }
25374 }
25375 }
25376#endif
25377 }
25378}
25379
Bram Moolenaar53744302015-07-17 17:38:22 +020025380/* reset v:option_new, v:option_old and v:option_type */
25381 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025382reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025383{
25384 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25385 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25386 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25387}
25388
25389
Bram Moolenaar071d4272004-06-13 20:20:40 +000025390#endif /* FEAT_EVAL */
25391
Bram Moolenaar071d4272004-06-13 20:20:40 +000025392
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025393#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025394
25395#ifdef WIN3264
25396/*
25397 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25398 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025399static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25400static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25401static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025402
25403/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025404 * Get the short path (8.3) for the filename in "fnamep".
25405 * Only works for a valid file name.
25406 * When the path gets longer "fnamep" is changed and the allocated buffer
25407 * is put in "bufp".
25408 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25409 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025410 */
25411 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025412get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025413{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025414 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025415 char_u *newbuf;
25416
25417 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025418 l = GetShortPathName(*fnamep, *fnamep, len);
25419 if (l > len - 1)
25420 {
25421 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025422 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025423 newbuf = vim_strnsave(*fnamep, l);
25424 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025425 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426
25427 vim_free(*bufp);
25428 *fnamep = *bufp = newbuf;
25429
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025430 /* Really should always succeed, as the buffer is big enough. */
25431 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025432 }
25433
25434 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025435 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025436}
25437
25438/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025439 * Get the short path (8.3) for the filename in "fname". The converted
25440 * path is returned in "bufp".
25441 *
25442 * Some of the directories specified in "fname" may not exist. This function
25443 * will shorten the existing directories at the beginning of the path and then
25444 * append the remaining non-existing path.
25445 *
25446 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025447 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025448 * bufp - Pointer to an allocated buffer for the filename.
25449 * fnamelen - Length of the filename pointed to by fname
25450 *
25451 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025452 */
25453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025454shortpath_for_invalid_fname(
25455 char_u **fname,
25456 char_u **bufp,
25457 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025459 char_u *short_fname, *save_fname, *pbuf_unused;
25460 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025462 int old_len, len;
25463 int new_len, sfx_len;
25464 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025465
25466 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025467 old_len = *fnamelen;
25468 save_fname = vim_strnsave(*fname, old_len);
25469 pbuf_unused = NULL;
25470 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025472 endp = save_fname + old_len - 1; /* Find the end of the copy */
25473 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025475 /*
25476 * Try shortening the supplied path till it succeeds by removing one
25477 * directory at a time from the tail of the path.
25478 */
25479 len = 0;
25480 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025482 /* go back one path-separator */
25483 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25484 --endp;
25485 if (endp <= save_fname)
25486 break; /* processed the complete path */
25487
25488 /*
25489 * Replace the path separator with a NUL and try to shorten the
25490 * resulting path.
25491 */
25492 ch = *endp;
25493 *endp = 0;
25494 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025495 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025496 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25497 {
25498 retval = FAIL;
25499 goto theend;
25500 }
25501 *endp = ch; /* preserve the string */
25502
25503 if (len > 0)
25504 break; /* successfully shortened the path */
25505
25506 /* failed to shorten the path. Skip the path separator */
25507 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025508 }
25509
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025510 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025511 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025512 /*
25513 * Succeeded in shortening the path. Now concatenate the shortened
25514 * path with the remaining path at the tail.
25515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025517 /* Compute the length of the new path. */
25518 sfx_len = (int)(save_endp - endp) + 1;
25519 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025520
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025521 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025523 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025524 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025525 /* There is not enough space in the currently allocated string,
25526 * copy it to a buffer big enough. */
25527 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025528 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025529 {
25530 retval = FAIL;
25531 goto theend;
25532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025533 }
25534 else
25535 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025536 /* Transfer short_fname to the main buffer (it's big enough),
25537 * unless get_short_pathname() did its work in-place. */
25538 *fname = *bufp = save_fname;
25539 if (short_fname != save_fname)
25540 vim_strncpy(save_fname, short_fname, len);
25541 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025542 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025543
25544 /* concat the not-shortened part of the path */
25545 vim_strncpy(*fname + len, endp, sfx_len);
25546 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025547 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025548
25549theend:
25550 vim_free(pbuf_unused);
25551 vim_free(save_fname);
25552
25553 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025554}
25555
25556/*
25557 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025558 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559 */
25560 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025561shortpath_for_partial(
25562 char_u **fnamep,
25563 char_u **bufp,
25564 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565{
25566 int sepcount, len, tflen;
25567 char_u *p;
25568 char_u *pbuf, *tfname;
25569 int hasTilde;
25570
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025571 /* Count up the path separators from the RHS.. so we know which part
25572 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025573 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025574 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025575 if (vim_ispathsep(*p))
25576 ++sepcount;
25577
25578 /* Need full path first (use expand_env() to remove a "~/") */
25579 hasTilde = (**fnamep == '~');
25580 if (hasTilde)
25581 pbuf = tfname = expand_env_save(*fnamep);
25582 else
25583 pbuf = tfname = FullName_save(*fnamep, FALSE);
25584
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025585 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025587 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25588 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025589
25590 if (len == 0)
25591 {
25592 /* Don't have a valid filename, so shorten the rest of the
25593 * path if we can. This CAN give us invalid 8.3 filenames, but
25594 * there's not a lot of point in guessing what it might be.
25595 */
25596 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025597 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25598 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025599 }
25600
25601 /* Count the paths backward to find the beginning of the desired string. */
25602 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025603 {
25604#ifdef FEAT_MBYTE
25605 if (has_mbyte)
25606 p -= mb_head_off(tfname, p);
25607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608 if (vim_ispathsep(*p))
25609 {
25610 if (sepcount == 0 || (hasTilde && sepcount == 1))
25611 break;
25612 else
25613 sepcount --;
25614 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616 if (hasTilde)
25617 {
25618 --p;
25619 if (p >= tfname)
25620 *p = '~';
25621 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025622 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623 }
25624 else
25625 ++p;
25626
25627 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25628 vim_free(*bufp);
25629 *fnamelen = (int)STRLEN(p);
25630 *bufp = pbuf;
25631 *fnamep = p;
25632
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025633 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025634}
25635#endif /* WIN3264 */
25636
25637/*
25638 * Adjust a filename, according to a string of modifiers.
25639 * *fnamep must be NUL terminated when called. When returning, the length is
25640 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025641 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025642 * When there is an error, *fnamep is set to NULL.
25643 */
25644 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025645modify_fname(
25646 char_u *src, /* string with modifiers */
25647 int *usedlen, /* characters after src that are used */
25648 char_u **fnamep, /* file name so far */
25649 char_u **bufp, /* buffer for allocated file name or NULL */
25650 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025651{
25652 int valid = 0;
25653 char_u *tail;
25654 char_u *s, *p, *pbuf;
25655 char_u dirname[MAXPATHL];
25656 int c;
25657 int has_fullname = 0;
25658#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025659 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025660 int has_shortname = 0;
25661#endif
25662
25663repeat:
25664 /* ":p" - full path/file_name */
25665 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25666 {
25667 has_fullname = 1;
25668
25669 valid |= VALID_PATH;
25670 *usedlen += 2;
25671
25672 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25673 if ((*fnamep)[0] == '~'
25674#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25675 && ((*fnamep)[1] == '/'
25676# ifdef BACKSLASH_IN_FILENAME
25677 || (*fnamep)[1] == '\\'
25678# endif
25679 || (*fnamep)[1] == NUL)
25680
25681#endif
25682 )
25683 {
25684 *fnamep = expand_env_save(*fnamep);
25685 vim_free(*bufp); /* free any allocated file name */
25686 *bufp = *fnamep;
25687 if (*fnamep == NULL)
25688 return -1;
25689 }
25690
25691 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025692 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025693 {
25694 if (vim_ispathsep(*p)
25695 && p[1] == '.'
25696 && (p[2] == NUL
25697 || vim_ispathsep(p[2])
25698 || (p[2] == '.'
25699 && (p[3] == NUL || vim_ispathsep(p[3])))))
25700 break;
25701 }
25702
25703 /* FullName_save() is slow, don't use it when not needed. */
25704 if (*p != NUL || !vim_isAbsName(*fnamep))
25705 {
25706 *fnamep = FullName_save(*fnamep, *p != NUL);
25707 vim_free(*bufp); /* free any allocated file name */
25708 *bufp = *fnamep;
25709 if (*fnamep == NULL)
25710 return -1;
25711 }
25712
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025713#ifdef WIN3264
25714# if _WIN32_WINNT >= 0x0500
25715 if (vim_strchr(*fnamep, '~') != NULL)
25716 {
25717 /* Expand 8.3 filename to full path. Needed to make sure the same
25718 * file does not have two different names.
25719 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25720 p = alloc(_MAX_PATH + 1);
25721 if (p != NULL)
25722 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025723 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025724 {
25725 vim_free(*bufp);
25726 *bufp = *fnamep = p;
25727 }
25728 else
25729 vim_free(p);
25730 }
25731 }
25732# endif
25733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025734 /* Append a path separator to a directory. */
25735 if (mch_isdir(*fnamep))
25736 {
25737 /* Make room for one or two extra characters. */
25738 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25739 vim_free(*bufp); /* free any allocated file name */
25740 *bufp = *fnamep;
25741 if (*fnamep == NULL)
25742 return -1;
25743 add_pathsep(*fnamep);
25744 }
25745 }
25746
25747 /* ":." - path relative to the current directory */
25748 /* ":~" - path relative to the home directory */
25749 /* ":8" - shortname path - postponed till after */
25750 while (src[*usedlen] == ':'
25751 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25752 {
25753 *usedlen += 2;
25754 if (c == '8')
25755 {
25756#ifdef WIN3264
25757 has_shortname = 1; /* Postpone this. */
25758#endif
25759 continue;
25760 }
25761 pbuf = NULL;
25762 /* Need full path first (use expand_env() to remove a "~/") */
25763 if (!has_fullname)
25764 {
25765 if (c == '.' && **fnamep == '~')
25766 p = pbuf = expand_env_save(*fnamep);
25767 else
25768 p = pbuf = FullName_save(*fnamep, FALSE);
25769 }
25770 else
25771 p = *fnamep;
25772
25773 has_fullname = 0;
25774
25775 if (p != NULL)
25776 {
25777 if (c == '.')
25778 {
25779 mch_dirname(dirname, MAXPATHL);
25780 s = shorten_fname(p, dirname);
25781 if (s != NULL)
25782 {
25783 *fnamep = s;
25784 if (pbuf != NULL)
25785 {
25786 vim_free(*bufp); /* free any allocated file name */
25787 *bufp = pbuf;
25788 pbuf = NULL;
25789 }
25790 }
25791 }
25792 else
25793 {
25794 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25795 /* Only replace it when it starts with '~' */
25796 if (*dirname == '~')
25797 {
25798 s = vim_strsave(dirname);
25799 if (s != NULL)
25800 {
25801 *fnamep = s;
25802 vim_free(*bufp);
25803 *bufp = s;
25804 }
25805 }
25806 }
25807 vim_free(pbuf);
25808 }
25809 }
25810
25811 tail = gettail(*fnamep);
25812 *fnamelen = (int)STRLEN(*fnamep);
25813
25814 /* ":h" - head, remove "/file_name", can be repeated */
25815 /* Don't remove the first "/" or "c:\" */
25816 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25817 {
25818 valid |= VALID_HEAD;
25819 *usedlen += 2;
25820 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025821 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025822 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025823 *fnamelen = (int)(tail - *fnamep);
25824#ifdef VMS
25825 if (*fnamelen > 0)
25826 *fnamelen += 1; /* the path separator is part of the path */
25827#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025828 if (*fnamelen == 0)
25829 {
25830 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25831 p = vim_strsave((char_u *)".");
25832 if (p == NULL)
25833 return -1;
25834 vim_free(*bufp);
25835 *bufp = *fnamep = tail = p;
25836 *fnamelen = 1;
25837 }
25838 else
25839 {
25840 while (tail > s && !after_pathsep(s, tail))
25841 mb_ptr_back(*fnamep, tail);
25842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025843 }
25844
25845 /* ":8" - shortname */
25846 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25847 {
25848 *usedlen += 2;
25849#ifdef WIN3264
25850 has_shortname = 1;
25851#endif
25852 }
25853
25854#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025855 /*
25856 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025857 */
25858 if (has_shortname)
25859 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025860 /* Copy the string if it is shortened by :h and when it wasn't copied
25861 * yet, because we are going to change it in place. Avoids changing
25862 * the buffer name for "%:8". */
25863 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864 {
25865 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025866 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025867 return -1;
25868 vim_free(*bufp);
25869 *bufp = *fnamep = p;
25870 }
25871
25872 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025873 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025874 if (!has_fullname && !vim_isAbsName(*fnamep))
25875 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025876 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025877 return -1;
25878 }
25879 else
25880 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025881 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025882
Bram Moolenaardc935552011-08-17 15:23:23 +020025883 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025884 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025885 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025886 return -1;
25887
25888 if (l == 0)
25889 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025890 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025891 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025892 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025893 return -1;
25894 }
25895 *fnamelen = l;
25896 }
25897 }
25898#endif /* WIN3264 */
25899
25900 /* ":t" - tail, just the basename */
25901 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25902 {
25903 *usedlen += 2;
25904 *fnamelen -= (int)(tail - *fnamep);
25905 *fnamep = tail;
25906 }
25907
25908 /* ":e" - extension, can be repeated */
25909 /* ":r" - root, without extension, can be repeated */
25910 while (src[*usedlen] == ':'
25911 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25912 {
25913 /* find a '.' in the tail:
25914 * - for second :e: before the current fname
25915 * - otherwise: The last '.'
25916 */
25917 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25918 s = *fnamep - 2;
25919 else
25920 s = *fnamep + *fnamelen - 1;
25921 for ( ; s > tail; --s)
25922 if (s[0] == '.')
25923 break;
25924 if (src[*usedlen + 1] == 'e') /* :e */
25925 {
25926 if (s > tail)
25927 {
25928 *fnamelen += (int)(*fnamep - (s + 1));
25929 *fnamep = s + 1;
25930#ifdef VMS
25931 /* cut version from the extension */
25932 s = *fnamep + *fnamelen - 1;
25933 for ( ; s > *fnamep; --s)
25934 if (s[0] == ';')
25935 break;
25936 if (s > *fnamep)
25937 *fnamelen = s - *fnamep;
25938#endif
25939 }
25940 else if (*fnamep <= tail)
25941 *fnamelen = 0;
25942 }
25943 else /* :r */
25944 {
25945 if (s > tail) /* remove one extension */
25946 *fnamelen = (int)(s - *fnamep);
25947 }
25948 *usedlen += 2;
25949 }
25950
25951 /* ":s?pat?foo?" - substitute */
25952 /* ":gs?pat?foo?" - global substitute */
25953 if (src[*usedlen] == ':'
25954 && (src[*usedlen + 1] == 's'
25955 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25956 {
25957 char_u *str;
25958 char_u *pat;
25959 char_u *sub;
25960 int sep;
25961 char_u *flags;
25962 int didit = FALSE;
25963
25964 flags = (char_u *)"";
25965 s = src + *usedlen + 2;
25966 if (src[*usedlen + 1] == 'g')
25967 {
25968 flags = (char_u *)"g";
25969 ++s;
25970 }
25971
25972 sep = *s++;
25973 if (sep)
25974 {
25975 /* find end of pattern */
25976 p = vim_strchr(s, sep);
25977 if (p != NULL)
25978 {
25979 pat = vim_strnsave(s, (int)(p - s));
25980 if (pat != NULL)
25981 {
25982 s = p + 1;
25983 /* find end of substitution */
25984 p = vim_strchr(s, sep);
25985 if (p != NULL)
25986 {
25987 sub = vim_strnsave(s, (int)(p - s));
25988 str = vim_strnsave(*fnamep, *fnamelen);
25989 if (sub != NULL && str != NULL)
25990 {
25991 *usedlen = (int)(p + 1 - src);
25992 s = do_string_sub(str, pat, sub, flags);
25993 if (s != NULL)
25994 {
25995 *fnamep = s;
25996 *fnamelen = (int)STRLEN(s);
25997 vim_free(*bufp);
25998 *bufp = s;
25999 didit = TRUE;
26000 }
26001 }
26002 vim_free(sub);
26003 vim_free(str);
26004 }
26005 vim_free(pat);
26006 }
26007 }
26008 /* after using ":s", repeat all the modifiers */
26009 if (didit)
26010 goto repeat;
26011 }
26012 }
26013
Bram Moolenaar26df0922014-02-23 23:39:13 +010026014 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26015 {
26016 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26017 if (p == NULL)
26018 return -1;
26019 vim_free(*bufp);
26020 *bufp = *fnamep = p;
26021 *fnamelen = (int)STRLEN(p);
26022 *usedlen += 2;
26023 }
26024
Bram Moolenaar071d4272004-06-13 20:20:40 +000026025 return valid;
26026}
26027
26028/*
26029 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26030 * "flags" can be "g" to do a global substitute.
26031 * Returns an allocated string, NULL for error.
26032 */
26033 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026034do_string_sub(
26035 char_u *str,
26036 char_u *pat,
26037 char_u *sub,
26038 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026039{
26040 int sublen;
26041 regmatch_T regmatch;
26042 int i;
26043 int do_all;
26044 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026045 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026046 garray_T ga;
26047 char_u *ret;
26048 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026049 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026050
26051 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26052 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026053 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026054
26055 ga_init2(&ga, 1, 200);
26056
26057 do_all = (flags[0] == 'g');
26058
26059 regmatch.rm_ic = p_ic;
26060 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26061 if (regmatch.regprog != NULL)
26062 {
26063 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026064 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026065 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26066 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026067 /* Skip empty match except for first match. */
26068 if (regmatch.startp[0] == regmatch.endp[0])
26069 {
26070 if (zero_width == regmatch.startp[0])
26071 {
26072 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026073 i = MB_PTR2LEN(tail);
26074 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26075 (size_t)i);
26076 ga.ga_len += i;
26077 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026078 continue;
26079 }
26080 zero_width = regmatch.startp[0];
26081 }
26082
Bram Moolenaar071d4272004-06-13 20:20:40 +000026083 /*
26084 * Get some space for a temporary buffer to do the substitution
26085 * into. It will contain:
26086 * - The text up to where the match is.
26087 * - The substituted text.
26088 * - The text after the match.
26089 */
26090 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026091 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026092 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26093 {
26094 ga_clear(&ga);
26095 break;
26096 }
26097
26098 /* copy the text up to where the match is */
26099 i = (int)(regmatch.startp[0] - tail);
26100 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26101 /* add the substituted text */
26102 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26103 + ga.ga_len + i, TRUE, TRUE, FALSE);
26104 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026105 tail = regmatch.endp[0];
26106 if (*tail == NUL)
26107 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026108 if (!do_all)
26109 break;
26110 }
26111
26112 if (ga.ga_data != NULL)
26113 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26114
Bram Moolenaar473de612013-06-08 18:19:48 +020026115 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026116 }
26117
26118 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26119 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026120 if (p_cpo == empty_option)
26121 p_cpo = save_cpo;
26122 else
26123 /* Darn, evaluating {sub} expression changed the value. */
26124 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026125
26126 return ret;
26127}
26128
26129#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */