blob: 8e4e5400abc398db8352574ba69a445d98354d32 [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 Moolenaarf57969a2016-02-02 20:47:49 +01009853#ifdef FEAT_CHANNEL
9854/*
Bram Moolenaar77073442016-02-13 23:23:53 +01009855 * Get the channel from the argument.
9856 * Returns NULL if the handle is invalid.
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009857 */
Bram Moolenaar77073442016-02-13 23:23:53 +01009858 static channel_T *
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009859get_channel_arg(typval_T *tv)
9860{
Bram Moolenaar77073442016-02-13 23:23:53 +01009861 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009862
Bram Moolenaar77073442016-02-13 23:23:53 +01009863 if (tv->v_type != VAR_CHANNEL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009864 {
9865 EMSG2(_(e_invarg2), get_tv_string(tv));
Bram Moolenaar77073442016-02-13 23:23:53 +01009866 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009867 }
Bram Moolenaar77073442016-02-13 23:23:53 +01009868 channel = tv->vval.v_channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009869
Bram Moolenaar77073442016-02-13 23:23:53 +01009870 if (channel == NULL || !channel_is_open(channel))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009871 {
Bram Moolenaar77073442016-02-13 23:23:53 +01009872 EMSG(_("E906: not an open channel"));
9873 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009874 }
Bram Moolenaar77073442016-02-13 23:23:53 +01009875 return channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009876}
9877
9878/*
9879 * "ch_close()" function
9880 */
9881 static void
9882f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9883{
Bram Moolenaar77073442016-02-13 23:23:53 +01009884 channel_T *channel = get_channel_arg(&argvars[0]);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009885
Bram Moolenaar77073442016-02-13 23:23:53 +01009886 if (channel != NULL)
9887 channel_close(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009888}
9889
9890/*
9891 * Get a callback from "arg". It can be a Funcref or a function name.
9892 * When "arg" is zero return an empty string.
9893 * Return NULL for an invalid argument.
9894 */
9895 static char_u *
9896get_callback(typval_T *arg)
9897{
9898 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9899 return arg->vval.v_string;
9900 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9901 return (char_u *)"";
9902 EMSG(_("E999: Invalid callback argument"));
9903 return NULL;
9904}
9905
9906/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +01009907 * "ch_logfile()" function
9908 */
9909 static void
9910f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
9911{
9912 char_u *fname;
9913 char_u *opt = (char_u *)"";
9914 char_u buf[NUMBUFLEN];
9915 FILE *file = NULL;
9916
9917 fname = get_tv_string(&argvars[0]);
9918 if (argvars[1].v_type == VAR_STRING)
9919 opt = get_tv_string_buf(&argvars[1], buf);
9920 if (*fname != NUL)
9921 {
9922 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
9923 if (file == NULL)
9924 {
9925 EMSG2(_(e_notopen), fname);
9926 return;
9927 }
9928 }
9929 ch_logfile(file);
9930}
9931
9932/*
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009933 * Get the "mode" entry from "dict", if it exists, and parse the mode name.
9934 * If the mode is invalide return FAIL.
9935 */
9936 static int
9937get_mode_arg(dict_T *dict, jobopt_T *opt)
9938{
9939 dictitem_T *item;
9940 char_u *mode;
9941
9942 if ((item = dict_find(dict, (char_u *)"mode", -1)) != NULL)
9943 {
9944 mode = get_tv_string(&item->di_tv);
9945 if (STRCMP(mode, "nl") == 0)
9946 opt->jo_mode = MODE_NL;
9947 else if (STRCMP(mode, "raw") == 0)
9948 opt->jo_mode = MODE_RAW;
9949 else if (STRCMP(mode, "js") == 0)
9950 opt->jo_mode = MODE_JS;
9951 else if (STRCMP(mode, "json") == 0)
9952 opt->jo_mode = MODE_JSON;
9953 else
9954 {
9955 EMSG2(_(e_invarg2), mode);
9956 return FAIL;
9957 }
9958 }
9959 return OK;
9960}
9961
9962/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009963 * "ch_open()" function
9964 */
9965 static void
9966f_ch_open(typval_T *argvars, typval_T *rettv)
9967{
9968 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009969 char_u *callback = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009970 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009971 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009972 int port;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009973 int waittime = 0;
9974 int timeout = 2000;
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009975 jobopt_T options;
Bram Moolenaar77073442016-02-13 23:23:53 +01009976 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009977
9978 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +01009979 rettv->v_type = VAR_CHANNEL;
9980 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009981
9982 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009983 if (argvars[1].v_type != VAR_UNKNOWN
9984 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009985 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009986 EMSG(_(e_invarg));
9987 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009988 }
9989
9990 /* parse address */
9991 p = vim_strchr(address, ':');
9992 if (p == NULL)
9993 {
9994 EMSG2(_(e_invarg2), address);
9995 return;
9996 }
9997 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01009998 port = strtol((char *)p, &rest, 10);
9999 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010000 {
10001 p[-1] = ':';
10002 EMSG2(_(e_invarg2), address);
10003 return;
10004 }
10005
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010006 options.jo_mode = MODE_JSON;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010007 if (argvars[1].v_type == VAR_DICT)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010008 {
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +010010009 dict_T *dict = argvars[1].vval.v_dict;
10010 dictitem_T *item;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010011
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +010010012 /* parse argdict */
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010013 if (get_mode_arg(dict, &options) == FAIL)
10014 return;
Bram Moolenaarb6a4fee2016-02-11 20:48:34 +010010015 if ((item = dict_find(dict, (char_u *)"waittime", -1)) != NULL)
10016 waittime = get_tv_number(&item->di_tv);
10017 if ((item = dict_find(dict, (char_u *)"timeout", -1)) != NULL)
10018 timeout = get_tv_number(&item->di_tv);
10019 if ((item = dict_find(dict, (char_u *)"callback", -1)) != NULL)
10020 callback = get_callback(&item->di_tv);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010021 }
10022 if (waittime < 0 || timeout < 0)
10023 {
10024 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010025 return;
10026 }
10027
Bram Moolenaar77073442016-02-13 23:23:53 +010010028 channel = channel_open((char *)address, port, waittime, NULL);
10029 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010030 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010031 rettv->vval.v_channel = channel;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010032 channel_set_mode(channel, options.jo_mode);
Bram Moolenaar77073442016-02-13 23:23:53 +010010033 channel_set_timeout(channel, timeout);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010034 if (callback != NULL && *callback != NUL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010035 channel_set_callback(channel, callback);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010036 }
10037}
10038
10039/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010040 * "ch_readraw()" function
10041 */
10042 static void
10043f_ch_readraw(typval_T *argvars, typval_T *rettv)
10044{
Bram Moolenaar77073442016-02-13 23:23:53 +010010045 channel_T *channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010046
10047 /* return an empty string by default */
10048 rettv->v_type = VAR_STRING;
10049 rettv->vval.v_string = NULL;
10050
Bram Moolenaar77073442016-02-13 23:23:53 +010010051 channel = get_channel_arg(&argvars[0]);
10052 if (channel != NULL)
10053 rettv->vval.v_string = channel_read_block(channel);
10054}
10055
10056/*
10057 * "ch_status()" function
10058 */
10059 static void
10060f_ch_status(typval_T *argvars, typval_T *rettv)
10061{
10062 /* return an empty string by default */
10063 rettv->v_type = VAR_STRING;
10064
10065 if (argvars[0].v_type != VAR_CHANNEL)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010066 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010067 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10068 rettv->vval.v_string = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010069 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010070 else
10071 rettv->vval.v_string = vim_strsave(
10072 (char_u *)channel_status(argvars[0].vval.v_channel));
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010073}
10074
10075/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010076 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010077 * Returns the channel if the caller should read the response.
10078 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010079 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010080 static channel_T *
Bram Moolenaara07fec92016-02-05 21:04:08 +010010081send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010082{
Bram Moolenaar77073442016-02-13 23:23:53 +010010083 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010084 char_u *callback = NULL;
10085
Bram Moolenaar77073442016-02-13 23:23:53 +010010086 channel = get_channel_arg(&argvars[0]);
10087 if (channel == NULL)
10088 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010089
10090 if (argvars[2].v_type != VAR_UNKNOWN)
10091 {
10092 callback = get_callback(&argvars[2]);
10093 if (callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010094 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010095 }
Bram Moolenaara07fec92016-02-05 21:04:08 +010010096 /* Set the callback. An empty callback means no callback and not reading
10097 * the response. */
10098 if (callback != NULL && *callback != NUL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010099 channel_set_req_callback(channel, callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010100
Bram Moolenaar77073442016-02-13 23:23:53 +010010101 if (channel_send(channel, text, fun) == OK && callback == NULL)
10102 return channel;
10103 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010104}
10105
10106/*
10107 * "ch_sendexpr()" function
10108 */
10109 static void
10110f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10111{
10112 char_u *text;
10113 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010114 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010115 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010116 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010117
10118 /* return an empty string by default */
10119 rettv->v_type = VAR_STRING;
10120 rettv->vval.v_string = NULL;
10121
Bram Moolenaar77073442016-02-13 23:23:53 +010010122 channel = get_channel_arg(&argvars[0]);
10123 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010124 return;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010125
Bram Moolenaar77073442016-02-13 23:23:53 +010010126 ch_mode = channel_get_mode(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010127 if (ch_mode == MODE_RAW)
10128 {
10129 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
10130 return;
10131 }
10132
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010133 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010134 text = json_encode_nr_expr(id, &argvars[1],
10135 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010136 if (text == NULL)
10137 return;
10138
Bram Moolenaar77073442016-02-13 23:23:53 +010010139 channel = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010140 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010141 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010142 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010143 if (channel_read_json_block(channel, id, &listtv) == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010144 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010145 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010146
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010147 /* Move the item from the list and then change the type to
10148 * avoid the value being freed. */
10149 *rettv = list->lv_last->li_tv;
10150 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010151 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010152 }
10153 }
10154}
10155
10156/*
10157 * "ch_sendraw()" function
10158 */
10159 static void
10160f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10161{
10162 char_u buf[NUMBUFLEN];
10163 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010164 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010165
10166 /* return an empty string by default */
10167 rettv->v_type = VAR_STRING;
10168 rettv->vval.v_string = NULL;
10169
10170 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar77073442016-02-13 23:23:53 +010010171 channel = send_common(argvars, text, 0, "sendraw");
10172 if (channel != NULL)
10173 rettv->vval.v_string = channel_read_block(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010174}
10175#endif
10176
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010177/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010178 * "changenr()" function
10179 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010181f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010182{
10183 rettv->vval.v_number = curbuf->b_u_seq_cur;
10184}
10185
10186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 * "char2nr(string)" function
10188 */
10189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010190f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191{
10192#ifdef FEAT_MBYTE
10193 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010194 {
10195 int utf8 = 0;
10196
10197 if (argvars[1].v_type != VAR_UNKNOWN)
10198 utf8 = get_tv_number_chk(&argvars[1], NULL);
10199
10200 if (utf8)
10201 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10202 else
10203 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 else
10206#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010207 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208}
10209
10210/*
10211 * "cindent(lnum)" function
10212 */
10213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010214f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215{
10216#ifdef FEAT_CINDENT
10217 pos_T pos;
10218 linenr_T lnum;
10219
10220 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010221 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10223 {
10224 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010225 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 curwin->w_cursor = pos;
10227 }
10228 else
10229#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010230 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231}
10232
10233/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010234 * "clearmatches()" function
10235 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010237f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010238{
10239#ifdef FEAT_SEARCH_EXTRA
10240 clear_matches(curwin);
10241#endif
10242}
10243
10244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 * "col(string)" function
10246 */
10247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010248f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249{
10250 colnr_T col = 0;
10251 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010252 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010254 fp = var2fpos(&argvars[0], FALSE, &fnum);
10255 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 {
10257 if (fp->col == MAXCOL)
10258 {
10259 /* '> can be MAXCOL, get the length of the line then */
10260 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010261 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 else
10263 col = MAXCOL;
10264 }
10265 else
10266 {
10267 col = fp->col + 1;
10268#ifdef FEAT_VIRTUALEDIT
10269 /* col(".") when the cursor is on the NUL at the end of the line
10270 * because of "coladd" can be seen as an extra column. */
10271 if (virtual_active() && fp == &curwin->w_cursor)
10272 {
10273 char_u *p = ml_get_cursor();
10274
10275 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10276 curwin->w_virtcol - curwin->w_cursor.coladd))
10277 {
10278# ifdef FEAT_MBYTE
10279 int l;
10280
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010281 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 col += l;
10283# else
10284 if (*p != NUL && p[1] == NUL)
10285 ++col;
10286# endif
10287 }
10288 }
10289#endif
10290 }
10291 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010292 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293}
10294
Bram Moolenaar572cb562005-08-05 21:35:02 +000010295#if defined(FEAT_INS_EXPAND)
10296/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010297 * "complete()" function
10298 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010300f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010301{
10302 int startcol;
10303
10304 if ((State & INSERT) == 0)
10305 {
10306 EMSG(_("E785: complete() can only be used in Insert mode"));
10307 return;
10308 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010309
10310 /* Check for undo allowed here, because if something was already inserted
10311 * the line was already saved for undo and this check isn't done. */
10312 if (!undo_allowed())
10313 return;
10314
Bram Moolenaarade00832006-03-10 21:46:58 +000010315 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10316 {
10317 EMSG(_(e_invarg));
10318 return;
10319 }
10320
10321 startcol = get_tv_number_chk(&argvars[0], NULL);
10322 if (startcol <= 0)
10323 return;
10324
10325 set_completion(startcol - 1, argvars[1].vval.v_list);
10326}
10327
10328/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010329 * "complete_add()" function
10330 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010332f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010333{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010334 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010335}
10336
10337/*
10338 * "complete_check()" function
10339 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010341f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010342{
10343 int saved = RedrawingDisabled;
10344
10345 RedrawingDisabled = 0;
10346 ins_compl_check_keys(0);
10347 rettv->vval.v_number = compl_interrupted;
10348 RedrawingDisabled = saved;
10349}
10350#endif
10351
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352/*
10353 * "confirm(message, buttons[, default [, type]])" function
10354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010356f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357{
10358#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10359 char_u *message;
10360 char_u *buttons = NULL;
10361 char_u buf[NUMBUFLEN];
10362 char_u buf2[NUMBUFLEN];
10363 int def = 1;
10364 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010365 char_u *typestr;
10366 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010368 message = get_tv_string_chk(&argvars[0]);
10369 if (message == NULL)
10370 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010371 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010373 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10374 if (buttons == NULL)
10375 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010376 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010378 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010379 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010381 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10382 if (typestr == NULL)
10383 error = TRUE;
10384 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010386 switch (TOUPPER_ASC(*typestr))
10387 {
10388 case 'E': type = VIM_ERROR; break;
10389 case 'Q': type = VIM_QUESTION; break;
10390 case 'I': type = VIM_INFO; break;
10391 case 'W': type = VIM_WARNING; break;
10392 case 'G': type = VIM_GENERIC; break;
10393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 }
10395 }
10396 }
10397 }
10398
10399 if (buttons == NULL || *buttons == NUL)
10400 buttons = (char_u *)_("&Ok");
10401
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010402 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010403 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010404 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405#endif
10406}
10407
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010408/*
10409 * "copy()" function
10410 */
10411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010412f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010413{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010414 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010415}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010417#ifdef FEAT_FLOAT
10418/*
10419 * "cos()" function
10420 */
10421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010422f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010423{
10424 float_T f;
10425
10426 rettv->v_type = VAR_FLOAT;
10427 if (get_float_arg(argvars, &f) == OK)
10428 rettv->vval.v_float = cos(f);
10429 else
10430 rettv->vval.v_float = 0.0;
10431}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010432
10433/*
10434 * "cosh()" function
10435 */
10436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010437f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010438{
10439 float_T f;
10440
10441 rettv->v_type = VAR_FLOAT;
10442 if (get_float_arg(argvars, &f) == OK)
10443 rettv->vval.v_float = cosh(f);
10444 else
10445 rettv->vval.v_float = 0.0;
10446}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010447#endif
10448
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010450 * "count()" function
10451 */
10452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010453f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010454{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010455 long n = 0;
10456 int ic = FALSE;
10457
Bram Moolenaare9a41262005-01-15 22:18:47 +000010458 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010459 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010460 listitem_T *li;
10461 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010462 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010463
Bram Moolenaare9a41262005-01-15 22:18:47 +000010464 if ((l = argvars[0].vval.v_list) != NULL)
10465 {
10466 li = l->lv_first;
10467 if (argvars[2].v_type != VAR_UNKNOWN)
10468 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469 int error = FALSE;
10470
10471 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010472 if (argvars[3].v_type != VAR_UNKNOWN)
10473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 idx = get_tv_number_chk(&argvars[3], &error);
10475 if (!error)
10476 {
10477 li = list_find(l, idx);
10478 if (li == NULL)
10479 EMSGN(_(e_listidx), idx);
10480 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010481 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 if (error)
10483 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010484 }
10485
10486 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010487 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010488 ++n;
10489 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010490 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010491 else if (argvars[0].v_type == VAR_DICT)
10492 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010493 int todo;
10494 dict_T *d;
10495 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010496
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010497 if ((d = argvars[0].vval.v_dict) != NULL)
10498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010499 int error = FALSE;
10500
Bram Moolenaare9a41262005-01-15 22:18:47 +000010501 if (argvars[2].v_type != VAR_UNKNOWN)
10502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010503 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010504 if (argvars[3].v_type != VAR_UNKNOWN)
10505 EMSG(_(e_invarg));
10506 }
10507
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010508 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010509 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010510 {
10511 if (!HASHITEM_EMPTY(hi))
10512 {
10513 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010514 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010515 ++n;
10516 }
10517 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010518 }
10519 }
10520 else
10521 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010522 rettv->vval.v_number = n;
10523}
10524
10525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10527 *
10528 * Checks the existence of a cscope connection.
10529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010531f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532{
10533#ifdef FEAT_CSCOPE
10534 int num = 0;
10535 char_u *dbpath = NULL;
10536 char_u *prepend = NULL;
10537 char_u buf[NUMBUFLEN];
10538
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010539 if (argvars[0].v_type != VAR_UNKNOWN
10540 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542 num = (int)get_tv_number(&argvars[0]);
10543 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010544 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 }
10547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549#endif
10550}
10551
10552/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010553 * "cursor(lnum, col)" function, or
10554 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010556 * Moves the cursor to the specified line and column.
10557 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010560f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561{
10562 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010563#ifdef FEAT_VIRTUALEDIT
10564 long coladd = 0;
10565#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010566 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010568 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010569 if (argvars[1].v_type == VAR_UNKNOWN)
10570 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010571 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010572 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010573
Bram Moolenaar493c1782014-05-28 14:34:46 +020010574 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010575 {
10576 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010577 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010578 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010579 line = pos.lnum;
10580 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010581#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010582 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010583#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010584 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010585 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010586 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010587 set_curswant = FALSE;
10588 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010589 }
10590 else
10591 {
10592 line = get_tv_lnum(argvars);
10593 col = get_tv_number_chk(&argvars[1], NULL);
10594#ifdef FEAT_VIRTUALEDIT
10595 if (argvars[2].v_type != VAR_UNKNOWN)
10596 coladd = get_tv_number_chk(&argvars[2], NULL);
10597#endif
10598 }
10599 if (line < 0 || col < 0
10600#ifdef FEAT_VIRTUALEDIT
10601 || coladd < 0
10602#endif
10603 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010604 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 if (line > 0)
10606 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 if (col > 0)
10608 curwin->w_cursor.col = col - 1;
10609#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010610 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611#endif
10612
10613 /* Make sure the cursor is in a valid position. */
10614 check_cursor();
10615#ifdef FEAT_MBYTE
10616 /* Correct cursor for multi-byte character. */
10617 if (has_mbyte)
10618 mb_adjust_cursor();
10619#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010620
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010621 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010622 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623}
10624
10625/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010626 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 */
10628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010629f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010631 int noref = 0;
10632
10633 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010634 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010635 if (noref < 0 || noref > 1)
10636 EMSG(_(e_invarg));
10637 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010638 {
10639 current_copyID += COPYID_INC;
10640 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642}
10643
10644/*
10645 * "delete()" function
10646 */
10647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010648f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010650 char_u nbuf[NUMBUFLEN];
10651 char_u *name;
10652 char_u *flags;
10653
10654 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010656 return;
10657
10658 name = get_tv_string(&argvars[0]);
10659 if (name == NULL || *name == NUL)
10660 {
10661 EMSG(_(e_invarg));
10662 return;
10663 }
10664
10665 if (argvars[1].v_type != VAR_UNKNOWN)
10666 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010668 flags = (char_u *)"";
10669
10670 if (*flags == NUL)
10671 /* delete a file */
10672 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10673 else if (STRCMP(flags, "d") == 0)
10674 /* delete an empty directory */
10675 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10676 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010677 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010678 rettv->vval.v_number = delete_recursive(name);
10679 else
10680 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681}
10682
10683/*
10684 * "did_filetype()" function
10685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010687f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688{
10689#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010690 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691#endif
10692}
10693
10694/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010695 * "diff_filler()" function
10696 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010698f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010699{
10700#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010701 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010702#endif
10703}
10704
10705/*
10706 * "diff_hlID()" function
10707 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010709f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010710{
10711#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010713 static linenr_T prev_lnum = 0;
10714 static int changedtick = 0;
10715 static int fnum = 0;
10716 static int change_start = 0;
10717 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010718 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010719 int filler_lines;
10720 int col;
10721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010722 if (lnum < 0) /* ignore type error in {lnum} arg */
10723 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010724 if (lnum != prev_lnum
10725 || changedtick != curbuf->b_changedtick
10726 || fnum != curbuf->b_fnum)
10727 {
10728 /* New line, buffer, change: need to get the values. */
10729 filler_lines = diff_check(curwin, lnum);
10730 if (filler_lines < 0)
10731 {
10732 if (filler_lines == -1)
10733 {
10734 change_start = MAXCOL;
10735 change_end = -1;
10736 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10737 hlID = HLF_ADD; /* added line */
10738 else
10739 hlID = HLF_CHD; /* changed line */
10740 }
10741 else
10742 hlID = HLF_ADD; /* added line */
10743 }
10744 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010745 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010746 prev_lnum = lnum;
10747 changedtick = curbuf->b_changedtick;
10748 fnum = curbuf->b_fnum;
10749 }
10750
10751 if (hlID == HLF_CHD || hlID == HLF_TXD)
10752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010753 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010754 if (col >= change_start && col <= change_end)
10755 hlID = HLF_TXD; /* changed text */
10756 else
10757 hlID = HLF_CHD; /* changed line */
10758 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010759 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010760#endif
10761}
10762
10763/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010764 * "disable_char_avail_for_testing({expr})" function
10765 */
10766 static void
10767f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10768{
10769 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10770}
10771
10772/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010773 * "empty({expr})" function
10774 */
10775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010776f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010777{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010778 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010779
10780 switch (argvars[0].v_type)
10781 {
10782 case VAR_STRING:
10783 case VAR_FUNC:
10784 n = argvars[0].vval.v_string == NULL
10785 || *argvars[0].vval.v_string == NUL;
10786 break;
10787 case VAR_NUMBER:
10788 n = argvars[0].vval.v_number == 0;
10789 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010790 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010791#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010792 n = argvars[0].vval.v_float == 0.0;
10793 break;
10794#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010795 case VAR_LIST:
10796 n = argvars[0].vval.v_list == NULL
10797 || argvars[0].vval.v_list->lv_first == NULL;
10798 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010799 case VAR_DICT:
10800 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010801 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010802 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010803 case VAR_SPECIAL:
10804 n = argvars[0].vval.v_number != VVAL_TRUE;
10805 break;
10806
Bram Moolenaar835dc632016-02-07 14:27:38 +010010807 case VAR_JOB:
10808#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010010809 n = argvars[0].vval.v_job == NULL
10810 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10811 break;
10812#endif
10813 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010010814#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010815 n = argvars[0].vval.v_channel == NULL
10816 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010817 break;
10818#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010819 case VAR_UNKNOWN:
10820 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10821 n = TRUE;
10822 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010823 }
10824
10825 rettv->vval.v_number = n;
10826}
10827
10828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 * "escape({string}, {chars})" function
10830 */
10831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010832f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833{
10834 char_u buf[NUMBUFLEN];
10835
Bram Moolenaar758711c2005-02-02 23:11:38 +000010836 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10837 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839}
10840
10841/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010842 * "eval()" function
10843 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010845f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010846{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010847 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 s = get_tv_string_chk(&argvars[0]);
10850 if (s != NULL)
10851 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010852
Bram Moolenaar615b9972015-01-14 17:15:05 +010010853 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010854 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10855 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010856 if (p != NULL && !aborting())
10857 EMSG2(_(e_invexpr2), p);
10858 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010859 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010860 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010861 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010862 else if (*s != NUL)
10863 EMSG(_(e_trailing));
10864}
10865
10866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 * "eventhandler()" function
10868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010870f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873}
10874
10875/*
10876 * "executable()" function
10877 */
10878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010879f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010881 char_u *name = get_tv_string(&argvars[0]);
10882
10883 /* Check in $PATH and also check directly if there is a directory name. */
10884 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10885 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010886}
10887
10888/*
10889 * "exepath()" function
10890 */
10891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010892f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010893{
10894 char_u *p = NULL;
10895
Bram Moolenaarb5971142015-03-21 17:32:19 +010010896 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010897 rettv->v_type = VAR_STRING;
10898 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899}
10900
10901/*
10902 * "exists()" function
10903 */
10904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010905f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906{
10907 char_u *p;
10908 char_u *name;
10909 int n = FALSE;
10910 int len = 0;
10911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 if (*p == '$') /* environment variable */
10914 {
10915 /* first try "normal" environment variables (fast) */
10916 if (mch_getenv(p + 1) != NULL)
10917 n = TRUE;
10918 else
10919 {
10920 /* try expanding things like $VIM and ${HOME} */
10921 p = expand_env_save(p);
10922 if (p != NULL && *p != '$')
10923 n = TRUE;
10924 vim_free(p);
10925 }
10926 }
10927 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010929 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010930 if (*skipwhite(p) != NUL)
10931 n = FALSE; /* trailing garbage */
10932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 else if (*p == '*') /* internal or user defined function */
10934 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010935 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 }
10937 else if (*p == ':')
10938 {
10939 n = cmd_exists(p + 1);
10940 }
10941 else if (*p == '#')
10942 {
10943#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010944 if (p[1] == '#')
10945 n = autocmd_supported(p + 2);
10946 else
10947 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948#endif
10949 }
10950 else /* internal variable */
10951 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010952 char_u *tofree;
10953 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010955 /* get_name_len() takes care of expanding curly braces */
10956 name = p;
10957 len = get_name_len(&p, &tofree, TRUE, FALSE);
10958 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010960 if (tofree != NULL)
10961 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010962 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010963 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010965 /* handle d.key, l[idx], f(expr) */
10966 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10967 if (n)
10968 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 }
10970 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010971 if (*p != NUL)
10972 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010974 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 }
10976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978}
10979
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010980#ifdef FEAT_FLOAT
10981/*
10982 * "exp()" function
10983 */
10984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010985f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010986{
10987 float_T f;
10988
10989 rettv->v_type = VAR_FLOAT;
10990 if (get_float_arg(argvars, &f) == OK)
10991 rettv->vval.v_float = exp(f);
10992 else
10993 rettv->vval.v_float = 0.0;
10994}
10995#endif
10996
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997/*
10998 * "expand()" function
10999 */
11000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011001f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002{
11003 char_u *s;
11004 int len;
11005 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011006 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011008 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011009 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011011 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011012 if (argvars[1].v_type != VAR_UNKNOWN
11013 && argvars[2].v_type != VAR_UNKNOWN
11014 && get_tv_number_chk(&argvars[2], &error)
11015 && !error)
11016 {
11017 rettv->v_type = VAR_LIST;
11018 rettv->vval.v_list = NULL;
11019 }
11020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011021 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 if (*s == '%' || *s == '#' || *s == '<')
11023 {
11024 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011025 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011027 if (rettv->v_type == VAR_LIST)
11028 {
11029 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11030 list_append_string(rettv->vval.v_list, result, -1);
11031 else
11032 vim_free(result);
11033 }
11034 else
11035 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 }
11037 else
11038 {
11039 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011040 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011041 if (argvars[1].v_type != VAR_UNKNOWN
11042 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011043 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011044 if (!error)
11045 {
11046 ExpandInit(&xpc);
11047 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011048 if (p_wic)
11049 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011050 if (rettv->v_type == VAR_STRING)
11051 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11052 options, WILD_ALL);
11053 else if (rettv_list_alloc(rettv) != FAIL)
11054 {
11055 int i;
11056
11057 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11058 for (i = 0; i < xpc.xp_numfiles; i++)
11059 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11060 ExpandCleanup(&xpc);
11061 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011062 }
11063 else
11064 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 }
11066}
11067
11068/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011069 * Go over all entries in "d2" and add them to "d1".
11070 * When "action" is "error" then a duplicate key is an error.
11071 * When "action" is "force" then a duplicate key is overwritten.
11072 * Otherwise duplicate keys are ignored ("action" is "keep").
11073 */
11074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011075dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011076{
11077 dictitem_T *di1;
11078 hashitem_T *hi2;
11079 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011080 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011081
11082 todo = (int)d2->dv_hashtab.ht_used;
11083 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11084 {
11085 if (!HASHITEM_EMPTY(hi2))
11086 {
11087 --todo;
11088 di1 = dict_find(d1, hi2->hi_key, -1);
11089 if (d1->dv_scope != 0)
11090 {
11091 /* Disallow replacing a builtin function in l: and g:.
11092 * Check the key to be valid when adding to any
11093 * scope. */
11094 if (d1->dv_scope == VAR_DEF_SCOPE
11095 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11096 && var_check_func_name(hi2->hi_key,
11097 di1 == NULL))
11098 break;
11099 if (!valid_varname(hi2->hi_key))
11100 break;
11101 }
11102 if (di1 == NULL)
11103 {
11104 di1 = dictitem_copy(HI2DI(hi2));
11105 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11106 dictitem_free(di1);
11107 }
11108 else if (*action == 'e')
11109 {
11110 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11111 break;
11112 }
11113 else if (*action == 'f' && HI2DI(hi2) != di1)
11114 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011115 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11116 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011117 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011118 clear_tv(&di1->di_tv);
11119 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11120 }
11121 }
11122 }
11123}
11124
11125/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011126 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011127 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011128 */
11129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011130f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011131{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011132 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011133
Bram Moolenaare9a41262005-01-15 22:18:47 +000011134 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011135 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011136 list_T *l1, *l2;
11137 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011138 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011139 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011140
Bram Moolenaare9a41262005-01-15 22:18:47 +000011141 l1 = argvars[0].vval.v_list;
11142 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011143 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011144 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011145 {
11146 if (argvars[2].v_type != VAR_UNKNOWN)
11147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 before = get_tv_number_chk(&argvars[2], &error);
11149 if (error)
11150 return; /* type error; errmsg already given */
11151
Bram Moolenaar758711c2005-02-02 23:11:38 +000011152 if (before == l1->lv_len)
11153 item = NULL;
11154 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011155 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011156 item = list_find(l1, before);
11157 if (item == NULL)
11158 {
11159 EMSGN(_(e_listidx), before);
11160 return;
11161 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011162 }
11163 }
11164 else
11165 item = NULL;
11166 list_extend(l1, l2, item);
11167
Bram Moolenaare9a41262005-01-15 22:18:47 +000011168 copy_tv(&argvars[0], rettv);
11169 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011170 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011171 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11172 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011173 dict_T *d1, *d2;
11174 char_u *action;
11175 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011176
11177 d1 = argvars[0].vval.v_dict;
11178 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011179 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011180 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011181 {
11182 /* Check the third argument. */
11183 if (argvars[2].v_type != VAR_UNKNOWN)
11184 {
11185 static char *(av[]) = {"keep", "force", "error"};
11186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 action = get_tv_string_chk(&argvars[2]);
11188 if (action == NULL)
11189 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011190 for (i = 0; i < 3; ++i)
11191 if (STRCMP(action, av[i]) == 0)
11192 break;
11193 if (i == 3)
11194 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011195 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011196 return;
11197 }
11198 }
11199 else
11200 action = (char_u *)"force";
11201
Bram Moolenaara9922d62013-05-30 13:01:18 +020011202 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011203
Bram Moolenaare9a41262005-01-15 22:18:47 +000011204 copy_tv(&argvars[0], rettv);
11205 }
11206 }
11207 else
11208 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011209}
11210
11211/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011212 * "feedkeys()" function
11213 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011215f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011216{
11217 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011218 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011219 char_u *keys, *flags;
11220 char_u nbuf[NUMBUFLEN];
11221 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011222 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011223 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011224
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011225 /* This is not allowed in the sandbox. If the commands would still be
11226 * executed in the sandbox it would be OK, but it probably happens later,
11227 * when "sandbox" is no longer set. */
11228 if (check_secure())
11229 return;
11230
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011231 keys = get_tv_string(&argvars[0]);
11232 if (*keys != NUL)
11233 {
11234 if (argvars[1].v_type != VAR_UNKNOWN)
11235 {
11236 flags = get_tv_string_buf(&argvars[1], nbuf);
11237 for ( ; *flags != NUL; ++flags)
11238 {
11239 switch (*flags)
11240 {
11241 case 'n': remap = FALSE; break;
11242 case 'm': remap = TRUE; break;
11243 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011244 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011245 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011246 }
11247 }
11248 }
11249
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011250 /* Need to escape K_SPECIAL and CSI before putting the string in the
11251 * typeahead buffer. */
11252 keys_esc = vim_strsave_escape_csi(keys);
11253 if (keys_esc != NULL)
11254 {
11255 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011256 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011257 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011258 if (vgetc_busy)
11259 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011260 if (execute)
11261 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011262 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011263 }
11264}
11265
11266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 * "filereadable()" function
11268 */
11269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011270f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011272 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 char_u *p;
11274 int n;
11275
Bram Moolenaarc236c162008-07-13 17:41:49 +000011276#ifndef O_NONBLOCK
11277# define O_NONBLOCK 0
11278#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011280 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11281 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 {
11283 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011284 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 }
11286 else
11287 n = FALSE;
11288
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011289 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290}
11291
11292/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011293 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 * rights to write into.
11295 */
11296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011297f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011299 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300}
11301
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011302 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011303findfilendir(
11304 typval_T *argvars UNUSED,
11305 typval_T *rettv,
11306 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011307{
11308#ifdef FEAT_SEARCHPATH
11309 char_u *fname;
11310 char_u *fresult = NULL;
11311 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11312 char_u *p;
11313 char_u pathbuf[NUMBUFLEN];
11314 int count = 1;
11315 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011316 int error = FALSE;
11317#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011318
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011319 rettv->vval.v_string = NULL;
11320 rettv->v_type = VAR_STRING;
11321
11322#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011324
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011325 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011327 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11328 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011329 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011330 else
11331 {
11332 if (*p != NUL)
11333 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011334
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011335 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011336 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011337 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011338 }
11339
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011340 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11341 error = TRUE;
11342
11343 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011345 do
11346 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011347 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011348 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011349 fresult = find_file_in_path_option(first ? fname : NULL,
11350 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011351 0, first, path,
11352 find_what,
11353 curbuf->b_ffname,
11354 find_what == FINDFILE_DIR
11355 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011356 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011357
11358 if (fresult != NULL && rettv->v_type == VAR_LIST)
11359 list_append_string(rettv->vval.v_list, fresult, -1);
11360
11361 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011362 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011363
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011364 if (rettv->v_type == VAR_STRING)
11365 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011366#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011367}
11368
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011369static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11370static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011371
11372/*
11373 * Implementation of map() and filter().
11374 */
11375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011376filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011377{
11378 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011379 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011380 listitem_T *li, *nli;
11381 list_T *l = NULL;
11382 dictitem_T *di;
11383 hashtab_T *ht;
11384 hashitem_T *hi;
11385 dict_T *d = NULL;
11386 typval_T save_val;
11387 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011388 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011389 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011390 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011391 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011392 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011393 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011394 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011395
Bram Moolenaare9a41262005-01-15 22:18:47 +000011396 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011397 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011398 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011399 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011400 return;
11401 }
11402 else if (argvars[0].v_type == VAR_DICT)
11403 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011404 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011405 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011406 return;
11407 }
11408 else
11409 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011410 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011411 return;
11412 }
11413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 expr = get_tv_string_buf_chk(&argvars[1], buf);
11415 /* On type errors, the preceding call has already displayed an error
11416 * message. Avoid a misleading error message for an empty string that
11417 * was not passed as argument. */
11418 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011420 prepare_vimvar(VV_VAL, &save_val);
11421 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011422
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011423 /* We reset "did_emsg" to be able to detect whether an error
11424 * occurred during evaluation of the expression. */
11425 save_did_emsg = did_emsg;
11426 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011427
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011428 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011429 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011431 vimvars[VV_KEY].vv_type = VAR_STRING;
11432
11433 ht = &d->dv_hashtab;
11434 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011435 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011436 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011438 if (!HASHITEM_EMPTY(hi))
11439 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011440 int r;
11441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011442 --todo;
11443 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011444 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011445 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11446 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011447 break;
11448 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011449 r = filter_map_one(&di->di_tv, expr, map, &rem);
11450 clear_tv(&vimvars[VV_KEY].vv_tv);
11451 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011452 break;
11453 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011454 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011455 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11456 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011457 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011458 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011459 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011460 }
11461 }
11462 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011463 }
11464 else
11465 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011466 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011468 for (li = l->lv_first; li != NULL; li = nli)
11469 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011470 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011471 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011472 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011473 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011474 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011475 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011476 break;
11477 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011478 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011479 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011480 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011481 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011482
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011483 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011484 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011485
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011486 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011487 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011488
11489 copy_tv(&argvars[0], rettv);
11490}
11491
11492 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011493filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011494{
Bram Moolenaar33570922005-01-25 22:26:29 +000011495 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011496 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011497 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011498
Bram Moolenaar33570922005-01-25 22:26:29 +000011499 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011500 s = expr;
11501 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011502 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011503 if (*s != NUL) /* check for trailing chars after expr */
11504 {
11505 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011506 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011507 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011508 }
11509 if (map)
11510 {
11511 /* map(): replace the list item value */
11512 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011513 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011514 *tv = rettv;
11515 }
11516 else
11517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011518 int error = FALSE;
11519
Bram Moolenaare9a41262005-01-15 22:18:47 +000011520 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011521 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011522 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011523 /* On type error, nothing has been removed; return FAIL to stop the
11524 * loop. The error message was given by get_tv_number_chk(). */
11525 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011526 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011527 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011528 retval = OK;
11529theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011530 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011531 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011532}
11533
11534/*
11535 * "filter()" function
11536 */
11537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011538f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011539{
11540 filter_map(argvars, rettv, FALSE);
11541}
11542
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011543/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544 * "finddir({fname}[, {path}[, {count}]])" function
11545 */
11546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011547f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011548{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011549 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011550}
11551
11552/*
11553 * "findfile({fname}[, {path}[, {count}]])" function
11554 */
11555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011556f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011557{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011558 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011559}
11560
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011561#ifdef FEAT_FLOAT
11562/*
11563 * "float2nr({float})" function
11564 */
11565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011566f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011567{
11568 float_T f;
11569
11570 if (get_float_arg(argvars, &f) == OK)
11571 {
11572 if (f < -0x7fffffff)
11573 rettv->vval.v_number = -0x7fffffff;
11574 else if (f > 0x7fffffff)
11575 rettv->vval.v_number = 0x7fffffff;
11576 else
11577 rettv->vval.v_number = (varnumber_T)f;
11578 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011579}
11580
11581/*
11582 * "floor({float})" function
11583 */
11584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011585f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011586{
11587 float_T f;
11588
11589 rettv->v_type = VAR_FLOAT;
11590 if (get_float_arg(argvars, &f) == OK)
11591 rettv->vval.v_float = floor(f);
11592 else
11593 rettv->vval.v_float = 0.0;
11594}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011595
11596/*
11597 * "fmod()" function
11598 */
11599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011600f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011601{
11602 float_T fx, fy;
11603
11604 rettv->v_type = VAR_FLOAT;
11605 if (get_float_arg(argvars, &fx) == OK
11606 && get_float_arg(&argvars[1], &fy) == OK)
11607 rettv->vval.v_float = fmod(fx, fy);
11608 else
11609 rettv->vval.v_float = 0.0;
11610}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011611#endif
11612
Bram Moolenaar0d660222005-01-07 21:51:51 +000011613/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011614 * "fnameescape({string})" function
11615 */
11616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011617f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011618{
11619 rettv->vval.v_string = vim_strsave_fnameescape(
11620 get_tv_string(&argvars[0]), FALSE);
11621 rettv->v_type = VAR_STRING;
11622}
11623
11624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 * "fnamemodify({fname}, {mods})" function
11626 */
11627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011628f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629{
11630 char_u *fname;
11631 char_u *mods;
11632 int usedlen = 0;
11633 int len;
11634 char_u *fbuf = NULL;
11635 char_u buf[NUMBUFLEN];
11636
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011637 fname = get_tv_string_chk(&argvars[0]);
11638 mods = get_tv_string_buf_chk(&argvars[1], buf);
11639 if (fname == NULL || mods == NULL)
11640 fname = NULL;
11641 else
11642 {
11643 len = (int)STRLEN(fname);
11644 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011649 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 vim_free(fbuf);
11653}
11654
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011655static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656
11657/*
11658 * "foldclosed()" function
11659 */
11660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011661foldclosed_both(
11662 typval_T *argvars UNUSED,
11663 typval_T *rettv,
11664 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665{
11666#ifdef FEAT_FOLDING
11667 linenr_T lnum;
11668 linenr_T first, last;
11669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11672 {
11673 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11674 {
11675 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011676 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011678 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 return;
11680 }
11681 }
11682#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684}
11685
11686/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011687 * "foldclosed()" function
11688 */
11689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011690f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011691{
11692 foldclosed_both(argvars, rettv, FALSE);
11693}
11694
11695/*
11696 * "foldclosedend()" function
11697 */
11698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011699f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011700{
11701 foldclosed_both(argvars, rettv, TRUE);
11702}
11703
11704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 * "foldlevel()" function
11706 */
11707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011708f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709{
11710#ifdef FEAT_FOLDING
11711 linenr_T lnum;
11712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011713 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011715 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717}
11718
11719/*
11720 * "foldtext()" function
11721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011723f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724{
11725#ifdef FEAT_FOLDING
11726 linenr_T lnum;
11727 char_u *s;
11728 char_u *r;
11729 int len;
11730 char *txt;
11731#endif
11732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011733 rettv->v_type = VAR_STRING;
11734 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011736 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11737 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11738 <= curbuf->b_ml.ml_line_count
11739 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 {
11741 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011742 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11743 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 {
11745 if (!linewhite(lnum))
11746 break;
11747 ++lnum;
11748 }
11749
11750 /* Find interesting text in this line. */
11751 s = skipwhite(ml_get(lnum));
11752 /* skip C comment-start */
11753 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011754 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011756 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011757 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011758 {
11759 s = skipwhite(ml_get(lnum + 1));
11760 if (*s == '*')
11761 s = skipwhite(s + 1);
11762 }
11763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 txt = _("+-%s%3ld lines: ");
11765 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011766 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767 + 20 /* for %3ld */
11768 + STRLEN(s))); /* concatenated */
11769 if (r != NULL)
11770 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011771 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11772 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11773 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 len = (int)STRLEN(r);
11775 STRCAT(r, s);
11776 /* remove 'foldmarker' and 'commentstring' */
11777 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779 }
11780 }
11781#endif
11782}
11783
11784/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011785 * "foldtextresult(lnum)" function
11786 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011788f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011789{
11790#ifdef FEAT_FOLDING
11791 linenr_T lnum;
11792 char_u *text;
11793 char_u buf[51];
11794 foldinfo_T foldinfo;
11795 int fold_count;
11796#endif
11797
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011798 rettv->v_type = VAR_STRING;
11799 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011800#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011801 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011802 /* treat illegal types and illegal string values for {lnum} the same */
11803 if (lnum < 0)
11804 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011805 fold_count = foldedCount(curwin, lnum, &foldinfo);
11806 if (fold_count > 0)
11807 {
11808 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11809 &foldinfo, buf);
11810 if (text == buf)
11811 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011813 }
11814#endif
11815}
11816
11817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 * "foreground()" function
11819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011821f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823#ifdef FEAT_GUI
11824 if (gui.in_use)
11825 gui_mch_set_foreground();
11826#else
11827# ifdef WIN32
11828 win32_set_foreground();
11829# endif
11830#endif
11831}
11832
11833/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011834 * "function()" function
11835 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011837f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011838{
11839 char_u *s;
11840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011841 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011842 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011843 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011844 /* Don't check an autoload name for existence here. */
11845 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011846 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011847 else
11848 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011849 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011850 {
11851 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011852 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011853
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011854 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11855 * also be called from another script. Using trans_function_name()
11856 * would also work, but some plugins depend on the name being
11857 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011858 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011859 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011860 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011861 if (rettv->vval.v_string != NULL)
11862 {
11863 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011864 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011865 }
11866 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011867 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011868 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011869 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011870 }
11871}
11872
11873/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011874 * "garbagecollect()" function
11875 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011877f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011878{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011879 /* This is postponed until we are back at the toplevel, because we may be
11880 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11881 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011882
11883 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11884 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011885}
11886
11887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011888 * "get()" function
11889 */
11890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011891f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011892{
Bram Moolenaar33570922005-01-25 22:26:29 +000011893 listitem_T *li;
11894 list_T *l;
11895 dictitem_T *di;
11896 dict_T *d;
11897 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011898
Bram Moolenaare9a41262005-01-15 22:18:47 +000011899 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011900 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011901 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011902 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 int error = FALSE;
11904
11905 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11906 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011907 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011908 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011909 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011910 else if (argvars[0].v_type == VAR_DICT)
11911 {
11912 if ((d = argvars[0].vval.v_dict) != NULL)
11913 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011914 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011915 if (di != NULL)
11916 tv = &di->di_tv;
11917 }
11918 }
11919 else
11920 EMSG2(_(e_listdictarg), "get()");
11921
11922 if (tv == NULL)
11923 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011924 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011925 copy_tv(&argvars[2], rettv);
11926 }
11927 else
11928 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011929}
11930
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011931static 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 +000011932
11933/*
11934 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011935 * Return a range (from start to end) of lines in rettv from the specified
11936 * buffer.
11937 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011938 */
11939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011940get_buffer_lines(
11941 buf_T *buf,
11942 linenr_T start,
11943 linenr_T end,
11944 int retlist,
11945 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011946{
11947 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011948
Bram Moolenaar959a1432013-12-14 12:17:38 +010011949 rettv->v_type = VAR_STRING;
11950 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011951 if (retlist && rettv_list_alloc(rettv) == FAIL)
11952 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011953
11954 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11955 return;
11956
11957 if (!retlist)
11958 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011959 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11960 p = ml_get_buf(buf, start, FALSE);
11961 else
11962 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011963 rettv->vval.v_string = vim_strsave(p);
11964 }
11965 else
11966 {
11967 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011968 return;
11969
11970 if (start < 1)
11971 start = 1;
11972 if (end > buf->b_ml.ml_line_count)
11973 end = buf->b_ml.ml_line_count;
11974 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011975 if (list_append_string(rettv->vval.v_list,
11976 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011977 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011978 }
11979}
11980
11981/*
11982 * "getbufline()" function
11983 */
11984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011985f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011986{
11987 linenr_T lnum;
11988 linenr_T end;
11989 buf_T *buf;
11990
11991 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11992 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011993 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011994 --emsg_off;
11995
Bram Moolenaar661b1822005-07-28 22:36:45 +000011996 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011997 if (argvars[2].v_type == VAR_UNKNOWN)
11998 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011999 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012000 end = get_tv_lnum_buf(&argvars[2], buf);
12001
Bram Moolenaar342337a2005-07-21 21:11:17 +000012002 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012003}
12004
Bram Moolenaar0d660222005-01-07 21:51:51 +000012005/*
12006 * "getbufvar()" function
12007 */
12008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012009f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012010{
12011 buf_T *buf;
12012 buf_T *save_curbuf;
12013 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012014 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012015 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012017 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12018 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012019 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012020 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012021
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012022 rettv->v_type = VAR_STRING;
12023 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012024
12025 if (buf != NULL && varname != NULL)
12026 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012027 /* set curbuf to be our buf, temporarily */
12028 save_curbuf = curbuf;
12029 curbuf = buf;
12030
Bram Moolenaar0d660222005-01-07 21:51:51 +000012031 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012032 {
12033 if (get_option_tv(&varname, rettv, TRUE) == OK)
12034 done = TRUE;
12035 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012036 else if (STRCMP(varname, "changedtick") == 0)
12037 {
12038 rettv->v_type = VAR_NUMBER;
12039 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012040 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012041 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012042 else
12043 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012044 /* Look up the variable. */
12045 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12046 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12047 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012048 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012049 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012050 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012051 done = TRUE;
12052 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012053 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012054
12055 /* restore previous notion of curbuf */
12056 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012057 }
12058
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012059 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12060 /* use the default value */
12061 copy_tv(&argvars[2], rettv);
12062
Bram Moolenaar0d660222005-01-07 21:51:51 +000012063 --emsg_off;
12064}
12065
12066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 * "getchar()" function
12068 */
12069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012070f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071{
12072 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012073 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012075 /* Position the cursor. Needed after a message that ends in a space. */
12076 windgoto(msg_row, msg_col);
12077
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078 ++no_mapping;
12079 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012080 for (;;)
12081 {
12082 if (argvars[0].v_type == VAR_UNKNOWN)
12083 /* getchar(): blocking wait. */
12084 n = safe_vgetc();
12085 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12086 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012087 n = vpeekc_any();
12088 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012089 /* illegal argument or getchar(0) and no char avail: return zero */
12090 n = 0;
12091 else
12092 /* getchar(0) and char avail: return char */
12093 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012094
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012095 if (n == K_IGNORE)
12096 continue;
12097 break;
12098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099 --no_mapping;
12100 --allow_keys;
12101
Bram Moolenaar219b8702006-11-01 14:32:36 +000012102 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12103 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12104 vimvars[VV_MOUSE_COL].vv_nr = 0;
12105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 if (IS_SPECIAL(n) || mod_mask != 0)
12108 {
12109 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12110 int i = 0;
12111
12112 /* Turn a special key into three bytes, plus modifier. */
12113 if (mod_mask != 0)
12114 {
12115 temp[i++] = K_SPECIAL;
12116 temp[i++] = KS_MODIFIER;
12117 temp[i++] = mod_mask;
12118 }
12119 if (IS_SPECIAL(n))
12120 {
12121 temp[i++] = K_SPECIAL;
12122 temp[i++] = K_SECOND(n);
12123 temp[i++] = K_THIRD(n);
12124 }
12125#ifdef FEAT_MBYTE
12126 else if (has_mbyte)
12127 i += (*mb_char2bytes)(n, temp + i);
12128#endif
12129 else
12130 temp[i++] = n;
12131 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012132 rettv->v_type = VAR_STRING;
12133 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012134
12135#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012136 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012137 {
12138 int row = mouse_row;
12139 int col = mouse_col;
12140 win_T *win;
12141 linenr_T lnum;
12142# ifdef FEAT_WINDOWS
12143 win_T *wp;
12144# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012145 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012146
12147 if (row >= 0 && col >= 0)
12148 {
12149 /* Find the window at the mouse coordinates and compute the
12150 * text position. */
12151 win = mouse_find_win(&row, &col);
12152 (void)mouse_comp_pos(win, &row, &col, &lnum);
12153# ifdef FEAT_WINDOWS
12154 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012155 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012156# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012157 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012158 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12159 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12160 }
12161 }
12162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163 }
12164}
12165
12166/*
12167 * "getcharmod()" function
12168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012170f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173}
12174
12175/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012176 * "getcharsearch()" function
12177 */
12178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012179f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012180{
12181 if (rettv_dict_alloc(rettv) != FAIL)
12182 {
12183 dict_T *dict = rettv->vval.v_dict;
12184
12185 dict_add_nr_str(dict, "char", 0L, last_csearch());
12186 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12187 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12188 }
12189}
12190
12191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192 * "getcmdline()" function
12193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012195f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012197 rettv->v_type = VAR_STRING;
12198 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199}
12200
12201/*
12202 * "getcmdpos()" function
12203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012205f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012207 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208}
12209
12210/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012211 * "getcmdtype()" function
12212 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012214f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012215{
12216 rettv->v_type = VAR_STRING;
12217 rettv->vval.v_string = alloc(2);
12218 if (rettv->vval.v_string != NULL)
12219 {
12220 rettv->vval.v_string[0] = get_cmdline_type();
12221 rettv->vval.v_string[1] = NUL;
12222 }
12223}
12224
12225/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012226 * "getcmdwintype()" function
12227 */
12228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012229f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012230{
12231 rettv->v_type = VAR_STRING;
12232 rettv->vval.v_string = NULL;
12233#ifdef FEAT_CMDWIN
12234 rettv->vval.v_string = alloc(2);
12235 if (rettv->vval.v_string != NULL)
12236 {
12237 rettv->vval.v_string[0] = cmdwin_type;
12238 rettv->vval.v_string[1] = NUL;
12239 }
12240#endif
12241}
12242
12243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 * "getcwd()" function
12245 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012247f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012249 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012250 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012252 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012253 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012254
12255 wp = find_tabwin(&argvars[0], &argvars[1]);
12256 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012258 if (wp->w_localdir != NULL)
12259 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12260 else if(globaldir != NULL)
12261 rettv->vval.v_string = vim_strsave(globaldir);
12262 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012263 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012264 cwd = alloc(MAXPATHL);
12265 if (cwd != NULL)
12266 {
12267 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12268 rettv->vval.v_string = vim_strsave(cwd);
12269 vim_free(cwd);
12270 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012271 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012272#ifdef BACKSLASH_IN_FILENAME
12273 if (rettv->vval.v_string != NULL)
12274 slash_adjust(rettv->vval.v_string);
12275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276 }
12277}
12278
12279/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012280 * "getfontname()" function
12281 */
12282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012283f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012284{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012285 rettv->v_type = VAR_STRING;
12286 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012287#ifdef FEAT_GUI
12288 if (gui.in_use)
12289 {
12290 GuiFont font;
12291 char_u *name = NULL;
12292
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012293 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012294 {
12295 /* Get the "Normal" font. Either the name saved by
12296 * hl_set_font_name() or from the font ID. */
12297 font = gui.norm_font;
12298 name = hl_get_font_name();
12299 }
12300 else
12301 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012302 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012303 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12304 return;
12305 font = gui_mch_get_font(name, FALSE);
12306 if (font == NOFONT)
12307 return; /* Invalid font name, return empty string. */
12308 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012309 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012310 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012311 gui_mch_free_font(font);
12312 }
12313#endif
12314}
12315
12316/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012317 * "getfperm({fname})" function
12318 */
12319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012320f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012321{
12322 char_u *fname;
12323 struct stat st;
12324 char_u *perm = NULL;
12325 char_u flags[] = "rwx";
12326 int i;
12327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012328 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012329
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012331 if (mch_stat((char *)fname, &st) >= 0)
12332 {
12333 perm = vim_strsave((char_u *)"---------");
12334 if (perm != NULL)
12335 {
12336 for (i = 0; i < 9; i++)
12337 {
12338 if (st.st_mode & (1 << (8 - i)))
12339 perm[i] = flags[i % 3];
12340 }
12341 }
12342 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012344}
12345
12346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 * "getfsize({fname})" function
12348 */
12349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012350f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351{
12352 char_u *fname;
12353 struct stat st;
12354
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012355 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012357 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358
12359 if (mch_stat((char *)fname, &st) >= 0)
12360 {
12361 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012362 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012365 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012366
12367 /* non-perfect check for overflow */
12368 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12369 rettv->vval.v_number = -2;
12370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371 }
12372 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012373 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374}
12375
12376/*
12377 * "getftime({fname})" function
12378 */
12379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012380f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381{
12382 char_u *fname;
12383 struct stat st;
12384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012385 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386
12387 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012388 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012390 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391}
12392
12393/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012394 * "getftype({fname})" function
12395 */
12396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012397f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012398{
12399 char_u *fname;
12400 struct stat st;
12401 char_u *type = NULL;
12402 char *t;
12403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012404 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012407 if (mch_lstat((char *)fname, &st) >= 0)
12408 {
12409#ifdef S_ISREG
12410 if (S_ISREG(st.st_mode))
12411 t = "file";
12412 else if (S_ISDIR(st.st_mode))
12413 t = "dir";
12414# ifdef S_ISLNK
12415 else if (S_ISLNK(st.st_mode))
12416 t = "link";
12417# endif
12418# ifdef S_ISBLK
12419 else if (S_ISBLK(st.st_mode))
12420 t = "bdev";
12421# endif
12422# ifdef S_ISCHR
12423 else if (S_ISCHR(st.st_mode))
12424 t = "cdev";
12425# endif
12426# ifdef S_ISFIFO
12427 else if (S_ISFIFO(st.st_mode))
12428 t = "fifo";
12429# endif
12430# ifdef S_ISSOCK
12431 else if (S_ISSOCK(st.st_mode))
12432 t = "fifo";
12433# endif
12434 else
12435 t = "other";
12436#else
12437# ifdef S_IFMT
12438 switch (st.st_mode & S_IFMT)
12439 {
12440 case S_IFREG: t = "file"; break;
12441 case S_IFDIR: t = "dir"; break;
12442# ifdef S_IFLNK
12443 case S_IFLNK: t = "link"; break;
12444# endif
12445# ifdef S_IFBLK
12446 case S_IFBLK: t = "bdev"; break;
12447# endif
12448# ifdef S_IFCHR
12449 case S_IFCHR: t = "cdev"; break;
12450# endif
12451# ifdef S_IFIFO
12452 case S_IFIFO: t = "fifo"; break;
12453# endif
12454# ifdef S_IFSOCK
12455 case S_IFSOCK: t = "socket"; break;
12456# endif
12457 default: t = "other";
12458 }
12459# else
12460 if (mch_isdir(fname))
12461 t = "dir";
12462 else
12463 t = "file";
12464# endif
12465#endif
12466 type = vim_strsave((char_u *)t);
12467 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012469}
12470
12471/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012472 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012473 */
12474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012475f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012476{
12477 linenr_T lnum;
12478 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012479 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012480
12481 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012482 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012483 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012484 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012485 retlist = FALSE;
12486 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012487 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012488 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012489 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012490 retlist = TRUE;
12491 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012492
Bram Moolenaar342337a2005-07-21 21:11:17 +000012493 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012494}
12495
12496/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012497 * "getmatches()" function
12498 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012500f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012501{
12502#ifdef FEAT_SEARCH_EXTRA
12503 dict_T *dict;
12504 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012505 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012506
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012507 if (rettv_list_alloc(rettv) == OK)
12508 {
12509 while (cur != NULL)
12510 {
12511 dict = dict_alloc();
12512 if (dict == NULL)
12513 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012514 if (cur->match.regprog == NULL)
12515 {
12516 /* match added with matchaddpos() */
12517 for (i = 0; i < MAXPOSMATCH; ++i)
12518 {
12519 llpos_T *llpos;
12520 char buf[6];
12521 list_T *l;
12522
12523 llpos = &cur->pos.pos[i];
12524 if (llpos->lnum == 0)
12525 break;
12526 l = list_alloc();
12527 if (l == NULL)
12528 break;
12529 list_append_number(l, (varnumber_T)llpos->lnum);
12530 if (llpos->col > 0)
12531 {
12532 list_append_number(l, (varnumber_T)llpos->col);
12533 list_append_number(l, (varnumber_T)llpos->len);
12534 }
12535 sprintf(buf, "pos%d", i + 1);
12536 dict_add_list(dict, buf, l);
12537 }
12538 }
12539 else
12540 {
12541 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12542 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012543 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012544 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12545 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012546# ifdef FEAT_CONCEAL
12547 if (cur->conceal_char)
12548 {
12549 char_u buf[MB_MAXBYTES + 1];
12550
12551 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12552 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12553 }
12554# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012555 list_append_dict(rettv->vval.v_list, dict);
12556 cur = cur->next;
12557 }
12558 }
12559#endif
12560}
12561
12562/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012563 * "getpid()" function
12564 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012566f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012567{
12568 rettv->vval.v_number = mch_get_pid();
12569}
12570
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012571static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012572
12573/*
12574 * "getcurpos()" function
12575 */
12576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012577f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012578{
12579 getpos_both(argvars, rettv, TRUE);
12580}
12581
Bram Moolenaar18081e32008-02-20 19:11:07 +000012582/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012583 * "getpos(string)" function
12584 */
12585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012586f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012587{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012588 getpos_both(argvars, rettv, FALSE);
12589}
12590
12591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012592getpos_both(
12593 typval_T *argvars,
12594 typval_T *rettv,
12595 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012596{
Bram Moolenaara5525202006-03-02 22:52:09 +000012597 pos_T *fp;
12598 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012599 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012600
12601 if (rettv_list_alloc(rettv) == OK)
12602 {
12603 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012604 if (getcurpos)
12605 fp = &curwin->w_cursor;
12606 else
12607 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012608 if (fnum != -1)
12609 list_append_number(l, (varnumber_T)fnum);
12610 else
12611 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012612 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12613 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012614 list_append_number(l, (fp != NULL)
12615 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012616 : (varnumber_T)0);
12617 list_append_number(l,
12618#ifdef FEAT_VIRTUALEDIT
12619 (fp != NULL) ? (varnumber_T)fp->coladd :
12620#endif
12621 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012622 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012623 {
12624 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012625 list_append_number(l, curwin->w_curswant == MAXCOL ?
12626 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012627 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012628 }
12629 else
12630 rettv->vval.v_number = FALSE;
12631}
12632
12633/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012634 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012635 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012637f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012638{
12639#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012640 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012641#endif
12642
Bram Moolenaar2641f772005-03-25 21:58:17 +000012643#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012644 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012645 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012646 wp = NULL;
12647 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12648 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012649 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012650 if (wp == NULL)
12651 return;
12652 }
12653
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012654 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012655 }
12656#endif
12657}
12658
12659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660 * "getreg()" function
12661 */
12662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012663f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664{
12665 char_u *strregname;
12666 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012667 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012668 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012669 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012671 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012673 strregname = get_tv_string_chk(&argvars[0]);
12674 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012675 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012677 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012678 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12679 return_list = get_tv_number_chk(&argvars[2], &error);
12680 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012683 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012684
12685 if (error)
12686 return;
12687
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 regname = (strregname == NULL ? '"' : *strregname);
12689 if (regname == 0)
12690 regname = '"';
12691
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012692 if (return_list)
12693 {
12694 rettv->v_type = VAR_LIST;
12695 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12696 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012697 if (rettv->vval.v_list != NULL)
12698 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012699 }
12700 else
12701 {
12702 rettv->v_type = VAR_STRING;
12703 rettv->vval.v_string = get_reg_contents(regname,
12704 arg2 ? GREG_EXPR_SRC : 0);
12705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706}
12707
12708/*
12709 * "getregtype()" function
12710 */
12711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012712f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713{
12714 char_u *strregname;
12715 int regname;
12716 char_u buf[NUMBUFLEN + 2];
12717 long reglen = 0;
12718
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012719 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012720 {
12721 strregname = get_tv_string_chk(&argvars[0]);
12722 if (strregname == NULL) /* type error; errmsg already given */
12723 {
12724 rettv->v_type = VAR_STRING;
12725 rettv->vval.v_string = NULL;
12726 return;
12727 }
12728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 else
12730 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012731 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732
12733 regname = (strregname == NULL ? '"' : *strregname);
12734 if (regname == 0)
12735 regname = '"';
12736
12737 buf[0] = NUL;
12738 buf[1] = NUL;
12739 switch (get_reg_type(regname, &reglen))
12740 {
12741 case MLINE: buf[0] = 'V'; break;
12742 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 case MBLOCK:
12744 buf[0] = Ctrl_V;
12745 sprintf((char *)buf + 1, "%ld", reglen + 1);
12746 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012748 rettv->v_type = VAR_STRING;
12749 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750}
12751
12752/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012753 * "gettabvar()" function
12754 */
12755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012756f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012757{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012758 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012759 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012760 dictitem_T *v;
12761 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012762 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012763
12764 rettv->v_type = VAR_STRING;
12765 rettv->vval.v_string = NULL;
12766
12767 varname = get_tv_string_chk(&argvars[1]);
12768 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12769 if (tp != NULL && varname != NULL)
12770 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012771 /* Set tp to be our tabpage, temporarily. Also set the window to the
12772 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012773 if (switch_win(&oldcurwin, &oldtabpage,
12774 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012775 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012776 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012777 /* look up the variable */
12778 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12779 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12780 if (v != NULL)
12781 {
12782 copy_tv(&v->di_tv, rettv);
12783 done = TRUE;
12784 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012785 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012786
12787 /* restore previous notion of curwin */
12788 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012789 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012790
12791 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012792 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012793}
12794
12795/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012796 * "gettabwinvar()" function
12797 */
12798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012799f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012800{
12801 getwinvar(argvars, rettv, 1);
12802}
12803
12804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 * "getwinposx()" function
12806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012808f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811#ifdef FEAT_GUI
12812 if (gui.in_use)
12813 {
12814 int x, y;
12815
12816 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 }
12819#endif
12820}
12821
12822/*
12823 * "getwinposy()" function
12824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012826f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829#ifdef FEAT_GUI
12830 if (gui.in_use)
12831 {
12832 int x, y;
12833
12834 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836 }
12837#endif
12838}
12839
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012840/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012841 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012842 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012843 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012844find_win_by_nr(
12845 typval_T *vp,
12846 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012847{
12848#ifdef FEAT_WINDOWS
12849 win_T *wp;
12850#endif
12851 int nr;
12852
12853 nr = get_tv_number_chk(vp, NULL);
12854
12855#ifdef FEAT_WINDOWS
12856 if (nr < 0)
12857 return NULL;
12858 if (nr == 0)
12859 return curwin;
12860
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012861 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12862 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012863 if (--nr <= 0)
12864 break;
12865 return wp;
12866#else
12867 if (nr == 0 || nr == 1)
12868 return curwin;
12869 return NULL;
12870#endif
12871}
12872
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012874 * Find window specified by "wvp" in tabpage "tvp".
12875 */
12876 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012877find_tabwin(
12878 typval_T *wvp, /* VAR_UNKNOWN for current window */
12879 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012880{
12881 win_T *wp = NULL;
12882 tabpage_T *tp = NULL;
12883 long n;
12884
12885 if (wvp->v_type != VAR_UNKNOWN)
12886 {
12887 if (tvp->v_type != VAR_UNKNOWN)
12888 {
12889 n = get_tv_number(tvp);
12890 if (n >= 0)
12891 tp = find_tabpage(n);
12892 }
12893 else
12894 tp = curtab;
12895
12896 if (tp != NULL)
12897 wp = find_win_by_nr(wvp, tp);
12898 }
12899 else
12900 wp = curwin;
12901
12902 return wp;
12903}
12904
12905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906 * "getwinvar()" function
12907 */
12908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012909f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012911 getwinvar(argvars, rettv, 0);
12912}
12913
12914/*
12915 * getwinvar() and gettabwinvar()
12916 */
12917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012918getwinvar(
12919 typval_T *argvars,
12920 typval_T *rettv,
12921 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012922{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012923 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012925 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012926 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012927 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012928#ifdef FEAT_WINDOWS
12929 win_T *oldcurwin;
12930 tabpage_T *oldtabpage;
12931 int need_switch_win;
12932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012934#ifdef FEAT_WINDOWS
12935 if (off == 1)
12936 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12937 else
12938 tp = curtab;
12939#endif
12940 win = find_win_by_nr(&argvars[off], tp);
12941 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012942 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012944 rettv->v_type = VAR_STRING;
12945 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946
12947 if (win != NULL && varname != NULL)
12948 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012949#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012950 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012951 * otherwise the window is not valid. Only do this when needed,
12952 * autocommands get blocked. */
12953 need_switch_win = !(tp == curtab && win == curwin);
12954 if (!need_switch_win
12955 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12956#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012957 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012958 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012959 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012960 if (get_option_tv(&varname, rettv, 1) == OK)
12961 done = TRUE;
12962 }
12963 else
12964 {
12965 /* Look up the variable. */
12966 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12967 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12968 varname, FALSE);
12969 if (v != NULL)
12970 {
12971 copy_tv(&v->di_tv, rettv);
12972 done = TRUE;
12973 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012976
Bram Moolenaarba117c22015-09-29 16:53:22 +020012977#ifdef FEAT_WINDOWS
12978 if (need_switch_win)
12979 /* restore previous notion of curwin */
12980 restore_win(oldcurwin, oldtabpage, TRUE);
12981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982 }
12983
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012984 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12985 /* use the default return value */
12986 copy_tv(&argvars[off + 2], rettv);
12987
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988 --emsg_off;
12989}
12990
12991/*
12992 * "glob()" function
12993 */
12994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012995f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012997 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012999 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013001 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013002 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013003 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013004 if (argvars[1].v_type != VAR_UNKNOWN)
13005 {
13006 if (get_tv_number_chk(&argvars[1], &error))
13007 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013008 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013009 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013010 if (get_tv_number_chk(&argvars[2], &error))
13011 {
13012 rettv->v_type = VAR_LIST;
13013 rettv->vval.v_list = NULL;
13014 }
13015 if (argvars[3].v_type != VAR_UNKNOWN
13016 && get_tv_number_chk(&argvars[3], &error))
13017 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013018 }
13019 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013020 if (!error)
13021 {
13022 ExpandInit(&xpc);
13023 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013024 if (p_wic)
13025 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013026 if (rettv->v_type == VAR_STRING)
13027 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013028 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013029 else if (rettv_list_alloc(rettv) != FAIL)
13030 {
13031 int i;
13032
13033 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13034 NULL, options, WILD_ALL_KEEP);
13035 for (i = 0; i < xpc.xp_numfiles; i++)
13036 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13037
13038 ExpandCleanup(&xpc);
13039 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013040 }
13041 else
13042 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043}
13044
13045/*
13046 * "globpath()" function
13047 */
13048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013049f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013051 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013053 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013054 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013055 garray_T ga;
13056 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013058 /* When the optional second argument is non-zero, don't remove matches
13059 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013061 if (argvars[2].v_type != VAR_UNKNOWN)
13062 {
13063 if (get_tv_number_chk(&argvars[2], &error))
13064 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013065 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013066 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013067 if (get_tv_number_chk(&argvars[3], &error))
13068 {
13069 rettv->v_type = VAR_LIST;
13070 rettv->vval.v_list = NULL;
13071 }
13072 if (argvars[4].v_type != VAR_UNKNOWN
13073 && get_tv_number_chk(&argvars[4], &error))
13074 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013075 }
13076 }
13077 if (file != NULL && !error)
13078 {
13079 ga_init2(&ga, (int)sizeof(char_u *), 10);
13080 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13081 if (rettv->v_type == VAR_STRING)
13082 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13083 else if (rettv_list_alloc(rettv) != FAIL)
13084 for (i = 0; i < ga.ga_len; ++i)
13085 list_append_string(rettv->vval.v_list,
13086 ((char_u **)(ga.ga_data))[i], -1);
13087 ga_clear_strings(&ga);
13088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013089 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013090 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091}
13092
13093/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013094 * "glob2regpat()" function
13095 */
13096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013097f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013098{
13099 char_u *pat = get_tv_string_chk(&argvars[0]);
13100
13101 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013102 rettv->vval.v_string = (pat == NULL)
13103 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013104}
13105
13106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 * "has()" function
13108 */
13109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013110f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111{
13112 int i;
13113 char_u *name;
13114 int n = FALSE;
13115 static char *(has_list[]) =
13116 {
13117#ifdef AMIGA
13118 "amiga",
13119# ifdef FEAT_ARP
13120 "arp",
13121# endif
13122#endif
13123#ifdef __BEOS__
13124 "beos",
13125#endif
13126#ifdef MSDOS
13127# ifdef DJGPP
13128 "dos32",
13129# else
13130 "dos16",
13131# endif
13132#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013133#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134 "mac",
13135#endif
13136#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013137 "macunix", /* built with 'darwin' enabled */
13138#endif
13139#if defined(__APPLE__) && __APPLE__ == 1
13140 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142#ifdef __QNX__
13143 "qnx",
13144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145#ifdef UNIX
13146 "unix",
13147#endif
13148#ifdef VMS
13149 "vms",
13150#endif
13151#ifdef WIN16
13152 "win16",
13153#endif
13154#ifdef WIN32
13155 "win32",
13156#endif
13157#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13158 "win32unix",
13159#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013160#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161 "win64",
13162#endif
13163#ifdef EBCDIC
13164 "ebcdic",
13165#endif
13166#ifndef CASE_INSENSITIVE_FILENAME
13167 "fname_case",
13168#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013169#ifdef HAVE_ACL
13170 "acl",
13171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172#ifdef FEAT_ARABIC
13173 "arabic",
13174#endif
13175#ifdef FEAT_AUTOCMD
13176 "autocmd",
13177#endif
13178#ifdef FEAT_BEVAL
13179 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013180# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13181 "balloon_multiline",
13182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183#endif
13184#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13185 "builtin_terms",
13186# ifdef ALL_BUILTIN_TCAPS
13187 "all_builtin_terms",
13188# endif
13189#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013190#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13191 || defined(FEAT_GUI_W32) \
13192 || defined(FEAT_GUI_MOTIF))
13193 "browsefilter",
13194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195#ifdef FEAT_BYTEOFF
13196 "byte_offset",
13197#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013198#ifdef FEAT_CHANNEL
13199 "channel",
13200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201#ifdef FEAT_CINDENT
13202 "cindent",
13203#endif
13204#ifdef FEAT_CLIENTSERVER
13205 "clientserver",
13206#endif
13207#ifdef FEAT_CLIPBOARD
13208 "clipboard",
13209#endif
13210#ifdef FEAT_CMDL_COMPL
13211 "cmdline_compl",
13212#endif
13213#ifdef FEAT_CMDHIST
13214 "cmdline_hist",
13215#endif
13216#ifdef FEAT_COMMENTS
13217 "comments",
13218#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013219#ifdef FEAT_CONCEAL
13220 "conceal",
13221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222#ifdef FEAT_CRYPT
13223 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013224 "crypt-blowfish",
13225 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226#endif
13227#ifdef FEAT_CSCOPE
13228 "cscope",
13229#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013230#ifdef FEAT_CURSORBIND
13231 "cursorbind",
13232#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013233#ifdef CURSOR_SHAPE
13234 "cursorshape",
13235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236#ifdef DEBUG
13237 "debug",
13238#endif
13239#ifdef FEAT_CON_DIALOG
13240 "dialog_con",
13241#endif
13242#ifdef FEAT_GUI_DIALOG
13243 "dialog_gui",
13244#endif
13245#ifdef FEAT_DIFF
13246 "diff",
13247#endif
13248#ifdef FEAT_DIGRAPHS
13249 "digraphs",
13250#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013251#ifdef FEAT_DIRECTX
13252 "directx",
13253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254#ifdef FEAT_DND
13255 "dnd",
13256#endif
13257#ifdef FEAT_EMACS_TAGS
13258 "emacs_tags",
13259#endif
13260 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013261 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262#ifdef FEAT_SEARCH_EXTRA
13263 "extra_search",
13264#endif
13265#ifdef FEAT_FKMAP
13266 "farsi",
13267#endif
13268#ifdef FEAT_SEARCHPATH
13269 "file_in_path",
13270#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013271#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013272 "filterpipe",
13273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274#ifdef FEAT_FIND_ID
13275 "find_in_path",
13276#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013277#ifdef FEAT_FLOAT
13278 "float",
13279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280#ifdef FEAT_FOLDING
13281 "folding",
13282#endif
13283#ifdef FEAT_FOOTER
13284 "footer",
13285#endif
13286#if !defined(USE_SYSTEM) && defined(UNIX)
13287 "fork",
13288#endif
13289#ifdef FEAT_GETTEXT
13290 "gettext",
13291#endif
13292#ifdef FEAT_GUI
13293 "gui",
13294#endif
13295#ifdef FEAT_GUI_ATHENA
13296# ifdef FEAT_GUI_NEXTAW
13297 "gui_neXtaw",
13298# else
13299 "gui_athena",
13300# endif
13301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302#ifdef FEAT_GUI_GTK
13303 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013306#ifdef FEAT_GUI_GNOME
13307 "gui_gnome",
13308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309#ifdef FEAT_GUI_MAC
13310 "gui_mac",
13311#endif
13312#ifdef FEAT_GUI_MOTIF
13313 "gui_motif",
13314#endif
13315#ifdef FEAT_GUI_PHOTON
13316 "gui_photon",
13317#endif
13318#ifdef FEAT_GUI_W16
13319 "gui_win16",
13320#endif
13321#ifdef FEAT_GUI_W32
13322 "gui_win32",
13323#endif
13324#ifdef FEAT_HANGULIN
13325 "hangul_input",
13326#endif
13327#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13328 "iconv",
13329#endif
13330#ifdef FEAT_INS_EXPAND
13331 "insert_expand",
13332#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013333#ifdef FEAT_JOB
13334 "job",
13335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336#ifdef FEAT_JUMPLIST
13337 "jumplist",
13338#endif
13339#ifdef FEAT_KEYMAP
13340 "keymap",
13341#endif
13342#ifdef FEAT_LANGMAP
13343 "langmap",
13344#endif
13345#ifdef FEAT_LIBCALL
13346 "libcall",
13347#endif
13348#ifdef FEAT_LINEBREAK
13349 "linebreak",
13350#endif
13351#ifdef FEAT_LISP
13352 "lispindent",
13353#endif
13354#ifdef FEAT_LISTCMDS
13355 "listcmds",
13356#endif
13357#ifdef FEAT_LOCALMAP
13358 "localmap",
13359#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013360#ifdef FEAT_LUA
13361# ifndef DYNAMIC_LUA
13362 "lua",
13363# endif
13364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365#ifdef FEAT_MENU
13366 "menu",
13367#endif
13368#ifdef FEAT_SESSION
13369 "mksession",
13370#endif
13371#ifdef FEAT_MODIFY_FNAME
13372 "modify_fname",
13373#endif
13374#ifdef FEAT_MOUSE
13375 "mouse",
13376#endif
13377#ifdef FEAT_MOUSESHAPE
13378 "mouseshape",
13379#endif
13380#if defined(UNIX) || defined(VMS)
13381# ifdef FEAT_MOUSE_DEC
13382 "mouse_dec",
13383# endif
13384# ifdef FEAT_MOUSE_GPM
13385 "mouse_gpm",
13386# endif
13387# ifdef FEAT_MOUSE_JSB
13388 "mouse_jsbterm",
13389# endif
13390# ifdef FEAT_MOUSE_NET
13391 "mouse_netterm",
13392# endif
13393# ifdef FEAT_MOUSE_PTERM
13394 "mouse_pterm",
13395# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013396# ifdef FEAT_MOUSE_SGR
13397 "mouse_sgr",
13398# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013399# ifdef FEAT_SYSMOUSE
13400 "mouse_sysmouse",
13401# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013402# ifdef FEAT_MOUSE_URXVT
13403 "mouse_urxvt",
13404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405# ifdef FEAT_MOUSE_XTERM
13406 "mouse_xterm",
13407# endif
13408#endif
13409#ifdef FEAT_MBYTE
13410 "multi_byte",
13411#endif
13412#ifdef FEAT_MBYTE_IME
13413 "multi_byte_ime",
13414#endif
13415#ifdef FEAT_MULTI_LANG
13416 "multi_lang",
13417#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013418#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013419#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013420 "mzscheme",
13421#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423#ifdef FEAT_OLE
13424 "ole",
13425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426#ifdef FEAT_PATH_EXTRA
13427 "path_extra",
13428#endif
13429#ifdef FEAT_PERL
13430#ifndef DYNAMIC_PERL
13431 "perl",
13432#endif
13433#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013434#ifdef FEAT_PERSISTENT_UNDO
13435 "persistent_undo",
13436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437#ifdef FEAT_PYTHON
13438#ifndef DYNAMIC_PYTHON
13439 "python",
13440#endif
13441#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013442#ifdef FEAT_PYTHON3
13443#ifndef DYNAMIC_PYTHON3
13444 "python3",
13445#endif
13446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447#ifdef FEAT_POSTSCRIPT
13448 "postscript",
13449#endif
13450#ifdef FEAT_PRINTER
13451 "printer",
13452#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013453#ifdef FEAT_PROFILE
13454 "profile",
13455#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013456#ifdef FEAT_RELTIME
13457 "reltime",
13458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459#ifdef FEAT_QUICKFIX
13460 "quickfix",
13461#endif
13462#ifdef FEAT_RIGHTLEFT
13463 "rightleft",
13464#endif
13465#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13466 "ruby",
13467#endif
13468#ifdef FEAT_SCROLLBIND
13469 "scrollbind",
13470#endif
13471#ifdef FEAT_CMDL_INFO
13472 "showcmd",
13473 "cmdline_info",
13474#endif
13475#ifdef FEAT_SIGNS
13476 "signs",
13477#endif
13478#ifdef FEAT_SMARTINDENT
13479 "smartindent",
13480#endif
13481#ifdef FEAT_SNIFF
13482 "sniff",
13483#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013484#ifdef STARTUPTIME
13485 "startuptime",
13486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487#ifdef FEAT_STL_OPT
13488 "statusline",
13489#endif
13490#ifdef FEAT_SUN_WORKSHOP
13491 "sun_workshop",
13492#endif
13493#ifdef FEAT_NETBEANS_INTG
13494 "netbeans_intg",
13495#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013496#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013497 "spell",
13498#endif
13499#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 "syntax",
13501#endif
13502#if defined(USE_SYSTEM) || !defined(UNIX)
13503 "system",
13504#endif
13505#ifdef FEAT_TAG_BINS
13506 "tag_binary",
13507#endif
13508#ifdef FEAT_TAG_OLDSTATIC
13509 "tag_old_static",
13510#endif
13511#ifdef FEAT_TAG_ANYWHITE
13512 "tag_any_white",
13513#endif
13514#ifdef FEAT_TCL
13515# ifndef DYNAMIC_TCL
13516 "tcl",
13517# endif
13518#endif
13519#ifdef TERMINFO
13520 "terminfo",
13521#endif
13522#ifdef FEAT_TERMRESPONSE
13523 "termresponse",
13524#endif
13525#ifdef FEAT_TEXTOBJ
13526 "textobjects",
13527#endif
13528#ifdef HAVE_TGETENT
13529 "tgetent",
13530#endif
13531#ifdef FEAT_TITLE
13532 "title",
13533#endif
13534#ifdef FEAT_TOOLBAR
13535 "toolbar",
13536#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013537#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13538 "unnamedplus",
13539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540#ifdef FEAT_USR_CMDS
13541 "user-commands", /* was accidentally included in 5.4 */
13542 "user_commands",
13543#endif
13544#ifdef FEAT_VIMINFO
13545 "viminfo",
13546#endif
13547#ifdef FEAT_VERTSPLIT
13548 "vertsplit",
13549#endif
13550#ifdef FEAT_VIRTUALEDIT
13551 "virtualedit",
13552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554#ifdef FEAT_VISUALEXTRA
13555 "visualextra",
13556#endif
13557#ifdef FEAT_VREPLACE
13558 "vreplace",
13559#endif
13560#ifdef FEAT_WILDIGN
13561 "wildignore",
13562#endif
13563#ifdef FEAT_WILDMENU
13564 "wildmenu",
13565#endif
13566#ifdef FEAT_WINDOWS
13567 "windows",
13568#endif
13569#ifdef FEAT_WAK
13570 "winaltkeys",
13571#endif
13572#ifdef FEAT_WRITEBACKUP
13573 "writebackup",
13574#endif
13575#ifdef FEAT_XIM
13576 "xim",
13577#endif
13578#ifdef FEAT_XFONTSET
13579 "xfontset",
13580#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013581#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013582 "xpm",
13583 "xpm_w32", /* for backward compatibility */
13584#else
13585# if defined(HAVE_XPM)
13586 "xpm",
13587# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589#ifdef USE_XSMP
13590 "xsmp",
13591#endif
13592#ifdef USE_XSMP_INTERACT
13593 "xsmp_interact",
13594#endif
13595#ifdef FEAT_XCLIPBOARD
13596 "xterm_clipboard",
13597#endif
13598#ifdef FEAT_XTERM_SAVE
13599 "xterm_save",
13600#endif
13601#if defined(UNIX) && defined(FEAT_X11)
13602 "X11",
13603#endif
13604 NULL
13605 };
13606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 for (i = 0; has_list[i] != NULL; ++i)
13609 if (STRICMP(name, has_list[i]) == 0)
13610 {
13611 n = TRUE;
13612 break;
13613 }
13614
13615 if (n == FALSE)
13616 {
13617 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013618 {
13619 if (name[5] == '-'
13620 && STRLEN(name) > 11
13621 && vim_isdigit(name[6])
13622 && vim_isdigit(name[8])
13623 && vim_isdigit(name[10]))
13624 {
13625 int major = atoi((char *)name + 6);
13626 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013627
13628 /* Expect "patch-9.9.01234". */
13629 n = (major < VIM_VERSION_MAJOR
13630 || (major == VIM_VERSION_MAJOR
13631 && (minor < VIM_VERSION_MINOR
13632 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013633 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013634 }
13635 else
13636 n = has_patch(atoi((char *)name + 5));
13637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 else if (STRICMP(name, "vim_starting") == 0)
13639 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013640#ifdef FEAT_MBYTE
13641 else if (STRICMP(name, "multi_byte_encoding") == 0)
13642 n = has_mbyte;
13643#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013644#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13645 else if (STRICMP(name, "balloon_multiline") == 0)
13646 n = multiline_balloon_available();
13647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648#ifdef DYNAMIC_TCL
13649 else if (STRICMP(name, "tcl") == 0)
13650 n = tcl_enabled(FALSE);
13651#endif
13652#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13653 else if (STRICMP(name, "iconv") == 0)
13654 n = iconv_enabled(FALSE);
13655#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013656#ifdef DYNAMIC_LUA
13657 else if (STRICMP(name, "lua") == 0)
13658 n = lua_enabled(FALSE);
13659#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013660#ifdef DYNAMIC_MZSCHEME
13661 else if (STRICMP(name, "mzscheme") == 0)
13662 n = mzscheme_enabled(FALSE);
13663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664#ifdef DYNAMIC_RUBY
13665 else if (STRICMP(name, "ruby") == 0)
13666 n = ruby_enabled(FALSE);
13667#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013668#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669#ifdef DYNAMIC_PYTHON
13670 else if (STRICMP(name, "python") == 0)
13671 n = python_enabled(FALSE);
13672#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013673#endif
13674#ifdef FEAT_PYTHON3
13675#ifdef DYNAMIC_PYTHON3
13676 else if (STRICMP(name, "python3") == 0)
13677 n = python3_enabled(FALSE);
13678#endif
13679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680#ifdef DYNAMIC_PERL
13681 else if (STRICMP(name, "perl") == 0)
13682 n = perl_enabled(FALSE);
13683#endif
13684#ifdef FEAT_GUI
13685 else if (STRICMP(name, "gui_running") == 0)
13686 n = (gui.in_use || gui.starting);
13687# ifdef FEAT_GUI_W32
13688 else if (STRICMP(name, "gui_win32s") == 0)
13689 n = gui_is_win32s();
13690# endif
13691# ifdef FEAT_BROWSE
13692 else if (STRICMP(name, "browse") == 0)
13693 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13694# endif
13695#endif
13696#ifdef FEAT_SYN_HL
13697 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013698 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699#endif
13700#if defined(WIN3264)
13701 else if (STRICMP(name, "win95") == 0)
13702 n = mch_windows95();
13703#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013704#ifdef FEAT_NETBEANS_INTG
13705 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013706 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708 }
13709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711}
13712
13713/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013714 * "has_key()" function
13715 */
13716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013717f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013718{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013719 if (argvars[0].v_type != VAR_DICT)
13720 {
13721 EMSG(_(e_dictreq));
13722 return;
13723 }
13724 if (argvars[0].vval.v_dict == NULL)
13725 return;
13726
13727 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013728 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013729}
13730
13731/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013732 * "haslocaldir()" function
13733 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013735f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013736{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013737 win_T *wp = NULL;
13738
13739 wp = find_tabwin(&argvars[0], &argvars[1]);
13740 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013741}
13742
13743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744 * "hasmapto()" function
13745 */
13746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013747f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748{
13749 char_u *name;
13750 char_u *mode;
13751 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013752 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013754 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013755 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 mode = (char_u *)"nvo";
13757 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013759 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013760 if (argvars[2].v_type != VAR_UNKNOWN)
13761 abbr = get_tv_number(&argvars[2]);
13762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763
Bram Moolenaar2c932302006-03-18 21:42:09 +000013764 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013765 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013767 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768}
13769
13770/*
13771 * "histadd()" function
13772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013774f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775{
13776#ifdef FEAT_CMDHIST
13777 int histype;
13778 char_u *str;
13779 char_u buf[NUMBUFLEN];
13780#endif
13781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013782 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783 if (check_restricted() || check_secure())
13784 return;
13785#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013786 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13787 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788 if (histype >= 0)
13789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013790 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791 if (*str != NUL)
13792 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013793 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013795 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 return;
13797 }
13798 }
13799#endif
13800}
13801
13802/*
13803 * "histdel()" function
13804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013806f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807{
13808#ifdef FEAT_CMDHIST
13809 int n;
13810 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013811 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13814 if (str == NULL)
13815 n = 0;
13816 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013818 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013819 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013821 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013822 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 else
13824 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013825 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013826 get_tv_string_buf(&argvars[1], buf));
13827 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828#endif
13829}
13830
13831/*
13832 * "histget()" function
13833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013835f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836{
13837#ifdef FEAT_CMDHIST
13838 int type;
13839 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013842 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13843 if (str == NULL)
13844 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013846 {
13847 type = get_histtype(str);
13848 if (argvars[1].v_type == VAR_UNKNOWN)
13849 idx = get_history_idx(type);
13850 else
13851 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13852 /* -1 on type error */
13853 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013856 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013858 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859}
13860
13861/*
13862 * "histnr()" function
13863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013865f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866{
13867 int i;
13868
13869#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013870 char_u *history = get_tv_string_chk(&argvars[0]);
13871
13872 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873 if (i >= HIST_CMD && i < HIST_COUNT)
13874 i = get_history_idx(i);
13875 else
13876#endif
13877 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013878 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879}
13880
13881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882 * "highlightID(name)" function
13883 */
13884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013885f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013887 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888}
13889
13890/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013891 * "highlight_exists()" function
13892 */
13893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013894f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013895{
13896 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13897}
13898
13899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900 * "hostname()" function
13901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013903f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904{
13905 char_u hostname[256];
13906
13907 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013908 rettv->v_type = VAR_STRING;
13909 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910}
13911
13912/*
13913 * iconv() function
13914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013916f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917{
13918#ifdef FEAT_MBYTE
13919 char_u buf1[NUMBUFLEN];
13920 char_u buf2[NUMBUFLEN];
13921 char_u *from, *to, *str;
13922 vimconv_T vimconv;
13923#endif
13924
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013925 rettv->v_type = VAR_STRING;
13926 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927
13928#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013929 str = get_tv_string(&argvars[0]);
13930 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13931 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 vimconv.vc_type = CONV_NONE;
13933 convert_setup(&vimconv, from, to);
13934
13935 /* If the encodings are equal, no conversion needed. */
13936 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013937 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013939 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940
13941 convert_setup(&vimconv, NULL, NULL);
13942 vim_free(from);
13943 vim_free(to);
13944#endif
13945}
13946
13947/*
13948 * "indent()" function
13949 */
13950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013951f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952{
13953 linenr_T lnum;
13954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013955 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013957 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013959 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960}
13961
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013962/*
13963 * "index()" function
13964 */
13965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013966f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013967{
Bram Moolenaar33570922005-01-25 22:26:29 +000013968 list_T *l;
13969 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013970 long idx = 0;
13971 int ic = FALSE;
13972
13973 rettv->vval.v_number = -1;
13974 if (argvars[0].v_type != VAR_LIST)
13975 {
13976 EMSG(_(e_listreq));
13977 return;
13978 }
13979 l = argvars[0].vval.v_list;
13980 if (l != NULL)
13981 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013982 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013983 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013984 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985 int error = FALSE;
13986
Bram Moolenaar758711c2005-02-02 23:11:38 +000013987 /* Start at specified item. Use the cached index that list_find()
13988 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013989 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013990 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013991 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013992 ic = get_tv_number_chk(&argvars[3], &error);
13993 if (error)
13994 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013995 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013996
Bram Moolenaar758711c2005-02-02 23:11:38 +000013997 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013998 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013999 {
14000 rettv->vval.v_number = idx;
14001 break;
14002 }
14003 }
14004}
14005
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006static int inputsecret_flag = 0;
14007
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014008static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014009
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014011 * This function is used by f_input() and f_inputdialog() functions. The third
14012 * argument to f_input() specifies the type of completion to use at the
14013 * prompt. The third argument to f_inputdialog() specifies the value to return
14014 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 */
14016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014017get_user_input(
14018 typval_T *argvars,
14019 typval_T *rettv,
14020 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014022 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023 char_u *p = NULL;
14024 int c;
14025 char_u buf[NUMBUFLEN];
14026 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014027 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014028 int xp_type = EXPAND_NOTHING;
14029 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014031 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014032 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033
14034#ifdef NO_CONSOLE_INPUT
14035 /* While starting up, there is no place to enter text. */
14036 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038#endif
14039
14040 cmd_silent = FALSE; /* Want to see the prompt. */
14041 if (prompt != NULL)
14042 {
14043 /* Only the part of the message after the last NL is considered as
14044 * prompt for the command line */
14045 p = vim_strrchr(prompt, '\n');
14046 if (p == NULL)
14047 p = prompt;
14048 else
14049 {
14050 ++p;
14051 c = *p;
14052 *p = NUL;
14053 msg_start();
14054 msg_clr_eos();
14055 msg_puts_attr(prompt, echo_attr);
14056 msg_didout = FALSE;
14057 msg_starthere();
14058 *p = c;
14059 }
14060 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014062 if (argvars[1].v_type != VAR_UNKNOWN)
14063 {
14064 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14065 if (defstr != NULL)
14066 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014068 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014069 {
14070 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014071 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014072 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014073
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014074 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014075 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014076
Bram Moolenaar4463f292005-09-25 22:20:24 +000014077 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14078 if (xp_name == NULL)
14079 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014080
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014081 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014082
Bram Moolenaar4463f292005-09-25 22:20:24 +000014083 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14084 &xp_arg) == FAIL)
14085 return;
14086 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014087 }
14088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014089 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014090 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014091 int save_ex_normal_busy = ex_normal_busy;
14092 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014093 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014094 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14095 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014096 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014097 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014098 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014099 && argvars[1].v_type != VAR_UNKNOWN
14100 && argvars[2].v_type != VAR_UNKNOWN)
14101 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14102 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014103
14104 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014106 /* since the user typed this, no need to wait for return */
14107 need_wait_return = FALSE;
14108 msg_didout = FALSE;
14109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110 cmd_silent = cmd_silent_save;
14111}
14112
14113/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014114 * "input()" function
14115 * Also handles inputsecret() when inputsecret is set.
14116 */
14117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014118f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014119{
14120 get_user_input(argvars, rettv, FALSE);
14121}
14122
14123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 * "inputdialog()" function
14125 */
14126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014127f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128{
14129#if defined(FEAT_GUI_TEXTDIALOG)
14130 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14131 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14132 {
14133 char_u *message;
14134 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014135 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014137 message = get_tv_string_chk(&argvars[0]);
14138 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014139 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014140 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 else
14142 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 if (message != NULL && defstr != NULL
14144 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014145 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014146 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147 else
14148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 if (message != NULL && defstr != NULL
14150 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014151 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014152 rettv->vval.v_string = vim_strsave(
14153 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014155 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 }
14159 else
14160#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014161 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162}
14163
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014164/*
14165 * "inputlist()" function
14166 */
14167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014168f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014169{
14170 listitem_T *li;
14171 int selected;
14172 int mouse_used;
14173
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014174#ifdef NO_CONSOLE_INPUT
14175 /* While starting up, there is no place to enter text. */
14176 if (no_console_input())
14177 return;
14178#endif
14179 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14180 {
14181 EMSG2(_(e_listarg), "inputlist()");
14182 return;
14183 }
14184
14185 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014186 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014187 lines_left = Rows; /* avoid more prompt */
14188 msg_scroll = TRUE;
14189 msg_clr_eos();
14190
14191 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14192 {
14193 msg_puts(get_tv_string(&li->li_tv));
14194 msg_putchar('\n');
14195 }
14196
14197 /* Ask for choice. */
14198 selected = prompt_for_number(&mouse_used);
14199 if (mouse_used)
14200 selected -= lines_left;
14201
14202 rettv->vval.v_number = selected;
14203}
14204
14205
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14207
14208/*
14209 * "inputrestore()" function
14210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014212f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213{
14214 if (ga_userinput.ga_len > 0)
14215 {
14216 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14218 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014219 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 }
14221 else if (p_verbose > 1)
14222 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014223 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014224 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225 }
14226}
14227
14228/*
14229 * "inputsave()" function
14230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014232f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014234 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 if (ga_grow(&ga_userinput, 1) == OK)
14236 {
14237 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14238 + ga_userinput.ga_len);
14239 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014240 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 }
14242 else
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 * "inputsecret()" function
14248 */
14249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014250f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251{
14252 ++cmdline_star;
14253 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014254 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 --cmdline_star;
14256 --inputsecret_flag;
14257}
14258
14259/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014260 * "insert()" function
14261 */
14262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014263f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014264{
14265 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014266 listitem_T *item;
14267 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014268 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014269
14270 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014271 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014272 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014273 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014274 {
14275 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014276 before = get_tv_number_chk(&argvars[2], &error);
14277 if (error)
14278 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014279
Bram Moolenaar758711c2005-02-02 23:11:38 +000014280 if (before == l->lv_len)
14281 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014282 else
14283 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014284 item = list_find(l, before);
14285 if (item == NULL)
14286 {
14287 EMSGN(_(e_listidx), before);
14288 l = NULL;
14289 }
14290 }
14291 if (l != NULL)
14292 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014293 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014294 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014295 }
14296 }
14297}
14298
14299/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014300 * "invert(expr)" function
14301 */
14302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014303f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014304{
14305 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14306}
14307
14308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 * "isdirectory()" function
14310 */
14311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014312f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315}
14316
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014317/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014318 * "islocked()" function
14319 */
14320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014321f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014322{
14323 lval_T lv;
14324 char_u *end;
14325 dictitem_T *di;
14326
14327 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014328 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14329 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014330 if (end != NULL && lv.ll_name != NULL)
14331 {
14332 if (*end != NUL)
14333 EMSG(_(e_trailing));
14334 else
14335 {
14336 if (lv.ll_tv == NULL)
14337 {
14338 if (check_changedtick(lv.ll_name))
14339 rettv->vval.v_number = 1; /* always locked */
14340 else
14341 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014342 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014343 if (di != NULL)
14344 {
14345 /* Consider a variable locked when:
14346 * 1. the variable itself is locked
14347 * 2. the value of the variable is locked.
14348 * 3. the List or Dict value is locked.
14349 */
14350 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14351 || tv_islocked(&di->di_tv));
14352 }
14353 }
14354 }
14355 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014356 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014357 else if (lv.ll_newkey != NULL)
14358 EMSG2(_(e_dictkey), lv.ll_newkey);
14359 else if (lv.ll_list != NULL)
14360 /* List item. */
14361 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14362 else
14363 /* Dictionary item. */
14364 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14365 }
14366 }
14367
14368 clear_lval(&lv);
14369}
14370
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014371static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014372
14373/*
14374 * Turn a dict into a list:
14375 * "what" == 0: list of keys
14376 * "what" == 1: list of values
14377 * "what" == 2: list of items
14378 */
14379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014380dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014381{
Bram Moolenaar33570922005-01-25 22:26:29 +000014382 list_T *l2;
14383 dictitem_T *di;
14384 hashitem_T *hi;
14385 listitem_T *li;
14386 listitem_T *li2;
14387 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014388 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014389
Bram Moolenaar8c711452005-01-14 21:53:12 +000014390 if (argvars[0].v_type != VAR_DICT)
14391 {
14392 EMSG(_(e_dictreq));
14393 return;
14394 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014395 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014396 return;
14397
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014398 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014399 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014400
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014401 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014402 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014403 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014404 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014405 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014406 --todo;
14407 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014408
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014409 li = listitem_alloc();
14410 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014411 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014412 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014413
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014414 if (what == 0)
14415 {
14416 /* keys() */
14417 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014418 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014419 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14420 }
14421 else if (what == 1)
14422 {
14423 /* values() */
14424 copy_tv(&di->di_tv, &li->li_tv);
14425 }
14426 else
14427 {
14428 /* items() */
14429 l2 = list_alloc();
14430 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014431 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014432 li->li_tv.vval.v_list = l2;
14433 if (l2 == NULL)
14434 break;
14435 ++l2->lv_refcount;
14436
14437 li2 = listitem_alloc();
14438 if (li2 == NULL)
14439 break;
14440 list_append(l2, li2);
14441 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014442 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014443 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14444
14445 li2 = listitem_alloc();
14446 if (li2 == NULL)
14447 break;
14448 list_append(l2, li2);
14449 copy_tv(&di->di_tv, &li2->li_tv);
14450 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014451 }
14452 }
14453}
14454
14455/*
14456 * "items(dict)" function
14457 */
14458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014459f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014460{
14461 dict_list(argvars, rettv, 2);
14462}
14463
Bram Moolenaar835dc632016-02-07 14:27:38 +010014464#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014465
14466# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014467/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014468 * "job_getchannel()" function
14469 */
14470 static void
14471f_job_getchannel(typval_T *argvars, typval_T *rettv)
14472{
14473 if (argvars[0].v_type != VAR_JOB)
14474 EMSG(_(e_invarg));
14475 else
14476 {
14477 job_T *job = argvars[0].vval.v_job;
14478
Bram Moolenaar77073442016-02-13 23:23:53 +010014479 rettv->v_type = VAR_CHANNEL;
14480 rettv->vval.v_channel = job->jv_channel;
14481 if (job->jv_channel != NULL)
14482 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014483 }
14484}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014485# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014486
14487/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014488 * "job_start()" function
14489 */
14490 static void
14491f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14492{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014493 job_T *job;
14494 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014495#if defined(UNIX)
14496# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014497 char **argv = NULL;
14498 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014499#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014500 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014501#endif
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014502 jobopt_T options;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014503
14504 rettv->v_type = VAR_JOB;
14505 job = job_alloc();
14506 rettv->vval.v_job = job;
14507 if (job == NULL)
14508 return;
14509
14510 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014511
14512 /* Default mode is NL. */
14513 options.jo_mode = MODE_NL;
14514 if (argvars[1].v_type != VAR_UNKNOWN)
14515 {
14516 dict_T *dict;
14517
14518 if (argvars[1].v_type != VAR_DICT)
14519 {
14520 EMSG(_(e_invarg));
14521 return;
14522 }
14523 dict = argvars[1].vval.v_dict;
14524 if (get_mode_arg(dict, &options) == FAIL)
14525 return;
14526 }
14527
Bram Moolenaar835dc632016-02-07 14:27:38 +010014528#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014529 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014530#endif
14531
14532 if (argvars[0].v_type == VAR_STRING)
14533 {
14534 /* Command is a string. */
14535 cmd = argvars[0].vval.v_string;
14536#ifdef USE_ARGV
14537 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14538 return;
14539 argv[argc] = NULL;
14540#endif
14541 }
14542 else if (argvars[0].v_type != VAR_LIST
14543 || argvars[0].vval.v_list == NULL
14544 || argvars[0].vval.v_list->lv_len < 1)
14545 {
14546 EMSG(_(e_invarg));
14547 return;
14548 }
14549 else
14550 {
14551 list_T *l = argvars[0].vval.v_list;
14552 listitem_T *li;
14553 char_u *s;
14554
14555#ifdef USE_ARGV
14556 /* Pass argv[] to mch_call_shell(). */
14557 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14558 if (argv == NULL)
14559 return;
14560#endif
14561 for (li = l->lv_first; li != NULL; li = li->li_next)
14562 {
14563 s = get_tv_string_chk(&li->li_tv);
14564 if (s == NULL)
14565 goto theend;
14566#ifdef USE_ARGV
14567 argv[argc++] = (char *)s;
14568#else
14569 if (li != l->lv_first)
14570 {
14571 s = vim_strsave_shellescape(s, FALSE, TRUE);
14572 if (s == NULL)
14573 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014574 ga_concat(&ga, s);
14575 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014576 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014577 else
14578 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014579 if (li->li_next != NULL)
14580 ga_append(&ga, ' ');
14581#endif
14582 }
14583#ifdef USE_ARGV
14584 argv[argc] = NULL;
14585#else
14586 cmd = ga.ga_data;
14587#endif
14588 }
14589#ifdef USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014590 mch_start_job(argv, job, &options);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014591#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014592 mch_start_job((char *)cmd, job, &options);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014593#endif
14594
14595theend:
14596#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014597 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014598#else
14599 vim_free(ga.ga_data);
14600#endif
14601}
14602
14603/*
14604 * "job_status()" function
14605 */
14606 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014607f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014608{
14609 char *result;
14610
14611 if (argvars[0].v_type != VAR_JOB)
14612 EMSG(_(e_invarg));
14613 else
14614 {
14615 job_T *job = argvars[0].vval.v_job;
14616
14617 if (job->jv_status == JOB_ENDED)
14618 /* No need to check, dead is dead. */
14619 result = "dead";
14620 else if (job->jv_status == JOB_FAILED)
14621 result = "fail";
14622 else
14623 result = mch_job_status(job);
14624 rettv->v_type = VAR_STRING;
14625 rettv->vval.v_string = vim_strsave((char_u *)result);
14626 }
14627}
14628
14629/*
14630 * "job_stop()" function
14631 */
14632 static void
14633f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14634{
14635 if (argvars[0].v_type != VAR_JOB)
14636 EMSG(_(e_invarg));
14637 else
14638 {
14639 char_u *arg;
14640
14641 if (argvars[1].v_type == VAR_UNKNOWN)
14642 arg = (char_u *)"";
14643 else
14644 {
14645 arg = get_tv_string_chk(&argvars[1]);
14646 if (arg == NULL)
14647 {
14648 EMSG(_(e_invarg));
14649 return;
14650 }
14651 }
14652 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14653 rettv->vval.v_number = 0;
14654 else
14655 rettv->vval.v_number = 1;
14656 }
14657}
14658#endif
14659
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014661 * "join()" function
14662 */
14663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014664f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014665{
14666 garray_T ga;
14667 char_u *sep;
14668
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014669 if (argvars[0].v_type != VAR_LIST)
14670 {
14671 EMSG(_(e_listreq));
14672 return;
14673 }
14674 if (argvars[0].vval.v_list == NULL)
14675 return;
14676 if (argvars[1].v_type == VAR_UNKNOWN)
14677 sep = (char_u *)" ";
14678 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014679 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014680
14681 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014682
14683 if (sep != NULL)
14684 {
14685 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014686 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014687 ga_append(&ga, NUL);
14688 rettv->vval.v_string = (char_u *)ga.ga_data;
14689 }
14690 else
14691 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014692}
14693
14694/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014695 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014696 */
14697 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014698f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014699{
14700 js_read_T reader;
14701
14702 reader.js_buf = get_tv_string(&argvars[0]);
14703 reader.js_fill = NULL;
14704 reader.js_used = 0;
14705 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14706 EMSG(_(e_invarg));
14707}
14708
14709/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014710 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014711 */
14712 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014713f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014714{
14715 rettv->v_type = VAR_STRING;
14716 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14717}
14718
14719/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014720 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014721 */
14722 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014723f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014724{
14725 js_read_T reader;
14726
14727 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014728 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014729 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014730 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014731 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014732}
14733
14734/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014735 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014736 */
14737 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014738f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014739{
14740 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014741 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014742}
14743
14744/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014745 * "keys()" function
14746 */
14747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014748f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014749{
14750 dict_list(argvars, rettv, 0);
14751}
14752
14753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754 * "last_buffer_nr()" function.
14755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014757f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758{
14759 int n = 0;
14760 buf_T *buf;
14761
14762 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14763 if (n < buf->b_fnum)
14764 n = buf->b_fnum;
14765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014766 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014767}
14768
14769/*
14770 * "len()" function
14771 */
14772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014773f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014774{
14775 switch (argvars[0].v_type)
14776 {
14777 case VAR_STRING:
14778 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014779 rettv->vval.v_number = (varnumber_T)STRLEN(
14780 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014781 break;
14782 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014783 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014784 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014785 case VAR_DICT:
14786 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14787 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014788 case VAR_UNKNOWN:
14789 case VAR_SPECIAL:
14790 case VAR_FLOAT:
14791 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014792 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014793 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014794 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014795 break;
14796 }
14797}
14798
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014799static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014800
14801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014802libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014803{
14804#ifdef FEAT_LIBCALL
14805 char_u *string_in;
14806 char_u **string_result;
14807 int nr_result;
14808#endif
14809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014810 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014811 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014812 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014813
14814 if (check_restricted() || check_secure())
14815 return;
14816
14817#ifdef FEAT_LIBCALL
14818 /* The first two args must be strings, otherwise its meaningless */
14819 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14820 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014821 string_in = NULL;
14822 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014823 string_in = argvars[2].vval.v_string;
14824 if (type == VAR_NUMBER)
14825 string_result = NULL;
14826 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014828 if (mch_libcall(argvars[0].vval.v_string,
14829 argvars[1].vval.v_string,
14830 string_in,
14831 argvars[2].vval.v_number,
14832 string_result,
14833 &nr_result) == OK
14834 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014835 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014836 }
14837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838}
14839
14840/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014841 * "libcall()" function
14842 */
14843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014844f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014845{
14846 libcall_common(argvars, rettv, VAR_STRING);
14847}
14848
14849/*
14850 * "libcallnr()" function
14851 */
14852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014853f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014854{
14855 libcall_common(argvars, rettv, VAR_NUMBER);
14856}
14857
14858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859 * "line(string)" function
14860 */
14861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014862f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863{
14864 linenr_T lnum = 0;
14865 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014866 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014868 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869 if (fp != NULL)
14870 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014871 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872}
14873
14874/*
14875 * "line2byte(lnum)" function
14876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014878f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879{
14880#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014881 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882#else
14883 linenr_T lnum;
14884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014885 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014887 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014889 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14890 if (rettv->vval.v_number >= 0)
14891 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892#endif
14893}
14894
14895/*
14896 * "lispindent(lnum)" function
14897 */
14898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014899f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014900{
14901#ifdef FEAT_LISP
14902 pos_T pos;
14903 linenr_T lnum;
14904
14905 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014906 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14908 {
14909 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014910 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911 curwin->w_cursor = pos;
14912 }
14913 else
14914#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014915 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916}
14917
14918/*
14919 * "localtime()" function
14920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014922f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014923{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014924 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925}
14926
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014927static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928
14929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014930get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931{
14932 char_u *keys;
14933 char_u *which;
14934 char_u buf[NUMBUFLEN];
14935 char_u *keys_buf = NULL;
14936 char_u *rhs;
14937 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014938 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014939 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014940 mapblock_T *mp;
14941 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942
14943 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014944 rettv->v_type = VAR_STRING;
14945 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014947 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014948 if (*keys == NUL)
14949 return;
14950
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014951 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014952 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014953 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014954 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014955 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014956 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014957 if (argvars[3].v_type != VAR_UNKNOWN)
14958 get_dict = get_tv_number(&argvars[3]);
14959 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 else
14962 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014963 if (which == NULL)
14964 return;
14965
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 mode = get_map_mode(&which, 0);
14967
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014968 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014969 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014971
14972 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014974 /* Return a string. */
14975 if (rhs != NULL)
14976 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977
Bram Moolenaarbd743252010-10-20 21:23:33 +020014978 }
14979 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14980 {
14981 /* Return a dictionary. */
14982 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14983 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14984 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985
Bram Moolenaarbd743252010-10-20 21:23:33 +020014986 dict_add_nr_str(dict, "lhs", 0L, lhs);
14987 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14988 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14989 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14990 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14991 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14992 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014993 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014994 dict_add_nr_str(dict, "mode", 0L, mapmode);
14995
14996 vim_free(lhs);
14997 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998 }
14999}
15000
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015001#ifdef FEAT_FLOAT
15002/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015003 * "log()" function
15004 */
15005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015006f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015007{
15008 float_T f;
15009
15010 rettv->v_type = VAR_FLOAT;
15011 if (get_float_arg(argvars, &f) == OK)
15012 rettv->vval.v_float = log(f);
15013 else
15014 rettv->vval.v_float = 0.0;
15015}
15016
15017/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015018 * "log10()" function
15019 */
15020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015021f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015022{
15023 float_T f;
15024
15025 rettv->v_type = VAR_FLOAT;
15026 if (get_float_arg(argvars, &f) == OK)
15027 rettv->vval.v_float = log10(f);
15028 else
15029 rettv->vval.v_float = 0.0;
15030}
15031#endif
15032
Bram Moolenaar1dced572012-04-05 16:54:08 +020015033#ifdef FEAT_LUA
15034/*
15035 * "luaeval()" function
15036 */
15037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015038f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015039{
15040 char_u *str;
15041 char_u buf[NUMBUFLEN];
15042
15043 str = get_tv_string_buf(&argvars[0], buf);
15044 do_luaeval(str, argvars + 1, rettv);
15045}
15046#endif
15047
Bram Moolenaar071d4272004-06-13 20:20:40 +000015048/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015049 * "map()" function
15050 */
15051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015052f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015053{
15054 filter_map(argvars, rettv, TRUE);
15055}
15056
15057/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015058 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059 */
15060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015061f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015063 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064}
15065
15066/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015067 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068 */
15069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015070f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015072 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073}
15074
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015075static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076
15077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015078find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015080 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015081 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015082 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083 char_u *pat;
15084 regmatch_T regmatch;
15085 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015086 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087 char_u *save_cpo;
15088 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015089 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015090 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015091 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015092 list_T *l = NULL;
15093 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015094 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015095 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015096
15097 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15098 save_cpo = p_cpo;
15099 p_cpo = (char_u *)"";
15100
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015101 rettv->vval.v_number = -1;
15102 if (type == 3)
15103 {
15104 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015105 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015106 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015107 }
15108 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015110 rettv->v_type = VAR_STRING;
15111 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015114 if (argvars[0].v_type == VAR_LIST)
15115 {
15116 if ((l = argvars[0].vval.v_list) == NULL)
15117 goto theend;
15118 li = l->lv_first;
15119 }
15120 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015121 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015122 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015123 len = (long)STRLEN(str);
15124 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015125
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015126 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15127 if (pat == NULL)
15128 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015129
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015130 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015132 int error = FALSE;
15133
15134 start = get_tv_number_chk(&argvars[2], &error);
15135 if (error)
15136 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015137 if (l != NULL)
15138 {
15139 li = list_find(l, start);
15140 if (li == NULL)
15141 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015142 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015143 }
15144 else
15145 {
15146 if (start < 0)
15147 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015148 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015149 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015150 /* When "count" argument is there ignore matches before "start",
15151 * otherwise skip part of the string. Differs when pattern is "^"
15152 * or "\<". */
15153 if (argvars[3].v_type != VAR_UNKNOWN)
15154 startcol = start;
15155 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015156 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015157 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015158 len -= start;
15159 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015160 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015161
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015162 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015163 nth = get_tv_number_chk(&argvars[3], &error);
15164 if (error)
15165 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166 }
15167
15168 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15169 if (regmatch.regprog != NULL)
15170 {
15171 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015172
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015173 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015174 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015175 if (l != NULL)
15176 {
15177 if (li == NULL)
15178 {
15179 match = FALSE;
15180 break;
15181 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015182 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015183 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015184 if (str == NULL)
15185 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015186 }
15187
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015188 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015189
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015190 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015191 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015192 if (l == NULL && !match)
15193 break;
15194
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015195 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015196 if (l != NULL)
15197 {
15198 li = li->li_next;
15199 ++idx;
15200 }
15201 else
15202 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015203#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015204 startcol = (colnr_T)(regmatch.startp[0]
15205 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015206#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015207 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015208#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015209 if (startcol > (colnr_T)len
15210 || str + startcol <= regmatch.startp[0])
15211 {
15212 match = FALSE;
15213 break;
15214 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015215 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015216 }
15217
15218 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015220 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015222 int i;
15223
15224 /* return list with matched string and submatches */
15225 for (i = 0; i < NSUBEXP; ++i)
15226 {
15227 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015228 {
15229 if (list_append_string(rettv->vval.v_list,
15230 (char_u *)"", 0) == FAIL)
15231 break;
15232 }
15233 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015234 regmatch.startp[i],
15235 (int)(regmatch.endp[i] - regmatch.startp[i]))
15236 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015237 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015238 }
15239 }
15240 else if (type == 2)
15241 {
15242 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015243 if (l != NULL)
15244 copy_tv(&li->li_tv, rettv);
15245 else
15246 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015248 }
15249 else if (l != NULL)
15250 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251 else
15252 {
15253 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015254 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255 (varnumber_T)(regmatch.startp[0] - str);
15256 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015257 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015259 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 }
15261 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015262 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 }
15264
15265theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015266 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267 p_cpo = save_cpo;
15268}
15269
15270/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015271 * "match()" function
15272 */
15273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015274f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015275{
15276 find_some_match(argvars, rettv, 1);
15277}
15278
15279/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015280 * "matchadd()" function
15281 */
15282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015283f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015284{
15285#ifdef FEAT_SEARCH_EXTRA
15286 char_u buf[NUMBUFLEN];
15287 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15288 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15289 int prio = 10; /* default priority */
15290 int id = -1;
15291 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015292 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015293
15294 rettv->vval.v_number = -1;
15295
15296 if (grp == NULL || pat == NULL)
15297 return;
15298 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015299 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015300 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015301 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015302 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015303 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015304 if (argvars[4].v_type != VAR_UNKNOWN)
15305 {
15306 if (argvars[4].v_type != VAR_DICT)
15307 {
15308 EMSG(_(e_dictreq));
15309 return;
15310 }
15311 if (dict_find(argvars[4].vval.v_dict,
15312 (char_u *)"conceal", -1) != NULL)
15313 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15314 (char_u *)"conceal", FALSE);
15315 }
15316 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015317 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015318 if (error == TRUE)
15319 return;
15320 if (id >= 1 && id <= 3)
15321 {
15322 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15323 return;
15324 }
15325
Bram Moolenaar6561d522015-07-21 15:48:27 +020015326 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15327 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015328#endif
15329}
15330
15331/*
15332 * "matchaddpos()" function
15333 */
15334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015335f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015336{
15337#ifdef FEAT_SEARCH_EXTRA
15338 char_u buf[NUMBUFLEN];
15339 char_u *group;
15340 int prio = 10;
15341 int id = -1;
15342 int error = FALSE;
15343 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015344 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015345
15346 rettv->vval.v_number = -1;
15347
15348 group = get_tv_string_buf_chk(&argvars[0], buf);
15349 if (group == NULL)
15350 return;
15351
15352 if (argvars[1].v_type != VAR_LIST)
15353 {
15354 EMSG2(_(e_listarg), "matchaddpos()");
15355 return;
15356 }
15357 l = argvars[1].vval.v_list;
15358 if (l == NULL)
15359 return;
15360
15361 if (argvars[2].v_type != VAR_UNKNOWN)
15362 {
15363 prio = get_tv_number_chk(&argvars[2], &error);
15364 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015365 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015366 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015367 if (argvars[4].v_type != VAR_UNKNOWN)
15368 {
15369 if (argvars[4].v_type != VAR_DICT)
15370 {
15371 EMSG(_(e_dictreq));
15372 return;
15373 }
15374 if (dict_find(argvars[4].vval.v_dict,
15375 (char_u *)"conceal", -1) != NULL)
15376 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15377 (char_u *)"conceal", FALSE);
15378 }
15379 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015380 }
15381 if (error == TRUE)
15382 return;
15383
15384 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15385 if (id == 1 || id == 2)
15386 {
15387 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15388 return;
15389 }
15390
Bram Moolenaar6561d522015-07-21 15:48:27 +020015391 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15392 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015393#endif
15394}
15395
15396/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015397 * "matcharg()" function
15398 */
15399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015400f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015401{
15402 if (rettv_list_alloc(rettv) == OK)
15403 {
15404#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015405 int id = get_tv_number(&argvars[0]);
15406 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015407
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015408 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015409 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015410 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15411 {
15412 list_append_string(rettv->vval.v_list,
15413 syn_id2name(m->hlg_id), -1);
15414 list_append_string(rettv->vval.v_list, m->pattern, -1);
15415 }
15416 else
15417 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015418 list_append_string(rettv->vval.v_list, NULL, -1);
15419 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015420 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015421 }
15422#endif
15423 }
15424}
15425
15426/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015427 * "matchdelete()" function
15428 */
15429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015430f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015431{
15432#ifdef FEAT_SEARCH_EXTRA
15433 rettv->vval.v_number = match_delete(curwin,
15434 (int)get_tv_number(&argvars[0]), TRUE);
15435#endif
15436}
15437
15438/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015439 * "matchend()" function
15440 */
15441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015442f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015443{
15444 find_some_match(argvars, rettv, 0);
15445}
15446
15447/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015448 * "matchlist()" function
15449 */
15450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015451f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015452{
15453 find_some_match(argvars, rettv, 3);
15454}
15455
15456/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015457 * "matchstr()" function
15458 */
15459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015460f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015461{
15462 find_some_match(argvars, rettv, 2);
15463}
15464
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015465static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015466
15467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015468max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015469{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015470 long n = 0;
15471 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015472 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015473
15474 if (argvars[0].v_type == VAR_LIST)
15475 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015476 list_T *l;
15477 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015478
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015479 l = argvars[0].vval.v_list;
15480 if (l != NULL)
15481 {
15482 li = l->lv_first;
15483 if (li != NULL)
15484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015485 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015486 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015487 {
15488 li = li->li_next;
15489 if (li == NULL)
15490 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015491 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015492 if (domax ? i > n : i < n)
15493 n = i;
15494 }
15495 }
15496 }
15497 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015498 else if (argvars[0].v_type == VAR_DICT)
15499 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015500 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015501 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015502 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015503 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015504
15505 d = argvars[0].vval.v_dict;
15506 if (d != NULL)
15507 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015508 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015509 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015510 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015511 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015512 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015513 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015514 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015515 if (first)
15516 {
15517 n = i;
15518 first = FALSE;
15519 }
15520 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015521 n = i;
15522 }
15523 }
15524 }
15525 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015526 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015527 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015528 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015529}
15530
15531/*
15532 * "max()" function
15533 */
15534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015535f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015536{
15537 max_min(argvars, rettv, TRUE);
15538}
15539
15540/*
15541 * "min()" function
15542 */
15543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015544f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015545{
15546 max_min(argvars, rettv, FALSE);
15547}
15548
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015549static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015550
15551/*
15552 * Create the directory in which "dir" is located, and higher levels when
15553 * needed.
15554 */
15555 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015556mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015557{
15558 char_u *p;
15559 char_u *updir;
15560 int r = FAIL;
15561
15562 /* Get end of directory name in "dir".
15563 * We're done when it's "/" or "c:/". */
15564 p = gettail_sep(dir);
15565 if (p <= get_past_head(dir))
15566 return OK;
15567
15568 /* If the directory exists we're done. Otherwise: create it.*/
15569 updir = vim_strnsave(dir, (int)(p - dir));
15570 if (updir == NULL)
15571 return FAIL;
15572 if (mch_isdir(updir))
15573 r = OK;
15574 else if (mkdir_recurse(updir, prot) == OK)
15575 r = vim_mkdir_emsg(updir, prot);
15576 vim_free(updir);
15577 return r;
15578}
15579
15580#ifdef vim_mkdir
15581/*
15582 * "mkdir()" function
15583 */
15584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015585f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015586{
15587 char_u *dir;
15588 char_u buf[NUMBUFLEN];
15589 int prot = 0755;
15590
15591 rettv->vval.v_number = FAIL;
15592 if (check_restricted() || check_secure())
15593 return;
15594
15595 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015596 if (*dir == NUL)
15597 rettv->vval.v_number = FAIL;
15598 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015599 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015600 if (*gettail(dir) == NUL)
15601 /* remove trailing slashes */
15602 *gettail_sep(dir) = NUL;
15603
15604 if (argvars[1].v_type != VAR_UNKNOWN)
15605 {
15606 if (argvars[2].v_type != VAR_UNKNOWN)
15607 prot = get_tv_number_chk(&argvars[2], NULL);
15608 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15609 mkdir_recurse(dir, prot);
15610 }
15611 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015612 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015613}
15614#endif
15615
Bram Moolenaar0d660222005-01-07 21:51:51 +000015616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617 * "mode()" function
15618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015620f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015622 char_u buf[3];
15623
15624 buf[1] = NUL;
15625 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 if (VIsual_active)
15628 {
15629 if (VIsual_select)
15630 buf[0] = VIsual_mode + 's' - 'v';
15631 else
15632 buf[0] = VIsual_mode;
15633 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015634 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015635 || State == CONFIRM)
15636 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015638 if (State == ASKMORE)
15639 buf[1] = 'm';
15640 else if (State == CONFIRM)
15641 buf[1] = '?';
15642 }
15643 else if (State == EXTERNCMD)
15644 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645 else if (State & INSERT)
15646 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015647#ifdef FEAT_VREPLACE
15648 if (State & VREPLACE_FLAG)
15649 {
15650 buf[0] = 'R';
15651 buf[1] = 'v';
15652 }
15653 else
15654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655 if (State & REPLACE_FLAG)
15656 buf[0] = 'R';
15657 else
15658 buf[0] = 'i';
15659 }
15660 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015661 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015663 if (exmode_active)
15664 buf[1] = 'v';
15665 }
15666 else if (exmode_active)
15667 {
15668 buf[0] = 'c';
15669 buf[1] = 'e';
15670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015672 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015674 if (finish_op)
15675 buf[1] = 'o';
15676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015678 /* Clear out the minor mode when the argument is not a non-zero number or
15679 * non-empty string. */
15680 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015681 buf[1] = NUL;
15682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015683 rettv->vval.v_string = vim_strsave(buf);
15684 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685}
15686
Bram Moolenaar429fa852013-04-15 12:27:36 +020015687#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015688/*
15689 * "mzeval()" function
15690 */
15691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015692f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015693{
15694 char_u *str;
15695 char_u buf[NUMBUFLEN];
15696
15697 str = get_tv_string_buf(&argvars[0], buf);
15698 do_mzeval(str, rettv);
15699}
Bram Moolenaar75676462013-01-30 14:55:42 +010015700
15701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015702mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015703{
15704 typval_T argvars[3];
15705
15706 argvars[0].v_type = VAR_STRING;
15707 argvars[0].vval.v_string = name;
15708 copy_tv(args, &argvars[1]);
15709 argvars[2].v_type = VAR_UNKNOWN;
15710 f_call(argvars, rettv);
15711 clear_tv(&argvars[1]);
15712}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015713#endif
15714
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015716 * "nextnonblank()" function
15717 */
15718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015719f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015720{
15721 linenr_T lnum;
15722
15723 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015725 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015726 {
15727 lnum = 0;
15728 break;
15729 }
15730 if (*skipwhite(ml_get(lnum)) != NUL)
15731 break;
15732 }
15733 rettv->vval.v_number = lnum;
15734}
15735
15736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737 * "nr2char()" function
15738 */
15739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015740f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741{
15742 char_u buf[NUMBUFLEN];
15743
15744#ifdef FEAT_MBYTE
15745 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015746 {
15747 int utf8 = 0;
15748
15749 if (argvars[1].v_type != VAR_UNKNOWN)
15750 utf8 = get_tv_number_chk(&argvars[1], NULL);
15751 if (utf8)
15752 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15753 else
15754 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 else
15757#endif
15758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015759 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 buf[1] = NUL;
15761 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015762 rettv->v_type = VAR_STRING;
15763 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015764}
15765
15766/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015767 * "or(expr, expr)" function
15768 */
15769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015770f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015771{
15772 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15773 | get_tv_number_chk(&argvars[1], NULL);
15774}
15775
15776/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015777 * "pathshorten()" function
15778 */
15779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015780f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015781{
15782 char_u *p;
15783
15784 rettv->v_type = VAR_STRING;
15785 p = get_tv_string_chk(&argvars[0]);
15786 if (p == NULL)
15787 rettv->vval.v_string = NULL;
15788 else
15789 {
15790 p = vim_strsave(p);
15791 rettv->vval.v_string = p;
15792 if (p != NULL)
15793 shorten_dir(p);
15794 }
15795}
15796
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015797#ifdef FEAT_PERL
15798/*
15799 * "perleval()" function
15800 */
15801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015802f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015803{
15804 char_u *str;
15805 char_u buf[NUMBUFLEN];
15806
15807 str = get_tv_string_buf(&argvars[0], buf);
15808 do_perleval(str, rettv);
15809}
15810#endif
15811
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015812#ifdef FEAT_FLOAT
15813/*
15814 * "pow()" function
15815 */
15816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015817f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015818{
15819 float_T fx, fy;
15820
15821 rettv->v_type = VAR_FLOAT;
15822 if (get_float_arg(argvars, &fx) == OK
15823 && get_float_arg(&argvars[1], &fy) == OK)
15824 rettv->vval.v_float = pow(fx, fy);
15825 else
15826 rettv->vval.v_float = 0.0;
15827}
15828#endif
15829
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015830/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015831 * "prevnonblank()" function
15832 */
15833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015834f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015835{
15836 linenr_T lnum;
15837
15838 lnum = get_tv_lnum(argvars);
15839 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15840 lnum = 0;
15841 else
15842 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15843 --lnum;
15844 rettv->vval.v_number = lnum;
15845}
15846
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015847/* This dummy va_list is here because:
15848 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15849 * - locally in the function results in a "used before set" warning
15850 * - using va_start() to initialize it gives "function with fixed args" error */
15851static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015852
Bram Moolenaar8c711452005-01-14 21:53:12 +000015853/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015854 * "printf()" function
15855 */
15856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015857f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015858{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015859 char_u buf[NUMBUFLEN];
15860 int len;
15861 char_u *s;
15862 int saved_did_emsg = did_emsg;
15863 char *fmt;
15864
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015865 rettv->v_type = VAR_STRING;
15866 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015867
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015868 /* Get the required length, allocate the buffer and do it for real. */
15869 did_emsg = FALSE;
15870 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15871 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15872 if (!did_emsg)
15873 {
15874 s = alloc(len + 1);
15875 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015876 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015877 rettv->vval.v_string = s;
15878 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015879 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015880 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015881 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015882}
15883
15884/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015885 * "pumvisible()" function
15886 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015888f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015889{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015890#ifdef FEAT_INS_EXPAND
15891 if (pum_visible())
15892 rettv->vval.v_number = 1;
15893#endif
15894}
15895
Bram Moolenaardb913952012-06-29 12:54:53 +020015896#ifdef FEAT_PYTHON3
15897/*
15898 * "py3eval()" function
15899 */
15900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015901f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015902{
15903 char_u *str;
15904 char_u buf[NUMBUFLEN];
15905
15906 str = get_tv_string_buf(&argvars[0], buf);
15907 do_py3eval(str, rettv);
15908}
15909#endif
15910
15911#ifdef FEAT_PYTHON
15912/*
15913 * "pyeval()" function
15914 */
15915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015916f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015917{
15918 char_u *str;
15919 char_u buf[NUMBUFLEN];
15920
15921 str = get_tv_string_buf(&argvars[0], buf);
15922 do_pyeval(str, rettv);
15923}
15924#endif
15925
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015926/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015927 * "range()" function
15928 */
15929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015930f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015931{
15932 long start;
15933 long end;
15934 long stride = 1;
15935 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015936 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015937
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015938 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015939 if (argvars[1].v_type == VAR_UNKNOWN)
15940 {
15941 end = start - 1;
15942 start = 0;
15943 }
15944 else
15945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015946 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015947 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015948 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015949 }
15950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015951 if (error)
15952 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015953 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015954 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015955 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015956 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015957 else
15958 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015959 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015960 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015961 if (list_append_number(rettv->vval.v_list,
15962 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015963 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015964 }
15965}
15966
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015967/*
15968 * "readfile()" function
15969 */
15970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015971f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015972{
15973 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015974 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015975 char_u *fname;
15976 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015977 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15978 int io_size = sizeof(buf);
15979 int readlen; /* size of last fread() */
15980 char_u *prev = NULL; /* previously read bytes, if any */
15981 long prevlen = 0; /* length of data in prev */
15982 long prevsize = 0; /* size of prev buffer */
15983 long maxline = MAXLNUM;
15984 long cnt = 0;
15985 char_u *p; /* position in buf */
15986 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015987
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015988 if (argvars[1].v_type != VAR_UNKNOWN)
15989 {
15990 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15991 binary = TRUE;
15992 if (argvars[2].v_type != VAR_UNKNOWN)
15993 maxline = get_tv_number(&argvars[2]);
15994 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015995
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015996 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015997 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015998
15999 /* Always open the file in binary mode, library functions have a mind of
16000 * their own about CR-LF conversion. */
16001 fname = get_tv_string(&argvars[0]);
16002 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16003 {
16004 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16005 return;
16006 }
16007
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016008 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016009 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016010 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016011
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016012 /* This for loop processes what was read, but is also entered at end
16013 * of file so that either:
16014 * - an incomplete line gets written
16015 * - a "binary" file gets an empty line at the end if it ends in a
16016 * newline. */
16017 for (p = buf, start = buf;
16018 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16019 ++p)
16020 {
16021 if (*p == '\n' || readlen <= 0)
16022 {
16023 listitem_T *li;
16024 char_u *s = NULL;
16025 long_u len = p - start;
16026
16027 /* Finished a line. Remove CRs before NL. */
16028 if (readlen > 0 && !binary)
16029 {
16030 while (len > 0 && start[len - 1] == '\r')
16031 --len;
16032 /* removal may cross back to the "prev" string */
16033 if (len == 0)
16034 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16035 --prevlen;
16036 }
16037 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016038 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016039 else
16040 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016041 /* Change "prev" buffer to be the right size. This way
16042 * the bytes are only copied once, and very long lines are
16043 * allocated only once. */
16044 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016045 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016046 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016047 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016048 prev = NULL; /* the list will own the string */
16049 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016050 }
16051 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016052 if (s == NULL)
16053 {
16054 do_outofmem_msg((long_u) prevlen + len + 1);
16055 failed = TRUE;
16056 break;
16057 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016058
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016059 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016060 {
16061 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016062 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016063 break;
16064 }
16065 li->li_tv.v_type = VAR_STRING;
16066 li->li_tv.v_lock = 0;
16067 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016068 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016069
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016070 start = p + 1; /* step over newline */
16071 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016072 break;
16073 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016074 else if (*p == NUL)
16075 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016076#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016077 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16078 * when finding the BF and check the previous two bytes. */
16079 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016080 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016081 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16082 * + 1, these may be in the "prev" string. */
16083 char_u back1 = p >= buf + 1 ? p[-1]
16084 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16085 char_u back2 = p >= buf + 2 ? p[-2]
16086 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16087 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016088
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016089 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016090 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016091 char_u *dest = p - 2;
16092
16093 /* Usually a BOM is at the beginning of a file, and so at
16094 * the beginning of a line; then we can just step over it.
16095 */
16096 if (start == dest)
16097 start = p + 1;
16098 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016099 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016100 /* have to shuffle buf to close gap */
16101 int adjust_prevlen = 0;
16102
16103 if (dest < buf)
16104 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016105 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016106 dest = buf;
16107 }
16108 if (readlen > p - buf + 1)
16109 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16110 readlen -= 3 - adjust_prevlen;
16111 prevlen -= adjust_prevlen;
16112 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016113 }
16114 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016115 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016116#endif
16117 } /* for */
16118
16119 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16120 break;
16121 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016122 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016123 /* There's part of a line in buf, store it in "prev". */
16124 if (p - start + prevlen >= prevsize)
16125 {
16126 /* need bigger "prev" buffer */
16127 char_u *newprev;
16128
16129 /* A common use case is ordinary text files and "prev" gets a
16130 * fragment of a line, so the first allocation is made
16131 * small, to avoid repeatedly 'allocing' large and
16132 * 'reallocing' small. */
16133 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016134 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016135 else
16136 {
16137 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016138 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016139 prevsize = grow50pc > growmin ? grow50pc : growmin;
16140 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016141 newprev = prev == NULL ? alloc(prevsize)
16142 : vim_realloc(prev, prevsize);
16143 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016144 {
16145 do_outofmem_msg((long_u)prevsize);
16146 failed = TRUE;
16147 break;
16148 }
16149 prev = newprev;
16150 }
16151 /* Add the line part to end of "prev". */
16152 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016153 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016154 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016155 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016156
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016157 /*
16158 * For a negative line count use only the lines at the end of the file,
16159 * free the rest.
16160 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016161 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016162 while (cnt > -maxline)
16163 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016164 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016165 --cnt;
16166 }
16167
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016168 if (failed)
16169 {
16170 list_free(rettv->vval.v_list, TRUE);
16171 /* readfile doc says an empty list is returned on error */
16172 rettv->vval.v_list = list_alloc();
16173 }
16174
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016175 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016176 fclose(fd);
16177}
16178
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016179#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016180static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016181
16182/*
16183 * Convert a List to proftime_T.
16184 * Return FAIL when there is something wrong.
16185 */
16186 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016187list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016188{
16189 long n1, n2;
16190 int error = FALSE;
16191
16192 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16193 || arg->vval.v_list->lv_len != 2)
16194 return FAIL;
16195 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16196 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16197# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016198 tm->HighPart = n1;
16199 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016200# else
16201 tm->tv_sec = n1;
16202 tm->tv_usec = n2;
16203# endif
16204 return error ? FAIL : OK;
16205}
16206#endif /* FEAT_RELTIME */
16207
16208/*
16209 * "reltime()" function
16210 */
16211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016212f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016213{
16214#ifdef FEAT_RELTIME
16215 proftime_T res;
16216 proftime_T start;
16217
16218 if (argvars[0].v_type == VAR_UNKNOWN)
16219 {
16220 /* No arguments: get current time. */
16221 profile_start(&res);
16222 }
16223 else if (argvars[1].v_type == VAR_UNKNOWN)
16224 {
16225 if (list2proftime(&argvars[0], &res) == FAIL)
16226 return;
16227 profile_end(&res);
16228 }
16229 else
16230 {
16231 /* Two arguments: compute the difference. */
16232 if (list2proftime(&argvars[0], &start) == FAIL
16233 || list2proftime(&argvars[1], &res) == FAIL)
16234 return;
16235 profile_sub(&res, &start);
16236 }
16237
16238 if (rettv_list_alloc(rettv) == OK)
16239 {
16240 long n1, n2;
16241
16242# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016243 n1 = res.HighPart;
16244 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016245# else
16246 n1 = res.tv_sec;
16247 n2 = res.tv_usec;
16248# endif
16249 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16250 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16251 }
16252#endif
16253}
16254
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016255#ifdef FEAT_FLOAT
16256/*
16257 * "reltimefloat()" function
16258 */
16259 static void
16260f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16261{
16262# ifdef FEAT_RELTIME
16263 proftime_T tm;
16264# endif
16265
16266 rettv->v_type = VAR_FLOAT;
16267 rettv->vval.v_float = 0;
16268# ifdef FEAT_RELTIME
16269 if (list2proftime(&argvars[0], &tm) == OK)
16270 rettv->vval.v_float = profile_float(&tm);
16271# endif
16272}
16273#endif
16274
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016275/*
16276 * "reltimestr()" function
16277 */
16278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016279f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016280{
16281#ifdef FEAT_RELTIME
16282 proftime_T tm;
16283#endif
16284
16285 rettv->v_type = VAR_STRING;
16286 rettv->vval.v_string = NULL;
16287#ifdef FEAT_RELTIME
16288 if (list2proftime(&argvars[0], &tm) == OK)
16289 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16290#endif
16291}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016292
Bram Moolenaar0d660222005-01-07 21:51:51 +000016293#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016294static void make_connection(void);
16295static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016296
16297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016298make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016299{
16300 if (X_DISPLAY == NULL
16301# ifdef FEAT_GUI
16302 && !gui.in_use
16303# endif
16304 )
16305 {
16306 x_force_connect = TRUE;
16307 setup_term_clip();
16308 x_force_connect = FALSE;
16309 }
16310}
16311
16312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016313check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016314{
16315 make_connection();
16316 if (X_DISPLAY == NULL)
16317 {
16318 EMSG(_("E240: No connection to Vim server"));
16319 return FAIL;
16320 }
16321 return OK;
16322}
16323#endif
16324
16325#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016326static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016327
16328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016329remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016330{
16331 char_u *server_name;
16332 char_u *keys;
16333 char_u *r = NULL;
16334 char_u buf[NUMBUFLEN];
16335# ifdef WIN32
16336 HWND w;
16337# else
16338 Window w;
16339# endif
16340
16341 if (check_restricted() || check_secure())
16342 return;
16343
16344# ifdef FEAT_X11
16345 if (check_connection() == FAIL)
16346 return;
16347# endif
16348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016349 server_name = get_tv_string_chk(&argvars[0]);
16350 if (server_name == NULL)
16351 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016352 keys = get_tv_string_buf(&argvars[1], buf);
16353# ifdef WIN32
16354 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16355# else
16356 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16357 < 0)
16358# endif
16359 {
16360 if (r != NULL)
16361 EMSG(r); /* sending worked but evaluation failed */
16362 else
16363 EMSG2(_("E241: Unable to send to %s"), server_name);
16364 return;
16365 }
16366
16367 rettv->vval.v_string = r;
16368
16369 if (argvars[2].v_type != VAR_UNKNOWN)
16370 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016371 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016372 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016373 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016374
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016375 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016376 v.di_tv.v_type = VAR_STRING;
16377 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016378 idvar = get_tv_string_chk(&argvars[2]);
16379 if (idvar != NULL)
16380 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016381 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016382 }
16383}
16384#endif
16385
16386/*
16387 * "remote_expr()" function
16388 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016390f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016391{
16392 rettv->v_type = VAR_STRING;
16393 rettv->vval.v_string = NULL;
16394#ifdef FEAT_CLIENTSERVER
16395 remote_common(argvars, rettv, TRUE);
16396#endif
16397}
16398
16399/*
16400 * "remote_foreground()" function
16401 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016403f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016404{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016405#ifdef FEAT_CLIENTSERVER
16406# ifdef WIN32
16407 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016408 {
16409 char_u *server_name = get_tv_string_chk(&argvars[0]);
16410
16411 if (server_name != NULL)
16412 serverForeground(server_name);
16413 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016414# else
16415 /* Send a foreground() expression to the server. */
16416 argvars[1].v_type = VAR_STRING;
16417 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16418 argvars[2].v_type = VAR_UNKNOWN;
16419 remote_common(argvars, rettv, TRUE);
16420 vim_free(argvars[1].vval.v_string);
16421# endif
16422#endif
16423}
16424
Bram Moolenaar0d660222005-01-07 21:51:51 +000016425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016426f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427{
16428#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016429 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016430 char_u *s = NULL;
16431# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016432 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016433# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016434 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016435
16436 if (check_restricted() || check_secure())
16437 {
16438 rettv->vval.v_number = -1;
16439 return;
16440 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016441 serverid = get_tv_string_chk(&argvars[0]);
16442 if (serverid == NULL)
16443 {
16444 rettv->vval.v_number = -1;
16445 return; /* type error; errmsg already given */
16446 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016447# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016448 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016449 if (n == 0)
16450 rettv->vval.v_number = -1;
16451 else
16452 {
16453 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16454 rettv->vval.v_number = (s != NULL);
16455 }
16456# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016457 if (check_connection() == FAIL)
16458 return;
16459
16460 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016461 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016462# endif
16463
16464 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16465 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016466 char_u *retvar;
16467
Bram Moolenaar33570922005-01-25 22:26:29 +000016468 v.di_tv.v_type = VAR_STRING;
16469 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016470 retvar = get_tv_string_chk(&argvars[1]);
16471 if (retvar != NULL)
16472 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016473 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474 }
16475#else
16476 rettv->vval.v_number = -1;
16477#endif
16478}
16479
Bram Moolenaar0d660222005-01-07 21:51:51 +000016480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016481f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016482{
16483 char_u *r = NULL;
16484
16485#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016486 char_u *serverid = get_tv_string_chk(&argvars[0]);
16487
16488 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489 {
16490# ifdef WIN32
16491 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016492 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016493
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016494 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016495 if (n != 0)
16496 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16497 if (r == NULL)
16498# else
16499 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016500 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016501# endif
16502 EMSG(_("E277: Unable to read a server reply"));
16503 }
16504#endif
16505 rettv->v_type = VAR_STRING;
16506 rettv->vval.v_string = r;
16507}
16508
16509/*
16510 * "remote_send()" function
16511 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016513f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016514{
16515 rettv->v_type = VAR_STRING;
16516 rettv->vval.v_string = NULL;
16517#ifdef FEAT_CLIENTSERVER
16518 remote_common(argvars, rettv, FALSE);
16519#endif
16520}
16521
16522/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016523 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016524 */
16525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016526f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016527{
Bram Moolenaar33570922005-01-25 22:26:29 +000016528 list_T *l;
16529 listitem_T *item, *item2;
16530 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016531 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016532 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016533 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016534 dict_T *d;
16535 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016536 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016537
Bram Moolenaar8c711452005-01-14 21:53:12 +000016538 if (argvars[0].v_type == VAR_DICT)
16539 {
16540 if (argvars[2].v_type != VAR_UNKNOWN)
16541 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016542 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016543 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016544 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016545 key = get_tv_string_chk(&argvars[1]);
16546 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016547 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016548 di = dict_find(d, key, -1);
16549 if (di == NULL)
16550 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016551 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16552 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016553 {
16554 *rettv = di->di_tv;
16555 init_tv(&di->di_tv);
16556 dictitem_remove(d, di);
16557 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016558 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016559 }
16560 }
16561 else if (argvars[0].v_type != VAR_LIST)
16562 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016563 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016564 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016565 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016566 int error = FALSE;
16567
16568 idx = get_tv_number_chk(&argvars[1], &error);
16569 if (error)
16570 ; /* type error: do nothing, errmsg already given */
16571 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016572 EMSGN(_(e_listidx), idx);
16573 else
16574 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016575 if (argvars[2].v_type == VAR_UNKNOWN)
16576 {
16577 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016578 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016579 *rettv = item->li_tv;
16580 vim_free(item);
16581 }
16582 else
16583 {
16584 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016585 end = get_tv_number_chk(&argvars[2], &error);
16586 if (error)
16587 ; /* type error: do nothing */
16588 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016589 EMSGN(_(e_listidx), end);
16590 else
16591 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016592 int cnt = 0;
16593
16594 for (li = item; li != NULL; li = li->li_next)
16595 {
16596 ++cnt;
16597 if (li == item2)
16598 break;
16599 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016600 if (li == NULL) /* didn't find "item2" after "item" */
16601 EMSG(_(e_invrange));
16602 else
16603 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016604 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016605 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016606 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016607 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016608 l->lv_first = item;
16609 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016610 item->li_prev = NULL;
16611 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016612 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016613 }
16614 }
16615 }
16616 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016617 }
16618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619}
16620
16621/*
16622 * "rename({from}, {to})" function
16623 */
16624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016625f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626{
16627 char_u buf[NUMBUFLEN];
16628
16629 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016630 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016632 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16633 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634}
16635
16636/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016637 * "repeat()" function
16638 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016640f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016641{
16642 char_u *p;
16643 int n;
16644 int slen;
16645 int len;
16646 char_u *r;
16647 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016648
16649 n = get_tv_number(&argvars[1]);
16650 if (argvars[0].v_type == VAR_LIST)
16651 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016652 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016653 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016654 if (list_extend(rettv->vval.v_list,
16655 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016656 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016657 }
16658 else
16659 {
16660 p = get_tv_string(&argvars[0]);
16661 rettv->v_type = VAR_STRING;
16662 rettv->vval.v_string = NULL;
16663
16664 slen = (int)STRLEN(p);
16665 len = slen * n;
16666 if (len <= 0)
16667 return;
16668
16669 r = alloc(len + 1);
16670 if (r != NULL)
16671 {
16672 for (i = 0; i < n; i++)
16673 mch_memmove(r + i * slen, p, (size_t)slen);
16674 r[len] = NUL;
16675 }
16676
16677 rettv->vval.v_string = r;
16678 }
16679}
16680
16681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 * "resolve()" function
16683 */
16684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016685f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686{
16687 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016688#ifdef HAVE_READLINK
16689 char_u *buf = NULL;
16690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016692 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693#ifdef FEAT_SHORTCUT
16694 {
16695 char_u *v = NULL;
16696
16697 v = mch_resolve_shortcut(p);
16698 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016699 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016701 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702 }
16703#else
16704# ifdef HAVE_READLINK
16705 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706 char_u *cpy;
16707 int len;
16708 char_u *remain = NULL;
16709 char_u *q;
16710 int is_relative_to_current = FALSE;
16711 int has_trailing_pathsep = FALSE;
16712 int limit = 100;
16713
16714 p = vim_strsave(p);
16715
16716 if (p[0] == '.' && (vim_ispathsep(p[1])
16717 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16718 is_relative_to_current = TRUE;
16719
16720 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016721 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016724 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726
16727 q = getnextcomp(p);
16728 if (*q != NUL)
16729 {
16730 /* Separate the first path component in "p", and keep the
16731 * remainder (beginning with the path separator). */
16732 remain = vim_strsave(q - 1);
16733 q[-1] = NUL;
16734 }
16735
Bram Moolenaard9462e32011-04-11 21:35:11 +020016736 buf = alloc(MAXPATHL + 1);
16737 if (buf == NULL)
16738 goto fail;
16739
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740 for (;;)
16741 {
16742 for (;;)
16743 {
16744 len = readlink((char *)p, (char *)buf, MAXPATHL);
16745 if (len <= 0)
16746 break;
16747 buf[len] = NUL;
16748
16749 if (limit-- == 0)
16750 {
16751 vim_free(p);
16752 vim_free(remain);
16753 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016754 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755 goto fail;
16756 }
16757
16758 /* Ensure that the result will have a trailing path separator
16759 * if the argument has one. */
16760 if (remain == NULL && has_trailing_pathsep)
16761 add_pathsep(buf);
16762
16763 /* Separate the first path component in the link value and
16764 * concatenate the remainders. */
16765 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16766 if (*q != NUL)
16767 {
16768 if (remain == NULL)
16769 remain = vim_strsave(q - 1);
16770 else
16771 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016772 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 if (cpy != NULL)
16774 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775 vim_free(remain);
16776 remain = cpy;
16777 }
16778 }
16779 q[-1] = NUL;
16780 }
16781
16782 q = gettail(p);
16783 if (q > p && *q == NUL)
16784 {
16785 /* Ignore trailing path separator. */
16786 q[-1] = NUL;
16787 q = gettail(p);
16788 }
16789 if (q > p && !mch_isFullName(buf))
16790 {
16791 /* symlink is relative to directory of argument */
16792 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16793 if (cpy != NULL)
16794 {
16795 STRCPY(cpy, p);
16796 STRCPY(gettail(cpy), buf);
16797 vim_free(p);
16798 p = cpy;
16799 }
16800 }
16801 else
16802 {
16803 vim_free(p);
16804 p = vim_strsave(buf);
16805 }
16806 }
16807
16808 if (remain == NULL)
16809 break;
16810
16811 /* Append the first path component of "remain" to "p". */
16812 q = getnextcomp(remain + 1);
16813 len = q - remain - (*q != NUL);
16814 cpy = vim_strnsave(p, STRLEN(p) + len);
16815 if (cpy != NULL)
16816 {
16817 STRNCAT(cpy, remain, len);
16818 vim_free(p);
16819 p = cpy;
16820 }
16821 /* Shorten "remain". */
16822 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016823 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 else
16825 {
16826 vim_free(remain);
16827 remain = NULL;
16828 }
16829 }
16830
16831 /* If the result is a relative path name, make it explicitly relative to
16832 * the current directory if and only if the argument had this form. */
16833 if (!vim_ispathsep(*p))
16834 {
16835 if (is_relative_to_current
16836 && *p != NUL
16837 && !(p[0] == '.'
16838 && (p[1] == NUL
16839 || vim_ispathsep(p[1])
16840 || (p[1] == '.'
16841 && (p[2] == NUL
16842 || vim_ispathsep(p[2]))))))
16843 {
16844 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016845 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 if (cpy != NULL)
16847 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 vim_free(p);
16849 p = cpy;
16850 }
16851 }
16852 else if (!is_relative_to_current)
16853 {
16854 /* Strip leading "./". */
16855 q = p;
16856 while (q[0] == '.' && vim_ispathsep(q[1]))
16857 q += 2;
16858 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016859 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860 }
16861 }
16862
16863 /* Ensure that the result will have no trailing path separator
16864 * if the argument had none. But keep "/" or "//". */
16865 if (!has_trailing_pathsep)
16866 {
16867 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016868 if (after_pathsep(p, q))
16869 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 }
16871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016872 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 }
16874# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016875 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876# endif
16877#endif
16878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016879 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880
16881#ifdef HAVE_READLINK
16882fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016883 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016885 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886}
16887
16888/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016889 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890 */
16891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016892f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893{
Bram Moolenaar33570922005-01-25 22:26:29 +000016894 list_T *l;
16895 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896
Bram Moolenaar0d660222005-01-07 21:51:51 +000016897 if (argvars[0].v_type != VAR_LIST)
16898 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016899 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016900 && !tv_check_lock(l->lv_lock,
16901 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016902 {
16903 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016904 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016905 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906 while (li != NULL)
16907 {
16908 ni = li->li_prev;
16909 list_append(l, li);
16910 li = ni;
16911 }
16912 rettv->vval.v_list = l;
16913 rettv->v_type = VAR_LIST;
16914 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016915 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917}
16918
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016919#define SP_NOMOVE 0x01 /* don't move cursor */
16920#define SP_REPEAT 0x02 /* repeat to find outer pair */
16921#define SP_RETCOUNT 0x04 /* return matchcount */
16922#define SP_SETPCMARK 0x08 /* set previous context mark */
16923#define SP_START 0x10 /* accept match at start position */
16924#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16925#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016926#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016927
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016928static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016929
16930/*
16931 * Get flags for a search function.
16932 * Possibly sets "p_ws".
16933 * Returns BACKWARD, FORWARD or zero (for an error).
16934 */
16935 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016936get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016937{
16938 int dir = FORWARD;
16939 char_u *flags;
16940 char_u nbuf[NUMBUFLEN];
16941 int mask;
16942
16943 if (varp->v_type != VAR_UNKNOWN)
16944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016945 flags = get_tv_string_buf_chk(varp, nbuf);
16946 if (flags == NULL)
16947 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948 while (*flags != NUL)
16949 {
16950 switch (*flags)
16951 {
16952 case 'b': dir = BACKWARD; break;
16953 case 'w': p_ws = TRUE; break;
16954 case 'W': p_ws = FALSE; break;
16955 default: mask = 0;
16956 if (flagsp != NULL)
16957 switch (*flags)
16958 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016959 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016960 case 'e': mask = SP_END; break;
16961 case 'm': mask = SP_RETCOUNT; break;
16962 case 'n': mask = SP_NOMOVE; break;
16963 case 'p': mask = SP_SUBPAT; break;
16964 case 'r': mask = SP_REPEAT; break;
16965 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016966 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967 }
16968 if (mask == 0)
16969 {
16970 EMSG2(_(e_invarg2), flags);
16971 dir = 0;
16972 }
16973 else
16974 *flagsp |= mask;
16975 }
16976 if (dir == 0)
16977 break;
16978 ++flags;
16979 }
16980 }
16981 return dir;
16982}
16983
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016985 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016987 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016988search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016990 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 char_u *pat;
16992 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016993 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 int save_p_ws = p_ws;
16995 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016996 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016997 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016998 proftime_T tm;
16999#ifdef FEAT_RELTIME
17000 long time_limit = 0;
17001#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017002 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017003 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017005 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017006 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017007 if (dir == 0)
17008 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017009 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017010 if (flags & SP_START)
17011 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017012 if (flags & SP_END)
17013 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017014 if (flags & SP_COLUMN)
17015 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017016
Bram Moolenaar76929292008-01-06 19:07:36 +000017017 /* Optional arguments: line number to stop searching and timeout. */
17018 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017019 {
17020 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17021 if (lnum_stop < 0)
17022 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017023#ifdef FEAT_RELTIME
17024 if (argvars[3].v_type != VAR_UNKNOWN)
17025 {
17026 time_limit = get_tv_number_chk(&argvars[3], NULL);
17027 if (time_limit < 0)
17028 goto theend;
17029 }
17030#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017031 }
17032
Bram Moolenaar76929292008-01-06 19:07:36 +000017033#ifdef FEAT_RELTIME
17034 /* Set the time limit, if there is one. */
17035 profile_setlimit(time_limit, &tm);
17036#endif
17037
Bram Moolenaar231334e2005-07-25 20:46:57 +000017038 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017039 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017040 * Check to make sure only those flags are set.
17041 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17042 * flags cannot be set. Check for that condition also.
17043 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017044 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017045 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017046 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017047 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017048 goto theend;
17049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017051 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017052 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017053 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017054 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017056 if (flags & SP_SUBPAT)
17057 retval = subpatnum;
17058 else
17059 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017060 if (flags & SP_SETPCMARK)
17061 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017063 if (match_pos != NULL)
17064 {
17065 /* Store the match cursor position */
17066 match_pos->lnum = pos.lnum;
17067 match_pos->col = pos.col + 1;
17068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017069 /* "/$" will put the cursor after the end of the line, may need to
17070 * correct that here */
17071 check_cursor();
17072 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017073
17074 /* If 'n' flag is used: restore cursor position. */
17075 if (flags & SP_NOMOVE)
17076 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017077 else
17078 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017079theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017081
17082 return retval;
17083}
17084
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017085#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017086
17087/*
17088 * round() is not in C90, use ceil() or floor() instead.
17089 */
17090 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017091vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017092{
17093 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17094}
17095
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017096/*
17097 * "round({float})" function
17098 */
17099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017100f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017101{
17102 float_T f;
17103
17104 rettv->v_type = VAR_FLOAT;
17105 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017106 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017107 else
17108 rettv->vval.v_float = 0.0;
17109}
17110#endif
17111
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017112/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017113 * "screenattr()" function
17114 */
17115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017116f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017117{
17118 int row;
17119 int col;
17120 int c;
17121
17122 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17123 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17124 if (row < 0 || row >= screen_Rows
17125 || col < 0 || col >= screen_Columns)
17126 c = -1;
17127 else
17128 c = ScreenAttrs[LineOffset[row] + col];
17129 rettv->vval.v_number = c;
17130}
17131
17132/*
17133 * "screenchar()" function
17134 */
17135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017136f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017137{
17138 int row;
17139 int col;
17140 int off;
17141 int c;
17142
17143 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17144 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17145 if (row < 0 || row >= screen_Rows
17146 || col < 0 || col >= screen_Columns)
17147 c = -1;
17148 else
17149 {
17150 off = LineOffset[row] + col;
17151#ifdef FEAT_MBYTE
17152 if (enc_utf8 && ScreenLinesUC[off] != 0)
17153 c = ScreenLinesUC[off];
17154 else
17155#endif
17156 c = ScreenLines[off];
17157 }
17158 rettv->vval.v_number = c;
17159}
17160
17161/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017162 * "screencol()" function
17163 *
17164 * First column is 1 to be consistent with virtcol().
17165 */
17166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017167f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017168{
17169 rettv->vval.v_number = screen_screencol() + 1;
17170}
17171
17172/*
17173 * "screenrow()" function
17174 */
17175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017176f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017177{
17178 rettv->vval.v_number = screen_screenrow() + 1;
17179}
17180
17181/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017182 * "search()" function
17183 */
17184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017185f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017186{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017187 int flags = 0;
17188
17189 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190}
17191
Bram Moolenaar071d4272004-06-13 20:20:40 +000017192/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017193 * "searchdecl()" function
17194 */
17195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017196f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017197{
17198 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017199 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017200 int error = FALSE;
17201 char_u *name;
17202
17203 rettv->vval.v_number = 1; /* default: FAIL */
17204
17205 name = get_tv_string_chk(&argvars[0]);
17206 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017207 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017208 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017209 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17210 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17211 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017212 if (!error && name != NULL)
17213 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017214 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017215}
17216
17217/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017218 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017220 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017221searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222{
17223 char_u *spat, *mpat, *epat;
17224 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 int dir;
17227 int flags = 0;
17228 char_u nbuf1[NUMBUFLEN];
17229 char_u nbuf2[NUMBUFLEN];
17230 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017231 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017232 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017233 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017236 spat = get_tv_string_chk(&argvars[0]);
17237 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17238 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17239 if (spat == NULL || mpat == NULL || epat == NULL)
17240 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242 /* Handle the optional fourth argument: flags */
17243 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017244 if (dir == 0)
17245 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017246
17247 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017248 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17249 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017250 if ((flags & (SP_END | SP_SUBPAT)) != 0
17251 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017252 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017253 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017254 goto theend;
17255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017257 /* Using 'r' implies 'W', otherwise it doesn't work. */
17258 if (flags & SP_REPEAT)
17259 p_ws = FALSE;
17260
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017261 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017262 if (argvars[3].v_type == VAR_UNKNOWN
17263 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 skip = (char_u *)"";
17265 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017267 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017268 if (argvars[5].v_type != VAR_UNKNOWN)
17269 {
17270 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17271 if (lnum_stop < 0)
17272 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017273#ifdef FEAT_RELTIME
17274 if (argvars[6].v_type != VAR_UNKNOWN)
17275 {
17276 time_limit = get_tv_number_chk(&argvars[6], NULL);
17277 if (time_limit < 0)
17278 goto theend;
17279 }
17280#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017281 }
17282 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017283 if (skip == NULL)
17284 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017286 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017287 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017288
17289theend:
17290 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017291
17292 return retval;
17293}
17294
17295/*
17296 * "searchpair()" function
17297 */
17298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017299f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017300{
17301 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17302}
17303
17304/*
17305 * "searchpairpos()" function
17306 */
17307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017308f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017309{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017310 pos_T match_pos;
17311 int lnum = 0;
17312 int col = 0;
17313
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017314 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017315 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017316
17317 if (searchpair_cmn(argvars, &match_pos) > 0)
17318 {
17319 lnum = match_pos.lnum;
17320 col = match_pos.col;
17321 }
17322
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017323 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17324 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017325}
17326
17327/*
17328 * Search for a start/middle/end thing.
17329 * Used by searchpair(), see its documentation for the details.
17330 * Returns 0 or -1 for no match,
17331 */
17332 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017333do_searchpair(
17334 char_u *spat, /* start pattern */
17335 char_u *mpat, /* middle pattern */
17336 char_u *epat, /* end pattern */
17337 int dir, /* BACKWARD or FORWARD */
17338 char_u *skip, /* skip expression */
17339 int flags, /* SP_SETPCMARK and other SP_ values */
17340 pos_T *match_pos,
17341 linenr_T lnum_stop, /* stop at this line if not zero */
17342 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017343{
17344 char_u *save_cpo;
17345 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17346 long retval = 0;
17347 pos_T pos;
17348 pos_T firstpos;
17349 pos_T foundpos;
17350 pos_T save_cursor;
17351 pos_T save_pos;
17352 int n;
17353 int r;
17354 int nest = 1;
17355 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017356 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017357 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017358
17359 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17360 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017361 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017362
Bram Moolenaar76929292008-01-06 19:07:36 +000017363#ifdef FEAT_RELTIME
17364 /* Set the time limit, if there is one. */
17365 profile_setlimit(time_limit, &tm);
17366#endif
17367
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017368 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17369 * start/middle/end (pat3, for the top pair). */
17370 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17371 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17372 if (pat2 == NULL || pat3 == NULL)
17373 goto theend;
17374 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17375 if (*mpat == NUL)
17376 STRCPY(pat3, pat2);
17377 else
17378 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17379 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017380 if (flags & SP_START)
17381 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017382
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 save_cursor = curwin->w_cursor;
17384 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017385 clearpos(&firstpos);
17386 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 pat = pat3;
17388 for (;;)
17389 {
17390 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017391 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17393 /* didn't find it or found the first match again: FAIL */
17394 break;
17395
17396 if (firstpos.lnum == 0)
17397 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017398 if (equalpos(pos, foundpos))
17399 {
17400 /* Found the same position again. Can happen with a pattern that
17401 * has "\zs" at the end and searching backwards. Advance one
17402 * character and try again. */
17403 if (dir == BACKWARD)
17404 decl(&pos);
17405 else
17406 incl(&pos);
17407 }
17408 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017410 /* clear the start flag to avoid getting stuck here */
17411 options &= ~SEARCH_START;
17412
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413 /* If the skip pattern matches, ignore this match. */
17414 if (*skip != NUL)
17415 {
17416 save_pos = curwin->w_cursor;
17417 curwin->w_cursor = pos;
17418 r = eval_to_bool(skip, &err, NULL, FALSE);
17419 curwin->w_cursor = save_pos;
17420 if (err)
17421 {
17422 /* Evaluating {skip} caused an error, break here. */
17423 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017424 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425 break;
17426 }
17427 if (r)
17428 continue;
17429 }
17430
17431 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17432 {
17433 /* Found end when searching backwards or start when searching
17434 * forward: nested pair. */
17435 ++nest;
17436 pat = pat2; /* nested, don't search for middle */
17437 }
17438 else
17439 {
17440 /* Found end when searching forward or start when searching
17441 * backward: end of (nested) pair; or found middle in outer pair. */
17442 if (--nest == 1)
17443 pat = pat3; /* outer level, search for middle */
17444 }
17445
17446 if (nest == 0)
17447 {
17448 /* Found the match: return matchcount or line number. */
17449 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017450 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017452 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017453 if (flags & SP_SETPCMARK)
17454 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 curwin->w_cursor = pos;
17456 if (!(flags & SP_REPEAT))
17457 break;
17458 nest = 1; /* search for next unmatched */
17459 }
17460 }
17461
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017462 if (match_pos != NULL)
17463 {
17464 /* Store the match cursor position */
17465 match_pos->lnum = curwin->w_cursor.lnum;
17466 match_pos->col = curwin->w_cursor.col + 1;
17467 }
17468
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017470 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 curwin->w_cursor = save_cursor;
17472
17473theend:
17474 vim_free(pat2);
17475 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017476 if (p_cpo == empty_option)
17477 p_cpo = save_cpo;
17478 else
17479 /* Darn, evaluating the {skip} expression changed the value. */
17480 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017481
17482 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483}
17484
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017485/*
17486 * "searchpos()" function
17487 */
17488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017489f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017490{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017491 pos_T match_pos;
17492 int lnum = 0;
17493 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017494 int n;
17495 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017496
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017497 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017498 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017499
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017500 n = search_cmn(argvars, &match_pos, &flags);
17501 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017502 {
17503 lnum = match_pos.lnum;
17504 col = match_pos.col;
17505 }
17506
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017507 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17508 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017509 if (flags & SP_SUBPAT)
17510 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017511}
17512
Bram Moolenaar0d660222005-01-07 21:51:51 +000017513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017514f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017516#ifdef FEAT_CLIENTSERVER
17517 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017518 char_u *server = get_tv_string_chk(&argvars[0]);
17519 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520
Bram Moolenaar0d660222005-01-07 21:51:51 +000017521 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017522 if (server == NULL || reply == NULL)
17523 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017524 if (check_restricted() || check_secure())
17525 return;
17526# ifdef FEAT_X11
17527 if (check_connection() == FAIL)
17528 return;
17529# endif
17530
17531 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017533 EMSG(_("E258: Unable to send to client"));
17534 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017536 rettv->vval.v_number = 0;
17537#else
17538 rettv->vval.v_number = -1;
17539#endif
17540}
17541
Bram Moolenaar0d660222005-01-07 21:51:51 +000017542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017543f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017544{
17545 char_u *r = NULL;
17546
17547#ifdef FEAT_CLIENTSERVER
17548# ifdef WIN32
17549 r = serverGetVimNames();
17550# else
17551 make_connection();
17552 if (X_DISPLAY != NULL)
17553 r = serverGetVimNames(X_DISPLAY);
17554# endif
17555#endif
17556 rettv->v_type = VAR_STRING;
17557 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558}
17559
17560/*
17561 * "setbufvar()" function
17562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017564f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565{
17566 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017569 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570 char_u nbuf[NUMBUFLEN];
17571
17572 if (check_restricted() || check_secure())
17573 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017574 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17575 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017576 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 varp = &argvars[2];
17578
17579 if (buf != NULL && varname != NULL && varp != NULL)
17580 {
17581 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583
17584 if (*varname == '&')
17585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017586 long numval;
17587 char_u *strval;
17588 int error = FALSE;
17589
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017591 numval = get_tv_number_chk(varp, &error);
17592 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017593 if (!error && strval != NULL)
17594 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595 }
17596 else
17597 {
17598 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17599 if (bufvarname != NULL)
17600 {
17601 STRCPY(bufvarname, "b:");
17602 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017603 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604 vim_free(bufvarname);
17605 }
17606 }
17607
17608 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611}
17612
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017614f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017615{
17616 dict_T *d;
17617 dictitem_T *di;
17618 char_u *csearch;
17619
17620 if (argvars[0].v_type != VAR_DICT)
17621 {
17622 EMSG(_(e_dictreq));
17623 return;
17624 }
17625
17626 if ((d = argvars[0].vval.v_dict) != NULL)
17627 {
17628 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17629 if (csearch != NULL)
17630 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017631#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017632 if (enc_utf8)
17633 {
17634 int pcc[MAX_MCO];
17635 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017636
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017637 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17638 }
17639 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017640#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017641 set_last_csearch(PTR2CHAR(csearch),
17642 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017643 }
17644
17645 di = dict_find(d, (char_u *)"forward", -1);
17646 if (di != NULL)
17647 set_csearch_direction(get_tv_number(&di->di_tv)
17648 ? FORWARD : BACKWARD);
17649
17650 di = dict_find(d, (char_u *)"until", -1);
17651 if (di != NULL)
17652 set_csearch_until(!!get_tv_number(&di->di_tv));
17653 }
17654}
17655
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656/*
17657 * "setcmdpos()" function
17658 */
17659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017660f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017662 int pos = (int)get_tv_number(&argvars[0]) - 1;
17663
17664 if (pos >= 0)
17665 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666}
17667
17668/*
17669 * "setline()" function
17670 */
17671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017672f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673{
17674 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017675 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017676 list_T *l = NULL;
17677 listitem_T *li = NULL;
17678 long added = 0;
17679 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017681 lnum = get_tv_lnum(&argvars[0]);
17682 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017684 l = argvars[1].vval.v_list;
17685 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017687 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017688 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017689
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017690 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017691 for (;;)
17692 {
17693 if (l != NULL)
17694 {
17695 /* list argument, get next string */
17696 if (li == NULL)
17697 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017698 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017699 li = li->li_next;
17700 }
17701
17702 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017703 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017704 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017705
17706 /* When coming here from Insert mode, sync undo, so that this can be
17707 * undone separately from what was previously inserted. */
17708 if (u_sync_once == 2)
17709 {
17710 u_sync_once = 1; /* notify that u_sync() was called */
17711 u_sync(TRUE);
17712 }
17713
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017714 if (lnum <= curbuf->b_ml.ml_line_count)
17715 {
17716 /* existing line, replace it */
17717 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17718 {
17719 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017720 if (lnum == curwin->w_cursor.lnum)
17721 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017722 rettv->vval.v_number = 0; /* OK */
17723 }
17724 }
17725 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17726 {
17727 /* lnum is one past the last line, append the line */
17728 ++added;
17729 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17730 rettv->vval.v_number = 0; /* OK */
17731 }
17732
17733 if (l == NULL) /* only one string argument */
17734 break;
17735 ++lnum;
17736 }
17737
17738 if (added > 0)
17739 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740}
17741
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017742static 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 +000017743
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017745 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017746 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017748set_qf_ll_list(
17749 win_T *wp UNUSED,
17750 typval_T *list_arg UNUSED,
17751 typval_T *action_arg UNUSED,
17752 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017753{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017754#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017755 char_u *act;
17756 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017757#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017758
Bram Moolenaar2641f772005-03-25 21:58:17 +000017759 rettv->vval.v_number = -1;
17760
17761#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017762 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017763 EMSG(_(e_listreq));
17764 else
17765 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017766 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017767
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017768 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017769 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017770 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017771 if (act == NULL)
17772 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017773 if (*act == 'a' || *act == 'r')
17774 action = *act;
17775 }
17776
Bram Moolenaar81484f42012-12-05 15:16:47 +010017777 if (l != NULL && set_errorlist(wp, l, action,
17778 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017779 rettv->vval.v_number = 0;
17780 }
17781#endif
17782}
17783
17784/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017785 * "setloclist()" function
17786 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017788f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017789{
17790 win_T *win;
17791
17792 rettv->vval.v_number = -1;
17793
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017794 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017795 if (win != NULL)
17796 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17797}
17798
17799/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017800 * "setmatches()" function
17801 */
17802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017803f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017804{
17805#ifdef FEAT_SEARCH_EXTRA
17806 list_T *l;
17807 listitem_T *li;
17808 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017809 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017810
17811 rettv->vval.v_number = -1;
17812 if (argvars[0].v_type != VAR_LIST)
17813 {
17814 EMSG(_(e_listreq));
17815 return;
17816 }
17817 if ((l = argvars[0].vval.v_list) != NULL)
17818 {
17819
17820 /* To some extent make sure that we are dealing with a list from
17821 * "getmatches()". */
17822 li = l->lv_first;
17823 while (li != NULL)
17824 {
17825 if (li->li_tv.v_type != VAR_DICT
17826 || (d = li->li_tv.vval.v_dict) == NULL)
17827 {
17828 EMSG(_(e_invarg));
17829 return;
17830 }
17831 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017832 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17833 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017834 && dict_find(d, (char_u *)"priority", -1) != NULL
17835 && dict_find(d, (char_u *)"id", -1) != NULL))
17836 {
17837 EMSG(_(e_invarg));
17838 return;
17839 }
17840 li = li->li_next;
17841 }
17842
17843 clear_matches(curwin);
17844 li = l->lv_first;
17845 while (li != NULL)
17846 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017847 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017848 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017849 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017850 char_u *group;
17851 int priority;
17852 int id;
17853 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017854
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017855 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017856 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17857 {
17858 if (s == NULL)
17859 {
17860 s = list_alloc();
17861 if (s == NULL)
17862 return;
17863 }
17864
17865 /* match from matchaddpos() */
17866 for (i = 1; i < 9; i++)
17867 {
17868 sprintf((char *)buf, (char *)"pos%d", i);
17869 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17870 {
17871 if (di->di_tv.v_type != VAR_LIST)
17872 return;
17873
17874 list_append_tv(s, &di->di_tv);
17875 s->lv_refcount++;
17876 }
17877 else
17878 break;
17879 }
17880 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017881
17882 group = get_dict_string(d, (char_u *)"group", FALSE);
17883 priority = (int)get_dict_number(d, (char_u *)"priority");
17884 id = (int)get_dict_number(d, (char_u *)"id");
17885 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17886 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17887 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017888 if (i == 0)
17889 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017890 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017891 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017892 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017893 }
17894 else
17895 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017896 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017897 list_unref(s);
17898 s = NULL;
17899 }
17900
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017901 li = li->li_next;
17902 }
17903 rettv->vval.v_number = 0;
17904 }
17905#endif
17906}
17907
17908/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017909 * "setpos()" function
17910 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017912f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017913{
17914 pos_T pos;
17915 int fnum;
17916 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017917 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017918
Bram Moolenaar08250432008-02-13 11:42:46 +000017919 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017920 name = get_tv_string_chk(argvars);
17921 if (name != NULL)
17922 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017923 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017924 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017925 if (--pos.col < 0)
17926 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017927 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017928 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017929 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017930 if (fnum == curbuf->b_fnum)
17931 {
17932 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017933 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017934 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017935 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017936 curwin->w_set_curswant = FALSE;
17937 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017938 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017939 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017940 }
17941 else
17942 EMSG(_(e_invarg));
17943 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017944 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17945 {
17946 /* set mark */
17947 if (setmark_pos(name[1], &pos, fnum) == OK)
17948 rettv->vval.v_number = 0;
17949 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017950 else
17951 EMSG(_(e_invarg));
17952 }
17953 }
17954}
17955
17956/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017957 * "setqflist()" function
17958 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017960f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017961{
17962 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17963}
17964
17965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966 * "setreg()" function
17967 */
17968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017969f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970{
17971 int regname;
17972 char_u *strregname;
17973 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017974 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 int append;
17976 char_u yank_type;
17977 long block_len;
17978
17979 block_len = -1;
17980 yank_type = MAUTO;
17981 append = FALSE;
17982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017983 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017984 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017986 if (strregname == NULL)
17987 return; /* type error; errmsg already given */
17988 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 if (regname == 0 || regname == '@')
17990 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017992 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017994 stropt = get_tv_string_chk(&argvars[2]);
17995 if (stropt == NULL)
17996 return; /* type error */
17997 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998 switch (*stropt)
17999 {
18000 case 'a': case 'A': /* append */
18001 append = TRUE;
18002 break;
18003 case 'v': case 'c': /* character-wise selection */
18004 yank_type = MCHAR;
18005 break;
18006 case 'V': case 'l': /* line-wise selection */
18007 yank_type = MLINE;
18008 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 case 'b': case Ctrl_V: /* block-wise selection */
18010 yank_type = MBLOCK;
18011 if (VIM_ISDIGIT(stropt[1]))
18012 {
18013 ++stropt;
18014 block_len = getdigits(&stropt) - 1;
18015 --stropt;
18016 }
18017 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018 }
18019 }
18020
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018021 if (argvars[1].v_type == VAR_LIST)
18022 {
18023 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018024 char_u **allocval;
18025 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018026 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018027 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018028 int len = argvars[1].vval.v_list->lv_len;
18029 listitem_T *li;
18030
Bram Moolenaar7d647822014-04-05 21:28:56 +020018031 /* First half: use for pointers to result lines; second half: use for
18032 * pointers to allocated copies. */
18033 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018034 if (lstval == NULL)
18035 return;
18036 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018037 allocval = lstval + len + 2;
18038 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018039
18040 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18041 li = li->li_next)
18042 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018043 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018044 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018045 goto free_lstval;
18046 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018047 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018048 /* Need to make a copy, next get_tv_string_buf_chk() will
18049 * overwrite the string. */
18050 strval = vim_strsave(buf);
18051 if (strval == NULL)
18052 goto free_lstval;
18053 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018054 }
18055 *curval++ = strval;
18056 }
18057 *curval++ = NULL;
18058
18059 write_reg_contents_lst(regname, lstval, -1,
18060 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018061free_lstval:
18062 while (curallocval > allocval)
18063 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018064 vim_free(lstval);
18065 }
18066 else
18067 {
18068 strval = get_tv_string_chk(&argvars[1]);
18069 if (strval == NULL)
18070 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018071 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018074 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075}
18076
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018077/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018078 * "settabvar()" function
18079 */
18080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018081f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018082{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018083#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018084 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018085 tabpage_T *tp;
18086#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018087 char_u *varname, *tabvarname;
18088 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018089
18090 rettv->vval.v_number = 0;
18091
18092 if (check_restricted() || check_secure())
18093 return;
18094
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018095#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018096 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018097#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018098 varname = get_tv_string_chk(&argvars[1]);
18099 varp = &argvars[2];
18100
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018101 if (varname != NULL && varp != NULL
18102#ifdef FEAT_WINDOWS
18103 && tp != NULL
18104#endif
18105 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018106 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018107#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018108 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018109 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018110#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018111
18112 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18113 if (tabvarname != NULL)
18114 {
18115 STRCPY(tabvarname, "t:");
18116 STRCPY(tabvarname + 2, varname);
18117 set_var(tabvarname, varp, TRUE);
18118 vim_free(tabvarname);
18119 }
18120
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018121#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018122 /* Restore current tabpage */
18123 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018124 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018125#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018126 }
18127}
18128
18129/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018130 * "settabwinvar()" function
18131 */
18132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018133f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018134{
18135 setwinvar(argvars, rettv, 1);
18136}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137
18138/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018139 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018142f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018144 setwinvar(argvars, rettv, 0);
18145}
18146
18147/*
18148 * "setwinvar()" and "settabwinvar()" functions
18149 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018150
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018152setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018153{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 win_T *win;
18155#ifdef FEAT_WINDOWS
18156 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018157 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018158 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159#endif
18160 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018161 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018163 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164
18165 if (check_restricted() || check_secure())
18166 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018167
18168#ifdef FEAT_WINDOWS
18169 if (off == 1)
18170 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18171 else
18172 tp = curtab;
18173#endif
18174 win = find_win_by_nr(&argvars[off], tp);
18175 varname = get_tv_string_chk(&argvars[off + 1]);
18176 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177
18178 if (win != NULL && varname != NULL && varp != NULL)
18179 {
18180#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018181 need_switch_win = !(tp == curtab && win == curwin);
18182 if (!need_switch_win
18183 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018186 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018188 long numval;
18189 char_u *strval;
18190 int error = FALSE;
18191
18192 ++varname;
18193 numval = get_tv_number_chk(varp, &error);
18194 strval = get_tv_string_buf_chk(varp, nbuf);
18195 if (!error && strval != NULL)
18196 set_option_value(varname, numval, strval, OPT_LOCAL);
18197 }
18198 else
18199 {
18200 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18201 if (winvarname != NULL)
18202 {
18203 STRCPY(winvarname, "w:");
18204 STRCPY(winvarname + 2, varname);
18205 set_var(winvarname, varp, TRUE);
18206 vim_free(winvarname);
18207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 }
18209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018211 if (need_switch_win)
18212 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213#endif
18214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215}
18216
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018217#ifdef FEAT_CRYPT
18218/*
18219 * "sha256({string})" function
18220 */
18221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018222f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018223{
18224 char_u *p;
18225
18226 p = get_tv_string(&argvars[0]);
18227 rettv->vval.v_string = vim_strsave(
18228 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18229 rettv->v_type = VAR_STRING;
18230}
18231#endif /* FEAT_CRYPT */
18232
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018234 * "shellescape({string})" function
18235 */
18236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018237f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018238{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018239 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018240 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018241 rettv->v_type = VAR_STRING;
18242}
18243
18244/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018245 * shiftwidth() function
18246 */
18247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018248f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018249{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018250 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018251}
18252
18253/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018254 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 */
18256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018257f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018259 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260
Bram Moolenaar0d660222005-01-07 21:51:51 +000018261 p = get_tv_string(&argvars[0]);
18262 rettv->vval.v_string = vim_strsave(p);
18263 simplify_filename(rettv->vval.v_string); /* simplify in place */
18264 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265}
18266
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018267#ifdef FEAT_FLOAT
18268/*
18269 * "sin()" function
18270 */
18271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018272f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018273{
18274 float_T f;
18275
18276 rettv->v_type = VAR_FLOAT;
18277 if (get_float_arg(argvars, &f) == OK)
18278 rettv->vval.v_float = sin(f);
18279 else
18280 rettv->vval.v_float = 0.0;
18281}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018282
18283/*
18284 * "sinh()" function
18285 */
18286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018287f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018288{
18289 float_T f;
18290
18291 rettv->v_type = VAR_FLOAT;
18292 if (get_float_arg(argvars, &f) == OK)
18293 rettv->vval.v_float = sinh(f);
18294 else
18295 rettv->vval.v_float = 0.0;
18296}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018297#endif
18298
Bram Moolenaar0d660222005-01-07 21:51:51 +000018299static int
18300#ifdef __BORLANDC__
18301 _RTLENTRYF
18302#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018303 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018304static int
18305#ifdef __BORLANDC__
18306 _RTLENTRYF
18307#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018308 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018309
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018310/* struct used in the array that's given to qsort() */
18311typedef struct
18312{
18313 listitem_T *item;
18314 int idx;
18315} sortItem_T;
18316
Bram Moolenaar0d660222005-01-07 21:51:51 +000018317static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018318static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018319static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018320#ifdef FEAT_FLOAT
18321static int item_compare_float;
18322#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018323static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018324static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018325static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018326static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018327static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018328#define ITEM_COMPARE_FAIL 999
18329
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018331 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018333 static int
18334#ifdef __BORLANDC__
18335_RTLENTRYF
18336#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018337item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018339 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018340 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018341 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018342 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018343 int res;
18344 char_u numbuf1[NUMBUFLEN];
18345 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018347 si1 = (sortItem_T *)s1;
18348 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018349 tv1 = &si1->item->li_tv;
18350 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018351
18352 if (item_compare_numbers)
18353 {
18354 long v1 = get_tv_number(tv1);
18355 long v2 = get_tv_number(tv2);
18356
18357 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18358 }
18359
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018360#ifdef FEAT_FLOAT
18361 if (item_compare_float)
18362 {
18363 float_T v1 = get_tv_float(tv1);
18364 float_T v2 = get_tv_float(tv2);
18365
18366 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18367 }
18368#endif
18369
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018370 /* tv2string() puts quotes around a string and allocates memory. Don't do
18371 * that for string variables. Use a single quote when comparing with a
18372 * non-string to do what the docs promise. */
18373 if (tv1->v_type == VAR_STRING)
18374 {
18375 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18376 p1 = (char_u *)"'";
18377 else
18378 p1 = tv1->vval.v_string;
18379 }
18380 else
18381 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18382 if (tv2->v_type == VAR_STRING)
18383 {
18384 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18385 p2 = (char_u *)"'";
18386 else
18387 p2 = tv2->vval.v_string;
18388 }
18389 else
18390 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018391 if (p1 == NULL)
18392 p1 = (char_u *)"";
18393 if (p2 == NULL)
18394 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018395 if (!item_compare_numeric)
18396 {
18397 if (item_compare_ic)
18398 res = STRICMP(p1, p2);
18399 else
18400 res = STRCMP(p1, p2);
18401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018403 {
18404 double n1, n2;
18405 n1 = strtod((char *)p1, (char **)&p1);
18406 n2 = strtod((char *)p2, (char **)&p2);
18407 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18408 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018409
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018410 /* When the result would be zero, compare the item indexes. Makes the
18411 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018412 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018413 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018414
Bram Moolenaar0d660222005-01-07 21:51:51 +000018415 vim_free(tofree1);
18416 vim_free(tofree2);
18417 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418}
18419
18420 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018421#ifdef __BORLANDC__
18422_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018424item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018425{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018426 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018427 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018428 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018429 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018430 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018432 /* shortcut after failure in previous call; compare all items equal */
18433 if (item_compare_func_err)
18434 return 0;
18435
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018436 si1 = (sortItem_T *)s1;
18437 si2 = (sortItem_T *)s2;
18438
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018439 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018440 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018441 copy_tv(&si1->item->li_tv, &argv[0]);
18442 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018443
18444 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018445 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018446 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18447 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018448 clear_tv(&argv[0]);
18449 clear_tv(&argv[1]);
18450
18451 if (res == FAIL)
18452 res = ITEM_COMPARE_FAIL;
18453 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018454 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18455 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018456 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018457 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018458
18459 /* When the result would be zero, compare the pointers themselves. Makes
18460 * the sort stable. */
18461 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018462 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018463
Bram Moolenaar0d660222005-01-07 21:51:51 +000018464 return res;
18465}
18466
18467/*
18468 * "sort({list})" function
18469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018471do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472{
Bram Moolenaar33570922005-01-25 22:26:29 +000018473 list_T *l;
18474 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018475 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018476 long len;
18477 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478
Bram Moolenaar0d660222005-01-07 21:51:51 +000018479 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018480 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481 else
18482 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018483 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018484 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018485 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18486 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018487 return;
18488 rettv->vval.v_list = l;
18489 rettv->v_type = VAR_LIST;
18490 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491
Bram Moolenaar0d660222005-01-07 21:51:51 +000018492 len = list_len(l);
18493 if (len <= 1)
18494 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495
Bram Moolenaar0d660222005-01-07 21:51:51 +000018496 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018497 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018498 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018499#ifdef FEAT_FLOAT
18500 item_compare_float = FALSE;
18501#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018502 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018503 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018504 if (argvars[1].v_type != VAR_UNKNOWN)
18505 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018506 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018507 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018508 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018509 else
18510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018511 int error = FALSE;
18512
18513 i = get_tv_number_chk(&argvars[1], &error);
18514 if (error)
18515 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018516 if (i == 1)
18517 item_compare_ic = TRUE;
18518 else
18519 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018520 if (item_compare_func != NULL)
18521 {
18522 if (STRCMP(item_compare_func, "n") == 0)
18523 {
18524 item_compare_func = NULL;
18525 item_compare_numeric = TRUE;
18526 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018527 else if (STRCMP(item_compare_func, "N") == 0)
18528 {
18529 item_compare_func = NULL;
18530 item_compare_numbers = TRUE;
18531 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018532#ifdef FEAT_FLOAT
18533 else if (STRCMP(item_compare_func, "f") == 0)
18534 {
18535 item_compare_func = NULL;
18536 item_compare_float = TRUE;
18537 }
18538#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018539 else if (STRCMP(item_compare_func, "i") == 0)
18540 {
18541 item_compare_func = NULL;
18542 item_compare_ic = TRUE;
18543 }
18544 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018545 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018546
18547 if (argvars[2].v_type != VAR_UNKNOWN)
18548 {
18549 /* optional third argument: {dict} */
18550 if (argvars[2].v_type != VAR_DICT)
18551 {
18552 EMSG(_(e_dictreq));
18553 return;
18554 }
18555 item_compare_selfdict = argvars[2].vval.v_dict;
18556 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558
Bram Moolenaar0d660222005-01-07 21:51:51 +000018559 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018560 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018561 if (ptrs == NULL)
18562 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563
Bram Moolenaar327aa022014-03-25 18:24:23 +010018564 i = 0;
18565 if (sort)
18566 {
18567 /* sort(): ptrs will be the list to sort */
18568 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018569 {
18570 ptrs[i].item = li;
18571 ptrs[i].idx = i;
18572 ++i;
18573 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018574
18575 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018576 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018577 /* test the compare function */
18578 if (item_compare_func != NULL
18579 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018580 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018581 EMSG(_("E702: Sort compare function failed"));
18582 else
18583 {
18584 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018585 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018586 item_compare_func == NULL ? item_compare : item_compare2);
18587
18588 if (!item_compare_func_err)
18589 {
18590 /* Clear the List and append the items in sorted order. */
18591 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18592 l->lv_len = 0;
18593 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018594 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018595 }
18596 }
18597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018599 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018600 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018601
18602 /* f_uniq(): ptrs will be a stack of items to remove */
18603 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018604 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018605 item_compare_func_ptr = item_compare_func
18606 ? item_compare2 : item_compare;
18607
18608 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18609 li = li->li_next)
18610 {
18611 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18612 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018613 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018614 if (item_compare_func_err)
18615 {
18616 EMSG(_("E882: Uniq compare function failed"));
18617 break;
18618 }
18619 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018621 if (!item_compare_func_err)
18622 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018623 while (--i >= 0)
18624 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018625 li = ptrs[i].item->li_next;
18626 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018627 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018628 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018629 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018630 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018631 list_fix_watch(l, li);
18632 listitem_free(li);
18633 l->lv_len--;
18634 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018635 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018636 }
18637
18638 vim_free(ptrs);
18639 }
18640}
18641
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018642/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018643 * "sort({list})" function
18644 */
18645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018646f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018647{
18648 do_sort_uniq(argvars, rettv, TRUE);
18649}
18650
18651/*
18652 * "uniq({list})" function
18653 */
18654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018655f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018656{
18657 do_sort_uniq(argvars, rettv, FALSE);
18658}
18659
18660/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018661 * "soundfold({word})" function
18662 */
18663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018664f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018665{
18666 char_u *s;
18667
18668 rettv->v_type = VAR_STRING;
18669 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018670#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018671 rettv->vval.v_string = eval_soundfold(s);
18672#else
18673 rettv->vval.v_string = vim_strsave(s);
18674#endif
18675}
18676
18677/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018678 * "spellbadword()" function
18679 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018681f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018682{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018683 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018684 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018685 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018686
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018687 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018688 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018689
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018690#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018691 if (argvars[0].v_type == VAR_UNKNOWN)
18692 {
18693 /* Find the start and length of the badly spelled word. */
18694 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18695 if (len != 0)
18696 word = ml_get_cursor();
18697 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018698 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018699 {
18700 char_u *str = get_tv_string_chk(&argvars[0]);
18701 int capcol = -1;
18702
18703 if (str != NULL)
18704 {
18705 /* Check the argument for spelling. */
18706 while (*str != NUL)
18707 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018708 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018709 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018710 {
18711 word = str;
18712 break;
18713 }
18714 str += len;
18715 }
18716 }
18717 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018718#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018719
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018720 list_append_string(rettv->vval.v_list, word, len);
18721 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018722 attr == HLF_SPB ? "bad" :
18723 attr == HLF_SPR ? "rare" :
18724 attr == HLF_SPL ? "local" :
18725 attr == HLF_SPC ? "caps" :
18726 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018727}
18728
18729/*
18730 * "spellsuggest()" function
18731 */
18732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018733f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018734{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018735#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018736 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018737 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018738 int maxcount;
18739 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018740 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018741 listitem_T *li;
18742 int need_capital = FALSE;
18743#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018744
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018745 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018746 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018747
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018748#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018749 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018750 {
18751 str = get_tv_string(&argvars[0]);
18752 if (argvars[1].v_type != VAR_UNKNOWN)
18753 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018754 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018755 if (maxcount <= 0)
18756 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018757 if (argvars[2].v_type != VAR_UNKNOWN)
18758 {
18759 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18760 if (typeerr)
18761 return;
18762 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018763 }
18764 else
18765 maxcount = 25;
18766
Bram Moolenaar4770d092006-01-12 23:22:24 +000018767 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018768
18769 for (i = 0; i < ga.ga_len; ++i)
18770 {
18771 str = ((char_u **)ga.ga_data)[i];
18772
18773 li = listitem_alloc();
18774 if (li == NULL)
18775 vim_free(str);
18776 else
18777 {
18778 li->li_tv.v_type = VAR_STRING;
18779 li->li_tv.v_lock = 0;
18780 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018781 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018782 }
18783 }
18784 ga_clear(&ga);
18785 }
18786#endif
18787}
18788
Bram Moolenaar0d660222005-01-07 21:51:51 +000018789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018790f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018791{
18792 char_u *str;
18793 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018794 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018795 regmatch_T regmatch;
18796 char_u patbuf[NUMBUFLEN];
18797 char_u *save_cpo;
18798 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018799 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018800 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018801 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018802
18803 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18804 save_cpo = p_cpo;
18805 p_cpo = (char_u *)"";
18806
18807 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018808 if (argvars[1].v_type != VAR_UNKNOWN)
18809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018810 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18811 if (pat == NULL)
18812 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018813 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018814 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018815 }
18816 if (pat == NULL || *pat == NUL)
18817 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018818
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018819 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018821 if (typeerr)
18822 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823
Bram Moolenaar0d660222005-01-07 21:51:51 +000018824 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18825 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018827 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018828 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018829 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018830 if (*str == NUL)
18831 match = FALSE; /* empty item at the end */
18832 else
18833 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018834 if (match)
18835 end = regmatch.startp[0];
18836 else
18837 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018838 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18839 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018840 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018841 if (list_append_string(rettv->vval.v_list, str,
18842 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018843 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018844 }
18845 if (!match)
18846 break;
18847 /* Advance to just after the match. */
18848 if (regmatch.endp[0] > str)
18849 col = 0;
18850 else
18851 {
18852 /* Don't get stuck at the same match. */
18853#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018854 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018855#else
18856 col = 1;
18857#endif
18858 }
18859 str = regmatch.endp[0];
18860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861
Bram Moolenaar473de612013-06-08 18:19:48 +020018862 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864
Bram Moolenaar0d660222005-01-07 21:51:51 +000018865 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866}
18867
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018868#ifdef FEAT_FLOAT
18869/*
18870 * "sqrt()" function
18871 */
18872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018873f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018874{
18875 float_T f;
18876
18877 rettv->v_type = VAR_FLOAT;
18878 if (get_float_arg(argvars, &f) == OK)
18879 rettv->vval.v_float = sqrt(f);
18880 else
18881 rettv->vval.v_float = 0.0;
18882}
18883
18884/*
18885 * "str2float()" function
18886 */
18887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018888f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018889{
18890 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18891
18892 if (*p == '+')
18893 p = skipwhite(p + 1);
18894 (void)string2float(p, &rettv->vval.v_float);
18895 rettv->v_type = VAR_FLOAT;
18896}
18897#endif
18898
Bram Moolenaar2c932302006-03-18 21:42:09 +000018899/*
18900 * "str2nr()" function
18901 */
18902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018903f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018904{
18905 int base = 10;
18906 char_u *p;
18907 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018908 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018909
18910 if (argvars[1].v_type != VAR_UNKNOWN)
18911 {
18912 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018913 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018914 {
18915 EMSG(_(e_invarg));
18916 return;
18917 }
18918 }
18919
18920 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018921 if (*p == '+')
18922 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018923 switch (base)
18924 {
18925 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18926 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18927 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18928 default: what = 0;
18929 }
18930 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018931 rettv->vval.v_number = n;
18932}
18933
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934#ifdef HAVE_STRFTIME
18935/*
18936 * "strftime({format}[, {time}])" function
18937 */
18938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018939f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940{
18941 char_u result_buf[256];
18942 struct tm *curtime;
18943 time_t seconds;
18944 char_u *p;
18945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018946 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018948 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018949 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950 seconds = time(NULL);
18951 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018952 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018953 curtime = localtime(&seconds);
18954 /* MSVC returns NULL for an invalid value of seconds. */
18955 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018956 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957 else
18958 {
18959# ifdef FEAT_MBYTE
18960 vimconv_T conv;
18961 char_u *enc;
18962
18963 conv.vc_type = CONV_NONE;
18964 enc = enc_locale();
18965 convert_setup(&conv, p_enc, enc);
18966 if (conv.vc_type != CONV_NONE)
18967 p = string_convert(&conv, p, NULL);
18968# endif
18969 if (p != NULL)
18970 (void)strftime((char *)result_buf, sizeof(result_buf),
18971 (char *)p, curtime);
18972 else
18973 result_buf[0] = NUL;
18974
18975# ifdef FEAT_MBYTE
18976 if (conv.vc_type != CONV_NONE)
18977 vim_free(p);
18978 convert_setup(&conv, enc, p_enc);
18979 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018980 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 else
18982# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018983 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984
18985# ifdef FEAT_MBYTE
18986 /* Release conversion descriptors */
18987 convert_setup(&conv, NULL, NULL);
18988 vim_free(enc);
18989# endif
18990 }
18991}
18992#endif
18993
18994/*
18995 * "stridx()" function
18996 */
18997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018998f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999{
19000 char_u buf[NUMBUFLEN];
19001 char_u *needle;
19002 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019003 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019005 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019007 needle = get_tv_string_chk(&argvars[1]);
19008 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019009 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019010 if (needle == NULL || haystack == NULL)
19011 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012
Bram Moolenaar33570922005-01-25 22:26:29 +000019013 if (argvars[2].v_type != VAR_UNKNOWN)
19014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019015 int error = FALSE;
19016
19017 start_idx = get_tv_number_chk(&argvars[2], &error);
19018 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019019 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019020 if (start_idx >= 0)
19021 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019022 }
19023
19024 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19025 if (pos != NULL)
19026 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027}
19028
19029/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019030 * "string()" function
19031 */
19032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019033f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019034{
19035 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019036 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019037
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019038 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019039 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019040 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019041 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019042 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043}
19044
19045/*
19046 * "strlen()" function
19047 */
19048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019049f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019051 rettv->vval.v_number = (varnumber_T)(STRLEN(
19052 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053}
19054
19055/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019056 * "strchars()" function
19057 */
19058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019059f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019060{
19061 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019062 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019063#ifdef FEAT_MBYTE
19064 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019065 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019066#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019067
19068 if (argvars[1].v_type != VAR_UNKNOWN)
19069 skipcc = get_tv_number_chk(&argvars[1], NULL);
19070 if (skipcc < 0 || skipcc > 1)
19071 EMSG(_(e_invarg));
19072 else
19073 {
19074#ifdef FEAT_MBYTE
19075 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19076 while (*s != NUL)
19077 {
19078 func_mb_ptr2char_adv(&s);
19079 ++len;
19080 }
19081 rettv->vval.v_number = len;
19082#else
19083 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19084#endif
19085 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019086}
19087
19088/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019089 * "strdisplaywidth()" function
19090 */
19091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019092f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019093{
19094 char_u *s = get_tv_string(&argvars[0]);
19095 int col = 0;
19096
19097 if (argvars[1].v_type != VAR_UNKNOWN)
19098 col = get_tv_number(&argvars[1]);
19099
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019100 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019101}
19102
19103/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019104 * "strwidth()" function
19105 */
19106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019107f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019108{
19109 char_u *s = get_tv_string(&argvars[0]);
19110
19111 rettv->vval.v_number = (varnumber_T)(
19112#ifdef FEAT_MBYTE
19113 mb_string2cells(s, -1)
19114#else
19115 STRLEN(s)
19116#endif
19117 );
19118}
19119
19120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 * "strpart()" function
19122 */
19123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019124f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125{
19126 char_u *p;
19127 int n;
19128 int len;
19129 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019130 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019132 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 slen = (int)STRLEN(p);
19134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019135 n = get_tv_number_chk(&argvars[1], &error);
19136 if (error)
19137 len = 0;
19138 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019139 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140 else
19141 len = slen - n; /* default len: all bytes that are available. */
19142
19143 /*
19144 * Only return the overlap between the specified part and the actual
19145 * string.
19146 */
19147 if (n < 0)
19148 {
19149 len += n;
19150 n = 0;
19151 }
19152 else if (n > slen)
19153 n = slen;
19154 if (len < 0)
19155 len = 0;
19156 else if (n + len > slen)
19157 len = slen - n;
19158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019159 rettv->v_type = VAR_STRING;
19160 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161}
19162
19163/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019164 * "strridx()" function
19165 */
19166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019167f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019168{
19169 char_u buf[NUMBUFLEN];
19170 char_u *needle;
19171 char_u *haystack;
19172 char_u *rest;
19173 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019174 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019176 needle = get_tv_string_chk(&argvars[1]);
19177 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019178
19179 rettv->vval.v_number = -1;
19180 if (needle == NULL || haystack == NULL)
19181 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019182
19183 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019184 if (argvars[2].v_type != VAR_UNKNOWN)
19185 {
19186 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019187 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019188 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019189 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019190 }
19191 else
19192 end_idx = haystack_len;
19193
Bram Moolenaar0d660222005-01-07 21:51:51 +000019194 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019195 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019196 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019197 lastmatch = haystack + end_idx;
19198 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019199 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019200 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019201 for (rest = haystack; *rest != '\0'; ++rest)
19202 {
19203 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019204 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019205 break;
19206 lastmatch = rest;
19207 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019208 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019209
19210 if (lastmatch == NULL)
19211 rettv->vval.v_number = -1;
19212 else
19213 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19214}
19215
19216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217 * "strtrans()" function
19218 */
19219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019220f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019222 rettv->v_type = VAR_STRING;
19223 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224}
19225
19226/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019227 * "submatch()" function
19228 */
19229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019230f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019231{
Bram Moolenaar41571762014-04-02 19:00:58 +020019232 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019233 int no;
19234 int retList = 0;
19235
19236 no = (int)get_tv_number_chk(&argvars[0], &error);
19237 if (error)
19238 return;
19239 error = FALSE;
19240 if (argvars[1].v_type != VAR_UNKNOWN)
19241 retList = get_tv_number_chk(&argvars[1], &error);
19242 if (error)
19243 return;
19244
19245 if (retList == 0)
19246 {
19247 rettv->v_type = VAR_STRING;
19248 rettv->vval.v_string = reg_submatch(no);
19249 }
19250 else
19251 {
19252 rettv->v_type = VAR_LIST;
19253 rettv->vval.v_list = reg_submatch_list(no);
19254 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019255}
19256
19257/*
19258 * "substitute()" function
19259 */
19260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019261f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019262{
19263 char_u patbuf[NUMBUFLEN];
19264 char_u subbuf[NUMBUFLEN];
19265 char_u flagsbuf[NUMBUFLEN];
19266
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019267 char_u *str = get_tv_string_chk(&argvars[0]);
19268 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19269 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19270 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19271
Bram Moolenaar0d660222005-01-07 21:51:51 +000019272 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019273 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19274 rettv->vval.v_string = NULL;
19275 else
19276 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019277}
19278
19279/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019280 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019283f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284{
19285 int id = 0;
19286#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019287 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 long col;
19289 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019290 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019292 lnum = get_tv_lnum(argvars); /* -1 on type error */
19293 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19294 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019296 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019297 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019298 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299#endif
19300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019301 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302}
19303
19304/*
19305 * "synIDattr(id, what [, mode])" function
19306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019308f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309{
19310 char_u *p = NULL;
19311#ifdef FEAT_SYN_HL
19312 int id;
19313 char_u *what;
19314 char_u *mode;
19315 char_u modebuf[NUMBUFLEN];
19316 int modec;
19317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019318 id = get_tv_number(&argvars[0]);
19319 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019320 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019322 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019324 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325 modec = 0; /* replace invalid with current */
19326 }
19327 else
19328 {
19329#ifdef FEAT_GUI
19330 if (gui.in_use)
19331 modec = 'g';
19332 else
19333#endif
19334 if (t_colors > 1)
19335 modec = 'c';
19336 else
19337 modec = 't';
19338 }
19339
19340
19341 switch (TOLOWER_ASC(what[0]))
19342 {
19343 case 'b':
19344 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19345 p = highlight_color(id, what, modec);
19346 else /* bold */
19347 p = highlight_has_attr(id, HL_BOLD, modec);
19348 break;
19349
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019350 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351 p = highlight_color(id, what, modec);
19352 break;
19353
19354 case 'i':
19355 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19356 p = highlight_has_attr(id, HL_INVERSE, modec);
19357 else /* italic */
19358 p = highlight_has_attr(id, HL_ITALIC, modec);
19359 break;
19360
19361 case 'n': /* name */
19362 p = get_highlight_name(NULL, id - 1);
19363 break;
19364
19365 case 'r': /* reverse */
19366 p = highlight_has_attr(id, HL_INVERSE, modec);
19367 break;
19368
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019369 case 's':
19370 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19371 p = highlight_color(id, what, modec);
19372 else /* standout */
19373 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374 break;
19375
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019376 case 'u':
19377 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19378 /* underline */
19379 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19380 else
19381 /* undercurl */
19382 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 break;
19384 }
19385
19386 if (p != NULL)
19387 p = vim_strsave(p);
19388#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019389 rettv->v_type = VAR_STRING;
19390 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391}
19392
19393/*
19394 * "synIDtrans(id)" function
19395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019397f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398{
19399 int id;
19400
19401#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019402 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403
19404 if (id > 0)
19405 id = syn_get_final_id(id);
19406 else
19407#endif
19408 id = 0;
19409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019410 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411}
19412
19413/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019414 * "synconcealed(lnum, col)" function
19415 */
19416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019417f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019418{
19419#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19420 long lnum;
19421 long col;
19422 int syntax_flags = 0;
19423 int cchar;
19424 int matchid = 0;
19425 char_u str[NUMBUFLEN];
19426#endif
19427
19428 rettv->v_type = VAR_LIST;
19429 rettv->vval.v_list = NULL;
19430
19431#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19432 lnum = get_tv_lnum(argvars); /* -1 on type error */
19433 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19434
19435 vim_memset(str, NUL, sizeof(str));
19436
19437 if (rettv_list_alloc(rettv) != FAIL)
19438 {
19439 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19440 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19441 && curwin->w_p_cole > 0)
19442 {
19443 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19444 syntax_flags = get_syntax_info(&matchid);
19445
19446 /* get the conceal character */
19447 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19448 {
19449 cchar = syn_get_sub_char();
19450 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19451 cchar = lcs_conceal;
19452 if (cchar != NUL)
19453 {
19454# ifdef FEAT_MBYTE
19455 if (has_mbyte)
19456 (*mb_char2bytes)(cchar, str);
19457 else
19458# endif
19459 str[0] = cchar;
19460 }
19461 }
19462 }
19463
19464 list_append_number(rettv->vval.v_list,
19465 (syntax_flags & HL_CONCEAL) != 0);
19466 /* -1 to auto-determine strlen */
19467 list_append_string(rettv->vval.v_list, str, -1);
19468 list_append_number(rettv->vval.v_list, matchid);
19469 }
19470#endif
19471}
19472
19473/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019474 * "synstack(lnum, col)" function
19475 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019477f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019478{
19479#ifdef FEAT_SYN_HL
19480 long lnum;
19481 long col;
19482 int i;
19483 int id;
19484#endif
19485
19486 rettv->v_type = VAR_LIST;
19487 rettv->vval.v_list = NULL;
19488
19489#ifdef FEAT_SYN_HL
19490 lnum = get_tv_lnum(argvars); /* -1 on type error */
19491 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19492
19493 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019494 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019495 && rettv_list_alloc(rettv) != FAIL)
19496 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019497 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019498 for (i = 0; ; ++i)
19499 {
19500 id = syn_get_stack_item(i);
19501 if (id < 0)
19502 break;
19503 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19504 break;
19505 }
19506 }
19507#endif
19508}
19509
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019511get_cmd_output_as_rettv(
19512 typval_T *argvars,
19513 typval_T *rettv,
19514 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019516 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019518 char_u *infile = NULL;
19519 char_u buf[NUMBUFLEN];
19520 int err = FALSE;
19521 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019522 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019523 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019525 rettv->v_type = VAR_STRING;
19526 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019527 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019528 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019529
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019530 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019531 {
19532 /*
19533 * Write the string to a temp file, to be used for input of the shell
19534 * command.
19535 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019536 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019537 {
19538 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019539 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019540 }
19541
19542 fd = mch_fopen((char *)infile, WRITEBIN);
19543 if (fd == NULL)
19544 {
19545 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019546 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019547 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019548 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019549 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019550 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19551 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019552 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019553 else
19554 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019555 size_t len;
19556
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019557 p = get_tv_string_buf_chk(&argvars[1], buf);
19558 if (p == NULL)
19559 {
19560 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019561 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019562 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019563 len = STRLEN(p);
19564 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019565 err = TRUE;
19566 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019567 if (fclose(fd) != 0)
19568 err = TRUE;
19569 if (err)
19570 {
19571 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019572 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019573 }
19574 }
19575
Bram Moolenaar52a72462014-08-29 15:53:52 +020019576 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19577 * echoes typeahead, that messes up the display. */
19578 if (!msg_silent)
19579 flags += SHELL_COOKED;
19580
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019581 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019583 int len;
19584 listitem_T *li;
19585 char_u *s = NULL;
19586 char_u *start;
19587 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019588 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019589
Bram Moolenaar52a72462014-08-29 15:53:52 +020019590 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019591 if (res == NULL)
19592 goto errret;
19593
19594 list = list_alloc();
19595 if (list == NULL)
19596 goto errret;
19597
19598 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019600 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019601 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019602 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019603 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019604
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019605 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019606 if (s == NULL)
19607 goto errret;
19608
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019609 for (p = s; start < end; ++p, ++start)
19610 *p = *start == NUL ? NL : *start;
19611 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019612
19613 li = listitem_alloc();
19614 if (li == NULL)
19615 {
19616 vim_free(s);
19617 goto errret;
19618 }
19619 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019620 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019621 li->li_tv.vval.v_string = s;
19622 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019624
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019625 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019626 rettv->v_type = VAR_LIST;
19627 rettv->vval.v_list = list;
19628 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019630 else
19631 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019632 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019633#ifdef USE_CR
19634 /* translate <CR> into <NL> */
19635 if (res != NULL)
19636 {
19637 char_u *s;
19638
19639 for (s = res; *s; ++s)
19640 {
19641 if (*s == CAR)
19642 *s = NL;
19643 }
19644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645#else
19646# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019647 /* translate <CR><NL> into <NL> */
19648 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019650 char_u *s, *d;
19651
19652 d = res;
19653 for (s = res; *s; ++s)
19654 {
19655 if (s[0] == CAR && s[1] == NL)
19656 ++s;
19657 *d++ = *s;
19658 }
19659 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661# endif
19662#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019663 rettv->vval.v_string = res;
19664 res = NULL;
19665 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019666
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019667errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019668 if (infile != NULL)
19669 {
19670 mch_remove(infile);
19671 vim_free(infile);
19672 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019673 if (res != NULL)
19674 vim_free(res);
19675 if (list != NULL)
19676 list_free(list, TRUE);
19677}
19678
19679/*
19680 * "system()" function
19681 */
19682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019683f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019684{
19685 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19686}
19687
19688/*
19689 * "systemlist()" function
19690 */
19691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019692f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019693{
19694 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695}
19696
19697/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019698 * "tabpagebuflist()" function
19699 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019701f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019702{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019703#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019704 tabpage_T *tp;
19705 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019706
19707 if (argvars[0].v_type == VAR_UNKNOWN)
19708 wp = firstwin;
19709 else
19710 {
19711 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19712 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019713 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019714 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019715 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019716 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019717 for (; wp != NULL; wp = wp->w_next)
19718 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019719 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019720 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019721 }
19722#endif
19723}
19724
19725
19726/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019727 * "tabpagenr()" function
19728 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019730f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019731{
19732 int nr = 1;
19733#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019734 char_u *arg;
19735
19736 if (argvars[0].v_type != VAR_UNKNOWN)
19737 {
19738 arg = get_tv_string_chk(&argvars[0]);
19739 nr = 0;
19740 if (arg != NULL)
19741 {
19742 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019743 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019744 else
19745 EMSG2(_(e_invexpr2), arg);
19746 }
19747 }
19748 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019749 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019750#endif
19751 rettv->vval.v_number = nr;
19752}
19753
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019754
19755#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019756static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019757
19758/*
19759 * Common code for tabpagewinnr() and winnr().
19760 */
19761 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019762get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019763{
19764 win_T *twin;
19765 int nr = 1;
19766 win_T *wp;
19767 char_u *arg;
19768
19769 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19770 if (argvar->v_type != VAR_UNKNOWN)
19771 {
19772 arg = get_tv_string_chk(argvar);
19773 if (arg == NULL)
19774 nr = 0; /* type error; errmsg already given */
19775 else if (STRCMP(arg, "$") == 0)
19776 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19777 else if (STRCMP(arg, "#") == 0)
19778 {
19779 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19780 if (twin == NULL)
19781 nr = 0;
19782 }
19783 else
19784 {
19785 EMSG2(_(e_invexpr2), arg);
19786 nr = 0;
19787 }
19788 }
19789
19790 if (nr > 0)
19791 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19792 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019793 {
19794 if (wp == NULL)
19795 {
19796 /* didn't find it in this tabpage */
19797 nr = 0;
19798 break;
19799 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019800 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019801 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019802 return nr;
19803}
19804#endif
19805
19806/*
19807 * "tabpagewinnr()" function
19808 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019810f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019811{
19812 int nr = 1;
19813#ifdef FEAT_WINDOWS
19814 tabpage_T *tp;
19815
19816 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19817 if (tp == NULL)
19818 nr = 0;
19819 else
19820 nr = get_winnr(tp, &argvars[1]);
19821#endif
19822 rettv->vval.v_number = nr;
19823}
19824
19825
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019826/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019827 * "tagfiles()" function
19828 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019830f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019831{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019832 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019833 tagname_T tn;
19834 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019835
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019836 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019837 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019838 fname = alloc(MAXPATHL);
19839 if (fname == NULL)
19840 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019841
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019842 for (first = TRUE; ; first = FALSE)
19843 if (get_tagfname(&tn, first, fname) == FAIL
19844 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019845 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019846 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019847 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019848}
19849
19850/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019851 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019852 */
19853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019854f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019855{
19856 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019857
19858 tag_pattern = get_tv_string(&argvars[0]);
19859
19860 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019861 if (*tag_pattern == NUL)
19862 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019863
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019864 if (rettv_list_alloc(rettv) == OK)
19865 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019866}
19867
19868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869 * "tempname()" function
19870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019872f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873{
19874 static int x = 'A';
19875
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019876 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019877 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878
19879 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19880 * names. Skip 'I' and 'O', they are used for shell redirection. */
19881 do
19882 {
19883 if (x == 'Z')
19884 x = '0';
19885 else if (x == '9')
19886 x = 'A';
19887 else
19888 {
19889#ifdef EBCDIC
19890 if (x == 'I')
19891 x = 'J';
19892 else if (x == 'R')
19893 x = 'S';
19894 else
19895#endif
19896 ++x;
19897 }
19898 } while (x == 'I' || x == 'O');
19899}
19900
19901/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019902 * "test(list)" function: Just checking the walls...
19903 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019905f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019906{
19907 /* Used for unit testing. Change the code below to your liking. */
19908#if 0
19909 listitem_T *li;
19910 list_T *l;
19911 char_u *bad, *good;
19912
19913 if (argvars[0].v_type != VAR_LIST)
19914 return;
19915 l = argvars[0].vval.v_list;
19916 if (l == NULL)
19917 return;
19918 li = l->lv_first;
19919 if (li == NULL)
19920 return;
19921 bad = get_tv_string(&li->li_tv);
19922 li = li->li_next;
19923 if (li == NULL)
19924 return;
19925 good = get_tv_string(&li->li_tv);
19926 rettv->vval.v_number = test_edit_score(bad, good);
19927#endif
19928}
19929
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019930#ifdef FEAT_FLOAT
19931/*
19932 * "tan()" function
19933 */
19934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019935f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019936{
19937 float_T f;
19938
19939 rettv->v_type = VAR_FLOAT;
19940 if (get_float_arg(argvars, &f) == OK)
19941 rettv->vval.v_float = tan(f);
19942 else
19943 rettv->vval.v_float = 0.0;
19944}
19945
19946/*
19947 * "tanh()" function
19948 */
19949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019950f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019951{
19952 float_T f;
19953
19954 rettv->v_type = VAR_FLOAT;
19955 if (get_float_arg(argvars, &f) == OK)
19956 rettv->vval.v_float = tanh(f);
19957 else
19958 rettv->vval.v_float = 0.0;
19959}
19960#endif
19961
Bram Moolenaard52d9742005-08-21 22:20:28 +000019962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 * "tolower(string)" function
19964 */
19965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019966f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967{
19968 char_u *p;
19969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019970 p = vim_strsave(get_tv_string(&argvars[0]));
19971 rettv->v_type = VAR_STRING;
19972 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973
19974 if (p != NULL)
19975 while (*p != NUL)
19976 {
19977#ifdef FEAT_MBYTE
19978 int l;
19979
19980 if (enc_utf8)
19981 {
19982 int c, lc;
19983
19984 c = utf_ptr2char(p);
19985 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019986 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987 /* TODO: reallocate string when byte count changes. */
19988 if (utf_char2len(lc) == l)
19989 utf_char2bytes(lc, p);
19990 p += l;
19991 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019992 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 p += l; /* skip multi-byte character */
19994 else
19995#endif
19996 {
19997 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19998 ++p;
19999 }
20000 }
20001}
20002
20003/*
20004 * "toupper(string)" function
20005 */
20006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020007f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020009 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020010 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011}
20012
20013/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020014 * "tr(string, fromstr, tostr)" function
20015 */
20016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020017f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020018{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020019 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020020 char_u *fromstr;
20021 char_u *tostr;
20022 char_u *p;
20023#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020024 int inlen;
20025 int fromlen;
20026 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020027 int idx;
20028 char_u *cpstr;
20029 int cplen;
20030 int first = TRUE;
20031#endif
20032 char_u buf[NUMBUFLEN];
20033 char_u buf2[NUMBUFLEN];
20034 garray_T ga;
20035
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020036 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020037 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20038 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020039
20040 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020041 rettv->v_type = VAR_STRING;
20042 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020043 if (fromstr == NULL || tostr == NULL)
20044 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020045 ga_init2(&ga, (int)sizeof(char), 80);
20046
20047#ifdef FEAT_MBYTE
20048 if (!has_mbyte)
20049#endif
20050 /* not multi-byte: fromstr and tostr must be the same length */
20051 if (STRLEN(fromstr) != STRLEN(tostr))
20052 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020053#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020054error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020055#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020056 EMSG2(_(e_invarg2), fromstr);
20057 ga_clear(&ga);
20058 return;
20059 }
20060
20061 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020062 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020063 {
20064#ifdef FEAT_MBYTE
20065 if (has_mbyte)
20066 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020067 inlen = (*mb_ptr2len)(in_str);
20068 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020069 cplen = inlen;
20070 idx = 0;
20071 for (p = fromstr; *p != NUL; p += fromlen)
20072 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020073 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020074 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020075 {
20076 for (p = tostr; *p != NUL; p += tolen)
20077 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020078 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020079 if (idx-- == 0)
20080 {
20081 cplen = tolen;
20082 cpstr = p;
20083 break;
20084 }
20085 }
20086 if (*p == NUL) /* tostr is shorter than fromstr */
20087 goto error;
20088 break;
20089 }
20090 ++idx;
20091 }
20092
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020093 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020094 {
20095 /* Check that fromstr and tostr have the same number of
20096 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020097 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020098 first = FALSE;
20099 for (p = tostr; *p != NUL; p += tolen)
20100 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020101 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020102 --idx;
20103 }
20104 if (idx != 0)
20105 goto error;
20106 }
20107
Bram Moolenaarcde88542015-08-11 19:14:00 +020020108 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020109 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020110 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020111
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020112 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020113 }
20114 else
20115#endif
20116 {
20117 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020118 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020119 if (p != NULL)
20120 ga_append(&ga, tostr[p - fromstr]);
20121 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020122 ga_append(&ga, *in_str);
20123 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020124 }
20125 }
20126
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020127 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020128 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020129 ga_append(&ga, NUL);
20130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020131 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020132}
20133
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020134#ifdef FEAT_FLOAT
20135/*
20136 * "trunc({float})" function
20137 */
20138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020139f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020140{
20141 float_T f;
20142
20143 rettv->v_type = VAR_FLOAT;
20144 if (get_float_arg(argvars, &f) == OK)
20145 /* trunc() is not in C90, use floor() or ceil() instead. */
20146 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20147 else
20148 rettv->vval.v_float = 0.0;
20149}
20150#endif
20151
Bram Moolenaar8299df92004-07-10 09:47:34 +000020152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 * "type(expr)" function
20154 */
20155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020156f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020158 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020159
20160 switch (argvars[0].v_type)
20161 {
20162 case VAR_NUMBER: n = 0; break;
20163 case VAR_STRING: n = 1; break;
20164 case VAR_FUNC: n = 2; break;
20165 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020166 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020167 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020168 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020169 if (argvars[0].vval.v_number == VVAL_FALSE
20170 || argvars[0].vval.v_number == VVAL_TRUE)
20171 n = 6;
20172 else
20173 n = 7;
20174 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020175 case VAR_JOB: n = 8; break;
20176 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020177 case VAR_UNKNOWN:
20178 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20179 n = -1;
20180 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020181 }
20182 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183}
20184
20185/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020186 * "undofile(name)" function
20187 */
20188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020189f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020190{
20191 rettv->v_type = VAR_STRING;
20192#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020193 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020194 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020195
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020196 if (*fname == NUL)
20197 {
20198 /* If there is no file name there will be no undo file. */
20199 rettv->vval.v_string = NULL;
20200 }
20201 else
20202 {
20203 char_u *ffname = FullName_save(fname, FALSE);
20204
20205 if (ffname != NULL)
20206 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20207 vim_free(ffname);
20208 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020209 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020210#else
20211 rettv->vval.v_string = NULL;
20212#endif
20213}
20214
20215/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020216 * "undotree()" function
20217 */
20218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020219f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020220{
20221 if (rettv_dict_alloc(rettv) == OK)
20222 {
20223 dict_T *dict = rettv->vval.v_dict;
20224 list_T *list;
20225
Bram Moolenaar730cde92010-06-27 05:18:54 +020020226 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020227 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020228 dict_add_nr_str(dict, "save_last",
20229 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020230 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20231 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020232 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020233
20234 list = list_alloc();
20235 if (list != NULL)
20236 {
20237 u_eval_tree(curbuf->b_u_oldhead, list);
20238 dict_add_list(dict, "entries", list);
20239 }
20240 }
20241}
20242
20243/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020244 * "values(dict)" function
20245 */
20246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020247f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020248{
20249 dict_list(argvars, rettv, 1);
20250}
20251
20252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 * "virtcol(string)" function
20254 */
20255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020256f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257{
20258 colnr_T vcol = 0;
20259 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020260 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020262 fp = var2fpos(&argvars[0], FALSE, &fnum);
20263 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20264 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265 {
20266 getvvcol(curwin, fp, NULL, NULL, &vcol);
20267 ++vcol;
20268 }
20269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020270 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271}
20272
20273/*
20274 * "visualmode()" function
20275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020277f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 char_u str[2];
20280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020281 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 str[0] = curbuf->b_visual_mode_eval;
20283 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020284 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285
20286 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020287 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289}
20290
20291/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020292 * "wildmenumode()" function
20293 */
20294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020295f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020296{
20297#ifdef FEAT_WILDMENU
20298 if (wild_menu_showing)
20299 rettv->vval.v_number = 1;
20300#endif
20301}
20302
20303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304 * "winbufnr(nr)" function
20305 */
20306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020307f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308{
20309 win_T *wp;
20310
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020311 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020313 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020315 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316}
20317
20318/*
20319 * "wincol()" function
20320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020322f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323{
20324 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020325 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326}
20327
20328/*
20329 * "winheight(nr)" function
20330 */
20331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020332f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333{
20334 win_T *wp;
20335
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020336 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020338 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020340 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341}
20342
20343/*
20344 * "winline()" function
20345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020347f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348{
20349 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020350 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351}
20352
20353/*
20354 * "winnr()" function
20355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020357f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358{
20359 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020360
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020362 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020364 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365}
20366
20367/*
20368 * "winrestcmd()" function
20369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020371f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372{
20373#ifdef FEAT_WINDOWS
20374 win_T *wp;
20375 int winnr = 1;
20376 garray_T ga;
20377 char_u buf[50];
20378
20379 ga_init2(&ga, (int)sizeof(char), 70);
20380 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20381 {
20382 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20383 ga_concat(&ga, buf);
20384# ifdef FEAT_VERTSPLIT
20385 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20386 ga_concat(&ga, buf);
20387# endif
20388 ++winnr;
20389 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020390 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020392 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020394 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020396 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397}
20398
20399/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020400 * "winrestview()" function
20401 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020403f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020404{
20405 dict_T *dict;
20406
20407 if (argvars[0].v_type != VAR_DICT
20408 || (dict = argvars[0].vval.v_dict) == NULL)
20409 EMSG(_(e_invarg));
20410 else
20411 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020412 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20413 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20414 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20415 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020416#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020417 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20418 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020419#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020420 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20421 {
20422 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20423 curwin->w_set_curswant = FALSE;
20424 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020425
Bram Moolenaar82c25852014-05-28 16:47:16 +020020426 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20427 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020428#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020429 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20430 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020431#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020432 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20433 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20434 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20435 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020436
20437 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020438 win_new_height(curwin, curwin->w_height);
20439# ifdef FEAT_VERTSPLIT
20440 win_new_width(curwin, W_WIDTH(curwin));
20441# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020442 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020443
Bram Moolenaarb851a962014-10-31 15:45:52 +010020444 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020445 curwin->w_topline = 1;
20446 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20447 curwin->w_topline = curbuf->b_ml.ml_line_count;
20448#ifdef FEAT_DIFF
20449 check_topfill(curwin, TRUE);
20450#endif
20451 }
20452}
20453
20454/*
20455 * "winsaveview()" function
20456 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020458f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020459{
20460 dict_T *dict;
20461
Bram Moolenaara800b422010-06-27 01:15:55 +020020462 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020463 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020464 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020465
20466 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20467 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20468#ifdef FEAT_VIRTUALEDIT
20469 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20470#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020471 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020472 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20473
20474 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20475#ifdef FEAT_DIFF
20476 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20477#endif
20478 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20479 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20480}
20481
20482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 * "winwidth(nr)" function
20484 */
20485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020486f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487{
20488 win_T *wp;
20489
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020490 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020492 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493 else
20494#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020495 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020497 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498#endif
20499}
20500
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020502 * "wordcount()" function
20503 */
20504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020505f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020506{
20507 if (rettv_dict_alloc(rettv) == FAIL)
20508 return;
20509 cursor_pos_info(rettv->vval.v_dict);
20510}
20511
20512/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020513 * Write list of strings to file
20514 */
20515 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020516write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020517{
20518 listitem_T *li;
20519 int c;
20520 int ret = OK;
20521 char_u *s;
20522
20523 for (li = list->lv_first; li != NULL; li = li->li_next)
20524 {
20525 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20526 {
20527 if (*s == '\n')
20528 c = putc(NUL, fd);
20529 else
20530 c = putc(*s, fd);
20531 if (c == EOF)
20532 {
20533 ret = FAIL;
20534 break;
20535 }
20536 }
20537 if (!binary || li->li_next != NULL)
20538 if (putc('\n', fd) == EOF)
20539 {
20540 ret = FAIL;
20541 break;
20542 }
20543 if (ret == FAIL)
20544 {
20545 EMSG(_(e_write));
20546 break;
20547 }
20548 }
20549 return ret;
20550}
20551
20552/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020553 * "writefile()" function
20554 */
20555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020556f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020557{
20558 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020559 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020560 char_u *fname;
20561 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020562 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020563
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020564 if (check_restricted() || check_secure())
20565 return;
20566
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020567 if (argvars[0].v_type != VAR_LIST)
20568 {
20569 EMSG2(_(e_listarg), "writefile()");
20570 return;
20571 }
20572 if (argvars[0].vval.v_list == NULL)
20573 return;
20574
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020575 if (argvars[2].v_type != VAR_UNKNOWN)
20576 {
20577 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20578 binary = TRUE;
20579 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20580 append = TRUE;
20581 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020582
20583 /* Always open the file in binary mode, library functions have a mind of
20584 * their own about CR-LF conversion. */
20585 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020586 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20587 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020588 {
20589 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20590 ret = -1;
20591 }
20592 else
20593 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020594 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20595 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020596 fclose(fd);
20597 }
20598
20599 rettv->vval.v_number = ret;
20600}
20601
20602/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020603 * "xor(expr, expr)" function
20604 */
20605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020606f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020607{
20608 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20609 ^ get_tv_number_chk(&argvars[1], NULL);
20610}
20611
20612
20613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020615 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 */
20617 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020618var2fpos(
20619 typval_T *varp,
20620 int dollar_lnum, /* TRUE when $ is last line */
20621 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020623 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020625 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626
Bram Moolenaara5525202006-03-02 22:52:09 +000020627 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020628 if (varp->v_type == VAR_LIST)
20629 {
20630 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020631 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020632 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020633 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020634
20635 l = varp->vval.v_list;
20636 if (l == NULL)
20637 return NULL;
20638
20639 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020640 pos.lnum = list_find_nr(l, 0L, &error);
20641 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020642 return NULL; /* invalid line number */
20643
20644 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020645 pos.col = list_find_nr(l, 1L, &error);
20646 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020647 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020648 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020649
20650 /* We accept "$" for the column number: last column. */
20651 li = list_find(l, 1L);
20652 if (li != NULL && li->li_tv.v_type == VAR_STRING
20653 && li->li_tv.vval.v_string != NULL
20654 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20655 pos.col = len + 1;
20656
Bram Moolenaara5525202006-03-02 22:52:09 +000020657 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020658 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020659 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020660 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020661
Bram Moolenaara5525202006-03-02 22:52:09 +000020662#ifdef FEAT_VIRTUALEDIT
20663 /* Get the virtual offset. Defaults to zero. */
20664 pos.coladd = list_find_nr(l, 2L, &error);
20665 if (error)
20666 pos.coladd = 0;
20667#endif
20668
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020669 return &pos;
20670 }
20671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020672 name = get_tv_string_chk(varp);
20673 if (name == NULL)
20674 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020675 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020677 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20678 {
20679 if (VIsual_active)
20680 return &VIsual;
20681 return &curwin->w_cursor;
20682 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020683 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020685 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20687 return NULL;
20688 return pp;
20689 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020690
20691#ifdef FEAT_VIRTUALEDIT
20692 pos.coladd = 0;
20693#endif
20694
Bram Moolenaar477933c2007-07-17 14:32:23 +000020695 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020696 {
20697 pos.col = 0;
20698 if (name[1] == '0') /* "w0": first visible line */
20699 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020700 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020701 pos.lnum = curwin->w_topline;
20702 return &pos;
20703 }
20704 else if (name[1] == '$') /* "w$": last visible line */
20705 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020706 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020707 pos.lnum = curwin->w_botline - 1;
20708 return &pos;
20709 }
20710 }
20711 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020713 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 {
20715 pos.lnum = curbuf->b_ml.ml_line_count;
20716 pos.col = 0;
20717 }
20718 else
20719 {
20720 pos.lnum = curwin->w_cursor.lnum;
20721 pos.col = (colnr_T)STRLEN(ml_get_curline());
20722 }
20723 return &pos;
20724 }
20725 return NULL;
20726}
20727
20728/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020729 * Convert list in "arg" into a position and optional file number.
20730 * When "fnump" is NULL there is no file number, only 3 items.
20731 * Note that the column is passed on as-is, the caller may want to decrement
20732 * it to use 1 for the first column.
20733 * Return FAIL when conversion is not possible, doesn't check the position for
20734 * validity.
20735 */
20736 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020737list2fpos(
20738 typval_T *arg,
20739 pos_T *posp,
20740 int *fnump,
20741 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020742{
20743 list_T *l = arg->vval.v_list;
20744 long i = 0;
20745 long n;
20746
Bram Moolenaar493c1782014-05-28 14:34:46 +020020747 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20748 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020749 if (arg->v_type != VAR_LIST
20750 || l == NULL
20751 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020752 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020753 return FAIL;
20754
20755 if (fnump != NULL)
20756 {
20757 n = list_find_nr(l, i++, NULL); /* fnum */
20758 if (n < 0)
20759 return FAIL;
20760 if (n == 0)
20761 n = curbuf->b_fnum; /* current buffer */
20762 *fnump = n;
20763 }
20764
20765 n = list_find_nr(l, i++, NULL); /* lnum */
20766 if (n < 0)
20767 return FAIL;
20768 posp->lnum = n;
20769
20770 n = list_find_nr(l, i++, NULL); /* col */
20771 if (n < 0)
20772 return FAIL;
20773 posp->col = n;
20774
20775#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020776 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020777 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020778 posp->coladd = 0;
20779 else
20780 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020781#endif
20782
Bram Moolenaar493c1782014-05-28 14:34:46 +020020783 if (curswantp != NULL)
20784 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20785
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020786 return OK;
20787}
20788
20789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 * Get the length of an environment variable name.
20791 * Advance "arg" to the first character after the name.
20792 * Return 0 for error.
20793 */
20794 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020795get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796{
20797 char_u *p;
20798 int len;
20799
20800 for (p = *arg; vim_isIDc(*p); ++p)
20801 ;
20802 if (p == *arg) /* no name found */
20803 return 0;
20804
20805 len = (int)(p - *arg);
20806 *arg = p;
20807 return len;
20808}
20809
20810/*
20811 * Get the length of the name of a function or internal variable.
20812 * "arg" is advanced to the first non-white character after the name.
20813 * Return 0 if something is wrong.
20814 */
20815 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020816get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817{
20818 char_u *p;
20819 int len;
20820
20821 /* Find the end of the name. */
20822 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020823 {
20824 if (*p == ':')
20825 {
20826 /* "s:" is start of "s:var", but "n:" is not and can be used in
20827 * slice "[n:]". Also "xx:" is not a namespace. */
20828 len = (int)(p - *arg);
20829 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20830 || len > 1)
20831 break;
20832 }
20833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 if (p == *arg) /* no name found */
20835 return 0;
20836
20837 len = (int)(p - *arg);
20838 *arg = skipwhite(p);
20839
20840 return len;
20841}
20842
20843/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020844 * Get the length of the name of a variable or function.
20845 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020847 * Return -1 if curly braces expansion failed.
20848 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849 * If the name contains 'magic' {}'s, expand them and return the
20850 * expanded name in an allocated string via 'alias' - caller must free.
20851 */
20852 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020853get_name_len(
20854 char_u **arg,
20855 char_u **alias,
20856 int evaluate,
20857 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858{
20859 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 char_u *p;
20861 char_u *expr_start;
20862 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863
20864 *alias = NULL; /* default to no alias */
20865
20866 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20867 && (*arg)[2] == (int)KE_SNR)
20868 {
20869 /* hard coded <SNR>, already translated */
20870 *arg += 3;
20871 return get_id_len(arg) + 3;
20872 }
20873 len = eval_fname_script(*arg);
20874 if (len > 0)
20875 {
20876 /* literal "<SID>", "s:" or "<SNR>" */
20877 *arg += len;
20878 }
20879
Bram Moolenaar071d4272004-06-13 20:20:40 +000020880 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020881 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020883 p = find_name_end(*arg, &expr_start, &expr_end,
20884 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 if (expr_start != NULL)
20886 {
20887 char_u *temp_string;
20888
20889 if (!evaluate)
20890 {
20891 len += (int)(p - *arg);
20892 *arg = skipwhite(p);
20893 return len;
20894 }
20895
20896 /*
20897 * Include any <SID> etc in the expanded string:
20898 * Thus the -len here.
20899 */
20900 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20901 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020902 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 *alias = temp_string;
20904 *arg = skipwhite(p);
20905 return (int)STRLEN(temp_string);
20906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907
20908 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020909 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 EMSG2(_(e_invexpr2), *arg);
20911
20912 return len;
20913}
20914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020915/*
20916 * Find the end of a variable or function name, taking care of magic braces.
20917 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20918 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020919 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020920 * Return a pointer to just after the name. Equal to "arg" if there is no
20921 * valid name.
20922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020924find_name_end(
20925 char_u *arg,
20926 char_u **expr_start,
20927 char_u **expr_end,
20928 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020930 int mb_nest = 0;
20931 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020933 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020935 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020937 *expr_start = NULL;
20938 *expr_end = NULL;
20939 }
20940
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020941 /* Quick check for valid starting character. */
20942 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20943 return arg;
20944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020945 for (p = arg; *p != NUL
20946 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020947 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020948 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020949 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020950 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020951 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020952 if (*p == '\'')
20953 {
20954 /* skip over 'string' to avoid counting [ and ] inside it. */
20955 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20956 ;
20957 if (*p == NUL)
20958 break;
20959 }
20960 else if (*p == '"')
20961 {
20962 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20963 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20964 if (*p == '\\' && p[1] != NUL)
20965 ++p;
20966 if (*p == NUL)
20967 break;
20968 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020969 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20970 {
20971 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020972 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020973 len = (int)(p - arg);
20974 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020975 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020976 break;
20977 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020978
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020979 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020981 if (*p == '[')
20982 ++br_nest;
20983 else if (*p == ']')
20984 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020987 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020989 if (*p == '{')
20990 {
20991 mb_nest++;
20992 if (expr_start != NULL && *expr_start == NULL)
20993 *expr_start = p;
20994 }
20995 else if (*p == '}')
20996 {
20997 mb_nest--;
20998 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20999 *expr_end = p;
21000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 }
21003
21004 return p;
21005}
21006
21007/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021008 * Expands out the 'magic' {}'s in a variable/function name.
21009 * Note that this can call itself recursively, to deal with
21010 * constructs like foo{bar}{baz}{bam}
21011 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21012 * "in_start" ^
21013 * "expr_start" ^
21014 * "expr_end" ^
21015 * "in_end" ^
21016 *
21017 * Returns a new allocated string, which the caller must free.
21018 * Returns NULL for failure.
21019 */
21020 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021021make_expanded_name(
21022 char_u *in_start,
21023 char_u *expr_start,
21024 char_u *expr_end,
21025 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021026{
21027 char_u c1;
21028 char_u *retval = NULL;
21029 char_u *temp_result;
21030 char_u *nextcmd = NULL;
21031
21032 if (expr_end == NULL || in_end == NULL)
21033 return NULL;
21034 *expr_start = NUL;
21035 *expr_end = NUL;
21036 c1 = *in_end;
21037 *in_end = NUL;
21038
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021039 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021040 if (temp_result != NULL && nextcmd == NULL)
21041 {
21042 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21043 + (in_end - expr_end) + 1));
21044 if (retval != NULL)
21045 {
21046 STRCPY(retval, in_start);
21047 STRCAT(retval, temp_result);
21048 STRCAT(retval, expr_end + 1);
21049 }
21050 }
21051 vim_free(temp_result);
21052
21053 *in_end = c1; /* put char back for error messages */
21054 *expr_start = '{';
21055 *expr_end = '}';
21056
21057 if (retval != NULL)
21058 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021059 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021060 if (expr_start != NULL)
21061 {
21062 /* Further expansion! */
21063 temp_result = make_expanded_name(retval, expr_start,
21064 expr_end, temp_result);
21065 vim_free(retval);
21066 retval = temp_result;
21067 }
21068 }
21069
21070 return retval;
21071}
21072
21073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021075 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076 */
21077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021078eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021080 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21081}
21082
21083/*
21084 * Return TRUE if character "c" can be used as the first character in a
21085 * variable or function name (excluding '{' and '}').
21086 */
21087 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021088eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021089{
21090 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091}
21092
21093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 * Set number v: variable to "val".
21095 */
21096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021097set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021099 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100}
21101
21102/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021103 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 */
21105 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021106get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021108 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109}
21110
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021111/*
21112 * Get string v: variable value. Uses a static buffer, can only be used once.
21113 */
21114 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021115get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021116{
21117 return get_tv_string(&vimvars[idx].vv_tv);
21118}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021119
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021121 * Get List v: variable value. Caller must take care of reference count when
21122 * needed.
21123 */
21124 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021125get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021126{
21127 return vimvars[idx].vv_list;
21128}
21129
21130/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021131 * Set v:char to character "c".
21132 */
21133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021134set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021135{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021136 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021137
21138#ifdef FEAT_MBYTE
21139 if (has_mbyte)
21140 buf[(*mb_char2bytes)(c, buf)] = NUL;
21141 else
21142#endif
21143 {
21144 buf[0] = c;
21145 buf[1] = NUL;
21146 }
21147 set_vim_var_string(VV_CHAR, buf, -1);
21148}
21149
21150/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021151 * Set v:count to "count" and v:count1 to "count1".
21152 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153 */
21154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021155set_vcount(
21156 long count,
21157 long count1,
21158 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021160 if (set_prevcount)
21161 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021162 vimvars[VV_COUNT].vv_nr = count;
21163 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164}
21165
21166/*
21167 * Set string v: variable to a copy of "val".
21168 */
21169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021170set_vim_var_string(
21171 int idx,
21172 char_u *val,
21173 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174{
Bram Moolenaara542c682016-01-31 16:28:04 +010021175 clear_tv(&vimvars[idx].vv_di.di_tv);
21176 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021178 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021180 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021182 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183}
21184
21185/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021186 * Set List v: variable to "val".
21187 */
21188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021189set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021190{
Bram Moolenaara542c682016-01-31 16:28:04 +010021191 clear_tv(&vimvars[idx].vv_di.di_tv);
21192 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021193 vimvars[idx].vv_list = val;
21194 if (val != NULL)
21195 ++val->lv_refcount;
21196}
21197
21198/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021199 * Set Dictionary v: variable to "val".
21200 */
21201 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021202set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021203{
21204 int todo;
21205 hashitem_T *hi;
21206
Bram Moolenaara542c682016-01-31 16:28:04 +010021207 clear_tv(&vimvars[idx].vv_di.di_tv);
21208 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021209 vimvars[idx].vv_dict = val;
21210 if (val != NULL)
21211 {
21212 ++val->dv_refcount;
21213
21214 /* Set readonly */
21215 todo = (int)val->dv_hashtab.ht_used;
21216 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21217 {
21218 if (HASHITEM_EMPTY(hi))
21219 continue;
21220 --todo;
21221 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21222 }
21223 }
21224}
21225
21226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 * Set v:register if needed.
21228 */
21229 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021230set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231{
21232 char_u regname;
21233
21234 if (c == 0 || c == ' ')
21235 regname = '"';
21236 else
21237 regname = c;
21238 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021239 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240 set_vim_var_string(VV_REG, &regname, 1);
21241}
21242
21243/*
21244 * Get or set v:exception. If "oldval" == NULL, return the current value.
21245 * Otherwise, restore the value to "oldval" and return NULL.
21246 * Must always be called in pairs to save and restore v:exception! Does not
21247 * take care of memory allocations.
21248 */
21249 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021250v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251{
21252 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021253 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254
Bram Moolenaare9a41262005-01-15 22:18:47 +000021255 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 return NULL;
21257}
21258
21259/*
21260 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21261 * Otherwise, restore the value to "oldval" and return NULL.
21262 * Must always be called in pairs to save and restore v:throwpoint! Does not
21263 * take care of memory allocations.
21264 */
21265 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021266v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267{
21268 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021269 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270
Bram Moolenaare9a41262005-01-15 22:18:47 +000021271 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 return NULL;
21273}
21274
21275#if defined(FEAT_AUTOCMD) || defined(PROTO)
21276/*
21277 * Set v:cmdarg.
21278 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21279 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21280 * Must always be called in pairs!
21281 */
21282 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021283set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284{
21285 char_u *oldval;
21286 char_u *newval;
21287 unsigned len;
21288
Bram Moolenaare9a41262005-01-15 22:18:47 +000021289 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021290 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021292 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021293 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021294 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 }
21296
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021297 if (eap->force_bin == FORCE_BIN)
21298 len = 6;
21299 else if (eap->force_bin == FORCE_NOBIN)
21300 len = 8;
21301 else
21302 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021303
21304 if (eap->read_edit)
21305 len += 7;
21306
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021307 if (eap->force_ff != 0)
21308 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21309# ifdef FEAT_MBYTE
21310 if (eap->force_enc != 0)
21311 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021312 if (eap->bad_char != 0)
21313 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021314# endif
21315
21316 newval = alloc(len + 1);
21317 if (newval == NULL)
21318 return NULL;
21319
21320 if (eap->force_bin == FORCE_BIN)
21321 sprintf((char *)newval, " ++bin");
21322 else if (eap->force_bin == FORCE_NOBIN)
21323 sprintf((char *)newval, " ++nobin");
21324 else
21325 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021326
21327 if (eap->read_edit)
21328 STRCAT(newval, " ++edit");
21329
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021330 if (eap->force_ff != 0)
21331 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21332 eap->cmd + eap->force_ff);
21333# ifdef FEAT_MBYTE
21334 if (eap->force_enc != 0)
21335 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21336 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021337 if (eap->bad_char == BAD_KEEP)
21338 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21339 else if (eap->bad_char == BAD_DROP)
21340 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21341 else if (eap->bad_char != 0)
21342 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021343# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021344 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021345 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346}
21347#endif
21348
21349/*
21350 * Get the value of internal variable "name".
21351 * Return OK or FAIL.
21352 */
21353 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021354get_var_tv(
21355 char_u *name,
21356 int len, /* length of "name" */
21357 typval_T *rettv, /* NULL when only checking existence */
21358 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21359 int verbose, /* may give error message */
21360 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361{
21362 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021363 typval_T *tv = NULL;
21364 typval_T atv;
21365 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367
21368 /* truncate the name, so that we can use strcmp() */
21369 cc = name[len];
21370 name[len] = NUL;
21371
21372 /*
21373 * Check for "b:changedtick".
21374 */
21375 if (STRCMP(name, "b:changedtick") == 0)
21376 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021377 atv.v_type = VAR_NUMBER;
21378 atv.vval.v_number = curbuf->b_changedtick;
21379 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 }
21381
21382 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 * Check for user-defined variables.
21384 */
21385 else
21386 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021387 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021389 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021390 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021391 if (dip != NULL)
21392 *dip = v;
21393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 }
21395
Bram Moolenaare9a41262005-01-15 22:18:47 +000021396 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021398 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021399 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400 ret = FAIL;
21401 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021402 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021403 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404
21405 name[len] = cc;
21406
21407 return ret;
21408}
21409
21410/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021411 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21412 * Also handle function call with Funcref variable: func(expr)
21413 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21414 */
21415 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021416handle_subscript(
21417 char_u **arg,
21418 typval_T *rettv,
21419 int evaluate, /* do more than finding the end */
21420 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021421{
21422 int ret = OK;
21423 dict_T *selfdict = NULL;
21424 char_u *s;
21425 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021426 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021427
21428 while (ret == OK
21429 && (**arg == '['
21430 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021431 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021432 && !vim_iswhite(*(*arg - 1)))
21433 {
21434 if (**arg == '(')
21435 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021436 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021437 if (evaluate)
21438 {
21439 functv = *rettv;
21440 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021441
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021442 /* Invoke the function. Recursive! */
21443 s = functv.vval.v_string;
21444 }
21445 else
21446 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021447 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021448 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21449 &len, evaluate, selfdict);
21450
21451 /* Clear the funcref afterwards, so that deleting it while
21452 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021453 if (evaluate)
21454 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021455
21456 /* Stop the expression evaluation when immediately aborting on
21457 * error, or when an interrupt occurred or an exception was thrown
21458 * but not caught. */
21459 if (aborting())
21460 {
21461 if (ret == OK)
21462 clear_tv(rettv);
21463 ret = FAIL;
21464 }
21465 dict_unref(selfdict);
21466 selfdict = NULL;
21467 }
21468 else /* **arg == '[' || **arg == '.' */
21469 {
21470 dict_unref(selfdict);
21471 if (rettv->v_type == VAR_DICT)
21472 {
21473 selfdict = rettv->vval.v_dict;
21474 if (selfdict != NULL)
21475 ++selfdict->dv_refcount;
21476 }
21477 else
21478 selfdict = NULL;
21479 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21480 {
21481 clear_tv(rettv);
21482 ret = FAIL;
21483 }
21484 }
21485 }
21486 dict_unref(selfdict);
21487 return ret;
21488}
21489
21490/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021491 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021492 * value).
21493 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021494 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021495alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021496{
Bram Moolenaar33570922005-01-25 22:26:29 +000021497 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021498}
21499
21500/*
21501 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 * The string "s" must have been allocated, it is consumed.
21503 * Return NULL for out of memory, the variable otherwise.
21504 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021505 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021506alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507{
Bram Moolenaar33570922005-01-25 22:26:29 +000021508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021510 rettv = alloc_tv();
21511 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021513 rettv->v_type = VAR_STRING;
21514 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021515 }
21516 else
21517 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021518 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519}
21520
21521/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021522 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021523 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021524 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021525free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526{
21527 if (varp != NULL)
21528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021529 switch (varp->v_type)
21530 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021531 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021532 func_unref(varp->vval.v_string);
21533 /*FALLTHROUGH*/
21534 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021535 vim_free(varp->vval.v_string);
21536 break;
21537 case VAR_LIST:
21538 list_unref(varp->vval.v_list);
21539 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021540 case VAR_DICT:
21541 dict_unref(varp->vval.v_dict);
21542 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021543 case VAR_JOB:
21544#ifdef FEAT_JOB
21545 job_unref(varp->vval.v_job);
21546 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021547#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021548 case VAR_CHANNEL:
21549#ifdef FEAT_CHANNEL
21550 channel_unref(varp->vval.v_channel);
21551 break;
21552#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021553 case VAR_NUMBER:
21554 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021555 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021556 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021557 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 vim_free(varp);
21560 }
21561}
21562
21563/*
21564 * Free the memory for a variable value and set the value to NULL or 0.
21565 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021566 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021567clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568{
21569 if (varp != NULL)
21570 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021571 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021573 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021574 func_unref(varp->vval.v_string);
21575 /*FALLTHROUGH*/
21576 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021577 vim_free(varp->vval.v_string);
21578 varp->vval.v_string = NULL;
21579 break;
21580 case VAR_LIST:
21581 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021582 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021583 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021584 case VAR_DICT:
21585 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021586 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021587 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021588 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021589 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021590 varp->vval.v_number = 0;
21591 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021592 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021593#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021594 varp->vval.v_float = 0.0;
21595 break;
21596#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021597 case VAR_JOB:
21598#ifdef FEAT_JOB
21599 job_unref(varp->vval.v_job);
21600 varp->vval.v_job = NULL;
21601#endif
21602 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021603 case VAR_CHANNEL:
21604#ifdef FEAT_CHANNEL
21605 channel_unref(varp->vval.v_channel);
21606 varp->vval.v_channel = NULL;
21607#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021608 case VAR_UNKNOWN:
21609 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021611 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 }
21613}
21614
21615/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021616 * Set the value of a variable to NULL without freeing items.
21617 */
21618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021619init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021620{
21621 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021622 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021623}
21624
21625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 * Get the number value of a variable.
21627 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021628 * For incompatible types, return 0.
21629 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21630 * caller of incompatible types: it sets *denote to TRUE if "denote"
21631 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 */
21633 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021634get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021636 int error = FALSE;
21637
21638 return get_tv_number_chk(varp, &error); /* return 0L on error */
21639}
21640
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021641 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021642get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021643{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021644 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021646 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021648 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021649 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021650 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021651#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021652 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021653 break;
21654#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021655 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021656 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021657 break;
21658 case VAR_STRING:
21659 if (varp->vval.v_string != NULL)
21660 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021661 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021662 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021663 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021664 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021665 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021666 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021667 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021668 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021669 case VAR_SPECIAL:
21670 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21671 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021672 case VAR_JOB:
21673#ifdef FEAT_JOB
21674 EMSG(_("E910: Using a Job as a Number"));
21675 break;
21676#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021677 case VAR_CHANNEL:
21678#ifdef FEAT_CHANNEL
21679 EMSG(_("E913: Using a Channel as a Number"));
21680 break;
21681#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021682 case VAR_UNKNOWN:
21683 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021684 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021686 if (denote == NULL) /* useful for values that must be unsigned */
21687 n = -1;
21688 else
21689 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021690 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691}
21692
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021693#ifdef FEAT_FLOAT
21694 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021695get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021696{
21697 switch (varp->v_type)
21698 {
21699 case VAR_NUMBER:
21700 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021701 case VAR_FLOAT:
21702 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021703 case VAR_FUNC:
21704 EMSG(_("E891: Using a Funcref as a Float"));
21705 break;
21706 case VAR_STRING:
21707 EMSG(_("E892: Using a String as a Float"));
21708 break;
21709 case VAR_LIST:
21710 EMSG(_("E893: Using a List as a Float"));
21711 break;
21712 case VAR_DICT:
21713 EMSG(_("E894: Using a Dictionary as a Float"));
21714 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021715 case VAR_SPECIAL:
21716 EMSG(_("E907: Using a special value as a Float"));
21717 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021718 case VAR_JOB:
21719# ifdef FEAT_JOB
21720 EMSG(_("E911: Using a Job as a Float"));
21721 break;
21722# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021723 case VAR_CHANNEL:
21724# ifdef FEAT_CHANNEL
21725 EMSG(_("E914: Using a Channel as a Float"));
21726 break;
21727# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021728 case VAR_UNKNOWN:
21729 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021730 break;
21731 }
21732 return 0;
21733}
21734#endif
21735
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021737 * Get the lnum from the first argument.
21738 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021739 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 */
21741 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021742get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743{
Bram Moolenaar33570922005-01-25 22:26:29 +000021744 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 linenr_T lnum;
21746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021747 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748 if (lnum == 0) /* no valid number, try using line() */
21749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021750 rettv.v_type = VAR_NUMBER;
21751 f_line(argvars, &rettv);
21752 lnum = rettv.vval.v_number;
21753 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 }
21755 return lnum;
21756}
21757
21758/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021759 * Get the lnum from the first argument.
21760 * Also accepts "$", then "buf" is used.
21761 * Returns 0 on error.
21762 */
21763 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021764get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021765{
21766 if (argvars[0].v_type == VAR_STRING
21767 && argvars[0].vval.v_string != NULL
21768 && argvars[0].vval.v_string[0] == '$'
21769 && buf != NULL)
21770 return buf->b_ml.ml_line_count;
21771 return get_tv_number_chk(&argvars[0], NULL);
21772}
21773
21774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 * Get the string value of a variable.
21776 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021777 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21778 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779 * If the String variable has never been set, return an empty string.
21780 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021781 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21782 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783 */
21784 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021785get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786{
21787 static char_u mybuf[NUMBUFLEN];
21788
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021789 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790}
21791
21792 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021793get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021795 char_u *res = get_tv_string_buf_chk(varp, buf);
21796
21797 return res != NULL ? res : (char_u *)"";
21798}
21799
Bram Moolenaar7d647822014-04-05 21:28:56 +020021800/*
21801 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21802 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021803 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021804get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021805{
21806 static char_u mybuf[NUMBUFLEN];
21807
21808 return get_tv_string_buf_chk(varp, mybuf);
21809}
21810
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021811 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021812get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021813{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021814 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021816 case VAR_NUMBER:
21817 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21818 return buf;
21819 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021820 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021821 break;
21822 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021823 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021824 break;
21825 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021826 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021827 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021828 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021829#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021830 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021831 break;
21832#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021833 case VAR_STRING:
21834 if (varp->vval.v_string != NULL)
21835 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021836 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021837 case VAR_SPECIAL:
21838 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21839 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021840 case VAR_JOB:
21841#ifdef FEAT_JOB
21842 {
21843 job_T *job = varp->vval.v_job;
21844 char *status = job->jv_status == JOB_FAILED ? "fail"
21845 : job->jv_status == JOB_ENDED ? "dead"
21846 : "run";
21847# ifdef UNIX
21848 vim_snprintf((char *)buf, NUMBUFLEN,
21849 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021850# elif defined(WIN32)
21851 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021852 "process %ld %s",
21853 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021854 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021855# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021856 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021857 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21858# endif
21859 return buf;
21860 }
21861#endif
21862 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021863 case VAR_CHANNEL:
21864#ifdef FEAT_CHANNEL
21865 {
21866 channel_T *channel = varp->vval.v_channel;
21867 char *status = channel_status(channel);
21868
Bram Moolenaar5cefd402016-02-16 12:44:26 +010021869 if (channel == NULL)
21870 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
21871 else
21872 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010021873 "channel %d %s", channel->ch_id, status);
21874 return buf;
21875 }
21876#endif
21877 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021878 case VAR_UNKNOWN:
21879 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021880 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021882 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883}
21884
21885/*
21886 * Find variable "name" in the list of variables.
21887 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021888 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021889 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021890 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021892 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021893find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021896 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897
Bram Moolenaara7043832005-01-21 11:56:39 +000021898 ht = find_var_ht(name, &varname);
21899 if (htp != NULL)
21900 *htp = ht;
21901 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021903 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904}
21905
21906/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021907 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021908 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021910 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021911find_var_in_ht(
21912 hashtab_T *ht,
21913 int htname,
21914 char_u *varname,
21915 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021916{
Bram Moolenaar33570922005-01-25 22:26:29 +000021917 hashitem_T *hi;
21918
21919 if (*varname == NUL)
21920 {
21921 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021922 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021923 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021924 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021925 case 'g': return &globvars_var;
21926 case 'v': return &vimvars_var;
21927 case 'b': return &curbuf->b_bufvar;
21928 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021929#ifdef FEAT_WINDOWS
21930 case 't': return &curtab->tp_winvar;
21931#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021932 case 'l': return current_funccal == NULL
21933 ? NULL : &current_funccal->l_vars_var;
21934 case 'a': return current_funccal == NULL
21935 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021936 }
21937 return NULL;
21938 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021939
21940 hi = hash_find(ht, varname);
21941 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021942 {
21943 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021944 * worked find the variable again. Don't auto-load a script if it was
21945 * loaded already, otherwise it would be loaded every time when
21946 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021947 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021948 {
21949 /* Note: script_autoload() may make "hi" invalid. It must either
21950 * be obtained again or not used. */
21951 if (!script_autoload(varname, FALSE) || aborting())
21952 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021953 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021954 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021955 if (HASHITEM_EMPTY(hi))
21956 return NULL;
21957 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021958 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021959}
21960
21961/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021962 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021963 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021964 * Set "varname" to the start of name without ':'.
21965 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021966 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021967find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021969 hashitem_T *hi;
21970
Bram Moolenaar73627d02015-08-11 15:46:09 +020021971 if (name[0] == NUL)
21972 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973 if (name[1] != ':')
21974 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021975 /* The name must not start with a colon or #. */
21976 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 return NULL;
21978 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021979
21980 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021981 hi = hash_find(&compat_hashtab, name);
21982 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021983 return &compat_hashtab;
21984
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021986 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021987 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 }
21989 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021990 if (*name == 'g') /* global variable */
21991 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021992 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21993 */
21994 if (vim_strchr(name + 2, ':') != NULL
21995 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021996 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021998 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022000 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022001#ifdef FEAT_WINDOWS
22002 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022003 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022004#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022005 if (*name == 'v') /* v: variable */
22006 return &vimvarht;
22007 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022008 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022009 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022010 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 if (*name == 's' /* script variable */
22012 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22013 return &SCRIPT_VARS(current_SID);
22014 return NULL;
22015}
22016
22017/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022018 * Get function call environment based on bactrace debug level
22019 */
22020 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022021get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022022{
22023 int i;
22024 funccall_T *funccal;
22025 funccall_T *temp_funccal;
22026
22027 funccal = current_funccal;
22028 if (debug_backtrace_level > 0)
22029 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022030 for (i = 0; i < debug_backtrace_level; i++)
22031 {
22032 temp_funccal = funccal->caller;
22033 if (temp_funccal)
22034 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022035 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022036 /* backtrace level overflow. reset to max */
22037 debug_backtrace_level = i;
22038 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022039 }
22040 return funccal;
22041}
22042
22043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022045 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 * Returns NULL when it doesn't exist.
22047 */
22048 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022049get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050{
Bram Moolenaar33570922005-01-25 22:26:29 +000022051 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022053 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 if (v == NULL)
22055 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022056 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057}
22058
22059/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022060 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 * sourcing this script and when executing functions defined in the script.
22062 */
22063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022064new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065{
Bram Moolenaara7043832005-01-21 11:56:39 +000022066 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022067 hashtab_T *ht;
22068 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022069
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22071 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022072 /* Re-allocating ga_data means that an ht_array pointing to
22073 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022074 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022075 for (i = 1; i <= ga_scripts.ga_len; ++i)
22076 {
22077 ht = &SCRIPT_VARS(i);
22078 if (ht->ht_mask == HT_INIT_SIZE - 1)
22079 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022080 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022081 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022082 }
22083
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 while (ga_scripts.ga_len < id)
22085 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022086 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022087 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022088 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090 }
22091 }
22092}
22093
22094/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022095 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22096 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 */
22098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022099init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100{
Bram Moolenaar33570922005-01-25 22:26:29 +000022101 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022102 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022103 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022104 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022105 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022106 dict_var->di_tv.vval.v_dict = dict;
22107 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022108 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022109 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22110 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111}
22112
22113/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022114 * Unreference a dictionary initialized by init_var_dict().
22115 */
22116 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022117unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022118{
22119 /* Now the dict needs to be freed if no one else is using it, go back to
22120 * normal reference counting. */
22121 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22122 dict_unref(dict);
22123}
22124
22125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022127 * Frees all allocated variables and the value they contain.
22128 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129 */
22130 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022131vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022132{
22133 vars_clear_ext(ht, TRUE);
22134}
22135
22136/*
22137 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22138 */
22139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022140vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141{
Bram Moolenaara7043832005-01-21 11:56:39 +000022142 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022143 hashitem_T *hi;
22144 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145
Bram Moolenaar33570922005-01-25 22:26:29 +000022146 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022147 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022148 for (hi = ht->ht_array; todo > 0; ++hi)
22149 {
22150 if (!HASHITEM_EMPTY(hi))
22151 {
22152 --todo;
22153
Bram Moolenaar33570922005-01-25 22:26:29 +000022154 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022155 * ht_array might change then. hash_clear() takes care of it
22156 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022157 v = HI2DI(hi);
22158 if (free_val)
22159 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022160 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022161 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022162 }
22163 }
22164 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022165 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166}
22167
Bram Moolenaara7043832005-01-21 11:56:39 +000022168/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022169 * Delete a variable from hashtab "ht" at item "hi".
22170 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022173delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174{
Bram Moolenaar33570922005-01-25 22:26:29 +000022175 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022176
22177 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022178 clear_tv(&di->di_tv);
22179 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180}
22181
22182/*
22183 * List the value of one internal variable.
22184 */
22185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022186list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022188 char_u *tofree;
22189 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022190 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022191
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022192 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022193 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022194 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022195 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196}
22197
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022199list_one_var_a(
22200 char_u *prefix,
22201 char_u *name,
22202 int type,
22203 char_u *string,
22204 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205{
Bram Moolenaar31859182007-08-14 20:41:13 +000022206 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22207 msg_start();
22208 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 if (name != NULL) /* "a:" vars don't have a name stored */
22210 msg_puts(name);
22211 msg_putchar(' ');
22212 msg_advance(22);
22213 if (type == VAR_NUMBER)
22214 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022215 else if (type == VAR_FUNC)
22216 msg_putchar('*');
22217 else if (type == VAR_LIST)
22218 {
22219 msg_putchar('[');
22220 if (*string == '[')
22221 ++string;
22222 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022223 else if (type == VAR_DICT)
22224 {
22225 msg_putchar('{');
22226 if (*string == '{')
22227 ++string;
22228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229 else
22230 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022231
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022233
22234 if (type == VAR_FUNC)
22235 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022236 if (*first)
22237 {
22238 msg_clr_eos();
22239 *first = FALSE;
22240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241}
22242
22243/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022244 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 * If the variable already exists, the value is updated.
22246 * Otherwise the variable is created.
22247 */
22248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022249set_var(
22250 char_u *name,
22251 typval_T *tv,
22252 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253{
Bram Moolenaar33570922005-01-25 22:26:29 +000022254 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022256 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022257
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022258 ht = find_var_ht(name, &varname);
22259 if (ht == NULL || *varname == NUL)
22260 {
22261 EMSG2(_(e_illvar), name);
22262 return;
22263 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022264 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022265
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022266 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22267 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022268
Bram Moolenaar33570922005-01-25 22:26:29 +000022269 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022271 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022272 if (var_check_ro(v->di_flags, name, FALSE)
22273 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022274 return;
22275 if (v->di_tv.v_type != tv->v_type
22276 && !((v->di_tv.v_type == VAR_STRING
22277 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022278 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022279 || tv->v_type == VAR_NUMBER))
22280#ifdef FEAT_FLOAT
22281 && !((v->di_tv.v_type == VAR_NUMBER
22282 || v->di_tv.v_type == VAR_FLOAT)
22283 && (tv->v_type == VAR_NUMBER
22284 || tv->v_type == VAR_FLOAT))
22285#endif
22286 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022287 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022288 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022289 return;
22290 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022291
22292 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022293 * Handle setting internal v: variables separately where needed to
22294 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022295 */
22296 if (ht == &vimvarht)
22297 {
22298 if (v->di_tv.v_type == VAR_STRING)
22299 {
22300 vim_free(v->di_tv.vval.v_string);
22301 if (copy || tv->v_type != VAR_STRING)
22302 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22303 else
22304 {
22305 /* Take over the string to avoid an extra alloc/free. */
22306 v->di_tv.vval.v_string = tv->vval.v_string;
22307 tv->vval.v_string = NULL;
22308 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022309 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022310 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022311 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022312 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022313 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022314 if (STRCMP(varname, "searchforward") == 0)
22315 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022316#ifdef FEAT_SEARCH_EXTRA
22317 else if (STRCMP(varname, "hlsearch") == 0)
22318 {
22319 no_hlsearch = !v->di_tv.vval.v_number;
22320 redraw_all_later(SOME_VALID);
22321 }
22322#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022323 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022324 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022325 else if (v->di_tv.v_type != tv->v_type)
22326 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022327 }
22328
22329 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 }
22331 else /* add a new variable */
22332 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022333 /* Can't add "v:" variable. */
22334 if (ht == &vimvarht)
22335 {
22336 EMSG2(_(e_illvar), name);
22337 return;
22338 }
22339
Bram Moolenaar92124a32005-06-17 22:03:40 +000022340 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022341 if (!valid_varname(varname))
22342 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022343
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022344 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22345 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022346 if (v == NULL)
22347 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022348 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022349 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022351 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352 return;
22353 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022354 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022356
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022357 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022358 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022359 else
22360 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022361 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022362 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022363 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365}
22366
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022367/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022368 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022369 * Also give an error message.
22370 */
22371 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022372var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022373{
22374 if (flags & DI_FLAGS_RO)
22375 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022376 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022377 return TRUE;
22378 }
22379 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22380 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022381 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022382 return TRUE;
22383 }
22384 return FALSE;
22385}
22386
22387/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022388 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22389 * Also give an error message.
22390 */
22391 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022392var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022393{
22394 if (flags & DI_FLAGS_FIX)
22395 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022396 EMSG2(_("E795: Cannot delete variable %s"),
22397 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022398 return TRUE;
22399 }
22400 return FALSE;
22401}
22402
22403/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022404 * Check if a funcref is assigned to a valid variable name.
22405 * Return TRUE and give an error if not.
22406 */
22407 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022408var_check_func_name(
22409 char_u *name, /* points to start of variable name */
22410 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022411{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022412 /* Allow for w: b: s: and t:. */
22413 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022414 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22415 ? name[2] : name[0]))
22416 {
22417 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22418 name);
22419 return TRUE;
22420 }
22421 /* Don't allow hiding a function. When "v" is not NULL we might be
22422 * assigning another function to the same var, the type is checked
22423 * below. */
22424 if (new_var && function_exists(name))
22425 {
22426 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22427 name);
22428 return TRUE;
22429 }
22430 return FALSE;
22431}
22432
22433/*
22434 * Check if a variable name is valid.
22435 * Return FALSE and give an error if not.
22436 */
22437 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022438valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022439{
22440 char_u *p;
22441
22442 for (p = varname; *p != NUL; ++p)
22443 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22444 && *p != AUTOLOAD_CHAR)
22445 {
22446 EMSG2(_(e_illvar), varname);
22447 return FALSE;
22448 }
22449 return TRUE;
22450}
22451
22452/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022453 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022454 * Also give an error message, using "name" or _("name") when use_gettext is
22455 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022456 */
22457 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022458tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022459{
22460 if (lock & VAR_LOCKED)
22461 {
22462 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022463 name == NULL ? (char_u *)_("Unknown")
22464 : use_gettext ? (char_u *)_(name)
22465 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022466 return TRUE;
22467 }
22468 if (lock & VAR_FIXED)
22469 {
22470 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022471 name == NULL ? (char_u *)_("Unknown")
22472 : use_gettext ? (char_u *)_(name)
22473 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022474 return TRUE;
22475 }
22476 return FALSE;
22477}
22478
22479/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022480 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022481 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022482 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022483 * It is OK for "from" and "to" to point to the same item. This is used to
22484 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022485 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022486 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022487copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022489 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022490 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022491 switch (from->v_type)
22492 {
22493 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022494 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022495 to->vval.v_number = from->vval.v_number;
22496 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022497 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022498#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022499 to->vval.v_float = from->vval.v_float;
22500 break;
22501#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022502 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022503#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022504 to->vval.v_job = from->vval.v_job;
22505 ++to->vval.v_job->jv_refcount;
22506 break;
22507#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022508 case VAR_CHANNEL:
22509#ifdef FEAT_CHANNEL
22510 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022511 if (to->vval.v_channel != NULL)
22512 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022513 break;
22514#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022515 case VAR_STRING:
22516 case VAR_FUNC:
22517 if (from->vval.v_string == NULL)
22518 to->vval.v_string = NULL;
22519 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022520 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022521 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022522 if (from->v_type == VAR_FUNC)
22523 func_ref(to->vval.v_string);
22524 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022525 break;
22526 case VAR_LIST:
22527 if (from->vval.v_list == NULL)
22528 to->vval.v_list = NULL;
22529 else
22530 {
22531 to->vval.v_list = from->vval.v_list;
22532 ++to->vval.v_list->lv_refcount;
22533 }
22534 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022535 case VAR_DICT:
22536 if (from->vval.v_dict == NULL)
22537 to->vval.v_dict = NULL;
22538 else
22539 {
22540 to->vval.v_dict = from->vval.v_dict;
22541 ++to->vval.v_dict->dv_refcount;
22542 }
22543 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022544 case VAR_UNKNOWN:
22545 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022546 break;
22547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548}
22549
22550/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022551 * Make a copy of an item.
22552 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022553 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22554 * reference to an already copied list/dict can be used.
22555 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022556 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022557 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022558item_copy(
22559 typval_T *from,
22560 typval_T *to,
22561 int deep,
22562 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022563{
22564 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022565 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022566
Bram Moolenaar33570922005-01-25 22:26:29 +000022567 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022568 {
22569 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022570 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022571 }
22572 ++recurse;
22573
22574 switch (from->v_type)
22575 {
22576 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022577 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022578 case VAR_STRING:
22579 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022580 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022581 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022582 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022583 copy_tv(from, to);
22584 break;
22585 case VAR_LIST:
22586 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022587 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022588 if (from->vval.v_list == NULL)
22589 to->vval.v_list = NULL;
22590 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22591 {
22592 /* use the copy made earlier */
22593 to->vval.v_list = from->vval.v_list->lv_copylist;
22594 ++to->vval.v_list->lv_refcount;
22595 }
22596 else
22597 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22598 if (to->vval.v_list == NULL)
22599 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022600 break;
22601 case VAR_DICT:
22602 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022603 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022604 if (from->vval.v_dict == NULL)
22605 to->vval.v_dict = NULL;
22606 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22607 {
22608 /* use the copy made earlier */
22609 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22610 ++to->vval.v_dict->dv_refcount;
22611 }
22612 else
22613 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22614 if (to->vval.v_dict == NULL)
22615 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022616 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022617 case VAR_UNKNOWN:
22618 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022619 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022620 }
22621 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022622 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022623}
22624
22625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 * ":echo expr1 ..." print each argument separated with a space, add a
22627 * newline at the end.
22628 * ":echon expr1 ..." print each argument plain.
22629 */
22630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022631ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632{
22633 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022634 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022635 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 char_u *p;
22637 int needclr = TRUE;
22638 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022639 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640
22641 if (eap->skip)
22642 ++emsg_skip;
22643 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22644 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022645 /* If eval1() causes an error message the text from the command may
22646 * still need to be cleared. E.g., "echo 22,44". */
22647 need_clr_eos = needclr;
22648
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022650 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 {
22652 /*
22653 * Report the invalid expression unless the expression evaluation
22654 * has been cancelled due to an aborting error, an interrupt, or an
22655 * exception.
22656 */
22657 if (!aborting())
22658 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022659 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 break;
22661 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022662 need_clr_eos = FALSE;
22663
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 if (!eap->skip)
22665 {
22666 if (atstart)
22667 {
22668 atstart = FALSE;
22669 /* Call msg_start() after eval1(), evaluating the expression
22670 * may cause a message to appear. */
22671 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022672 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022673 /* Mark the saved text as finishing the line, so that what
22674 * follows is displayed on a new line when scrolling back
22675 * at the more prompt. */
22676 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022679 }
22680 else if (eap->cmdidx == CMD_echo)
22681 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022682 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022683 if (p != NULL)
22684 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022686 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022688 if (*p != TAB && needclr)
22689 {
22690 /* remove any text still there from the command */
22691 msg_clr_eos();
22692 needclr = FALSE;
22693 }
22694 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695 }
22696 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022697 {
22698#ifdef FEAT_MBYTE
22699 if (has_mbyte)
22700 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022701 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022702
22703 (void)msg_outtrans_len_attr(p, i, echo_attr);
22704 p += i - 1;
22705 }
22706 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022708 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022711 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022713 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 arg = skipwhite(arg);
22715 }
22716 eap->nextcmd = check_nextcmd(arg);
22717
22718 if (eap->skip)
22719 --emsg_skip;
22720 else
22721 {
22722 /* remove text that may still be there from the command */
22723 if (needclr)
22724 msg_clr_eos();
22725 if (eap->cmdidx == CMD_echo)
22726 msg_end();
22727 }
22728}
22729
22730/*
22731 * ":echohl {name}".
22732 */
22733 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022734ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735{
22736 int id;
22737
22738 id = syn_name2id(eap->arg);
22739 if (id == 0)
22740 echo_attr = 0;
22741 else
22742 echo_attr = syn_id2attr(id);
22743}
22744
22745/*
22746 * ":execute expr1 ..." execute the result of an expression.
22747 * ":echomsg expr1 ..." Print a message
22748 * ":echoerr expr1 ..." Print an error
22749 * Each gets spaces around each argument and a newline at the end for
22750 * echo commands
22751 */
22752 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022753ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754{
22755 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022756 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 int ret = OK;
22758 char_u *p;
22759 garray_T ga;
22760 int len;
22761 int save_did_emsg;
22762
22763 ga_init2(&ga, 1, 80);
22764
22765 if (eap->skip)
22766 ++emsg_skip;
22767 while (*arg != NUL && *arg != '|' && *arg != '\n')
22768 {
22769 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022770 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 {
22772 /*
22773 * Report the invalid expression unless the expression evaluation
22774 * has been cancelled due to an aborting error, an interrupt, or an
22775 * exception.
22776 */
22777 if (!aborting())
22778 EMSG2(_(e_invexpr2), p);
22779 ret = FAIL;
22780 break;
22781 }
22782
22783 if (!eap->skip)
22784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022785 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 len = (int)STRLEN(p);
22787 if (ga_grow(&ga, len + 2) == FAIL)
22788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022789 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 ret = FAIL;
22791 break;
22792 }
22793 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 ga.ga_len += len;
22797 }
22798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022799 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800 arg = skipwhite(arg);
22801 }
22802
22803 if (ret != FAIL && ga.ga_data != NULL)
22804 {
22805 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022808 out_flush();
22809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810 else if (eap->cmdidx == CMD_echoerr)
22811 {
22812 /* We don't want to abort following commands, restore did_emsg. */
22813 save_did_emsg = did_emsg;
22814 EMSG((char_u *)ga.ga_data);
22815 if (!force_abort)
22816 did_emsg = save_did_emsg;
22817 }
22818 else if (eap->cmdidx == CMD_execute)
22819 do_cmdline((char_u *)ga.ga_data,
22820 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22821 }
22822
22823 ga_clear(&ga);
22824
22825 if (eap->skip)
22826 --emsg_skip;
22827
22828 eap->nextcmd = check_nextcmd(arg);
22829}
22830
22831/*
22832 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22833 * "arg" points to the "&" or '+' when called, to "option" when returning.
22834 * Returns NULL when no option name found. Otherwise pointer to the char
22835 * after the option name.
22836 */
22837 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022838find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839{
22840 char_u *p = *arg;
22841
22842 ++p;
22843 if (*p == 'g' && p[1] == ':')
22844 {
22845 *opt_flags = OPT_GLOBAL;
22846 p += 2;
22847 }
22848 else if (*p == 'l' && p[1] == ':')
22849 {
22850 *opt_flags = OPT_LOCAL;
22851 p += 2;
22852 }
22853 else
22854 *opt_flags = 0;
22855
22856 if (!ASCII_ISALPHA(*p))
22857 return NULL;
22858 *arg = p;
22859
22860 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22861 p += 4; /* termcap option */
22862 else
22863 while (ASCII_ISALPHA(*p))
22864 ++p;
22865 return p;
22866}
22867
22868/*
22869 * ":function"
22870 */
22871 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022872ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022873{
22874 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022875 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 int j;
22877 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022879 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 char_u *name = NULL;
22881 char_u *p;
22882 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022883 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884 garray_T newargs;
22885 garray_T newlines;
22886 int varargs = FALSE;
22887 int mustend = FALSE;
22888 int flags = 0;
22889 ufunc_T *fp;
22890 int indent;
22891 int nesting;
22892 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022893 dictitem_T *v;
22894 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022895 static int func_nr = 0; /* number for nameless function */
22896 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022897 hashtab_T *ht;
22898 int todo;
22899 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022900 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901
22902 /*
22903 * ":function" without argument: list functions.
22904 */
22905 if (ends_excmd(*eap->arg))
22906 {
22907 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022908 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022909 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022910 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022911 {
22912 if (!HASHITEM_EMPTY(hi))
22913 {
22914 --todo;
22915 fp = HI2UF(hi);
22916 if (!isdigit(*fp->uf_name))
22917 list_func_head(fp, FALSE);
22918 }
22919 }
22920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 eap->nextcmd = check_nextcmd(eap->arg);
22922 return;
22923 }
22924
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022925 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022926 * ":function /pat": list functions matching pattern.
22927 */
22928 if (*eap->arg == '/')
22929 {
22930 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22931 if (!eap->skip)
22932 {
22933 regmatch_T regmatch;
22934
22935 c = *p;
22936 *p = NUL;
22937 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22938 *p = c;
22939 if (regmatch.regprog != NULL)
22940 {
22941 regmatch.rm_ic = p_ic;
22942
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022943 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022944 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22945 {
22946 if (!HASHITEM_EMPTY(hi))
22947 {
22948 --todo;
22949 fp = HI2UF(hi);
22950 if (!isdigit(*fp->uf_name)
22951 && vim_regexec(&regmatch, fp->uf_name, 0))
22952 list_func_head(fp, FALSE);
22953 }
22954 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022955 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022956 }
22957 }
22958 if (*p == '/')
22959 ++p;
22960 eap->nextcmd = check_nextcmd(p);
22961 return;
22962 }
22963
22964 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022965 * Get the function name. There are these situations:
22966 * func normal function name
22967 * "name" == func, "fudi.fd_dict" == NULL
22968 * dict.func new dictionary entry
22969 * "name" == NULL, "fudi.fd_dict" set,
22970 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22971 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022972 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022973 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22974 * dict.func existing dict entry that's not a Funcref
22975 * "name" == NULL, "fudi.fd_dict" set,
22976 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022977 * s:func script-local function name
22978 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022981 name = trans_function_name(&p, eap->skip, 0, &fudi);
22982 paren = (vim_strchr(p, '(') != NULL);
22983 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 {
22985 /*
22986 * Return on an invalid expression in braces, unless the expression
22987 * evaluation has been cancelled due to an aborting error, an
22988 * interrupt, or an exception.
22989 */
22990 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022991 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022992 if (!eap->skip && fudi.fd_newkey != NULL)
22993 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022994 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997 else
22998 eap->skip = TRUE;
22999 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023000
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 /* An error in a function call during evaluation of an expression in magic
23002 * braces should not cause the function not to be defined. */
23003 saved_did_emsg = did_emsg;
23004 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005
23006 /*
23007 * ":function func" with only function name: list function.
23008 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023009 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 {
23011 if (!ends_excmd(*skipwhite(p)))
23012 {
23013 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023014 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015 }
23016 eap->nextcmd = check_nextcmd(p);
23017 if (eap->nextcmd != NULL)
23018 *p = NUL;
23019 if (!eap->skip && !got_int)
23020 {
23021 fp = find_func(name);
23022 if (fp != NULL)
23023 {
23024 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023025 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023027 if (FUNCLINE(fp, j) == NULL)
23028 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 msg_putchar('\n');
23030 msg_outnum((long)(j + 1));
23031 if (j < 9)
23032 msg_putchar(' ');
23033 if (j < 99)
23034 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023035 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023036 out_flush(); /* show a line at a time */
23037 ui_breakcheck();
23038 }
23039 if (!got_int)
23040 {
23041 msg_putchar('\n');
23042 msg_puts((char_u *)" endfunction");
23043 }
23044 }
23045 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023046 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023048 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049 }
23050
23051 /*
23052 * ":function name(arg1, arg2)" Define function.
23053 */
23054 p = skipwhite(p);
23055 if (*p != '(')
23056 {
23057 if (!eap->skip)
23058 {
23059 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023060 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061 }
23062 /* attempt to continue by skipping some text */
23063 if (vim_strchr(p, '(') != NULL)
23064 p = vim_strchr(p, '(');
23065 }
23066 p = skipwhite(p + 1);
23067
23068 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23069 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23070
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023071 if (!eap->skip)
23072 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023073 /* Check the name of the function. Unless it's a dictionary function
23074 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023075 if (name != NULL)
23076 arg = name;
23077 else
23078 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023079 if (arg != NULL && (fudi.fd_di == NULL
23080 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023081 {
23082 if (*arg == K_SPECIAL)
23083 j = 3;
23084 else
23085 j = 0;
23086 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23087 : eval_isnamec(arg[j])))
23088 ++j;
23089 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023090 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023091 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023092 /* Disallow using the g: dict. */
23093 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23094 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023095 }
23096
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097 /*
23098 * Isolate the arguments: "arg1, arg2, ...)"
23099 */
23100 while (*p != ')')
23101 {
23102 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23103 {
23104 varargs = TRUE;
23105 p += 3;
23106 mustend = TRUE;
23107 }
23108 else
23109 {
23110 arg = p;
23111 while (ASCII_ISALNUM(*p) || *p == '_')
23112 ++p;
23113 if (arg == p || isdigit(*arg)
23114 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23115 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23116 {
23117 if (!eap->skip)
23118 EMSG2(_("E125: Illegal argument: %s"), arg);
23119 break;
23120 }
23121 if (ga_grow(&newargs, 1) == FAIL)
23122 goto erret;
23123 c = *p;
23124 *p = NUL;
23125 arg = vim_strsave(arg);
23126 if (arg == NULL)
23127 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023128
23129 /* Check for duplicate argument name. */
23130 for (i = 0; i < newargs.ga_len; ++i)
23131 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23132 {
23133 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023134 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023135 goto erret;
23136 }
23137
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23139 *p = c;
23140 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 if (*p == ',')
23142 ++p;
23143 else
23144 mustend = TRUE;
23145 }
23146 p = skipwhite(p);
23147 if (mustend && *p != ')')
23148 {
23149 if (!eap->skip)
23150 EMSG2(_(e_invarg2), eap->arg);
23151 break;
23152 }
23153 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023154 if (*p != ')')
23155 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 ++p; /* skip the ')' */
23157
Bram Moolenaare9a41262005-01-15 22:18:47 +000023158 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159 for (;;)
23160 {
23161 p = skipwhite(p);
23162 if (STRNCMP(p, "range", 5) == 0)
23163 {
23164 flags |= FC_RANGE;
23165 p += 5;
23166 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023167 else if (STRNCMP(p, "dict", 4) == 0)
23168 {
23169 flags |= FC_DICT;
23170 p += 4;
23171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 else if (STRNCMP(p, "abort", 5) == 0)
23173 {
23174 flags |= FC_ABORT;
23175 p += 5;
23176 }
23177 else
23178 break;
23179 }
23180
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023181 /* When there is a line break use what follows for the function body.
23182 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23183 if (*p == '\n')
23184 line_arg = p + 1;
23185 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 EMSG(_(e_trailing));
23187
23188 /*
23189 * Read the body of the function, until ":endfunction" is found.
23190 */
23191 if (KeyTyped)
23192 {
23193 /* Check if the function already exists, don't let the user type the
23194 * whole function before telling him it doesn't work! For a script we
23195 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023196 if (!eap->skip && !eap->forceit)
23197 {
23198 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23199 EMSG(_(e_funcdict));
23200 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023201 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023204 if (!eap->skip && did_emsg)
23205 goto erret;
23206
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 msg_putchar('\n'); /* don't overwrite the function name */
23208 cmdline_row = msg_row;
23209 }
23210
23211 indent = 2;
23212 nesting = 0;
23213 for (;;)
23214 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023215 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023216 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023217 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023218 saved_wait_return = FALSE;
23219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023221 sourcing_lnum_off = sourcing_lnum;
23222
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023223 if (line_arg != NULL)
23224 {
23225 /* Use eap->arg, split up in parts by line breaks. */
23226 theline = line_arg;
23227 p = vim_strchr(theline, '\n');
23228 if (p == NULL)
23229 line_arg += STRLEN(line_arg);
23230 else
23231 {
23232 *p = NUL;
23233 line_arg = p + 1;
23234 }
23235 }
23236 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 theline = getcmdline(':', 0L, indent);
23238 else
23239 theline = eap->getline(':', eap->cookie, indent);
23240 if (KeyTyped)
23241 lines_left = Rows - 1;
23242 if (theline == NULL)
23243 {
23244 EMSG(_("E126: Missing :endfunction"));
23245 goto erret;
23246 }
23247
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023248 /* Detect line continuation: sourcing_lnum increased more than one. */
23249 if (sourcing_lnum > sourcing_lnum_off + 1)
23250 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23251 else
23252 sourcing_lnum_off = 0;
23253
Bram Moolenaar071d4272004-06-13 20:20:40 +000023254 if (skip_until != NULL)
23255 {
23256 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23257 * don't check for ":endfunc". */
23258 if (STRCMP(theline, skip_until) == 0)
23259 {
23260 vim_free(skip_until);
23261 skip_until = NULL;
23262 }
23263 }
23264 else
23265 {
23266 /* skip ':' and blanks*/
23267 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23268 ;
23269
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023270 /* Check for "endfunction". */
23271 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023272 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023273 if (line_arg == NULL)
23274 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275 break;
23276 }
23277
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023278 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023279 * at "end". */
23280 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23281 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023282 else if (STRNCMP(p, "if", 2) == 0
23283 || STRNCMP(p, "wh", 2) == 0
23284 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023285 || STRNCMP(p, "try", 3) == 0)
23286 indent += 2;
23287
23288 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023289 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023291 if (*p == '!')
23292 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023294 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23295 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023297 ++nesting;
23298 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299 }
23300 }
23301
23302 /* Check for ":append" or ":insert". */
23303 p = skip_range(p, NULL);
23304 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23305 || (p[0] == 'i'
23306 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23307 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23308 skip_until = vim_strsave((char_u *)".");
23309
23310 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23311 arg = skipwhite(skiptowhite(p));
23312 if (arg[0] == '<' && arg[1] =='<'
23313 && ((p[0] == 'p' && p[1] == 'y'
23314 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23315 || (p[0] == 'p' && p[1] == 'e'
23316 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23317 || (p[0] == 't' && p[1] == 'c'
23318 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023319 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23320 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023321 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23322 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023323 || (p[0] == 'm' && p[1] == 'z'
23324 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325 ))
23326 {
23327 /* ":python <<" continues until a dot, like ":append" */
23328 p = skipwhite(arg + 2);
23329 if (*p == NUL)
23330 skip_until = vim_strsave((char_u *)".");
23331 else
23332 skip_until = vim_strsave(p);
23333 }
23334 }
23335
23336 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023337 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023338 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023339 if (line_arg == NULL)
23340 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023342 }
23343
23344 /* Copy the line to newly allocated memory. get_one_sourceline()
23345 * allocates 250 bytes per line, this saves 80% on average. The cost
23346 * is an extra alloc/free. */
23347 p = vim_strsave(theline);
23348 if (p != NULL)
23349 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023350 if (line_arg == NULL)
23351 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023352 theline = p;
23353 }
23354
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023355 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23356
23357 /* Add NULL lines for continuation lines, so that the line count is
23358 * equal to the index in the growarray. */
23359 while (sourcing_lnum_off-- > 0)
23360 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023361
23362 /* Check for end of eap->arg. */
23363 if (line_arg != NULL && *line_arg == NUL)
23364 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023365 }
23366
23367 /* Don't define the function when skipping commands or when an error was
23368 * detected. */
23369 if (eap->skip || did_emsg)
23370 goto erret;
23371
23372 /*
23373 * If there are no errors, add the function
23374 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023375 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023376 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023377 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023378 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023379 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023380 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023381 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023382 goto erret;
23383 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023384
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023385 fp = find_func(name);
23386 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023388 if (!eap->forceit)
23389 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023390 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023391 goto erret;
23392 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023393 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023394 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023395 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023396 name);
23397 goto erret;
23398 }
23399 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023400 ga_clear_strings(&(fp->uf_args));
23401 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023402 vim_free(name);
23403 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023405 }
23406 else
23407 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023408 char numbuf[20];
23409
23410 fp = NULL;
23411 if (fudi.fd_newkey == NULL && !eap->forceit)
23412 {
23413 EMSG(_(e_funcdict));
23414 goto erret;
23415 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023416 if (fudi.fd_di == NULL)
23417 {
23418 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023419 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023420 goto erret;
23421 }
23422 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023423 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023424 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023425
23426 /* Give the function a sequential number. Can only be used with a
23427 * Funcref! */
23428 vim_free(name);
23429 sprintf(numbuf, "%d", ++func_nr);
23430 name = vim_strsave((char_u *)numbuf);
23431 if (name == NULL)
23432 goto erret;
23433 }
23434
23435 if (fp == NULL)
23436 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023437 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023438 {
23439 int slen, plen;
23440 char_u *scriptname;
23441
23442 /* Check that the autoload name matches the script name. */
23443 j = FAIL;
23444 if (sourcing_name != NULL)
23445 {
23446 scriptname = autoload_name(name);
23447 if (scriptname != NULL)
23448 {
23449 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023450 plen = (int)STRLEN(p);
23451 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023452 if (slen > plen && fnamecmp(p,
23453 sourcing_name + slen - plen) == 0)
23454 j = OK;
23455 vim_free(scriptname);
23456 }
23457 }
23458 if (j == FAIL)
23459 {
23460 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23461 goto erret;
23462 }
23463 }
23464
23465 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023466 if (fp == NULL)
23467 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023468
23469 if (fudi.fd_dict != NULL)
23470 {
23471 if (fudi.fd_di == NULL)
23472 {
23473 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023474 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023475 if (fudi.fd_di == NULL)
23476 {
23477 vim_free(fp);
23478 goto erret;
23479 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023480 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23481 {
23482 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023483 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023484 goto erret;
23485 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023486 }
23487 else
23488 /* overwrite existing dict entry */
23489 clear_tv(&fudi.fd_di->di_tv);
23490 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023491 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023492 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023493 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023494
23495 /* behave like "dict" was used */
23496 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023497 }
23498
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023500 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023501 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23502 {
23503 vim_free(fp);
23504 goto erret;
23505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023507 fp->uf_args = newargs;
23508 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023509#ifdef FEAT_PROFILE
23510 fp->uf_tml_count = NULL;
23511 fp->uf_tml_total = NULL;
23512 fp->uf_tml_self = NULL;
23513 fp->uf_profiling = FALSE;
23514 if (prof_def_func())
23515 func_do_profile(fp);
23516#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023517 fp->uf_varargs = varargs;
23518 fp->uf_flags = flags;
23519 fp->uf_calls = 0;
23520 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023521 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522
23523erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524 ga_clear_strings(&newargs);
23525 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023526ret_free:
23527 vim_free(skip_until);
23528 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023531 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532}
23533
23534/*
23535 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023536 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023538 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023539 * TFN_INT: internal function name OK
23540 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023541 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542 * Advances "pp" to just after the function name (if no error).
23543 */
23544 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023545trans_function_name(
23546 char_u **pp,
23547 int skip, /* only find the end, don't evaluate */
23548 int flags,
23549 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023550{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023551 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 char_u *start;
23553 char_u *end;
23554 int lead;
23555 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023557 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023558
23559 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023560 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023562
23563 /* Check for hard coded <SNR>: already translated function ID (from a user
23564 * command). */
23565 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23566 && (*pp)[2] == (int)KE_SNR)
23567 {
23568 *pp += 3;
23569 len = get_id_len(pp) + 3;
23570 return vim_strnsave(start, len);
23571 }
23572
23573 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23574 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023576 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023578
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023579 /* Note that TFN_ flags use the same values as GLV_ flags. */
23580 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023581 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023582 if (end == start)
23583 {
23584 if (!skip)
23585 EMSG(_("E129: Function name required"));
23586 goto theend;
23587 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023588 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023589 {
23590 /*
23591 * Report an invalid expression in braces, unless the expression
23592 * evaluation has been cancelled due to an aborting error, an
23593 * interrupt, or an exception.
23594 */
23595 if (!aborting())
23596 {
23597 if (end != NULL)
23598 EMSG2(_(e_invarg2), start);
23599 }
23600 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023601 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023602 goto theend;
23603 }
23604
23605 if (lv.ll_tv != NULL)
23606 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023607 if (fdp != NULL)
23608 {
23609 fdp->fd_dict = lv.ll_dict;
23610 fdp->fd_newkey = lv.ll_newkey;
23611 lv.ll_newkey = NULL;
23612 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023613 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023614 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23615 {
23616 name = vim_strsave(lv.ll_tv->vval.v_string);
23617 *pp = end;
23618 }
23619 else
23620 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023621 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23622 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023623 EMSG(_(e_funcref));
23624 else
23625 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023626 name = NULL;
23627 }
23628 goto theend;
23629 }
23630
23631 if (lv.ll_name == NULL)
23632 {
23633 /* Error found, but continue after the function name. */
23634 *pp = end;
23635 goto theend;
23636 }
23637
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023638 /* Check if the name is a Funcref. If so, use the value. */
23639 if (lv.ll_exp_name != NULL)
23640 {
23641 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023642 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023643 if (name == lv.ll_exp_name)
23644 name = NULL;
23645 }
23646 else
23647 {
23648 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023649 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023650 if (name == *pp)
23651 name = NULL;
23652 }
23653 if (name != NULL)
23654 {
23655 name = vim_strsave(name);
23656 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023657 if (STRNCMP(name, "<SNR>", 5) == 0)
23658 {
23659 /* Change "<SNR>" to the byte sequence. */
23660 name[0] = K_SPECIAL;
23661 name[1] = KS_EXTRA;
23662 name[2] = (int)KE_SNR;
23663 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23664 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023665 goto theend;
23666 }
23667
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023668 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023669 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023670 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023671 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23672 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23673 {
23674 /* When there was "s:" already or the name expanded to get a
23675 * leading "s:" then remove it. */
23676 lv.ll_name += 2;
23677 len -= 2;
23678 lead = 2;
23679 }
23680 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023681 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023682 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023683 /* skip over "s:" and "g:" */
23684 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023685 lv.ll_name += 2;
23686 len = (int)(end - lv.ll_name);
23687 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023688
23689 /*
23690 * Copy the function name to allocated memory.
23691 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23692 * Accept <SNR>123_name() outside a script.
23693 */
23694 if (skip)
23695 lead = 0; /* do nothing */
23696 else if (lead > 0)
23697 {
23698 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023699 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23700 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023701 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023702 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023703 if (current_SID <= 0)
23704 {
23705 EMSG(_(e_usingsid));
23706 goto theend;
23707 }
23708 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23709 lead += (int)STRLEN(sid_buf);
23710 }
23711 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023712 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023713 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023714 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023715 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023716 goto theend;
23717 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023718 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023719 {
23720 char_u *cp = vim_strchr(lv.ll_name, ':');
23721
23722 if (cp != NULL && cp < end)
23723 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023724 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023725 goto theend;
23726 }
23727 }
23728
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023729 name = alloc((unsigned)(len + lead + 1));
23730 if (name != NULL)
23731 {
23732 if (lead > 0)
23733 {
23734 name[0] = K_SPECIAL;
23735 name[1] = KS_EXTRA;
23736 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023737 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023738 STRCPY(name + 3, sid_buf);
23739 }
23740 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023741 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023742 }
23743 *pp = end;
23744
23745theend:
23746 clear_lval(&lv);
23747 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748}
23749
23750/*
23751 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23752 * Return 2 if "p" starts with "s:".
23753 * Return 0 otherwise.
23754 */
23755 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023756eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023758 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23759 * the standard library function. */
23760 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23761 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023762 return 5;
23763 if (p[0] == 's' && p[1] == ':')
23764 return 2;
23765 return 0;
23766}
23767
23768/*
23769 * Return TRUE if "p" starts with "<SID>" or "s:".
23770 * Only works if eval_fname_script() returned non-zero for "p"!
23771 */
23772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023773eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023774{
23775 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23776}
23777
23778/*
23779 * List the head of the function: "name(arg1, arg2)".
23780 */
23781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023782list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023783{
23784 int j;
23785
23786 msg_start();
23787 if (indent)
23788 MSG_PUTS(" ");
23789 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023790 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023791 {
23792 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023793 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023794 }
23795 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023796 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023797 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023798 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023799 {
23800 if (j)
23801 MSG_PUTS(", ");
23802 msg_puts(FUNCARG(fp, j));
23803 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023804 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805 {
23806 if (j)
23807 MSG_PUTS(", ");
23808 MSG_PUTS("...");
23809 }
23810 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023811 if (fp->uf_flags & FC_ABORT)
23812 MSG_PUTS(" abort");
23813 if (fp->uf_flags & FC_RANGE)
23814 MSG_PUTS(" range");
23815 if (fp->uf_flags & FC_DICT)
23816 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023817 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023818 if (p_verbose > 0)
23819 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820}
23821
23822/*
23823 * Find a function by name, return pointer to it in ufuncs.
23824 * Return NULL for unknown function.
23825 */
23826 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023827find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023828{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023829 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023831 hi = hash_find(&func_hashtab, name);
23832 if (!HASHITEM_EMPTY(hi))
23833 return HI2UF(hi);
23834 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835}
23836
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023837#if defined(EXITFREE) || defined(PROTO)
23838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023839free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023840{
23841 hashitem_T *hi;
23842
23843 /* Need to start all over every time, because func_free() may change the
23844 * hash table. */
23845 while (func_hashtab.ht_used > 0)
23846 for (hi = func_hashtab.ht_array; ; ++hi)
23847 if (!HASHITEM_EMPTY(hi))
23848 {
23849 func_free(HI2UF(hi));
23850 break;
23851 }
23852}
23853#endif
23854
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023856translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023857{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023858 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023859 return find_internal_func(name) >= 0;
23860 return find_func(name) != NULL;
23861}
23862
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023863/*
23864 * Return TRUE if a function "name" exists.
23865 */
23866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023867function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023868{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023869 char_u *nm = name;
23870 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023871 int n = FALSE;
23872
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023873 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23874 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023875 nm = skipwhite(nm);
23876
23877 /* Only accept "funcname", "funcname ", "funcname (..." and
23878 * "funcname(...", not "funcname!...". */
23879 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023880 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023881 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023882 return n;
23883}
23884
Bram Moolenaara1544c02013-05-30 12:35:52 +020023885 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023886get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023887{
23888 char_u *nm = name;
23889 char_u *p;
23890
23891 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23892
23893 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023894 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023895 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023896
Bram Moolenaara1544c02013-05-30 12:35:52 +020023897 vim_free(p);
23898 return NULL;
23899}
23900
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023901/*
23902 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023903 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23904 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023905 */
23906 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023907builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023908{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023909 char_u *p;
23910
23911 if (!ASCII_ISLOWER(name[0]))
23912 return FALSE;
23913 p = vim_strchr(name, AUTOLOAD_CHAR);
23914 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023915}
23916
Bram Moolenaar05159a02005-02-26 23:04:13 +000023917#if defined(FEAT_PROFILE) || defined(PROTO)
23918/*
23919 * Start profiling function "fp".
23920 */
23921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023922func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023923{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023924 int len = fp->uf_lines.ga_len;
23925
23926 if (len == 0)
23927 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023928 fp->uf_tm_count = 0;
23929 profile_zero(&fp->uf_tm_self);
23930 profile_zero(&fp->uf_tm_total);
23931 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023932 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023933 if (fp->uf_tml_total == NULL)
23934 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023935 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023936 if (fp->uf_tml_self == NULL)
23937 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023938 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023939 fp->uf_tml_idx = -1;
23940 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23941 || fp->uf_tml_self == NULL)
23942 return; /* out of memory */
23943
23944 fp->uf_profiling = TRUE;
23945}
23946
23947/*
23948 * Dump the profiling results for all functions in file "fd".
23949 */
23950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023951func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023952{
23953 hashitem_T *hi;
23954 int todo;
23955 ufunc_T *fp;
23956 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023957 ufunc_T **sorttab;
23958 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023959
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023960 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023961 if (todo == 0)
23962 return; /* nothing to dump */
23963
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023964 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023965
Bram Moolenaar05159a02005-02-26 23:04:13 +000023966 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23967 {
23968 if (!HASHITEM_EMPTY(hi))
23969 {
23970 --todo;
23971 fp = HI2UF(hi);
23972 if (fp->uf_profiling)
23973 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023974 if (sorttab != NULL)
23975 sorttab[st_len++] = fp;
23976
Bram Moolenaar05159a02005-02-26 23:04:13 +000023977 if (fp->uf_name[0] == K_SPECIAL)
23978 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23979 else
23980 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23981 if (fp->uf_tm_count == 1)
23982 fprintf(fd, "Called 1 time\n");
23983 else
23984 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23985 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23986 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23987 fprintf(fd, "\n");
23988 fprintf(fd, "count total (s) self (s)\n");
23989
23990 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23991 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023992 if (FUNCLINE(fp, i) == NULL)
23993 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023994 prof_func_line(fd, fp->uf_tml_count[i],
23995 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023996 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23997 }
23998 fprintf(fd, "\n");
23999 }
24000 }
24001 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024002
24003 if (sorttab != NULL && st_len > 0)
24004 {
24005 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24006 prof_total_cmp);
24007 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24008 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24009 prof_self_cmp);
24010 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24011 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024012
24013 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024014}
Bram Moolenaar73830342005-02-28 22:48:19 +000024015
24016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024017prof_sort_list(
24018 FILE *fd,
24019 ufunc_T **sorttab,
24020 int st_len,
24021 char *title,
24022 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024023{
24024 int i;
24025 ufunc_T *fp;
24026
24027 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24028 fprintf(fd, "count total (s) self (s) function\n");
24029 for (i = 0; i < 20 && i < st_len; ++i)
24030 {
24031 fp = sorttab[i];
24032 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24033 prefer_self);
24034 if (fp->uf_name[0] == K_SPECIAL)
24035 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24036 else
24037 fprintf(fd, " %s()\n", fp->uf_name);
24038 }
24039 fprintf(fd, "\n");
24040}
24041
24042/*
24043 * Print the count and times for one function or function line.
24044 */
24045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024046prof_func_line(
24047 FILE *fd,
24048 int count,
24049 proftime_T *total,
24050 proftime_T *self,
24051 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024052{
24053 if (count > 0)
24054 {
24055 fprintf(fd, "%5d ", count);
24056 if (prefer_self && profile_equal(total, self))
24057 fprintf(fd, " ");
24058 else
24059 fprintf(fd, "%s ", profile_msg(total));
24060 if (!prefer_self && profile_equal(total, self))
24061 fprintf(fd, " ");
24062 else
24063 fprintf(fd, "%s ", profile_msg(self));
24064 }
24065 else
24066 fprintf(fd, " ");
24067}
24068
24069/*
24070 * Compare function for total time sorting.
24071 */
24072 static int
24073#ifdef __BORLANDC__
24074_RTLENTRYF
24075#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024076prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024077{
24078 ufunc_T *p1, *p2;
24079
24080 p1 = *(ufunc_T **)s1;
24081 p2 = *(ufunc_T **)s2;
24082 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24083}
24084
24085/*
24086 * Compare function for self time sorting.
24087 */
24088 static int
24089#ifdef __BORLANDC__
24090_RTLENTRYF
24091#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024092prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024093{
24094 ufunc_T *p1, *p2;
24095
24096 p1 = *(ufunc_T **)s1;
24097 p2 = *(ufunc_T **)s2;
24098 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24099}
24100
Bram Moolenaar05159a02005-02-26 23:04:13 +000024101#endif
24102
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024103/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024104 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024105 * Return TRUE if a package was loaded.
24106 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024107 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024108script_autoload(
24109 char_u *name,
24110 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024111{
24112 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024113 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024114 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024115 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024116
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024117 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024118 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024119 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024120 return FALSE;
24121
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024122 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024123
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024124 /* Find the name in the list of previously loaded package names. Skip
24125 * "autoload/", it's always the same. */
24126 for (i = 0; i < ga_loaded.ga_len; ++i)
24127 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24128 break;
24129 if (!reload && i < ga_loaded.ga_len)
24130 ret = FALSE; /* was loaded already */
24131 else
24132 {
24133 /* Remember the name if it wasn't loaded already. */
24134 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24135 {
24136 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24137 tofree = NULL;
24138 }
24139
24140 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024141 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024142 ret = TRUE;
24143 }
24144
24145 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024146 return ret;
24147}
24148
24149/*
24150 * Return the autoload script name for a function or variable name.
24151 * Returns NULL when out of memory.
24152 */
24153 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024154autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024155{
24156 char_u *p;
24157 char_u *scriptname;
24158
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024159 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024160 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24161 if (scriptname == NULL)
24162 return FALSE;
24163 STRCPY(scriptname, "autoload/");
24164 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024165 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024166 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024167 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024168 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024169 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024170}
24171
Bram Moolenaar071d4272004-06-13 20:20:40 +000024172#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24173
24174/*
24175 * Function given to ExpandGeneric() to obtain the list of user defined
24176 * function names.
24177 */
24178 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024179get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024180{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024181 static long_u done;
24182 static hashitem_T *hi;
24183 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184
24185 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024187 done = 0;
24188 hi = func_hashtab.ht_array;
24189 }
24190 if (done < func_hashtab.ht_used)
24191 {
24192 if (done++ > 0)
24193 ++hi;
24194 while (HASHITEM_EMPTY(hi))
24195 ++hi;
24196 fp = HI2UF(hi);
24197
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024198 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024199 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024200
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024201 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24202 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203
24204 cat_func_name(IObuff, fp);
24205 if (xp->xp_context != EXPAND_USER_FUNC)
24206 {
24207 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024208 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209 STRCAT(IObuff, ")");
24210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024211 return IObuff;
24212 }
24213 return NULL;
24214}
24215
24216#endif /* FEAT_CMDL_COMPL */
24217
24218/*
24219 * Copy the function name of "fp" to buffer "buf".
24220 * "buf" must be able to hold the function name plus three bytes.
24221 * Takes care of script-local function names.
24222 */
24223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024224cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024226 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227 {
24228 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024229 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024230 }
24231 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024232 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024233}
24234
24235/*
24236 * ":delfunction {name}"
24237 */
24238 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024239ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024240{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024241 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 char_u *p;
24243 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024244 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245
24246 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024247 name = trans_function_name(&p, eap->skip, 0, &fudi);
24248 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024249 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024250 {
24251 if (fudi.fd_dict != NULL && !eap->skip)
24252 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024253 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255 if (!ends_excmd(*skipwhite(p)))
24256 {
24257 vim_free(name);
24258 EMSG(_(e_trailing));
24259 return;
24260 }
24261 eap->nextcmd = check_nextcmd(p);
24262 if (eap->nextcmd != NULL)
24263 *p = NUL;
24264
24265 if (!eap->skip)
24266 fp = find_func(name);
24267 vim_free(name);
24268
24269 if (!eap->skip)
24270 {
24271 if (fp == NULL)
24272 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024273 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024274 return;
24275 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024276 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277 {
24278 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24279 return;
24280 }
24281
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024282 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024284 /* Delete the dict item that refers to the function, it will
24285 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024286 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024287 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024288 else
24289 func_free(fp);
24290 }
24291}
24292
24293/*
24294 * Free a function and remove it from the list of functions.
24295 */
24296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024297func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024298{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024299 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024300
24301 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024302 ga_clear_strings(&(fp->uf_args));
24303 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024304#ifdef FEAT_PROFILE
24305 vim_free(fp->uf_tml_count);
24306 vim_free(fp->uf_tml_total);
24307 vim_free(fp->uf_tml_self);
24308#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024309
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024310 /* remove the function from the function hashtable */
24311 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24312 if (HASHITEM_EMPTY(hi))
24313 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024314 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024315 hash_remove(&func_hashtab, hi);
24316
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024317 vim_free(fp);
24318}
24319
24320/*
24321 * Unreference a Function: decrement the reference count and free it when it
24322 * becomes zero. Only for numbered functions.
24323 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024324 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024325func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024326{
24327 ufunc_T *fp;
24328
24329 if (name != NULL && isdigit(*name))
24330 {
24331 fp = find_func(name);
24332 if (fp == NULL)
24333 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024334 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024335 {
24336 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024337 * when "uf_calls" becomes zero. */
24338 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024339 func_free(fp);
24340 }
24341 }
24342}
24343
24344/*
24345 * Count a reference to a Function.
24346 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024347 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024348func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024349{
24350 ufunc_T *fp;
24351
24352 if (name != NULL && isdigit(*name))
24353 {
24354 fp = find_func(name);
24355 if (fp == NULL)
24356 EMSG2(_(e_intern2), "func_ref()");
24357 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024358 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024359 }
24360}
24361
24362/*
24363 * Call a user function.
24364 */
24365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024366call_user_func(
24367 ufunc_T *fp, /* pointer to function */
24368 int argcount, /* nr of args */
24369 typval_T *argvars, /* arguments */
24370 typval_T *rettv, /* return value */
24371 linenr_T firstline, /* first line of range */
24372 linenr_T lastline, /* last line of range */
24373 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024374{
Bram Moolenaar33570922005-01-25 22:26:29 +000024375 char_u *save_sourcing_name;
24376 linenr_T save_sourcing_lnum;
24377 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024378 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024379 int save_did_emsg;
24380 static int depth = 0;
24381 dictitem_T *v;
24382 int fixvar_idx = 0; /* index in fixvar[] */
24383 int i;
24384 int ai;
24385 char_u numbuf[NUMBUFLEN];
24386 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024387 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024388#ifdef FEAT_PROFILE
24389 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024390 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392
24393 /* If depth of calling is getting too high, don't execute the function */
24394 if (depth >= p_mfd)
24395 {
24396 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024397 rettv->v_type = VAR_NUMBER;
24398 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024399 return;
24400 }
24401 ++depth;
24402
24403 line_breakcheck(); /* check for CTRL-C hit */
24404
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024405 fc = (funccall_T *)alloc(sizeof(funccall_T));
24406 fc->caller = current_funccal;
24407 current_funccal = fc;
24408 fc->func = fp;
24409 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024410 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024411 fc->linenr = 0;
24412 fc->returned = FALSE;
24413 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024415 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24416 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024417
Bram Moolenaar33570922005-01-25 22:26:29 +000024418 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024419 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024420 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24421 * each argument variable and saves a lot of time.
24422 */
24423 /*
24424 * Init l: variables.
24425 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024426 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024427 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024428 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024429 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24430 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024431 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024432 name = v->di_key;
24433 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024434 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024435 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024436 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024437 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024438 v->di_tv.vval.v_dict = selfdict;
24439 ++selfdict->dv_refcount;
24440 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024441
Bram Moolenaar33570922005-01-25 22:26:29 +000024442 /*
24443 * Init a: variables.
24444 * Set a:0 to "argcount".
24445 * Set a:000 to a list with room for the "..." arguments.
24446 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024447 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024448 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024449 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024450 /* Use "name" to avoid a warning from some compiler that checks the
24451 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024452 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024453 name = v->di_key;
24454 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024455 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024456 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024457 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024458 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024459 v->di_tv.vval.v_list = &fc->l_varlist;
24460 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24461 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24462 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024463
24464 /*
24465 * Set a:firstline to "firstline" and a:lastline to "lastline".
24466 * Set a:name to named arguments.
24467 * Set a:N to the "..." arguments.
24468 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024469 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024470 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024471 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024472 (varnumber_T)lastline);
24473 for (i = 0; i < argcount; ++i)
24474 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024475 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024476 if (ai < 0)
24477 /* named argument a:name */
24478 name = FUNCARG(fp, i);
24479 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024480 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024481 /* "..." argument a:1, a:2, etc. */
24482 sprintf((char *)numbuf, "%d", ai + 1);
24483 name = numbuf;
24484 }
24485 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24486 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024487 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024488 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24489 }
24490 else
24491 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024492 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24493 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024494 if (v == NULL)
24495 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024496 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024497 }
24498 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024499 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024500
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024501 /* Note: the values are copied directly to avoid alloc/free.
24502 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024503 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024504 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024505
24506 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24507 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024508 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24509 fc->l_listitems[ai].li_tv = argvars[i];
24510 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024511 }
24512 }
24513
Bram Moolenaar071d4272004-06-13 20:20:40 +000024514 /* Don't redraw while executing the function. */
24515 ++RedrawingDisabled;
24516 save_sourcing_name = sourcing_name;
24517 save_sourcing_lnum = sourcing_lnum;
24518 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024519 /* need space for function name + ("function " + 3) or "[number]" */
24520 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24521 + STRLEN(fp->uf_name) + 20;
24522 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024523 if (sourcing_name != NULL)
24524 {
24525 if (save_sourcing_name != NULL
24526 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024527 sprintf((char *)sourcing_name, "%s[%d]..",
24528 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024529 else
24530 STRCPY(sourcing_name, "function ");
24531 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24532
24533 if (p_verbose >= 12)
24534 {
24535 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024536 verbose_enter_scroll();
24537
Bram Moolenaar555b2802005-05-19 21:08:39 +000024538 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024539 if (p_verbose >= 14)
24540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024542 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024543 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024544 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545
24546 msg_puts((char_u *)"(");
24547 for (i = 0; i < argcount; ++i)
24548 {
24549 if (i > 0)
24550 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024551 if (argvars[i].v_type == VAR_NUMBER)
24552 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024553 else
24554 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024555 /* Do not want errors such as E724 here. */
24556 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024557 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024558 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024559 if (s != NULL)
24560 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024561 if (vim_strsize(s) > MSG_BUF_CLEN)
24562 {
24563 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24564 s = buf;
24565 }
24566 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024567 vim_free(tofree);
24568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024569 }
24570 }
24571 msg_puts((char_u *)")");
24572 }
24573 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024574
24575 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576 --no_wait_return;
24577 }
24578 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024579#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024580 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024581 {
24582 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24583 func_do_profile(fp);
24584 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024585 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024586 {
24587 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024588 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024589 profile_zero(&fp->uf_tm_children);
24590 }
24591 script_prof_save(&wait_start);
24592 }
24593#endif
24594
Bram Moolenaar071d4272004-06-13 20:20:40 +000024595 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024596 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024597 save_did_emsg = did_emsg;
24598 did_emsg = FALSE;
24599
24600 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024601 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024602 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24603
24604 --RedrawingDisabled;
24605
24606 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024607 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024609 clear_tv(rettv);
24610 rettv->v_type = VAR_NUMBER;
24611 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612 }
24613
Bram Moolenaar05159a02005-02-26 23:04:13 +000024614#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024615 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024616 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024617 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024618 profile_end(&call_start);
24619 profile_sub_wait(&wait_start, &call_start);
24620 profile_add(&fp->uf_tm_total, &call_start);
24621 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024622 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024623 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024624 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24625 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024626 }
24627 }
24628#endif
24629
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630 /* when being verbose, mention the return value */
24631 if (p_verbose >= 12)
24632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024634 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024637 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024638 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024639 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024640 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024643 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024644 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024645 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024646 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024647
Bram Moolenaar555b2802005-05-19 21:08:39 +000024648 /* The value may be very long. Skip the middle part, so that we
24649 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024650 * truncate it at the end. Don't want errors such as E724 here. */
24651 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024652 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024653 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024654 if (s != NULL)
24655 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024656 if (vim_strsize(s) > MSG_BUF_CLEN)
24657 {
24658 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24659 s = buf;
24660 }
24661 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024662 vim_free(tofree);
24663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664 }
24665 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024666
24667 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668 --no_wait_return;
24669 }
24670
24671 vim_free(sourcing_name);
24672 sourcing_name = save_sourcing_name;
24673 sourcing_lnum = save_sourcing_lnum;
24674 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024675#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024676 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024677 script_prof_restore(&wait_start);
24678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679
24680 if (p_verbose >= 12 && sourcing_name != NULL)
24681 {
24682 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024683 verbose_enter_scroll();
24684
Bram Moolenaar555b2802005-05-19 21:08:39 +000024685 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024686 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024687
24688 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024689 --no_wait_return;
24690 }
24691
24692 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024693 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024695
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024696 /* If the a:000 list and the l: and a: dicts are not referenced we can
24697 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024698 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24699 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24700 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24701 {
24702 free_funccal(fc, FALSE);
24703 }
24704 else
24705 {
24706 hashitem_T *hi;
24707 listitem_T *li;
24708 int todo;
24709
24710 /* "fc" is still in use. This can happen when returning "a:000" or
24711 * assigning "l:" to a global variable.
24712 * Link "fc" in the list for garbage collection later. */
24713 fc->caller = previous_funccal;
24714 previous_funccal = fc;
24715
24716 /* Make a copy of the a: variables, since we didn't do that above. */
24717 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24718 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24719 {
24720 if (!HASHITEM_EMPTY(hi))
24721 {
24722 --todo;
24723 v = HI2DI(hi);
24724 copy_tv(&v->di_tv, &v->di_tv);
24725 }
24726 }
24727
24728 /* Make a copy of the a:000 items, since we didn't do that above. */
24729 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24730 copy_tv(&li->li_tv, &li->li_tv);
24731 }
24732}
24733
24734/*
24735 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024736 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024737 */
24738 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024739can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024740{
24741 return (fc->l_varlist.lv_copyID != copyID
24742 && fc->l_vars.dv_copyID != copyID
24743 && fc->l_avars.dv_copyID != copyID);
24744}
24745
24746/*
24747 * Free "fc" and what it contains.
24748 */
24749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024750free_funccal(
24751 funccall_T *fc,
24752 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024753{
24754 listitem_T *li;
24755
24756 /* The a: variables typevals may not have been allocated, only free the
24757 * allocated variables. */
24758 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24759
24760 /* free all l: variables */
24761 vars_clear(&fc->l_vars.dv_hashtab);
24762
24763 /* Free the a:000 variables if they were allocated. */
24764 if (free_val)
24765 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24766 clear_tv(&li->li_tv);
24767
24768 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024769}
24770
24771/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024772 * Add a number variable "name" to dict "dp" with value "nr".
24773 */
24774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024775add_nr_var(
24776 dict_T *dp,
24777 dictitem_T *v,
24778 char *name,
24779 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024780{
24781 STRCPY(v->di_key, name);
24782 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24783 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24784 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024785 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024786 v->di_tv.vval.v_number = nr;
24787}
24788
24789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024790 * ":return [expr]"
24791 */
24792 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024793ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024794{
24795 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024796 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024797 int returning = FALSE;
24798
24799 if (current_funccal == NULL)
24800 {
24801 EMSG(_("E133: :return not inside a function"));
24802 return;
24803 }
24804
24805 if (eap->skip)
24806 ++emsg_skip;
24807
24808 eap->nextcmd = NULL;
24809 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024810 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024811 {
24812 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024813 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024815 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816 }
24817 /* It's safer to return also on error. */
24818 else if (!eap->skip)
24819 {
24820 /*
24821 * Return unless the expression evaluation has been cancelled due to an
24822 * aborting error, an interrupt, or an exception.
24823 */
24824 if (!aborting())
24825 returning = do_return(eap, FALSE, TRUE, NULL);
24826 }
24827
24828 /* When skipping or the return gets pending, advance to the next command
24829 * in this line (!returning). Otherwise, ignore the rest of the line.
24830 * Following lines will be ignored by get_func_line(). */
24831 if (returning)
24832 eap->nextcmd = NULL;
24833 else if (eap->nextcmd == NULL) /* no argument */
24834 eap->nextcmd = check_nextcmd(arg);
24835
24836 if (eap->skip)
24837 --emsg_skip;
24838}
24839
24840/*
24841 * Return from a function. Possibly makes the return pending. Also called
24842 * for a pending return at the ":endtry" or after returning from an extra
24843 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024844 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024845 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846 * FALSE when the return gets pending.
24847 */
24848 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024849do_return(
24850 exarg_T *eap,
24851 int reanimate,
24852 int is_cmd,
24853 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854{
24855 int idx;
24856 struct condstack *cstack = eap->cstack;
24857
24858 if (reanimate)
24859 /* Undo the return. */
24860 current_funccal->returned = FALSE;
24861
24862 /*
24863 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24864 * not in its finally clause (which then is to be executed next) is found.
24865 * In this case, make the ":return" pending for execution at the ":endtry".
24866 * Otherwise, return normally.
24867 */
24868 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24869 if (idx >= 0)
24870 {
24871 cstack->cs_pending[idx] = CSTP_RETURN;
24872
24873 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024874 /* A pending return again gets pending. "rettv" points to an
24875 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024876 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024877 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024878 else
24879 {
24880 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024881 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024882 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024883 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024885 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886 {
24887 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024888 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024889 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024890 else
24891 EMSG(_(e_outofmem));
24892 }
24893 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024894 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895
24896 if (reanimate)
24897 {
24898 /* The pending return value could be overwritten by a ":return"
24899 * without argument in a finally clause; reset the default
24900 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024901 current_funccal->rettv->v_type = VAR_NUMBER;
24902 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903 }
24904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024905 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906 }
24907 else
24908 {
24909 current_funccal->returned = TRUE;
24910
24911 /* If the return is carried out now, store the return value. For
24912 * a return immediately after reanimation, the value is already
24913 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024914 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024916 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024917 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024918 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024919 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920 }
24921 }
24922
24923 return idx < 0;
24924}
24925
24926/*
24927 * Free the variable with a pending return value.
24928 */
24929 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024930discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931{
Bram Moolenaar33570922005-01-25 22:26:29 +000024932 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933}
24934
24935/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024936 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 * is an allocated string. Used by report_pending() for verbose messages.
24938 */
24939 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024940get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024942 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024943 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024944 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024945
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024946 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024947 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024948 if (s == NULL)
24949 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024950
24951 STRCPY(IObuff, ":return ");
24952 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24953 if (STRLEN(s) + 8 >= IOSIZE)
24954 STRCPY(IObuff + IOSIZE - 4, "...");
24955 vim_free(tofree);
24956 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024957}
24958
24959/*
24960 * Get next function line.
24961 * Called by do_cmdline() to get the next line.
24962 * Returns allocated string, or NULL for end of function.
24963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024965get_func_line(
24966 int c UNUSED,
24967 void *cookie,
24968 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024969{
Bram Moolenaar33570922005-01-25 22:26:29 +000024970 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024971 ufunc_T *fp = fcp->func;
24972 char_u *retval;
24973 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024974
24975 /* If breakpoints have been added/deleted need to check for it. */
24976 if (fcp->dbg_tick != debug_tick)
24977 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024978 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024979 sourcing_lnum);
24980 fcp->dbg_tick = debug_tick;
24981 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024982#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024983 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024984 func_line_end(cookie);
24985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024986
Bram Moolenaar05159a02005-02-26 23:04:13 +000024987 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024988 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24989 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990 retval = NULL;
24991 else
24992 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024993 /* Skip NULL lines (continuation lines). */
24994 while (fcp->linenr < gap->ga_len
24995 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24996 ++fcp->linenr;
24997 if (fcp->linenr >= gap->ga_len)
24998 retval = NULL;
24999 else
25000 {
25001 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25002 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025003#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025004 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025005 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025006#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025008 }
25009
25010 /* Did we encounter a breakpoint? */
25011 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25012 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025013 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025014 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025015 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025016 sourcing_lnum);
25017 fcp->dbg_tick = debug_tick;
25018 }
25019
25020 return retval;
25021}
25022
Bram Moolenaar05159a02005-02-26 23:04:13 +000025023#if defined(FEAT_PROFILE) || defined(PROTO)
25024/*
25025 * Called when starting to read a function line.
25026 * "sourcing_lnum" must be correct!
25027 * When skipping lines it may not actually be executed, but we won't find out
25028 * until later and we need to store the time now.
25029 */
25030 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025031func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025032{
25033 funccall_T *fcp = (funccall_T *)cookie;
25034 ufunc_T *fp = fcp->func;
25035
25036 if (fp->uf_profiling && sourcing_lnum >= 1
25037 && sourcing_lnum <= fp->uf_lines.ga_len)
25038 {
25039 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025040 /* Skip continuation lines. */
25041 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25042 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025043 fp->uf_tml_execed = FALSE;
25044 profile_start(&fp->uf_tml_start);
25045 profile_zero(&fp->uf_tml_children);
25046 profile_get_wait(&fp->uf_tml_wait);
25047 }
25048}
25049
25050/*
25051 * Called when actually executing a function line.
25052 */
25053 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025054func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025055{
25056 funccall_T *fcp = (funccall_T *)cookie;
25057 ufunc_T *fp = fcp->func;
25058
25059 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25060 fp->uf_tml_execed = TRUE;
25061}
25062
25063/*
25064 * Called when done with a function line.
25065 */
25066 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025067func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025068{
25069 funccall_T *fcp = (funccall_T *)cookie;
25070 ufunc_T *fp = fcp->func;
25071
25072 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25073 {
25074 if (fp->uf_tml_execed)
25075 {
25076 ++fp->uf_tml_count[fp->uf_tml_idx];
25077 profile_end(&fp->uf_tml_start);
25078 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025079 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025080 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25081 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025082 }
25083 fp->uf_tml_idx = -1;
25084 }
25085}
25086#endif
25087
Bram Moolenaar071d4272004-06-13 20:20:40 +000025088/*
25089 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025090 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025091 */
25092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025093func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025094{
Bram Moolenaar33570922005-01-25 22:26:29 +000025095 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025096
25097 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25098 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025099 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025100 || fcp->returned);
25101}
25102
25103/*
25104 * return TRUE if cookie indicates a function which "abort"s on errors.
25105 */
25106 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025107func_has_abort(
25108 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025110 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025111}
25112
25113#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25114typedef enum
25115{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025116 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25117 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25118 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025119} var_flavour_T;
25120
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025121static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122
25123 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025124var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025125{
25126 char_u *p = varname;
25127
25128 if (ASCII_ISUPPER(*p))
25129 {
25130 while (*(++p))
25131 if (ASCII_ISLOWER(*p))
25132 return VAR_FLAVOUR_SESSION;
25133 return VAR_FLAVOUR_VIMINFO;
25134 }
25135 else
25136 return VAR_FLAVOUR_DEFAULT;
25137}
25138#endif
25139
25140#if defined(FEAT_VIMINFO) || defined(PROTO)
25141/*
25142 * Restore global vars that start with a capital from the viminfo file
25143 */
25144 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025145read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025146{
25147 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025148 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025149 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025150 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025151
25152 if (!writing && (find_viminfo_parameter('!') != NULL))
25153 {
25154 tab = vim_strchr(virp->vir_line + 1, '\t');
25155 if (tab != NULL)
25156 {
25157 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025158 switch (*tab)
25159 {
25160 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025161#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025162 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025163#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025164 case 'D': type = VAR_DICT; break;
25165 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025166 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025168
25169 tab = vim_strchr(tab, '\t');
25170 if (tab != NULL)
25171 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025172 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025173 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025174 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025176#ifdef FEAT_FLOAT
25177 else if (type == VAR_FLOAT)
25178 (void)string2float(tab + 1, &tv.vval.v_float);
25179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025180 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025181 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025182 if (type == VAR_DICT || type == VAR_LIST)
25183 {
25184 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25185
25186 if (etv == NULL)
25187 /* Failed to parse back the dict or list, use it as a
25188 * string. */
25189 tv.v_type = VAR_STRING;
25190 else
25191 {
25192 vim_free(tv.vval.v_string);
25193 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025194 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025195 }
25196 }
25197
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025198 /* when in a function use global variables */
25199 save_funccal = current_funccal;
25200 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025201 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025202 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025203
25204 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025205 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025206 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25207 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025208 }
25209 }
25210 }
25211
25212 return viminfo_readline(virp);
25213}
25214
25215/*
25216 * Write global vars that start with a capital to the viminfo file
25217 */
25218 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025219write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220{
Bram Moolenaar33570922005-01-25 22:26:29 +000025221 hashitem_T *hi;
25222 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025223 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025224 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025225 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025226 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025227 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228
25229 if (find_viminfo_parameter('!') == NULL)
25230 return;
25231
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025232 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025233
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025234 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025235 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025237 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025238 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025239 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025240 this_var = HI2DI(hi);
25241 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025242 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025243 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025244 {
25245 case VAR_STRING: s = "STR"; break;
25246 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025247 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025248 case VAR_DICT: s = "DIC"; break;
25249 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025250 case VAR_SPECIAL: s = "XPL"; break;
25251
25252 case VAR_UNKNOWN:
25253 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025254 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025255 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025256 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025257 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025258 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025259 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025260 if (p != NULL)
25261 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025262 vim_free(tofree);
25263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264 }
25265 }
25266}
25267#endif
25268
25269#if defined(FEAT_SESSION) || defined(PROTO)
25270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025271store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025272{
Bram Moolenaar33570922005-01-25 22:26:29 +000025273 hashitem_T *hi;
25274 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025275 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025276 char_u *p, *t;
25277
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025278 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025279 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025281 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025282 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025283 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025284 this_var = HI2DI(hi);
25285 if ((this_var->di_tv.v_type == VAR_NUMBER
25286 || this_var->di_tv.v_type == VAR_STRING)
25287 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025288 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025289 /* Escape special characters with a backslash. Turn a LF and
25290 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025291 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025292 (char_u *)"\\\"\n\r");
25293 if (p == NULL) /* out of memory */
25294 break;
25295 for (t = p; *t != NUL; ++t)
25296 if (*t == '\n')
25297 *t = 'n';
25298 else if (*t == '\r')
25299 *t = 'r';
25300 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025301 this_var->di_key,
25302 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25303 : ' ',
25304 p,
25305 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25306 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025307 || put_eol(fd) == FAIL)
25308 {
25309 vim_free(p);
25310 return FAIL;
25311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312 vim_free(p);
25313 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025314#ifdef FEAT_FLOAT
25315 else if (this_var->di_tv.v_type == VAR_FLOAT
25316 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25317 {
25318 float_T f = this_var->di_tv.vval.v_float;
25319 int sign = ' ';
25320
25321 if (f < 0)
25322 {
25323 f = -f;
25324 sign = '-';
25325 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025326 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025327 this_var->di_key, sign, f) < 0)
25328 || put_eol(fd) == FAIL)
25329 return FAIL;
25330 }
25331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332 }
25333 }
25334 return OK;
25335}
25336#endif
25337
Bram Moolenaar661b1822005-07-28 22:36:45 +000025338/*
25339 * Display script name where an item was last set.
25340 * Should only be invoked when 'verbose' is non-zero.
25341 */
25342 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025343last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025344{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025345 char_u *p;
25346
Bram Moolenaar661b1822005-07-28 22:36:45 +000025347 if (scriptID != 0)
25348 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025349 p = home_replace_save(NULL, get_scriptname(scriptID));
25350 if (p != NULL)
25351 {
25352 verbose_enter();
25353 MSG_PUTS(_("\n\tLast set from "));
25354 MSG_PUTS(p);
25355 vim_free(p);
25356 verbose_leave();
25357 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025358 }
25359}
25360
Bram Moolenaard812df62008-11-09 12:46:09 +000025361/*
25362 * List v:oldfiles in a nice way.
25363 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025365ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025366{
25367 list_T *l = vimvars[VV_OLDFILES].vv_list;
25368 listitem_T *li;
25369 int nr = 0;
25370
25371 if (l == NULL)
25372 msg((char_u *)_("No old files"));
25373 else
25374 {
25375 msg_start();
25376 msg_scroll = TRUE;
25377 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25378 {
25379 msg_outnum((long)++nr);
25380 MSG_PUTS(": ");
25381 msg_outtrans(get_tv_string(&li->li_tv));
25382 msg_putchar('\n');
25383 out_flush(); /* output one line at a time */
25384 ui_breakcheck();
25385 }
25386 /* Assume "got_int" was set to truncate the listing. */
25387 got_int = FALSE;
25388
25389#ifdef FEAT_BROWSE_CMD
25390 if (cmdmod.browse)
25391 {
25392 quit_more = FALSE;
25393 nr = prompt_for_number(FALSE);
25394 msg_starthere();
25395 if (nr > 0)
25396 {
25397 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25398 (long)nr);
25399
25400 if (p != NULL)
25401 {
25402 p = expand_env_save(p);
25403 eap->arg = p;
25404 eap->cmdidx = CMD_edit;
25405 cmdmod.browse = FALSE;
25406 do_exedit(eap, NULL);
25407 vim_free(p);
25408 }
25409 }
25410 }
25411#endif
25412 }
25413}
25414
Bram Moolenaar53744302015-07-17 17:38:22 +020025415/* reset v:option_new, v:option_old and v:option_type */
25416 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025417reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025418{
25419 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25420 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25421 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25422}
25423
25424
Bram Moolenaar071d4272004-06-13 20:20:40 +000025425#endif /* FEAT_EVAL */
25426
Bram Moolenaar071d4272004-06-13 20:20:40 +000025427
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025428#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025429
25430#ifdef WIN3264
25431/*
25432 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25433 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025434static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25435static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25436static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025437
25438/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025439 * Get the short path (8.3) for the filename in "fnamep".
25440 * Only works for a valid file name.
25441 * When the path gets longer "fnamep" is changed and the allocated buffer
25442 * is put in "bufp".
25443 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25444 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025445 */
25446 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025447get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025448{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025449 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025450 char_u *newbuf;
25451
25452 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025453 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025454 if (l > len - 1)
25455 {
25456 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025457 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458 newbuf = vim_strnsave(*fnamep, l);
25459 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025460 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461
25462 vim_free(*bufp);
25463 *fnamep = *bufp = newbuf;
25464
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025465 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025466 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467 }
25468
25469 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025470 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471}
25472
25473/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025474 * Get the short path (8.3) for the filename in "fname". The converted
25475 * path is returned in "bufp".
25476 *
25477 * Some of the directories specified in "fname" may not exist. This function
25478 * will shorten the existing directories at the beginning of the path and then
25479 * append the remaining non-existing path.
25480 *
25481 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025482 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025483 * bufp - Pointer to an allocated buffer for the filename.
25484 * fnamelen - Length of the filename pointed to by fname
25485 *
25486 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025487 */
25488 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025489shortpath_for_invalid_fname(
25490 char_u **fname,
25491 char_u **bufp,
25492 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025494 char_u *short_fname, *save_fname, *pbuf_unused;
25495 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025496 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025497 int old_len, len;
25498 int new_len, sfx_len;
25499 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500
25501 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025502 old_len = *fnamelen;
25503 save_fname = vim_strnsave(*fname, old_len);
25504 pbuf_unused = NULL;
25505 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025506
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025507 endp = save_fname + old_len - 1; /* Find the end of the copy */
25508 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025510 /*
25511 * Try shortening the supplied path till it succeeds by removing one
25512 * directory at a time from the tail of the path.
25513 */
25514 len = 0;
25515 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025517 /* go back one path-separator */
25518 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25519 --endp;
25520 if (endp <= save_fname)
25521 break; /* processed the complete path */
25522
25523 /*
25524 * Replace the path separator with a NUL and try to shorten the
25525 * resulting path.
25526 */
25527 ch = *endp;
25528 *endp = 0;
25529 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025530 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025531 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25532 {
25533 retval = FAIL;
25534 goto theend;
25535 }
25536 *endp = ch; /* preserve the string */
25537
25538 if (len > 0)
25539 break; /* successfully shortened the path */
25540
25541 /* failed to shorten the path. Skip the path separator */
25542 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025543 }
25544
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025545 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025546 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025547 /*
25548 * Succeeded in shortening the path. Now concatenate the shortened
25549 * path with the remaining path at the tail.
25550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025552 /* Compute the length of the new path. */
25553 sfx_len = (int)(save_endp - endp) + 1;
25554 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025555
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025556 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025557 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025558 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025560 /* There is not enough space in the currently allocated string,
25561 * copy it to a buffer big enough. */
25562 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025563 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025564 {
25565 retval = FAIL;
25566 goto theend;
25567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568 }
25569 else
25570 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025571 /* Transfer short_fname to the main buffer (it's big enough),
25572 * unless get_short_pathname() did its work in-place. */
25573 *fname = *bufp = save_fname;
25574 if (short_fname != save_fname)
25575 vim_strncpy(save_fname, short_fname, len);
25576 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025578
25579 /* concat the not-shortened part of the path */
25580 vim_strncpy(*fname + len, endp, sfx_len);
25581 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025582 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025583
25584theend:
25585 vim_free(pbuf_unused);
25586 vim_free(save_fname);
25587
25588 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025589}
25590
25591/*
25592 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025593 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594 */
25595 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025596shortpath_for_partial(
25597 char_u **fnamep,
25598 char_u **bufp,
25599 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025600{
25601 int sepcount, len, tflen;
25602 char_u *p;
25603 char_u *pbuf, *tfname;
25604 int hasTilde;
25605
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025606 /* Count up the path separators from the RHS.. so we know which part
25607 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025609 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025610 if (vim_ispathsep(*p))
25611 ++sepcount;
25612
25613 /* Need full path first (use expand_env() to remove a "~/") */
25614 hasTilde = (**fnamep == '~');
25615 if (hasTilde)
25616 pbuf = tfname = expand_env_save(*fnamep);
25617 else
25618 pbuf = tfname = FullName_save(*fnamep, FALSE);
25619
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025620 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025621
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025622 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25623 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025624
25625 if (len == 0)
25626 {
25627 /* Don't have a valid filename, so shorten the rest of the
25628 * path if we can. This CAN give us invalid 8.3 filenames, but
25629 * there's not a lot of point in guessing what it might be.
25630 */
25631 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025632 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25633 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025634 }
25635
25636 /* Count the paths backward to find the beginning of the desired string. */
25637 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025638 {
25639#ifdef FEAT_MBYTE
25640 if (has_mbyte)
25641 p -= mb_head_off(tfname, p);
25642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025643 if (vim_ispathsep(*p))
25644 {
25645 if (sepcount == 0 || (hasTilde && sepcount == 1))
25646 break;
25647 else
25648 sepcount --;
25649 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025651 if (hasTilde)
25652 {
25653 --p;
25654 if (p >= tfname)
25655 *p = '~';
25656 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025657 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658 }
25659 else
25660 ++p;
25661
25662 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25663 vim_free(*bufp);
25664 *fnamelen = (int)STRLEN(p);
25665 *bufp = pbuf;
25666 *fnamep = p;
25667
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025668 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025669}
25670#endif /* WIN3264 */
25671
25672/*
25673 * Adjust a filename, according to a string of modifiers.
25674 * *fnamep must be NUL terminated when called. When returning, the length is
25675 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025676 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025677 * When there is an error, *fnamep is set to NULL.
25678 */
25679 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025680modify_fname(
25681 char_u *src, /* string with modifiers */
25682 int *usedlen, /* characters after src that are used */
25683 char_u **fnamep, /* file name so far */
25684 char_u **bufp, /* buffer for allocated file name or NULL */
25685 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686{
25687 int valid = 0;
25688 char_u *tail;
25689 char_u *s, *p, *pbuf;
25690 char_u dirname[MAXPATHL];
25691 int c;
25692 int has_fullname = 0;
25693#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025694 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025695 int has_shortname = 0;
25696#endif
25697
25698repeat:
25699 /* ":p" - full path/file_name */
25700 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25701 {
25702 has_fullname = 1;
25703
25704 valid |= VALID_PATH;
25705 *usedlen += 2;
25706
25707 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25708 if ((*fnamep)[0] == '~'
25709#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25710 && ((*fnamep)[1] == '/'
25711# ifdef BACKSLASH_IN_FILENAME
25712 || (*fnamep)[1] == '\\'
25713# endif
25714 || (*fnamep)[1] == NUL)
25715
25716#endif
25717 )
25718 {
25719 *fnamep = expand_env_save(*fnamep);
25720 vim_free(*bufp); /* free any allocated file name */
25721 *bufp = *fnamep;
25722 if (*fnamep == NULL)
25723 return -1;
25724 }
25725
25726 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025727 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025728 {
25729 if (vim_ispathsep(*p)
25730 && p[1] == '.'
25731 && (p[2] == NUL
25732 || vim_ispathsep(p[2])
25733 || (p[2] == '.'
25734 && (p[3] == NUL || vim_ispathsep(p[3])))))
25735 break;
25736 }
25737
25738 /* FullName_save() is slow, don't use it when not needed. */
25739 if (*p != NUL || !vim_isAbsName(*fnamep))
25740 {
25741 *fnamep = FullName_save(*fnamep, *p != NUL);
25742 vim_free(*bufp); /* free any allocated file name */
25743 *bufp = *fnamep;
25744 if (*fnamep == NULL)
25745 return -1;
25746 }
25747
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025748#ifdef WIN3264
25749# if _WIN32_WINNT >= 0x0500
25750 if (vim_strchr(*fnamep, '~') != NULL)
25751 {
25752 /* Expand 8.3 filename to full path. Needed to make sure the same
25753 * file does not have two different names.
25754 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25755 p = alloc(_MAX_PATH + 1);
25756 if (p != NULL)
25757 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025758 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025759 {
25760 vim_free(*bufp);
25761 *bufp = *fnamep = p;
25762 }
25763 else
25764 vim_free(p);
25765 }
25766 }
25767# endif
25768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769 /* Append a path separator to a directory. */
25770 if (mch_isdir(*fnamep))
25771 {
25772 /* Make room for one or two extra characters. */
25773 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25774 vim_free(*bufp); /* free any allocated file name */
25775 *bufp = *fnamep;
25776 if (*fnamep == NULL)
25777 return -1;
25778 add_pathsep(*fnamep);
25779 }
25780 }
25781
25782 /* ":." - path relative to the current directory */
25783 /* ":~" - path relative to the home directory */
25784 /* ":8" - shortname path - postponed till after */
25785 while (src[*usedlen] == ':'
25786 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25787 {
25788 *usedlen += 2;
25789 if (c == '8')
25790 {
25791#ifdef WIN3264
25792 has_shortname = 1; /* Postpone this. */
25793#endif
25794 continue;
25795 }
25796 pbuf = NULL;
25797 /* Need full path first (use expand_env() to remove a "~/") */
25798 if (!has_fullname)
25799 {
25800 if (c == '.' && **fnamep == '~')
25801 p = pbuf = expand_env_save(*fnamep);
25802 else
25803 p = pbuf = FullName_save(*fnamep, FALSE);
25804 }
25805 else
25806 p = *fnamep;
25807
25808 has_fullname = 0;
25809
25810 if (p != NULL)
25811 {
25812 if (c == '.')
25813 {
25814 mch_dirname(dirname, MAXPATHL);
25815 s = shorten_fname(p, dirname);
25816 if (s != NULL)
25817 {
25818 *fnamep = s;
25819 if (pbuf != NULL)
25820 {
25821 vim_free(*bufp); /* free any allocated file name */
25822 *bufp = pbuf;
25823 pbuf = NULL;
25824 }
25825 }
25826 }
25827 else
25828 {
25829 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25830 /* Only replace it when it starts with '~' */
25831 if (*dirname == '~')
25832 {
25833 s = vim_strsave(dirname);
25834 if (s != NULL)
25835 {
25836 *fnamep = s;
25837 vim_free(*bufp);
25838 *bufp = s;
25839 }
25840 }
25841 }
25842 vim_free(pbuf);
25843 }
25844 }
25845
25846 tail = gettail(*fnamep);
25847 *fnamelen = (int)STRLEN(*fnamep);
25848
25849 /* ":h" - head, remove "/file_name", can be repeated */
25850 /* Don't remove the first "/" or "c:\" */
25851 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25852 {
25853 valid |= VALID_HEAD;
25854 *usedlen += 2;
25855 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025856 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025857 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858 *fnamelen = (int)(tail - *fnamep);
25859#ifdef VMS
25860 if (*fnamelen > 0)
25861 *fnamelen += 1; /* the path separator is part of the path */
25862#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025863 if (*fnamelen == 0)
25864 {
25865 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25866 p = vim_strsave((char_u *)".");
25867 if (p == NULL)
25868 return -1;
25869 vim_free(*bufp);
25870 *bufp = *fnamep = tail = p;
25871 *fnamelen = 1;
25872 }
25873 else
25874 {
25875 while (tail > s && !after_pathsep(s, tail))
25876 mb_ptr_back(*fnamep, tail);
25877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025878 }
25879
25880 /* ":8" - shortname */
25881 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25882 {
25883 *usedlen += 2;
25884#ifdef WIN3264
25885 has_shortname = 1;
25886#endif
25887 }
25888
25889#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025890 /*
25891 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025892 */
25893 if (has_shortname)
25894 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025895 /* Copy the string if it is shortened by :h and when it wasn't copied
25896 * yet, because we are going to change it in place. Avoids changing
25897 * the buffer name for "%:8". */
25898 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025899 {
25900 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025901 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025902 return -1;
25903 vim_free(*bufp);
25904 *bufp = *fnamep = p;
25905 }
25906
25907 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025908 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025909 if (!has_fullname && !vim_isAbsName(*fnamep))
25910 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025911 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025912 return -1;
25913 }
25914 else
25915 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025916 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025917
Bram Moolenaardc935552011-08-17 15:23:23 +020025918 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025919 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025920 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025921 return -1;
25922
25923 if (l == 0)
25924 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025925 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025927 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025928 return -1;
25929 }
25930 *fnamelen = l;
25931 }
25932 }
25933#endif /* WIN3264 */
25934
25935 /* ":t" - tail, just the basename */
25936 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25937 {
25938 *usedlen += 2;
25939 *fnamelen -= (int)(tail - *fnamep);
25940 *fnamep = tail;
25941 }
25942
25943 /* ":e" - extension, can be repeated */
25944 /* ":r" - root, without extension, can be repeated */
25945 while (src[*usedlen] == ':'
25946 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25947 {
25948 /* find a '.' in the tail:
25949 * - for second :e: before the current fname
25950 * - otherwise: The last '.'
25951 */
25952 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25953 s = *fnamep - 2;
25954 else
25955 s = *fnamep + *fnamelen - 1;
25956 for ( ; s > tail; --s)
25957 if (s[0] == '.')
25958 break;
25959 if (src[*usedlen + 1] == 'e') /* :e */
25960 {
25961 if (s > tail)
25962 {
25963 *fnamelen += (int)(*fnamep - (s + 1));
25964 *fnamep = s + 1;
25965#ifdef VMS
25966 /* cut version from the extension */
25967 s = *fnamep + *fnamelen - 1;
25968 for ( ; s > *fnamep; --s)
25969 if (s[0] == ';')
25970 break;
25971 if (s > *fnamep)
25972 *fnamelen = s - *fnamep;
25973#endif
25974 }
25975 else if (*fnamep <= tail)
25976 *fnamelen = 0;
25977 }
25978 else /* :r */
25979 {
25980 if (s > tail) /* remove one extension */
25981 *fnamelen = (int)(s - *fnamep);
25982 }
25983 *usedlen += 2;
25984 }
25985
25986 /* ":s?pat?foo?" - substitute */
25987 /* ":gs?pat?foo?" - global substitute */
25988 if (src[*usedlen] == ':'
25989 && (src[*usedlen + 1] == 's'
25990 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25991 {
25992 char_u *str;
25993 char_u *pat;
25994 char_u *sub;
25995 int sep;
25996 char_u *flags;
25997 int didit = FALSE;
25998
25999 flags = (char_u *)"";
26000 s = src + *usedlen + 2;
26001 if (src[*usedlen + 1] == 'g')
26002 {
26003 flags = (char_u *)"g";
26004 ++s;
26005 }
26006
26007 sep = *s++;
26008 if (sep)
26009 {
26010 /* find end of pattern */
26011 p = vim_strchr(s, sep);
26012 if (p != NULL)
26013 {
26014 pat = vim_strnsave(s, (int)(p - s));
26015 if (pat != NULL)
26016 {
26017 s = p + 1;
26018 /* find end of substitution */
26019 p = vim_strchr(s, sep);
26020 if (p != NULL)
26021 {
26022 sub = vim_strnsave(s, (int)(p - s));
26023 str = vim_strnsave(*fnamep, *fnamelen);
26024 if (sub != NULL && str != NULL)
26025 {
26026 *usedlen = (int)(p + 1 - src);
26027 s = do_string_sub(str, pat, sub, flags);
26028 if (s != NULL)
26029 {
26030 *fnamep = s;
26031 *fnamelen = (int)STRLEN(s);
26032 vim_free(*bufp);
26033 *bufp = s;
26034 didit = TRUE;
26035 }
26036 }
26037 vim_free(sub);
26038 vim_free(str);
26039 }
26040 vim_free(pat);
26041 }
26042 }
26043 /* after using ":s", repeat all the modifiers */
26044 if (didit)
26045 goto repeat;
26046 }
26047 }
26048
Bram Moolenaar26df0922014-02-23 23:39:13 +010026049 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26050 {
26051 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26052 if (p == NULL)
26053 return -1;
26054 vim_free(*bufp);
26055 *bufp = *fnamep = p;
26056 *fnamelen = (int)STRLEN(p);
26057 *usedlen += 2;
26058 }
26059
Bram Moolenaar071d4272004-06-13 20:20:40 +000026060 return valid;
26061}
26062
26063/*
26064 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26065 * "flags" can be "g" to do a global substitute.
26066 * Returns an allocated string, NULL for error.
26067 */
26068 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026069do_string_sub(
26070 char_u *str,
26071 char_u *pat,
26072 char_u *sub,
26073 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026074{
26075 int sublen;
26076 regmatch_T regmatch;
26077 int i;
26078 int do_all;
26079 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026080 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026081 garray_T ga;
26082 char_u *ret;
26083 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026084 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026085
26086 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26087 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026088 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026089
26090 ga_init2(&ga, 1, 200);
26091
26092 do_all = (flags[0] == 'g');
26093
26094 regmatch.rm_ic = p_ic;
26095 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26096 if (regmatch.regprog != NULL)
26097 {
26098 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026099 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026100 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26101 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026102 /* Skip empty match except for first match. */
26103 if (regmatch.startp[0] == regmatch.endp[0])
26104 {
26105 if (zero_width == regmatch.startp[0])
26106 {
26107 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026108 i = MB_PTR2LEN(tail);
26109 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26110 (size_t)i);
26111 ga.ga_len += i;
26112 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026113 continue;
26114 }
26115 zero_width = regmatch.startp[0];
26116 }
26117
Bram Moolenaar071d4272004-06-13 20:20:40 +000026118 /*
26119 * Get some space for a temporary buffer to do the substitution
26120 * into. It will contain:
26121 * - The text up to where the match is.
26122 * - The substituted text.
26123 * - The text after the match.
26124 */
26125 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026126 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026127 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26128 {
26129 ga_clear(&ga);
26130 break;
26131 }
26132
26133 /* copy the text up to where the match is */
26134 i = (int)(regmatch.startp[0] - tail);
26135 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26136 /* add the substituted text */
26137 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26138 + ga.ga_len + i, TRUE, TRUE, FALSE);
26139 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026140 tail = regmatch.endp[0];
26141 if (*tail == NUL)
26142 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026143 if (!do_all)
26144 break;
26145 }
26146
26147 if (ga.ga_data != NULL)
26148 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26149
Bram Moolenaar473de612013-06-08 18:19:48 +020026150 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026151 }
26152
26153 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26154 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026155 if (p_cpo == empty_option)
26156 p_cpo = save_cpo;
26157 else
26158 /* Darn, evaluating {sub} expression changed the value. */
26159 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160
26161 return ret;
26162}
26163
26164#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */