blob: efabc08ea75b473e582889e7af73524bc9040999 [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.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007538 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007539 */
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 Moolenaarcd39bbc2016-02-17 10:05:42 +01009853#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009854/*
9855 * Get a callback from "arg". It can be a Funcref or a function name.
9856 * When "arg" is zero return an empty string.
9857 * Return NULL for an invalid argument.
9858 */
9859 static char_u *
9860get_callback(typval_T *arg)
9861{
9862 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9863 return arg->vval.v_string;
9864 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9865 return (char_u *)"";
9866 EMSG(_("E999: Invalid callback argument"));
9867 return NULL;
9868}
9869
9870/*
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009871 * Get the option entries from "dict", and parse them.
9872 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009873 */
9874 static int
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009875get_job_options(dict_T *dict, jobopt_T *opt)
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009876{
9877 dictitem_T *item;
9878 char_u *mode;
9879
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009880 if (dict == NULL)
9881 return OK;
9882
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009883 if ((item = dict_find(dict, (char_u *)"mode", -1)) != NULL)
9884 {
9885 mode = get_tv_string(&item->di_tv);
9886 if (STRCMP(mode, "nl") == 0)
9887 opt->jo_mode = MODE_NL;
9888 else if (STRCMP(mode, "raw") == 0)
9889 opt->jo_mode = MODE_RAW;
9890 else if (STRCMP(mode, "js") == 0)
9891 opt->jo_mode = MODE_JS;
9892 else if (STRCMP(mode, "json") == 0)
9893 opt->jo_mode = MODE_JSON;
9894 else
9895 {
9896 EMSG2(_(e_invarg2), mode);
9897 return FAIL;
9898 }
9899 }
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009900
9901 if ((item = dict_find(dict, (char_u *)"callback", -1)) != NULL)
9902 {
9903 opt->jo_callback = get_callback(&item->di_tv);
9904 if (opt->jo_callback == NULL)
9905 {
9906 EMSG2(_(e_invarg2), "callback");
9907 return FAIL;
9908 }
9909 }
9910
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009911 return OK;
9912}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009913#endif
9914
9915#ifdef FEAT_CHANNEL
9916/*
9917 * Get the channel from the argument.
9918 * Returns NULL if the handle is invalid.
9919 */
9920 static channel_T *
9921get_channel_arg(typval_T *tv)
9922{
9923 channel_T *channel;
9924
9925 if (tv->v_type != VAR_CHANNEL)
9926 {
9927 EMSG2(_(e_invarg2), get_tv_string(tv));
9928 return NULL;
9929 }
9930 channel = tv->vval.v_channel;
9931
9932 if (channel == NULL || !channel_is_open(channel))
9933 {
9934 EMSG(_("E906: not an open channel"));
9935 return NULL;
9936 }
9937 return channel;
9938}
9939
9940/*
9941 * "ch_close()" function
9942 */
9943 static void
9944f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9945{
9946 channel_T *channel = get_channel_arg(&argvars[0]);
9947
9948 if (channel != NULL)
9949 channel_close(channel);
9950}
9951
9952/*
9953 * "ch_logfile()" function
9954 */
9955 static void
9956f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
9957{
9958 char_u *fname;
9959 char_u *opt = (char_u *)"";
9960 char_u buf[NUMBUFLEN];
9961 FILE *file = NULL;
9962
9963 fname = get_tv_string(&argvars[0]);
9964 if (argvars[1].v_type == VAR_STRING)
9965 opt = get_tv_string_buf(&argvars[1], buf);
9966 if (*fname != NUL)
9967 {
9968 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
9969 if (file == NULL)
9970 {
9971 EMSG2(_(e_notopen), fname);
9972 return;
9973 }
9974 }
9975 ch_logfile(file);
9976}
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009977
9978/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009979 * "ch_open()" function
9980 */
9981 static void
9982f_ch_open(typval_T *argvars, typval_T *rettv)
9983{
9984 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009985 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009986 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009987 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009988 int waittime = 0;
9989 int timeout = 2000;
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009990 jobopt_T options;
Bram Moolenaar77073442016-02-13 23:23:53 +01009991 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009992
9993 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +01009994 rettv->v_type = VAR_CHANNEL;
9995 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009996
9997 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009998 if (argvars[1].v_type != VAR_UNKNOWN
9999 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010000 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010001 EMSG(_(e_invarg));
10002 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010003 }
10004
10005 /* parse address */
10006 p = vim_strchr(address, ':');
10007 if (p == NULL)
10008 {
10009 EMSG2(_(e_invarg2), address);
10010 return;
10011 }
10012 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010013 port = strtol((char *)p, &rest, 10);
10014 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010015 {
10016 p[-1] = ':';
10017 EMSG2(_(e_invarg2), address);
10018 return;
10019 }
10020
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010021 options.jo_mode = MODE_JSON;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010022 options.jo_callback = NULL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010023 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010024 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +010010025 dict_T *dict = argvars[1].vval.v_dict;
10026 dictitem_T *item;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010027
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +010010028 /* parse argdict */
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010029 if (get_job_options(dict, &options) == FAIL)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010030 return;
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +010010031 if ((item = dict_find(dict, (char_u *)"waittime", -1)) != NULL)
10032 waittime = get_tv_number(&item->di_tv);
10033 if ((item = dict_find(dict, (char_u *)"timeout", -1)) != NULL)
10034 timeout = get_tv_number(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010035 }
Bram Moolenaare74e8e72016-02-16 22:01:30 +010010036 if (timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010037 {
10038 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010039 return;
10040 }
10041
Bram Moolenaar77073442016-02-13 23:23:53 +010010042 channel = channel_open((char *)address, port, waittime, NULL);
10043 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010044 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010045 rettv->vval.v_channel = channel;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010046 channel_set_options(channel, &options);
Bram Moolenaar77073442016-02-13 23:23:53 +010010047 channel_set_timeout(channel, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010048 }
10049}
10050
10051/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010052 * "ch_readraw()" function
10053 */
10054 static void
10055f_ch_readraw(typval_T *argvars, typval_T *rettv)
10056{
Bram Moolenaar77073442016-02-13 23:23:53 +010010057 channel_T *channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010058
10059 /* return an empty string by default */
10060 rettv->v_type = VAR_STRING;
10061 rettv->vval.v_string = NULL;
10062
Bram Moolenaar77073442016-02-13 23:23:53 +010010063 channel = get_channel_arg(&argvars[0]);
10064 if (channel != NULL)
10065 rettv->vval.v_string = channel_read_block(channel);
10066}
10067
10068/*
10069 * "ch_status()" function
10070 */
10071 static void
10072f_ch_status(typval_T *argvars, typval_T *rettv)
10073{
10074 /* return an empty string by default */
10075 rettv->v_type = VAR_STRING;
10076
10077 if (argvars[0].v_type != VAR_CHANNEL)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010078 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010079 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10080 rettv->vval.v_string = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010081 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010082 else
10083 rettv->vval.v_string = vim_strsave(
10084 (char_u *)channel_status(argvars[0].vval.v_channel));
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010085}
10086
10087/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010088 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010089 * Returns the channel if the caller should read the response.
10090 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010091 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010092 static channel_T *
Bram Moolenaara07fec92016-02-05 21:04:08 +010010093send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010094{
Bram Moolenaar77073442016-02-13 23:23:53 +010010095 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010096 char_u *callback = NULL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010097 jobopt_T options;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010098
Bram Moolenaar77073442016-02-13 23:23:53 +010010099 channel = get_channel_arg(&argvars[0]);
10100 if (channel == NULL)
10101 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010102
10103 if (argvars[2].v_type != VAR_UNKNOWN)
10104 {
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010105 if (argvars[2].v_type != VAR_DICT)
10106 {
10107 EMSG(_(e_invarg));
Bram Moolenaar77073442016-02-13 23:23:53 +010010108 return NULL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010109 }
10110 options.jo_callback = NULL;
10111 if (get_job_options(argvars[2].vval.v_dict, &options) == FAIL)
10112 return NULL;
10113 callback = options.jo_callback;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010114 }
Bram Moolenaara07fec92016-02-05 21:04:08 +010010115 /* Set the callback. An empty callback means no callback and not reading
10116 * the response. */
10117 if (callback != NULL && *callback != NUL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010118 channel_set_req_callback(channel, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010119
Bram Moolenaar77073442016-02-13 23:23:53 +010010120 if (channel_send(channel, text, fun) == OK && callback == NULL)
10121 return channel;
10122 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010123}
10124
10125/*
10126 * "ch_sendexpr()" function
10127 */
10128 static void
10129f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10130{
10131 char_u *text;
10132 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010133 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010134 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010135 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010136
10137 /* return an empty string by default */
10138 rettv->v_type = VAR_STRING;
10139 rettv->vval.v_string = NULL;
10140
Bram Moolenaar77073442016-02-13 23:23:53 +010010141 channel = get_channel_arg(&argvars[0]);
10142 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010143 return;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010144
Bram Moolenaar77073442016-02-13 23:23:53 +010010145 ch_mode = channel_get_mode(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010146 if (ch_mode == MODE_RAW)
10147 {
10148 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
10149 return;
10150 }
10151
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010152 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010153 text = json_encode_nr_expr(id, &argvars[1],
10154 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010155 if (text == NULL)
10156 return;
10157
Bram Moolenaar77073442016-02-13 23:23:53 +010010158 channel = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010159 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010160 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010161 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010162 if (channel_read_json_block(channel, id, &listtv) == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010163 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010164 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010165
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010166 /* Move the item from the list and then change the type to
10167 * avoid the value being freed. */
10168 *rettv = list->lv_last->li_tv;
10169 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010170 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010171 }
10172 }
10173}
10174
10175/*
10176 * "ch_sendraw()" function
10177 */
10178 static void
10179f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10180{
10181 char_u buf[NUMBUFLEN];
10182 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010183 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010184
10185 /* return an empty string by default */
10186 rettv->v_type = VAR_STRING;
10187 rettv->vval.v_string = NULL;
10188
10189 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar77073442016-02-13 23:23:53 +010010190 channel = send_common(argvars, text, 0, "sendraw");
10191 if (channel != NULL)
10192 rettv->vval.v_string = channel_read_block(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010193}
10194#endif
10195
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010196/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010197 * "changenr()" function
10198 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010200f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010201{
10202 rettv->vval.v_number = curbuf->b_u_seq_cur;
10203}
10204
10205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 * "char2nr(string)" function
10207 */
10208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010209f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210{
10211#ifdef FEAT_MBYTE
10212 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010213 {
10214 int utf8 = 0;
10215
10216 if (argvars[1].v_type != VAR_UNKNOWN)
10217 utf8 = get_tv_number_chk(&argvars[1], NULL);
10218
10219 if (utf8)
10220 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10221 else
10222 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 else
10225#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010226 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227}
10228
10229/*
10230 * "cindent(lnum)" function
10231 */
10232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010233f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234{
10235#ifdef FEAT_CINDENT
10236 pos_T pos;
10237 linenr_T lnum;
10238
10239 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010240 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10242 {
10243 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010244 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 curwin->w_cursor = pos;
10246 }
10247 else
10248#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010249 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250}
10251
10252/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010253 * "clearmatches()" function
10254 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010256f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010257{
10258#ifdef FEAT_SEARCH_EXTRA
10259 clear_matches(curwin);
10260#endif
10261}
10262
10263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 * "col(string)" function
10265 */
10266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010267f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268{
10269 colnr_T col = 0;
10270 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010271 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010273 fp = var2fpos(&argvars[0], FALSE, &fnum);
10274 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 {
10276 if (fp->col == MAXCOL)
10277 {
10278 /* '> can be MAXCOL, get the length of the line then */
10279 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010280 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 else
10282 col = MAXCOL;
10283 }
10284 else
10285 {
10286 col = fp->col + 1;
10287#ifdef FEAT_VIRTUALEDIT
10288 /* col(".") when the cursor is on the NUL at the end of the line
10289 * because of "coladd" can be seen as an extra column. */
10290 if (virtual_active() && fp == &curwin->w_cursor)
10291 {
10292 char_u *p = ml_get_cursor();
10293
10294 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10295 curwin->w_virtcol - curwin->w_cursor.coladd))
10296 {
10297# ifdef FEAT_MBYTE
10298 int l;
10299
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010300 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 col += l;
10302# else
10303 if (*p != NUL && p[1] == NUL)
10304 ++col;
10305# endif
10306 }
10307 }
10308#endif
10309 }
10310 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010311 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312}
10313
Bram Moolenaar572cb562005-08-05 21:35:02 +000010314#if defined(FEAT_INS_EXPAND)
10315/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010316 * "complete()" function
10317 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010319f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010320{
10321 int startcol;
10322
10323 if ((State & INSERT) == 0)
10324 {
10325 EMSG(_("E785: complete() can only be used in Insert mode"));
10326 return;
10327 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010328
10329 /* Check for undo allowed here, because if something was already inserted
10330 * the line was already saved for undo and this check isn't done. */
10331 if (!undo_allowed())
10332 return;
10333
Bram Moolenaarade00832006-03-10 21:46:58 +000010334 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10335 {
10336 EMSG(_(e_invarg));
10337 return;
10338 }
10339
10340 startcol = get_tv_number_chk(&argvars[0], NULL);
10341 if (startcol <= 0)
10342 return;
10343
10344 set_completion(startcol - 1, argvars[1].vval.v_list);
10345}
10346
10347/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010348 * "complete_add()" function
10349 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010351f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010352{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010353 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010354}
10355
10356/*
10357 * "complete_check()" function
10358 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010360f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010361{
10362 int saved = RedrawingDisabled;
10363
10364 RedrawingDisabled = 0;
10365 ins_compl_check_keys(0);
10366 rettv->vval.v_number = compl_interrupted;
10367 RedrawingDisabled = saved;
10368}
10369#endif
10370
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371/*
10372 * "confirm(message, buttons[, default [, type]])" function
10373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010375f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376{
10377#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10378 char_u *message;
10379 char_u *buttons = NULL;
10380 char_u buf[NUMBUFLEN];
10381 char_u buf2[NUMBUFLEN];
10382 int def = 1;
10383 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010384 char_u *typestr;
10385 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010387 message = get_tv_string_chk(&argvars[0]);
10388 if (message == NULL)
10389 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010390 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010392 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10393 if (buttons == NULL)
10394 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010395 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010397 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010398 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010400 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10401 if (typestr == NULL)
10402 error = TRUE;
10403 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010405 switch (TOUPPER_ASC(*typestr))
10406 {
10407 case 'E': type = VIM_ERROR; break;
10408 case 'Q': type = VIM_QUESTION; break;
10409 case 'I': type = VIM_INFO; break;
10410 case 'W': type = VIM_WARNING; break;
10411 case 'G': type = VIM_GENERIC; break;
10412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 }
10414 }
10415 }
10416 }
10417
10418 if (buttons == NULL || *buttons == NUL)
10419 buttons = (char_u *)_("&Ok");
10420
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010421 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010422 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010423 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424#endif
10425}
10426
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010427/*
10428 * "copy()" function
10429 */
10430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010431f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010432{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010433 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010434}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010436#ifdef FEAT_FLOAT
10437/*
10438 * "cos()" function
10439 */
10440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010441f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010442{
10443 float_T f;
10444
10445 rettv->v_type = VAR_FLOAT;
10446 if (get_float_arg(argvars, &f) == OK)
10447 rettv->vval.v_float = cos(f);
10448 else
10449 rettv->vval.v_float = 0.0;
10450}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010451
10452/*
10453 * "cosh()" function
10454 */
10455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010456f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010457{
10458 float_T f;
10459
10460 rettv->v_type = VAR_FLOAT;
10461 if (get_float_arg(argvars, &f) == OK)
10462 rettv->vval.v_float = cosh(f);
10463 else
10464 rettv->vval.v_float = 0.0;
10465}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010466#endif
10467
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010469 * "count()" function
10470 */
10471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010472f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010473{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010474 long n = 0;
10475 int ic = FALSE;
10476
Bram Moolenaare9a41262005-01-15 22:18:47 +000010477 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010478 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010479 listitem_T *li;
10480 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010481 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010482
Bram Moolenaare9a41262005-01-15 22:18:47 +000010483 if ((l = argvars[0].vval.v_list) != NULL)
10484 {
10485 li = l->lv_first;
10486 if (argvars[2].v_type != VAR_UNKNOWN)
10487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010488 int error = FALSE;
10489
10490 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010491 if (argvars[3].v_type != VAR_UNKNOWN)
10492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010493 idx = get_tv_number_chk(&argvars[3], &error);
10494 if (!error)
10495 {
10496 li = list_find(l, idx);
10497 if (li == NULL)
10498 EMSGN(_(e_listidx), idx);
10499 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010500 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010501 if (error)
10502 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010503 }
10504
10505 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010506 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010507 ++n;
10508 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010509 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010510 else if (argvars[0].v_type == VAR_DICT)
10511 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010512 int todo;
10513 dict_T *d;
10514 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010515
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010516 if ((d = argvars[0].vval.v_dict) != NULL)
10517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010518 int error = FALSE;
10519
Bram Moolenaare9a41262005-01-15 22:18:47 +000010520 if (argvars[2].v_type != VAR_UNKNOWN)
10521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010522 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010523 if (argvars[3].v_type != VAR_UNKNOWN)
10524 EMSG(_(e_invarg));
10525 }
10526
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010527 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010528 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010529 {
10530 if (!HASHITEM_EMPTY(hi))
10531 {
10532 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010533 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010534 ++n;
10535 }
10536 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010537 }
10538 }
10539 else
10540 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010541 rettv->vval.v_number = n;
10542}
10543
10544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10546 *
10547 * Checks the existence of a cscope connection.
10548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010550f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551{
10552#ifdef FEAT_CSCOPE
10553 int num = 0;
10554 char_u *dbpath = NULL;
10555 char_u *prepend = NULL;
10556 char_u buf[NUMBUFLEN];
10557
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010558 if (argvars[0].v_type != VAR_UNKNOWN
10559 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561 num = (int)get_tv_number(&argvars[0]);
10562 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010563 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010564 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 }
10566
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010567 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568#endif
10569}
10570
10571/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010572 * "cursor(lnum, col)" function, or
10573 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010575 * Moves the cursor to the specified line and column.
10576 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010579f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580{
10581 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010582#ifdef FEAT_VIRTUALEDIT
10583 long coladd = 0;
10584#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010585 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010587 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010588 if (argvars[1].v_type == VAR_UNKNOWN)
10589 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010590 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010591 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010592
Bram Moolenaar493c1782014-05-28 14:34:46 +020010593 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010594 {
10595 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010596 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010597 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010598 line = pos.lnum;
10599 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010600#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010601 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010602#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010603 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010604 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010605 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010606 set_curswant = FALSE;
10607 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010608 }
10609 else
10610 {
10611 line = get_tv_lnum(argvars);
10612 col = get_tv_number_chk(&argvars[1], NULL);
10613#ifdef FEAT_VIRTUALEDIT
10614 if (argvars[2].v_type != VAR_UNKNOWN)
10615 coladd = get_tv_number_chk(&argvars[2], NULL);
10616#endif
10617 }
10618 if (line < 0 || col < 0
10619#ifdef FEAT_VIRTUALEDIT
10620 || coladd < 0
10621#endif
10622 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 if (line > 0)
10625 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 if (col > 0)
10627 curwin->w_cursor.col = col - 1;
10628#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010629 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630#endif
10631
10632 /* Make sure the cursor is in a valid position. */
10633 check_cursor();
10634#ifdef FEAT_MBYTE
10635 /* Correct cursor for multi-byte character. */
10636 if (has_mbyte)
10637 mb_adjust_cursor();
10638#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010639
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010640 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010641 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642}
10643
10644/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010645 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 */
10647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010648f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010650 int noref = 0;
10651
10652 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010653 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010654 if (noref < 0 || noref > 1)
10655 EMSG(_(e_invarg));
10656 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010657 {
10658 current_copyID += COPYID_INC;
10659 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661}
10662
10663/*
10664 * "delete()" function
10665 */
10666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010667f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010669 char_u nbuf[NUMBUFLEN];
10670 char_u *name;
10671 char_u *flags;
10672
10673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010675 return;
10676
10677 name = get_tv_string(&argvars[0]);
10678 if (name == NULL || *name == NUL)
10679 {
10680 EMSG(_(e_invarg));
10681 return;
10682 }
10683
10684 if (argvars[1].v_type != VAR_UNKNOWN)
10685 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010687 flags = (char_u *)"";
10688
10689 if (*flags == NUL)
10690 /* delete a file */
10691 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10692 else if (STRCMP(flags, "d") == 0)
10693 /* delete an empty directory */
10694 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10695 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010696 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010697 rettv->vval.v_number = delete_recursive(name);
10698 else
10699 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700}
10701
10702/*
10703 * "did_filetype()" function
10704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010706f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707{
10708#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010709 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710#endif
10711}
10712
10713/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010714 * "diff_filler()" function
10715 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010717f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010718{
10719#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010720 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010721#endif
10722}
10723
10724/*
10725 * "diff_hlID()" function
10726 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010728f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010729{
10730#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010731 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010732 static linenr_T prev_lnum = 0;
10733 static int changedtick = 0;
10734 static int fnum = 0;
10735 static int change_start = 0;
10736 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010737 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010738 int filler_lines;
10739 int col;
10740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 if (lnum < 0) /* ignore type error in {lnum} arg */
10742 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010743 if (lnum != prev_lnum
10744 || changedtick != curbuf->b_changedtick
10745 || fnum != curbuf->b_fnum)
10746 {
10747 /* New line, buffer, change: need to get the values. */
10748 filler_lines = diff_check(curwin, lnum);
10749 if (filler_lines < 0)
10750 {
10751 if (filler_lines == -1)
10752 {
10753 change_start = MAXCOL;
10754 change_end = -1;
10755 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10756 hlID = HLF_ADD; /* added line */
10757 else
10758 hlID = HLF_CHD; /* changed line */
10759 }
10760 else
10761 hlID = HLF_ADD; /* added line */
10762 }
10763 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010764 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010765 prev_lnum = lnum;
10766 changedtick = curbuf->b_changedtick;
10767 fnum = curbuf->b_fnum;
10768 }
10769
10770 if (hlID == HLF_CHD || hlID == HLF_TXD)
10771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010772 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010773 if (col >= change_start && col <= change_end)
10774 hlID = HLF_TXD; /* changed text */
10775 else
10776 hlID = HLF_CHD; /* changed line */
10777 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010778 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010779#endif
10780}
10781
10782/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010783 * "disable_char_avail_for_testing({expr})" function
10784 */
10785 static void
10786f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10787{
10788 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10789}
10790
10791/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010792 * "empty({expr})" function
10793 */
10794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010795f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010796{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010797 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010798
10799 switch (argvars[0].v_type)
10800 {
10801 case VAR_STRING:
10802 case VAR_FUNC:
10803 n = argvars[0].vval.v_string == NULL
10804 || *argvars[0].vval.v_string == NUL;
10805 break;
10806 case VAR_NUMBER:
10807 n = argvars[0].vval.v_number == 0;
10808 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010809 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010810#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010811 n = argvars[0].vval.v_float == 0.0;
10812 break;
10813#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010814 case VAR_LIST:
10815 n = argvars[0].vval.v_list == NULL
10816 || argvars[0].vval.v_list->lv_first == NULL;
10817 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010818 case VAR_DICT:
10819 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010820 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010821 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010822 case VAR_SPECIAL:
10823 n = argvars[0].vval.v_number != VVAL_TRUE;
10824 break;
10825
Bram Moolenaar835dc632016-02-07 14:27:38 +010010826 case VAR_JOB:
10827#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010010828 n = argvars[0].vval.v_job == NULL
10829 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10830 break;
10831#endif
10832 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010010833#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010834 n = argvars[0].vval.v_channel == NULL
10835 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010836 break;
10837#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010838 case VAR_UNKNOWN:
10839 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10840 n = TRUE;
10841 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010842 }
10843
10844 rettv->vval.v_number = n;
10845}
10846
10847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 * "escape({string}, {chars})" function
10849 */
10850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010851f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852{
10853 char_u buf[NUMBUFLEN];
10854
Bram Moolenaar758711c2005-02-02 23:11:38 +000010855 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10856 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010857 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858}
10859
10860/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010861 * "eval()" function
10862 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010864f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010865{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010866 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010867
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 s = get_tv_string_chk(&argvars[0]);
10869 if (s != NULL)
10870 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010871
Bram Moolenaar615b9972015-01-14 17:15:05 +010010872 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010873 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10874 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010875 if (p != NULL && !aborting())
10876 EMSG2(_(e_invexpr2), p);
10877 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010879 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010880 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010881 else if (*s != NUL)
10882 EMSG(_(e_trailing));
10883}
10884
10885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 * "eventhandler()" function
10887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010889f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010891 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892}
10893
10894/*
10895 * "executable()" function
10896 */
10897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010898f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010900 char_u *name = get_tv_string(&argvars[0]);
10901
10902 /* Check in $PATH and also check directly if there is a directory name. */
10903 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10904 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010905}
10906
10907/*
10908 * "exepath()" function
10909 */
10910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010911f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010912{
10913 char_u *p = NULL;
10914
Bram Moolenaarb5971142015-03-21 17:32:19 +010010915 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010916 rettv->v_type = VAR_STRING;
10917 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918}
10919
10920/*
10921 * "exists()" function
10922 */
10923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010924f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925{
10926 char_u *p;
10927 char_u *name;
10928 int n = FALSE;
10929 int len = 0;
10930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010931 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 if (*p == '$') /* environment variable */
10933 {
10934 /* first try "normal" environment variables (fast) */
10935 if (mch_getenv(p + 1) != NULL)
10936 n = TRUE;
10937 else
10938 {
10939 /* try expanding things like $VIM and ${HOME} */
10940 p = expand_env_save(p);
10941 if (p != NULL && *p != '$')
10942 n = TRUE;
10943 vim_free(p);
10944 }
10945 }
10946 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010948 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010949 if (*skipwhite(p) != NUL)
10950 n = FALSE; /* trailing garbage */
10951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 else if (*p == '*') /* internal or user defined function */
10953 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010954 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 }
10956 else if (*p == ':')
10957 {
10958 n = cmd_exists(p + 1);
10959 }
10960 else if (*p == '#')
10961 {
10962#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010963 if (p[1] == '#')
10964 n = autocmd_supported(p + 2);
10965 else
10966 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967#endif
10968 }
10969 else /* internal variable */
10970 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010971 char_u *tofree;
10972 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010974 /* get_name_len() takes care of expanding curly braces */
10975 name = p;
10976 len = get_name_len(&p, &tofree, TRUE, FALSE);
10977 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010979 if (tofree != NULL)
10980 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010981 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010982 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010984 /* handle d.key, l[idx], f(expr) */
10985 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10986 if (n)
10987 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 }
10989 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010990 if (*p != NUL)
10991 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010993 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 }
10995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010996 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997}
10998
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010999#ifdef FEAT_FLOAT
11000/*
11001 * "exp()" function
11002 */
11003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011004f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011005{
11006 float_T f;
11007
11008 rettv->v_type = VAR_FLOAT;
11009 if (get_float_arg(argvars, &f) == OK)
11010 rettv->vval.v_float = exp(f);
11011 else
11012 rettv->vval.v_float = 0.0;
11013}
11014#endif
11015
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016/*
11017 * "expand()" function
11018 */
11019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011020f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021{
11022 char_u *s;
11023 int len;
11024 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011025 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011027 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011028 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011031 if (argvars[1].v_type != VAR_UNKNOWN
11032 && argvars[2].v_type != VAR_UNKNOWN
11033 && get_tv_number_chk(&argvars[2], &error)
11034 && !error)
11035 {
11036 rettv->v_type = VAR_LIST;
11037 rettv->vval.v_list = NULL;
11038 }
11039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 if (*s == '%' || *s == '#' || *s == '<')
11042 {
11043 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011044 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011046 if (rettv->v_type == VAR_LIST)
11047 {
11048 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11049 list_append_string(rettv->vval.v_list, result, -1);
11050 else
11051 vim_free(result);
11052 }
11053 else
11054 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 }
11056 else
11057 {
11058 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011059 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011060 if (argvars[1].v_type != VAR_UNKNOWN
11061 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011062 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 if (!error)
11064 {
11065 ExpandInit(&xpc);
11066 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011067 if (p_wic)
11068 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011069 if (rettv->v_type == VAR_STRING)
11070 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11071 options, WILD_ALL);
11072 else if (rettv_list_alloc(rettv) != FAIL)
11073 {
11074 int i;
11075
11076 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11077 for (i = 0; i < xpc.xp_numfiles; i++)
11078 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11079 ExpandCleanup(&xpc);
11080 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 }
11082 else
11083 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 }
11085}
11086
11087/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011088 * Go over all entries in "d2" and add them to "d1".
11089 * When "action" is "error" then a duplicate key is an error.
11090 * When "action" is "force" then a duplicate key is overwritten.
11091 * Otherwise duplicate keys are ignored ("action" is "keep").
11092 */
11093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011094dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011095{
11096 dictitem_T *di1;
11097 hashitem_T *hi2;
11098 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011099 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011100
11101 todo = (int)d2->dv_hashtab.ht_used;
11102 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11103 {
11104 if (!HASHITEM_EMPTY(hi2))
11105 {
11106 --todo;
11107 di1 = dict_find(d1, hi2->hi_key, -1);
11108 if (d1->dv_scope != 0)
11109 {
11110 /* Disallow replacing a builtin function in l: and g:.
11111 * Check the key to be valid when adding to any
11112 * scope. */
11113 if (d1->dv_scope == VAR_DEF_SCOPE
11114 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11115 && var_check_func_name(hi2->hi_key,
11116 di1 == NULL))
11117 break;
11118 if (!valid_varname(hi2->hi_key))
11119 break;
11120 }
11121 if (di1 == NULL)
11122 {
11123 di1 = dictitem_copy(HI2DI(hi2));
11124 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11125 dictitem_free(di1);
11126 }
11127 else if (*action == 'e')
11128 {
11129 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11130 break;
11131 }
11132 else if (*action == 'f' && HI2DI(hi2) != di1)
11133 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011134 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11135 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011136 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011137 clear_tv(&di1->di_tv);
11138 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11139 }
11140 }
11141 }
11142}
11143
11144/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011145 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011146 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011147 */
11148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011149f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011150{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011151 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011152
Bram Moolenaare9a41262005-01-15 22:18:47 +000011153 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011154 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011155 list_T *l1, *l2;
11156 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011157 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011158 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011159
Bram Moolenaare9a41262005-01-15 22:18:47 +000011160 l1 = argvars[0].vval.v_list;
11161 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011162 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011163 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011164 {
11165 if (argvars[2].v_type != VAR_UNKNOWN)
11166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011167 before = get_tv_number_chk(&argvars[2], &error);
11168 if (error)
11169 return; /* type error; errmsg already given */
11170
Bram Moolenaar758711c2005-02-02 23:11:38 +000011171 if (before == l1->lv_len)
11172 item = NULL;
11173 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011174 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011175 item = list_find(l1, before);
11176 if (item == NULL)
11177 {
11178 EMSGN(_(e_listidx), before);
11179 return;
11180 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011181 }
11182 }
11183 else
11184 item = NULL;
11185 list_extend(l1, l2, item);
11186
Bram Moolenaare9a41262005-01-15 22:18:47 +000011187 copy_tv(&argvars[0], rettv);
11188 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011189 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011190 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11191 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011192 dict_T *d1, *d2;
11193 char_u *action;
11194 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011195
11196 d1 = argvars[0].vval.v_dict;
11197 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011198 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011199 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011200 {
11201 /* Check the third argument. */
11202 if (argvars[2].v_type != VAR_UNKNOWN)
11203 {
11204 static char *(av[]) = {"keep", "force", "error"};
11205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011206 action = get_tv_string_chk(&argvars[2]);
11207 if (action == NULL)
11208 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011209 for (i = 0; i < 3; ++i)
11210 if (STRCMP(action, av[i]) == 0)
11211 break;
11212 if (i == 3)
11213 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011214 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011215 return;
11216 }
11217 }
11218 else
11219 action = (char_u *)"force";
11220
Bram Moolenaara9922d62013-05-30 13:01:18 +020011221 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011222
Bram Moolenaare9a41262005-01-15 22:18:47 +000011223 copy_tv(&argvars[0], rettv);
11224 }
11225 }
11226 else
11227 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011228}
11229
11230/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011231 * "feedkeys()" function
11232 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011234f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011235{
11236 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011237 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011238 char_u *keys, *flags;
11239 char_u nbuf[NUMBUFLEN];
11240 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011241 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011242 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011243
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011244 /* This is not allowed in the sandbox. If the commands would still be
11245 * executed in the sandbox it would be OK, but it probably happens later,
11246 * when "sandbox" is no longer set. */
11247 if (check_secure())
11248 return;
11249
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011250 keys = get_tv_string(&argvars[0]);
11251 if (*keys != NUL)
11252 {
11253 if (argvars[1].v_type != VAR_UNKNOWN)
11254 {
11255 flags = get_tv_string_buf(&argvars[1], nbuf);
11256 for ( ; *flags != NUL; ++flags)
11257 {
11258 switch (*flags)
11259 {
11260 case 'n': remap = FALSE; break;
11261 case 'm': remap = TRUE; break;
11262 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011263 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011264 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011265 }
11266 }
11267 }
11268
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011269 /* Need to escape K_SPECIAL and CSI before putting the string in the
11270 * typeahead buffer. */
11271 keys_esc = vim_strsave_escape_csi(keys);
11272 if (keys_esc != NULL)
11273 {
11274 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011275 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011276 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011277 if (vgetc_busy)
11278 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011279 if (execute)
11280 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011281 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011282 }
11283}
11284
11285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 * "filereadable()" function
11287 */
11288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011289f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011291 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 char_u *p;
11293 int n;
11294
Bram Moolenaarc236c162008-07-13 17:41:49 +000011295#ifndef O_NONBLOCK
11296# define O_NONBLOCK 0
11297#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011299 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11300 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 {
11302 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011303 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 }
11305 else
11306 n = FALSE;
11307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309}
11310
11311/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011312 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 * rights to write into.
11314 */
11315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011316f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011318 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319}
11320
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011321 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011322findfilendir(
11323 typval_T *argvars UNUSED,
11324 typval_T *rettv,
11325 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011326{
11327#ifdef FEAT_SEARCHPATH
11328 char_u *fname;
11329 char_u *fresult = NULL;
11330 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11331 char_u *p;
11332 char_u pathbuf[NUMBUFLEN];
11333 int count = 1;
11334 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011335 int error = FALSE;
11336#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011337
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011338 rettv->vval.v_string = NULL;
11339 rettv->v_type = VAR_STRING;
11340
11341#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011343
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011344 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011346 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11347 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011348 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011349 else
11350 {
11351 if (*p != NUL)
11352 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011354 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011355 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011356 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011357 }
11358
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011359 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11360 error = TRUE;
11361
11362 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011364 do
11365 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011366 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011367 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 fresult = find_file_in_path_option(first ? fname : NULL,
11369 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011370 0, first, path,
11371 find_what,
11372 curbuf->b_ffname,
11373 find_what == FINDFILE_DIR
11374 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011375 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011376
11377 if (fresult != NULL && rettv->v_type == VAR_LIST)
11378 list_append_string(rettv->vval.v_list, fresult, -1);
11379
11380 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011382
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011383 if (rettv->v_type == VAR_STRING)
11384 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011385#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011386}
11387
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011388static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11389static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011390
11391/*
11392 * Implementation of map() and filter().
11393 */
11394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011395filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011396{
11397 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011398 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011399 listitem_T *li, *nli;
11400 list_T *l = NULL;
11401 dictitem_T *di;
11402 hashtab_T *ht;
11403 hashitem_T *hi;
11404 dict_T *d = NULL;
11405 typval_T save_val;
11406 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011407 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011408 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011409 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011410 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011411 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011412 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011413 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011414
Bram Moolenaare9a41262005-01-15 22:18:47 +000011415 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011416 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011417 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011418 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011419 return;
11420 }
11421 else if (argvars[0].v_type == VAR_DICT)
11422 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011423 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011424 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011425 return;
11426 }
11427 else
11428 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011429 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011430 return;
11431 }
11432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011433 expr = get_tv_string_buf_chk(&argvars[1], buf);
11434 /* On type errors, the preceding call has already displayed an error
11435 * message. Avoid a misleading error message for an empty string that
11436 * was not passed as argument. */
11437 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011439 prepare_vimvar(VV_VAL, &save_val);
11440 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011441
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011442 /* We reset "did_emsg" to be able to detect whether an error
11443 * occurred during evaluation of the expression. */
11444 save_did_emsg = did_emsg;
11445 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011446
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011447 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011448 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011449 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011450 vimvars[VV_KEY].vv_type = VAR_STRING;
11451
11452 ht = &d->dv_hashtab;
11453 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011454 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011455 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457 if (!HASHITEM_EMPTY(hi))
11458 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011459 int r;
11460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011461 --todo;
11462 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011463 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011464 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11465 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011466 break;
11467 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011468 r = filter_map_one(&di->di_tv, expr, map, &rem);
11469 clear_tv(&vimvars[VV_KEY].vv_tv);
11470 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011471 break;
11472 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011473 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011474 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11475 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011476 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011477 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011478 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011479 }
11480 }
11481 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011482 }
11483 else
11484 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011485 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011487 for (li = l->lv_first; li != NULL; li = nli)
11488 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011489 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011490 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011491 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011492 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011493 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011494 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011495 break;
11496 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011497 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011498 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011499 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011500 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011501
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011502 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011503 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011504
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011505 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011506 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011507
11508 copy_tv(&argvars[0], rettv);
11509}
11510
11511 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011512filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011513{
Bram Moolenaar33570922005-01-25 22:26:29 +000011514 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011515 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011516 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011517
Bram Moolenaar33570922005-01-25 22:26:29 +000011518 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011519 s = expr;
11520 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011521 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011522 if (*s != NUL) /* check for trailing chars after expr */
11523 {
11524 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011525 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011526 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011527 }
11528 if (map)
11529 {
11530 /* map(): replace the list item value */
11531 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011532 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011533 *tv = rettv;
11534 }
11535 else
11536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011537 int error = FALSE;
11538
Bram Moolenaare9a41262005-01-15 22:18:47 +000011539 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011540 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011541 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011542 /* On type error, nothing has been removed; return FAIL to stop the
11543 * loop. The error message was given by get_tv_number_chk(). */
11544 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011545 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011546 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011547 retval = OK;
11548theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011549 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011550 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011551}
11552
11553/*
11554 * "filter()" function
11555 */
11556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011557f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011558{
11559 filter_map(argvars, rettv, FALSE);
11560}
11561
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011562/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011563 * "finddir({fname}[, {path}[, {count}]])" function
11564 */
11565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011566f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011567{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011568 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011569}
11570
11571/*
11572 * "findfile({fname}[, {path}[, {count}]])" function
11573 */
11574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011575f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011576{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011577 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011578}
11579
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011580#ifdef FEAT_FLOAT
11581/*
11582 * "float2nr({float})" function
11583 */
11584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011585f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011586{
11587 float_T f;
11588
11589 if (get_float_arg(argvars, &f) == OK)
11590 {
11591 if (f < -0x7fffffff)
11592 rettv->vval.v_number = -0x7fffffff;
11593 else if (f > 0x7fffffff)
11594 rettv->vval.v_number = 0x7fffffff;
11595 else
11596 rettv->vval.v_number = (varnumber_T)f;
11597 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011598}
11599
11600/*
11601 * "floor({float})" function
11602 */
11603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011604f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011605{
11606 float_T f;
11607
11608 rettv->v_type = VAR_FLOAT;
11609 if (get_float_arg(argvars, &f) == OK)
11610 rettv->vval.v_float = floor(f);
11611 else
11612 rettv->vval.v_float = 0.0;
11613}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011614
11615/*
11616 * "fmod()" function
11617 */
11618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011619f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011620{
11621 float_T fx, fy;
11622
11623 rettv->v_type = VAR_FLOAT;
11624 if (get_float_arg(argvars, &fx) == OK
11625 && get_float_arg(&argvars[1], &fy) == OK)
11626 rettv->vval.v_float = fmod(fx, fy);
11627 else
11628 rettv->vval.v_float = 0.0;
11629}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011630#endif
11631
Bram Moolenaar0d660222005-01-07 21:51:51 +000011632/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011633 * "fnameescape({string})" function
11634 */
11635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011636f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011637{
11638 rettv->vval.v_string = vim_strsave_fnameescape(
11639 get_tv_string(&argvars[0]), FALSE);
11640 rettv->v_type = VAR_STRING;
11641}
11642
11643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 * "fnamemodify({fname}, {mods})" function
11645 */
11646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011647f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648{
11649 char_u *fname;
11650 char_u *mods;
11651 int usedlen = 0;
11652 int len;
11653 char_u *fbuf = NULL;
11654 char_u buf[NUMBUFLEN];
11655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011656 fname = get_tv_string_chk(&argvars[0]);
11657 mods = get_tv_string_buf_chk(&argvars[1], buf);
11658 if (fname == NULL || mods == NULL)
11659 fname = NULL;
11660 else
11661 {
11662 len = (int)STRLEN(fname);
11663 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671 vim_free(fbuf);
11672}
11673
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011674static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675
11676/*
11677 * "foldclosed()" function
11678 */
11679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011680foldclosed_both(
11681 typval_T *argvars UNUSED,
11682 typval_T *rettv,
11683 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684{
11685#ifdef FEAT_FOLDING
11686 linenr_T lnum;
11687 linenr_T first, last;
11688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11691 {
11692 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11693 {
11694 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011695 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011697 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 return;
11699 }
11700 }
11701#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011702 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703}
11704
11705/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011706 * "foldclosed()" function
11707 */
11708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011709f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011710{
11711 foldclosed_both(argvars, rettv, FALSE);
11712}
11713
11714/*
11715 * "foldclosedend()" function
11716 */
11717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011718f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011719{
11720 foldclosed_both(argvars, rettv, TRUE);
11721}
11722
11723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724 * "foldlevel()" function
11725 */
11726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011727f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728{
11729#ifdef FEAT_FOLDING
11730 linenr_T lnum;
11731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011732 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011734 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736}
11737
11738/*
11739 * "foldtext()" function
11740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011742f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743{
11744#ifdef FEAT_FOLDING
11745 linenr_T lnum;
11746 char_u *s;
11747 char_u *r;
11748 int len;
11749 char *txt;
11750#endif
11751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752 rettv->v_type = VAR_STRING;
11753 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011755 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11756 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11757 <= curbuf->b_ml.ml_line_count
11758 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 {
11760 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011761 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11762 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 {
11764 if (!linewhite(lnum))
11765 break;
11766 ++lnum;
11767 }
11768
11769 /* Find interesting text in this line. */
11770 s = skipwhite(ml_get(lnum));
11771 /* skip C comment-start */
11772 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011773 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011775 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011776 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011777 {
11778 s = skipwhite(ml_get(lnum + 1));
11779 if (*s == '*')
11780 s = skipwhite(s + 1);
11781 }
11782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783 txt = _("+-%s%3ld lines: ");
11784 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011785 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786 + 20 /* for %3ld */
11787 + STRLEN(s))); /* concatenated */
11788 if (r != NULL)
11789 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011790 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11791 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11792 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 len = (int)STRLEN(r);
11794 STRCAT(r, s);
11795 /* remove 'foldmarker' and 'commentstring' */
11796 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 }
11799 }
11800#endif
11801}
11802
11803/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011804 * "foldtextresult(lnum)" function
11805 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011807f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011808{
11809#ifdef FEAT_FOLDING
11810 linenr_T lnum;
11811 char_u *text;
11812 char_u buf[51];
11813 foldinfo_T foldinfo;
11814 int fold_count;
11815#endif
11816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011817 rettv->v_type = VAR_STRING;
11818 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011819#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011820 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011821 /* treat illegal types and illegal string values for {lnum} the same */
11822 if (lnum < 0)
11823 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011824 fold_count = foldedCount(curwin, lnum, &foldinfo);
11825 if (fold_count > 0)
11826 {
11827 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11828 &foldinfo, buf);
11829 if (text == buf)
11830 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011831 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011832 }
11833#endif
11834}
11835
11836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 * "foreground()" function
11838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011840f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842#ifdef FEAT_GUI
11843 if (gui.in_use)
11844 gui_mch_set_foreground();
11845#else
11846# ifdef WIN32
11847 win32_set_foreground();
11848# endif
11849#endif
11850}
11851
11852/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011853 * "function()" function
11854 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011856f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011857{
11858 char_u *s;
11859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011861 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011862 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011863 /* Don't check an autoload name for existence here. */
11864 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011865 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011866 else
11867 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011868 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011869 {
11870 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011871 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011872
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011873 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11874 * also be called from another script. Using trans_function_name()
11875 * would also work, but some plugins depend on the name being
11876 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011877 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011878 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011879 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011880 if (rettv->vval.v_string != NULL)
11881 {
11882 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011883 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011884 }
11885 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011886 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011887 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011889 }
11890}
11891
11892/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011893 * "garbagecollect()" function
11894 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011896f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011897{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011898 /* This is postponed until we are back at the toplevel, because we may be
11899 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11900 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011901
11902 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11903 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011904}
11905
11906/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011907 * "get()" function
11908 */
11909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011910f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011911{
Bram Moolenaar33570922005-01-25 22:26:29 +000011912 listitem_T *li;
11913 list_T *l;
11914 dictitem_T *di;
11915 dict_T *d;
11916 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011917
Bram Moolenaare9a41262005-01-15 22:18:47 +000011918 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011919 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011920 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011922 int error = FALSE;
11923
11924 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11925 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011926 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011927 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011928 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011929 else if (argvars[0].v_type == VAR_DICT)
11930 {
11931 if ((d = argvars[0].vval.v_dict) != NULL)
11932 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011933 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011934 if (di != NULL)
11935 tv = &di->di_tv;
11936 }
11937 }
11938 else
11939 EMSG2(_(e_listdictarg), "get()");
11940
11941 if (tv == NULL)
11942 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011943 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011944 copy_tv(&argvars[2], rettv);
11945 }
11946 else
11947 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011948}
11949
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011950static 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 +000011951
11952/*
11953 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011954 * Return a range (from start to end) of lines in rettv from the specified
11955 * buffer.
11956 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011957 */
11958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011959get_buffer_lines(
11960 buf_T *buf,
11961 linenr_T start,
11962 linenr_T end,
11963 int retlist,
11964 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011965{
11966 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011967
Bram Moolenaar959a1432013-12-14 12:17:38 +010011968 rettv->v_type = VAR_STRING;
11969 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011970 if (retlist && rettv_list_alloc(rettv) == FAIL)
11971 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011972
11973 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11974 return;
11975
11976 if (!retlist)
11977 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011978 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11979 p = ml_get_buf(buf, start, FALSE);
11980 else
11981 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011982 rettv->vval.v_string = vim_strsave(p);
11983 }
11984 else
11985 {
11986 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011987 return;
11988
11989 if (start < 1)
11990 start = 1;
11991 if (end > buf->b_ml.ml_line_count)
11992 end = buf->b_ml.ml_line_count;
11993 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011994 if (list_append_string(rettv->vval.v_list,
11995 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011996 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011997 }
11998}
11999
12000/*
12001 * "getbufline()" function
12002 */
12003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012004f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012005{
12006 linenr_T lnum;
12007 linenr_T end;
12008 buf_T *buf;
12009
12010 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12011 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012012 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012013 --emsg_off;
12014
Bram Moolenaar661b1822005-07-28 22:36:45 +000012015 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012016 if (argvars[2].v_type == VAR_UNKNOWN)
12017 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012018 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012019 end = get_tv_lnum_buf(&argvars[2], buf);
12020
Bram Moolenaar342337a2005-07-21 21:11:17 +000012021 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012022}
12023
Bram Moolenaar0d660222005-01-07 21:51:51 +000012024/*
12025 * "getbufvar()" function
12026 */
12027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012028f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012029{
12030 buf_T *buf;
12031 buf_T *save_curbuf;
12032 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012033 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012034 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012035
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012036 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12037 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012038 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012039 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012040
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012041 rettv->v_type = VAR_STRING;
12042 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012043
12044 if (buf != NULL && varname != NULL)
12045 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012046 /* set curbuf to be our buf, temporarily */
12047 save_curbuf = curbuf;
12048 curbuf = buf;
12049
Bram Moolenaar0d660222005-01-07 21:51:51 +000012050 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012051 {
12052 if (get_option_tv(&varname, rettv, TRUE) == OK)
12053 done = TRUE;
12054 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012055 else if (STRCMP(varname, "changedtick") == 0)
12056 {
12057 rettv->v_type = VAR_NUMBER;
12058 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012059 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012060 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012061 else
12062 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012063 /* Look up the variable. */
12064 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12065 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12066 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012067 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012068 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012069 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012070 done = TRUE;
12071 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012072 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012073
12074 /* restore previous notion of curbuf */
12075 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012076 }
12077
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012078 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12079 /* use the default value */
12080 copy_tv(&argvars[2], rettv);
12081
Bram Moolenaar0d660222005-01-07 21:51:51 +000012082 --emsg_off;
12083}
12084
12085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 * "getchar()" function
12087 */
12088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012089f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090{
12091 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012092 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012094 /* Position the cursor. Needed after a message that ends in a space. */
12095 windgoto(msg_row, msg_col);
12096
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 ++no_mapping;
12098 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012099 for (;;)
12100 {
12101 if (argvars[0].v_type == VAR_UNKNOWN)
12102 /* getchar(): blocking wait. */
12103 n = safe_vgetc();
12104 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12105 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012106 n = vpeekc_any();
12107 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012108 /* illegal argument or getchar(0) and no char avail: return zero */
12109 n = 0;
12110 else
12111 /* getchar(0) and char avail: return char */
12112 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012113
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012114 if (n == K_IGNORE)
12115 continue;
12116 break;
12117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 --no_mapping;
12119 --allow_keys;
12120
Bram Moolenaar219b8702006-11-01 14:32:36 +000012121 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12122 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12123 vimvars[VV_MOUSE_COL].vv_nr = 0;
12124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 if (IS_SPECIAL(n) || mod_mask != 0)
12127 {
12128 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12129 int i = 0;
12130
12131 /* Turn a special key into three bytes, plus modifier. */
12132 if (mod_mask != 0)
12133 {
12134 temp[i++] = K_SPECIAL;
12135 temp[i++] = KS_MODIFIER;
12136 temp[i++] = mod_mask;
12137 }
12138 if (IS_SPECIAL(n))
12139 {
12140 temp[i++] = K_SPECIAL;
12141 temp[i++] = K_SECOND(n);
12142 temp[i++] = K_THIRD(n);
12143 }
12144#ifdef FEAT_MBYTE
12145 else if (has_mbyte)
12146 i += (*mb_char2bytes)(n, temp + i);
12147#endif
12148 else
12149 temp[i++] = n;
12150 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012151 rettv->v_type = VAR_STRING;
12152 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012153
12154#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012155 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012156 {
12157 int row = mouse_row;
12158 int col = mouse_col;
12159 win_T *win;
12160 linenr_T lnum;
12161# ifdef FEAT_WINDOWS
12162 win_T *wp;
12163# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012164 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012165
12166 if (row >= 0 && col >= 0)
12167 {
12168 /* Find the window at the mouse coordinates and compute the
12169 * text position. */
12170 win = mouse_find_win(&row, &col);
12171 (void)mouse_comp_pos(win, &row, &col, &lnum);
12172# ifdef FEAT_WINDOWS
12173 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012174 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012175# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012176 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012177 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12178 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12179 }
12180 }
12181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182 }
12183}
12184
12185/*
12186 * "getcharmod()" function
12187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012189f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012191 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192}
12193
12194/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012195 * "getcharsearch()" function
12196 */
12197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012198f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012199{
12200 if (rettv_dict_alloc(rettv) != FAIL)
12201 {
12202 dict_T *dict = rettv->vval.v_dict;
12203
12204 dict_add_nr_str(dict, "char", 0L, last_csearch());
12205 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12206 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12207 }
12208}
12209
12210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211 * "getcmdline()" function
12212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012214f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012216 rettv->v_type = VAR_STRING;
12217 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218}
12219
12220/*
12221 * "getcmdpos()" function
12222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012224f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012226 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227}
12228
12229/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012230 * "getcmdtype()" function
12231 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012233f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012234{
12235 rettv->v_type = VAR_STRING;
12236 rettv->vval.v_string = alloc(2);
12237 if (rettv->vval.v_string != NULL)
12238 {
12239 rettv->vval.v_string[0] = get_cmdline_type();
12240 rettv->vval.v_string[1] = NUL;
12241 }
12242}
12243
12244/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012245 * "getcmdwintype()" function
12246 */
12247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012248f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012249{
12250 rettv->v_type = VAR_STRING;
12251 rettv->vval.v_string = NULL;
12252#ifdef FEAT_CMDWIN
12253 rettv->vval.v_string = alloc(2);
12254 if (rettv->vval.v_string != NULL)
12255 {
12256 rettv->vval.v_string[0] = cmdwin_type;
12257 rettv->vval.v_string[1] = NUL;
12258 }
12259#endif
12260}
12261
12262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 * "getcwd()" function
12264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012266f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012268 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012269 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012271 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012272 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012273
12274 wp = find_tabwin(&argvars[0], &argvars[1]);
12275 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012277 if (wp->w_localdir != NULL)
12278 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12279 else if(globaldir != NULL)
12280 rettv->vval.v_string = vim_strsave(globaldir);
12281 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012282 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012283 cwd = alloc(MAXPATHL);
12284 if (cwd != NULL)
12285 {
12286 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12287 rettv->vval.v_string = vim_strsave(cwd);
12288 vim_free(cwd);
12289 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012290 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012291#ifdef BACKSLASH_IN_FILENAME
12292 if (rettv->vval.v_string != NULL)
12293 slash_adjust(rettv->vval.v_string);
12294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 }
12296}
12297
12298/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012299 * "getfontname()" function
12300 */
12301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012302f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012303{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012304 rettv->v_type = VAR_STRING;
12305 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012306#ifdef FEAT_GUI
12307 if (gui.in_use)
12308 {
12309 GuiFont font;
12310 char_u *name = NULL;
12311
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012312 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012313 {
12314 /* Get the "Normal" font. Either the name saved by
12315 * hl_set_font_name() or from the font ID. */
12316 font = gui.norm_font;
12317 name = hl_get_font_name();
12318 }
12319 else
12320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012321 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012322 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12323 return;
12324 font = gui_mch_get_font(name, FALSE);
12325 if (font == NOFONT)
12326 return; /* Invalid font name, return empty string. */
12327 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012328 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012329 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012330 gui_mch_free_font(font);
12331 }
12332#endif
12333}
12334
12335/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012336 * "getfperm({fname})" function
12337 */
12338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012339f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012340{
12341 char_u *fname;
12342 struct stat st;
12343 char_u *perm = NULL;
12344 char_u flags[] = "rwx";
12345 int i;
12346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012347 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012349 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012350 if (mch_stat((char *)fname, &st) >= 0)
12351 {
12352 perm = vim_strsave((char_u *)"---------");
12353 if (perm != NULL)
12354 {
12355 for (i = 0; i < 9; i++)
12356 {
12357 if (st.st_mode & (1 << (8 - i)))
12358 perm[i] = flags[i % 3];
12359 }
12360 }
12361 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012362 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012363}
12364
12365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 * "getfsize({fname})" function
12367 */
12368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012369f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370{
12371 char_u *fname;
12372 struct stat st;
12373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012374 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377
12378 if (mch_stat((char *)fname, &st) >= 0)
12379 {
12380 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012381 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012385
12386 /* non-perfect check for overflow */
12387 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12388 rettv->vval.v_number = -2;
12389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 }
12391 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012392 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393}
12394
12395/*
12396 * "getftime({fname})" function
12397 */
12398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012399f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400{
12401 char_u *fname;
12402 struct stat st;
12403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012404 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405
12406 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410}
12411
12412/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012413 * "getftype({fname})" function
12414 */
12415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012416f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012417{
12418 char_u *fname;
12419 struct stat st;
12420 char_u *type = NULL;
12421 char *t;
12422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012423 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012424
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012425 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012426 if (mch_lstat((char *)fname, &st) >= 0)
12427 {
12428#ifdef S_ISREG
12429 if (S_ISREG(st.st_mode))
12430 t = "file";
12431 else if (S_ISDIR(st.st_mode))
12432 t = "dir";
12433# ifdef S_ISLNK
12434 else if (S_ISLNK(st.st_mode))
12435 t = "link";
12436# endif
12437# ifdef S_ISBLK
12438 else if (S_ISBLK(st.st_mode))
12439 t = "bdev";
12440# endif
12441# ifdef S_ISCHR
12442 else if (S_ISCHR(st.st_mode))
12443 t = "cdev";
12444# endif
12445# ifdef S_ISFIFO
12446 else if (S_ISFIFO(st.st_mode))
12447 t = "fifo";
12448# endif
12449# ifdef S_ISSOCK
12450 else if (S_ISSOCK(st.st_mode))
12451 t = "fifo";
12452# endif
12453 else
12454 t = "other";
12455#else
12456# ifdef S_IFMT
12457 switch (st.st_mode & S_IFMT)
12458 {
12459 case S_IFREG: t = "file"; break;
12460 case S_IFDIR: t = "dir"; break;
12461# ifdef S_IFLNK
12462 case S_IFLNK: t = "link"; break;
12463# endif
12464# ifdef S_IFBLK
12465 case S_IFBLK: t = "bdev"; break;
12466# endif
12467# ifdef S_IFCHR
12468 case S_IFCHR: t = "cdev"; break;
12469# endif
12470# ifdef S_IFIFO
12471 case S_IFIFO: t = "fifo"; break;
12472# endif
12473# ifdef S_IFSOCK
12474 case S_IFSOCK: t = "socket"; break;
12475# endif
12476 default: t = "other";
12477 }
12478# else
12479 if (mch_isdir(fname))
12480 t = "dir";
12481 else
12482 t = "file";
12483# endif
12484#endif
12485 type = vim_strsave((char_u *)t);
12486 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012487 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012488}
12489
12490/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012491 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012492 */
12493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012494f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012495{
12496 linenr_T lnum;
12497 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012498 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012499
12500 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012501 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012502 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012503 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012504 retlist = FALSE;
12505 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012506 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012507 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012509 retlist = TRUE;
12510 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012511
Bram Moolenaar342337a2005-07-21 21:11:17 +000012512 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012513}
12514
12515/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012516 * "getmatches()" function
12517 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012519f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012520{
12521#ifdef FEAT_SEARCH_EXTRA
12522 dict_T *dict;
12523 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012524 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012525
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012526 if (rettv_list_alloc(rettv) == OK)
12527 {
12528 while (cur != NULL)
12529 {
12530 dict = dict_alloc();
12531 if (dict == NULL)
12532 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012533 if (cur->match.regprog == NULL)
12534 {
12535 /* match added with matchaddpos() */
12536 for (i = 0; i < MAXPOSMATCH; ++i)
12537 {
12538 llpos_T *llpos;
12539 char buf[6];
12540 list_T *l;
12541
12542 llpos = &cur->pos.pos[i];
12543 if (llpos->lnum == 0)
12544 break;
12545 l = list_alloc();
12546 if (l == NULL)
12547 break;
12548 list_append_number(l, (varnumber_T)llpos->lnum);
12549 if (llpos->col > 0)
12550 {
12551 list_append_number(l, (varnumber_T)llpos->col);
12552 list_append_number(l, (varnumber_T)llpos->len);
12553 }
12554 sprintf(buf, "pos%d", i + 1);
12555 dict_add_list(dict, buf, l);
12556 }
12557 }
12558 else
12559 {
12560 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12561 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012562 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012563 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12564 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012565# ifdef FEAT_CONCEAL
12566 if (cur->conceal_char)
12567 {
12568 char_u buf[MB_MAXBYTES + 1];
12569
12570 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12571 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12572 }
12573# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012574 list_append_dict(rettv->vval.v_list, dict);
12575 cur = cur->next;
12576 }
12577 }
12578#endif
12579}
12580
12581/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012582 * "getpid()" function
12583 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012585f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012586{
12587 rettv->vval.v_number = mch_get_pid();
12588}
12589
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012590static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012591
12592/*
12593 * "getcurpos()" function
12594 */
12595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012596f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012597{
12598 getpos_both(argvars, rettv, TRUE);
12599}
12600
Bram Moolenaar18081e32008-02-20 19:11:07 +000012601/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012602 * "getpos(string)" function
12603 */
12604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012605f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012606{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012607 getpos_both(argvars, rettv, FALSE);
12608}
12609
12610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012611getpos_both(
12612 typval_T *argvars,
12613 typval_T *rettv,
12614 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012615{
Bram Moolenaara5525202006-03-02 22:52:09 +000012616 pos_T *fp;
12617 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012618 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012619
12620 if (rettv_list_alloc(rettv) == OK)
12621 {
12622 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012623 if (getcurpos)
12624 fp = &curwin->w_cursor;
12625 else
12626 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012627 if (fnum != -1)
12628 list_append_number(l, (varnumber_T)fnum);
12629 else
12630 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012631 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12632 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012633 list_append_number(l, (fp != NULL)
12634 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012635 : (varnumber_T)0);
12636 list_append_number(l,
12637#ifdef FEAT_VIRTUALEDIT
12638 (fp != NULL) ? (varnumber_T)fp->coladd :
12639#endif
12640 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012641 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012642 {
12643 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012644 list_append_number(l, curwin->w_curswant == MAXCOL ?
12645 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012646 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012647 }
12648 else
12649 rettv->vval.v_number = FALSE;
12650}
12651
12652/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012653 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012654 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012656f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012657{
12658#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012659 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012660#endif
12661
Bram Moolenaar2641f772005-03-25 21:58:17 +000012662#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012663 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012664 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012665 wp = NULL;
12666 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12667 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012668 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012669 if (wp == NULL)
12670 return;
12671 }
12672
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012673 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012674 }
12675#endif
12676}
12677
12678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679 * "getreg()" function
12680 */
12681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012682f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683{
12684 char_u *strregname;
12685 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012686 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012687 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012688 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012690 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012692 strregname = get_tv_string_chk(&argvars[0]);
12693 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012694 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012696 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012697 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12698 return_list = get_tv_number_chk(&argvars[2], &error);
12699 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012702 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012703
12704 if (error)
12705 return;
12706
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707 regname = (strregname == NULL ? '"' : *strregname);
12708 if (regname == 0)
12709 regname = '"';
12710
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012711 if (return_list)
12712 {
12713 rettv->v_type = VAR_LIST;
12714 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12715 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012716 if (rettv->vval.v_list != NULL)
12717 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012718 }
12719 else
12720 {
12721 rettv->v_type = VAR_STRING;
12722 rettv->vval.v_string = get_reg_contents(regname,
12723 arg2 ? GREG_EXPR_SRC : 0);
12724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725}
12726
12727/*
12728 * "getregtype()" function
12729 */
12730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012731f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732{
12733 char_u *strregname;
12734 int regname;
12735 char_u buf[NUMBUFLEN + 2];
12736 long reglen = 0;
12737
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012738 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012739 {
12740 strregname = get_tv_string_chk(&argvars[0]);
12741 if (strregname == NULL) /* type error; errmsg already given */
12742 {
12743 rettv->v_type = VAR_STRING;
12744 rettv->vval.v_string = NULL;
12745 return;
12746 }
12747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748 else
12749 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012750 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751
12752 regname = (strregname == NULL ? '"' : *strregname);
12753 if (regname == 0)
12754 regname = '"';
12755
12756 buf[0] = NUL;
12757 buf[1] = NUL;
12758 switch (get_reg_type(regname, &reglen))
12759 {
12760 case MLINE: buf[0] = 'V'; break;
12761 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762 case MBLOCK:
12763 buf[0] = Ctrl_V;
12764 sprintf((char *)buf + 1, "%ld", reglen + 1);
12765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012767 rettv->v_type = VAR_STRING;
12768 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769}
12770
12771/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012772 * "gettabvar()" function
12773 */
12774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012775f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012776{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012777 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012778 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012779 dictitem_T *v;
12780 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012781 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012782
12783 rettv->v_type = VAR_STRING;
12784 rettv->vval.v_string = NULL;
12785
12786 varname = get_tv_string_chk(&argvars[1]);
12787 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12788 if (tp != NULL && varname != NULL)
12789 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012790 /* Set tp to be our tabpage, temporarily. Also set the window to the
12791 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012792 if (switch_win(&oldcurwin, &oldtabpage,
12793 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012794 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012795 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012796 /* look up the variable */
12797 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12798 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12799 if (v != NULL)
12800 {
12801 copy_tv(&v->di_tv, rettv);
12802 done = TRUE;
12803 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012804 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012805
12806 /* restore previous notion of curwin */
12807 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012808 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012809
12810 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012811 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012812}
12813
12814/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012815 * "gettabwinvar()" function
12816 */
12817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012818f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012819{
12820 getwinvar(argvars, rettv, 1);
12821}
12822
12823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 * "getwinposx()" function
12825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012827f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012829 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830#ifdef FEAT_GUI
12831 if (gui.in_use)
12832 {
12833 int x, y;
12834
12835 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 }
12838#endif
12839}
12840
12841/*
12842 * "getwinposy()" function
12843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012845f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012847 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848#ifdef FEAT_GUI
12849 if (gui.in_use)
12850 {
12851 int x, y;
12852
12853 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 }
12856#endif
12857}
12858
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012859/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012860 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012861 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012862 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012863find_win_by_nr(
12864 typval_T *vp,
12865 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012866{
12867#ifdef FEAT_WINDOWS
12868 win_T *wp;
12869#endif
12870 int nr;
12871
12872 nr = get_tv_number_chk(vp, NULL);
12873
12874#ifdef FEAT_WINDOWS
12875 if (nr < 0)
12876 return NULL;
12877 if (nr == 0)
12878 return curwin;
12879
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012880 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12881 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012882 if (--nr <= 0)
12883 break;
12884 return wp;
12885#else
12886 if (nr == 0 || nr == 1)
12887 return curwin;
12888 return NULL;
12889#endif
12890}
12891
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012893 * Find window specified by "wvp" in tabpage "tvp".
12894 */
12895 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012896find_tabwin(
12897 typval_T *wvp, /* VAR_UNKNOWN for current window */
12898 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012899{
12900 win_T *wp = NULL;
12901 tabpage_T *tp = NULL;
12902 long n;
12903
12904 if (wvp->v_type != VAR_UNKNOWN)
12905 {
12906 if (tvp->v_type != VAR_UNKNOWN)
12907 {
12908 n = get_tv_number(tvp);
12909 if (n >= 0)
12910 tp = find_tabpage(n);
12911 }
12912 else
12913 tp = curtab;
12914
12915 if (tp != NULL)
12916 wp = find_win_by_nr(wvp, tp);
12917 }
12918 else
12919 wp = curwin;
12920
12921 return wp;
12922}
12923
12924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925 * "getwinvar()" function
12926 */
12927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012928f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012930 getwinvar(argvars, rettv, 0);
12931}
12932
12933/*
12934 * getwinvar() and gettabwinvar()
12935 */
12936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012937getwinvar(
12938 typval_T *argvars,
12939 typval_T *rettv,
12940 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012941{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012942 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012944 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012945 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012946 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012947#ifdef FEAT_WINDOWS
12948 win_T *oldcurwin;
12949 tabpage_T *oldtabpage;
12950 int need_switch_win;
12951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012953#ifdef FEAT_WINDOWS
12954 if (off == 1)
12955 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12956 else
12957 tp = curtab;
12958#endif
12959 win = find_win_by_nr(&argvars[off], tp);
12960 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012961 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012963 rettv->v_type = VAR_STRING;
12964 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965
12966 if (win != NULL && varname != NULL)
12967 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012968#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012969 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012970 * otherwise the window is not valid. Only do this when needed,
12971 * autocommands get blocked. */
12972 need_switch_win = !(tp == curtab && win == curwin);
12973 if (!need_switch_win
12974 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12975#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012976 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012977 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012978 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012979 if (get_option_tv(&varname, rettv, 1) == OK)
12980 done = TRUE;
12981 }
12982 else
12983 {
12984 /* Look up the variable. */
12985 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12986 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12987 varname, FALSE);
12988 if (v != NULL)
12989 {
12990 copy_tv(&v->di_tv, rettv);
12991 done = TRUE;
12992 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012995
Bram Moolenaarba117c22015-09-29 16:53:22 +020012996#ifdef FEAT_WINDOWS
12997 if (need_switch_win)
12998 /* restore previous notion of curwin */
12999 restore_win(oldcurwin, oldtabpage, TRUE);
13000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001 }
13002
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013003 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13004 /* use the default return value */
13005 copy_tv(&argvars[off + 2], rettv);
13006
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007 --emsg_off;
13008}
13009
13010/*
13011 * "glob()" function
13012 */
13013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013014f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013016 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013018 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013020 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013021 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013022 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013023 if (argvars[1].v_type != VAR_UNKNOWN)
13024 {
13025 if (get_tv_number_chk(&argvars[1], &error))
13026 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013027 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013028 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013029 if (get_tv_number_chk(&argvars[2], &error))
13030 {
13031 rettv->v_type = VAR_LIST;
13032 rettv->vval.v_list = NULL;
13033 }
13034 if (argvars[3].v_type != VAR_UNKNOWN
13035 && get_tv_number_chk(&argvars[3], &error))
13036 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013037 }
13038 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013039 if (!error)
13040 {
13041 ExpandInit(&xpc);
13042 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013043 if (p_wic)
13044 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013045 if (rettv->v_type == VAR_STRING)
13046 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013047 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013048 else if (rettv_list_alloc(rettv) != FAIL)
13049 {
13050 int i;
13051
13052 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13053 NULL, options, WILD_ALL_KEEP);
13054 for (i = 0; i < xpc.xp_numfiles; i++)
13055 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13056
13057 ExpandCleanup(&xpc);
13058 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013059 }
13060 else
13061 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062}
13063
13064/*
13065 * "globpath()" function
13066 */
13067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013068f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013070 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013072 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013073 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013074 garray_T ga;
13075 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013077 /* When the optional second argument is non-zero, don't remove matches
13078 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013079 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013080 if (argvars[2].v_type != VAR_UNKNOWN)
13081 {
13082 if (get_tv_number_chk(&argvars[2], &error))
13083 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013084 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013085 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013086 if (get_tv_number_chk(&argvars[3], &error))
13087 {
13088 rettv->v_type = VAR_LIST;
13089 rettv->vval.v_list = NULL;
13090 }
13091 if (argvars[4].v_type != VAR_UNKNOWN
13092 && get_tv_number_chk(&argvars[4], &error))
13093 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013094 }
13095 }
13096 if (file != NULL && !error)
13097 {
13098 ga_init2(&ga, (int)sizeof(char_u *), 10);
13099 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13100 if (rettv->v_type == VAR_STRING)
13101 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13102 else if (rettv_list_alloc(rettv) != FAIL)
13103 for (i = 0; i < ga.ga_len; ++i)
13104 list_append_string(rettv->vval.v_list,
13105 ((char_u **)(ga.ga_data))[i], -1);
13106 ga_clear_strings(&ga);
13107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013108 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013109 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110}
13111
13112/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013113 * "glob2regpat()" function
13114 */
13115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013116f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013117{
13118 char_u *pat = get_tv_string_chk(&argvars[0]);
13119
13120 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013121 rettv->vval.v_string = (pat == NULL)
13122 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013123}
13124
13125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126 * "has()" function
13127 */
13128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013129f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130{
13131 int i;
13132 char_u *name;
13133 int n = FALSE;
13134 static char *(has_list[]) =
13135 {
13136#ifdef AMIGA
13137 "amiga",
13138# ifdef FEAT_ARP
13139 "arp",
13140# endif
13141#endif
13142#ifdef __BEOS__
13143 "beos",
13144#endif
13145#ifdef MSDOS
13146# ifdef DJGPP
13147 "dos32",
13148# else
13149 "dos16",
13150# endif
13151#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013152#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153 "mac",
13154#endif
13155#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013156 "macunix", /* built with 'darwin' enabled */
13157#endif
13158#if defined(__APPLE__) && __APPLE__ == 1
13159 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161#ifdef __QNX__
13162 "qnx",
13163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164#ifdef UNIX
13165 "unix",
13166#endif
13167#ifdef VMS
13168 "vms",
13169#endif
13170#ifdef WIN16
13171 "win16",
13172#endif
13173#ifdef WIN32
13174 "win32",
13175#endif
13176#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13177 "win32unix",
13178#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013179#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180 "win64",
13181#endif
13182#ifdef EBCDIC
13183 "ebcdic",
13184#endif
13185#ifndef CASE_INSENSITIVE_FILENAME
13186 "fname_case",
13187#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013188#ifdef HAVE_ACL
13189 "acl",
13190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191#ifdef FEAT_ARABIC
13192 "arabic",
13193#endif
13194#ifdef FEAT_AUTOCMD
13195 "autocmd",
13196#endif
13197#ifdef FEAT_BEVAL
13198 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013199# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13200 "balloon_multiline",
13201# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202#endif
13203#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13204 "builtin_terms",
13205# ifdef ALL_BUILTIN_TCAPS
13206 "all_builtin_terms",
13207# endif
13208#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013209#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13210 || defined(FEAT_GUI_W32) \
13211 || defined(FEAT_GUI_MOTIF))
13212 "browsefilter",
13213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214#ifdef FEAT_BYTEOFF
13215 "byte_offset",
13216#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013217#ifdef FEAT_CHANNEL
13218 "channel",
13219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220#ifdef FEAT_CINDENT
13221 "cindent",
13222#endif
13223#ifdef FEAT_CLIENTSERVER
13224 "clientserver",
13225#endif
13226#ifdef FEAT_CLIPBOARD
13227 "clipboard",
13228#endif
13229#ifdef FEAT_CMDL_COMPL
13230 "cmdline_compl",
13231#endif
13232#ifdef FEAT_CMDHIST
13233 "cmdline_hist",
13234#endif
13235#ifdef FEAT_COMMENTS
13236 "comments",
13237#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013238#ifdef FEAT_CONCEAL
13239 "conceal",
13240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241#ifdef FEAT_CRYPT
13242 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013243 "crypt-blowfish",
13244 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245#endif
13246#ifdef FEAT_CSCOPE
13247 "cscope",
13248#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013249#ifdef FEAT_CURSORBIND
13250 "cursorbind",
13251#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013252#ifdef CURSOR_SHAPE
13253 "cursorshape",
13254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255#ifdef DEBUG
13256 "debug",
13257#endif
13258#ifdef FEAT_CON_DIALOG
13259 "dialog_con",
13260#endif
13261#ifdef FEAT_GUI_DIALOG
13262 "dialog_gui",
13263#endif
13264#ifdef FEAT_DIFF
13265 "diff",
13266#endif
13267#ifdef FEAT_DIGRAPHS
13268 "digraphs",
13269#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013270#ifdef FEAT_DIRECTX
13271 "directx",
13272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273#ifdef FEAT_DND
13274 "dnd",
13275#endif
13276#ifdef FEAT_EMACS_TAGS
13277 "emacs_tags",
13278#endif
13279 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013280 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281#ifdef FEAT_SEARCH_EXTRA
13282 "extra_search",
13283#endif
13284#ifdef FEAT_FKMAP
13285 "farsi",
13286#endif
13287#ifdef FEAT_SEARCHPATH
13288 "file_in_path",
13289#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013290#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013291 "filterpipe",
13292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293#ifdef FEAT_FIND_ID
13294 "find_in_path",
13295#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013296#ifdef FEAT_FLOAT
13297 "float",
13298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299#ifdef FEAT_FOLDING
13300 "folding",
13301#endif
13302#ifdef FEAT_FOOTER
13303 "footer",
13304#endif
13305#if !defined(USE_SYSTEM) && defined(UNIX)
13306 "fork",
13307#endif
13308#ifdef FEAT_GETTEXT
13309 "gettext",
13310#endif
13311#ifdef FEAT_GUI
13312 "gui",
13313#endif
13314#ifdef FEAT_GUI_ATHENA
13315# ifdef FEAT_GUI_NEXTAW
13316 "gui_neXtaw",
13317# else
13318 "gui_athena",
13319# endif
13320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321#ifdef FEAT_GUI_GTK
13322 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013325#ifdef FEAT_GUI_GNOME
13326 "gui_gnome",
13327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328#ifdef FEAT_GUI_MAC
13329 "gui_mac",
13330#endif
13331#ifdef FEAT_GUI_MOTIF
13332 "gui_motif",
13333#endif
13334#ifdef FEAT_GUI_PHOTON
13335 "gui_photon",
13336#endif
13337#ifdef FEAT_GUI_W16
13338 "gui_win16",
13339#endif
13340#ifdef FEAT_GUI_W32
13341 "gui_win32",
13342#endif
13343#ifdef FEAT_HANGULIN
13344 "hangul_input",
13345#endif
13346#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13347 "iconv",
13348#endif
13349#ifdef FEAT_INS_EXPAND
13350 "insert_expand",
13351#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013352#ifdef FEAT_JOB
13353 "job",
13354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355#ifdef FEAT_JUMPLIST
13356 "jumplist",
13357#endif
13358#ifdef FEAT_KEYMAP
13359 "keymap",
13360#endif
13361#ifdef FEAT_LANGMAP
13362 "langmap",
13363#endif
13364#ifdef FEAT_LIBCALL
13365 "libcall",
13366#endif
13367#ifdef FEAT_LINEBREAK
13368 "linebreak",
13369#endif
13370#ifdef FEAT_LISP
13371 "lispindent",
13372#endif
13373#ifdef FEAT_LISTCMDS
13374 "listcmds",
13375#endif
13376#ifdef FEAT_LOCALMAP
13377 "localmap",
13378#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013379#ifdef FEAT_LUA
13380# ifndef DYNAMIC_LUA
13381 "lua",
13382# endif
13383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384#ifdef FEAT_MENU
13385 "menu",
13386#endif
13387#ifdef FEAT_SESSION
13388 "mksession",
13389#endif
13390#ifdef FEAT_MODIFY_FNAME
13391 "modify_fname",
13392#endif
13393#ifdef FEAT_MOUSE
13394 "mouse",
13395#endif
13396#ifdef FEAT_MOUSESHAPE
13397 "mouseshape",
13398#endif
13399#if defined(UNIX) || defined(VMS)
13400# ifdef FEAT_MOUSE_DEC
13401 "mouse_dec",
13402# endif
13403# ifdef FEAT_MOUSE_GPM
13404 "mouse_gpm",
13405# endif
13406# ifdef FEAT_MOUSE_JSB
13407 "mouse_jsbterm",
13408# endif
13409# ifdef FEAT_MOUSE_NET
13410 "mouse_netterm",
13411# endif
13412# ifdef FEAT_MOUSE_PTERM
13413 "mouse_pterm",
13414# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013415# ifdef FEAT_MOUSE_SGR
13416 "mouse_sgr",
13417# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013418# ifdef FEAT_SYSMOUSE
13419 "mouse_sysmouse",
13420# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013421# ifdef FEAT_MOUSE_URXVT
13422 "mouse_urxvt",
13423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424# ifdef FEAT_MOUSE_XTERM
13425 "mouse_xterm",
13426# endif
13427#endif
13428#ifdef FEAT_MBYTE
13429 "multi_byte",
13430#endif
13431#ifdef FEAT_MBYTE_IME
13432 "multi_byte_ime",
13433#endif
13434#ifdef FEAT_MULTI_LANG
13435 "multi_lang",
13436#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013437#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013438#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013439 "mzscheme",
13440#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442#ifdef FEAT_OLE
13443 "ole",
13444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445#ifdef FEAT_PATH_EXTRA
13446 "path_extra",
13447#endif
13448#ifdef FEAT_PERL
13449#ifndef DYNAMIC_PERL
13450 "perl",
13451#endif
13452#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013453#ifdef FEAT_PERSISTENT_UNDO
13454 "persistent_undo",
13455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456#ifdef FEAT_PYTHON
13457#ifndef DYNAMIC_PYTHON
13458 "python",
13459#endif
13460#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013461#ifdef FEAT_PYTHON3
13462#ifndef DYNAMIC_PYTHON3
13463 "python3",
13464#endif
13465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466#ifdef FEAT_POSTSCRIPT
13467 "postscript",
13468#endif
13469#ifdef FEAT_PRINTER
13470 "printer",
13471#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013472#ifdef FEAT_PROFILE
13473 "profile",
13474#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013475#ifdef FEAT_RELTIME
13476 "reltime",
13477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478#ifdef FEAT_QUICKFIX
13479 "quickfix",
13480#endif
13481#ifdef FEAT_RIGHTLEFT
13482 "rightleft",
13483#endif
13484#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13485 "ruby",
13486#endif
13487#ifdef FEAT_SCROLLBIND
13488 "scrollbind",
13489#endif
13490#ifdef FEAT_CMDL_INFO
13491 "showcmd",
13492 "cmdline_info",
13493#endif
13494#ifdef FEAT_SIGNS
13495 "signs",
13496#endif
13497#ifdef FEAT_SMARTINDENT
13498 "smartindent",
13499#endif
13500#ifdef FEAT_SNIFF
13501 "sniff",
13502#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013503#ifdef STARTUPTIME
13504 "startuptime",
13505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506#ifdef FEAT_STL_OPT
13507 "statusline",
13508#endif
13509#ifdef FEAT_SUN_WORKSHOP
13510 "sun_workshop",
13511#endif
13512#ifdef FEAT_NETBEANS_INTG
13513 "netbeans_intg",
13514#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013515#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013516 "spell",
13517#endif
13518#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519 "syntax",
13520#endif
13521#if defined(USE_SYSTEM) || !defined(UNIX)
13522 "system",
13523#endif
13524#ifdef FEAT_TAG_BINS
13525 "tag_binary",
13526#endif
13527#ifdef FEAT_TAG_OLDSTATIC
13528 "tag_old_static",
13529#endif
13530#ifdef FEAT_TAG_ANYWHITE
13531 "tag_any_white",
13532#endif
13533#ifdef FEAT_TCL
13534# ifndef DYNAMIC_TCL
13535 "tcl",
13536# endif
13537#endif
13538#ifdef TERMINFO
13539 "terminfo",
13540#endif
13541#ifdef FEAT_TERMRESPONSE
13542 "termresponse",
13543#endif
13544#ifdef FEAT_TEXTOBJ
13545 "textobjects",
13546#endif
13547#ifdef HAVE_TGETENT
13548 "tgetent",
13549#endif
13550#ifdef FEAT_TITLE
13551 "title",
13552#endif
13553#ifdef FEAT_TOOLBAR
13554 "toolbar",
13555#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013556#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13557 "unnamedplus",
13558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559#ifdef FEAT_USR_CMDS
13560 "user-commands", /* was accidentally included in 5.4 */
13561 "user_commands",
13562#endif
13563#ifdef FEAT_VIMINFO
13564 "viminfo",
13565#endif
13566#ifdef FEAT_VERTSPLIT
13567 "vertsplit",
13568#endif
13569#ifdef FEAT_VIRTUALEDIT
13570 "virtualedit",
13571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573#ifdef FEAT_VISUALEXTRA
13574 "visualextra",
13575#endif
13576#ifdef FEAT_VREPLACE
13577 "vreplace",
13578#endif
13579#ifdef FEAT_WILDIGN
13580 "wildignore",
13581#endif
13582#ifdef FEAT_WILDMENU
13583 "wildmenu",
13584#endif
13585#ifdef FEAT_WINDOWS
13586 "windows",
13587#endif
13588#ifdef FEAT_WAK
13589 "winaltkeys",
13590#endif
13591#ifdef FEAT_WRITEBACKUP
13592 "writebackup",
13593#endif
13594#ifdef FEAT_XIM
13595 "xim",
13596#endif
13597#ifdef FEAT_XFONTSET
13598 "xfontset",
13599#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013600#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013601 "xpm",
13602 "xpm_w32", /* for backward compatibility */
13603#else
13604# if defined(HAVE_XPM)
13605 "xpm",
13606# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608#ifdef USE_XSMP
13609 "xsmp",
13610#endif
13611#ifdef USE_XSMP_INTERACT
13612 "xsmp_interact",
13613#endif
13614#ifdef FEAT_XCLIPBOARD
13615 "xterm_clipboard",
13616#endif
13617#ifdef FEAT_XTERM_SAVE
13618 "xterm_save",
13619#endif
13620#if defined(UNIX) && defined(FEAT_X11)
13621 "X11",
13622#endif
13623 NULL
13624 };
13625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 for (i = 0; has_list[i] != NULL; ++i)
13628 if (STRICMP(name, has_list[i]) == 0)
13629 {
13630 n = TRUE;
13631 break;
13632 }
13633
13634 if (n == FALSE)
13635 {
13636 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013637 {
13638 if (name[5] == '-'
13639 && STRLEN(name) > 11
13640 && vim_isdigit(name[6])
13641 && vim_isdigit(name[8])
13642 && vim_isdigit(name[10]))
13643 {
13644 int major = atoi((char *)name + 6);
13645 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013646
13647 /* Expect "patch-9.9.01234". */
13648 n = (major < VIM_VERSION_MAJOR
13649 || (major == VIM_VERSION_MAJOR
13650 && (minor < VIM_VERSION_MINOR
13651 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013652 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013653 }
13654 else
13655 n = has_patch(atoi((char *)name + 5));
13656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 else if (STRICMP(name, "vim_starting") == 0)
13658 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013659#ifdef FEAT_MBYTE
13660 else if (STRICMP(name, "multi_byte_encoding") == 0)
13661 n = has_mbyte;
13662#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013663#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13664 else if (STRICMP(name, "balloon_multiline") == 0)
13665 n = multiline_balloon_available();
13666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667#ifdef DYNAMIC_TCL
13668 else if (STRICMP(name, "tcl") == 0)
13669 n = tcl_enabled(FALSE);
13670#endif
13671#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13672 else if (STRICMP(name, "iconv") == 0)
13673 n = iconv_enabled(FALSE);
13674#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013675#ifdef DYNAMIC_LUA
13676 else if (STRICMP(name, "lua") == 0)
13677 n = lua_enabled(FALSE);
13678#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013679#ifdef DYNAMIC_MZSCHEME
13680 else if (STRICMP(name, "mzscheme") == 0)
13681 n = mzscheme_enabled(FALSE);
13682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683#ifdef DYNAMIC_RUBY
13684 else if (STRICMP(name, "ruby") == 0)
13685 n = ruby_enabled(FALSE);
13686#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013687#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688#ifdef DYNAMIC_PYTHON
13689 else if (STRICMP(name, "python") == 0)
13690 n = python_enabled(FALSE);
13691#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013692#endif
13693#ifdef FEAT_PYTHON3
13694#ifdef DYNAMIC_PYTHON3
13695 else if (STRICMP(name, "python3") == 0)
13696 n = python3_enabled(FALSE);
13697#endif
13698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699#ifdef DYNAMIC_PERL
13700 else if (STRICMP(name, "perl") == 0)
13701 n = perl_enabled(FALSE);
13702#endif
13703#ifdef FEAT_GUI
13704 else if (STRICMP(name, "gui_running") == 0)
13705 n = (gui.in_use || gui.starting);
13706# ifdef FEAT_GUI_W32
13707 else if (STRICMP(name, "gui_win32s") == 0)
13708 n = gui_is_win32s();
13709# endif
13710# ifdef FEAT_BROWSE
13711 else if (STRICMP(name, "browse") == 0)
13712 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13713# endif
13714#endif
13715#ifdef FEAT_SYN_HL
13716 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013717 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718#endif
13719#if defined(WIN3264)
13720 else if (STRICMP(name, "win95") == 0)
13721 n = mch_windows95();
13722#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013723#ifdef FEAT_NETBEANS_INTG
13724 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013725 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 }
13728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730}
13731
13732/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013733 * "has_key()" function
13734 */
13735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013736f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013737{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013738 if (argvars[0].v_type != VAR_DICT)
13739 {
13740 EMSG(_(e_dictreq));
13741 return;
13742 }
13743 if (argvars[0].vval.v_dict == NULL)
13744 return;
13745
13746 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013747 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013748}
13749
13750/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013751 * "haslocaldir()" function
13752 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013754f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013755{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013756 win_T *wp = NULL;
13757
13758 wp = find_tabwin(&argvars[0], &argvars[1]);
13759 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013760}
13761
13762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763 * "hasmapto()" function
13764 */
13765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013766f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767{
13768 char_u *name;
13769 char_u *mode;
13770 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013771 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013773 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013774 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 mode = (char_u *)"nvo";
13776 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013779 if (argvars[2].v_type != VAR_UNKNOWN)
13780 abbr = get_tv_number(&argvars[2]);
13781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782
Bram Moolenaar2c932302006-03-18 21:42:09 +000013783 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013784 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013786 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787}
13788
13789/*
13790 * "histadd()" function
13791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013793f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794{
13795#ifdef FEAT_CMDHIST
13796 int histype;
13797 char_u *str;
13798 char_u buf[NUMBUFLEN];
13799#endif
13800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013801 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802 if (check_restricted() || check_secure())
13803 return;
13804#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013805 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13806 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 if (histype >= 0)
13808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013809 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810 if (*str != NUL)
13811 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013812 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013814 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 return;
13816 }
13817 }
13818#endif
13819}
13820
13821/*
13822 * "histdel()" function
13823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013825f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826{
13827#ifdef FEAT_CMDHIST
13828 int n;
13829 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013830 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013832 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13833 if (str == NULL)
13834 n = 0;
13835 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013837 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013838 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013841 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 else
13843 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013844 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013845 get_tv_string_buf(&argvars[1], buf));
13846 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847#endif
13848}
13849
13850/*
13851 * "histget()" function
13852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013854f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855{
13856#ifdef FEAT_CMDHIST
13857 int type;
13858 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013859 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13862 if (str == NULL)
13863 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013865 {
13866 type = get_histtype(str);
13867 if (argvars[1].v_type == VAR_UNKNOWN)
13868 idx = get_history_idx(type);
13869 else
13870 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13871 /* -1 on type error */
13872 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013875 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013877 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878}
13879
13880/*
13881 * "histnr()" function
13882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013884f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885{
13886 int i;
13887
13888#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013889 char_u *history = get_tv_string_chk(&argvars[0]);
13890
13891 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892 if (i >= HIST_CMD && i < HIST_COUNT)
13893 i = get_history_idx(i);
13894 else
13895#endif
13896 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013897 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898}
13899
13900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 * "highlightID(name)" function
13902 */
13903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013904f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013906 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907}
13908
13909/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013910 * "highlight_exists()" function
13911 */
13912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013913f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013914{
13915 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13916}
13917
13918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 * "hostname()" function
13920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013922f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923{
13924 char_u hostname[256];
13925
13926 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013927 rettv->v_type = VAR_STRING;
13928 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929}
13930
13931/*
13932 * iconv() function
13933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013935f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936{
13937#ifdef FEAT_MBYTE
13938 char_u buf1[NUMBUFLEN];
13939 char_u buf2[NUMBUFLEN];
13940 char_u *from, *to, *str;
13941 vimconv_T vimconv;
13942#endif
13943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013944 rettv->v_type = VAR_STRING;
13945 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946
13947#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013948 str = get_tv_string(&argvars[0]);
13949 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13950 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 vimconv.vc_type = CONV_NONE;
13952 convert_setup(&vimconv, from, to);
13953
13954 /* If the encodings are equal, no conversion needed. */
13955 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013956 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013958 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959
13960 convert_setup(&vimconv, NULL, NULL);
13961 vim_free(from);
13962 vim_free(to);
13963#endif
13964}
13965
13966/*
13967 * "indent()" function
13968 */
13969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013970f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971{
13972 linenr_T lnum;
13973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013974 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013976 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013978 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979}
13980
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013981/*
13982 * "index()" function
13983 */
13984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013985f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013986{
Bram Moolenaar33570922005-01-25 22:26:29 +000013987 list_T *l;
13988 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013989 long idx = 0;
13990 int ic = FALSE;
13991
13992 rettv->vval.v_number = -1;
13993 if (argvars[0].v_type != VAR_LIST)
13994 {
13995 EMSG(_(e_listreq));
13996 return;
13997 }
13998 l = argvars[0].vval.v_list;
13999 if (l != NULL)
14000 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014001 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014002 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014003 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014004 int error = FALSE;
14005
Bram Moolenaar758711c2005-02-02 23:11:38 +000014006 /* Start at specified item. Use the cached index that list_find()
14007 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014008 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014009 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014010 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014011 ic = get_tv_number_chk(&argvars[3], &error);
14012 if (error)
14013 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014014 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014015
Bram Moolenaar758711c2005-02-02 23:11:38 +000014016 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014017 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014018 {
14019 rettv->vval.v_number = idx;
14020 break;
14021 }
14022 }
14023}
14024
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025static int inputsecret_flag = 0;
14026
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014027static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014028
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014030 * This function is used by f_input() and f_inputdialog() functions. The third
14031 * argument to f_input() specifies the type of completion to use at the
14032 * prompt. The third argument to f_inputdialog() specifies the value to return
14033 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034 */
14035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014036get_user_input(
14037 typval_T *argvars,
14038 typval_T *rettv,
14039 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014041 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042 char_u *p = NULL;
14043 int c;
14044 char_u buf[NUMBUFLEN];
14045 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014046 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014047 int xp_type = EXPAND_NOTHING;
14048 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014050 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014051 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052
14053#ifdef NO_CONSOLE_INPUT
14054 /* While starting up, there is no place to enter text. */
14055 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057#endif
14058
14059 cmd_silent = FALSE; /* Want to see the prompt. */
14060 if (prompt != NULL)
14061 {
14062 /* Only the part of the message after the last NL is considered as
14063 * prompt for the command line */
14064 p = vim_strrchr(prompt, '\n');
14065 if (p == NULL)
14066 p = prompt;
14067 else
14068 {
14069 ++p;
14070 c = *p;
14071 *p = NUL;
14072 msg_start();
14073 msg_clr_eos();
14074 msg_puts_attr(prompt, echo_attr);
14075 msg_didout = FALSE;
14076 msg_starthere();
14077 *p = c;
14078 }
14079 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014081 if (argvars[1].v_type != VAR_UNKNOWN)
14082 {
14083 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14084 if (defstr != NULL)
14085 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014087 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014088 {
14089 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014090 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014091 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014092
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014093 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014094 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014095
Bram Moolenaar4463f292005-09-25 22:20:24 +000014096 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14097 if (xp_name == NULL)
14098 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014099
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014100 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014101
Bram Moolenaar4463f292005-09-25 22:20:24 +000014102 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14103 &xp_arg) == FAIL)
14104 return;
14105 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014106 }
14107
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014108 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014109 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014110 int save_ex_normal_busy = ex_normal_busy;
14111 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014112 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014113 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14114 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014115 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014116 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014117 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014118 && argvars[1].v_type != VAR_UNKNOWN
14119 && argvars[2].v_type != VAR_UNKNOWN)
14120 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14121 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014122
14123 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014125 /* since the user typed this, no need to wait for return */
14126 need_wait_return = FALSE;
14127 msg_didout = FALSE;
14128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129 cmd_silent = cmd_silent_save;
14130}
14131
14132/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014133 * "input()" function
14134 * Also handles inputsecret() when inputsecret is set.
14135 */
14136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014137f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014138{
14139 get_user_input(argvars, rettv, FALSE);
14140}
14141
14142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 * "inputdialog()" function
14144 */
14145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014146f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147{
14148#if defined(FEAT_GUI_TEXTDIALOG)
14149 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14150 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14151 {
14152 char_u *message;
14153 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014154 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014156 message = get_tv_string_chk(&argvars[0]);
14157 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014158 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014159 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160 else
14161 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014162 if (message != NULL && defstr != NULL
14163 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014164 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014165 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166 else
14167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014168 if (message != NULL && defstr != NULL
14169 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014170 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014171 rettv->vval.v_string = vim_strsave(
14172 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014174 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014176 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 }
14178 else
14179#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014180 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181}
14182
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014183/*
14184 * "inputlist()" function
14185 */
14186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014187f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014188{
14189 listitem_T *li;
14190 int selected;
14191 int mouse_used;
14192
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014193#ifdef NO_CONSOLE_INPUT
14194 /* While starting up, there is no place to enter text. */
14195 if (no_console_input())
14196 return;
14197#endif
14198 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14199 {
14200 EMSG2(_(e_listarg), "inputlist()");
14201 return;
14202 }
14203
14204 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014205 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014206 lines_left = Rows; /* avoid more prompt */
14207 msg_scroll = TRUE;
14208 msg_clr_eos();
14209
14210 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14211 {
14212 msg_puts(get_tv_string(&li->li_tv));
14213 msg_putchar('\n');
14214 }
14215
14216 /* Ask for choice. */
14217 selected = prompt_for_number(&mouse_used);
14218 if (mouse_used)
14219 selected -= lines_left;
14220
14221 rettv->vval.v_number = selected;
14222}
14223
14224
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14226
14227/*
14228 * "inputrestore()" function
14229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014231f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232{
14233 if (ga_userinput.ga_len > 0)
14234 {
14235 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14237 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014238 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 }
14240 else if (p_verbose > 1)
14241 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014242 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014243 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244 }
14245}
14246
14247/*
14248 * "inputsave()" function
14249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014251f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014253 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254 if (ga_grow(&ga_userinput, 1) == OK)
14255 {
14256 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14257 + ga_userinput.ga_len);
14258 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014259 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 }
14261 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014262 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263}
14264
14265/*
14266 * "inputsecret()" function
14267 */
14268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014269f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270{
14271 ++cmdline_star;
14272 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014273 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 --cmdline_star;
14275 --inputsecret_flag;
14276}
14277
14278/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014279 * "insert()" function
14280 */
14281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014282f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014283{
14284 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014285 listitem_T *item;
14286 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014287 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014288
14289 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014290 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014291 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014292 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014293 {
14294 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014295 before = get_tv_number_chk(&argvars[2], &error);
14296 if (error)
14297 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014298
Bram Moolenaar758711c2005-02-02 23:11:38 +000014299 if (before == l->lv_len)
14300 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014301 else
14302 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014303 item = list_find(l, before);
14304 if (item == NULL)
14305 {
14306 EMSGN(_(e_listidx), before);
14307 l = NULL;
14308 }
14309 }
14310 if (l != NULL)
14311 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014312 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014313 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014314 }
14315 }
14316}
14317
14318/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014319 * "invert(expr)" function
14320 */
14321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014322f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014323{
14324 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14325}
14326
14327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 * "isdirectory()" function
14329 */
14330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014331f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014333 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334}
14335
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014336/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014337 * "islocked()" function
14338 */
14339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014340f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014341{
14342 lval_T lv;
14343 char_u *end;
14344 dictitem_T *di;
14345
14346 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014347 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14348 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014349 if (end != NULL && lv.ll_name != NULL)
14350 {
14351 if (*end != NUL)
14352 EMSG(_(e_trailing));
14353 else
14354 {
14355 if (lv.ll_tv == NULL)
14356 {
14357 if (check_changedtick(lv.ll_name))
14358 rettv->vval.v_number = 1; /* always locked */
14359 else
14360 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014361 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014362 if (di != NULL)
14363 {
14364 /* Consider a variable locked when:
14365 * 1. the variable itself is locked
14366 * 2. the value of the variable is locked.
14367 * 3. the List or Dict value is locked.
14368 */
14369 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14370 || tv_islocked(&di->di_tv));
14371 }
14372 }
14373 }
14374 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014375 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014376 else if (lv.ll_newkey != NULL)
14377 EMSG2(_(e_dictkey), lv.ll_newkey);
14378 else if (lv.ll_list != NULL)
14379 /* List item. */
14380 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14381 else
14382 /* Dictionary item. */
14383 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14384 }
14385 }
14386
14387 clear_lval(&lv);
14388}
14389
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014390static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014391
14392/*
14393 * Turn a dict into a list:
14394 * "what" == 0: list of keys
14395 * "what" == 1: list of values
14396 * "what" == 2: list of items
14397 */
14398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014399dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014400{
Bram Moolenaar33570922005-01-25 22:26:29 +000014401 list_T *l2;
14402 dictitem_T *di;
14403 hashitem_T *hi;
14404 listitem_T *li;
14405 listitem_T *li2;
14406 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014407 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014408
Bram Moolenaar8c711452005-01-14 21:53:12 +000014409 if (argvars[0].v_type != VAR_DICT)
14410 {
14411 EMSG(_(e_dictreq));
14412 return;
14413 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014414 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014415 return;
14416
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014417 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014418 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014419
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014420 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014421 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014422 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014423 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014424 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014425 --todo;
14426 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014427
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014428 li = listitem_alloc();
14429 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014430 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014431 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014432
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014433 if (what == 0)
14434 {
14435 /* keys() */
14436 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014437 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014438 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14439 }
14440 else if (what == 1)
14441 {
14442 /* values() */
14443 copy_tv(&di->di_tv, &li->li_tv);
14444 }
14445 else
14446 {
14447 /* items() */
14448 l2 = list_alloc();
14449 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014450 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014451 li->li_tv.vval.v_list = l2;
14452 if (l2 == NULL)
14453 break;
14454 ++l2->lv_refcount;
14455
14456 li2 = listitem_alloc();
14457 if (li2 == NULL)
14458 break;
14459 list_append(l2, li2);
14460 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014461 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014462 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14463
14464 li2 = listitem_alloc();
14465 if (li2 == NULL)
14466 break;
14467 list_append(l2, li2);
14468 copy_tv(&di->di_tv, &li2->li_tv);
14469 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014470 }
14471 }
14472}
14473
14474/*
14475 * "items(dict)" function
14476 */
14477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014478f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014479{
14480 dict_list(argvars, rettv, 2);
14481}
14482
Bram Moolenaar835dc632016-02-07 14:27:38 +010014483#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014484
14485# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014486/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014487 * "job_getchannel()" function
14488 */
14489 static void
14490f_job_getchannel(typval_T *argvars, typval_T *rettv)
14491{
14492 if (argvars[0].v_type != VAR_JOB)
14493 EMSG(_(e_invarg));
14494 else
14495 {
14496 job_T *job = argvars[0].vval.v_job;
14497
Bram Moolenaar77073442016-02-13 23:23:53 +010014498 rettv->v_type = VAR_CHANNEL;
14499 rettv->vval.v_channel = job->jv_channel;
14500 if (job->jv_channel != NULL)
14501 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014502 }
14503}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014504# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014505
14506/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014507 * "job_start()" function
14508 */
14509 static void
14510f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14511{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014512 job_T *job;
14513 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014514#if defined(UNIX)
14515# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014516 char **argv = NULL;
14517 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014518#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014519 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014520#endif
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014521 jobopt_T options;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014522
14523 rettv->v_type = VAR_JOB;
14524 job = job_alloc();
14525 rettv->vval.v_job = job;
14526 if (job == NULL)
14527 return;
14528
14529 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014530
14531 /* Default mode is NL. */
14532 options.jo_mode = MODE_NL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010014533 options.jo_callback = NULL;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014534 if (argvars[1].v_type != VAR_UNKNOWN)
14535 {
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014536 if (argvars[1].v_type != VAR_DICT)
14537 {
14538 EMSG(_(e_invarg));
14539 return;
14540 }
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010014541 if (get_job_options(argvars[1].vval.v_dict, &options) == FAIL)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014542 return;
14543 }
14544
Bram Moolenaar835dc632016-02-07 14:27:38 +010014545#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014546 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014547#endif
14548
14549 if (argvars[0].v_type == VAR_STRING)
14550 {
14551 /* Command is a string. */
14552 cmd = argvars[0].vval.v_string;
14553#ifdef USE_ARGV
14554 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14555 return;
14556 argv[argc] = NULL;
14557#endif
14558 }
14559 else if (argvars[0].v_type != VAR_LIST
14560 || argvars[0].vval.v_list == NULL
14561 || argvars[0].vval.v_list->lv_len < 1)
14562 {
14563 EMSG(_(e_invarg));
14564 return;
14565 }
14566 else
14567 {
14568 list_T *l = argvars[0].vval.v_list;
14569 listitem_T *li;
14570 char_u *s;
14571
14572#ifdef USE_ARGV
14573 /* Pass argv[] to mch_call_shell(). */
14574 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14575 if (argv == NULL)
14576 return;
14577#endif
14578 for (li = l->lv_first; li != NULL; li = li->li_next)
14579 {
14580 s = get_tv_string_chk(&li->li_tv);
14581 if (s == NULL)
14582 goto theend;
14583#ifdef USE_ARGV
14584 argv[argc++] = (char *)s;
14585#else
14586 if (li != l->lv_first)
14587 {
14588 s = vim_strsave_shellescape(s, FALSE, TRUE);
14589 if (s == NULL)
14590 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014591 ga_concat(&ga, s);
14592 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014593 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014594 else
14595 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014596 if (li->li_next != NULL)
14597 ga_append(&ga, ' ');
14598#endif
14599 }
14600#ifdef USE_ARGV
14601 argv[argc] = NULL;
14602#else
14603 cmd = ga.ga_data;
14604#endif
14605 }
14606#ifdef USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014607 mch_start_job(argv, job, &options);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014608#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014609 mch_start_job((char *)cmd, job, &options);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014610#endif
14611
14612theend:
14613#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014614 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014615#else
14616 vim_free(ga.ga_data);
14617#endif
14618}
14619
14620/*
14621 * "job_status()" function
14622 */
14623 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014624f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014625{
14626 char *result;
14627
14628 if (argvars[0].v_type != VAR_JOB)
14629 EMSG(_(e_invarg));
14630 else
14631 {
14632 job_T *job = argvars[0].vval.v_job;
14633
14634 if (job->jv_status == JOB_ENDED)
14635 /* No need to check, dead is dead. */
14636 result = "dead";
14637 else if (job->jv_status == JOB_FAILED)
14638 result = "fail";
14639 else
14640 result = mch_job_status(job);
14641 rettv->v_type = VAR_STRING;
14642 rettv->vval.v_string = vim_strsave((char_u *)result);
14643 }
14644}
14645
14646/*
14647 * "job_stop()" function
14648 */
14649 static void
14650f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14651{
14652 if (argvars[0].v_type != VAR_JOB)
14653 EMSG(_(e_invarg));
14654 else
14655 {
14656 char_u *arg;
14657
14658 if (argvars[1].v_type == VAR_UNKNOWN)
14659 arg = (char_u *)"";
14660 else
14661 {
14662 arg = get_tv_string_chk(&argvars[1]);
14663 if (arg == NULL)
14664 {
14665 EMSG(_(e_invarg));
14666 return;
14667 }
14668 }
14669 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14670 rettv->vval.v_number = 0;
14671 else
14672 rettv->vval.v_number = 1;
14673 }
14674}
14675#endif
14676
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014678 * "join()" function
14679 */
14680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014681f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014682{
14683 garray_T ga;
14684 char_u *sep;
14685
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014686 if (argvars[0].v_type != VAR_LIST)
14687 {
14688 EMSG(_(e_listreq));
14689 return;
14690 }
14691 if (argvars[0].vval.v_list == NULL)
14692 return;
14693 if (argvars[1].v_type == VAR_UNKNOWN)
14694 sep = (char_u *)" ";
14695 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014696 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014697
14698 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014699
14700 if (sep != NULL)
14701 {
14702 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014703 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014704 ga_append(&ga, NUL);
14705 rettv->vval.v_string = (char_u *)ga.ga_data;
14706 }
14707 else
14708 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014709}
14710
14711/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014712 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014713 */
14714 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014715f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014716{
14717 js_read_T reader;
14718
14719 reader.js_buf = get_tv_string(&argvars[0]);
14720 reader.js_fill = NULL;
14721 reader.js_used = 0;
14722 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14723 EMSG(_(e_invarg));
14724}
14725
14726/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014727 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014728 */
14729 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014730f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014731{
14732 rettv->v_type = VAR_STRING;
14733 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14734}
14735
14736/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014737 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014738 */
14739 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014740f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014741{
14742 js_read_T reader;
14743
14744 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014745 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014746 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014747 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014748 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014749}
14750
14751/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014752 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014753 */
14754 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014755f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014756{
14757 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014758 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014759}
14760
14761/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014762 * "keys()" function
14763 */
14764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014765f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014766{
14767 dict_list(argvars, rettv, 0);
14768}
14769
14770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771 * "last_buffer_nr()" function.
14772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014774f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775{
14776 int n = 0;
14777 buf_T *buf;
14778
14779 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14780 if (n < buf->b_fnum)
14781 n = buf->b_fnum;
14782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014783 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014784}
14785
14786/*
14787 * "len()" function
14788 */
14789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014790f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014791{
14792 switch (argvars[0].v_type)
14793 {
14794 case VAR_STRING:
14795 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014796 rettv->vval.v_number = (varnumber_T)STRLEN(
14797 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014798 break;
14799 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014800 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014801 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014802 case VAR_DICT:
14803 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14804 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014805 case VAR_UNKNOWN:
14806 case VAR_SPECIAL:
14807 case VAR_FLOAT:
14808 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014809 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014810 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014811 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014812 break;
14813 }
14814}
14815
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014816static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014817
14818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014819libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014820{
14821#ifdef FEAT_LIBCALL
14822 char_u *string_in;
14823 char_u **string_result;
14824 int nr_result;
14825#endif
14826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014828 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014829 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014830
14831 if (check_restricted() || check_secure())
14832 return;
14833
14834#ifdef FEAT_LIBCALL
14835 /* The first two args must be strings, otherwise its meaningless */
14836 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14837 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014838 string_in = NULL;
14839 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014840 string_in = argvars[2].vval.v_string;
14841 if (type == VAR_NUMBER)
14842 string_result = NULL;
14843 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014844 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014845 if (mch_libcall(argvars[0].vval.v_string,
14846 argvars[1].vval.v_string,
14847 string_in,
14848 argvars[2].vval.v_number,
14849 string_result,
14850 &nr_result) == OK
14851 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014852 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014853 }
14854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855}
14856
14857/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014858 * "libcall()" function
14859 */
14860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014861f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014862{
14863 libcall_common(argvars, rettv, VAR_STRING);
14864}
14865
14866/*
14867 * "libcallnr()" function
14868 */
14869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014870f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014871{
14872 libcall_common(argvars, rettv, VAR_NUMBER);
14873}
14874
14875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876 * "line(string)" function
14877 */
14878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014879f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880{
14881 linenr_T lnum = 0;
14882 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014883 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014885 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886 if (fp != NULL)
14887 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014888 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889}
14890
14891/*
14892 * "line2byte(lnum)" function
14893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014895f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896{
14897#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014898 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899#else
14900 linenr_T lnum;
14901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014902 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014903 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014904 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014906 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14907 if (rettv->vval.v_number >= 0)
14908 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909#endif
14910}
14911
14912/*
14913 * "lispindent(lnum)" function
14914 */
14915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014916f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917{
14918#ifdef FEAT_LISP
14919 pos_T pos;
14920 linenr_T lnum;
14921
14922 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014923 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14925 {
14926 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014927 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928 curwin->w_cursor = pos;
14929 }
14930 else
14931#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014932 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933}
14934
14935/*
14936 * "localtime()" function
14937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014939f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014941 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942}
14943
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014944static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945
14946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014947get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014948{
14949 char_u *keys;
14950 char_u *which;
14951 char_u buf[NUMBUFLEN];
14952 char_u *keys_buf = NULL;
14953 char_u *rhs;
14954 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014955 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014956 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014957 mapblock_T *mp;
14958 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959
14960 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014961 rettv->v_type = VAR_STRING;
14962 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014964 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965 if (*keys == NUL)
14966 return;
14967
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014968 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014970 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014971 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014972 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014973 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014974 if (argvars[3].v_type != VAR_UNKNOWN)
14975 get_dict = get_tv_number(&argvars[3]);
14976 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978 else
14979 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014980 if (which == NULL)
14981 return;
14982
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 mode = get_map_mode(&which, 0);
14984
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014985 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014986 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014988
14989 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014991 /* Return a string. */
14992 if (rhs != NULL)
14993 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994
Bram Moolenaarbd743252010-10-20 21:23:33 +020014995 }
14996 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14997 {
14998 /* Return a dictionary. */
14999 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15000 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15001 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002
Bram Moolenaarbd743252010-10-20 21:23:33 +020015003 dict_add_nr_str(dict, "lhs", 0L, lhs);
15004 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15005 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15006 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15007 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15008 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15009 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015010 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015011 dict_add_nr_str(dict, "mode", 0L, mapmode);
15012
15013 vim_free(lhs);
15014 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 }
15016}
15017
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015018#ifdef FEAT_FLOAT
15019/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015020 * "log()" function
15021 */
15022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015023f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015024{
15025 float_T f;
15026
15027 rettv->v_type = VAR_FLOAT;
15028 if (get_float_arg(argvars, &f) == OK)
15029 rettv->vval.v_float = log(f);
15030 else
15031 rettv->vval.v_float = 0.0;
15032}
15033
15034/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015035 * "log10()" function
15036 */
15037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015038f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015039{
15040 float_T f;
15041
15042 rettv->v_type = VAR_FLOAT;
15043 if (get_float_arg(argvars, &f) == OK)
15044 rettv->vval.v_float = log10(f);
15045 else
15046 rettv->vval.v_float = 0.0;
15047}
15048#endif
15049
Bram Moolenaar1dced572012-04-05 16:54:08 +020015050#ifdef FEAT_LUA
15051/*
15052 * "luaeval()" function
15053 */
15054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015055f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015056{
15057 char_u *str;
15058 char_u buf[NUMBUFLEN];
15059
15060 str = get_tv_string_buf(&argvars[0], buf);
15061 do_luaeval(str, argvars + 1, rettv);
15062}
15063#endif
15064
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015066 * "map()" function
15067 */
15068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015069f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015070{
15071 filter_map(argvars, rettv, TRUE);
15072}
15073
15074/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076 */
15077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015078f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015080 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081}
15082
15083/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085 */
15086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015087f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090}
15091
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015092static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093
15094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015095find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015096{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015097 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015098 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015099 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100 char_u *pat;
15101 regmatch_T regmatch;
15102 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015103 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104 char_u *save_cpo;
15105 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015106 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015107 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015108 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015109 list_T *l = NULL;
15110 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015111 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015112 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113
15114 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15115 save_cpo = p_cpo;
15116 p_cpo = (char_u *)"";
15117
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015118 rettv->vval.v_number = -1;
15119 if (type == 3)
15120 {
15121 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015122 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015123 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015124 }
15125 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015127 rettv->v_type = VAR_STRING;
15128 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015131 if (argvars[0].v_type == VAR_LIST)
15132 {
15133 if ((l = argvars[0].vval.v_list) == NULL)
15134 goto theend;
15135 li = l->lv_first;
15136 }
15137 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015138 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015139 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015140 len = (long)STRLEN(str);
15141 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015143 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15144 if (pat == NULL)
15145 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015146
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015147 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015149 int error = FALSE;
15150
15151 start = get_tv_number_chk(&argvars[2], &error);
15152 if (error)
15153 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015154 if (l != NULL)
15155 {
15156 li = list_find(l, start);
15157 if (li == NULL)
15158 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015159 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015160 }
15161 else
15162 {
15163 if (start < 0)
15164 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015165 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015166 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015167 /* When "count" argument is there ignore matches before "start",
15168 * otherwise skip part of the string. Differs when pattern is "^"
15169 * or "\<". */
15170 if (argvars[3].v_type != VAR_UNKNOWN)
15171 startcol = start;
15172 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015173 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015174 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015175 len -= start;
15176 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015177 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015178
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015179 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015180 nth = get_tv_number_chk(&argvars[3], &error);
15181 if (error)
15182 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183 }
15184
15185 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15186 if (regmatch.regprog != NULL)
15187 {
15188 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015189
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015190 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015191 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015192 if (l != NULL)
15193 {
15194 if (li == NULL)
15195 {
15196 match = FALSE;
15197 break;
15198 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015199 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015200 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015201 if (str == NULL)
15202 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015203 }
15204
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015205 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015206
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015207 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015208 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015209 if (l == NULL && !match)
15210 break;
15211
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015212 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015213 if (l != NULL)
15214 {
15215 li = li->li_next;
15216 ++idx;
15217 }
15218 else
15219 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015220#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015221 startcol = (colnr_T)(regmatch.startp[0]
15222 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015223#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015224 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015225#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015226 if (startcol > (colnr_T)len
15227 || str + startcol <= regmatch.startp[0])
15228 {
15229 match = FALSE;
15230 break;
15231 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015232 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015233 }
15234
15235 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015237 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015238 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015239 int i;
15240
15241 /* return list with matched string and submatches */
15242 for (i = 0; i < NSUBEXP; ++i)
15243 {
15244 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015245 {
15246 if (list_append_string(rettv->vval.v_list,
15247 (char_u *)"", 0) == FAIL)
15248 break;
15249 }
15250 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015251 regmatch.startp[i],
15252 (int)(regmatch.endp[i] - regmatch.startp[i]))
15253 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015254 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015255 }
15256 }
15257 else if (type == 2)
15258 {
15259 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015260 if (l != NULL)
15261 copy_tv(&li->li_tv, rettv);
15262 else
15263 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015265 }
15266 else if (l != NULL)
15267 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268 else
15269 {
15270 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015271 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272 (varnumber_T)(regmatch.startp[0] - str);
15273 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015274 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015276 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277 }
15278 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015279 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015280 }
15281
15282theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015283 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284 p_cpo = save_cpo;
15285}
15286
15287/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015288 * "match()" function
15289 */
15290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015291f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015292{
15293 find_some_match(argvars, rettv, 1);
15294}
15295
15296/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015297 * "matchadd()" function
15298 */
15299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015300f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015301{
15302#ifdef FEAT_SEARCH_EXTRA
15303 char_u buf[NUMBUFLEN];
15304 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15305 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15306 int prio = 10; /* default priority */
15307 int id = -1;
15308 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015309 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015310
15311 rettv->vval.v_number = -1;
15312
15313 if (grp == NULL || pat == NULL)
15314 return;
15315 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015316 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015317 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015318 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015319 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015320 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015321 if (argvars[4].v_type != VAR_UNKNOWN)
15322 {
15323 if (argvars[4].v_type != VAR_DICT)
15324 {
15325 EMSG(_(e_dictreq));
15326 return;
15327 }
15328 if (dict_find(argvars[4].vval.v_dict,
15329 (char_u *)"conceal", -1) != NULL)
15330 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15331 (char_u *)"conceal", FALSE);
15332 }
15333 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015334 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015335 if (error == TRUE)
15336 return;
15337 if (id >= 1 && id <= 3)
15338 {
15339 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15340 return;
15341 }
15342
Bram Moolenaar6561d522015-07-21 15:48:27 +020015343 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15344 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015345#endif
15346}
15347
15348/*
15349 * "matchaddpos()" function
15350 */
15351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015352f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015353{
15354#ifdef FEAT_SEARCH_EXTRA
15355 char_u buf[NUMBUFLEN];
15356 char_u *group;
15357 int prio = 10;
15358 int id = -1;
15359 int error = FALSE;
15360 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015361 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015362
15363 rettv->vval.v_number = -1;
15364
15365 group = get_tv_string_buf_chk(&argvars[0], buf);
15366 if (group == NULL)
15367 return;
15368
15369 if (argvars[1].v_type != VAR_LIST)
15370 {
15371 EMSG2(_(e_listarg), "matchaddpos()");
15372 return;
15373 }
15374 l = argvars[1].vval.v_list;
15375 if (l == NULL)
15376 return;
15377
15378 if (argvars[2].v_type != VAR_UNKNOWN)
15379 {
15380 prio = get_tv_number_chk(&argvars[2], &error);
15381 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015382 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015383 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015384 if (argvars[4].v_type != VAR_UNKNOWN)
15385 {
15386 if (argvars[4].v_type != VAR_DICT)
15387 {
15388 EMSG(_(e_dictreq));
15389 return;
15390 }
15391 if (dict_find(argvars[4].vval.v_dict,
15392 (char_u *)"conceal", -1) != NULL)
15393 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15394 (char_u *)"conceal", FALSE);
15395 }
15396 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015397 }
15398 if (error == TRUE)
15399 return;
15400
15401 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15402 if (id == 1 || id == 2)
15403 {
15404 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15405 return;
15406 }
15407
Bram Moolenaar6561d522015-07-21 15:48:27 +020015408 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15409 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015410#endif
15411}
15412
15413/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015414 * "matcharg()" function
15415 */
15416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015417f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015418{
15419 if (rettv_list_alloc(rettv) == OK)
15420 {
15421#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015422 int id = get_tv_number(&argvars[0]);
15423 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015424
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015425 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015426 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015427 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15428 {
15429 list_append_string(rettv->vval.v_list,
15430 syn_id2name(m->hlg_id), -1);
15431 list_append_string(rettv->vval.v_list, m->pattern, -1);
15432 }
15433 else
15434 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015435 list_append_string(rettv->vval.v_list, NULL, -1);
15436 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015437 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015438 }
15439#endif
15440 }
15441}
15442
15443/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015444 * "matchdelete()" function
15445 */
15446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015447f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015448{
15449#ifdef FEAT_SEARCH_EXTRA
15450 rettv->vval.v_number = match_delete(curwin,
15451 (int)get_tv_number(&argvars[0]), TRUE);
15452#endif
15453}
15454
15455/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015456 * "matchend()" function
15457 */
15458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015459f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015460{
15461 find_some_match(argvars, rettv, 0);
15462}
15463
15464/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015465 * "matchlist()" function
15466 */
15467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015468f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015469{
15470 find_some_match(argvars, rettv, 3);
15471}
15472
15473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015474 * "matchstr()" function
15475 */
15476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015477f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015478{
15479 find_some_match(argvars, rettv, 2);
15480}
15481
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015482static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015483
15484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015485max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015486{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015487 long n = 0;
15488 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015489 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015490
15491 if (argvars[0].v_type == VAR_LIST)
15492 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015493 list_T *l;
15494 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015495
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015496 l = argvars[0].vval.v_list;
15497 if (l != NULL)
15498 {
15499 li = l->lv_first;
15500 if (li != NULL)
15501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015502 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015503 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015504 {
15505 li = li->li_next;
15506 if (li == NULL)
15507 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015508 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015509 if (domax ? i > n : i < n)
15510 n = i;
15511 }
15512 }
15513 }
15514 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015515 else if (argvars[0].v_type == VAR_DICT)
15516 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015517 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015518 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015519 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015520 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015521
15522 d = argvars[0].vval.v_dict;
15523 if (d != NULL)
15524 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015525 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015526 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015527 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015528 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015529 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015530 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015531 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015532 if (first)
15533 {
15534 n = i;
15535 first = FALSE;
15536 }
15537 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015538 n = i;
15539 }
15540 }
15541 }
15542 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015543 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015544 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015545 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015546}
15547
15548/*
15549 * "max()" function
15550 */
15551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015552f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015553{
15554 max_min(argvars, rettv, TRUE);
15555}
15556
15557/*
15558 * "min()" function
15559 */
15560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015561f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015562{
15563 max_min(argvars, rettv, FALSE);
15564}
15565
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015566static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015567
15568/*
15569 * Create the directory in which "dir" is located, and higher levels when
15570 * needed.
15571 */
15572 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015573mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015574{
15575 char_u *p;
15576 char_u *updir;
15577 int r = FAIL;
15578
15579 /* Get end of directory name in "dir".
15580 * We're done when it's "/" or "c:/". */
15581 p = gettail_sep(dir);
15582 if (p <= get_past_head(dir))
15583 return OK;
15584
15585 /* If the directory exists we're done. Otherwise: create it.*/
15586 updir = vim_strnsave(dir, (int)(p - dir));
15587 if (updir == NULL)
15588 return FAIL;
15589 if (mch_isdir(updir))
15590 r = OK;
15591 else if (mkdir_recurse(updir, prot) == OK)
15592 r = vim_mkdir_emsg(updir, prot);
15593 vim_free(updir);
15594 return r;
15595}
15596
15597#ifdef vim_mkdir
15598/*
15599 * "mkdir()" function
15600 */
15601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015602f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015603{
15604 char_u *dir;
15605 char_u buf[NUMBUFLEN];
15606 int prot = 0755;
15607
15608 rettv->vval.v_number = FAIL;
15609 if (check_restricted() || check_secure())
15610 return;
15611
15612 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015613 if (*dir == NUL)
15614 rettv->vval.v_number = FAIL;
15615 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015616 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015617 if (*gettail(dir) == NUL)
15618 /* remove trailing slashes */
15619 *gettail_sep(dir) = NUL;
15620
15621 if (argvars[1].v_type != VAR_UNKNOWN)
15622 {
15623 if (argvars[2].v_type != VAR_UNKNOWN)
15624 prot = get_tv_number_chk(&argvars[2], NULL);
15625 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15626 mkdir_recurse(dir, prot);
15627 }
15628 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015629 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015630}
15631#endif
15632
Bram Moolenaar0d660222005-01-07 21:51:51 +000015633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 * "mode()" function
15635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015637f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015639 char_u buf[3];
15640
15641 buf[1] = NUL;
15642 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643
Bram Moolenaar071d4272004-06-13 20:20:40 +000015644 if (VIsual_active)
15645 {
15646 if (VIsual_select)
15647 buf[0] = VIsual_mode + 's' - 'v';
15648 else
15649 buf[0] = VIsual_mode;
15650 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015651 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015652 || State == CONFIRM)
15653 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015655 if (State == ASKMORE)
15656 buf[1] = 'm';
15657 else if (State == CONFIRM)
15658 buf[1] = '?';
15659 }
15660 else if (State == EXTERNCMD)
15661 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662 else if (State & INSERT)
15663 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015664#ifdef FEAT_VREPLACE
15665 if (State & VREPLACE_FLAG)
15666 {
15667 buf[0] = 'R';
15668 buf[1] = 'v';
15669 }
15670 else
15671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 if (State & REPLACE_FLAG)
15673 buf[0] = 'R';
15674 else
15675 buf[0] = 'i';
15676 }
15677 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015678 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015679 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015680 if (exmode_active)
15681 buf[1] = 'v';
15682 }
15683 else if (exmode_active)
15684 {
15685 buf[0] = 'c';
15686 buf[1] = 'e';
15687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015689 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015691 if (finish_op)
15692 buf[1] = 'o';
15693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015695 /* Clear out the minor mode when the argument is not a non-zero number or
15696 * non-empty string. */
15697 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015698 buf[1] = NUL;
15699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015700 rettv->vval.v_string = vim_strsave(buf);
15701 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702}
15703
Bram Moolenaar429fa852013-04-15 12:27:36 +020015704#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015705/*
15706 * "mzeval()" function
15707 */
15708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015709f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015710{
15711 char_u *str;
15712 char_u buf[NUMBUFLEN];
15713
15714 str = get_tv_string_buf(&argvars[0], buf);
15715 do_mzeval(str, rettv);
15716}
Bram Moolenaar75676462013-01-30 14:55:42 +010015717
15718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015719mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015720{
15721 typval_T argvars[3];
15722
15723 argvars[0].v_type = VAR_STRING;
15724 argvars[0].vval.v_string = name;
15725 copy_tv(args, &argvars[1]);
15726 argvars[2].v_type = VAR_UNKNOWN;
15727 f_call(argvars, rettv);
15728 clear_tv(&argvars[1]);
15729}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015730#endif
15731
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015733 * "nextnonblank()" function
15734 */
15735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015736f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015737{
15738 linenr_T lnum;
15739
15740 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015742 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015743 {
15744 lnum = 0;
15745 break;
15746 }
15747 if (*skipwhite(ml_get(lnum)) != NUL)
15748 break;
15749 }
15750 rettv->vval.v_number = lnum;
15751}
15752
15753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 * "nr2char()" function
15755 */
15756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015757f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758{
15759 char_u buf[NUMBUFLEN];
15760
15761#ifdef FEAT_MBYTE
15762 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015763 {
15764 int utf8 = 0;
15765
15766 if (argvars[1].v_type != VAR_UNKNOWN)
15767 utf8 = get_tv_number_chk(&argvars[1], NULL);
15768 if (utf8)
15769 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15770 else
15771 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773 else
15774#endif
15775 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015776 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 buf[1] = NUL;
15778 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015779 rettv->v_type = VAR_STRING;
15780 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015781}
15782
15783/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015784 * "or(expr, expr)" function
15785 */
15786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015787f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015788{
15789 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15790 | get_tv_number_chk(&argvars[1], NULL);
15791}
15792
15793/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015794 * "pathshorten()" function
15795 */
15796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015797f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015798{
15799 char_u *p;
15800
15801 rettv->v_type = VAR_STRING;
15802 p = get_tv_string_chk(&argvars[0]);
15803 if (p == NULL)
15804 rettv->vval.v_string = NULL;
15805 else
15806 {
15807 p = vim_strsave(p);
15808 rettv->vval.v_string = p;
15809 if (p != NULL)
15810 shorten_dir(p);
15811 }
15812}
15813
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015814#ifdef FEAT_PERL
15815/*
15816 * "perleval()" function
15817 */
15818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015819f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015820{
15821 char_u *str;
15822 char_u buf[NUMBUFLEN];
15823
15824 str = get_tv_string_buf(&argvars[0], buf);
15825 do_perleval(str, rettv);
15826}
15827#endif
15828
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015829#ifdef FEAT_FLOAT
15830/*
15831 * "pow()" function
15832 */
15833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015834f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015835{
15836 float_T fx, fy;
15837
15838 rettv->v_type = VAR_FLOAT;
15839 if (get_float_arg(argvars, &fx) == OK
15840 && get_float_arg(&argvars[1], &fy) == OK)
15841 rettv->vval.v_float = pow(fx, fy);
15842 else
15843 rettv->vval.v_float = 0.0;
15844}
15845#endif
15846
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015847/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015848 * "prevnonblank()" function
15849 */
15850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015851f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015852{
15853 linenr_T lnum;
15854
15855 lnum = get_tv_lnum(argvars);
15856 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15857 lnum = 0;
15858 else
15859 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15860 --lnum;
15861 rettv->vval.v_number = lnum;
15862}
15863
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015864/* This dummy va_list is here because:
15865 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15866 * - locally in the function results in a "used before set" warning
15867 * - using va_start() to initialize it gives "function with fixed args" error */
15868static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015869
Bram Moolenaar8c711452005-01-14 21:53:12 +000015870/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015871 * "printf()" function
15872 */
15873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015874f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015875{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015876 char_u buf[NUMBUFLEN];
15877 int len;
15878 char_u *s;
15879 int saved_did_emsg = did_emsg;
15880 char *fmt;
15881
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015882 rettv->v_type = VAR_STRING;
15883 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015884
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015885 /* Get the required length, allocate the buffer and do it for real. */
15886 did_emsg = FALSE;
15887 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15888 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15889 if (!did_emsg)
15890 {
15891 s = alloc(len + 1);
15892 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015893 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015894 rettv->vval.v_string = s;
15895 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015896 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015897 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015898 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015899}
15900
15901/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015902 * "pumvisible()" function
15903 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015905f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015906{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015907#ifdef FEAT_INS_EXPAND
15908 if (pum_visible())
15909 rettv->vval.v_number = 1;
15910#endif
15911}
15912
Bram Moolenaardb913952012-06-29 12:54:53 +020015913#ifdef FEAT_PYTHON3
15914/*
15915 * "py3eval()" function
15916 */
15917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015918f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015919{
15920 char_u *str;
15921 char_u buf[NUMBUFLEN];
15922
15923 str = get_tv_string_buf(&argvars[0], buf);
15924 do_py3eval(str, rettv);
15925}
15926#endif
15927
15928#ifdef FEAT_PYTHON
15929/*
15930 * "pyeval()" function
15931 */
15932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015933f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015934{
15935 char_u *str;
15936 char_u buf[NUMBUFLEN];
15937
15938 str = get_tv_string_buf(&argvars[0], buf);
15939 do_pyeval(str, rettv);
15940}
15941#endif
15942
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015943/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015944 * "range()" function
15945 */
15946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015947f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015948{
15949 long start;
15950 long end;
15951 long stride = 1;
15952 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015953 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015955 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015956 if (argvars[1].v_type == VAR_UNKNOWN)
15957 {
15958 end = start - 1;
15959 start = 0;
15960 }
15961 else
15962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015963 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015964 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015965 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015966 }
15967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015968 if (error)
15969 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015970 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015971 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015972 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015973 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015974 else
15975 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015976 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015977 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015978 if (list_append_number(rettv->vval.v_list,
15979 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015980 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015981 }
15982}
15983
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015984/*
15985 * "readfile()" function
15986 */
15987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015988f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015989{
15990 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015991 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015992 char_u *fname;
15993 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015994 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15995 int io_size = sizeof(buf);
15996 int readlen; /* size of last fread() */
15997 char_u *prev = NULL; /* previously read bytes, if any */
15998 long prevlen = 0; /* length of data in prev */
15999 long prevsize = 0; /* size of prev buffer */
16000 long maxline = MAXLNUM;
16001 long cnt = 0;
16002 char_u *p; /* position in buf */
16003 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016004
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016005 if (argvars[1].v_type != VAR_UNKNOWN)
16006 {
16007 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16008 binary = TRUE;
16009 if (argvars[2].v_type != VAR_UNKNOWN)
16010 maxline = get_tv_number(&argvars[2]);
16011 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016012
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016013 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016014 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016015
16016 /* Always open the file in binary mode, library functions have a mind of
16017 * their own about CR-LF conversion. */
16018 fname = get_tv_string(&argvars[0]);
16019 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16020 {
16021 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16022 return;
16023 }
16024
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016025 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016026 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016027 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016028
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016029 /* This for loop processes what was read, but is also entered at end
16030 * of file so that either:
16031 * - an incomplete line gets written
16032 * - a "binary" file gets an empty line at the end if it ends in a
16033 * newline. */
16034 for (p = buf, start = buf;
16035 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16036 ++p)
16037 {
16038 if (*p == '\n' || readlen <= 0)
16039 {
16040 listitem_T *li;
16041 char_u *s = NULL;
16042 long_u len = p - start;
16043
16044 /* Finished a line. Remove CRs before NL. */
16045 if (readlen > 0 && !binary)
16046 {
16047 while (len > 0 && start[len - 1] == '\r')
16048 --len;
16049 /* removal may cross back to the "prev" string */
16050 if (len == 0)
16051 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16052 --prevlen;
16053 }
16054 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016055 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016056 else
16057 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016058 /* Change "prev" buffer to be the right size. This way
16059 * the bytes are only copied once, and very long lines are
16060 * allocated only once. */
16061 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016062 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016063 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016064 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016065 prev = NULL; /* the list will own the string */
16066 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016067 }
16068 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016069 if (s == NULL)
16070 {
16071 do_outofmem_msg((long_u) prevlen + len + 1);
16072 failed = TRUE;
16073 break;
16074 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016075
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016076 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016077 {
16078 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016079 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016080 break;
16081 }
16082 li->li_tv.v_type = VAR_STRING;
16083 li->li_tv.v_lock = 0;
16084 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016085 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016086
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016087 start = p + 1; /* step over newline */
16088 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016089 break;
16090 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016091 else if (*p == NUL)
16092 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016093#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016094 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16095 * when finding the BF and check the previous two bytes. */
16096 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016097 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016098 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16099 * + 1, these may be in the "prev" string. */
16100 char_u back1 = p >= buf + 1 ? p[-1]
16101 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16102 char_u back2 = p >= buf + 2 ? p[-2]
16103 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16104 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016105
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016106 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016107 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016108 char_u *dest = p - 2;
16109
16110 /* Usually a BOM is at the beginning of a file, and so at
16111 * the beginning of a line; then we can just step over it.
16112 */
16113 if (start == dest)
16114 start = p + 1;
16115 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016116 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016117 /* have to shuffle buf to close gap */
16118 int adjust_prevlen = 0;
16119
16120 if (dest < buf)
16121 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016122 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016123 dest = buf;
16124 }
16125 if (readlen > p - buf + 1)
16126 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16127 readlen -= 3 - adjust_prevlen;
16128 prevlen -= adjust_prevlen;
16129 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016130 }
16131 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016132 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016133#endif
16134 } /* for */
16135
16136 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16137 break;
16138 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016139 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016140 /* There's part of a line in buf, store it in "prev". */
16141 if (p - start + prevlen >= prevsize)
16142 {
16143 /* need bigger "prev" buffer */
16144 char_u *newprev;
16145
16146 /* A common use case is ordinary text files and "prev" gets a
16147 * fragment of a line, so the first allocation is made
16148 * small, to avoid repeatedly 'allocing' large and
16149 * 'reallocing' small. */
16150 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016151 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016152 else
16153 {
16154 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016155 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016156 prevsize = grow50pc > growmin ? grow50pc : growmin;
16157 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016158 newprev = prev == NULL ? alloc(prevsize)
16159 : vim_realloc(prev, prevsize);
16160 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016161 {
16162 do_outofmem_msg((long_u)prevsize);
16163 failed = TRUE;
16164 break;
16165 }
16166 prev = newprev;
16167 }
16168 /* Add the line part to end of "prev". */
16169 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016170 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016171 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016172 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016173
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016174 /*
16175 * For a negative line count use only the lines at the end of the file,
16176 * free the rest.
16177 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016178 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016179 while (cnt > -maxline)
16180 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016181 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016182 --cnt;
16183 }
16184
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016185 if (failed)
16186 {
16187 list_free(rettv->vval.v_list, TRUE);
16188 /* readfile doc says an empty list is returned on error */
16189 rettv->vval.v_list = list_alloc();
16190 }
16191
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016192 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016193 fclose(fd);
16194}
16195
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016196#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016197static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016198
16199/*
16200 * Convert a List to proftime_T.
16201 * Return FAIL when there is something wrong.
16202 */
16203 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016204list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016205{
16206 long n1, n2;
16207 int error = FALSE;
16208
16209 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16210 || arg->vval.v_list->lv_len != 2)
16211 return FAIL;
16212 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16213 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16214# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016215 tm->HighPart = n1;
16216 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016217# else
16218 tm->tv_sec = n1;
16219 tm->tv_usec = n2;
16220# endif
16221 return error ? FAIL : OK;
16222}
16223#endif /* FEAT_RELTIME */
16224
16225/*
16226 * "reltime()" function
16227 */
16228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016229f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016230{
16231#ifdef FEAT_RELTIME
16232 proftime_T res;
16233 proftime_T start;
16234
16235 if (argvars[0].v_type == VAR_UNKNOWN)
16236 {
16237 /* No arguments: get current time. */
16238 profile_start(&res);
16239 }
16240 else if (argvars[1].v_type == VAR_UNKNOWN)
16241 {
16242 if (list2proftime(&argvars[0], &res) == FAIL)
16243 return;
16244 profile_end(&res);
16245 }
16246 else
16247 {
16248 /* Two arguments: compute the difference. */
16249 if (list2proftime(&argvars[0], &start) == FAIL
16250 || list2proftime(&argvars[1], &res) == FAIL)
16251 return;
16252 profile_sub(&res, &start);
16253 }
16254
16255 if (rettv_list_alloc(rettv) == OK)
16256 {
16257 long n1, n2;
16258
16259# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016260 n1 = res.HighPart;
16261 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016262# else
16263 n1 = res.tv_sec;
16264 n2 = res.tv_usec;
16265# endif
16266 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16267 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16268 }
16269#endif
16270}
16271
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016272#ifdef FEAT_FLOAT
16273/*
16274 * "reltimefloat()" function
16275 */
16276 static void
16277f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16278{
16279# ifdef FEAT_RELTIME
16280 proftime_T tm;
16281# endif
16282
16283 rettv->v_type = VAR_FLOAT;
16284 rettv->vval.v_float = 0;
16285# ifdef FEAT_RELTIME
16286 if (list2proftime(&argvars[0], &tm) == OK)
16287 rettv->vval.v_float = profile_float(&tm);
16288# endif
16289}
16290#endif
16291
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016292/*
16293 * "reltimestr()" function
16294 */
16295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016296f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016297{
16298#ifdef FEAT_RELTIME
16299 proftime_T tm;
16300#endif
16301
16302 rettv->v_type = VAR_STRING;
16303 rettv->vval.v_string = NULL;
16304#ifdef FEAT_RELTIME
16305 if (list2proftime(&argvars[0], &tm) == OK)
16306 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16307#endif
16308}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016309
Bram Moolenaar0d660222005-01-07 21:51:51 +000016310#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016311static void make_connection(void);
16312static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016313
16314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016315make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316{
16317 if (X_DISPLAY == NULL
16318# ifdef FEAT_GUI
16319 && !gui.in_use
16320# endif
16321 )
16322 {
16323 x_force_connect = TRUE;
16324 setup_term_clip();
16325 x_force_connect = FALSE;
16326 }
16327}
16328
16329 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016330check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016331{
16332 make_connection();
16333 if (X_DISPLAY == NULL)
16334 {
16335 EMSG(_("E240: No connection to Vim server"));
16336 return FAIL;
16337 }
16338 return OK;
16339}
16340#endif
16341
16342#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016343static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016344
16345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016346remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016347{
16348 char_u *server_name;
16349 char_u *keys;
16350 char_u *r = NULL;
16351 char_u buf[NUMBUFLEN];
16352# ifdef WIN32
16353 HWND w;
16354# else
16355 Window w;
16356# endif
16357
16358 if (check_restricted() || check_secure())
16359 return;
16360
16361# ifdef FEAT_X11
16362 if (check_connection() == FAIL)
16363 return;
16364# endif
16365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016366 server_name = get_tv_string_chk(&argvars[0]);
16367 if (server_name == NULL)
16368 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016369 keys = get_tv_string_buf(&argvars[1], buf);
16370# ifdef WIN32
16371 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16372# else
16373 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16374 < 0)
16375# endif
16376 {
16377 if (r != NULL)
16378 EMSG(r); /* sending worked but evaluation failed */
16379 else
16380 EMSG2(_("E241: Unable to send to %s"), server_name);
16381 return;
16382 }
16383
16384 rettv->vval.v_string = r;
16385
16386 if (argvars[2].v_type != VAR_UNKNOWN)
16387 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016388 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016389 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016390 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016391
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016392 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016393 v.di_tv.v_type = VAR_STRING;
16394 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016395 idvar = get_tv_string_chk(&argvars[2]);
16396 if (idvar != NULL)
16397 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016398 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399 }
16400}
16401#endif
16402
16403/*
16404 * "remote_expr()" function
16405 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016407f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016408{
16409 rettv->v_type = VAR_STRING;
16410 rettv->vval.v_string = NULL;
16411#ifdef FEAT_CLIENTSERVER
16412 remote_common(argvars, rettv, TRUE);
16413#endif
16414}
16415
16416/*
16417 * "remote_foreground()" function
16418 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016420f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016421{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016422#ifdef FEAT_CLIENTSERVER
16423# ifdef WIN32
16424 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016425 {
16426 char_u *server_name = get_tv_string_chk(&argvars[0]);
16427
16428 if (server_name != NULL)
16429 serverForeground(server_name);
16430 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016431# else
16432 /* Send a foreground() expression to the server. */
16433 argvars[1].v_type = VAR_STRING;
16434 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16435 argvars[2].v_type = VAR_UNKNOWN;
16436 remote_common(argvars, rettv, TRUE);
16437 vim_free(argvars[1].vval.v_string);
16438# endif
16439#endif
16440}
16441
Bram Moolenaar0d660222005-01-07 21:51:51 +000016442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016443f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016444{
16445#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016446 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016447 char_u *s = NULL;
16448# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016449 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016451 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016452
16453 if (check_restricted() || check_secure())
16454 {
16455 rettv->vval.v_number = -1;
16456 return;
16457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016458 serverid = get_tv_string_chk(&argvars[0]);
16459 if (serverid == NULL)
16460 {
16461 rettv->vval.v_number = -1;
16462 return; /* type error; errmsg already given */
16463 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016464# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016465 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016466 if (n == 0)
16467 rettv->vval.v_number = -1;
16468 else
16469 {
16470 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16471 rettv->vval.v_number = (s != NULL);
16472 }
16473# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474 if (check_connection() == FAIL)
16475 return;
16476
16477 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016478 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016479# endif
16480
16481 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016483 char_u *retvar;
16484
Bram Moolenaar33570922005-01-25 22:26:29 +000016485 v.di_tv.v_type = VAR_STRING;
16486 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016487 retvar = get_tv_string_chk(&argvars[1]);
16488 if (retvar != NULL)
16489 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016490 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016491 }
16492#else
16493 rettv->vval.v_number = -1;
16494#endif
16495}
16496
Bram Moolenaar0d660222005-01-07 21:51:51 +000016497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016498f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016499{
16500 char_u *r = NULL;
16501
16502#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016503 char_u *serverid = get_tv_string_chk(&argvars[0]);
16504
16505 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016506 {
16507# ifdef WIN32
16508 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016509 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016510
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016511 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016512 if (n != 0)
16513 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16514 if (r == NULL)
16515# else
16516 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016517 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016518# endif
16519 EMSG(_("E277: Unable to read a server reply"));
16520 }
16521#endif
16522 rettv->v_type = VAR_STRING;
16523 rettv->vval.v_string = r;
16524}
16525
16526/*
16527 * "remote_send()" function
16528 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016530f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016531{
16532 rettv->v_type = VAR_STRING;
16533 rettv->vval.v_string = NULL;
16534#ifdef FEAT_CLIENTSERVER
16535 remote_common(argvars, rettv, FALSE);
16536#endif
16537}
16538
16539/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016540 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016541 */
16542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016543f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016544{
Bram Moolenaar33570922005-01-25 22:26:29 +000016545 list_T *l;
16546 listitem_T *item, *item2;
16547 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016548 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016549 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016550 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016551 dict_T *d;
16552 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016553 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016554
Bram Moolenaar8c711452005-01-14 21:53:12 +000016555 if (argvars[0].v_type == VAR_DICT)
16556 {
16557 if (argvars[2].v_type != VAR_UNKNOWN)
16558 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016559 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016560 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016562 key = get_tv_string_chk(&argvars[1]);
16563 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016564 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016565 di = dict_find(d, key, -1);
16566 if (di == NULL)
16567 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016568 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16569 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016570 {
16571 *rettv = di->di_tv;
16572 init_tv(&di->di_tv);
16573 dictitem_remove(d, di);
16574 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016575 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016576 }
16577 }
16578 else if (argvars[0].v_type != VAR_LIST)
16579 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016580 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016581 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016583 int error = FALSE;
16584
16585 idx = get_tv_number_chk(&argvars[1], &error);
16586 if (error)
16587 ; /* type error: do nothing, errmsg already given */
16588 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016589 EMSGN(_(e_listidx), idx);
16590 else
16591 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016592 if (argvars[2].v_type == VAR_UNKNOWN)
16593 {
16594 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016595 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016596 *rettv = item->li_tv;
16597 vim_free(item);
16598 }
16599 else
16600 {
16601 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016602 end = get_tv_number_chk(&argvars[2], &error);
16603 if (error)
16604 ; /* type error: do nothing */
16605 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016606 EMSGN(_(e_listidx), end);
16607 else
16608 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016609 int cnt = 0;
16610
16611 for (li = item; li != NULL; li = li->li_next)
16612 {
16613 ++cnt;
16614 if (li == item2)
16615 break;
16616 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016617 if (li == NULL) /* didn't find "item2" after "item" */
16618 EMSG(_(e_invrange));
16619 else
16620 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016621 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016622 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016623 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016624 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016625 l->lv_first = item;
16626 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016627 item->li_prev = NULL;
16628 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016629 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016630 }
16631 }
16632 }
16633 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016634 }
16635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636}
16637
16638/*
16639 * "rename({from}, {to})" function
16640 */
16641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016642f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643{
16644 char_u buf[NUMBUFLEN];
16645
16646 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016647 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016649 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16650 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016651}
16652
16653/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016654 * "repeat()" function
16655 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016657f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016658{
16659 char_u *p;
16660 int n;
16661 int slen;
16662 int len;
16663 char_u *r;
16664 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016665
16666 n = get_tv_number(&argvars[1]);
16667 if (argvars[0].v_type == VAR_LIST)
16668 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016669 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016670 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016671 if (list_extend(rettv->vval.v_list,
16672 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016673 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016674 }
16675 else
16676 {
16677 p = get_tv_string(&argvars[0]);
16678 rettv->v_type = VAR_STRING;
16679 rettv->vval.v_string = NULL;
16680
16681 slen = (int)STRLEN(p);
16682 len = slen * n;
16683 if (len <= 0)
16684 return;
16685
16686 r = alloc(len + 1);
16687 if (r != NULL)
16688 {
16689 for (i = 0; i < n; i++)
16690 mch_memmove(r + i * slen, p, (size_t)slen);
16691 r[len] = NUL;
16692 }
16693
16694 rettv->vval.v_string = r;
16695 }
16696}
16697
16698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 * "resolve()" function
16700 */
16701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016702f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703{
16704 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016705#ifdef HAVE_READLINK
16706 char_u *buf = NULL;
16707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016709 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710#ifdef FEAT_SHORTCUT
16711 {
16712 char_u *v = NULL;
16713
16714 v = mch_resolve_shortcut(p);
16715 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016716 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016718 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719 }
16720#else
16721# ifdef HAVE_READLINK
16722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 char_u *cpy;
16724 int len;
16725 char_u *remain = NULL;
16726 char_u *q;
16727 int is_relative_to_current = FALSE;
16728 int has_trailing_pathsep = FALSE;
16729 int limit = 100;
16730
16731 p = vim_strsave(p);
16732
16733 if (p[0] == '.' && (vim_ispathsep(p[1])
16734 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16735 is_relative_to_current = TRUE;
16736
16737 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016738 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016741 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743
16744 q = getnextcomp(p);
16745 if (*q != NUL)
16746 {
16747 /* Separate the first path component in "p", and keep the
16748 * remainder (beginning with the path separator). */
16749 remain = vim_strsave(q - 1);
16750 q[-1] = NUL;
16751 }
16752
Bram Moolenaard9462e32011-04-11 21:35:11 +020016753 buf = alloc(MAXPATHL + 1);
16754 if (buf == NULL)
16755 goto fail;
16756
Bram Moolenaar071d4272004-06-13 20:20:40 +000016757 for (;;)
16758 {
16759 for (;;)
16760 {
16761 len = readlink((char *)p, (char *)buf, MAXPATHL);
16762 if (len <= 0)
16763 break;
16764 buf[len] = NUL;
16765
16766 if (limit-- == 0)
16767 {
16768 vim_free(p);
16769 vim_free(remain);
16770 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016771 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 goto fail;
16773 }
16774
16775 /* Ensure that the result will have a trailing path separator
16776 * if the argument has one. */
16777 if (remain == NULL && has_trailing_pathsep)
16778 add_pathsep(buf);
16779
16780 /* Separate the first path component in the link value and
16781 * concatenate the remainders. */
16782 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16783 if (*q != NUL)
16784 {
16785 if (remain == NULL)
16786 remain = vim_strsave(q - 1);
16787 else
16788 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016789 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 if (cpy != NULL)
16791 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 vim_free(remain);
16793 remain = cpy;
16794 }
16795 }
16796 q[-1] = NUL;
16797 }
16798
16799 q = gettail(p);
16800 if (q > p && *q == NUL)
16801 {
16802 /* Ignore trailing path separator. */
16803 q[-1] = NUL;
16804 q = gettail(p);
16805 }
16806 if (q > p && !mch_isFullName(buf))
16807 {
16808 /* symlink is relative to directory of argument */
16809 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16810 if (cpy != NULL)
16811 {
16812 STRCPY(cpy, p);
16813 STRCPY(gettail(cpy), buf);
16814 vim_free(p);
16815 p = cpy;
16816 }
16817 }
16818 else
16819 {
16820 vim_free(p);
16821 p = vim_strsave(buf);
16822 }
16823 }
16824
16825 if (remain == NULL)
16826 break;
16827
16828 /* Append the first path component of "remain" to "p". */
16829 q = getnextcomp(remain + 1);
16830 len = q - remain - (*q != NUL);
16831 cpy = vim_strnsave(p, STRLEN(p) + len);
16832 if (cpy != NULL)
16833 {
16834 STRNCAT(cpy, remain, len);
16835 vim_free(p);
16836 p = cpy;
16837 }
16838 /* Shorten "remain". */
16839 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016840 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 else
16842 {
16843 vim_free(remain);
16844 remain = NULL;
16845 }
16846 }
16847
16848 /* If the result is a relative path name, make it explicitly relative to
16849 * the current directory if and only if the argument had this form. */
16850 if (!vim_ispathsep(*p))
16851 {
16852 if (is_relative_to_current
16853 && *p != NUL
16854 && !(p[0] == '.'
16855 && (p[1] == NUL
16856 || vim_ispathsep(p[1])
16857 || (p[1] == '.'
16858 && (p[2] == NUL
16859 || vim_ispathsep(p[2]))))))
16860 {
16861 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016862 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863 if (cpy != NULL)
16864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865 vim_free(p);
16866 p = cpy;
16867 }
16868 }
16869 else if (!is_relative_to_current)
16870 {
16871 /* Strip leading "./". */
16872 q = p;
16873 while (q[0] == '.' && vim_ispathsep(q[1]))
16874 q += 2;
16875 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016876 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877 }
16878 }
16879
16880 /* Ensure that the result will have no trailing path separator
16881 * if the argument had none. But keep "/" or "//". */
16882 if (!has_trailing_pathsep)
16883 {
16884 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016885 if (after_pathsep(p, q))
16886 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 }
16888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016889 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890 }
16891# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016892 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893# endif
16894#endif
16895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016896 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897
16898#ifdef HAVE_READLINK
16899fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016900 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016902 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903}
16904
16905/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 */
16908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016909f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910{
Bram Moolenaar33570922005-01-25 22:26:29 +000016911 list_T *l;
16912 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914 if (argvars[0].v_type != VAR_LIST)
16915 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016916 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016917 && !tv_check_lock(l->lv_lock,
16918 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016919 {
16920 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016921 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016922 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 while (li != NULL)
16924 {
16925 ni = li->li_prev;
16926 list_append(l, li);
16927 li = ni;
16928 }
16929 rettv->vval.v_list = l;
16930 rettv->v_type = VAR_LIST;
16931 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016932 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934}
16935
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016936#define SP_NOMOVE 0x01 /* don't move cursor */
16937#define SP_REPEAT 0x02 /* repeat to find outer pair */
16938#define SP_RETCOUNT 0x04 /* return matchcount */
16939#define SP_SETPCMARK 0x08 /* set previous context mark */
16940#define SP_START 0x10 /* accept match at start position */
16941#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16942#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016943#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016944
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016945static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016946
16947/*
16948 * Get flags for a search function.
16949 * Possibly sets "p_ws".
16950 * Returns BACKWARD, FORWARD or zero (for an error).
16951 */
16952 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016953get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954{
16955 int dir = FORWARD;
16956 char_u *flags;
16957 char_u nbuf[NUMBUFLEN];
16958 int mask;
16959
16960 if (varp->v_type != VAR_UNKNOWN)
16961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016962 flags = get_tv_string_buf_chk(varp, nbuf);
16963 if (flags == NULL)
16964 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965 while (*flags != NUL)
16966 {
16967 switch (*flags)
16968 {
16969 case 'b': dir = BACKWARD; break;
16970 case 'w': p_ws = TRUE; break;
16971 case 'W': p_ws = FALSE; break;
16972 default: mask = 0;
16973 if (flagsp != NULL)
16974 switch (*flags)
16975 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016976 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016977 case 'e': mask = SP_END; break;
16978 case 'm': mask = SP_RETCOUNT; break;
16979 case 'n': mask = SP_NOMOVE; break;
16980 case 'p': mask = SP_SUBPAT; break;
16981 case 'r': mask = SP_REPEAT; break;
16982 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016983 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016984 }
16985 if (mask == 0)
16986 {
16987 EMSG2(_(e_invarg2), flags);
16988 dir = 0;
16989 }
16990 else
16991 *flagsp |= mask;
16992 }
16993 if (dir == 0)
16994 break;
16995 ++flags;
16996 }
16997 }
16998 return dir;
16999}
17000
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017002 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017004 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017005search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017007 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008 char_u *pat;
17009 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017010 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 int save_p_ws = p_ws;
17012 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017013 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017014 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017015 proftime_T tm;
17016#ifdef FEAT_RELTIME
17017 long time_limit = 0;
17018#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017019 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017020 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017022 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017023 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017024 if (dir == 0)
17025 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017026 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017027 if (flags & SP_START)
17028 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017029 if (flags & SP_END)
17030 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017031 if (flags & SP_COLUMN)
17032 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017033
Bram Moolenaar76929292008-01-06 19:07:36 +000017034 /* Optional arguments: line number to stop searching and timeout. */
17035 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017036 {
17037 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17038 if (lnum_stop < 0)
17039 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017040#ifdef FEAT_RELTIME
17041 if (argvars[3].v_type != VAR_UNKNOWN)
17042 {
17043 time_limit = get_tv_number_chk(&argvars[3], NULL);
17044 if (time_limit < 0)
17045 goto theend;
17046 }
17047#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017048 }
17049
Bram Moolenaar76929292008-01-06 19:07:36 +000017050#ifdef FEAT_RELTIME
17051 /* Set the time limit, if there is one. */
17052 profile_setlimit(time_limit, &tm);
17053#endif
17054
Bram Moolenaar231334e2005-07-25 20:46:57 +000017055 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017056 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017057 * Check to make sure only those flags are set.
17058 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17059 * flags cannot be set. Check for that condition also.
17060 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017061 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017062 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017064 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017065 goto theend;
17066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017068 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017069 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017070 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017071 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017073 if (flags & SP_SUBPAT)
17074 retval = subpatnum;
17075 else
17076 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017077 if (flags & SP_SETPCMARK)
17078 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017080 if (match_pos != NULL)
17081 {
17082 /* Store the match cursor position */
17083 match_pos->lnum = pos.lnum;
17084 match_pos->col = pos.col + 1;
17085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 /* "/$" will put the cursor after the end of the line, may need to
17087 * correct that here */
17088 check_cursor();
17089 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017090
17091 /* If 'n' flag is used: restore cursor position. */
17092 if (flags & SP_NOMOVE)
17093 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017094 else
17095 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017096theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017098
17099 return retval;
17100}
17101
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017102#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017103
17104/*
17105 * round() is not in C90, use ceil() or floor() instead.
17106 */
17107 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017108vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017109{
17110 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17111}
17112
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017113/*
17114 * "round({float})" function
17115 */
17116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017117f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017118{
17119 float_T f;
17120
17121 rettv->v_type = VAR_FLOAT;
17122 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017123 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017124 else
17125 rettv->vval.v_float = 0.0;
17126}
17127#endif
17128
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017129/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017130 * "screenattr()" function
17131 */
17132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017133f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017134{
17135 int row;
17136 int col;
17137 int c;
17138
17139 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17140 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17141 if (row < 0 || row >= screen_Rows
17142 || col < 0 || col >= screen_Columns)
17143 c = -1;
17144 else
17145 c = ScreenAttrs[LineOffset[row] + col];
17146 rettv->vval.v_number = c;
17147}
17148
17149/*
17150 * "screenchar()" function
17151 */
17152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017153f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017154{
17155 int row;
17156 int col;
17157 int off;
17158 int c;
17159
17160 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17161 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17162 if (row < 0 || row >= screen_Rows
17163 || col < 0 || col >= screen_Columns)
17164 c = -1;
17165 else
17166 {
17167 off = LineOffset[row] + col;
17168#ifdef FEAT_MBYTE
17169 if (enc_utf8 && ScreenLinesUC[off] != 0)
17170 c = ScreenLinesUC[off];
17171 else
17172#endif
17173 c = ScreenLines[off];
17174 }
17175 rettv->vval.v_number = c;
17176}
17177
17178/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017179 * "screencol()" function
17180 *
17181 * First column is 1 to be consistent with virtcol().
17182 */
17183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017184f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017185{
17186 rettv->vval.v_number = screen_screencol() + 1;
17187}
17188
17189/*
17190 * "screenrow()" function
17191 */
17192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017193f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017194{
17195 rettv->vval.v_number = screen_screenrow() + 1;
17196}
17197
17198/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017199 * "search()" function
17200 */
17201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017202f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017203{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017204 int flags = 0;
17205
17206 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207}
17208
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017210 * "searchdecl()" function
17211 */
17212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017213f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017214{
17215 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017216 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017217 int error = FALSE;
17218 char_u *name;
17219
17220 rettv->vval.v_number = 1; /* default: FAIL */
17221
17222 name = get_tv_string_chk(&argvars[0]);
17223 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017224 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017225 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017226 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17227 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17228 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017229 if (!error && name != NULL)
17230 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017231 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017232}
17233
17234/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017235 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017238searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239{
17240 char_u *spat, *mpat, *epat;
17241 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243 int dir;
17244 int flags = 0;
17245 char_u nbuf1[NUMBUFLEN];
17246 char_u nbuf2[NUMBUFLEN];
17247 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017248 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017249 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017250 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017253 spat = get_tv_string_chk(&argvars[0]);
17254 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17255 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17256 if (spat == NULL || mpat == NULL || epat == NULL)
17257 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259 /* Handle the optional fourth argument: flags */
17260 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017261 if (dir == 0)
17262 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017263
17264 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017265 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17266 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017267 if ((flags & (SP_END | SP_SUBPAT)) != 0
17268 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017269 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017270 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017271 goto theend;
17272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017274 /* Using 'r' implies 'W', otherwise it doesn't work. */
17275 if (flags & SP_REPEAT)
17276 p_ws = FALSE;
17277
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017278 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017279 if (argvars[3].v_type == VAR_UNKNOWN
17280 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 skip = (char_u *)"";
17282 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017284 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017285 if (argvars[5].v_type != VAR_UNKNOWN)
17286 {
17287 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17288 if (lnum_stop < 0)
17289 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017290#ifdef FEAT_RELTIME
17291 if (argvars[6].v_type != VAR_UNKNOWN)
17292 {
17293 time_limit = get_tv_number_chk(&argvars[6], NULL);
17294 if (time_limit < 0)
17295 goto theend;
17296 }
17297#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017298 }
17299 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017300 if (skip == NULL)
17301 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017303 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017304 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017305
17306theend:
17307 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017308
17309 return retval;
17310}
17311
17312/*
17313 * "searchpair()" function
17314 */
17315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017316f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017317{
17318 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17319}
17320
17321/*
17322 * "searchpairpos()" function
17323 */
17324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017325f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017326{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017327 pos_T match_pos;
17328 int lnum = 0;
17329 int col = 0;
17330
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017331 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017332 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017333
17334 if (searchpair_cmn(argvars, &match_pos) > 0)
17335 {
17336 lnum = match_pos.lnum;
17337 col = match_pos.col;
17338 }
17339
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017340 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17341 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017342}
17343
17344/*
17345 * Search for a start/middle/end thing.
17346 * Used by searchpair(), see its documentation for the details.
17347 * Returns 0 or -1 for no match,
17348 */
17349 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017350do_searchpair(
17351 char_u *spat, /* start pattern */
17352 char_u *mpat, /* middle pattern */
17353 char_u *epat, /* end pattern */
17354 int dir, /* BACKWARD or FORWARD */
17355 char_u *skip, /* skip expression */
17356 int flags, /* SP_SETPCMARK and other SP_ values */
17357 pos_T *match_pos,
17358 linenr_T lnum_stop, /* stop at this line if not zero */
17359 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017360{
17361 char_u *save_cpo;
17362 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17363 long retval = 0;
17364 pos_T pos;
17365 pos_T firstpos;
17366 pos_T foundpos;
17367 pos_T save_cursor;
17368 pos_T save_pos;
17369 int n;
17370 int r;
17371 int nest = 1;
17372 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017373 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017374 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017375
17376 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17377 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017378 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017379
Bram Moolenaar76929292008-01-06 19:07:36 +000017380#ifdef FEAT_RELTIME
17381 /* Set the time limit, if there is one. */
17382 profile_setlimit(time_limit, &tm);
17383#endif
17384
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017385 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17386 * start/middle/end (pat3, for the top pair). */
17387 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17388 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17389 if (pat2 == NULL || pat3 == NULL)
17390 goto theend;
17391 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17392 if (*mpat == NUL)
17393 STRCPY(pat3, pat2);
17394 else
17395 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17396 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017397 if (flags & SP_START)
17398 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017399
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400 save_cursor = curwin->w_cursor;
17401 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017402 clearpos(&firstpos);
17403 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 pat = pat3;
17405 for (;;)
17406 {
17407 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017408 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17410 /* didn't find it or found the first match again: FAIL */
17411 break;
17412
17413 if (firstpos.lnum == 0)
17414 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017415 if (equalpos(pos, foundpos))
17416 {
17417 /* Found the same position again. Can happen with a pattern that
17418 * has "\zs" at the end and searching backwards. Advance one
17419 * character and try again. */
17420 if (dir == BACKWARD)
17421 decl(&pos);
17422 else
17423 incl(&pos);
17424 }
17425 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017427 /* clear the start flag to avoid getting stuck here */
17428 options &= ~SEARCH_START;
17429
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430 /* If the skip pattern matches, ignore this match. */
17431 if (*skip != NUL)
17432 {
17433 save_pos = curwin->w_cursor;
17434 curwin->w_cursor = pos;
17435 r = eval_to_bool(skip, &err, NULL, FALSE);
17436 curwin->w_cursor = save_pos;
17437 if (err)
17438 {
17439 /* Evaluating {skip} caused an error, break here. */
17440 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017441 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 break;
17443 }
17444 if (r)
17445 continue;
17446 }
17447
17448 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17449 {
17450 /* Found end when searching backwards or start when searching
17451 * forward: nested pair. */
17452 ++nest;
17453 pat = pat2; /* nested, don't search for middle */
17454 }
17455 else
17456 {
17457 /* Found end when searching forward or start when searching
17458 * backward: end of (nested) pair; or found middle in outer pair. */
17459 if (--nest == 1)
17460 pat = pat3; /* outer level, search for middle */
17461 }
17462
17463 if (nest == 0)
17464 {
17465 /* Found the match: return matchcount or line number. */
17466 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017467 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017469 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017470 if (flags & SP_SETPCMARK)
17471 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472 curwin->w_cursor = pos;
17473 if (!(flags & SP_REPEAT))
17474 break;
17475 nest = 1; /* search for next unmatched */
17476 }
17477 }
17478
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017479 if (match_pos != NULL)
17480 {
17481 /* Store the match cursor position */
17482 match_pos->lnum = curwin->w_cursor.lnum;
17483 match_pos->col = curwin->w_cursor.col + 1;
17484 }
17485
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017487 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 curwin->w_cursor = save_cursor;
17489
17490theend:
17491 vim_free(pat2);
17492 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017493 if (p_cpo == empty_option)
17494 p_cpo = save_cpo;
17495 else
17496 /* Darn, evaluating the {skip} expression changed the value. */
17497 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017498
17499 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500}
17501
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017502/*
17503 * "searchpos()" function
17504 */
17505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017506f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017507{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017508 pos_T match_pos;
17509 int lnum = 0;
17510 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017511 int n;
17512 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017513
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017514 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017515 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017516
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017517 n = search_cmn(argvars, &match_pos, &flags);
17518 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017519 {
17520 lnum = match_pos.lnum;
17521 col = match_pos.col;
17522 }
17523
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017524 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17525 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017526 if (flags & SP_SUBPAT)
17527 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017528}
17529
Bram Moolenaar0d660222005-01-07 21:51:51 +000017530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017531f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017533#ifdef FEAT_CLIENTSERVER
17534 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017535 char_u *server = get_tv_string_chk(&argvars[0]);
17536 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537
Bram Moolenaar0d660222005-01-07 21:51:51 +000017538 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017539 if (server == NULL || reply == NULL)
17540 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017541 if (check_restricted() || check_secure())
17542 return;
17543# ifdef FEAT_X11
17544 if (check_connection() == FAIL)
17545 return;
17546# endif
17547
17548 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017550 EMSG(_("E258: Unable to send to client"));
17551 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017553 rettv->vval.v_number = 0;
17554#else
17555 rettv->vval.v_number = -1;
17556#endif
17557}
17558
Bram Moolenaar0d660222005-01-07 21:51:51 +000017559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017560f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017561{
17562 char_u *r = NULL;
17563
17564#ifdef FEAT_CLIENTSERVER
17565# ifdef WIN32
17566 r = serverGetVimNames();
17567# else
17568 make_connection();
17569 if (X_DISPLAY != NULL)
17570 r = serverGetVimNames(X_DISPLAY);
17571# endif
17572#endif
17573 rettv->v_type = VAR_STRING;
17574 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575}
17576
17577/*
17578 * "setbufvar()" function
17579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017581f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582{
17583 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017586 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 char_u nbuf[NUMBUFLEN];
17588
17589 if (check_restricted() || check_secure())
17590 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017591 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17592 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017593 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 varp = &argvars[2];
17595
17596 if (buf != NULL && varname != NULL && varp != NULL)
17597 {
17598 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600
17601 if (*varname == '&')
17602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017603 long numval;
17604 char_u *strval;
17605 int error = FALSE;
17606
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017608 numval = get_tv_number_chk(varp, &error);
17609 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017610 if (!error && strval != NULL)
17611 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612 }
17613 else
17614 {
17615 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17616 if (bufvarname != NULL)
17617 {
17618 STRCPY(bufvarname, "b:");
17619 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017620 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 vim_free(bufvarname);
17622 }
17623 }
17624
17625 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628}
17629
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017631f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017632{
17633 dict_T *d;
17634 dictitem_T *di;
17635 char_u *csearch;
17636
17637 if (argvars[0].v_type != VAR_DICT)
17638 {
17639 EMSG(_(e_dictreq));
17640 return;
17641 }
17642
17643 if ((d = argvars[0].vval.v_dict) != NULL)
17644 {
17645 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17646 if (csearch != NULL)
17647 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017648#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017649 if (enc_utf8)
17650 {
17651 int pcc[MAX_MCO];
17652 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017653
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017654 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17655 }
17656 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017657#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017658 set_last_csearch(PTR2CHAR(csearch),
17659 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017660 }
17661
17662 di = dict_find(d, (char_u *)"forward", -1);
17663 if (di != NULL)
17664 set_csearch_direction(get_tv_number(&di->di_tv)
17665 ? FORWARD : BACKWARD);
17666
17667 di = dict_find(d, (char_u *)"until", -1);
17668 if (di != NULL)
17669 set_csearch_until(!!get_tv_number(&di->di_tv));
17670 }
17671}
17672
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673/*
17674 * "setcmdpos()" function
17675 */
17676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017677f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017679 int pos = (int)get_tv_number(&argvars[0]) - 1;
17680
17681 if (pos >= 0)
17682 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683}
17684
17685/*
17686 * "setline()" function
17687 */
17688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017689f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690{
17691 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017692 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017693 list_T *l = NULL;
17694 listitem_T *li = NULL;
17695 long added = 0;
17696 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017698 lnum = get_tv_lnum(&argvars[0]);
17699 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017701 l = argvars[1].vval.v_list;
17702 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017704 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017705 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017706
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017707 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017708 for (;;)
17709 {
17710 if (l != NULL)
17711 {
17712 /* list argument, get next string */
17713 if (li == NULL)
17714 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017715 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017716 li = li->li_next;
17717 }
17718
17719 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017720 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017721 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017722
17723 /* When coming here from Insert mode, sync undo, so that this can be
17724 * undone separately from what was previously inserted. */
17725 if (u_sync_once == 2)
17726 {
17727 u_sync_once = 1; /* notify that u_sync() was called */
17728 u_sync(TRUE);
17729 }
17730
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017731 if (lnum <= curbuf->b_ml.ml_line_count)
17732 {
17733 /* existing line, replace it */
17734 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17735 {
17736 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017737 if (lnum == curwin->w_cursor.lnum)
17738 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017739 rettv->vval.v_number = 0; /* OK */
17740 }
17741 }
17742 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17743 {
17744 /* lnum is one past the last line, append the line */
17745 ++added;
17746 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17747 rettv->vval.v_number = 0; /* OK */
17748 }
17749
17750 if (l == NULL) /* only one string argument */
17751 break;
17752 ++lnum;
17753 }
17754
17755 if (added > 0)
17756 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757}
17758
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017759static 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 +000017760
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017762 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017763 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017765set_qf_ll_list(
17766 win_T *wp UNUSED,
17767 typval_T *list_arg UNUSED,
17768 typval_T *action_arg UNUSED,
17769 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017770{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017771#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017772 char_u *act;
17773 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017774#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017775
Bram Moolenaar2641f772005-03-25 21:58:17 +000017776 rettv->vval.v_number = -1;
17777
17778#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017779 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017780 EMSG(_(e_listreq));
17781 else
17782 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017783 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017784
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017785 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017786 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017787 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017788 if (act == NULL)
17789 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017790 if (*act == 'a' || *act == 'r')
17791 action = *act;
17792 }
17793
Bram Moolenaar81484f42012-12-05 15:16:47 +010017794 if (l != NULL && set_errorlist(wp, l, action,
17795 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017796 rettv->vval.v_number = 0;
17797 }
17798#endif
17799}
17800
17801/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017802 * "setloclist()" function
17803 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017805f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017806{
17807 win_T *win;
17808
17809 rettv->vval.v_number = -1;
17810
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017811 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017812 if (win != NULL)
17813 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17814}
17815
17816/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017817 * "setmatches()" function
17818 */
17819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017820f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017821{
17822#ifdef FEAT_SEARCH_EXTRA
17823 list_T *l;
17824 listitem_T *li;
17825 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017826 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017827
17828 rettv->vval.v_number = -1;
17829 if (argvars[0].v_type != VAR_LIST)
17830 {
17831 EMSG(_(e_listreq));
17832 return;
17833 }
17834 if ((l = argvars[0].vval.v_list) != NULL)
17835 {
17836
17837 /* To some extent make sure that we are dealing with a list from
17838 * "getmatches()". */
17839 li = l->lv_first;
17840 while (li != NULL)
17841 {
17842 if (li->li_tv.v_type != VAR_DICT
17843 || (d = li->li_tv.vval.v_dict) == NULL)
17844 {
17845 EMSG(_(e_invarg));
17846 return;
17847 }
17848 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017849 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17850 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017851 && dict_find(d, (char_u *)"priority", -1) != NULL
17852 && dict_find(d, (char_u *)"id", -1) != NULL))
17853 {
17854 EMSG(_(e_invarg));
17855 return;
17856 }
17857 li = li->li_next;
17858 }
17859
17860 clear_matches(curwin);
17861 li = l->lv_first;
17862 while (li != NULL)
17863 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017864 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017865 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017866 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017867 char_u *group;
17868 int priority;
17869 int id;
17870 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017871
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017872 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017873 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17874 {
17875 if (s == NULL)
17876 {
17877 s = list_alloc();
17878 if (s == NULL)
17879 return;
17880 }
17881
17882 /* match from matchaddpos() */
17883 for (i = 1; i < 9; i++)
17884 {
17885 sprintf((char *)buf, (char *)"pos%d", i);
17886 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17887 {
17888 if (di->di_tv.v_type != VAR_LIST)
17889 return;
17890
17891 list_append_tv(s, &di->di_tv);
17892 s->lv_refcount++;
17893 }
17894 else
17895 break;
17896 }
17897 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017898
17899 group = get_dict_string(d, (char_u *)"group", FALSE);
17900 priority = (int)get_dict_number(d, (char_u *)"priority");
17901 id = (int)get_dict_number(d, (char_u *)"id");
17902 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17903 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17904 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017905 if (i == 0)
17906 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017907 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017908 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017909 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017910 }
17911 else
17912 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017913 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017914 list_unref(s);
17915 s = NULL;
17916 }
17917
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017918 li = li->li_next;
17919 }
17920 rettv->vval.v_number = 0;
17921 }
17922#endif
17923}
17924
17925/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017926 * "setpos()" function
17927 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017929f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017930{
17931 pos_T pos;
17932 int fnum;
17933 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017934 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017935
Bram Moolenaar08250432008-02-13 11:42:46 +000017936 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017937 name = get_tv_string_chk(argvars);
17938 if (name != NULL)
17939 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017940 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017941 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017942 if (--pos.col < 0)
17943 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017944 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017945 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017946 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017947 if (fnum == curbuf->b_fnum)
17948 {
17949 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017950 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017951 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017952 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017953 curwin->w_set_curswant = FALSE;
17954 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017955 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017956 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017957 }
17958 else
17959 EMSG(_(e_invarg));
17960 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017961 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17962 {
17963 /* set mark */
17964 if (setmark_pos(name[1], &pos, fnum) == OK)
17965 rettv->vval.v_number = 0;
17966 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017967 else
17968 EMSG(_(e_invarg));
17969 }
17970 }
17971}
17972
17973/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017974 * "setqflist()" function
17975 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017977f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017978{
17979 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17980}
17981
17982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983 * "setreg()" function
17984 */
17985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017986f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987{
17988 int regname;
17989 char_u *strregname;
17990 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017991 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992 int append;
17993 char_u yank_type;
17994 long block_len;
17995
17996 block_len = -1;
17997 yank_type = MAUTO;
17998 append = FALSE;
17999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018000 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018001 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018003 if (strregname == NULL)
18004 return; /* type error; errmsg already given */
18005 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006 if (regname == 0 || regname == '@')
18007 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018009 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018011 stropt = get_tv_string_chk(&argvars[2]);
18012 if (stropt == NULL)
18013 return; /* type error */
18014 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015 switch (*stropt)
18016 {
18017 case 'a': case 'A': /* append */
18018 append = TRUE;
18019 break;
18020 case 'v': case 'c': /* character-wise selection */
18021 yank_type = MCHAR;
18022 break;
18023 case 'V': case 'l': /* line-wise selection */
18024 yank_type = MLINE;
18025 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026 case 'b': case Ctrl_V: /* block-wise selection */
18027 yank_type = MBLOCK;
18028 if (VIM_ISDIGIT(stropt[1]))
18029 {
18030 ++stropt;
18031 block_len = getdigits(&stropt) - 1;
18032 --stropt;
18033 }
18034 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 }
18036 }
18037
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018038 if (argvars[1].v_type == VAR_LIST)
18039 {
18040 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018041 char_u **allocval;
18042 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018043 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018044 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018045 int len = argvars[1].vval.v_list->lv_len;
18046 listitem_T *li;
18047
Bram Moolenaar7d647822014-04-05 21:28:56 +020018048 /* First half: use for pointers to result lines; second half: use for
18049 * pointers to allocated copies. */
18050 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018051 if (lstval == NULL)
18052 return;
18053 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018054 allocval = lstval + len + 2;
18055 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018056
18057 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18058 li = li->li_next)
18059 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018060 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018061 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018062 goto free_lstval;
18063 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018064 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018065 /* Need to make a copy, next get_tv_string_buf_chk() will
18066 * overwrite the string. */
18067 strval = vim_strsave(buf);
18068 if (strval == NULL)
18069 goto free_lstval;
18070 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018071 }
18072 *curval++ = strval;
18073 }
18074 *curval++ = NULL;
18075
18076 write_reg_contents_lst(regname, lstval, -1,
18077 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018078free_lstval:
18079 while (curallocval > allocval)
18080 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018081 vim_free(lstval);
18082 }
18083 else
18084 {
18085 strval = get_tv_string_chk(&argvars[1]);
18086 if (strval == NULL)
18087 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018088 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018091 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092}
18093
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018094/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018095 * "settabvar()" function
18096 */
18097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018098f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018099{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018100#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018101 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018102 tabpage_T *tp;
18103#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018104 char_u *varname, *tabvarname;
18105 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018106
18107 rettv->vval.v_number = 0;
18108
18109 if (check_restricted() || check_secure())
18110 return;
18111
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018112#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018113 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018114#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018115 varname = get_tv_string_chk(&argvars[1]);
18116 varp = &argvars[2];
18117
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018118 if (varname != NULL && varp != NULL
18119#ifdef FEAT_WINDOWS
18120 && tp != NULL
18121#endif
18122 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018123 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018124#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018125 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018126 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018127#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018128
18129 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18130 if (tabvarname != NULL)
18131 {
18132 STRCPY(tabvarname, "t:");
18133 STRCPY(tabvarname + 2, varname);
18134 set_var(tabvarname, varp, TRUE);
18135 vim_free(tabvarname);
18136 }
18137
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018138#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018139 /* Restore current tabpage */
18140 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018141 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018142#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018143 }
18144}
18145
18146/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018147 * "settabwinvar()" function
18148 */
18149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018150f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018151{
18152 setwinvar(argvars, rettv, 1);
18153}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154
18155/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018156 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018159f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018161 setwinvar(argvars, rettv, 0);
18162}
18163
18164/*
18165 * "setwinvar()" and "settabwinvar()" functions
18166 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018167
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018169setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018170{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 win_T *win;
18172#ifdef FEAT_WINDOWS
18173 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018174 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018175 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176#endif
18177 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018178 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018180 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181
18182 if (check_restricted() || check_secure())
18183 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018184
18185#ifdef FEAT_WINDOWS
18186 if (off == 1)
18187 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18188 else
18189 tp = curtab;
18190#endif
18191 win = find_win_by_nr(&argvars[off], tp);
18192 varname = get_tv_string_chk(&argvars[off + 1]);
18193 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194
18195 if (win != NULL && varname != NULL && varp != NULL)
18196 {
18197#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018198 need_switch_win = !(tp == curtab && win == curwin);
18199 if (!need_switch_win
18200 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018203 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018205 long numval;
18206 char_u *strval;
18207 int error = FALSE;
18208
18209 ++varname;
18210 numval = get_tv_number_chk(varp, &error);
18211 strval = get_tv_string_buf_chk(varp, nbuf);
18212 if (!error && strval != NULL)
18213 set_option_value(varname, numval, strval, OPT_LOCAL);
18214 }
18215 else
18216 {
18217 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18218 if (winvarname != NULL)
18219 {
18220 STRCPY(winvarname, "w:");
18221 STRCPY(winvarname + 2, varname);
18222 set_var(winvarname, varp, TRUE);
18223 vim_free(winvarname);
18224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 }
18226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018228 if (need_switch_win)
18229 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230#endif
18231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232}
18233
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018234#ifdef FEAT_CRYPT
18235/*
18236 * "sha256({string})" function
18237 */
18238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018239f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018240{
18241 char_u *p;
18242
18243 p = get_tv_string(&argvars[0]);
18244 rettv->vval.v_string = vim_strsave(
18245 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18246 rettv->v_type = VAR_STRING;
18247}
18248#endif /* FEAT_CRYPT */
18249
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018251 * "shellescape({string})" function
18252 */
18253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018254f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018255{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018256 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018257 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018258 rettv->v_type = VAR_STRING;
18259}
18260
18261/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018262 * shiftwidth() function
18263 */
18264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018265f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018266{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018267 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018268}
18269
18270/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018271 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 */
18273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018274f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018276 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018277
Bram Moolenaar0d660222005-01-07 21:51:51 +000018278 p = get_tv_string(&argvars[0]);
18279 rettv->vval.v_string = vim_strsave(p);
18280 simplify_filename(rettv->vval.v_string); /* simplify in place */
18281 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282}
18283
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018284#ifdef FEAT_FLOAT
18285/*
18286 * "sin()" function
18287 */
18288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018289f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018290{
18291 float_T f;
18292
18293 rettv->v_type = VAR_FLOAT;
18294 if (get_float_arg(argvars, &f) == OK)
18295 rettv->vval.v_float = sin(f);
18296 else
18297 rettv->vval.v_float = 0.0;
18298}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018299
18300/*
18301 * "sinh()" function
18302 */
18303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018304f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018305{
18306 float_T f;
18307
18308 rettv->v_type = VAR_FLOAT;
18309 if (get_float_arg(argvars, &f) == OK)
18310 rettv->vval.v_float = sinh(f);
18311 else
18312 rettv->vval.v_float = 0.0;
18313}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018314#endif
18315
Bram Moolenaar0d660222005-01-07 21:51:51 +000018316static int
18317#ifdef __BORLANDC__
18318 _RTLENTRYF
18319#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018320 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018321static int
18322#ifdef __BORLANDC__
18323 _RTLENTRYF
18324#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018325 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018326
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018327/* struct used in the array that's given to qsort() */
18328typedef struct
18329{
18330 listitem_T *item;
18331 int idx;
18332} sortItem_T;
18333
Bram Moolenaar0d660222005-01-07 21:51:51 +000018334static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018335static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018336static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018337#ifdef FEAT_FLOAT
18338static int item_compare_float;
18339#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018340static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018341static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018342static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018343static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018344static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018345#define ITEM_COMPARE_FAIL 999
18346
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018348 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018350 static int
18351#ifdef __BORLANDC__
18352_RTLENTRYF
18353#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018354item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018356 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018357 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018358 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018359 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018360 int res;
18361 char_u numbuf1[NUMBUFLEN];
18362 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018364 si1 = (sortItem_T *)s1;
18365 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018366 tv1 = &si1->item->li_tv;
18367 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018368
18369 if (item_compare_numbers)
18370 {
18371 long v1 = get_tv_number(tv1);
18372 long v2 = get_tv_number(tv2);
18373
18374 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18375 }
18376
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018377#ifdef FEAT_FLOAT
18378 if (item_compare_float)
18379 {
18380 float_T v1 = get_tv_float(tv1);
18381 float_T v2 = get_tv_float(tv2);
18382
18383 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18384 }
18385#endif
18386
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018387 /* tv2string() puts quotes around a string and allocates memory. Don't do
18388 * that for string variables. Use a single quote when comparing with a
18389 * non-string to do what the docs promise. */
18390 if (tv1->v_type == VAR_STRING)
18391 {
18392 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18393 p1 = (char_u *)"'";
18394 else
18395 p1 = tv1->vval.v_string;
18396 }
18397 else
18398 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18399 if (tv2->v_type == VAR_STRING)
18400 {
18401 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18402 p2 = (char_u *)"'";
18403 else
18404 p2 = tv2->vval.v_string;
18405 }
18406 else
18407 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018408 if (p1 == NULL)
18409 p1 = (char_u *)"";
18410 if (p2 == NULL)
18411 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018412 if (!item_compare_numeric)
18413 {
18414 if (item_compare_ic)
18415 res = STRICMP(p1, p2);
18416 else
18417 res = STRCMP(p1, p2);
18418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018420 {
18421 double n1, n2;
18422 n1 = strtod((char *)p1, (char **)&p1);
18423 n2 = strtod((char *)p2, (char **)&p2);
18424 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18425 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018426
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018427 /* When the result would be zero, compare the item indexes. Makes the
18428 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018429 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018430 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018431
Bram Moolenaar0d660222005-01-07 21:51:51 +000018432 vim_free(tofree1);
18433 vim_free(tofree2);
18434 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435}
18436
18437 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018438#ifdef __BORLANDC__
18439_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018441item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018442{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018443 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018444 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018445 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018446 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018447 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018449 /* shortcut after failure in previous call; compare all items equal */
18450 if (item_compare_func_err)
18451 return 0;
18452
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018453 si1 = (sortItem_T *)s1;
18454 si2 = (sortItem_T *)s2;
18455
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018456 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018457 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018458 copy_tv(&si1->item->li_tv, &argv[0]);
18459 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018460
18461 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018462 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018463 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18464 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018465 clear_tv(&argv[0]);
18466 clear_tv(&argv[1]);
18467
18468 if (res == FAIL)
18469 res = ITEM_COMPARE_FAIL;
18470 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018471 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18472 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018473 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018474 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018475
18476 /* When the result would be zero, compare the pointers themselves. Makes
18477 * the sort stable. */
18478 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018479 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018480
Bram Moolenaar0d660222005-01-07 21:51:51 +000018481 return res;
18482}
18483
18484/*
18485 * "sort({list})" function
18486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018488do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489{
Bram Moolenaar33570922005-01-25 22:26:29 +000018490 list_T *l;
18491 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018492 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018493 long len;
18494 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495
Bram Moolenaar0d660222005-01-07 21:51:51 +000018496 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018497 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 else
18499 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018500 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018501 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018502 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18503 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018504 return;
18505 rettv->vval.v_list = l;
18506 rettv->v_type = VAR_LIST;
18507 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508
Bram Moolenaar0d660222005-01-07 21:51:51 +000018509 len = list_len(l);
18510 if (len <= 1)
18511 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512
Bram Moolenaar0d660222005-01-07 21:51:51 +000018513 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018514 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018515 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018516#ifdef FEAT_FLOAT
18517 item_compare_float = FALSE;
18518#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018519 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018520 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018521 if (argvars[1].v_type != VAR_UNKNOWN)
18522 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018523 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018524 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018525 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018526 else
18527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018528 int error = FALSE;
18529
18530 i = get_tv_number_chk(&argvars[1], &error);
18531 if (error)
18532 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018533 if (i == 1)
18534 item_compare_ic = TRUE;
18535 else
18536 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018537 if (item_compare_func != NULL)
18538 {
18539 if (STRCMP(item_compare_func, "n") == 0)
18540 {
18541 item_compare_func = NULL;
18542 item_compare_numeric = TRUE;
18543 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018544 else if (STRCMP(item_compare_func, "N") == 0)
18545 {
18546 item_compare_func = NULL;
18547 item_compare_numbers = TRUE;
18548 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018549#ifdef FEAT_FLOAT
18550 else if (STRCMP(item_compare_func, "f") == 0)
18551 {
18552 item_compare_func = NULL;
18553 item_compare_float = TRUE;
18554 }
18555#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018556 else if (STRCMP(item_compare_func, "i") == 0)
18557 {
18558 item_compare_func = NULL;
18559 item_compare_ic = TRUE;
18560 }
18561 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018562 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018563
18564 if (argvars[2].v_type != VAR_UNKNOWN)
18565 {
18566 /* optional third argument: {dict} */
18567 if (argvars[2].v_type != VAR_DICT)
18568 {
18569 EMSG(_(e_dictreq));
18570 return;
18571 }
18572 item_compare_selfdict = argvars[2].vval.v_dict;
18573 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575
Bram Moolenaar0d660222005-01-07 21:51:51 +000018576 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018577 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018578 if (ptrs == NULL)
18579 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580
Bram Moolenaar327aa022014-03-25 18:24:23 +010018581 i = 0;
18582 if (sort)
18583 {
18584 /* sort(): ptrs will be the list to sort */
18585 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018586 {
18587 ptrs[i].item = li;
18588 ptrs[i].idx = i;
18589 ++i;
18590 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018591
18592 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018593 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018594 /* test the compare function */
18595 if (item_compare_func != NULL
18596 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018597 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018598 EMSG(_("E702: Sort compare function failed"));
18599 else
18600 {
18601 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018602 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018603 item_compare_func == NULL ? item_compare : item_compare2);
18604
18605 if (!item_compare_func_err)
18606 {
18607 /* Clear the List and append the items in sorted order. */
18608 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18609 l->lv_len = 0;
18610 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018611 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018612 }
18613 }
18614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018616 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018617 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018618
18619 /* f_uniq(): ptrs will be a stack of items to remove */
18620 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018621 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018622 item_compare_func_ptr = item_compare_func
18623 ? item_compare2 : item_compare;
18624
18625 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18626 li = li->li_next)
18627 {
18628 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18629 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018630 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018631 if (item_compare_func_err)
18632 {
18633 EMSG(_("E882: Uniq compare function failed"));
18634 break;
18635 }
18636 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018638 if (!item_compare_func_err)
18639 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018640 while (--i >= 0)
18641 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018642 li = ptrs[i].item->li_next;
18643 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018644 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018645 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018646 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018647 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018648 list_fix_watch(l, li);
18649 listitem_free(li);
18650 l->lv_len--;
18651 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018652 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018653 }
18654
18655 vim_free(ptrs);
18656 }
18657}
18658
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018659/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018660 * "sort({list})" function
18661 */
18662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018663f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018664{
18665 do_sort_uniq(argvars, rettv, TRUE);
18666}
18667
18668/*
18669 * "uniq({list})" function
18670 */
18671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018672f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018673{
18674 do_sort_uniq(argvars, rettv, FALSE);
18675}
18676
18677/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018678 * "soundfold({word})" function
18679 */
18680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018681f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018682{
18683 char_u *s;
18684
18685 rettv->v_type = VAR_STRING;
18686 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018687#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018688 rettv->vval.v_string = eval_soundfold(s);
18689#else
18690 rettv->vval.v_string = vim_strsave(s);
18691#endif
18692}
18693
18694/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018695 * "spellbadword()" function
18696 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018698f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018699{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018700 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018701 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018702 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018703
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018704 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018705 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018706
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018707#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018708 if (argvars[0].v_type == VAR_UNKNOWN)
18709 {
18710 /* Find the start and length of the badly spelled word. */
18711 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18712 if (len != 0)
18713 word = ml_get_cursor();
18714 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018715 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018716 {
18717 char_u *str = get_tv_string_chk(&argvars[0]);
18718 int capcol = -1;
18719
18720 if (str != NULL)
18721 {
18722 /* Check the argument for spelling. */
18723 while (*str != NUL)
18724 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018725 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018726 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018727 {
18728 word = str;
18729 break;
18730 }
18731 str += len;
18732 }
18733 }
18734 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018735#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018736
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018737 list_append_string(rettv->vval.v_list, word, len);
18738 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018739 attr == HLF_SPB ? "bad" :
18740 attr == HLF_SPR ? "rare" :
18741 attr == HLF_SPL ? "local" :
18742 attr == HLF_SPC ? "caps" :
18743 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018744}
18745
18746/*
18747 * "spellsuggest()" function
18748 */
18749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018750f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018751{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018752#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018753 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018754 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018755 int maxcount;
18756 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018757 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018758 listitem_T *li;
18759 int need_capital = FALSE;
18760#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018761
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018762 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018763 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018764
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018765#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018766 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018767 {
18768 str = get_tv_string(&argvars[0]);
18769 if (argvars[1].v_type != VAR_UNKNOWN)
18770 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018771 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018772 if (maxcount <= 0)
18773 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018774 if (argvars[2].v_type != VAR_UNKNOWN)
18775 {
18776 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18777 if (typeerr)
18778 return;
18779 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018780 }
18781 else
18782 maxcount = 25;
18783
Bram Moolenaar4770d092006-01-12 23:22:24 +000018784 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018785
18786 for (i = 0; i < ga.ga_len; ++i)
18787 {
18788 str = ((char_u **)ga.ga_data)[i];
18789
18790 li = listitem_alloc();
18791 if (li == NULL)
18792 vim_free(str);
18793 else
18794 {
18795 li->li_tv.v_type = VAR_STRING;
18796 li->li_tv.v_lock = 0;
18797 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018798 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018799 }
18800 }
18801 ga_clear(&ga);
18802 }
18803#endif
18804}
18805
Bram Moolenaar0d660222005-01-07 21:51:51 +000018806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018807f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018808{
18809 char_u *str;
18810 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018811 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018812 regmatch_T regmatch;
18813 char_u patbuf[NUMBUFLEN];
18814 char_u *save_cpo;
18815 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018816 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018817 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018818 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018819
18820 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18821 save_cpo = p_cpo;
18822 p_cpo = (char_u *)"";
18823
18824 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018825 if (argvars[1].v_type != VAR_UNKNOWN)
18826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018827 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18828 if (pat == NULL)
18829 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018830 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018831 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018832 }
18833 if (pat == NULL || *pat == NUL)
18834 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018835
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018836 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018838 if (typeerr)
18839 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840
Bram Moolenaar0d660222005-01-07 21:51:51 +000018841 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18842 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018844 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018845 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018846 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018847 if (*str == NUL)
18848 match = FALSE; /* empty item at the end */
18849 else
18850 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018851 if (match)
18852 end = regmatch.startp[0];
18853 else
18854 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018855 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18856 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018857 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018858 if (list_append_string(rettv->vval.v_list, str,
18859 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018860 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018861 }
18862 if (!match)
18863 break;
18864 /* Advance to just after the match. */
18865 if (regmatch.endp[0] > str)
18866 col = 0;
18867 else
18868 {
18869 /* Don't get stuck at the same match. */
18870#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018871 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018872#else
18873 col = 1;
18874#endif
18875 }
18876 str = regmatch.endp[0];
18877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878
Bram Moolenaar473de612013-06-08 18:19:48 +020018879 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881
Bram Moolenaar0d660222005-01-07 21:51:51 +000018882 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883}
18884
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018885#ifdef FEAT_FLOAT
18886/*
18887 * "sqrt()" function
18888 */
18889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018890f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018891{
18892 float_T f;
18893
18894 rettv->v_type = VAR_FLOAT;
18895 if (get_float_arg(argvars, &f) == OK)
18896 rettv->vval.v_float = sqrt(f);
18897 else
18898 rettv->vval.v_float = 0.0;
18899}
18900
18901/*
18902 * "str2float()" function
18903 */
18904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018905f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018906{
18907 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18908
18909 if (*p == '+')
18910 p = skipwhite(p + 1);
18911 (void)string2float(p, &rettv->vval.v_float);
18912 rettv->v_type = VAR_FLOAT;
18913}
18914#endif
18915
Bram Moolenaar2c932302006-03-18 21:42:09 +000018916/*
18917 * "str2nr()" function
18918 */
18919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018920f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018921{
18922 int base = 10;
18923 char_u *p;
18924 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018925 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018926
18927 if (argvars[1].v_type != VAR_UNKNOWN)
18928 {
18929 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018930 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018931 {
18932 EMSG(_(e_invarg));
18933 return;
18934 }
18935 }
18936
18937 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018938 if (*p == '+')
18939 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018940 switch (base)
18941 {
18942 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18943 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18944 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18945 default: what = 0;
18946 }
18947 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018948 rettv->vval.v_number = n;
18949}
18950
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951#ifdef HAVE_STRFTIME
18952/*
18953 * "strftime({format}[, {time}])" function
18954 */
18955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018956f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957{
18958 char_u result_buf[256];
18959 struct tm *curtime;
18960 time_t seconds;
18961 char_u *p;
18962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018963 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018965 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018966 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 seconds = time(NULL);
18968 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018969 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 curtime = localtime(&seconds);
18971 /* MSVC returns NULL for an invalid value of seconds. */
18972 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018973 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974 else
18975 {
18976# ifdef FEAT_MBYTE
18977 vimconv_T conv;
18978 char_u *enc;
18979
18980 conv.vc_type = CONV_NONE;
18981 enc = enc_locale();
18982 convert_setup(&conv, p_enc, enc);
18983 if (conv.vc_type != CONV_NONE)
18984 p = string_convert(&conv, p, NULL);
18985# endif
18986 if (p != NULL)
18987 (void)strftime((char *)result_buf, sizeof(result_buf),
18988 (char *)p, curtime);
18989 else
18990 result_buf[0] = NUL;
18991
18992# ifdef FEAT_MBYTE
18993 if (conv.vc_type != CONV_NONE)
18994 vim_free(p);
18995 convert_setup(&conv, enc, p_enc);
18996 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018997 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 else
18999# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019000 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001
19002# ifdef FEAT_MBYTE
19003 /* Release conversion descriptors */
19004 convert_setup(&conv, NULL, NULL);
19005 vim_free(enc);
19006# endif
19007 }
19008}
19009#endif
19010
19011/*
19012 * "stridx()" function
19013 */
19014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019015f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016{
19017 char_u buf[NUMBUFLEN];
19018 char_u *needle;
19019 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019020 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019022 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019024 needle = get_tv_string_chk(&argvars[1]);
19025 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019026 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019027 if (needle == NULL || haystack == NULL)
19028 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029
Bram Moolenaar33570922005-01-25 22:26:29 +000019030 if (argvars[2].v_type != VAR_UNKNOWN)
19031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019032 int error = FALSE;
19033
19034 start_idx = get_tv_number_chk(&argvars[2], &error);
19035 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019036 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019037 if (start_idx >= 0)
19038 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019039 }
19040
19041 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19042 if (pos != NULL)
19043 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044}
19045
19046/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019047 * "string()" function
19048 */
19049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019050f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019051{
19052 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019053 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019055 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019056 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019057 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019058 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019059 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060}
19061
19062/*
19063 * "strlen()" function
19064 */
19065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019066f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019068 rettv->vval.v_number = (varnumber_T)(STRLEN(
19069 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070}
19071
19072/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019073 * "strchars()" function
19074 */
19075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019076f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019077{
19078 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019079 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019080#ifdef FEAT_MBYTE
19081 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019082 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019083#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019084
19085 if (argvars[1].v_type != VAR_UNKNOWN)
19086 skipcc = get_tv_number_chk(&argvars[1], NULL);
19087 if (skipcc < 0 || skipcc > 1)
19088 EMSG(_(e_invarg));
19089 else
19090 {
19091#ifdef FEAT_MBYTE
19092 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19093 while (*s != NUL)
19094 {
19095 func_mb_ptr2char_adv(&s);
19096 ++len;
19097 }
19098 rettv->vval.v_number = len;
19099#else
19100 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19101#endif
19102 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019103}
19104
19105/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019106 * "strdisplaywidth()" function
19107 */
19108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019109f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019110{
19111 char_u *s = get_tv_string(&argvars[0]);
19112 int col = 0;
19113
19114 if (argvars[1].v_type != VAR_UNKNOWN)
19115 col = get_tv_number(&argvars[1]);
19116
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019117 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019118}
19119
19120/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019121 * "strwidth()" function
19122 */
19123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019124f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019125{
19126 char_u *s = get_tv_string(&argvars[0]);
19127
19128 rettv->vval.v_number = (varnumber_T)(
19129#ifdef FEAT_MBYTE
19130 mb_string2cells(s, -1)
19131#else
19132 STRLEN(s)
19133#endif
19134 );
19135}
19136
19137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 * "strpart()" function
19139 */
19140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019141f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142{
19143 char_u *p;
19144 int n;
19145 int len;
19146 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019147 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019149 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150 slen = (int)STRLEN(p);
19151
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019152 n = get_tv_number_chk(&argvars[1], &error);
19153 if (error)
19154 len = 0;
19155 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019156 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 else
19158 len = slen - n; /* default len: all bytes that are available. */
19159
19160 /*
19161 * Only return the overlap between the specified part and the actual
19162 * string.
19163 */
19164 if (n < 0)
19165 {
19166 len += n;
19167 n = 0;
19168 }
19169 else if (n > slen)
19170 n = slen;
19171 if (len < 0)
19172 len = 0;
19173 else if (n + len > slen)
19174 len = slen - n;
19175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019176 rettv->v_type = VAR_STRING;
19177 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178}
19179
19180/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019181 * "strridx()" function
19182 */
19183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019184f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019185{
19186 char_u buf[NUMBUFLEN];
19187 char_u *needle;
19188 char_u *haystack;
19189 char_u *rest;
19190 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019191 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019192
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019193 needle = get_tv_string_chk(&argvars[1]);
19194 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019195
19196 rettv->vval.v_number = -1;
19197 if (needle == NULL || haystack == NULL)
19198 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019199
19200 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019201 if (argvars[2].v_type != VAR_UNKNOWN)
19202 {
19203 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019204 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019205 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019206 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019207 }
19208 else
19209 end_idx = haystack_len;
19210
Bram Moolenaar0d660222005-01-07 21:51:51 +000019211 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019212 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019213 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019214 lastmatch = haystack + end_idx;
19215 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019216 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019217 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019218 for (rest = haystack; *rest != '\0'; ++rest)
19219 {
19220 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019221 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019222 break;
19223 lastmatch = rest;
19224 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019225 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019226
19227 if (lastmatch == NULL)
19228 rettv->vval.v_number = -1;
19229 else
19230 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19231}
19232
19233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234 * "strtrans()" function
19235 */
19236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019237f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019239 rettv->v_type = VAR_STRING;
19240 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241}
19242
19243/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019244 * "submatch()" function
19245 */
19246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019247f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019248{
Bram Moolenaar41571762014-04-02 19:00:58 +020019249 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019250 int no;
19251 int retList = 0;
19252
19253 no = (int)get_tv_number_chk(&argvars[0], &error);
19254 if (error)
19255 return;
19256 error = FALSE;
19257 if (argvars[1].v_type != VAR_UNKNOWN)
19258 retList = get_tv_number_chk(&argvars[1], &error);
19259 if (error)
19260 return;
19261
19262 if (retList == 0)
19263 {
19264 rettv->v_type = VAR_STRING;
19265 rettv->vval.v_string = reg_submatch(no);
19266 }
19267 else
19268 {
19269 rettv->v_type = VAR_LIST;
19270 rettv->vval.v_list = reg_submatch_list(no);
19271 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019272}
19273
19274/*
19275 * "substitute()" function
19276 */
19277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019278f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019279{
19280 char_u patbuf[NUMBUFLEN];
19281 char_u subbuf[NUMBUFLEN];
19282 char_u flagsbuf[NUMBUFLEN];
19283
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019284 char_u *str = get_tv_string_chk(&argvars[0]);
19285 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19286 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19287 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19288
Bram Moolenaar0d660222005-01-07 21:51:51 +000019289 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019290 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19291 rettv->vval.v_string = NULL;
19292 else
19293 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019294}
19295
19296/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019297 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019300f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301{
19302 int id = 0;
19303#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019304 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305 long col;
19306 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019307 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019309 lnum = get_tv_lnum(argvars); /* -1 on type error */
19310 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19311 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019313 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019314 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019315 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316#endif
19317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019318 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319}
19320
19321/*
19322 * "synIDattr(id, what [, mode])" function
19323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019325f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326{
19327 char_u *p = NULL;
19328#ifdef FEAT_SYN_HL
19329 int id;
19330 char_u *what;
19331 char_u *mode;
19332 char_u modebuf[NUMBUFLEN];
19333 int modec;
19334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019335 id = get_tv_number(&argvars[0]);
19336 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019337 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019339 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019341 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 modec = 0; /* replace invalid with current */
19343 }
19344 else
19345 {
19346#ifdef FEAT_GUI
19347 if (gui.in_use)
19348 modec = 'g';
19349 else
19350#endif
19351 if (t_colors > 1)
19352 modec = 'c';
19353 else
19354 modec = 't';
19355 }
19356
19357
19358 switch (TOLOWER_ASC(what[0]))
19359 {
19360 case 'b':
19361 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19362 p = highlight_color(id, what, modec);
19363 else /* bold */
19364 p = highlight_has_attr(id, HL_BOLD, modec);
19365 break;
19366
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019367 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368 p = highlight_color(id, what, modec);
19369 break;
19370
19371 case 'i':
19372 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19373 p = highlight_has_attr(id, HL_INVERSE, modec);
19374 else /* italic */
19375 p = highlight_has_attr(id, HL_ITALIC, modec);
19376 break;
19377
19378 case 'n': /* name */
19379 p = get_highlight_name(NULL, id - 1);
19380 break;
19381
19382 case 'r': /* reverse */
19383 p = highlight_has_attr(id, HL_INVERSE, modec);
19384 break;
19385
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019386 case 's':
19387 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19388 p = highlight_color(id, what, modec);
19389 else /* standout */
19390 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 break;
19392
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019393 case 'u':
19394 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19395 /* underline */
19396 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19397 else
19398 /* undercurl */
19399 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 break;
19401 }
19402
19403 if (p != NULL)
19404 p = vim_strsave(p);
19405#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019406 rettv->v_type = VAR_STRING;
19407 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408}
19409
19410/*
19411 * "synIDtrans(id)" function
19412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019414f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415{
19416 int id;
19417
19418#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019419 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420
19421 if (id > 0)
19422 id = syn_get_final_id(id);
19423 else
19424#endif
19425 id = 0;
19426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019427 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428}
19429
19430/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019431 * "synconcealed(lnum, col)" function
19432 */
19433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019434f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019435{
19436#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19437 long lnum;
19438 long col;
19439 int syntax_flags = 0;
19440 int cchar;
19441 int matchid = 0;
19442 char_u str[NUMBUFLEN];
19443#endif
19444
19445 rettv->v_type = VAR_LIST;
19446 rettv->vval.v_list = NULL;
19447
19448#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19449 lnum = get_tv_lnum(argvars); /* -1 on type error */
19450 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19451
19452 vim_memset(str, NUL, sizeof(str));
19453
19454 if (rettv_list_alloc(rettv) != FAIL)
19455 {
19456 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19457 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19458 && curwin->w_p_cole > 0)
19459 {
19460 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19461 syntax_flags = get_syntax_info(&matchid);
19462
19463 /* get the conceal character */
19464 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19465 {
19466 cchar = syn_get_sub_char();
19467 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19468 cchar = lcs_conceal;
19469 if (cchar != NUL)
19470 {
19471# ifdef FEAT_MBYTE
19472 if (has_mbyte)
19473 (*mb_char2bytes)(cchar, str);
19474 else
19475# endif
19476 str[0] = cchar;
19477 }
19478 }
19479 }
19480
19481 list_append_number(rettv->vval.v_list,
19482 (syntax_flags & HL_CONCEAL) != 0);
19483 /* -1 to auto-determine strlen */
19484 list_append_string(rettv->vval.v_list, str, -1);
19485 list_append_number(rettv->vval.v_list, matchid);
19486 }
19487#endif
19488}
19489
19490/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019491 * "synstack(lnum, col)" function
19492 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019494f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019495{
19496#ifdef FEAT_SYN_HL
19497 long lnum;
19498 long col;
19499 int i;
19500 int id;
19501#endif
19502
19503 rettv->v_type = VAR_LIST;
19504 rettv->vval.v_list = NULL;
19505
19506#ifdef FEAT_SYN_HL
19507 lnum = get_tv_lnum(argvars); /* -1 on type error */
19508 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19509
19510 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019511 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019512 && rettv_list_alloc(rettv) != FAIL)
19513 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019514 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019515 for (i = 0; ; ++i)
19516 {
19517 id = syn_get_stack_item(i);
19518 if (id < 0)
19519 break;
19520 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19521 break;
19522 }
19523 }
19524#endif
19525}
19526
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019528get_cmd_output_as_rettv(
19529 typval_T *argvars,
19530 typval_T *rettv,
19531 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019533 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019535 char_u *infile = NULL;
19536 char_u buf[NUMBUFLEN];
19537 int err = FALSE;
19538 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019539 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019540 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019542 rettv->v_type = VAR_STRING;
19543 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019544 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019545 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019546
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019547 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019548 {
19549 /*
19550 * Write the string to a temp file, to be used for input of the shell
19551 * command.
19552 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019553 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019554 {
19555 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019556 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019557 }
19558
19559 fd = mch_fopen((char *)infile, WRITEBIN);
19560 if (fd == NULL)
19561 {
19562 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019563 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019564 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019565 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019566 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019567 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19568 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019569 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019570 else
19571 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019572 size_t len;
19573
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019574 p = get_tv_string_buf_chk(&argvars[1], buf);
19575 if (p == NULL)
19576 {
19577 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019578 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019579 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019580 len = STRLEN(p);
19581 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019582 err = TRUE;
19583 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019584 if (fclose(fd) != 0)
19585 err = TRUE;
19586 if (err)
19587 {
19588 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019589 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019590 }
19591 }
19592
Bram Moolenaar52a72462014-08-29 15:53:52 +020019593 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19594 * echoes typeahead, that messes up the display. */
19595 if (!msg_silent)
19596 flags += SHELL_COOKED;
19597
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019598 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019600 int len;
19601 listitem_T *li;
19602 char_u *s = NULL;
19603 char_u *start;
19604 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019605 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606
Bram Moolenaar52a72462014-08-29 15:53:52 +020019607 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019608 if (res == NULL)
19609 goto errret;
19610
19611 list = list_alloc();
19612 if (list == NULL)
19613 goto errret;
19614
19615 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019617 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019618 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019619 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019620 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019621
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019622 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019623 if (s == NULL)
19624 goto errret;
19625
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019626 for (p = s; start < end; ++p, ++start)
19627 *p = *start == NUL ? NL : *start;
19628 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019629
19630 li = listitem_alloc();
19631 if (li == NULL)
19632 {
19633 vim_free(s);
19634 goto errret;
19635 }
19636 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019637 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019638 li->li_tv.vval.v_string = s;
19639 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019641
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019642 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019643 rettv->v_type = VAR_LIST;
19644 rettv->vval.v_list = list;
19645 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019647 else
19648 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019649 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019650#ifdef USE_CR
19651 /* translate <CR> into <NL> */
19652 if (res != NULL)
19653 {
19654 char_u *s;
19655
19656 for (s = res; *s; ++s)
19657 {
19658 if (*s == CAR)
19659 *s = NL;
19660 }
19661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662#else
19663# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019664 /* translate <CR><NL> into <NL> */
19665 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019667 char_u *s, *d;
19668
19669 d = res;
19670 for (s = res; *s; ++s)
19671 {
19672 if (s[0] == CAR && s[1] == NL)
19673 ++s;
19674 *d++ = *s;
19675 }
19676 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678# endif
19679#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019680 rettv->vval.v_string = res;
19681 res = NULL;
19682 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019683
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019684errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019685 if (infile != NULL)
19686 {
19687 mch_remove(infile);
19688 vim_free(infile);
19689 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019690 if (res != NULL)
19691 vim_free(res);
19692 if (list != NULL)
19693 list_free(list, TRUE);
19694}
19695
19696/*
19697 * "system()" function
19698 */
19699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019700f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019701{
19702 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19703}
19704
19705/*
19706 * "systemlist()" function
19707 */
19708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019709f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019710{
19711 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712}
19713
19714/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019715 * "tabpagebuflist()" function
19716 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019718f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019719{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019720#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019721 tabpage_T *tp;
19722 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019723
19724 if (argvars[0].v_type == VAR_UNKNOWN)
19725 wp = firstwin;
19726 else
19727 {
19728 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19729 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019730 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019731 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019732 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019733 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019734 for (; wp != NULL; wp = wp->w_next)
19735 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019736 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019737 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019738 }
19739#endif
19740}
19741
19742
19743/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019744 * "tabpagenr()" function
19745 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019747f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019748{
19749 int nr = 1;
19750#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019751 char_u *arg;
19752
19753 if (argvars[0].v_type != VAR_UNKNOWN)
19754 {
19755 arg = get_tv_string_chk(&argvars[0]);
19756 nr = 0;
19757 if (arg != NULL)
19758 {
19759 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019760 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019761 else
19762 EMSG2(_(e_invexpr2), arg);
19763 }
19764 }
19765 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019766 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019767#endif
19768 rettv->vval.v_number = nr;
19769}
19770
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019771
19772#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019773static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019774
19775/*
19776 * Common code for tabpagewinnr() and winnr().
19777 */
19778 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019779get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019780{
19781 win_T *twin;
19782 int nr = 1;
19783 win_T *wp;
19784 char_u *arg;
19785
19786 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19787 if (argvar->v_type != VAR_UNKNOWN)
19788 {
19789 arg = get_tv_string_chk(argvar);
19790 if (arg == NULL)
19791 nr = 0; /* type error; errmsg already given */
19792 else if (STRCMP(arg, "$") == 0)
19793 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19794 else if (STRCMP(arg, "#") == 0)
19795 {
19796 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19797 if (twin == NULL)
19798 nr = 0;
19799 }
19800 else
19801 {
19802 EMSG2(_(e_invexpr2), arg);
19803 nr = 0;
19804 }
19805 }
19806
19807 if (nr > 0)
19808 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19809 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019810 {
19811 if (wp == NULL)
19812 {
19813 /* didn't find it in this tabpage */
19814 nr = 0;
19815 break;
19816 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019817 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019818 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019819 return nr;
19820}
19821#endif
19822
19823/*
19824 * "tabpagewinnr()" function
19825 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019827f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019828{
19829 int nr = 1;
19830#ifdef FEAT_WINDOWS
19831 tabpage_T *tp;
19832
19833 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19834 if (tp == NULL)
19835 nr = 0;
19836 else
19837 nr = get_winnr(tp, &argvars[1]);
19838#endif
19839 rettv->vval.v_number = nr;
19840}
19841
19842
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019843/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019844 * "tagfiles()" function
19845 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019847f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019848{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019849 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019850 tagname_T tn;
19851 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019852
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019853 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019854 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019855 fname = alloc(MAXPATHL);
19856 if (fname == NULL)
19857 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019858
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019859 for (first = TRUE; ; first = FALSE)
19860 if (get_tagfname(&tn, first, fname) == FAIL
19861 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019862 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019863 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019864 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019865}
19866
19867/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019868 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019869 */
19870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019871f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019872{
19873 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019874
19875 tag_pattern = get_tv_string(&argvars[0]);
19876
19877 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019878 if (*tag_pattern == NUL)
19879 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019880
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019881 if (rettv_list_alloc(rettv) == OK)
19882 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019883}
19884
19885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 * "tempname()" function
19887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019889f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890{
19891 static int x = 'A';
19892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019893 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019894 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895
19896 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19897 * names. Skip 'I' and 'O', they are used for shell redirection. */
19898 do
19899 {
19900 if (x == 'Z')
19901 x = '0';
19902 else if (x == '9')
19903 x = 'A';
19904 else
19905 {
19906#ifdef EBCDIC
19907 if (x == 'I')
19908 x = 'J';
19909 else if (x == 'R')
19910 x = 'S';
19911 else
19912#endif
19913 ++x;
19914 }
19915 } while (x == 'I' || x == 'O');
19916}
19917
19918/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019919 * "test(list)" function: Just checking the walls...
19920 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019922f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019923{
19924 /* Used for unit testing. Change the code below to your liking. */
19925#if 0
19926 listitem_T *li;
19927 list_T *l;
19928 char_u *bad, *good;
19929
19930 if (argvars[0].v_type != VAR_LIST)
19931 return;
19932 l = argvars[0].vval.v_list;
19933 if (l == NULL)
19934 return;
19935 li = l->lv_first;
19936 if (li == NULL)
19937 return;
19938 bad = get_tv_string(&li->li_tv);
19939 li = li->li_next;
19940 if (li == NULL)
19941 return;
19942 good = get_tv_string(&li->li_tv);
19943 rettv->vval.v_number = test_edit_score(bad, good);
19944#endif
19945}
19946
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019947#ifdef FEAT_FLOAT
19948/*
19949 * "tan()" function
19950 */
19951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019952f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019953{
19954 float_T f;
19955
19956 rettv->v_type = VAR_FLOAT;
19957 if (get_float_arg(argvars, &f) == OK)
19958 rettv->vval.v_float = tan(f);
19959 else
19960 rettv->vval.v_float = 0.0;
19961}
19962
19963/*
19964 * "tanh()" function
19965 */
19966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019967f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019968{
19969 float_T f;
19970
19971 rettv->v_type = VAR_FLOAT;
19972 if (get_float_arg(argvars, &f) == OK)
19973 rettv->vval.v_float = tanh(f);
19974 else
19975 rettv->vval.v_float = 0.0;
19976}
19977#endif
19978
Bram Moolenaard52d9742005-08-21 22:20:28 +000019979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 * "tolower(string)" function
19981 */
19982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019983f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984{
19985 char_u *p;
19986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019987 p = vim_strsave(get_tv_string(&argvars[0]));
19988 rettv->v_type = VAR_STRING;
19989 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990
19991 if (p != NULL)
19992 while (*p != NUL)
19993 {
19994#ifdef FEAT_MBYTE
19995 int l;
19996
19997 if (enc_utf8)
19998 {
19999 int c, lc;
20000
20001 c = utf_ptr2char(p);
20002 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020003 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004 /* TODO: reallocate string when byte count changes. */
20005 if (utf_char2len(lc) == l)
20006 utf_char2bytes(lc, p);
20007 p += l;
20008 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020009 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010 p += l; /* skip multi-byte character */
20011 else
20012#endif
20013 {
20014 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20015 ++p;
20016 }
20017 }
20018}
20019
20020/*
20021 * "toupper(string)" function
20022 */
20023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020024f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020026 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020027 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028}
20029
20030/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020031 * "tr(string, fromstr, tostr)" function
20032 */
20033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020034f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020035{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020036 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020037 char_u *fromstr;
20038 char_u *tostr;
20039 char_u *p;
20040#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020041 int inlen;
20042 int fromlen;
20043 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020044 int idx;
20045 char_u *cpstr;
20046 int cplen;
20047 int first = TRUE;
20048#endif
20049 char_u buf[NUMBUFLEN];
20050 char_u buf2[NUMBUFLEN];
20051 garray_T ga;
20052
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020053 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020054 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20055 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020056
20057 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020058 rettv->v_type = VAR_STRING;
20059 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020060 if (fromstr == NULL || tostr == NULL)
20061 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020062 ga_init2(&ga, (int)sizeof(char), 80);
20063
20064#ifdef FEAT_MBYTE
20065 if (!has_mbyte)
20066#endif
20067 /* not multi-byte: fromstr and tostr must be the same length */
20068 if (STRLEN(fromstr) != STRLEN(tostr))
20069 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020070#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020071error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020072#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020073 EMSG2(_(e_invarg2), fromstr);
20074 ga_clear(&ga);
20075 return;
20076 }
20077
20078 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020079 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020080 {
20081#ifdef FEAT_MBYTE
20082 if (has_mbyte)
20083 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020084 inlen = (*mb_ptr2len)(in_str);
20085 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020086 cplen = inlen;
20087 idx = 0;
20088 for (p = fromstr; *p != NUL; p += fromlen)
20089 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020090 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020091 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020092 {
20093 for (p = tostr; *p != NUL; p += tolen)
20094 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020095 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020096 if (idx-- == 0)
20097 {
20098 cplen = tolen;
20099 cpstr = p;
20100 break;
20101 }
20102 }
20103 if (*p == NUL) /* tostr is shorter than fromstr */
20104 goto error;
20105 break;
20106 }
20107 ++idx;
20108 }
20109
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020110 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020111 {
20112 /* Check that fromstr and tostr have the same number of
20113 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020114 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020115 first = FALSE;
20116 for (p = tostr; *p != NUL; p += tolen)
20117 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020118 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020119 --idx;
20120 }
20121 if (idx != 0)
20122 goto error;
20123 }
20124
Bram Moolenaarcde88542015-08-11 19:14:00 +020020125 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020126 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020127 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020128
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020129 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020130 }
20131 else
20132#endif
20133 {
20134 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020135 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020136 if (p != NULL)
20137 ga_append(&ga, tostr[p - fromstr]);
20138 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020139 ga_append(&ga, *in_str);
20140 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020141 }
20142 }
20143
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020144 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020145 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020146 ga_append(&ga, NUL);
20147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020148 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020149}
20150
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020151#ifdef FEAT_FLOAT
20152/*
20153 * "trunc({float})" function
20154 */
20155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020156f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020157{
20158 float_T f;
20159
20160 rettv->v_type = VAR_FLOAT;
20161 if (get_float_arg(argvars, &f) == OK)
20162 /* trunc() is not in C90, use floor() or ceil() instead. */
20163 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20164 else
20165 rettv->vval.v_float = 0.0;
20166}
20167#endif
20168
Bram Moolenaar8299df92004-07-10 09:47:34 +000020169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 * "type(expr)" function
20171 */
20172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020173f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020175 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020176
20177 switch (argvars[0].v_type)
20178 {
20179 case VAR_NUMBER: n = 0; break;
20180 case VAR_STRING: n = 1; break;
20181 case VAR_FUNC: n = 2; break;
20182 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020183 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020184 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020185 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020186 if (argvars[0].vval.v_number == VVAL_FALSE
20187 || argvars[0].vval.v_number == VVAL_TRUE)
20188 n = 6;
20189 else
20190 n = 7;
20191 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020192 case VAR_JOB: n = 8; break;
20193 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020194 case VAR_UNKNOWN:
20195 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20196 n = -1;
20197 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020198 }
20199 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200}
20201
20202/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020203 * "undofile(name)" function
20204 */
20205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020206f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020207{
20208 rettv->v_type = VAR_STRING;
20209#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020210 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020211 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020212
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020213 if (*fname == NUL)
20214 {
20215 /* If there is no file name there will be no undo file. */
20216 rettv->vval.v_string = NULL;
20217 }
20218 else
20219 {
20220 char_u *ffname = FullName_save(fname, FALSE);
20221
20222 if (ffname != NULL)
20223 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20224 vim_free(ffname);
20225 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020226 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020227#else
20228 rettv->vval.v_string = NULL;
20229#endif
20230}
20231
20232/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020233 * "undotree()" function
20234 */
20235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020236f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020237{
20238 if (rettv_dict_alloc(rettv) == OK)
20239 {
20240 dict_T *dict = rettv->vval.v_dict;
20241 list_T *list;
20242
Bram Moolenaar730cde92010-06-27 05:18:54 +020020243 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020244 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020245 dict_add_nr_str(dict, "save_last",
20246 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020247 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20248 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020249 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020250
20251 list = list_alloc();
20252 if (list != NULL)
20253 {
20254 u_eval_tree(curbuf->b_u_oldhead, list);
20255 dict_add_list(dict, "entries", list);
20256 }
20257 }
20258}
20259
20260/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020261 * "values(dict)" function
20262 */
20263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020264f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020265{
20266 dict_list(argvars, rettv, 1);
20267}
20268
20269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270 * "virtcol(string)" function
20271 */
20272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020273f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274{
20275 colnr_T vcol = 0;
20276 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020277 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020279 fp = var2fpos(&argvars[0], FALSE, &fnum);
20280 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20281 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 {
20283 getvvcol(curwin, fp, NULL, NULL, &vcol);
20284 ++vcol;
20285 }
20286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020287 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288}
20289
20290/*
20291 * "visualmode()" function
20292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020294f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 char_u str[2];
20297
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020298 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 str[0] = curbuf->b_visual_mode_eval;
20300 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020301 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302
20303 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020304 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306}
20307
20308/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020309 * "wildmenumode()" function
20310 */
20311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020312f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020313{
20314#ifdef FEAT_WILDMENU
20315 if (wild_menu_showing)
20316 rettv->vval.v_number = 1;
20317#endif
20318}
20319
20320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 * "winbufnr(nr)" function
20322 */
20323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020324f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325{
20326 win_T *wp;
20327
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020328 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020330 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020332 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333}
20334
20335/*
20336 * "wincol()" function
20337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020339f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340{
20341 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020342 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343}
20344
20345/*
20346 * "winheight(nr)" function
20347 */
20348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020349f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350{
20351 win_T *wp;
20352
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020353 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020355 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020357 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358}
20359
20360/*
20361 * "winline()" function
20362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020364f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365{
20366 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020367 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368}
20369
20370/*
20371 * "winnr()" function
20372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020374f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375{
20376 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020377
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020379 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020381 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382}
20383
20384/*
20385 * "winrestcmd()" function
20386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020388f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389{
20390#ifdef FEAT_WINDOWS
20391 win_T *wp;
20392 int winnr = 1;
20393 garray_T ga;
20394 char_u buf[50];
20395
20396 ga_init2(&ga, (int)sizeof(char), 70);
20397 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20398 {
20399 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20400 ga_concat(&ga, buf);
20401# ifdef FEAT_VERTSPLIT
20402 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20403 ga_concat(&ga, buf);
20404# endif
20405 ++winnr;
20406 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020407 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020409 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020411 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020413 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414}
20415
20416/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020417 * "winrestview()" function
20418 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020420f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020421{
20422 dict_T *dict;
20423
20424 if (argvars[0].v_type != VAR_DICT
20425 || (dict = argvars[0].vval.v_dict) == NULL)
20426 EMSG(_(e_invarg));
20427 else
20428 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020429 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20430 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20431 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20432 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020433#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020434 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20435 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020436#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020437 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20438 {
20439 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20440 curwin->w_set_curswant = FALSE;
20441 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020442
Bram Moolenaar82c25852014-05-28 16:47:16 +020020443 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20444 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020445#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020446 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20447 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020448#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020449 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20450 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20451 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20452 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020453
20454 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020455 win_new_height(curwin, curwin->w_height);
20456# ifdef FEAT_VERTSPLIT
20457 win_new_width(curwin, W_WIDTH(curwin));
20458# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020459 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020460
Bram Moolenaarb851a962014-10-31 15:45:52 +010020461 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020462 curwin->w_topline = 1;
20463 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20464 curwin->w_topline = curbuf->b_ml.ml_line_count;
20465#ifdef FEAT_DIFF
20466 check_topfill(curwin, TRUE);
20467#endif
20468 }
20469}
20470
20471/*
20472 * "winsaveview()" function
20473 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020475f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020476{
20477 dict_T *dict;
20478
Bram Moolenaara800b422010-06-27 01:15:55 +020020479 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020480 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020481 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020482
20483 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20484 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20485#ifdef FEAT_VIRTUALEDIT
20486 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20487#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020488 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020489 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20490
20491 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20492#ifdef FEAT_DIFF
20493 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20494#endif
20495 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20496 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20497}
20498
20499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500 * "winwidth(nr)" function
20501 */
20502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020503f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020504{
20505 win_T *wp;
20506
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020507 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020509 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 else
20511#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020512 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020514 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515#endif
20516}
20517
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020519 * "wordcount()" function
20520 */
20521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020522f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020523{
20524 if (rettv_dict_alloc(rettv) == FAIL)
20525 return;
20526 cursor_pos_info(rettv->vval.v_dict);
20527}
20528
20529/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020530 * Write list of strings to file
20531 */
20532 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020533write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020534{
20535 listitem_T *li;
20536 int c;
20537 int ret = OK;
20538 char_u *s;
20539
20540 for (li = list->lv_first; li != NULL; li = li->li_next)
20541 {
20542 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20543 {
20544 if (*s == '\n')
20545 c = putc(NUL, fd);
20546 else
20547 c = putc(*s, fd);
20548 if (c == EOF)
20549 {
20550 ret = FAIL;
20551 break;
20552 }
20553 }
20554 if (!binary || li->li_next != NULL)
20555 if (putc('\n', fd) == EOF)
20556 {
20557 ret = FAIL;
20558 break;
20559 }
20560 if (ret == FAIL)
20561 {
20562 EMSG(_(e_write));
20563 break;
20564 }
20565 }
20566 return ret;
20567}
20568
20569/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020570 * "writefile()" function
20571 */
20572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020573f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020574{
20575 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020576 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020577 char_u *fname;
20578 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020579 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020580
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020581 if (check_restricted() || check_secure())
20582 return;
20583
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020584 if (argvars[0].v_type != VAR_LIST)
20585 {
20586 EMSG2(_(e_listarg), "writefile()");
20587 return;
20588 }
20589 if (argvars[0].vval.v_list == NULL)
20590 return;
20591
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020592 if (argvars[2].v_type != VAR_UNKNOWN)
20593 {
20594 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20595 binary = TRUE;
20596 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20597 append = TRUE;
20598 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020599
20600 /* Always open the file in binary mode, library functions have a mind of
20601 * their own about CR-LF conversion. */
20602 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020603 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20604 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020605 {
20606 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20607 ret = -1;
20608 }
20609 else
20610 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020611 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20612 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020613 fclose(fd);
20614 }
20615
20616 rettv->vval.v_number = ret;
20617}
20618
20619/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020620 * "xor(expr, expr)" function
20621 */
20622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020623f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020624{
20625 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20626 ^ get_tv_number_chk(&argvars[1], NULL);
20627}
20628
20629
20630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020632 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 */
20634 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020635var2fpos(
20636 typval_T *varp,
20637 int dollar_lnum, /* TRUE when $ is last line */
20638 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020640 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020642 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643
Bram Moolenaara5525202006-03-02 22:52:09 +000020644 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020645 if (varp->v_type == VAR_LIST)
20646 {
20647 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020648 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020649 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020650 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020651
20652 l = varp->vval.v_list;
20653 if (l == NULL)
20654 return NULL;
20655
20656 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020657 pos.lnum = list_find_nr(l, 0L, &error);
20658 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020659 return NULL; /* invalid line number */
20660
20661 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020662 pos.col = list_find_nr(l, 1L, &error);
20663 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020664 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020665 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020666
20667 /* We accept "$" for the column number: last column. */
20668 li = list_find(l, 1L);
20669 if (li != NULL && li->li_tv.v_type == VAR_STRING
20670 && li->li_tv.vval.v_string != NULL
20671 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20672 pos.col = len + 1;
20673
Bram Moolenaara5525202006-03-02 22:52:09 +000020674 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020675 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020676 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020677 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020678
Bram Moolenaara5525202006-03-02 22:52:09 +000020679#ifdef FEAT_VIRTUALEDIT
20680 /* Get the virtual offset. Defaults to zero. */
20681 pos.coladd = list_find_nr(l, 2L, &error);
20682 if (error)
20683 pos.coladd = 0;
20684#endif
20685
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020686 return &pos;
20687 }
20688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020689 name = get_tv_string_chk(varp);
20690 if (name == NULL)
20691 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020692 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020694 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20695 {
20696 if (VIsual_active)
20697 return &VIsual;
20698 return &curwin->w_cursor;
20699 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020700 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020702 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020703 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20704 return NULL;
20705 return pp;
20706 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020707
20708#ifdef FEAT_VIRTUALEDIT
20709 pos.coladd = 0;
20710#endif
20711
Bram Moolenaar477933c2007-07-17 14:32:23 +000020712 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020713 {
20714 pos.col = 0;
20715 if (name[1] == '0') /* "w0": first visible line */
20716 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020717 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020718 pos.lnum = curwin->w_topline;
20719 return &pos;
20720 }
20721 else if (name[1] == '$') /* "w$": last visible line */
20722 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020723 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020724 pos.lnum = curwin->w_botline - 1;
20725 return &pos;
20726 }
20727 }
20728 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020730 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 {
20732 pos.lnum = curbuf->b_ml.ml_line_count;
20733 pos.col = 0;
20734 }
20735 else
20736 {
20737 pos.lnum = curwin->w_cursor.lnum;
20738 pos.col = (colnr_T)STRLEN(ml_get_curline());
20739 }
20740 return &pos;
20741 }
20742 return NULL;
20743}
20744
20745/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020746 * Convert list in "arg" into a position and optional file number.
20747 * When "fnump" is NULL there is no file number, only 3 items.
20748 * Note that the column is passed on as-is, the caller may want to decrement
20749 * it to use 1 for the first column.
20750 * Return FAIL when conversion is not possible, doesn't check the position for
20751 * validity.
20752 */
20753 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020754list2fpos(
20755 typval_T *arg,
20756 pos_T *posp,
20757 int *fnump,
20758 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020759{
20760 list_T *l = arg->vval.v_list;
20761 long i = 0;
20762 long n;
20763
Bram Moolenaar493c1782014-05-28 14:34:46 +020020764 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20765 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020766 if (arg->v_type != VAR_LIST
20767 || l == NULL
20768 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020769 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020770 return FAIL;
20771
20772 if (fnump != NULL)
20773 {
20774 n = list_find_nr(l, i++, NULL); /* fnum */
20775 if (n < 0)
20776 return FAIL;
20777 if (n == 0)
20778 n = curbuf->b_fnum; /* current buffer */
20779 *fnump = n;
20780 }
20781
20782 n = list_find_nr(l, i++, NULL); /* lnum */
20783 if (n < 0)
20784 return FAIL;
20785 posp->lnum = n;
20786
20787 n = list_find_nr(l, i++, NULL); /* col */
20788 if (n < 0)
20789 return FAIL;
20790 posp->col = n;
20791
20792#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020793 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020794 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020795 posp->coladd = 0;
20796 else
20797 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020798#endif
20799
Bram Moolenaar493c1782014-05-28 14:34:46 +020020800 if (curswantp != NULL)
20801 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20802
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020803 return OK;
20804}
20805
20806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 * Get the length of an environment variable name.
20808 * Advance "arg" to the first character after the name.
20809 * Return 0 for error.
20810 */
20811 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020812get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813{
20814 char_u *p;
20815 int len;
20816
20817 for (p = *arg; vim_isIDc(*p); ++p)
20818 ;
20819 if (p == *arg) /* no name found */
20820 return 0;
20821
20822 len = (int)(p - *arg);
20823 *arg = p;
20824 return len;
20825}
20826
20827/*
20828 * Get the length of the name of a function or internal variable.
20829 * "arg" is advanced to the first non-white character after the name.
20830 * Return 0 if something is wrong.
20831 */
20832 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020833get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834{
20835 char_u *p;
20836 int len;
20837
20838 /* Find the end of the name. */
20839 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020840 {
20841 if (*p == ':')
20842 {
20843 /* "s:" is start of "s:var", but "n:" is not and can be used in
20844 * slice "[n:]". Also "xx:" is not a namespace. */
20845 len = (int)(p - *arg);
20846 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20847 || len > 1)
20848 break;
20849 }
20850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 if (p == *arg) /* no name found */
20852 return 0;
20853
20854 len = (int)(p - *arg);
20855 *arg = skipwhite(p);
20856
20857 return len;
20858}
20859
20860/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020861 * Get the length of the name of a variable or function.
20862 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020864 * Return -1 if curly braces expansion failed.
20865 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866 * If the name contains 'magic' {}'s, expand them and return the
20867 * expanded name in an allocated string via 'alias' - caller must free.
20868 */
20869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020870get_name_len(
20871 char_u **arg,
20872 char_u **alias,
20873 int evaluate,
20874 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875{
20876 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877 char_u *p;
20878 char_u *expr_start;
20879 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020880
20881 *alias = NULL; /* default to no alias */
20882
20883 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20884 && (*arg)[2] == (int)KE_SNR)
20885 {
20886 /* hard coded <SNR>, already translated */
20887 *arg += 3;
20888 return get_id_len(arg) + 3;
20889 }
20890 len = eval_fname_script(*arg);
20891 if (len > 0)
20892 {
20893 /* literal "<SID>", "s:" or "<SNR>" */
20894 *arg += len;
20895 }
20896
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020898 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020900 p = find_name_end(*arg, &expr_start, &expr_end,
20901 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 if (expr_start != NULL)
20903 {
20904 char_u *temp_string;
20905
20906 if (!evaluate)
20907 {
20908 len += (int)(p - *arg);
20909 *arg = skipwhite(p);
20910 return len;
20911 }
20912
20913 /*
20914 * Include any <SID> etc in the expanded string:
20915 * Thus the -len here.
20916 */
20917 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20918 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020919 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 *alias = temp_string;
20921 *arg = skipwhite(p);
20922 return (int)STRLEN(temp_string);
20923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924
20925 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020926 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927 EMSG2(_(e_invexpr2), *arg);
20928
20929 return len;
20930}
20931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020932/*
20933 * Find the end of a variable or function name, taking care of magic braces.
20934 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20935 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020936 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020937 * Return a pointer to just after the name. Equal to "arg" if there is no
20938 * valid name.
20939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020941find_name_end(
20942 char_u *arg,
20943 char_u **expr_start,
20944 char_u **expr_end,
20945 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020947 int mb_nest = 0;
20948 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020950 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020952 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020954 *expr_start = NULL;
20955 *expr_end = NULL;
20956 }
20957
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020958 /* Quick check for valid starting character. */
20959 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20960 return arg;
20961
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020962 for (p = arg; *p != NUL
20963 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020964 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020965 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020966 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020967 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020968 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020969 if (*p == '\'')
20970 {
20971 /* skip over 'string' to avoid counting [ and ] inside it. */
20972 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20973 ;
20974 if (*p == NUL)
20975 break;
20976 }
20977 else if (*p == '"')
20978 {
20979 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20980 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20981 if (*p == '\\' && p[1] != NUL)
20982 ++p;
20983 if (*p == NUL)
20984 break;
20985 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020986 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20987 {
20988 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020989 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020990 len = (int)(p - arg);
20991 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020992 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020993 break;
20994 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020996 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020998 if (*p == '[')
20999 ++br_nest;
21000 else if (*p == ']')
21001 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021004 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021006 if (*p == '{')
21007 {
21008 mb_nest++;
21009 if (expr_start != NULL && *expr_start == NULL)
21010 *expr_start = p;
21011 }
21012 else if (*p == '}')
21013 {
21014 mb_nest--;
21015 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21016 *expr_end = p;
21017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 }
21020
21021 return p;
21022}
21023
21024/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021025 * Expands out the 'magic' {}'s in a variable/function name.
21026 * Note that this can call itself recursively, to deal with
21027 * constructs like foo{bar}{baz}{bam}
21028 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21029 * "in_start" ^
21030 * "expr_start" ^
21031 * "expr_end" ^
21032 * "in_end" ^
21033 *
21034 * Returns a new allocated string, which the caller must free.
21035 * Returns NULL for failure.
21036 */
21037 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021038make_expanded_name(
21039 char_u *in_start,
21040 char_u *expr_start,
21041 char_u *expr_end,
21042 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021043{
21044 char_u c1;
21045 char_u *retval = NULL;
21046 char_u *temp_result;
21047 char_u *nextcmd = NULL;
21048
21049 if (expr_end == NULL || in_end == NULL)
21050 return NULL;
21051 *expr_start = NUL;
21052 *expr_end = NUL;
21053 c1 = *in_end;
21054 *in_end = NUL;
21055
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021056 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021057 if (temp_result != NULL && nextcmd == NULL)
21058 {
21059 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21060 + (in_end - expr_end) + 1));
21061 if (retval != NULL)
21062 {
21063 STRCPY(retval, in_start);
21064 STRCAT(retval, temp_result);
21065 STRCAT(retval, expr_end + 1);
21066 }
21067 }
21068 vim_free(temp_result);
21069
21070 *in_end = c1; /* put char back for error messages */
21071 *expr_start = '{';
21072 *expr_end = '}';
21073
21074 if (retval != NULL)
21075 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021076 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021077 if (expr_start != NULL)
21078 {
21079 /* Further expansion! */
21080 temp_result = make_expanded_name(retval, expr_start,
21081 expr_end, temp_result);
21082 vim_free(retval);
21083 retval = temp_result;
21084 }
21085 }
21086
21087 return retval;
21088}
21089
21090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021092 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 */
21094 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021095eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021097 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21098}
21099
21100/*
21101 * Return TRUE if character "c" can be used as the first character in a
21102 * variable or function name (excluding '{' and '}').
21103 */
21104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021105eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021106{
21107 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108}
21109
21110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 * Set number v: variable to "val".
21112 */
21113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021114set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021116 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117}
21118
21119/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021120 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 */
21122 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021123get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021125 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126}
21127
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021128/*
21129 * Get string v: variable value. Uses a static buffer, can only be used once.
21130 */
21131 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021132get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021133{
21134 return get_tv_string(&vimvars[idx].vv_tv);
21135}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021136
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021138 * Get List v: variable value. Caller must take care of reference count when
21139 * needed.
21140 */
21141 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021142get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021143{
21144 return vimvars[idx].vv_list;
21145}
21146
21147/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021148 * Set v:char to character "c".
21149 */
21150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021151set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021152{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021153 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021154
21155#ifdef FEAT_MBYTE
21156 if (has_mbyte)
21157 buf[(*mb_char2bytes)(c, buf)] = NUL;
21158 else
21159#endif
21160 {
21161 buf[0] = c;
21162 buf[1] = NUL;
21163 }
21164 set_vim_var_string(VV_CHAR, buf, -1);
21165}
21166
21167/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021168 * Set v:count to "count" and v:count1 to "count1".
21169 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170 */
21171 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021172set_vcount(
21173 long count,
21174 long count1,
21175 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021177 if (set_prevcount)
21178 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021179 vimvars[VV_COUNT].vv_nr = count;
21180 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181}
21182
21183/*
21184 * Set string v: variable to a copy of "val".
21185 */
21186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021187set_vim_var_string(
21188 int idx,
21189 char_u *val,
21190 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191{
Bram Moolenaara542c682016-01-31 16:28:04 +010021192 clear_tv(&vimvars[idx].vv_di.di_tv);
21193 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021195 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021197 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021199 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200}
21201
21202/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021203 * Set List v: variable to "val".
21204 */
21205 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021206set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021207{
Bram Moolenaara542c682016-01-31 16:28:04 +010021208 clear_tv(&vimvars[idx].vv_di.di_tv);
21209 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021210 vimvars[idx].vv_list = val;
21211 if (val != NULL)
21212 ++val->lv_refcount;
21213}
21214
21215/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021216 * Set Dictionary v: variable to "val".
21217 */
21218 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021219set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021220{
21221 int todo;
21222 hashitem_T *hi;
21223
Bram Moolenaara542c682016-01-31 16:28:04 +010021224 clear_tv(&vimvars[idx].vv_di.di_tv);
21225 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021226 vimvars[idx].vv_dict = val;
21227 if (val != NULL)
21228 {
21229 ++val->dv_refcount;
21230
21231 /* Set readonly */
21232 todo = (int)val->dv_hashtab.ht_used;
21233 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21234 {
21235 if (HASHITEM_EMPTY(hi))
21236 continue;
21237 --todo;
21238 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21239 }
21240 }
21241}
21242
21243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 * Set v:register if needed.
21245 */
21246 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021247set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248{
21249 char_u regname;
21250
21251 if (c == 0 || c == ' ')
21252 regname = '"';
21253 else
21254 regname = c;
21255 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021256 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257 set_vim_var_string(VV_REG, &regname, 1);
21258}
21259
21260/*
21261 * Get or set v:exception. If "oldval" == NULL, return the current value.
21262 * Otherwise, restore the value to "oldval" and return NULL.
21263 * Must always be called in pairs to save and restore v:exception! Does not
21264 * take care of memory allocations.
21265 */
21266 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021267v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268{
21269 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021270 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271
Bram Moolenaare9a41262005-01-15 22:18:47 +000021272 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 return NULL;
21274}
21275
21276/*
21277 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21278 * Otherwise, restore the value to "oldval" and return NULL.
21279 * Must always be called in pairs to save and restore v:throwpoint! Does not
21280 * take care of memory allocations.
21281 */
21282 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021283v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284{
21285 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021286 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287
Bram Moolenaare9a41262005-01-15 22:18:47 +000021288 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 return NULL;
21290}
21291
21292#if defined(FEAT_AUTOCMD) || defined(PROTO)
21293/*
21294 * Set v:cmdarg.
21295 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21296 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21297 * Must always be called in pairs!
21298 */
21299 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021300set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301{
21302 char_u *oldval;
21303 char_u *newval;
21304 unsigned len;
21305
Bram Moolenaare9a41262005-01-15 22:18:47 +000021306 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021307 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021309 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021310 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021311 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 }
21313
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021314 if (eap->force_bin == FORCE_BIN)
21315 len = 6;
21316 else if (eap->force_bin == FORCE_NOBIN)
21317 len = 8;
21318 else
21319 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021320
21321 if (eap->read_edit)
21322 len += 7;
21323
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021324 if (eap->force_ff != 0)
21325 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21326# ifdef FEAT_MBYTE
21327 if (eap->force_enc != 0)
21328 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021329 if (eap->bad_char != 0)
21330 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021331# endif
21332
21333 newval = alloc(len + 1);
21334 if (newval == NULL)
21335 return NULL;
21336
21337 if (eap->force_bin == FORCE_BIN)
21338 sprintf((char *)newval, " ++bin");
21339 else if (eap->force_bin == FORCE_NOBIN)
21340 sprintf((char *)newval, " ++nobin");
21341 else
21342 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021343
21344 if (eap->read_edit)
21345 STRCAT(newval, " ++edit");
21346
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021347 if (eap->force_ff != 0)
21348 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21349 eap->cmd + eap->force_ff);
21350# ifdef FEAT_MBYTE
21351 if (eap->force_enc != 0)
21352 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21353 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021354 if (eap->bad_char == BAD_KEEP)
21355 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21356 else if (eap->bad_char == BAD_DROP)
21357 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21358 else if (eap->bad_char != 0)
21359 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021360# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021361 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021362 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363}
21364#endif
21365
21366/*
21367 * Get the value of internal variable "name".
21368 * Return OK or FAIL.
21369 */
21370 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021371get_var_tv(
21372 char_u *name,
21373 int len, /* length of "name" */
21374 typval_T *rettv, /* NULL when only checking existence */
21375 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21376 int verbose, /* may give error message */
21377 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378{
21379 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021380 typval_T *tv = NULL;
21381 typval_T atv;
21382 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384
21385 /* truncate the name, so that we can use strcmp() */
21386 cc = name[len];
21387 name[len] = NUL;
21388
21389 /*
21390 * Check for "b:changedtick".
21391 */
21392 if (STRCMP(name, "b:changedtick") == 0)
21393 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021394 atv.v_type = VAR_NUMBER;
21395 atv.vval.v_number = curbuf->b_changedtick;
21396 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 }
21398
21399 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400 * Check for user-defined variables.
21401 */
21402 else
21403 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021404 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021406 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021407 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021408 if (dip != NULL)
21409 *dip = v;
21410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411 }
21412
Bram Moolenaare9a41262005-01-15 22:18:47 +000021413 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021415 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021416 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 ret = FAIL;
21418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021419 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021420 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421
21422 name[len] = cc;
21423
21424 return ret;
21425}
21426
21427/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021428 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21429 * Also handle function call with Funcref variable: func(expr)
21430 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21431 */
21432 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021433handle_subscript(
21434 char_u **arg,
21435 typval_T *rettv,
21436 int evaluate, /* do more than finding the end */
21437 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021438{
21439 int ret = OK;
21440 dict_T *selfdict = NULL;
21441 char_u *s;
21442 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021443 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021444
21445 while (ret == OK
21446 && (**arg == '['
21447 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021448 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021449 && !vim_iswhite(*(*arg - 1)))
21450 {
21451 if (**arg == '(')
21452 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021453 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021454 if (evaluate)
21455 {
21456 functv = *rettv;
21457 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021458
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021459 /* Invoke the function. Recursive! */
21460 s = functv.vval.v_string;
21461 }
21462 else
21463 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021464 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021465 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21466 &len, evaluate, selfdict);
21467
21468 /* Clear the funcref afterwards, so that deleting it while
21469 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021470 if (evaluate)
21471 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021472
21473 /* Stop the expression evaluation when immediately aborting on
21474 * error, or when an interrupt occurred or an exception was thrown
21475 * but not caught. */
21476 if (aborting())
21477 {
21478 if (ret == OK)
21479 clear_tv(rettv);
21480 ret = FAIL;
21481 }
21482 dict_unref(selfdict);
21483 selfdict = NULL;
21484 }
21485 else /* **arg == '[' || **arg == '.' */
21486 {
21487 dict_unref(selfdict);
21488 if (rettv->v_type == VAR_DICT)
21489 {
21490 selfdict = rettv->vval.v_dict;
21491 if (selfdict != NULL)
21492 ++selfdict->dv_refcount;
21493 }
21494 else
21495 selfdict = NULL;
21496 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21497 {
21498 clear_tv(rettv);
21499 ret = FAIL;
21500 }
21501 }
21502 }
21503 dict_unref(selfdict);
21504 return ret;
21505}
21506
21507/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021508 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021509 * value).
21510 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021511 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021512alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021513{
Bram Moolenaar33570922005-01-25 22:26:29 +000021514 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021515}
21516
21517/*
21518 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 * The string "s" must have been allocated, it is consumed.
21520 * Return NULL for out of memory, the variable otherwise.
21521 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021522 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021523alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524{
Bram Moolenaar33570922005-01-25 22:26:29 +000021525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021527 rettv = alloc_tv();
21528 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021530 rettv->v_type = VAR_STRING;
21531 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 }
21533 else
21534 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021535 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536}
21537
21538/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021539 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021541 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021542free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543{
21544 if (varp != NULL)
21545 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021546 switch (varp->v_type)
21547 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021548 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021549 func_unref(varp->vval.v_string);
21550 /*FALLTHROUGH*/
21551 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021552 vim_free(varp->vval.v_string);
21553 break;
21554 case VAR_LIST:
21555 list_unref(varp->vval.v_list);
21556 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021557 case VAR_DICT:
21558 dict_unref(varp->vval.v_dict);
21559 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021560 case VAR_JOB:
21561#ifdef FEAT_JOB
21562 job_unref(varp->vval.v_job);
21563 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021564#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021565 case VAR_CHANNEL:
21566#ifdef FEAT_CHANNEL
21567 channel_unref(varp->vval.v_channel);
21568 break;
21569#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021570 case VAR_NUMBER:
21571 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021572 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021573 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021574 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576 vim_free(varp);
21577 }
21578}
21579
21580/*
21581 * Free the memory for a variable value and set the value to NULL or 0.
21582 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021584clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585{
21586 if (varp != NULL)
21587 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021588 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021590 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021591 func_unref(varp->vval.v_string);
21592 /*FALLTHROUGH*/
21593 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021594 vim_free(varp->vval.v_string);
21595 varp->vval.v_string = NULL;
21596 break;
21597 case VAR_LIST:
21598 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021599 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021600 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021601 case VAR_DICT:
21602 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021603 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021604 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021605 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021606 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021607 varp->vval.v_number = 0;
21608 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021609 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021610#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021611 varp->vval.v_float = 0.0;
21612 break;
21613#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021614 case VAR_JOB:
21615#ifdef FEAT_JOB
21616 job_unref(varp->vval.v_job);
21617 varp->vval.v_job = NULL;
21618#endif
21619 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021620 case VAR_CHANNEL:
21621#ifdef FEAT_CHANNEL
21622 channel_unref(varp->vval.v_channel);
21623 varp->vval.v_channel = NULL;
21624#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021625 case VAR_UNKNOWN:
21626 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021628 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 }
21630}
21631
21632/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021633 * Set the value of a variable to NULL without freeing items.
21634 */
21635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021636init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021637{
21638 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021639 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021640}
21641
21642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 * Get the number value of a variable.
21644 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021645 * For incompatible types, return 0.
21646 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21647 * caller of incompatible types: it sets *denote to TRUE if "denote"
21648 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 */
21650 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021651get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021653 int error = FALSE;
21654
21655 return get_tv_number_chk(varp, &error); /* return 0L on error */
21656}
21657
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021658 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021659get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021660{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021661 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021663 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021665 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021666 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021667 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021668#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021669 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021670 break;
21671#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021672 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021673 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021674 break;
21675 case VAR_STRING:
21676 if (varp->vval.v_string != NULL)
21677 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021678 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021679 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021680 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021681 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021682 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021683 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021684 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021685 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021686 case VAR_SPECIAL:
21687 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21688 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021689 case VAR_JOB:
21690#ifdef FEAT_JOB
21691 EMSG(_("E910: Using a Job as a Number"));
21692 break;
21693#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021694 case VAR_CHANNEL:
21695#ifdef FEAT_CHANNEL
21696 EMSG(_("E913: Using a Channel as a Number"));
21697 break;
21698#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021699 case VAR_UNKNOWN:
21700 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021701 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021703 if (denote == NULL) /* useful for values that must be unsigned */
21704 n = -1;
21705 else
21706 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021707 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708}
21709
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021710#ifdef FEAT_FLOAT
21711 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021712get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021713{
21714 switch (varp->v_type)
21715 {
21716 case VAR_NUMBER:
21717 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021718 case VAR_FLOAT:
21719 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021720 case VAR_FUNC:
21721 EMSG(_("E891: Using a Funcref as a Float"));
21722 break;
21723 case VAR_STRING:
21724 EMSG(_("E892: Using a String as a Float"));
21725 break;
21726 case VAR_LIST:
21727 EMSG(_("E893: Using a List as a Float"));
21728 break;
21729 case VAR_DICT:
21730 EMSG(_("E894: Using a Dictionary as a Float"));
21731 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021732 case VAR_SPECIAL:
21733 EMSG(_("E907: Using a special value as a Float"));
21734 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021735 case VAR_JOB:
21736# ifdef FEAT_JOB
21737 EMSG(_("E911: Using a Job as a Float"));
21738 break;
21739# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021740 case VAR_CHANNEL:
21741# ifdef FEAT_CHANNEL
21742 EMSG(_("E914: Using a Channel as a Float"));
21743 break;
21744# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021745 case VAR_UNKNOWN:
21746 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021747 break;
21748 }
21749 return 0;
21750}
21751#endif
21752
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021754 * Get the lnum from the first argument.
21755 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021756 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 */
21758 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021759get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760{
Bram Moolenaar33570922005-01-25 22:26:29 +000021761 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762 linenr_T lnum;
21763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021764 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 if (lnum == 0) /* no valid number, try using line() */
21766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021767 rettv.v_type = VAR_NUMBER;
21768 f_line(argvars, &rettv);
21769 lnum = rettv.vval.v_number;
21770 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771 }
21772 return lnum;
21773}
21774
21775/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021776 * Get the lnum from the first argument.
21777 * Also accepts "$", then "buf" is used.
21778 * Returns 0 on error.
21779 */
21780 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021781get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021782{
21783 if (argvars[0].v_type == VAR_STRING
21784 && argvars[0].vval.v_string != NULL
21785 && argvars[0].vval.v_string[0] == '$'
21786 && buf != NULL)
21787 return buf->b_ml.ml_line_count;
21788 return get_tv_number_chk(&argvars[0], NULL);
21789}
21790
21791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792 * Get the string value of a variable.
21793 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021794 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21795 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796 * If the String variable has never been set, return an empty string.
21797 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021798 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21799 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 */
21801 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021802get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803{
21804 static char_u mybuf[NUMBUFLEN];
21805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021806 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807}
21808
21809 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021810get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021812 char_u *res = get_tv_string_buf_chk(varp, buf);
21813
21814 return res != NULL ? res : (char_u *)"";
21815}
21816
Bram Moolenaar7d647822014-04-05 21:28:56 +020021817/*
21818 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21819 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021820 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021821get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021822{
21823 static char_u mybuf[NUMBUFLEN];
21824
21825 return get_tv_string_buf_chk(varp, mybuf);
21826}
21827
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021828 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021829get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021830{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021831 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021833 case VAR_NUMBER:
21834 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21835 return buf;
21836 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021837 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021838 break;
21839 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021840 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021841 break;
21842 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021843 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021844 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021845 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021846#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021847 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021848 break;
21849#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021850 case VAR_STRING:
21851 if (varp->vval.v_string != NULL)
21852 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021853 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021854 case VAR_SPECIAL:
21855 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21856 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021857 case VAR_JOB:
21858#ifdef FEAT_JOB
21859 {
21860 job_T *job = varp->vval.v_job;
21861 char *status = job->jv_status == JOB_FAILED ? "fail"
21862 : job->jv_status == JOB_ENDED ? "dead"
21863 : "run";
21864# ifdef UNIX
21865 vim_snprintf((char *)buf, NUMBUFLEN,
21866 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021867# elif defined(WIN32)
21868 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021869 "process %ld %s",
21870 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021871 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021872# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021873 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021874 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21875# endif
21876 return buf;
21877 }
21878#endif
21879 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021880 case VAR_CHANNEL:
21881#ifdef FEAT_CHANNEL
21882 {
21883 channel_T *channel = varp->vval.v_channel;
21884 char *status = channel_status(channel);
21885
Bram Moolenaar5cefd402016-02-16 12:44:26 +010021886 if (channel == NULL)
21887 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
21888 else
21889 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010021890 "channel %d %s", channel->ch_id, status);
21891 return buf;
21892 }
21893#endif
21894 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021895 case VAR_UNKNOWN:
21896 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021897 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021899 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900}
21901
21902/*
21903 * Find variable "name" in the list of variables.
21904 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021905 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021906 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021907 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021909 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021910find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021911{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021913 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021914
Bram Moolenaara7043832005-01-21 11:56:39 +000021915 ht = find_var_ht(name, &varname);
21916 if (htp != NULL)
21917 *htp = ht;
21918 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021920 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921}
21922
21923/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021924 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021925 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021927 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021928find_var_in_ht(
21929 hashtab_T *ht,
21930 int htname,
21931 char_u *varname,
21932 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021933{
Bram Moolenaar33570922005-01-25 22:26:29 +000021934 hashitem_T *hi;
21935
21936 if (*varname == NUL)
21937 {
21938 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021939 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021940 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021941 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021942 case 'g': return &globvars_var;
21943 case 'v': return &vimvars_var;
21944 case 'b': return &curbuf->b_bufvar;
21945 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021946#ifdef FEAT_WINDOWS
21947 case 't': return &curtab->tp_winvar;
21948#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021949 case 'l': return current_funccal == NULL
21950 ? NULL : &current_funccal->l_vars_var;
21951 case 'a': return current_funccal == NULL
21952 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021953 }
21954 return NULL;
21955 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021956
21957 hi = hash_find(ht, varname);
21958 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021959 {
21960 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021961 * worked find the variable again. Don't auto-load a script if it was
21962 * loaded already, otherwise it would be loaded every time when
21963 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021964 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021965 {
21966 /* Note: script_autoload() may make "hi" invalid. It must either
21967 * be obtained again or not used. */
21968 if (!script_autoload(varname, FALSE) || aborting())
21969 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021970 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021971 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021972 if (HASHITEM_EMPTY(hi))
21973 return NULL;
21974 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021975 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021976}
21977
21978/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021979 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021980 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021981 * Set "varname" to the start of name without ':'.
21982 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021983 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021984find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021986 hashitem_T *hi;
21987
Bram Moolenaar73627d02015-08-11 15:46:09 +020021988 if (name[0] == NUL)
21989 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 if (name[1] != ':')
21991 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021992 /* The name must not start with a colon or #. */
21993 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 return NULL;
21995 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021996
21997 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021998 hi = hash_find(&compat_hashtab, name);
21999 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022000 return &compat_hashtab;
22001
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022003 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022004 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005 }
22006 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022007 if (*name == 'g') /* global variable */
22008 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022009 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22010 */
22011 if (vim_strchr(name + 2, ':') != NULL
22012 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022013 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022015 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022017 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022018#ifdef FEAT_WINDOWS
22019 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022020 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022021#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022022 if (*name == 'v') /* v: variable */
22023 return &vimvarht;
22024 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022025 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022026 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022027 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 if (*name == 's' /* script variable */
22029 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22030 return &SCRIPT_VARS(current_SID);
22031 return NULL;
22032}
22033
22034/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022035 * Get function call environment based on bactrace debug level
22036 */
22037 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022038get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022039{
22040 int i;
22041 funccall_T *funccal;
22042 funccall_T *temp_funccal;
22043
22044 funccal = current_funccal;
22045 if (debug_backtrace_level > 0)
22046 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022047 for (i = 0; i < debug_backtrace_level; i++)
22048 {
22049 temp_funccal = funccal->caller;
22050 if (temp_funccal)
22051 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022052 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022053 /* backtrace level overflow. reset to max */
22054 debug_backtrace_level = i;
22055 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022056 }
22057 return funccal;
22058}
22059
22060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022062 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 * Returns NULL when it doesn't exist.
22064 */
22065 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022066get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067{
Bram Moolenaar33570922005-01-25 22:26:29 +000022068 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022070 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 if (v == NULL)
22072 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022073 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074}
22075
22076/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022077 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078 * sourcing this script and when executing functions defined in the script.
22079 */
22080 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022081new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082{
Bram Moolenaara7043832005-01-21 11:56:39 +000022083 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022084 hashtab_T *ht;
22085 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022086
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22088 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022089 /* Re-allocating ga_data means that an ht_array pointing to
22090 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022091 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022092 for (i = 1; i <= ga_scripts.ga_len; ++i)
22093 {
22094 ht = &SCRIPT_VARS(i);
22095 if (ht->ht_mask == HT_INIT_SIZE - 1)
22096 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022097 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022098 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022099 }
22100
Bram Moolenaar071d4272004-06-13 20:20:40 +000022101 while (ga_scripts.ga_len < id)
22102 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022103 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022104 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022105 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 }
22108 }
22109}
22110
22111/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022112 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22113 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114 */
22115 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022116init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117{
Bram Moolenaar33570922005-01-25 22:26:29 +000022118 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022119 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022120 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022121 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022122 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022123 dict_var->di_tv.vval.v_dict = dict;
22124 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022125 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022126 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22127 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128}
22129
22130/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022131 * Unreference a dictionary initialized by init_var_dict().
22132 */
22133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022134unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022135{
22136 /* Now the dict needs to be freed if no one else is using it, go back to
22137 * normal reference counting. */
22138 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22139 dict_unref(dict);
22140}
22141
22142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022144 * Frees all allocated variables and the value they contain.
22145 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 */
22147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022148vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022149{
22150 vars_clear_ext(ht, TRUE);
22151}
22152
22153/*
22154 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22155 */
22156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022157vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158{
Bram Moolenaara7043832005-01-21 11:56:39 +000022159 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022160 hashitem_T *hi;
22161 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162
Bram Moolenaar33570922005-01-25 22:26:29 +000022163 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022164 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022165 for (hi = ht->ht_array; todo > 0; ++hi)
22166 {
22167 if (!HASHITEM_EMPTY(hi))
22168 {
22169 --todo;
22170
Bram Moolenaar33570922005-01-25 22:26:29 +000022171 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022172 * ht_array might change then. hash_clear() takes care of it
22173 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022174 v = HI2DI(hi);
22175 if (free_val)
22176 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022177 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022178 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022179 }
22180 }
22181 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022182 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022183}
22184
Bram Moolenaara7043832005-01-21 11:56:39 +000022185/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022186 * Delete a variable from hashtab "ht" at item "hi".
22187 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022190delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191{
Bram Moolenaar33570922005-01-25 22:26:29 +000022192 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022193
22194 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022195 clear_tv(&di->di_tv);
22196 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197}
22198
22199/*
22200 * List the value of one internal variable.
22201 */
22202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022203list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022205 char_u *tofree;
22206 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022207 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022208
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022209 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022210 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022211 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022212 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213}
22214
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022216list_one_var_a(
22217 char_u *prefix,
22218 char_u *name,
22219 int type,
22220 char_u *string,
22221 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222{
Bram Moolenaar31859182007-08-14 20:41:13 +000022223 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22224 msg_start();
22225 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 if (name != NULL) /* "a:" vars don't have a name stored */
22227 msg_puts(name);
22228 msg_putchar(' ');
22229 msg_advance(22);
22230 if (type == VAR_NUMBER)
22231 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022232 else if (type == VAR_FUNC)
22233 msg_putchar('*');
22234 else if (type == VAR_LIST)
22235 {
22236 msg_putchar('[');
22237 if (*string == '[')
22238 ++string;
22239 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022240 else if (type == VAR_DICT)
22241 {
22242 msg_putchar('{');
22243 if (*string == '{')
22244 ++string;
22245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 else
22247 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022248
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022250
22251 if (type == VAR_FUNC)
22252 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022253 if (*first)
22254 {
22255 msg_clr_eos();
22256 *first = FALSE;
22257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258}
22259
22260/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022261 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 * If the variable already exists, the value is updated.
22263 * Otherwise the variable is created.
22264 */
22265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022266set_var(
22267 char_u *name,
22268 typval_T *tv,
22269 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270{
Bram Moolenaar33570922005-01-25 22:26:29 +000022271 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022273 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022275 ht = find_var_ht(name, &varname);
22276 if (ht == NULL || *varname == NUL)
22277 {
22278 EMSG2(_(e_illvar), name);
22279 return;
22280 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022281 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022282
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022283 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22284 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022285
Bram Moolenaar33570922005-01-25 22:26:29 +000022286 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022288 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022289 if (var_check_ro(v->di_flags, name, FALSE)
22290 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022291 return;
22292 if (v->di_tv.v_type != tv->v_type
22293 && !((v->di_tv.v_type == VAR_STRING
22294 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022295 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022296 || tv->v_type == VAR_NUMBER))
22297#ifdef FEAT_FLOAT
22298 && !((v->di_tv.v_type == VAR_NUMBER
22299 || v->di_tv.v_type == VAR_FLOAT)
22300 && (tv->v_type == VAR_NUMBER
22301 || tv->v_type == VAR_FLOAT))
22302#endif
22303 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022304 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022305 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022306 return;
22307 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022308
22309 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022310 * Handle setting internal v: variables separately where needed to
22311 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022312 */
22313 if (ht == &vimvarht)
22314 {
22315 if (v->di_tv.v_type == VAR_STRING)
22316 {
22317 vim_free(v->di_tv.vval.v_string);
22318 if (copy || tv->v_type != VAR_STRING)
22319 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22320 else
22321 {
22322 /* Take over the string to avoid an extra alloc/free. */
22323 v->di_tv.vval.v_string = tv->vval.v_string;
22324 tv->vval.v_string = NULL;
22325 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022326 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022327 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022328 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022329 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022330 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022331 if (STRCMP(varname, "searchforward") == 0)
22332 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022333#ifdef FEAT_SEARCH_EXTRA
22334 else if (STRCMP(varname, "hlsearch") == 0)
22335 {
22336 no_hlsearch = !v->di_tv.vval.v_number;
22337 redraw_all_later(SOME_VALID);
22338 }
22339#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022340 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022341 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022342 else if (v->di_tv.v_type != tv->v_type)
22343 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022344 }
22345
22346 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347 }
22348 else /* add a new variable */
22349 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022350 /* Can't add "v:" variable. */
22351 if (ht == &vimvarht)
22352 {
22353 EMSG2(_(e_illvar), name);
22354 return;
22355 }
22356
Bram Moolenaar92124a32005-06-17 22:03:40 +000022357 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022358 if (!valid_varname(varname))
22359 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022360
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022361 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22362 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022363 if (v == NULL)
22364 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022365 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022366 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022368 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 return;
22370 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022371 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022373
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022374 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022375 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022376 else
22377 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022378 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022379 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022380 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382}
22383
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022384/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022385 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022386 * Also give an error message.
22387 */
22388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022389var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022390{
22391 if (flags & DI_FLAGS_RO)
22392 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022393 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022394 return TRUE;
22395 }
22396 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22397 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022398 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022399 return TRUE;
22400 }
22401 return FALSE;
22402}
22403
22404/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022405 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22406 * Also give an error message.
22407 */
22408 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022409var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022410{
22411 if (flags & DI_FLAGS_FIX)
22412 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022413 EMSG2(_("E795: Cannot delete variable %s"),
22414 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022415 return TRUE;
22416 }
22417 return FALSE;
22418}
22419
22420/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022421 * Check if a funcref is assigned to a valid variable name.
22422 * Return TRUE and give an error if not.
22423 */
22424 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022425var_check_func_name(
22426 char_u *name, /* points to start of variable name */
22427 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022428{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022429 /* Allow for w: b: s: and t:. */
22430 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022431 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22432 ? name[2] : name[0]))
22433 {
22434 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22435 name);
22436 return TRUE;
22437 }
22438 /* Don't allow hiding a function. When "v" is not NULL we might be
22439 * assigning another function to the same var, the type is checked
22440 * below. */
22441 if (new_var && function_exists(name))
22442 {
22443 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22444 name);
22445 return TRUE;
22446 }
22447 return FALSE;
22448}
22449
22450/*
22451 * Check if a variable name is valid.
22452 * Return FALSE and give an error if not.
22453 */
22454 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022455valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022456{
22457 char_u *p;
22458
22459 for (p = varname; *p != NUL; ++p)
22460 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22461 && *p != AUTOLOAD_CHAR)
22462 {
22463 EMSG2(_(e_illvar), varname);
22464 return FALSE;
22465 }
22466 return TRUE;
22467}
22468
22469/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022470 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022471 * Also give an error message, using "name" or _("name") when use_gettext is
22472 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022473 */
22474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022475tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022476{
22477 if (lock & VAR_LOCKED)
22478 {
22479 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022480 name == NULL ? (char_u *)_("Unknown")
22481 : use_gettext ? (char_u *)_(name)
22482 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022483 return TRUE;
22484 }
22485 if (lock & VAR_FIXED)
22486 {
22487 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022488 name == NULL ? (char_u *)_("Unknown")
22489 : use_gettext ? (char_u *)_(name)
22490 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022491 return TRUE;
22492 }
22493 return FALSE;
22494}
22495
22496/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022497 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022498 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022499 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022500 * It is OK for "from" and "to" to point to the same item. This is used to
22501 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022502 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022503 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022504copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022505{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022506 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022507 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022508 switch (from->v_type)
22509 {
22510 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022511 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022512 to->vval.v_number = from->vval.v_number;
22513 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022514 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022515#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022516 to->vval.v_float = from->vval.v_float;
22517 break;
22518#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022519 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022520#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022521 to->vval.v_job = from->vval.v_job;
22522 ++to->vval.v_job->jv_refcount;
22523 break;
22524#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022525 case VAR_CHANNEL:
22526#ifdef FEAT_CHANNEL
22527 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022528 if (to->vval.v_channel != NULL)
22529 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022530 break;
22531#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022532 case VAR_STRING:
22533 case VAR_FUNC:
22534 if (from->vval.v_string == NULL)
22535 to->vval.v_string = NULL;
22536 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022537 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022538 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022539 if (from->v_type == VAR_FUNC)
22540 func_ref(to->vval.v_string);
22541 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022542 break;
22543 case VAR_LIST:
22544 if (from->vval.v_list == NULL)
22545 to->vval.v_list = NULL;
22546 else
22547 {
22548 to->vval.v_list = from->vval.v_list;
22549 ++to->vval.v_list->lv_refcount;
22550 }
22551 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022552 case VAR_DICT:
22553 if (from->vval.v_dict == NULL)
22554 to->vval.v_dict = NULL;
22555 else
22556 {
22557 to->vval.v_dict = from->vval.v_dict;
22558 ++to->vval.v_dict->dv_refcount;
22559 }
22560 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022561 case VAR_UNKNOWN:
22562 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022563 break;
22564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565}
22566
22567/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022568 * Make a copy of an item.
22569 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022570 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22571 * reference to an already copied list/dict can be used.
22572 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022573 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022574 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022575item_copy(
22576 typval_T *from,
22577 typval_T *to,
22578 int deep,
22579 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022580{
22581 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022582 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022583
Bram Moolenaar33570922005-01-25 22:26:29 +000022584 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022585 {
22586 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022587 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022588 }
22589 ++recurse;
22590
22591 switch (from->v_type)
22592 {
22593 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022594 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022595 case VAR_STRING:
22596 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022597 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022598 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022599 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022600 copy_tv(from, to);
22601 break;
22602 case VAR_LIST:
22603 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022604 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022605 if (from->vval.v_list == NULL)
22606 to->vval.v_list = NULL;
22607 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22608 {
22609 /* use the copy made earlier */
22610 to->vval.v_list = from->vval.v_list->lv_copylist;
22611 ++to->vval.v_list->lv_refcount;
22612 }
22613 else
22614 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22615 if (to->vval.v_list == NULL)
22616 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022617 break;
22618 case VAR_DICT:
22619 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022620 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022621 if (from->vval.v_dict == NULL)
22622 to->vval.v_dict = NULL;
22623 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22624 {
22625 /* use the copy made earlier */
22626 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22627 ++to->vval.v_dict->dv_refcount;
22628 }
22629 else
22630 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22631 if (to->vval.v_dict == NULL)
22632 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022633 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022634 case VAR_UNKNOWN:
22635 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022636 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022637 }
22638 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022639 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022640}
22641
22642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 * ":echo expr1 ..." print each argument separated with a space, add a
22644 * newline at the end.
22645 * ":echon expr1 ..." print each argument plain.
22646 */
22647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022648ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649{
22650 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022651 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022652 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 char_u *p;
22654 int needclr = TRUE;
22655 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022656 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022657
22658 if (eap->skip)
22659 ++emsg_skip;
22660 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22661 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022662 /* If eval1() causes an error message the text from the command may
22663 * still need to be cleared. E.g., "echo 22,44". */
22664 need_clr_eos = needclr;
22665
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022667 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668 {
22669 /*
22670 * Report the invalid expression unless the expression evaluation
22671 * has been cancelled due to an aborting error, an interrupt, or an
22672 * exception.
22673 */
22674 if (!aborting())
22675 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022676 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677 break;
22678 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022679 need_clr_eos = FALSE;
22680
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 if (!eap->skip)
22682 {
22683 if (atstart)
22684 {
22685 atstart = FALSE;
22686 /* Call msg_start() after eval1(), evaluating the expression
22687 * may cause a message to appear. */
22688 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022689 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022690 /* Mark the saved text as finishing the line, so that what
22691 * follows is displayed on a new line when scrolling back
22692 * at the more prompt. */
22693 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696 }
22697 else if (eap->cmdidx == CMD_echo)
22698 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022699 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022700 if (p != NULL)
22701 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022703 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022705 if (*p != TAB && needclr)
22706 {
22707 /* remove any text still there from the command */
22708 msg_clr_eos();
22709 needclr = FALSE;
22710 }
22711 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 }
22713 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022714 {
22715#ifdef FEAT_MBYTE
22716 if (has_mbyte)
22717 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022718 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022719
22720 (void)msg_outtrans_len_attr(p, i, echo_attr);
22721 p += i - 1;
22722 }
22723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022725 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022728 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022730 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731 arg = skipwhite(arg);
22732 }
22733 eap->nextcmd = check_nextcmd(arg);
22734
22735 if (eap->skip)
22736 --emsg_skip;
22737 else
22738 {
22739 /* remove text that may still be there from the command */
22740 if (needclr)
22741 msg_clr_eos();
22742 if (eap->cmdidx == CMD_echo)
22743 msg_end();
22744 }
22745}
22746
22747/*
22748 * ":echohl {name}".
22749 */
22750 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022751ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752{
22753 int id;
22754
22755 id = syn_name2id(eap->arg);
22756 if (id == 0)
22757 echo_attr = 0;
22758 else
22759 echo_attr = syn_id2attr(id);
22760}
22761
22762/*
22763 * ":execute expr1 ..." execute the result of an expression.
22764 * ":echomsg expr1 ..." Print a message
22765 * ":echoerr expr1 ..." Print an error
22766 * Each gets spaces around each argument and a newline at the end for
22767 * echo commands
22768 */
22769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022770ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771{
22772 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022773 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774 int ret = OK;
22775 char_u *p;
22776 garray_T ga;
22777 int len;
22778 int save_did_emsg;
22779
22780 ga_init2(&ga, 1, 80);
22781
22782 if (eap->skip)
22783 ++emsg_skip;
22784 while (*arg != NUL && *arg != '|' && *arg != '\n')
22785 {
22786 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022787 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 {
22789 /*
22790 * Report the invalid expression unless the expression evaluation
22791 * has been cancelled due to an aborting error, an interrupt, or an
22792 * exception.
22793 */
22794 if (!aborting())
22795 EMSG2(_(e_invexpr2), p);
22796 ret = FAIL;
22797 break;
22798 }
22799
22800 if (!eap->skip)
22801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022802 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 len = (int)STRLEN(p);
22804 if (ga_grow(&ga, len + 2) == FAIL)
22805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022806 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807 ret = FAIL;
22808 break;
22809 }
22810 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813 ga.ga_len += len;
22814 }
22815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022816 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817 arg = skipwhite(arg);
22818 }
22819
22820 if (ret != FAIL && ga.ga_data != NULL)
22821 {
22822 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022823 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022825 out_flush();
22826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022827 else if (eap->cmdidx == CMD_echoerr)
22828 {
22829 /* We don't want to abort following commands, restore did_emsg. */
22830 save_did_emsg = did_emsg;
22831 EMSG((char_u *)ga.ga_data);
22832 if (!force_abort)
22833 did_emsg = save_did_emsg;
22834 }
22835 else if (eap->cmdidx == CMD_execute)
22836 do_cmdline((char_u *)ga.ga_data,
22837 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22838 }
22839
22840 ga_clear(&ga);
22841
22842 if (eap->skip)
22843 --emsg_skip;
22844
22845 eap->nextcmd = check_nextcmd(arg);
22846}
22847
22848/*
22849 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22850 * "arg" points to the "&" or '+' when called, to "option" when returning.
22851 * Returns NULL when no option name found. Otherwise pointer to the char
22852 * after the option name.
22853 */
22854 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022855find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856{
22857 char_u *p = *arg;
22858
22859 ++p;
22860 if (*p == 'g' && p[1] == ':')
22861 {
22862 *opt_flags = OPT_GLOBAL;
22863 p += 2;
22864 }
22865 else if (*p == 'l' && p[1] == ':')
22866 {
22867 *opt_flags = OPT_LOCAL;
22868 p += 2;
22869 }
22870 else
22871 *opt_flags = 0;
22872
22873 if (!ASCII_ISALPHA(*p))
22874 return NULL;
22875 *arg = p;
22876
22877 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22878 p += 4; /* termcap option */
22879 else
22880 while (ASCII_ISALPHA(*p))
22881 ++p;
22882 return p;
22883}
22884
22885/*
22886 * ":function"
22887 */
22888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022889ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890{
22891 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022892 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 int j;
22894 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022896 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 char_u *name = NULL;
22898 char_u *p;
22899 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022900 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 garray_T newargs;
22902 garray_T newlines;
22903 int varargs = FALSE;
22904 int mustend = FALSE;
22905 int flags = 0;
22906 ufunc_T *fp;
22907 int indent;
22908 int nesting;
22909 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022910 dictitem_T *v;
22911 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022912 static int func_nr = 0; /* number for nameless function */
22913 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022914 hashtab_T *ht;
22915 int todo;
22916 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022917 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918
22919 /*
22920 * ":function" without argument: list functions.
22921 */
22922 if (ends_excmd(*eap->arg))
22923 {
22924 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022925 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022926 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022927 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022928 {
22929 if (!HASHITEM_EMPTY(hi))
22930 {
22931 --todo;
22932 fp = HI2UF(hi);
22933 if (!isdigit(*fp->uf_name))
22934 list_func_head(fp, FALSE);
22935 }
22936 }
22937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 eap->nextcmd = check_nextcmd(eap->arg);
22939 return;
22940 }
22941
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022942 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022943 * ":function /pat": list functions matching pattern.
22944 */
22945 if (*eap->arg == '/')
22946 {
22947 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22948 if (!eap->skip)
22949 {
22950 regmatch_T regmatch;
22951
22952 c = *p;
22953 *p = NUL;
22954 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22955 *p = c;
22956 if (regmatch.regprog != NULL)
22957 {
22958 regmatch.rm_ic = p_ic;
22959
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022960 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022961 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22962 {
22963 if (!HASHITEM_EMPTY(hi))
22964 {
22965 --todo;
22966 fp = HI2UF(hi);
22967 if (!isdigit(*fp->uf_name)
22968 && vim_regexec(&regmatch, fp->uf_name, 0))
22969 list_func_head(fp, FALSE);
22970 }
22971 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022972 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022973 }
22974 }
22975 if (*p == '/')
22976 ++p;
22977 eap->nextcmd = check_nextcmd(p);
22978 return;
22979 }
22980
22981 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022982 * Get the function name. There are these situations:
22983 * func normal function name
22984 * "name" == func, "fudi.fd_dict" == NULL
22985 * dict.func new dictionary entry
22986 * "name" == NULL, "fudi.fd_dict" set,
22987 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22988 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022989 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022990 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22991 * dict.func existing dict entry that's not a Funcref
22992 * "name" == NULL, "fudi.fd_dict" set,
22993 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022994 * s:func script-local function name
22995 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022998 name = trans_function_name(&p, eap->skip, 0, &fudi);
22999 paren = (vim_strchr(p, '(') != NULL);
23000 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 {
23002 /*
23003 * Return on an invalid expression in braces, unless the expression
23004 * evaluation has been cancelled due to an aborting error, an
23005 * interrupt, or an exception.
23006 */
23007 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023008 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023009 if (!eap->skip && fudi.fd_newkey != NULL)
23010 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023011 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014 else
23015 eap->skip = TRUE;
23016 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023017
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018 /* An error in a function call during evaluation of an expression in magic
23019 * braces should not cause the function not to be defined. */
23020 saved_did_emsg = did_emsg;
23021 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022
23023 /*
23024 * ":function func" with only function name: list function.
23025 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023026 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027 {
23028 if (!ends_excmd(*skipwhite(p)))
23029 {
23030 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023031 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 }
23033 eap->nextcmd = check_nextcmd(p);
23034 if (eap->nextcmd != NULL)
23035 *p = NUL;
23036 if (!eap->skip && !got_int)
23037 {
23038 fp = find_func(name);
23039 if (fp != NULL)
23040 {
23041 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023042 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023044 if (FUNCLINE(fp, j) == NULL)
23045 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 msg_putchar('\n');
23047 msg_outnum((long)(j + 1));
23048 if (j < 9)
23049 msg_putchar(' ');
23050 if (j < 99)
23051 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023052 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023053 out_flush(); /* show a line at a time */
23054 ui_breakcheck();
23055 }
23056 if (!got_int)
23057 {
23058 msg_putchar('\n');
23059 msg_puts((char_u *)" endfunction");
23060 }
23061 }
23062 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023063 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023064 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023065 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066 }
23067
23068 /*
23069 * ":function name(arg1, arg2)" Define function.
23070 */
23071 p = skipwhite(p);
23072 if (*p != '(')
23073 {
23074 if (!eap->skip)
23075 {
23076 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023077 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 }
23079 /* attempt to continue by skipping some text */
23080 if (vim_strchr(p, '(') != NULL)
23081 p = vim_strchr(p, '(');
23082 }
23083 p = skipwhite(p + 1);
23084
23085 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23086 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23087
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023088 if (!eap->skip)
23089 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023090 /* Check the name of the function. Unless it's a dictionary function
23091 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023092 if (name != NULL)
23093 arg = name;
23094 else
23095 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023096 if (arg != NULL && (fudi.fd_di == NULL
23097 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023098 {
23099 if (*arg == K_SPECIAL)
23100 j = 3;
23101 else
23102 j = 0;
23103 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23104 : eval_isnamec(arg[j])))
23105 ++j;
23106 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023107 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023108 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023109 /* Disallow using the g: dict. */
23110 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23111 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023112 }
23113
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 /*
23115 * Isolate the arguments: "arg1, arg2, ...)"
23116 */
23117 while (*p != ')')
23118 {
23119 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23120 {
23121 varargs = TRUE;
23122 p += 3;
23123 mustend = TRUE;
23124 }
23125 else
23126 {
23127 arg = p;
23128 while (ASCII_ISALNUM(*p) || *p == '_')
23129 ++p;
23130 if (arg == p || isdigit(*arg)
23131 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23132 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23133 {
23134 if (!eap->skip)
23135 EMSG2(_("E125: Illegal argument: %s"), arg);
23136 break;
23137 }
23138 if (ga_grow(&newargs, 1) == FAIL)
23139 goto erret;
23140 c = *p;
23141 *p = NUL;
23142 arg = vim_strsave(arg);
23143 if (arg == NULL)
23144 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023145
23146 /* Check for duplicate argument name. */
23147 for (i = 0; i < newargs.ga_len; ++i)
23148 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23149 {
23150 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023151 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023152 goto erret;
23153 }
23154
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23156 *p = c;
23157 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 if (*p == ',')
23159 ++p;
23160 else
23161 mustend = TRUE;
23162 }
23163 p = skipwhite(p);
23164 if (mustend && *p != ')')
23165 {
23166 if (!eap->skip)
23167 EMSG2(_(e_invarg2), eap->arg);
23168 break;
23169 }
23170 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023171 if (*p != ')')
23172 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 ++p; /* skip the ')' */
23174
Bram Moolenaare9a41262005-01-15 22:18:47 +000023175 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176 for (;;)
23177 {
23178 p = skipwhite(p);
23179 if (STRNCMP(p, "range", 5) == 0)
23180 {
23181 flags |= FC_RANGE;
23182 p += 5;
23183 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023184 else if (STRNCMP(p, "dict", 4) == 0)
23185 {
23186 flags |= FC_DICT;
23187 p += 4;
23188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 else if (STRNCMP(p, "abort", 5) == 0)
23190 {
23191 flags |= FC_ABORT;
23192 p += 5;
23193 }
23194 else
23195 break;
23196 }
23197
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023198 /* When there is a line break use what follows for the function body.
23199 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23200 if (*p == '\n')
23201 line_arg = p + 1;
23202 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203 EMSG(_(e_trailing));
23204
23205 /*
23206 * Read the body of the function, until ":endfunction" is found.
23207 */
23208 if (KeyTyped)
23209 {
23210 /* Check if the function already exists, don't let the user type the
23211 * whole function before telling him it doesn't work! For a script we
23212 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023213 if (!eap->skip && !eap->forceit)
23214 {
23215 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23216 EMSG(_(e_funcdict));
23217 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023218 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023221 if (!eap->skip && did_emsg)
23222 goto erret;
23223
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224 msg_putchar('\n'); /* don't overwrite the function name */
23225 cmdline_row = msg_row;
23226 }
23227
23228 indent = 2;
23229 nesting = 0;
23230 for (;;)
23231 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023232 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023233 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023234 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023235 saved_wait_return = FALSE;
23236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023238 sourcing_lnum_off = sourcing_lnum;
23239
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023240 if (line_arg != NULL)
23241 {
23242 /* Use eap->arg, split up in parts by line breaks. */
23243 theline = line_arg;
23244 p = vim_strchr(theline, '\n');
23245 if (p == NULL)
23246 line_arg += STRLEN(line_arg);
23247 else
23248 {
23249 *p = NUL;
23250 line_arg = p + 1;
23251 }
23252 }
23253 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023254 theline = getcmdline(':', 0L, indent);
23255 else
23256 theline = eap->getline(':', eap->cookie, indent);
23257 if (KeyTyped)
23258 lines_left = Rows - 1;
23259 if (theline == NULL)
23260 {
23261 EMSG(_("E126: Missing :endfunction"));
23262 goto erret;
23263 }
23264
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023265 /* Detect line continuation: sourcing_lnum increased more than one. */
23266 if (sourcing_lnum > sourcing_lnum_off + 1)
23267 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23268 else
23269 sourcing_lnum_off = 0;
23270
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 if (skip_until != NULL)
23272 {
23273 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23274 * don't check for ":endfunc". */
23275 if (STRCMP(theline, skip_until) == 0)
23276 {
23277 vim_free(skip_until);
23278 skip_until = NULL;
23279 }
23280 }
23281 else
23282 {
23283 /* skip ':' and blanks*/
23284 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23285 ;
23286
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023287 /* Check for "endfunction". */
23288 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023290 if (line_arg == NULL)
23291 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 break;
23293 }
23294
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023295 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 * at "end". */
23297 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23298 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023299 else if (STRNCMP(p, "if", 2) == 0
23300 || STRNCMP(p, "wh", 2) == 0
23301 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 || STRNCMP(p, "try", 3) == 0)
23303 indent += 2;
23304
23305 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023306 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023308 if (*p == '!')
23309 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023311 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23312 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023313 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023314 ++nesting;
23315 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 }
23317 }
23318
23319 /* Check for ":append" or ":insert". */
23320 p = skip_range(p, NULL);
23321 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23322 || (p[0] == 'i'
23323 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23324 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23325 skip_until = vim_strsave((char_u *)".");
23326
23327 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23328 arg = skipwhite(skiptowhite(p));
23329 if (arg[0] == '<' && arg[1] =='<'
23330 && ((p[0] == 'p' && p[1] == 'y'
23331 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23332 || (p[0] == 'p' && p[1] == 'e'
23333 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23334 || (p[0] == 't' && p[1] == 'c'
23335 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023336 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23337 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023338 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23339 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023340 || (p[0] == 'm' && p[1] == 'z'
23341 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023342 ))
23343 {
23344 /* ":python <<" continues until a dot, like ":append" */
23345 p = skipwhite(arg + 2);
23346 if (*p == NUL)
23347 skip_until = vim_strsave((char_u *)".");
23348 else
23349 skip_until = vim_strsave(p);
23350 }
23351 }
23352
23353 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023354 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023355 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023356 if (line_arg == NULL)
23357 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023359 }
23360
23361 /* Copy the line to newly allocated memory. get_one_sourceline()
23362 * allocates 250 bytes per line, this saves 80% on average. The cost
23363 * is an extra alloc/free. */
23364 p = vim_strsave(theline);
23365 if (p != NULL)
23366 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023367 if (line_arg == NULL)
23368 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023369 theline = p;
23370 }
23371
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023372 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23373
23374 /* Add NULL lines for continuation lines, so that the line count is
23375 * equal to the index in the growarray. */
23376 while (sourcing_lnum_off-- > 0)
23377 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023378
23379 /* Check for end of eap->arg. */
23380 if (line_arg != NULL && *line_arg == NUL)
23381 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 }
23383
23384 /* Don't define the function when skipping commands or when an error was
23385 * detected. */
23386 if (eap->skip || did_emsg)
23387 goto erret;
23388
23389 /*
23390 * If there are no errors, add the function
23391 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023392 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023393 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023394 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023395 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023396 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023397 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023398 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023399 goto erret;
23400 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023401
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023402 fp = find_func(name);
23403 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023405 if (!eap->forceit)
23406 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023407 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023408 goto erret;
23409 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023410 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023411 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023412 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023413 name);
23414 goto erret;
23415 }
23416 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023417 ga_clear_strings(&(fp->uf_args));
23418 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023419 vim_free(name);
23420 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023422 }
23423 else
23424 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023425 char numbuf[20];
23426
23427 fp = NULL;
23428 if (fudi.fd_newkey == NULL && !eap->forceit)
23429 {
23430 EMSG(_(e_funcdict));
23431 goto erret;
23432 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023433 if (fudi.fd_di == NULL)
23434 {
23435 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023436 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023437 goto erret;
23438 }
23439 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023440 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023441 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023442
23443 /* Give the function a sequential number. Can only be used with a
23444 * Funcref! */
23445 vim_free(name);
23446 sprintf(numbuf, "%d", ++func_nr);
23447 name = vim_strsave((char_u *)numbuf);
23448 if (name == NULL)
23449 goto erret;
23450 }
23451
23452 if (fp == NULL)
23453 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023454 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023455 {
23456 int slen, plen;
23457 char_u *scriptname;
23458
23459 /* Check that the autoload name matches the script name. */
23460 j = FAIL;
23461 if (sourcing_name != NULL)
23462 {
23463 scriptname = autoload_name(name);
23464 if (scriptname != NULL)
23465 {
23466 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023467 plen = (int)STRLEN(p);
23468 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023469 if (slen > plen && fnamecmp(p,
23470 sourcing_name + slen - plen) == 0)
23471 j = OK;
23472 vim_free(scriptname);
23473 }
23474 }
23475 if (j == FAIL)
23476 {
23477 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23478 goto erret;
23479 }
23480 }
23481
23482 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 if (fp == NULL)
23484 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023485
23486 if (fudi.fd_dict != NULL)
23487 {
23488 if (fudi.fd_di == NULL)
23489 {
23490 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023491 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023492 if (fudi.fd_di == NULL)
23493 {
23494 vim_free(fp);
23495 goto erret;
23496 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023497 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23498 {
23499 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023500 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023501 goto erret;
23502 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023503 }
23504 else
23505 /* overwrite existing dict entry */
23506 clear_tv(&fudi.fd_di->di_tv);
23507 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023508 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023509 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023510 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023511
23512 /* behave like "dict" was used */
23513 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023514 }
23515
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023517 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023518 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23519 {
23520 vim_free(fp);
23521 goto erret;
23522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023524 fp->uf_args = newargs;
23525 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023526#ifdef FEAT_PROFILE
23527 fp->uf_tml_count = NULL;
23528 fp->uf_tml_total = NULL;
23529 fp->uf_tml_self = NULL;
23530 fp->uf_profiling = FALSE;
23531 if (prof_def_func())
23532 func_do_profile(fp);
23533#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023534 fp->uf_varargs = varargs;
23535 fp->uf_flags = flags;
23536 fp->uf_calls = 0;
23537 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023538 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539
23540erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 ga_clear_strings(&newargs);
23542 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023543ret_free:
23544 vim_free(skip_until);
23545 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023548 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549}
23550
23551/*
23552 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023553 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023555 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023556 * TFN_INT: internal function name OK
23557 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023558 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 * Advances "pp" to just after the function name (if no error).
23560 */
23561 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023562trans_function_name(
23563 char_u **pp,
23564 int skip, /* only find the end, don't evaluate */
23565 int flags,
23566 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023568 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569 char_u *start;
23570 char_u *end;
23571 int lead;
23572 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023574 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023575
23576 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023577 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023579
23580 /* Check for hard coded <SNR>: already translated function ID (from a user
23581 * command). */
23582 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23583 && (*pp)[2] == (int)KE_SNR)
23584 {
23585 *pp += 3;
23586 len = get_id_len(pp) + 3;
23587 return vim_strnsave(start, len);
23588 }
23589
23590 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23591 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023593 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023595
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023596 /* Note that TFN_ flags use the same values as GLV_ flags. */
23597 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023598 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023599 if (end == start)
23600 {
23601 if (!skip)
23602 EMSG(_("E129: Function name required"));
23603 goto theend;
23604 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023605 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023606 {
23607 /*
23608 * Report an invalid expression in braces, unless the expression
23609 * evaluation has been cancelled due to an aborting error, an
23610 * interrupt, or an exception.
23611 */
23612 if (!aborting())
23613 {
23614 if (end != NULL)
23615 EMSG2(_(e_invarg2), start);
23616 }
23617 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023618 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023619 goto theend;
23620 }
23621
23622 if (lv.ll_tv != NULL)
23623 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023624 if (fdp != NULL)
23625 {
23626 fdp->fd_dict = lv.ll_dict;
23627 fdp->fd_newkey = lv.ll_newkey;
23628 lv.ll_newkey = NULL;
23629 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023630 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023631 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23632 {
23633 name = vim_strsave(lv.ll_tv->vval.v_string);
23634 *pp = end;
23635 }
23636 else
23637 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023638 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23639 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023640 EMSG(_(e_funcref));
23641 else
23642 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023643 name = NULL;
23644 }
23645 goto theend;
23646 }
23647
23648 if (lv.ll_name == NULL)
23649 {
23650 /* Error found, but continue after the function name. */
23651 *pp = end;
23652 goto theend;
23653 }
23654
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023655 /* Check if the name is a Funcref. If so, use the value. */
23656 if (lv.ll_exp_name != NULL)
23657 {
23658 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023659 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023660 if (name == lv.ll_exp_name)
23661 name = NULL;
23662 }
23663 else
23664 {
23665 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023666 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023667 if (name == *pp)
23668 name = NULL;
23669 }
23670 if (name != NULL)
23671 {
23672 name = vim_strsave(name);
23673 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023674 if (STRNCMP(name, "<SNR>", 5) == 0)
23675 {
23676 /* Change "<SNR>" to the byte sequence. */
23677 name[0] = K_SPECIAL;
23678 name[1] = KS_EXTRA;
23679 name[2] = (int)KE_SNR;
23680 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23681 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023682 goto theend;
23683 }
23684
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023685 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023686 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023687 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023688 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23689 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23690 {
23691 /* When there was "s:" already or the name expanded to get a
23692 * leading "s:" then remove it. */
23693 lv.ll_name += 2;
23694 len -= 2;
23695 lead = 2;
23696 }
23697 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023698 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023699 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023700 /* skip over "s:" and "g:" */
23701 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023702 lv.ll_name += 2;
23703 len = (int)(end - lv.ll_name);
23704 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023705
23706 /*
23707 * Copy the function name to allocated memory.
23708 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23709 * Accept <SNR>123_name() outside a script.
23710 */
23711 if (skip)
23712 lead = 0; /* do nothing */
23713 else if (lead > 0)
23714 {
23715 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023716 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23717 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023718 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023719 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023720 if (current_SID <= 0)
23721 {
23722 EMSG(_(e_usingsid));
23723 goto theend;
23724 }
23725 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23726 lead += (int)STRLEN(sid_buf);
23727 }
23728 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023729 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023730 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023731 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023732 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023733 goto theend;
23734 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023735 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023736 {
23737 char_u *cp = vim_strchr(lv.ll_name, ':');
23738
23739 if (cp != NULL && cp < end)
23740 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023741 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023742 goto theend;
23743 }
23744 }
23745
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023746 name = alloc((unsigned)(len + lead + 1));
23747 if (name != NULL)
23748 {
23749 if (lead > 0)
23750 {
23751 name[0] = K_SPECIAL;
23752 name[1] = KS_EXTRA;
23753 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023754 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023755 STRCPY(name + 3, sid_buf);
23756 }
23757 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023758 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023759 }
23760 *pp = end;
23761
23762theend:
23763 clear_lval(&lv);
23764 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765}
23766
23767/*
23768 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23769 * Return 2 if "p" starts with "s:".
23770 * Return 0 otherwise.
23771 */
23772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023773eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023774{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023775 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23776 * the standard library function. */
23777 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23778 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023779 return 5;
23780 if (p[0] == 's' && p[1] == ':')
23781 return 2;
23782 return 0;
23783}
23784
23785/*
23786 * Return TRUE if "p" starts with "<SID>" or "s:".
23787 * Only works if eval_fname_script() returned non-zero for "p"!
23788 */
23789 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023790eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023791{
23792 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23793}
23794
23795/*
23796 * List the head of the function: "name(arg1, arg2)".
23797 */
23798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023799list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800{
23801 int j;
23802
23803 msg_start();
23804 if (indent)
23805 MSG_PUTS(" ");
23806 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023807 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023808 {
23809 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023810 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023811 }
23812 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023813 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023815 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816 {
23817 if (j)
23818 MSG_PUTS(", ");
23819 msg_puts(FUNCARG(fp, j));
23820 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023821 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822 {
23823 if (j)
23824 MSG_PUTS(", ");
23825 MSG_PUTS("...");
23826 }
23827 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023828 if (fp->uf_flags & FC_ABORT)
23829 MSG_PUTS(" abort");
23830 if (fp->uf_flags & FC_RANGE)
23831 MSG_PUTS(" range");
23832 if (fp->uf_flags & FC_DICT)
23833 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023834 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023835 if (p_verbose > 0)
23836 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837}
23838
23839/*
23840 * Find a function by name, return pointer to it in ufuncs.
23841 * Return NULL for unknown function.
23842 */
23843 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023844find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023846 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023848 hi = hash_find(&func_hashtab, name);
23849 if (!HASHITEM_EMPTY(hi))
23850 return HI2UF(hi);
23851 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852}
23853
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023854#if defined(EXITFREE) || defined(PROTO)
23855 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023856free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023857{
23858 hashitem_T *hi;
23859
23860 /* Need to start all over every time, because func_free() may change the
23861 * hash table. */
23862 while (func_hashtab.ht_used > 0)
23863 for (hi = func_hashtab.ht_array; ; ++hi)
23864 if (!HASHITEM_EMPTY(hi))
23865 {
23866 func_free(HI2UF(hi));
23867 break;
23868 }
23869}
23870#endif
23871
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023872 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023873translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023874{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023875 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023876 return find_internal_func(name) >= 0;
23877 return find_func(name) != NULL;
23878}
23879
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023880/*
23881 * Return TRUE if a function "name" exists.
23882 */
23883 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023884function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023885{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023886 char_u *nm = name;
23887 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023888 int n = FALSE;
23889
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023890 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23891 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023892 nm = skipwhite(nm);
23893
23894 /* Only accept "funcname", "funcname ", "funcname (..." and
23895 * "funcname(...", not "funcname!...". */
23896 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023897 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023898 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023899 return n;
23900}
23901
Bram Moolenaara1544c02013-05-30 12:35:52 +020023902 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023903get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023904{
23905 char_u *nm = name;
23906 char_u *p;
23907
23908 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23909
23910 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023911 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023912 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023913
Bram Moolenaara1544c02013-05-30 12:35:52 +020023914 vim_free(p);
23915 return NULL;
23916}
23917
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023918/*
23919 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023920 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23921 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023922 */
23923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023924builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023925{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023926 char_u *p;
23927
23928 if (!ASCII_ISLOWER(name[0]))
23929 return FALSE;
23930 p = vim_strchr(name, AUTOLOAD_CHAR);
23931 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023932}
23933
Bram Moolenaar05159a02005-02-26 23:04:13 +000023934#if defined(FEAT_PROFILE) || defined(PROTO)
23935/*
23936 * Start profiling function "fp".
23937 */
23938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023939func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023940{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023941 int len = fp->uf_lines.ga_len;
23942
23943 if (len == 0)
23944 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023945 fp->uf_tm_count = 0;
23946 profile_zero(&fp->uf_tm_self);
23947 profile_zero(&fp->uf_tm_total);
23948 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023949 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023950 if (fp->uf_tml_total == NULL)
23951 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023952 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023953 if (fp->uf_tml_self == NULL)
23954 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023955 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023956 fp->uf_tml_idx = -1;
23957 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23958 || fp->uf_tml_self == NULL)
23959 return; /* out of memory */
23960
23961 fp->uf_profiling = TRUE;
23962}
23963
23964/*
23965 * Dump the profiling results for all functions in file "fd".
23966 */
23967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023968func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023969{
23970 hashitem_T *hi;
23971 int todo;
23972 ufunc_T *fp;
23973 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023974 ufunc_T **sorttab;
23975 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023976
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023977 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023978 if (todo == 0)
23979 return; /* nothing to dump */
23980
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023981 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023982
Bram Moolenaar05159a02005-02-26 23:04:13 +000023983 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23984 {
23985 if (!HASHITEM_EMPTY(hi))
23986 {
23987 --todo;
23988 fp = HI2UF(hi);
23989 if (fp->uf_profiling)
23990 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023991 if (sorttab != NULL)
23992 sorttab[st_len++] = fp;
23993
Bram Moolenaar05159a02005-02-26 23:04:13 +000023994 if (fp->uf_name[0] == K_SPECIAL)
23995 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23996 else
23997 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23998 if (fp->uf_tm_count == 1)
23999 fprintf(fd, "Called 1 time\n");
24000 else
24001 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24002 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24003 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24004 fprintf(fd, "\n");
24005 fprintf(fd, "count total (s) self (s)\n");
24006
24007 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24008 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024009 if (FUNCLINE(fp, i) == NULL)
24010 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024011 prof_func_line(fd, fp->uf_tml_count[i],
24012 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024013 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24014 }
24015 fprintf(fd, "\n");
24016 }
24017 }
24018 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024019
24020 if (sorttab != NULL && st_len > 0)
24021 {
24022 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24023 prof_total_cmp);
24024 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24025 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24026 prof_self_cmp);
24027 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24028 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024029
24030 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024031}
Bram Moolenaar73830342005-02-28 22:48:19 +000024032
24033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024034prof_sort_list(
24035 FILE *fd,
24036 ufunc_T **sorttab,
24037 int st_len,
24038 char *title,
24039 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024040{
24041 int i;
24042 ufunc_T *fp;
24043
24044 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24045 fprintf(fd, "count total (s) self (s) function\n");
24046 for (i = 0; i < 20 && i < st_len; ++i)
24047 {
24048 fp = sorttab[i];
24049 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24050 prefer_self);
24051 if (fp->uf_name[0] == K_SPECIAL)
24052 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24053 else
24054 fprintf(fd, " %s()\n", fp->uf_name);
24055 }
24056 fprintf(fd, "\n");
24057}
24058
24059/*
24060 * Print the count and times for one function or function line.
24061 */
24062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024063prof_func_line(
24064 FILE *fd,
24065 int count,
24066 proftime_T *total,
24067 proftime_T *self,
24068 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024069{
24070 if (count > 0)
24071 {
24072 fprintf(fd, "%5d ", count);
24073 if (prefer_self && profile_equal(total, self))
24074 fprintf(fd, " ");
24075 else
24076 fprintf(fd, "%s ", profile_msg(total));
24077 if (!prefer_self && profile_equal(total, self))
24078 fprintf(fd, " ");
24079 else
24080 fprintf(fd, "%s ", profile_msg(self));
24081 }
24082 else
24083 fprintf(fd, " ");
24084}
24085
24086/*
24087 * Compare function for total time sorting.
24088 */
24089 static int
24090#ifdef __BORLANDC__
24091_RTLENTRYF
24092#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024093prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024094{
24095 ufunc_T *p1, *p2;
24096
24097 p1 = *(ufunc_T **)s1;
24098 p2 = *(ufunc_T **)s2;
24099 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24100}
24101
24102/*
24103 * Compare function for self time sorting.
24104 */
24105 static int
24106#ifdef __BORLANDC__
24107_RTLENTRYF
24108#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024109prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024110{
24111 ufunc_T *p1, *p2;
24112
24113 p1 = *(ufunc_T **)s1;
24114 p2 = *(ufunc_T **)s2;
24115 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24116}
24117
Bram Moolenaar05159a02005-02-26 23:04:13 +000024118#endif
24119
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024120/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024121 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024122 * Return TRUE if a package was loaded.
24123 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024124 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024125script_autoload(
24126 char_u *name,
24127 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024128{
24129 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024130 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024131 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024132 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024133
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024134 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024135 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024136 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024137 return FALSE;
24138
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024139 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024140
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024141 /* Find the name in the list of previously loaded package names. Skip
24142 * "autoload/", it's always the same. */
24143 for (i = 0; i < ga_loaded.ga_len; ++i)
24144 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24145 break;
24146 if (!reload && i < ga_loaded.ga_len)
24147 ret = FALSE; /* was loaded already */
24148 else
24149 {
24150 /* Remember the name if it wasn't loaded already. */
24151 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24152 {
24153 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24154 tofree = NULL;
24155 }
24156
24157 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024158 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024159 ret = TRUE;
24160 }
24161
24162 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024163 return ret;
24164}
24165
24166/*
24167 * Return the autoload script name for a function or variable name.
24168 * Returns NULL when out of memory.
24169 */
24170 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024171autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024172{
24173 char_u *p;
24174 char_u *scriptname;
24175
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024176 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024177 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24178 if (scriptname == NULL)
24179 return FALSE;
24180 STRCPY(scriptname, "autoload/");
24181 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024182 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024183 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024184 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024185 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024186 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024187}
24188
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24190
24191/*
24192 * Function given to ExpandGeneric() to obtain the list of user defined
24193 * function names.
24194 */
24195 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024196get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024197{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024198 static long_u done;
24199 static hashitem_T *hi;
24200 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024201
24202 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024204 done = 0;
24205 hi = func_hashtab.ht_array;
24206 }
24207 if (done < func_hashtab.ht_used)
24208 {
24209 if (done++ > 0)
24210 ++hi;
24211 while (HASHITEM_EMPTY(hi))
24212 ++hi;
24213 fp = HI2UF(hi);
24214
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024215 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024216 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024218 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24219 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220
24221 cat_func_name(IObuff, fp);
24222 if (xp->xp_context != EXPAND_USER_FUNC)
24223 {
24224 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024225 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024226 STRCAT(IObuff, ")");
24227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 return IObuff;
24229 }
24230 return NULL;
24231}
24232
24233#endif /* FEAT_CMDL_COMPL */
24234
24235/*
24236 * Copy the function name of "fp" to buffer "buf".
24237 * "buf" must be able to hold the function name plus three bytes.
24238 * Takes care of script-local function names.
24239 */
24240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024241cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024243 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244 {
24245 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024246 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247 }
24248 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024249 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024250}
24251
24252/*
24253 * ":delfunction {name}"
24254 */
24255 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024256ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024257{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024258 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259 char_u *p;
24260 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024261 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262
24263 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024264 name = trans_function_name(&p, eap->skip, 0, &fudi);
24265 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024266 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024267 {
24268 if (fudi.fd_dict != NULL && !eap->skip)
24269 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024272 if (!ends_excmd(*skipwhite(p)))
24273 {
24274 vim_free(name);
24275 EMSG(_(e_trailing));
24276 return;
24277 }
24278 eap->nextcmd = check_nextcmd(p);
24279 if (eap->nextcmd != NULL)
24280 *p = NUL;
24281
24282 if (!eap->skip)
24283 fp = find_func(name);
24284 vim_free(name);
24285
24286 if (!eap->skip)
24287 {
24288 if (fp == NULL)
24289 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024290 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291 return;
24292 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024293 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294 {
24295 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24296 return;
24297 }
24298
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024299 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024301 /* Delete the dict item that refers to the function, it will
24302 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024303 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024305 else
24306 func_free(fp);
24307 }
24308}
24309
24310/*
24311 * Free a function and remove it from the list of functions.
24312 */
24313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024314func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024315{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024316 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024317
24318 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024319 ga_clear_strings(&(fp->uf_args));
24320 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024321#ifdef FEAT_PROFILE
24322 vim_free(fp->uf_tml_count);
24323 vim_free(fp->uf_tml_total);
24324 vim_free(fp->uf_tml_self);
24325#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024326
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024327 /* remove the function from the function hashtable */
24328 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24329 if (HASHITEM_EMPTY(hi))
24330 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024331 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024332 hash_remove(&func_hashtab, hi);
24333
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024334 vim_free(fp);
24335}
24336
24337/*
24338 * Unreference a Function: decrement the reference count and free it when it
24339 * becomes zero. Only for numbered functions.
24340 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024342func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024343{
24344 ufunc_T *fp;
24345
24346 if (name != NULL && isdigit(*name))
24347 {
24348 fp = find_func(name);
24349 if (fp == NULL)
24350 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024351 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024352 {
24353 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024354 * when "uf_calls" becomes zero. */
24355 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024356 func_free(fp);
24357 }
24358 }
24359}
24360
24361/*
24362 * Count a reference to a Function.
24363 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024365func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024366{
24367 ufunc_T *fp;
24368
24369 if (name != NULL && isdigit(*name))
24370 {
24371 fp = find_func(name);
24372 if (fp == NULL)
24373 EMSG2(_(e_intern2), "func_ref()");
24374 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024375 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024376 }
24377}
24378
24379/*
24380 * Call a user function.
24381 */
24382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024383call_user_func(
24384 ufunc_T *fp, /* pointer to function */
24385 int argcount, /* nr of args */
24386 typval_T *argvars, /* arguments */
24387 typval_T *rettv, /* return value */
24388 linenr_T firstline, /* first line of range */
24389 linenr_T lastline, /* last line of range */
24390 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024391{
Bram Moolenaar33570922005-01-25 22:26:29 +000024392 char_u *save_sourcing_name;
24393 linenr_T save_sourcing_lnum;
24394 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024395 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024396 int save_did_emsg;
24397 static int depth = 0;
24398 dictitem_T *v;
24399 int fixvar_idx = 0; /* index in fixvar[] */
24400 int i;
24401 int ai;
24402 char_u numbuf[NUMBUFLEN];
24403 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024404 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024405#ifdef FEAT_PROFILE
24406 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024407 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409
24410 /* If depth of calling is getting too high, don't execute the function */
24411 if (depth >= p_mfd)
24412 {
24413 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024414 rettv->v_type = VAR_NUMBER;
24415 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024416 return;
24417 }
24418 ++depth;
24419
24420 line_breakcheck(); /* check for CTRL-C hit */
24421
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024422 fc = (funccall_T *)alloc(sizeof(funccall_T));
24423 fc->caller = current_funccal;
24424 current_funccal = fc;
24425 fc->func = fp;
24426 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024427 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024428 fc->linenr = 0;
24429 fc->returned = FALSE;
24430 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024431 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024432 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24433 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024434
Bram Moolenaar33570922005-01-25 22:26:29 +000024435 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024436 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024437 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24438 * each argument variable and saves a lot of time.
24439 */
24440 /*
24441 * Init l: variables.
24442 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024443 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024444 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024445 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024446 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24447 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024448 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024449 name = v->di_key;
24450 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024451 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024452 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024453 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024454 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024455 v->di_tv.vval.v_dict = selfdict;
24456 ++selfdict->dv_refcount;
24457 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024458
Bram Moolenaar33570922005-01-25 22:26:29 +000024459 /*
24460 * Init a: variables.
24461 * Set a:0 to "argcount".
24462 * Set a:000 to a list with room for the "..." arguments.
24463 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024464 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024465 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024466 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024467 /* Use "name" to avoid a warning from some compiler that checks the
24468 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024469 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024470 name = v->di_key;
24471 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024472 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024473 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024474 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024475 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024476 v->di_tv.vval.v_list = &fc->l_varlist;
24477 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24478 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24479 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024480
24481 /*
24482 * Set a:firstline to "firstline" and a:lastline to "lastline".
24483 * Set a:name to named arguments.
24484 * Set a:N to the "..." arguments.
24485 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024486 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024487 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024488 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024489 (varnumber_T)lastline);
24490 for (i = 0; i < argcount; ++i)
24491 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024492 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024493 if (ai < 0)
24494 /* named argument a:name */
24495 name = FUNCARG(fp, i);
24496 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024497 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024498 /* "..." argument a:1, a:2, etc. */
24499 sprintf((char *)numbuf, "%d", ai + 1);
24500 name = numbuf;
24501 }
24502 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24503 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024504 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024505 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24506 }
24507 else
24508 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024509 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24510 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024511 if (v == NULL)
24512 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024513 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024514 }
24515 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024516 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024517
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024518 /* Note: the values are copied directly to avoid alloc/free.
24519 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024520 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024521 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024522
24523 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24524 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024525 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24526 fc->l_listitems[ai].li_tv = argvars[i];
24527 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024528 }
24529 }
24530
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531 /* Don't redraw while executing the function. */
24532 ++RedrawingDisabled;
24533 save_sourcing_name = sourcing_name;
24534 save_sourcing_lnum = sourcing_lnum;
24535 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024536 /* need space for function name + ("function " + 3) or "[number]" */
24537 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24538 + STRLEN(fp->uf_name) + 20;
24539 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540 if (sourcing_name != NULL)
24541 {
24542 if (save_sourcing_name != NULL
24543 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024544 sprintf((char *)sourcing_name, "%s[%d]..",
24545 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546 else
24547 STRCPY(sourcing_name, "function ");
24548 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24549
24550 if (p_verbose >= 12)
24551 {
24552 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024553 verbose_enter_scroll();
24554
Bram Moolenaar555b2802005-05-19 21:08:39 +000024555 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024556 if (p_verbose >= 14)
24557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024559 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024560 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024561 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024562
24563 msg_puts((char_u *)"(");
24564 for (i = 0; i < argcount; ++i)
24565 {
24566 if (i > 0)
24567 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024568 if (argvars[i].v_type == VAR_NUMBER)
24569 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 else
24571 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024572 /* Do not want errors such as E724 here. */
24573 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024574 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024575 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024576 if (s != NULL)
24577 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024578 if (vim_strsize(s) > MSG_BUF_CLEN)
24579 {
24580 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24581 s = buf;
24582 }
24583 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024584 vim_free(tofree);
24585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024586 }
24587 }
24588 msg_puts((char_u *)")");
24589 }
24590 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024591
24592 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024593 --no_wait_return;
24594 }
24595 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024596#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024597 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024598 {
24599 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24600 func_do_profile(fp);
24601 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024602 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024603 {
24604 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024605 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024606 profile_zero(&fp->uf_tm_children);
24607 }
24608 script_prof_save(&wait_start);
24609 }
24610#endif
24611
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024613 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024614 save_did_emsg = did_emsg;
24615 did_emsg = FALSE;
24616
24617 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024618 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024619 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24620
24621 --RedrawingDisabled;
24622
24623 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024624 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024626 clear_tv(rettv);
24627 rettv->v_type = VAR_NUMBER;
24628 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024629 }
24630
Bram Moolenaar05159a02005-02-26 23:04:13 +000024631#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024632 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024633 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024634 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024635 profile_end(&call_start);
24636 profile_sub_wait(&wait_start, &call_start);
24637 profile_add(&fp->uf_tm_total, &call_start);
24638 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024639 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024640 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024641 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24642 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024643 }
24644 }
24645#endif
24646
Bram Moolenaar071d4272004-06-13 20:20:40 +000024647 /* when being verbose, mention the return value */
24648 if (p_verbose >= 12)
24649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024651 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024654 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024655 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024656 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024657 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024658 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024660 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024661 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024662 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024663 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024664
Bram Moolenaar555b2802005-05-19 21:08:39 +000024665 /* The value may be very long. Skip the middle part, so that we
24666 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024667 * truncate it at the end. Don't want errors such as E724 here. */
24668 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024669 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024670 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024671 if (s != NULL)
24672 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024673 if (vim_strsize(s) > MSG_BUF_CLEN)
24674 {
24675 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24676 s = buf;
24677 }
24678 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024679 vim_free(tofree);
24680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024681 }
24682 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024683
24684 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685 --no_wait_return;
24686 }
24687
24688 vim_free(sourcing_name);
24689 sourcing_name = save_sourcing_name;
24690 sourcing_lnum = save_sourcing_lnum;
24691 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024692#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024693 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024694 script_prof_restore(&wait_start);
24695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024696
24697 if (p_verbose >= 12 && sourcing_name != NULL)
24698 {
24699 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024700 verbose_enter_scroll();
24701
Bram Moolenaar555b2802005-05-19 21:08:39 +000024702 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024703 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024704
24705 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024706 --no_wait_return;
24707 }
24708
24709 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024710 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024711 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024712
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024713 /* If the a:000 list and the l: and a: dicts are not referenced we can
24714 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024715 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24716 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24717 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24718 {
24719 free_funccal(fc, FALSE);
24720 }
24721 else
24722 {
24723 hashitem_T *hi;
24724 listitem_T *li;
24725 int todo;
24726
24727 /* "fc" is still in use. This can happen when returning "a:000" or
24728 * assigning "l:" to a global variable.
24729 * Link "fc" in the list for garbage collection later. */
24730 fc->caller = previous_funccal;
24731 previous_funccal = fc;
24732
24733 /* Make a copy of the a: variables, since we didn't do that above. */
24734 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24735 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24736 {
24737 if (!HASHITEM_EMPTY(hi))
24738 {
24739 --todo;
24740 v = HI2DI(hi);
24741 copy_tv(&v->di_tv, &v->di_tv);
24742 }
24743 }
24744
24745 /* Make a copy of the a:000 items, since we didn't do that above. */
24746 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24747 copy_tv(&li->li_tv, &li->li_tv);
24748 }
24749}
24750
24751/*
24752 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024753 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024754 */
24755 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024756can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024757{
24758 return (fc->l_varlist.lv_copyID != copyID
24759 && fc->l_vars.dv_copyID != copyID
24760 && fc->l_avars.dv_copyID != copyID);
24761}
24762
24763/*
24764 * Free "fc" and what it contains.
24765 */
24766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024767free_funccal(
24768 funccall_T *fc,
24769 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024770{
24771 listitem_T *li;
24772
24773 /* The a: variables typevals may not have been allocated, only free the
24774 * allocated variables. */
24775 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24776
24777 /* free all l: variables */
24778 vars_clear(&fc->l_vars.dv_hashtab);
24779
24780 /* Free the a:000 variables if they were allocated. */
24781 if (free_val)
24782 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24783 clear_tv(&li->li_tv);
24784
24785 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024786}
24787
24788/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024789 * Add a number variable "name" to dict "dp" with value "nr".
24790 */
24791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024792add_nr_var(
24793 dict_T *dp,
24794 dictitem_T *v,
24795 char *name,
24796 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024797{
24798 STRCPY(v->di_key, name);
24799 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24800 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24801 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024802 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024803 v->di_tv.vval.v_number = nr;
24804}
24805
24806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807 * ":return [expr]"
24808 */
24809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024810ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024811{
24812 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024813 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814 int returning = FALSE;
24815
24816 if (current_funccal == NULL)
24817 {
24818 EMSG(_("E133: :return not inside a function"));
24819 return;
24820 }
24821
24822 if (eap->skip)
24823 ++emsg_skip;
24824
24825 eap->nextcmd = NULL;
24826 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024827 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024828 {
24829 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024830 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024831 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024832 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024833 }
24834 /* It's safer to return also on error. */
24835 else if (!eap->skip)
24836 {
24837 /*
24838 * Return unless the expression evaluation has been cancelled due to an
24839 * aborting error, an interrupt, or an exception.
24840 */
24841 if (!aborting())
24842 returning = do_return(eap, FALSE, TRUE, NULL);
24843 }
24844
24845 /* When skipping or the return gets pending, advance to the next command
24846 * in this line (!returning). Otherwise, ignore the rest of the line.
24847 * Following lines will be ignored by get_func_line(). */
24848 if (returning)
24849 eap->nextcmd = NULL;
24850 else if (eap->nextcmd == NULL) /* no argument */
24851 eap->nextcmd = check_nextcmd(arg);
24852
24853 if (eap->skip)
24854 --emsg_skip;
24855}
24856
24857/*
24858 * Return from a function. Possibly makes the return pending. Also called
24859 * for a pending return at the ":endtry" or after returning from an extra
24860 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024861 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024862 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024863 * FALSE when the return gets pending.
24864 */
24865 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024866do_return(
24867 exarg_T *eap,
24868 int reanimate,
24869 int is_cmd,
24870 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871{
24872 int idx;
24873 struct condstack *cstack = eap->cstack;
24874
24875 if (reanimate)
24876 /* Undo the return. */
24877 current_funccal->returned = FALSE;
24878
24879 /*
24880 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24881 * not in its finally clause (which then is to be executed next) is found.
24882 * In this case, make the ":return" pending for execution at the ":endtry".
24883 * Otherwise, return normally.
24884 */
24885 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24886 if (idx >= 0)
24887 {
24888 cstack->cs_pending[idx] = CSTP_RETURN;
24889
24890 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024891 /* A pending return again gets pending. "rettv" points to an
24892 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024893 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024894 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895 else
24896 {
24897 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024898 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024900 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024902 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903 {
24904 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024905 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024906 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024907 else
24908 EMSG(_(e_outofmem));
24909 }
24910 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024911 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912
24913 if (reanimate)
24914 {
24915 /* The pending return value could be overwritten by a ":return"
24916 * without argument in a finally clause; reset the default
24917 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024918 current_funccal->rettv->v_type = VAR_NUMBER;
24919 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920 }
24921 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024922 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024923 }
24924 else
24925 {
24926 current_funccal->returned = TRUE;
24927
24928 /* If the return is carried out now, store the return value. For
24929 * a return immediately after reanimation, the value is already
24930 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024931 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024933 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024934 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024936 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 }
24938 }
24939
24940 return idx < 0;
24941}
24942
24943/*
24944 * Free the variable with a pending return value.
24945 */
24946 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024947discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024948{
Bram Moolenaar33570922005-01-25 22:26:29 +000024949 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024950}
24951
24952/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024953 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024954 * is an allocated string. Used by report_pending() for verbose messages.
24955 */
24956 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024957get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024958{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024959 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024960 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024961 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024962
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024963 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024964 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024965 if (s == NULL)
24966 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024967
24968 STRCPY(IObuff, ":return ");
24969 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24970 if (STRLEN(s) + 8 >= IOSIZE)
24971 STRCPY(IObuff + IOSIZE - 4, "...");
24972 vim_free(tofree);
24973 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024974}
24975
24976/*
24977 * Get next function line.
24978 * Called by do_cmdline() to get the next line.
24979 * Returns allocated string, or NULL for end of function.
24980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024981 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024982get_func_line(
24983 int c UNUSED,
24984 void *cookie,
24985 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024986{
Bram Moolenaar33570922005-01-25 22:26:29 +000024987 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024988 ufunc_T *fp = fcp->func;
24989 char_u *retval;
24990 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024991
24992 /* If breakpoints have been added/deleted need to check for it. */
24993 if (fcp->dbg_tick != debug_tick)
24994 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024995 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024996 sourcing_lnum);
24997 fcp->dbg_tick = debug_tick;
24998 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024999#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025000 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025001 func_line_end(cookie);
25002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025003
Bram Moolenaar05159a02005-02-26 23:04:13 +000025004 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025005 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25006 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025007 retval = NULL;
25008 else
25009 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025010 /* Skip NULL lines (continuation lines). */
25011 while (fcp->linenr < gap->ga_len
25012 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25013 ++fcp->linenr;
25014 if (fcp->linenr >= gap->ga_len)
25015 retval = NULL;
25016 else
25017 {
25018 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25019 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025020#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025021 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025022 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025023#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025025 }
25026
25027 /* Did we encounter a breakpoint? */
25028 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25029 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025030 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025032 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025033 sourcing_lnum);
25034 fcp->dbg_tick = debug_tick;
25035 }
25036
25037 return retval;
25038}
25039
Bram Moolenaar05159a02005-02-26 23:04:13 +000025040#if defined(FEAT_PROFILE) || defined(PROTO)
25041/*
25042 * Called when starting to read a function line.
25043 * "sourcing_lnum" must be correct!
25044 * When skipping lines it may not actually be executed, but we won't find out
25045 * until later and we need to store the time now.
25046 */
25047 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025048func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025049{
25050 funccall_T *fcp = (funccall_T *)cookie;
25051 ufunc_T *fp = fcp->func;
25052
25053 if (fp->uf_profiling && sourcing_lnum >= 1
25054 && sourcing_lnum <= fp->uf_lines.ga_len)
25055 {
25056 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025057 /* Skip continuation lines. */
25058 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25059 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025060 fp->uf_tml_execed = FALSE;
25061 profile_start(&fp->uf_tml_start);
25062 profile_zero(&fp->uf_tml_children);
25063 profile_get_wait(&fp->uf_tml_wait);
25064 }
25065}
25066
25067/*
25068 * Called when actually executing a function line.
25069 */
25070 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025071func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025072{
25073 funccall_T *fcp = (funccall_T *)cookie;
25074 ufunc_T *fp = fcp->func;
25075
25076 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25077 fp->uf_tml_execed = TRUE;
25078}
25079
25080/*
25081 * Called when done with a function line.
25082 */
25083 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025084func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025085{
25086 funccall_T *fcp = (funccall_T *)cookie;
25087 ufunc_T *fp = fcp->func;
25088
25089 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25090 {
25091 if (fp->uf_tml_execed)
25092 {
25093 ++fp->uf_tml_count[fp->uf_tml_idx];
25094 profile_end(&fp->uf_tml_start);
25095 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025096 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025097 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25098 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025099 }
25100 fp->uf_tml_idx = -1;
25101 }
25102}
25103#endif
25104
Bram Moolenaar071d4272004-06-13 20:20:40 +000025105/*
25106 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025107 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025108 */
25109 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025110func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025111{
Bram Moolenaar33570922005-01-25 22:26:29 +000025112 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113
25114 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25115 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025116 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025117 || fcp->returned);
25118}
25119
25120/*
25121 * return TRUE if cookie indicates a function which "abort"s on errors.
25122 */
25123 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025124func_has_abort(
25125 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025126{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025127 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128}
25129
25130#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25131typedef enum
25132{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025133 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25134 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25135 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025136} var_flavour_T;
25137
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025138static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139
25140 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025141var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025142{
25143 char_u *p = varname;
25144
25145 if (ASCII_ISUPPER(*p))
25146 {
25147 while (*(++p))
25148 if (ASCII_ISLOWER(*p))
25149 return VAR_FLAVOUR_SESSION;
25150 return VAR_FLAVOUR_VIMINFO;
25151 }
25152 else
25153 return VAR_FLAVOUR_DEFAULT;
25154}
25155#endif
25156
25157#if defined(FEAT_VIMINFO) || defined(PROTO)
25158/*
25159 * Restore global vars that start with a capital from the viminfo file
25160 */
25161 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025162read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025163{
25164 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025165 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025166 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025167 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025168
25169 if (!writing && (find_viminfo_parameter('!') != NULL))
25170 {
25171 tab = vim_strchr(virp->vir_line + 1, '\t');
25172 if (tab != NULL)
25173 {
25174 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025175 switch (*tab)
25176 {
25177 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025178#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025179 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025180#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025181 case 'D': type = VAR_DICT; break;
25182 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025183 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185
25186 tab = vim_strchr(tab, '\t');
25187 if (tab != NULL)
25188 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025189 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025190 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025191 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025192 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025193#ifdef FEAT_FLOAT
25194 else if (type == VAR_FLOAT)
25195 (void)string2float(tab + 1, &tv.vval.v_float);
25196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025198 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025199 if (type == VAR_DICT || type == VAR_LIST)
25200 {
25201 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25202
25203 if (etv == NULL)
25204 /* Failed to parse back the dict or list, use it as a
25205 * string. */
25206 tv.v_type = VAR_STRING;
25207 else
25208 {
25209 vim_free(tv.vval.v_string);
25210 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025211 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025212 }
25213 }
25214
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025215 /* when in a function use global variables */
25216 save_funccal = current_funccal;
25217 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025218 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025219 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025220
25221 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025222 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025223 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25224 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225 }
25226 }
25227 }
25228
25229 return viminfo_readline(virp);
25230}
25231
25232/*
25233 * Write global vars that start with a capital to the viminfo file
25234 */
25235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025236write_viminfo_varlist(FILE *fp)
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 Moolenaar2fc83fc2016-02-08 22:57:24 +010025241 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025242 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025243 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025244 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245
25246 if (find_viminfo_parameter('!') == NULL)
25247 return;
25248
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025249 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025250
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025251 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025252 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025253 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025254 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025256 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025257 this_var = HI2DI(hi);
25258 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025259 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025260 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025261 {
25262 case VAR_STRING: s = "STR"; break;
25263 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025264 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025265 case VAR_DICT: s = "DIC"; break;
25266 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025267 case VAR_SPECIAL: s = "XPL"; break;
25268
25269 case VAR_UNKNOWN:
25270 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025271 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025272 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025273 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025274 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025275 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025276 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025277 if (p != NULL)
25278 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025279 vim_free(tofree);
25280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025281 }
25282 }
25283}
25284#endif
25285
25286#if defined(FEAT_SESSION) || defined(PROTO)
25287 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025288store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289{
Bram Moolenaar33570922005-01-25 22:26:29 +000025290 hashitem_T *hi;
25291 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025292 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293 char_u *p, *t;
25294
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025295 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025296 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025297 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025298 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025299 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025300 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025301 this_var = HI2DI(hi);
25302 if ((this_var->di_tv.v_type == VAR_NUMBER
25303 || this_var->di_tv.v_type == VAR_STRING)
25304 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025305 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025306 /* Escape special characters with a backslash. Turn a LF and
25307 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025308 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025309 (char_u *)"\\\"\n\r");
25310 if (p == NULL) /* out of memory */
25311 break;
25312 for (t = p; *t != NUL; ++t)
25313 if (*t == '\n')
25314 *t = 'n';
25315 else if (*t == '\r')
25316 *t = 'r';
25317 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025318 this_var->di_key,
25319 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25320 : ' ',
25321 p,
25322 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25323 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025324 || put_eol(fd) == FAIL)
25325 {
25326 vim_free(p);
25327 return FAIL;
25328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025329 vim_free(p);
25330 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025331#ifdef FEAT_FLOAT
25332 else if (this_var->di_tv.v_type == VAR_FLOAT
25333 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25334 {
25335 float_T f = this_var->di_tv.vval.v_float;
25336 int sign = ' ';
25337
25338 if (f < 0)
25339 {
25340 f = -f;
25341 sign = '-';
25342 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025343 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025344 this_var->di_key, sign, f) < 0)
25345 || put_eol(fd) == FAIL)
25346 return FAIL;
25347 }
25348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025349 }
25350 }
25351 return OK;
25352}
25353#endif
25354
Bram Moolenaar661b1822005-07-28 22:36:45 +000025355/*
25356 * Display script name where an item was last set.
25357 * Should only be invoked when 'verbose' is non-zero.
25358 */
25359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025360last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025361{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025362 char_u *p;
25363
Bram Moolenaar661b1822005-07-28 22:36:45 +000025364 if (scriptID != 0)
25365 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025366 p = home_replace_save(NULL, get_scriptname(scriptID));
25367 if (p != NULL)
25368 {
25369 verbose_enter();
25370 MSG_PUTS(_("\n\tLast set from "));
25371 MSG_PUTS(p);
25372 vim_free(p);
25373 verbose_leave();
25374 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025375 }
25376}
25377
Bram Moolenaard812df62008-11-09 12:46:09 +000025378/*
25379 * List v:oldfiles in a nice way.
25380 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025381 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025382ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025383{
25384 list_T *l = vimvars[VV_OLDFILES].vv_list;
25385 listitem_T *li;
25386 int nr = 0;
25387
25388 if (l == NULL)
25389 msg((char_u *)_("No old files"));
25390 else
25391 {
25392 msg_start();
25393 msg_scroll = TRUE;
25394 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25395 {
25396 msg_outnum((long)++nr);
25397 MSG_PUTS(": ");
25398 msg_outtrans(get_tv_string(&li->li_tv));
25399 msg_putchar('\n');
25400 out_flush(); /* output one line at a time */
25401 ui_breakcheck();
25402 }
25403 /* Assume "got_int" was set to truncate the listing. */
25404 got_int = FALSE;
25405
25406#ifdef FEAT_BROWSE_CMD
25407 if (cmdmod.browse)
25408 {
25409 quit_more = FALSE;
25410 nr = prompt_for_number(FALSE);
25411 msg_starthere();
25412 if (nr > 0)
25413 {
25414 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25415 (long)nr);
25416
25417 if (p != NULL)
25418 {
25419 p = expand_env_save(p);
25420 eap->arg = p;
25421 eap->cmdidx = CMD_edit;
25422 cmdmod.browse = FALSE;
25423 do_exedit(eap, NULL);
25424 vim_free(p);
25425 }
25426 }
25427 }
25428#endif
25429 }
25430}
25431
Bram Moolenaar53744302015-07-17 17:38:22 +020025432/* reset v:option_new, v:option_old and v:option_type */
25433 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025434reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025435{
25436 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25437 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25438 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25439}
25440
25441
Bram Moolenaar071d4272004-06-13 20:20:40 +000025442#endif /* FEAT_EVAL */
25443
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025445#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025446
25447#ifdef WIN3264
25448/*
25449 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25450 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025451static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25452static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25453static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025454
25455/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025456 * Get the short path (8.3) for the filename in "fnamep".
25457 * Only works for a valid file name.
25458 * When the path gets longer "fnamep" is changed and the allocated buffer
25459 * is put in "bufp".
25460 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25461 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025462 */
25463 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025464get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025465{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025466 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467 char_u *newbuf;
25468
25469 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025470 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471 if (l > len - 1)
25472 {
25473 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025474 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025475 newbuf = vim_strnsave(*fnamep, l);
25476 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025477 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025478
25479 vim_free(*bufp);
25480 *fnamep = *bufp = newbuf;
25481
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025482 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025483 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484 }
25485
25486 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025487 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025488}
25489
25490/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025491 * Get the short path (8.3) for the filename in "fname". The converted
25492 * path is returned in "bufp".
25493 *
25494 * Some of the directories specified in "fname" may not exist. This function
25495 * will shorten the existing directories at the beginning of the path and then
25496 * append the remaining non-existing path.
25497 *
25498 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025499 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025500 * bufp - Pointer to an allocated buffer for the filename.
25501 * fnamelen - Length of the filename pointed to by fname
25502 *
25503 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025504 */
25505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025506shortpath_for_invalid_fname(
25507 char_u **fname,
25508 char_u **bufp,
25509 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025510{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025511 char_u *short_fname, *save_fname, *pbuf_unused;
25512 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025514 int old_len, len;
25515 int new_len, sfx_len;
25516 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025517
25518 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025519 old_len = *fnamelen;
25520 save_fname = vim_strnsave(*fname, old_len);
25521 pbuf_unused = NULL;
25522 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025523
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025524 endp = save_fname + old_len - 1; /* Find the end of the copy */
25525 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025526
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025527 /*
25528 * Try shortening the supplied path till it succeeds by removing one
25529 * directory at a time from the tail of the path.
25530 */
25531 len = 0;
25532 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025533 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025534 /* go back one path-separator */
25535 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25536 --endp;
25537 if (endp <= save_fname)
25538 break; /* processed the complete path */
25539
25540 /*
25541 * Replace the path separator with a NUL and try to shorten the
25542 * resulting path.
25543 */
25544 ch = *endp;
25545 *endp = 0;
25546 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025547 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025548 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25549 {
25550 retval = FAIL;
25551 goto theend;
25552 }
25553 *endp = ch; /* preserve the string */
25554
25555 if (len > 0)
25556 break; /* successfully shortened the path */
25557
25558 /* failed to shorten the path. Skip the path separator */
25559 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025560 }
25561
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025562 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025563 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025564 /*
25565 * Succeeded in shortening the path. Now concatenate the shortened
25566 * path with the remaining path at the tail.
25567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025569 /* Compute the length of the new path. */
25570 sfx_len = (int)(save_endp - endp) + 1;
25571 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025572
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025573 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025574 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025575 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025576 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025577 /* There is not enough space in the currently allocated string,
25578 * copy it to a buffer big enough. */
25579 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025580 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025581 {
25582 retval = FAIL;
25583 goto theend;
25584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025585 }
25586 else
25587 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025588 /* Transfer short_fname to the main buffer (it's big enough),
25589 * unless get_short_pathname() did its work in-place. */
25590 *fname = *bufp = save_fname;
25591 if (short_fname != save_fname)
25592 vim_strncpy(save_fname, short_fname, len);
25593 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025595
25596 /* concat the not-shortened part of the path */
25597 vim_strncpy(*fname + len, endp, sfx_len);
25598 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025599 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025600
25601theend:
25602 vim_free(pbuf_unused);
25603 vim_free(save_fname);
25604
25605 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025606}
25607
25608/*
25609 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025610 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611 */
25612 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025613shortpath_for_partial(
25614 char_u **fnamep,
25615 char_u **bufp,
25616 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617{
25618 int sepcount, len, tflen;
25619 char_u *p;
25620 char_u *pbuf, *tfname;
25621 int hasTilde;
25622
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025623 /* Count up the path separators from the RHS.. so we know which part
25624 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025625 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025626 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025627 if (vim_ispathsep(*p))
25628 ++sepcount;
25629
25630 /* Need full path first (use expand_env() to remove a "~/") */
25631 hasTilde = (**fnamep == '~');
25632 if (hasTilde)
25633 pbuf = tfname = expand_env_save(*fnamep);
25634 else
25635 pbuf = tfname = FullName_save(*fnamep, FALSE);
25636
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025637 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025638
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025639 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25640 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641
25642 if (len == 0)
25643 {
25644 /* Don't have a valid filename, so shorten the rest of the
25645 * path if we can. This CAN give us invalid 8.3 filenames, but
25646 * there's not a lot of point in guessing what it might be.
25647 */
25648 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025649 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25650 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025651 }
25652
25653 /* Count the paths backward to find the beginning of the desired string. */
25654 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025655 {
25656#ifdef FEAT_MBYTE
25657 if (has_mbyte)
25658 p -= mb_head_off(tfname, p);
25659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025660 if (vim_ispathsep(*p))
25661 {
25662 if (sepcount == 0 || (hasTilde && sepcount == 1))
25663 break;
25664 else
25665 sepcount --;
25666 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025668 if (hasTilde)
25669 {
25670 --p;
25671 if (p >= tfname)
25672 *p = '~';
25673 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025674 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025675 }
25676 else
25677 ++p;
25678
25679 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25680 vim_free(*bufp);
25681 *fnamelen = (int)STRLEN(p);
25682 *bufp = pbuf;
25683 *fnamep = p;
25684
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025685 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686}
25687#endif /* WIN3264 */
25688
25689/*
25690 * Adjust a filename, according to a string of modifiers.
25691 * *fnamep must be NUL terminated when called. When returning, the length is
25692 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025693 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025694 * When there is an error, *fnamep is set to NULL.
25695 */
25696 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025697modify_fname(
25698 char_u *src, /* string with modifiers */
25699 int *usedlen, /* characters after src that are used */
25700 char_u **fnamep, /* file name so far */
25701 char_u **bufp, /* buffer for allocated file name or NULL */
25702 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025703{
25704 int valid = 0;
25705 char_u *tail;
25706 char_u *s, *p, *pbuf;
25707 char_u dirname[MAXPATHL];
25708 int c;
25709 int has_fullname = 0;
25710#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025711 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025712 int has_shortname = 0;
25713#endif
25714
25715repeat:
25716 /* ":p" - full path/file_name */
25717 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25718 {
25719 has_fullname = 1;
25720
25721 valid |= VALID_PATH;
25722 *usedlen += 2;
25723
25724 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25725 if ((*fnamep)[0] == '~'
25726#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25727 && ((*fnamep)[1] == '/'
25728# ifdef BACKSLASH_IN_FILENAME
25729 || (*fnamep)[1] == '\\'
25730# endif
25731 || (*fnamep)[1] == NUL)
25732
25733#endif
25734 )
25735 {
25736 *fnamep = expand_env_save(*fnamep);
25737 vim_free(*bufp); /* free any allocated file name */
25738 *bufp = *fnamep;
25739 if (*fnamep == NULL)
25740 return -1;
25741 }
25742
25743 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025744 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025745 {
25746 if (vim_ispathsep(*p)
25747 && p[1] == '.'
25748 && (p[2] == NUL
25749 || vim_ispathsep(p[2])
25750 || (p[2] == '.'
25751 && (p[3] == NUL || vim_ispathsep(p[3])))))
25752 break;
25753 }
25754
25755 /* FullName_save() is slow, don't use it when not needed. */
25756 if (*p != NUL || !vim_isAbsName(*fnamep))
25757 {
25758 *fnamep = FullName_save(*fnamep, *p != NUL);
25759 vim_free(*bufp); /* free any allocated file name */
25760 *bufp = *fnamep;
25761 if (*fnamep == NULL)
25762 return -1;
25763 }
25764
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025765#ifdef WIN3264
25766# if _WIN32_WINNT >= 0x0500
25767 if (vim_strchr(*fnamep, '~') != NULL)
25768 {
25769 /* Expand 8.3 filename to full path. Needed to make sure the same
25770 * file does not have two different names.
25771 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25772 p = alloc(_MAX_PATH + 1);
25773 if (p != NULL)
25774 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025775 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025776 {
25777 vim_free(*bufp);
25778 *bufp = *fnamep = p;
25779 }
25780 else
25781 vim_free(p);
25782 }
25783 }
25784# endif
25785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786 /* Append a path separator to a directory. */
25787 if (mch_isdir(*fnamep))
25788 {
25789 /* Make room for one or two extra characters. */
25790 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25791 vim_free(*bufp); /* free any allocated file name */
25792 *bufp = *fnamep;
25793 if (*fnamep == NULL)
25794 return -1;
25795 add_pathsep(*fnamep);
25796 }
25797 }
25798
25799 /* ":." - path relative to the current directory */
25800 /* ":~" - path relative to the home directory */
25801 /* ":8" - shortname path - postponed till after */
25802 while (src[*usedlen] == ':'
25803 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25804 {
25805 *usedlen += 2;
25806 if (c == '8')
25807 {
25808#ifdef WIN3264
25809 has_shortname = 1; /* Postpone this. */
25810#endif
25811 continue;
25812 }
25813 pbuf = NULL;
25814 /* Need full path first (use expand_env() to remove a "~/") */
25815 if (!has_fullname)
25816 {
25817 if (c == '.' && **fnamep == '~')
25818 p = pbuf = expand_env_save(*fnamep);
25819 else
25820 p = pbuf = FullName_save(*fnamep, FALSE);
25821 }
25822 else
25823 p = *fnamep;
25824
25825 has_fullname = 0;
25826
25827 if (p != NULL)
25828 {
25829 if (c == '.')
25830 {
25831 mch_dirname(dirname, MAXPATHL);
25832 s = shorten_fname(p, dirname);
25833 if (s != NULL)
25834 {
25835 *fnamep = s;
25836 if (pbuf != NULL)
25837 {
25838 vim_free(*bufp); /* free any allocated file name */
25839 *bufp = pbuf;
25840 pbuf = NULL;
25841 }
25842 }
25843 }
25844 else
25845 {
25846 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25847 /* Only replace it when it starts with '~' */
25848 if (*dirname == '~')
25849 {
25850 s = vim_strsave(dirname);
25851 if (s != NULL)
25852 {
25853 *fnamep = s;
25854 vim_free(*bufp);
25855 *bufp = s;
25856 }
25857 }
25858 }
25859 vim_free(pbuf);
25860 }
25861 }
25862
25863 tail = gettail(*fnamep);
25864 *fnamelen = (int)STRLEN(*fnamep);
25865
25866 /* ":h" - head, remove "/file_name", can be repeated */
25867 /* Don't remove the first "/" or "c:\" */
25868 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25869 {
25870 valid |= VALID_HEAD;
25871 *usedlen += 2;
25872 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025873 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025874 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025875 *fnamelen = (int)(tail - *fnamep);
25876#ifdef VMS
25877 if (*fnamelen > 0)
25878 *fnamelen += 1; /* the path separator is part of the path */
25879#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025880 if (*fnamelen == 0)
25881 {
25882 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25883 p = vim_strsave((char_u *)".");
25884 if (p == NULL)
25885 return -1;
25886 vim_free(*bufp);
25887 *bufp = *fnamep = tail = p;
25888 *fnamelen = 1;
25889 }
25890 else
25891 {
25892 while (tail > s && !after_pathsep(s, tail))
25893 mb_ptr_back(*fnamep, tail);
25894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025895 }
25896
25897 /* ":8" - shortname */
25898 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25899 {
25900 *usedlen += 2;
25901#ifdef WIN3264
25902 has_shortname = 1;
25903#endif
25904 }
25905
25906#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025907 /*
25908 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025909 */
25910 if (has_shortname)
25911 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025912 /* Copy the string if it is shortened by :h and when it wasn't copied
25913 * yet, because we are going to change it in place. Avoids changing
25914 * the buffer name for "%:8". */
25915 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025916 {
25917 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025918 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025919 return -1;
25920 vim_free(*bufp);
25921 *bufp = *fnamep = p;
25922 }
25923
25924 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025925 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926 if (!has_fullname && !vim_isAbsName(*fnamep))
25927 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025928 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025929 return -1;
25930 }
25931 else
25932 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025933 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025934
Bram Moolenaardc935552011-08-17 15:23:23 +020025935 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025936 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025937 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025938 return -1;
25939
25940 if (l == 0)
25941 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025942 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025943 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025944 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025945 return -1;
25946 }
25947 *fnamelen = l;
25948 }
25949 }
25950#endif /* WIN3264 */
25951
25952 /* ":t" - tail, just the basename */
25953 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25954 {
25955 *usedlen += 2;
25956 *fnamelen -= (int)(tail - *fnamep);
25957 *fnamep = tail;
25958 }
25959
25960 /* ":e" - extension, can be repeated */
25961 /* ":r" - root, without extension, can be repeated */
25962 while (src[*usedlen] == ':'
25963 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25964 {
25965 /* find a '.' in the tail:
25966 * - for second :e: before the current fname
25967 * - otherwise: The last '.'
25968 */
25969 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25970 s = *fnamep - 2;
25971 else
25972 s = *fnamep + *fnamelen - 1;
25973 for ( ; s > tail; --s)
25974 if (s[0] == '.')
25975 break;
25976 if (src[*usedlen + 1] == 'e') /* :e */
25977 {
25978 if (s > tail)
25979 {
25980 *fnamelen += (int)(*fnamep - (s + 1));
25981 *fnamep = s + 1;
25982#ifdef VMS
25983 /* cut version from the extension */
25984 s = *fnamep + *fnamelen - 1;
25985 for ( ; s > *fnamep; --s)
25986 if (s[0] == ';')
25987 break;
25988 if (s > *fnamep)
25989 *fnamelen = s - *fnamep;
25990#endif
25991 }
25992 else if (*fnamep <= tail)
25993 *fnamelen = 0;
25994 }
25995 else /* :r */
25996 {
25997 if (s > tail) /* remove one extension */
25998 *fnamelen = (int)(s - *fnamep);
25999 }
26000 *usedlen += 2;
26001 }
26002
26003 /* ":s?pat?foo?" - substitute */
26004 /* ":gs?pat?foo?" - global substitute */
26005 if (src[*usedlen] == ':'
26006 && (src[*usedlen + 1] == 's'
26007 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26008 {
26009 char_u *str;
26010 char_u *pat;
26011 char_u *sub;
26012 int sep;
26013 char_u *flags;
26014 int didit = FALSE;
26015
26016 flags = (char_u *)"";
26017 s = src + *usedlen + 2;
26018 if (src[*usedlen + 1] == 'g')
26019 {
26020 flags = (char_u *)"g";
26021 ++s;
26022 }
26023
26024 sep = *s++;
26025 if (sep)
26026 {
26027 /* find end of pattern */
26028 p = vim_strchr(s, sep);
26029 if (p != NULL)
26030 {
26031 pat = vim_strnsave(s, (int)(p - s));
26032 if (pat != NULL)
26033 {
26034 s = p + 1;
26035 /* find end of substitution */
26036 p = vim_strchr(s, sep);
26037 if (p != NULL)
26038 {
26039 sub = vim_strnsave(s, (int)(p - s));
26040 str = vim_strnsave(*fnamep, *fnamelen);
26041 if (sub != NULL && str != NULL)
26042 {
26043 *usedlen = (int)(p + 1 - src);
26044 s = do_string_sub(str, pat, sub, flags);
26045 if (s != NULL)
26046 {
26047 *fnamep = s;
26048 *fnamelen = (int)STRLEN(s);
26049 vim_free(*bufp);
26050 *bufp = s;
26051 didit = TRUE;
26052 }
26053 }
26054 vim_free(sub);
26055 vim_free(str);
26056 }
26057 vim_free(pat);
26058 }
26059 }
26060 /* after using ":s", repeat all the modifiers */
26061 if (didit)
26062 goto repeat;
26063 }
26064 }
26065
Bram Moolenaar26df0922014-02-23 23:39:13 +010026066 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26067 {
26068 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26069 if (p == NULL)
26070 return -1;
26071 vim_free(*bufp);
26072 *bufp = *fnamep = p;
26073 *fnamelen = (int)STRLEN(p);
26074 *usedlen += 2;
26075 }
26076
Bram Moolenaar071d4272004-06-13 20:20:40 +000026077 return valid;
26078}
26079
26080/*
26081 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26082 * "flags" can be "g" to do a global substitute.
26083 * Returns an allocated string, NULL for error.
26084 */
26085 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026086do_string_sub(
26087 char_u *str,
26088 char_u *pat,
26089 char_u *sub,
26090 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026091{
26092 int sublen;
26093 regmatch_T regmatch;
26094 int i;
26095 int do_all;
26096 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026097 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026098 garray_T ga;
26099 char_u *ret;
26100 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026101 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026102
26103 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26104 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026105 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026106
26107 ga_init2(&ga, 1, 200);
26108
26109 do_all = (flags[0] == 'g');
26110
26111 regmatch.rm_ic = p_ic;
26112 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26113 if (regmatch.regprog != NULL)
26114 {
26115 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026116 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026117 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26118 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026119 /* Skip empty match except for first match. */
26120 if (regmatch.startp[0] == regmatch.endp[0])
26121 {
26122 if (zero_width == regmatch.startp[0])
26123 {
26124 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026125 i = MB_PTR2LEN(tail);
26126 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26127 (size_t)i);
26128 ga.ga_len += i;
26129 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026130 continue;
26131 }
26132 zero_width = regmatch.startp[0];
26133 }
26134
Bram Moolenaar071d4272004-06-13 20:20:40 +000026135 /*
26136 * Get some space for a temporary buffer to do the substitution
26137 * into. It will contain:
26138 * - The text up to where the match is.
26139 * - The substituted text.
26140 * - The text after the match.
26141 */
26142 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026143 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026144 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26145 {
26146 ga_clear(&ga);
26147 break;
26148 }
26149
26150 /* copy the text up to where the match is */
26151 i = (int)(regmatch.startp[0] - tail);
26152 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26153 /* add the substituted text */
26154 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26155 + ga.ga_len + i, TRUE, TRUE, FALSE);
26156 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026157 tail = regmatch.endp[0];
26158 if (*tail == NUL)
26159 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160 if (!do_all)
26161 break;
26162 }
26163
26164 if (ga.ga_data != NULL)
26165 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26166
Bram Moolenaar473de612013-06-08 18:19:48 +020026167 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026168 }
26169
26170 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26171 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026172 if (p_cpo == empty_option)
26173 p_cpo = save_cpo;
26174 else
26175 /* Darn, evaluating {sub} expression changed the value. */
26176 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026177
26178 return ret;
26179}
26180
26181#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */